﻿function validEmail()
{	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var email_address = $("#email").val();
	if(reg.test(email_address) == false) 
	  return false;
	else
		return true;
}

function validPhone(phone_field)
{	
	var area_codes = "02,03,04,08,09,072,073,076,077,078,079,050,052,054,057";
	var phone_number = document.getElementById(phone_field).value;
	var pre_code = document.getElementById("pre_phone_code").value;

	phone_number = pre_code + phone_number;

	var valid = false;
	// Check for correct phone number
	 var rePhoneNumber = new RegExp(/^([0-9]{7})$/);
	 var phone_areas = area_codes.split(",");
	 for(area_code=0;area_code<phone_areas.length;area_code++)
	 {
		if(phone_number.substr(0, phone_areas[area_code].length) == phone_areas[area_code])
		{
			full_phone = phone_number.substr(phone_areas[area_code].length);
			if(rePhoneNumber.test(full_phone))
			{
				valid = true;
			}
		}//If area code found
	 }//For each area code in list
	return valid;
}
function submit_it(){
	$('#leadbox_form').submit()
}


	function check_leadbox()
	{
		$("#fullname_alert").hide();
		$("#pre_phone_code_alert").hide();
		$("#pre_phone_alert").hide();
		$("#email_alert").hide();
		
		if ($("#fullname").val() == "")
		{
			$("#fullname_alert").show();
			$("#fullname").focus();
			return false;
		}

		if ($("#pre_phone_code").val() == "")
		{	
			$("#pre_phone_code").focus();
			$("#pre_phone_code_alert").show();
			return;
		}
		
		if (!validPhone('pre_phone'))
		{	
			$("#pre_phone").focus();
			$("#pre_phone_alert").show();
			return;
		}

		if(!validEmail())
		{
			$("#email").focus();
			$("#email_alert").show();
			return;
		}
		
		if ($('#have_visa').attr('checked'))
			var have_visa = true;
		else
			var have_visa = false;
		// Got to final stage event
		_gaq.push(['_trackEvent', 'confirm box', 'Got go finals stage','',1]);
		// Confirm the phone number - then submit
		jConfirm('האם '+ $("#pre_phone_code").val() + $("#pre_phone").val() +' הוא אכן הטלפון שלך?','לוודא מספר טלפון!', function(r) {
			if(r == true)
			{
				_gaq.push(['_trackEvent', 'confirm box', 'good phone','',1]);
				//if(have_visa == false)
				//{
					
					params ='fullname='+encodeURIComponent ($('#fullname').val())+'&pre_phone='+$('#pre_phone').val()+'&pre_phone_code='+$('#pre_phone_code').val() +'&email='+$('#email').val() +'&country='+encodeURIComponent($('#country').val())+'&pref_work='+encodeURIComponent($('#pref_work').val())+"&media="+$('#camp_id').val();
					document.getElementById("terminal_div").innerHTML="<iframe src='http://www.terminal.co.il/jim/kaitzmini/world/mail_3dot_ext.php?" + params+"' id='iframe_terminal2' frameborder='0' scrolling='no' style='width: 1px; height:1px'></iframe>";
				//}
				setTimeout ( submit_it, 1800 );
			}
			else
			{
				_gaq.push(['_trackEvent', 'confirm box', 'bad phone','',1]);
				$("#pre_phone").focus();
				$("#fix_phone_alert").show();
			}
		});		
	};


