// Start Scripts

$(document).ready(function(){
						   
						   
		// Random id creation for each visitor from 1 to 10.000.000
		var numRand = Math.floor(Math.random()*10000001);
		
		// Trigger the tabs
		var $tabs = $('#tabs').tabs();
		
		//Google Conversion Function

		var googleConv = function(){
		var image = new Image(1,1);
		image.src = "http://www.googleadservices.com/pagead/conversion/1072657298/?label=lead&amp;guid=ON&amp;script=0";
		return;
		}
		
		//Special tabs function
		function goToTab() {
		$tabs.tabs('select', $tabToOpen);
		}
		
		$.validator.addMethod('phone', function(value) { return (value.match(/^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]{1,12}){1,2}(\s*(ext|x)\s*\.?:?\s*([0-9]+))?$/)); }, 'Please enter a valid phone number (Intl format accepted + ext: or x:)');


		// Close related videos link
		$('#relclose').click(function() {
			$("#relatedVids").slideUp();
			return false;
		});
		
		// Back to video link
		$('.back2vid').click(function() {
			$tabs.tabs('select', 0);
			if ($videotype == 0) {
			setTimeout( function() {
			var player = document.getElementById("videoPlayer");
			jwplayer(player).play();
			}, 300); 
			}
			return false;
		});
		
		// Privacy Policy link
		$('.privpol').click(function() {
			$tabs.tabs('select', 5);
			if ($videotype == 0) {
			setTimeout( function() {
			var player = document.getElementById("videoPlayer");
			jwplayer(player).play();
			}, 300);
			}
			return false;
		});
		
		// Privacy Policy link on Registration page
		$('#privpolreg').click(function() {
			$("#register").hide();
			$("#privpol-register").slideDown();
			return false;
		});
		
		// Privacy Policy link on Registration page
		$('#back2reg').click(function() {
			$("#privpol-register").hide();
			$("#register").slideDown();
			return false;
		});
		
		//Pause video
		$('.tablink').click(function() {
			if ($videotype == 0) {
			setTimeout( function() {
			var player = document.getElementById("videoPlayer");
			jwplayer(player).play();
			}, 300);
			}
			return false;
		});
		
		// Verify if the cookie exists
		if($.cookie("CAFMEO")) { 
		var cafmeo_data = $.cookie("CAFMEO").split(",");
		$("#email").val(cafmeo_data[0]);
		$("#email2").val(cafmeo_data[0]);
		$("#email3").val(cafmeo_data[0]);
		}
		
		if ($freeToView == 1) {
			$("#tabs").show();
			goToTab();
			$tabs.tabs('select', 0);
		} else {
		// Verify if the cookie exists
		if($.cookie("CAFM")) { 
		var cookie_data = $.cookie("CAFM").split(",");
		} else {
		var cookie_data = 0;
		}
		if(cookie_data[0] == "OK")
		{
			// If it exists
			$("#tabs").show();
			goToTab();
			
			// Load from the database via Ajax the values needed, matching cookie_data[1]
			$.ajax({
   				type: "POST",
   				url: "php/mysql_query.php",
   				data: "numrand="+cookie_data[1],
   				success: function(allfields){
									
     				var field = allfields.split(",");
					//alert(field[6]);
					$("#fullname2").val(field[1]);
					$("#fullname3").val(field[1]);
					
					$("#email2").val(field[2]);
					$("#email3").val(field[2]);
					
					$("#company2").val(field[3]);
					//$("#area2").val(field[4]);
					//$("#telephone2").val(field[4]);
					//$("#country2").val(field[6]);
   				}
 			});
		}
		else
		{
			// If it doesn't exist
			$("#register-form").show();
		}
		}

		
		$("#form-register").validate({	// Validation for the Register form
			submitHandler: function() {
				
				$('#button-register').attr('disabled', 'disabled');
				googleConv();
				
				$.ajax({
   					type: "POST",
   					url: "php/mysql_register.php",
					data: ({
						fullname : document.getElementById("fullname").value,
						email : document.getElementById("email").value,
						company : document.getElementById("company").value,
						telephone : document.getElementById("area").value+document.getElementById("telephone").value,
						country : document.getElementById("country").value,
						howfind : document.getElementById("howfind").value,
						numrand : numRand
					}),
   					success: function(){
     					//alert("Data Saved in Database!");
						$.cookie("CAFM", "OK,"+numRand, { path: "/", expires: 30 }); // Set the cookie
						//alert('Cookie was set!')
						$("#register").hide();
						$("#confirm-register").slideDown();
						setTimeout( function() {
						$("#register-form").hide(); // Hide the registration form
						$("#tabs").show(); // Show the video and the tabs
						}, 3000);
						
						// Load from the database via Ajax the values needed, matching cookie_data[1]
						var cookie_data = $.cookie("CAFM").split(",");
						$.ajax({
							type: "POST",
							url: "php/mysql_query.php",
							data: "numrand="+cookie_data[1],
							success: function(allfields){
									
							var field = allfields.split(",");
							//alert(field[0]);
							$("#fullname2").val(field[1]);
							$("#fullname3").val(field[1]);
							
							$("#email2").val(field[2]);
							$("#email3").val(field[2]);
							
							$("#company2").val(field[3]);
							$("#area2").val(field[4]);
							$("#telephone2").val(field[5]);
							$("#country2").val(field[6]);
							
							}
							
						});
						
   					}
 				});
			}
		});
		
		$("#form-regemail").validate({	// Validation for the Register form
			submitHandler: function() {
				
				$('#button-regemail').attr('disabled', 'disabled');
				googleConv();
				
				$.ajax({
   					type: "POST",
   					url: "php/mysql_register.php",
					data: ({
						emailonly : document.getElementById("emailonly").value,
						country : document.getElementById("country").value,
						numrand : numRand
					}),
   					success: function(){
     					//alert("Data Saved in Database!");
						var emailonly = document.getElementById("emailonly").value;
						$.cookie("CAFMEO", emailonly+","+numRand, { path: "/", expires: 30 }); // Set the cookie
						//alert('Cookie was set!')
						$("#email2").val(emailonly);
						$("#email3").val(emailonly);
						$("#register").hide();
						$("#confirm-register").slideDown();
						setTimeout( function() {
						$("#register-form").hide(); // Hide the registration form
						$("#tabs").show(); // Show the video and the tabs
						}, 3000);					
   					}
 				});
			}
		});
		
		$("#form-bookademo").validate({	// Validation for the "Book a demo" form
			submitHandler: function() {
				
				// Load from the database via Ajax the values needed, matching cookie_data[1]
				if($.cookie("CAFM")) { 
				var cookie_data = $.cookie("CAFM").split(",");
				} else {
				var cookie_data = 0;
				}
				$('#button-demo').attr('disabled', 'disabled');
				googleConv();
				
				$.ajax({
   					type: "POST",
   					url: "php/mysql_register.php",
					data: ({
						fullname2 : document.getElementById("fullname2").value,
						email2 : document.getElementById("email2").value,
						company2 : document.getElementById("company2").value,
						telephone2 : document.getElementById("area2").value+document.getElementById("telephone2").value,
						country2 : document.getElementById("country2").value,
						numrand : cookie_data[1]
					}),
   					success: function(){
						// Registered correctly
     					//alert("Data Saved!");
						$("#bookademo").hide();
						$("#confirm-bookademo").slideDown();
						setTimeout( function() {
						$tabs.tabs('select', 0);
						}, 3000);
   					}
 				});
			}
		});
		
		$("#form-askaquestion").validate({	// Validation for the "Ask a question" form
			submitHandler: function() {
				
				// Load from the database via Ajax the values needed, matching cookie_data[1]
				if($.cookie("CAFM")) { 
				var cookie_data = $.cookie("CAFM").split(",");
				} else {
				var cookie_data = 0;
				}
				$('#button-question').attr('disabled', 'disabled');
				googleConv();
				
				$.ajax({
   					type: "POST",
   					url: "php/mysql_register.php",
					data: ({
						fullname3 : document.getElementById("fullname3").value,
						email3 : document.getElementById("email3").value,
						question : document.getElementById("question").value,
						numrand : cookie_data[1]
					}),
   					success: function(){
						// Registered correctly
     					//alert("Data Saved!");
						$("#askaquestion").hide();
						$("#confirm-askaquestion").slideDown();
						setTimeout( function() {
						$tabs.tabs('select', 0);
						}, 3000);
   					}
 				});
			}
		});
		
	});
