// JavaScript Document<script language="JavaScript" type="text/JavaScript">
function validateForm()
{
  // Create a reference to the Title text box
  var formField = document.frmlogin.txtUserName;

  // See if the user typed anything in the text box
  if (formField.value == '')
  {
    // If not, display error message and exit
    alert('Please enter your user name!');
    formField.focus();
    return false;
  }


  // Create a reference to the start date text box
  formField = document.frmlogin.txtPassword;

var re = /^\w{4,6}$/;
if (!re.test(formField.value)) 
{ 
alert("Password must consists of 4 to 6 characters");
   formField.focus();
    return false;
 }

  return true;
}

function vaEnrol()
{
	
if (isDate(form1.StartDate.value) == false)
{		
	form1.StartDate.focus()
	return false;			
}

if (!form1.Duration[0].checked && !form1.Duration[1].checked) 
{
    	alert("Please select the course duration.");
    	return false;
    }
	
  return true;
}

function validateEnrol()
{

 formField = document.form1.email;

  // See if the user typed anything in the text box
  if (formField.value == '')
  {
    // If not, display error message and exit
    alert('Email cannot be empty');
    formField.focus();
    return false;
  }



  // Check Email for a valid pattern,
  var objRegExp = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
  if (!objRegExp.test(formField.value))
  {
    alert('This is not a valid email, please enter a valid format!');
    formField.focus();
    return false;
  }


// check password
var re = /^\w{4,6}$/;
if (!re.test(myString))
{
alert("Password must consists of 4 to 6 characters");
	  formField.focus();
    return false;
	}
	
var enrol
if (!isValidDate(myDateString, "DMY")) 
{
	alert("The date is not in the correct format."); 
  formField.focus();
    return false;
	}



  return true;
}


function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || _
      (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else _
      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else _
      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else _
      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}
function isValidTime(vtime) {
   var hasMeridian = false;
   var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
   if (!re.test(vtime)) { return false; }
   if (vtime.toLowerCase().indexOf("p") != -1) { hasMeridian = true; }
   if (vtime.toLowerCase().indexOf("a") != -1) { hasMeridian = true; }
   var values = vtime.split(":");
   if ( (parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) { return false; }
   if (hasMeridian) {
      if ( (parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12) ) { return false; }
   }
   if ( (parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) { return false; }
   if (values.length > 2) {
      if ( (parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59) ) { return false; }
   }
   return true;
}
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function validForm(form1) {   
  
  
  if (form1.LastName.value == "") { 
    alert("Please enter your Family Name");
    form1.LastName.focus();
    return false;
  }   
  
    if (form1.FirstName.value == "") { 
    alert("Please enter your Given Name");
    form1.FirstName.focus();
    return false;
  } 
  

if (!document.form1.Gender[0].checked && !document.form1.Gender[1].checked) 
{
    	alert("Please select your gender.");
    	return false;
    }

 var str=form1.PWD.value 
 var reg = /^\w{4,6}$/;		

 if (!reg.test(str)) 
	{
			alert("Password must consists of 4 to 6 characters");
			form1.PWD.focus();
			return false;
		
	}
	
  
  if (form1.cpwd.value == "") { 
    alert("Please confirm your password");
    form1.cpwd.focus();
    return false;
  }   
 if (form1.PWD.value != form1.cpwd.value)
 { 
    alert("The password is not confirmed, please re-enter your password and confirm it.");
    form1.PWD.focus();
    return false;
  } 
  		
		

	if (isDate(form1.DOB.value)==false){
		form1.DOB.focus()
		return false
	}
  
if (form1.Email.value=="")
	{		
		alert("Please enter your email, e.g. yourname@company.com")
		form1.Email.focus()
			return false;			
		}
else
	{		
		var str=form1.Email.value
		var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(str))
		{			
		}
		else
		{
			alert("Please enter an email, e.g. yourname@company.com")
			form1.Email.focus()
			return(false)
		}	
	}
	
 
var regu=/^\d{1,4}(\-)\d{1,4}\1\d{6,10}$/
if (!regu.test(form1.Phone.value))
{		
  alert("Please enter a phone number, including country and area code. e.g. 86-1-22334455");
    form1.Phone.focus();
    return false;		
}

var regu=/^\d{1,4}(\-)\d{1,4}\1\d{6,10}$/
if (form1.Fax.value != "")
{
if (!regu.test(form1.Fax.value))
{		
  alert("Please enter a fax number, including country and area code, .e.g. 86-1-22334455");
    form1.Fax.focus();
    return false;		
}
}

  
if (form1.PostAddress.value == "") 
 { 
    alert("Please enter the postal address in your country of residence.");
    form1.PostAddress.focus();
    return false;
  }   
      else
  {
	  if (form1.PostAddress.value.length > 1000)
	  {
    alert('The postal address can not exceed 1000 characters.');
    form1.PostAddress.focus();
    return false; 
	  }
	  }	 
	  
if (form1.NZAddress.value != "") 
 { 

	  if (form1.NZAddress.value.length > 1000)
	  {
    alert('The New Zealand address can not exceed 1000 characters.');
    form1.NZAddress.focus();
    return false; 
	  }
	  }	 
   
if (form1.UserName.value == "") { 
    alert("please enter your user name, it will be used for login after enrolment.");
    form1.UserName.focus();
    return false;
  }  

   if (form1.ECName.value == "") { 
    alert("Please name  your next of kin.");
    form1.ECName.focus();
    return false;
  }  
    
  
  var reguE=/^\d{1,4}(\-)\d{1,4}\1\d{6,10}$/
if (!reguE.test(form1.ECPhone.value))
{		
  alert("Please supply a contact phone number for your next of kin. e.g.86-1-43292345.");
    form1.ECPhone.focus();
    return false;		
}


  
 if (!document.form1.accommondation[0].checked) 
{
if (document.form1.Smoking[0].checked || document.form1.Smoking[1].checked) 
 if (form1.medical.value != "")
	{ 

alert('Please remove the text in the medical conditions field, as you do not require us to arrange accommodation for you.');
form1.medical.focus();
return false;
	
	}
else if (form1.food.value != "")
{
alert('Please remove the text in the food field, as you do not require us to arrange accommodation for you.');
form1.food.focus();
return false; 
}
}


if (form1.medical.value != "") 
 { 

	  if (form1.medical.value.length > 1000)
	  {
    alert('The medical condition can not exceed 1000 characters.');
    form1.medical.focus();
    return false; 
	  }
	  }	 

if (form1.food.value != "") 
 { 

	  if (form1.food.value.length > 1000)
	  {
    alert('The foods area can not exceed 1000 characters.');
    form1.food.focus();
    return false; 
	  }
	  }	 


if (!document.form1.transfer[0].checked) 
{
if (form1.FlightNo.value != "") 
{
    	alert("Please remove the text in the Flight Number field, as you do not require us to arrange transportation for you..\n");
		form1.FlightNo.focus();
    	return false;
    }
	else if (form1.arrivedate.value != "")
	{ 

alert('Please remove the text in the arrival date field, as you do not require us to arrange transportation for you.');
form1.arrivedate.focus();
return false;
	
	}
else if (form1.arrivetime.value != "")
{
alert('Please remove the text in the arrival time field, as you do not require us to arrange transportation for you.');
form1.arrivetime.focus();
return false; 
}
}


if (document.form1.transfer[0].checked) 
{
 if (form1.FlightNo.value == "")
	{ 

alert('Please enter your flight number.');
form1.FlightNo.focus();
return false;
	
	}
else if (isDate(form1.arrivedate.value)==false)
{		
    form1.arrivedate.focus();
    return false;
}

else  if (isValidTime(form1.arrivetime.value) == false)
{
alert('Please enter your time of arrival, e.g.11:24 am');
form1.arrivetime.focus();
return false;
}
}

if (form1.arrivedate.value != "")
{
	if (isDate(form1.arrivedate.value)==false)
{		
    form1.arrivedate.focus();
    return false;
}
}

if (form1.arrivetime.value != "")
{
    if (isValidTime(form1.arrivetime.value) == false)
{		
alert('Please enter your time of arrival, e.g.11:24 am');
    form1.arrivetime.focus();
    return false;
}
}

  return true;
}


