/**
 * Javascript for enrolment form
 */
 
// control showup colums
function show(which,name,type) {

	if (document.getElementById && document.createTextNode){
		m=document.getElementById(name);
		trig=m.getElementsByTagName("div").item(which).style.display;
		if (type=="box") 
			{if (trig=="block") trig="none";
		else if (trig=="" || trig=="none") trig="block";
		}
		if (type=="radio") {
		  if (trig=="" || trig=="none") trig="block";}
		    m.getElementsByTagName("div").item(which).style.display=trig;
	    }
	  if (which==0) m.getElementsByTagName("div").item(1).style.display="none";
	  if (which==1) m.getElementsByTagName("div").item(0).style.display="none";
}


//email validation
function emailCk() {
  function isValidEmail(str) {
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  }
  if(!isValidEmail(document.EnrolForm.EmailAdd.value)) {
    alert("You must enter a correct Email address     (Field No.10)");
  	document.EnrolForm.EmailAdd.focus();
	return false;
  }
}

function newColor(checkfield,field) {
  if (checkfield.checked) {
    field.disabled=false;
    field.style.backgroundColor="white";
  }
  else {
    field.style.backgroundColor="#C0C0C0";
    field.disabled=true;
  }
}
function OnlyString(strtocheck,message) {
  var pattern=/[^a-z ]/i;
  if (pattern.test(strtocheck)) {
    alert(message);
    //strtocheck.focus();
  }
}
function OnlyNumeric(notocheck,message) {
  var pattern=/[^\d]/;
  if (pattern.test(notocheck)) {
    alert(message);
    //notocheck.focus();
  }
}

// Guardian Check
function GuardianCK(yearB) {
  var d=new Date();
  var ile=d.getFullYear();
  ile=ile-yearB;
  if (ile<18) {
    alert("You are "+ile+" years old. You need a guardian");
	document.EnrolForm.Guardian[0].disabled=false;
	document.EnrolForm.Guardian[1].disabled=false;
	} 
	else {
	  alert("You are "+ile+" years old. You do not need a guardian");
	  document.EnrolForm.Guardian[0].disabled=true;
	  document.EnrolForm.Guardian[1].disabled=true;  
	  document.EnrolForm.Accom[0].checked=false;
      document.EnrolForm.Accwhich[0].checked=false;
      document.EnrolForm.Homestayroom[0].checked=false;
      document.EnrolForm.Homestaymeal[0].checked=false;
      document.EnrolForm.Accom[1].disabled=false;
      document.EnrolForm.Accwhich[1].disabled=false;
      document.EnrolForm.Homestayroom[1].disabled=false;
      document.EnrolForm.Homestaymeal[1].disabled=false;
    }
}

function openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

function yesacc() {
  alert("You acc.");
  document.EnrolForm.AccDay.disabled="disabled";
}

function mlody() {
  show(0,'menu2','radio');
  show(0,'menu1','radio');
  show(0,'menu','radio');
  document.EnrolForm.Accom[0].checked="checked";
  document.EnrolForm.Accwhich[0].checked="checked";
  document.EnrolForm.Homestayroom[0].checked="checked";
  document.EnrolForm.Homestaymeal[0].checked="checked";
  document.EnrolForm.Accom[1].disabled="disabled";
  document.EnrolForm.Accwhich[1].disabled="disabled";
  document.EnrolForm.Homestayroom[1].disabled="disabled";
  document.EnrolForm.Homestaymeal[1].disabled="disabled";
  document.EnrolForm.Homestaymeal[2].disabled="disabled";
}

function antymlody() {
  show(1,'menu2','radio');
  show(1,'menu1','radio');
  show(1,'menu','radio');
  document.EnrolForm.Accom[0].checked=false;
  document.EnrolForm.Accwhich[0].checked=false;
  document.EnrolForm.Homestayroom[0].checked=false;
  document.EnrolForm.Homestaymeal[0].checked=false;
  document.EnrolForm.Accom[1].disabled=false;
  document.EnrolForm.Accwhich[1].disabled=false;
  document.EnrolForm.Homestayroom[1].disabled=false;
  document.EnrolForm.Homestaymeal[1].disabled=false;
  document.EnrolForm.Homestaymeal[2].disabled=false;
}

function checkField() {
  
  // Personal Information Check
  if (document.EnrolForm.LastName.value=="") {
    alert("You must enter your last name  (Field No.1)");
    document.EnrolForm.LastName.focus();
    return false;
  }  
  if (document.EnrolForm.FirstName.value=="") {
    alert("You must enter your first name  (Field No.2)");
	document.EnrolForm.FirstName.focus();
    return false;
  }
  if (document.EnrolForm.PostalAddress.value=="") {
    alert("You must enter your postal address in home country  (Field No.3)");
    document.EnrolForm.PostalAddress.focus();
    return false;
  }
  if (document.EnrolForm.PhoneCountry.value=="") {
    alert("You must enter your telephone country code  (Field No.4)");
    document.EnrolForm.PhoneCountry.focus();
    return false;
  }
  if (document.EnrolForm.PhoneArea.value=="") {
    alert("You must enter your telephone area code  (Field No.4)");
    document.EnrolForm.PhoneArea.focus();
    return false;
  }
  if (document.EnrolForm.PhoneLocal.value=="") {
    alert("You must enter your telephone local number  (Field No.4)");
    document.EnrolForm.PhoneLocal.focus();
    return false;
  }
  if (document.EnrolForm.Nationality.value=="NonSelected") {
    alert("You must enter your nationality  (Field No.6)");
    document.EnrolForm.Nationality.focus();
    return false;
  }
  if (document.EnrolForm.VisaType.value=="NonSelected") {
    alert("You must enter your visa type  (Field No.7)");
    document.EnrolForm.VisaType.focus();
    return false;
  }
  if (document.EnrolForm.BirthDay.value=="NonSelected") {
    alert("You must enter your date of birth (Field No.8 -Date)");
    document.EnrolForm.BirthDay.focus();
    return false;
  }
  if (document.EnrolForm.BirthMonth.value=="NonSelected") {
    alert("You must enter your date of birth (Field No.8 -Month)");
    document.EnrolForm.BirthMonth.focus();
    return false;
  }
  if (document.EnrolForm.BirthYear.value=="NonSelected") {
    alert("You must enter your date of birth (Field No.8 -Year)");
    document.EnrolForm.BirthYear.focus();
    return false;
  }
  if (document.EnrolForm.Sex.value=="NonSelected") {
    alert("You must enter your sex    (Field No.9)");
    document.EnrolForm.Sex.focus();
    return false;
  }
  if (document.EnrolForm.EmailAdd.value=="") {
 	alert("You must enter your Email address    (Field No.10)");
	document.EnrolForm.EmailAdd.focus();
	return false;
  }
  if (document.EnrolForm.D1.value=="NonSelected") {
  	alert("You must enter your starting day    (Field No.12)");
  	document.EnrolForm.D1.focus();
 	return false;
  } 
  
  // Course Check
  if ((!document.EnrolForm.GE.checked) && 
		(!document.EnrolForm.EAP1.checked)&&
		(!document.EnrolForm.EAP2.checked) &&
		(!document.EnrolForm.EAP3.checked)&&
		(!document.EnrolForm.PET.checked)&&
		(!document.EnrolForm.FCE.checked)&&				
		(!document.EnrolForm.CAE.checked)&&
		(!document.EnrolForm.CPE.checked)&&
		(!document.EnrolForm.IELTS.checked)&&
		(!document.EnrolForm.GEplusEXAM.checked)&&		
		(!document.EnrolForm.TourismHosp.checked)&&
		(!document.EnrolForm.HealthPro.checked)&&
		(!document.EnrolForm.HIGH.checked)&&
		(!document.EnrolForm.ESSE.checked)&&
		(!document.EnrolForm.SUPER.checked)&&
		(!document.EnrolForm.JUNIOR.checked)) {														
		  alert("You must choose any of English courses (Field No.13)");
		  return false;
  }

  // General English Check
  if (document.EnrolForm.GE.checked)	{
	if (document.EnrolForm.WeekGE.value=="") {
	  alert("You must enter your length of General English");
  	  document.EnrolForm.WeekGE.focus();
  	  return false;
  	}
  	if (document.EnrolForm.CourseType.value=="NonSelected") {
	  alert("You must enter your course type");
	  document.EnrolForm.CourseType.focus();
  	  return false;
  	}
  }
  
  // Guardian Check 
  if (document.EnrolForm.Guardian[1].checked) {
    if (document.EnrolForm.Gname.value=="") {
	  alert("You must enter Guardian name");
  	  document.EnrolForm.Gname.focus();
	  return false;
  	}
	if (document.EnrolForm.Gdate.value=="") {
	  alert("You must enter Guardian DOB");
  	  document.EnrolForm.Gdate.focus();
  	  return false;
  	}
    if (document.EnrolForm.Gaddress.value=="") {
	  alert("You must enter Guardian address");
  	  document.EnrolForm.Gaddress.focus();
  	  return false;
  	}
  }
 
  // Accommodation Check
  if (!document.getElementById('accomYes').checked && !document.getElementById('accomNo').checked) {
    alert("You must choose if you need to arrange accommodation  (Field No.16)");
    return false;
  }
  
  // If Accommodation is sellected
  if (document.getElementById('accomYes').checked)  {
	// Accommodation Type Check
	 if (document.EnrolForm.Accwhich[0].checked==false && document.EnrolForm.Accwhich[1].checked==false) {
	  alert("You must choose which accommodation (Field No.16)");
  	  return false;
  	}	
	// If Homestay is sellected
	if (document.EnrolForm.Accwhich[0].checked)  {
	  if (document.EnrolForm.Homestayroom[0].checked==false && document.EnrolForm.Homestayroom[1].checked==false) {
	    alert("You must choose homestay room type (Field No.16)");
  		return false;
	  }  
	  if (document.EnrolForm.Homestaymeal[0].checked==false && document.EnrolForm.Homestaymeal[1].checked==false
		&& document.EnrolForm.Homestaymeal[2].checked==false) {
	    alert("You must choose homestey with or without meals");
  	    return false;
  	  }
	  if (document.EnrolForm.AccWeeks1.value=="")  {
	    alert("You must enter how many weeks");
  	    document.EnrolForm.AccWeeks1.focus();
  	    return false;
  	  }
	  if (document.EnrolForm.AccDay1.value=="NonSelected")  {
	    alert("You must enter homestay start date");
  	    document.EnrolForm.AccDay1.focus();
  	    return false;
  	  }
	  if (document.EnrolForm.AccMonth1.value=="NonSelected")  {
	    alert("You must enter homestay start month");
  	    document.EnrolForm.AccMonth1.focus();
	    return false;
  	  }
	  if (document.EnrolForm.AccYear1.value=="NonSelected")  {
	    alert("You must enter homestay start year");
	    document.EnrolForm.AccYear1.focus();
  	    return false;
  	  }
	  if (document.EnrolForm.Smoke[1].checked==false && document.EnrolForm.Smoke[0].checked==false)  {
	    alert("You must choose do you smoke: yes or no (Field No.17)");
  	    return false;
      }
      if (document.EnrolForm.Smokeok[0].checked==false && document.EnrolForm.Smokeok[1].checked==false) {
        alert("You must choose if it is ok for other people to smoke: yes or no (Field No.17)");
        return false;
      }
      if (document.EnrolForm.Pets[0].checked==false && document.EnrolForm.Pets[1].checked==false) {
        alert("You must choose if pets are ok for you: yes or no (Field No.18)");
        return false;
      }
      if (document.EnrolForm.Pets[1].checked) {
        if (document.EnrolForm.PetsDetails.value=="") {
         alert("You must provide details (Field No.18)");
         return false;
        }
      }
      if (document.EnrolForm.Health[0].checked==false && document.EnrolForm.Health[1].checked==false) {
        alert("You must choose health problem: yes or no (Field No.19)");
        return false;
      }
      if (document.EnrolForm.Health[0].checked) {
        if (document.EnrolForm.HealthDetails.value=="") {
         alert("You must provide details (Field No.19)");
         return false;
        }
      }
      if (document.EnrolForm.Food[0].checked) {
        if (document.EnrolForm.FoodDetails.value=="") {
         alert("You must provide details (Field No.20)");
         return false;
        }
      }
      if (document.EnrolForm.Requests[0].checked) {
        if (document.EnrolForm.RequestsDetails.value=="") {
         alert("You must provide details (Field No.21)");
         return false;
        }
      }
    }
	// If Hostel is sellected
	if (document.EnrolForm.Accwhich[1].checked)  {
	  if (document.EnrolForm.Hostelroom[0].checked==false && document.EnrolForm.Hostelroom[1].checked==false) {
	    alert("You must choose Hostel room type (Field No.16)");
  		return false;
	  }
	  if (document.EnrolForm.AccWeeks2.value=="")  {
	     alert("You must enter how many weeks (Field No.16)");
	     document.EnrolForm.AccWeeks2.focus();
	     return false;
      }
      if (document.EnrolForm.AccDay2.value=="NonSelected")  {
	    alert("You must enter hostel start day (Field No.16)");
	    document.EnrolForm.AccDay2.focus();
	    return false;
      }
      if (document.EnrolForm.AccMonth2.value=="NonSelected")  {
	    alert("You must enter hostel start month (Field No.16)");
	    document.EnrolForm.AccMonth2.focus();
	    return false;
      }
      if (document.EnrolForm.AccYear2.value=="NonSelected") {
	    alert("You must enter hostel start year (Field No.16)");
	    document.EnrolForm.AccYear2.focus();
	    return false;
      }
  	}
	// Airport Meeting Check
	if (document.EnrolForm.Airport[0].checked==false && document.EnrolForm.Airport[1].checked==false) {
      alert("You must choose airport pickup: yes or no (Field No.22)");
      return false;
    }
    if (document.EnrolForm.Airport[0].checked) {
      if (document.EnrolForm.AirType.value=="NonSelected") {
        alert("You must choose type of airport pickup (Field No.22)");
  	    document.EnrolForm.AirType.focus();
        return false;
      }
    }
  }

  // Declaration Check
  if (!document.getElementById('declaration').checked) {
	 alert("You must check Declaration (Field No.23)");
  	 return false;
  }

  OnlyString(document.EnrolForm.LastName.value,'Please enter characters only for family name');
  OnlyString(document.EnrolForm.FirstName.value,'Please enter characters only for given name');
  OnlyString(document.EnrolForm.Gname.value,'Please enter characters only for quardian contact name');
  OnlyNumeric(document.EnrolForm.PhoneCountry.value,'Please enter numbers only for country code');
  OnlyNumeric(document.EnrolForm.PhoneArea.value,'Please enter numbers only for area code');
  OnlyNumeric(document.EnrolForm.PhoneLocal.value,'Please enter numbers only for contact number');
  OnlyNumeric(document.EnrolForm.WeekGE.value,'Please enter number of GE weeks only');
  OnlyNumeric(document.EnrolForm.AccWeeks1.value,'Please enter numbers only for homestay weeks');
  
  OnlyNumeric(document.EnrolForm.AccWeeks2.value,'Please enter numbers only for hostel weeks');
  
  OnlyString(document.EnrolForm.Tinst.value,'Please enter characters only for tertiary institution');
}
