$(document).ready(function() {
	hideAll();
	$(".error").hide();
	
	$("#submit_btn_registration").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		if(!$("#courses_0").is(':checked') && !$("#courses_1").is(':checked') && !$("#courses_2").is(':checked') && !$("#courses_3").is(':checked') && !$("#courses_4").is(':checked') && !$("#courses_5").is(':checked') && !$("#courses_6").is(':checked') && !$("#courses_7").is(':checked') && !$("#courses_8").is(':checked') && !$("#courses_9").is(':checked') && !$("#courses_10").is(':checked') && !$("#courses_11").is(':checked') && !$("#courses_12").is(':checked') && !$("#courses_13").is(':checked') && !$("#courses_14").is(':checked') && !$("#courses_15").is(':checked') ){
			$("label#courses_error").show();  
			$(':input:first').focus();

       		//$("input#courses_0").focus();  
			hasError = true;
		}



		var course_sdVal = $("#course_sd").val();
		if($("#course_sd").attr("selectedIndex") < 1) {
			$("label#course_sd_error").show();  
       		$("input#course_sd").focus();  
			hasError = true;
		}


		var nationalityVal = $("#nationality").val();
		if(nationalityVal == '') {
			$("label#nationality_error").show();  
       		$("input#nationality").focus();  
			hasError = true;
		}

		var passportVal = $("#passport").val();
		if(passportVal == '') {
			$("label#passport_error").show();  
       		$("input#passport").focus();  
			hasError = true;
		}

		var lastnameVal = $("#lastname").val();
		if(lastnameVal == '') {
			$("label#lastname_error").show();  
       		$("input#lastname").focus();  
			hasError = true;
		}

		var firstnameVal = $("#firstname").val();
		if(firstnameVal == '') {
			$("label#firstname_error").show();  
       		$("input#firstname").focus();  
			hasError = true;
		}

		var dobVal = $("#doby").val();
		if(dobVal == '' || !/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/.test(dobVal)) {
			$("label#dob_error").show();  
       		//$("input#doby").focus();  
			hasError = true;
		}

		var addressVal = $("#address").val();
		if(addressVal == '') {
			$("label#address_error").show();  
       		$("input#address").focus();  
			hasError = true;
		}

		var cityVal = $("#city").val();
		if(cityVal == '') {
			$("label#city_error").show();  
       		$("input#city").focus();  
			hasError = true;
		}

		var countryVal = $("#country").val();
		if(countryVal == '') {
			$("label#country_error").show();  
       		$("input#country").focus();  
			hasError = true;
		}
		

		var emailVal = $("#email").val();
		if(emailVal == '') {
			$("label#email_error").show();  
       		$("input#email").focus();  
			hasError = true;
		} else if(!emailReg.test(emailVal)) {	
			$("label#email_error").show();  
       		$("input#email").focus();  
			hasError = true;
		}

		var phoneVal = $("#phone").val();
		if(phoneVal == '') {
			$("label#phone_error").show();  
       		$("input#phone").focus();  
			hasError = true;
		}

		/*var arrival_dateVal = $("#arrival_datey").val() ;
		if(arrival_dateVal == '' || !/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/.test(arrival_dateVal)) {
			$("label#arrival_date_error").show();  
       		//$("input#arrival_datey").focus();  
			hasError = true;
		}

		var departure_dateVal = $("#departure_datey").val();
		if(departure_dateVal == '' || !/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/.test(departure_dateVal)) {
			$("label#departure_date_error").show();  
       		//$("input#departure_datey").focus();  
			hasError = true;
		}*/


		if(!$("#insurance_0").is(':checked') && !$("#insurance_1").is(':checked')){
			$("label#insurance_error").show();  
       		$("input#insurance_0").focus();  
			hasError = true;
		}
		
		if($("#insurance_0").is(':checked') && $("#policy_number").val() == '' ){
			$("label#policy_number_error").show();  
       		$("input#policy_number").focus();  
			hasError = true;
		}
		if($("#insurance_0").is(':checked') && ($("#policy_sd").val() == '' || !/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/.test($("#policy_sd").val()))){
			$("label#policy_sd_error").show();  
       		$("input#policy_sd").focus();  
			hasError = true;
		}

		if($("#insurance_0").is(':checked') && ($("#policy_ed").val() == '' || !/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/.test($("#policy_ed").val()))){
			$("label#policy_ed_error").show();  
       		$("input#policy_ed").focus();  
			hasError = true;
		}


		if($("#insurance_1").is(':checked') && (!$("#insurance_ilac_0").is(':checked') && !$("#insurance_ilac_1").is(':checked'))){
			$("label#insurance_ilac_error").show();  
       		$("input#insurance_ilac").focus();  
			hasError = true;
		}

		if(!$("#allergies_0").is(':checked') && !$("#allergies_1").is(':checked')){
			$("label#allergies_error").show();  
       		$("input#allergies").focus();  
			hasError = true;
		}

		if($("#allergies_0").is(':checked') && $("#allergies_list").val() == ''){
			$("label#allergies_list_error").show();  
       		$("input#allergies_list").focus();  
			hasError = true;
		}

		if(!$("#medical_0").is(':checked') && !$("#medical_1").is(':checked')){
			$("label#medical_error").show();  
       		$("input#medical").focus();  
			hasError = true;
		}

		if($("#medical_0").is(':checked') && $("#medical_list").val() == ''){
			$("label#medical_list_error").show();  
       		$("input#medical_list").focus();  
			hasError = true;
		}

		if(!$("#food_0").is(':checked') && !$("#food_1").is(':checked')){
			$("label#food_error").show();  
       		$("input#food").focus();  
			hasError = true;
		}

		if($("#food_0").is(':checked') && $("#food_list").val() == ''){
			$("label#food_list_error").show();  
       		$("input#food_list").focus();  
			hasError = true;
		}

		if(!$("#smoke_0").is(':checked') && !$("#smoke_1").is(':checked')){
			$("label#smoke_error").show();  
       		$("input#smoke").focus();  
			hasError = true;
		}


		if(hasError == false) {
			return true;
			/*$(this).hide();
			$("#form1").append('<img src="/images/loading.gif" alt="Loading" id="loading" />');
			$.post("/forms/registration.php", {data: $("#registration").serialize(), submit_btn_registration: $("#submit_btn_registration").val()},
   					function(data){
						
						$("#registration").slideUp("normal", function() {				   
							$("#registration").before('<br><h2>Thank you for your information.<br /><br />We invite you to continue reading to know about this great opportunity of learning English fast while enjoying the best vacation plan you will ever choose.</h2><p>ILAC STAFF</p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>');	
							//alert("Data Loaded: " + data);
							$("input#fullname").val("");
							$("input#country").val("");
							$("input#email").val("");
						});
   					}
				 );*/
		}else{		
		/*$.fancybox(
		'<h2>Hi!</h2><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam quis mi eu elit tempor facilisis id et neque</p>',
		{
        		'autoDimensions'	: false,
			'width'         		: 350,
			'height'        		: 'auto',
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
		}
	);*/

			alert ('Please complete all fields marked with "*" using the right format');
		}
		return false;
	});	
						   
						   
/*Pre Enroll Form	*/

	$(".error").hide();
	$("#submit_btn_preenroll").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		
		var fullnameVal = $("#fullname").val();
		if(fullnameVal == '') {
			$("label#fullname_error").show();  
       		$("input#fullname").focus();  
			hasError = true;
		}
		
		var countryVal = $("#country").val();
		if(countryVal == '') {
			$("label#country_error").show();  
       		$("input#country").focus();  
			hasError = true;
		}
		

		var emailVal = $("#email").val();
		if(emailVal == '') {
			$("label#email_error").show();  
       		$("input#email").focus();  
			hasError = true;
		} else if(!emailReg.test(emailVal)) {	
			$("label#email_error").show();  
       		$("input#email").focus();  
			hasError = true;
		}
		
		if(hasError == false) {
			$(this).hide();
			$("#form1").append('<img src="/images/loading.gif" alt="Loading" id="loading" />');
			$.post("/forms/preenroll.php",
   				{ fullname: fullnameVal, country: countryVal, email: emailVal, toronto_campus: $("#toronto_campus").val(), vancouver_campus: $("#vancouver_campus").val(),page: $("#page").val(), submit_btn_preenroll: $("#submit_btn_preenroll").val()},
   					function(data){
						
						$("#form1").slideUp("normal", function() {				   
							$("#form1").before('<br><p class="text">Thank you for your information.<br /><br />We invite you to continue reading to know about this great opportunity of learning English fast while enjoying the best vacation plan you will ever choose.</p><p class="text">ILAC STAFF</p>');	
							$("input#fullname").val("");
							$("input#country").val("");
							$("input#email").val("");
						});
   					}
				 );
		}
		
		return false;
	});	
			
			
/*Start Dates Pre Enroll Form	*/

	$(".error").hide();
	$("#submit_btn_SDpreenroll").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		
		var fullnameVal = $("#fullname").val();
		if(fullnameVal == '') {
			$("label#fullname_error").show();  
       		$("input#fullname").focus();  
			hasError = true;
		}
		
		var countryVal = $("#country").val();
		if(countryVal == '') {
			$("label#country_error").show();  
       		$("input#country").focus();  
			hasError = true;
		}
		

		var emailVal = $("#email").val();
		if(emailVal == '') {
			$("label#email_error").show();  
       		$("input#email").focus();  
			hasError = true;
		} else if(!emailReg.test(emailVal)) {	
			$("label#email_error").show();  
       		$("input#email").focus();  
			hasError = true;
		}
		
		if(!$("#toronto_campus").is(':checked') && !$("#vancouver_campus").is(':checked')) {
			$("label#campus_error").show();  
       		$("input#toronto_campus").focus();  
			hasError = true;
		}

		if(hasError == false) {
			$(this).hide();
			$("#form1").append('<img src="/images/loading.gif" alt="Loading" id="loading" />');
			$.post("/forms/preenroll.php",
   				{ fullname: fullnameVal, country: countryVal, email: emailVal, toronto_campus: ($("#toronto_campus").is(':checked')? $("#toronto_campus").val():"" ), vancouver_campus: ($("#vancouver_campus").is(':checked')? $("#vancouver_campus").val():"" ),page: $("#page").val(), submit_btn_SDpreenroll: $("#submit_btn_SDpreenroll").val()},
   					function(data){
						//alert("Data Loaded: " + data);
						$("#form1").slideUp("normal", function() {				   
							$("#form1").before('<br><p class="text">Thank you for your information.<br /><br />We invite you to continue reading to know about this great opportunity of learning English fast while enjoying the best vacation plan you will ever choose.</p><p class="text">ILAC STAFF</p>');	
							$("input#fullname").val("");
							$("input#country").val("");
							$("input#email").val("");
						});
   					}
				 );
		}
		
		return false;
	});	

/*Contact Us Form*/
	$(".error").hide();
	$("#submit_btn_contactus").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var fullnameVal = $("#fullname").val();
		if(fullnameVal == '') {
			$("label#fullname_error").show();  
       		$("input#fullname").focus();  
			hasError = true;
		}

		var countryVal = $("#country").val();
		if(countryVal == '') {
			$("label#country_error").show();  
       		$("input#country").focus();  
			hasError = true;
		}

		var cityVal = $("#city").val();
		if(cityVal == '') {
			$("label#city_error").show();  
       		$("input#city").focus();  
			hasError = true;
		}

		var emailVal = $("#email").val();
		if(emailVal == '') {
			$("label#email_error").show();  
       		$("input#email").focus();  
			hasError = true;
		} else if(!emailReg.test(emailVal)) {	
			$("label#email_error").show();  
       		$("input#email").focus();  
			hasError = true;
		}
		var phoneVal = $("#phone").val();
		var questionVal = $("#question").val();
		
		if(hasError == false) {
			$(this).hide();
			$("#form1").append('<img src="/images/loading.gif" alt="Loading" id="loading" />');
			
			$.post("/forms/contactus.php",
   				{ fullname: fullnameVal, country: countryVal, city: cityVal, email: emailVal,  question: questionVal, page: $("#page").val(), submit_btn_contactus: $("#submit_btn_contactus").val()},
   					function(data){
						//alert("Data Loaded: " + data);
						$("#form1").slideUp("normal", function() {				   
							$("#form1").before('<br><p class="text">Thank you for your contact information.<br />We will contact you soon.<br />We invite you to continue reading to know about this great opportunity of learning English fast while enjoying the best vacation plan you will ever choose.</p><p class="text"">ILAC STAFF</p>');	
							$("input#fullname").val("");
							$("input#city").val("");
							$("input#email").val("");
							$("input#country").val("");
							$("input#question").val("");
						});
   					}
				 );
		}else{		
			alert ('Please complete all fields marked with "*" using the right format');
		}
		
		return false;
	});		
	

});

function MM_openBrWindow(mypage,myname,w,h,features) { //v2.0
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(mypage,myname,settings);
  
}

function hideAll() {
	$('#yes-insurance').hide();
	$('#no-insurance').hide();
	$('#yes-allergies').hide();
	$('#yes-medical').hide();
	$('#yes-food').hide();
}
