function checkDomForm(form){

    //check helper's DOB
    var dom_date =  new Date((form.d_bmm.selectedIndex+1)+"/"+(form.d_bdd.selectedIndex+1)+"/"+form.d_byy.value)
    var gooddDOB = isValidDate((form.d_bmm.selectedIndex+1)+"/"+(form.d_bdd.selectedIndex+1)+"/"+form.d_byy.value) 
    var d_byear = form.d_byy.value
    if (gooddDOB && d_byear.length==4){
	if(calcAge(form.d_bmm.selectedIndex+1,form.d_bdd.selectedIndex+1,form.d_byy.value)<18 || calcAge(form.d_bmm.selectedIndex+1,form.d_bdd.selectedIndex+1,form.d_byy.value)>53){
	    alert("Domestic helper should be between 18 and 55!");
	    return false
	}//if
    }else{
	alert("Invalid domestic helper's Date of Birth. \nPlease check the year of the date of birth.");
	return false
    }//else

    return form.submit();

}//checkDomForm

function checkHseForm(frm){

rc=true;

// if ( frm.lorise[0].checked ){ frm.action=""; location.href="unsupported.htm"; rc=false;}     
// if ( frm.lorise[1].checked ){ frm.action=""; location.href="unsupported.htm"; rc=false;}     
    return rc;
}//checkHseForm

function checkTvlForm(form){

	if (checkInsured(form.age1.value,form.relation1.selectedIndex)=="Bad"){
		alert("Please check insured person 1 's info.")
		return false
	}	 

	if (checkInsured(form.age2.value,form.relation2.selectedIndex)=="Bad"){
		alert("Please check insured person 2 's info.")
		return false
	}	 

	if (checkInsured(form.age3.value,form.relation3.selectedIndex)=="Bad"){
		alert("Please check insured person 3 's info.")
		return false
	}	 

	if (checkInsured(form.age4.value,form.relation4.selectedIndex)=="Bad"){
		alert("Please check insured person 4 's info.")
		return false
	}	 

	if (checkInsured(form.age5.value,form.relation5.selectedIndex)=="Bad"){
		alert("Please check insured person 5 's info.")
		return false
	}	 

	if (checkInsured(form.age6.value,form.relation6.selectedIndex)=="Bad"){
		alert("Please check insured person 6 's info.")
		return false
	}	 
  
	var fromyear = ""
	var toyear = ""

	fromyear = form.FYY[form.FYY.selectedIndex].value
	toyear   = form.TYY[form.TYY.selectedIndex].value

	var goodFromDate = isValidDate((form.FMM.selectedIndex+1)+"/"+(form.FDD.selectedIndex+1)+"/"+fromyear) 
	var goodToDate = isValidDate((form.TMM.selectedIndex+1)+"/"+(form.TDD.selectedIndex+1)+"/"+toyear) 
	
	if (goodFromDate && goodToDate){
		var DfromDate = new Date((form.FMM.selectedIndex+1)+"/"+(form.FDD.selectedIndex+1)+"/"+fromyear)
		var DtoDate = new Date((form.TMM.selectedIndex+1)+"/"+(form.TDD.selectedIndex+1)+"/"+toyear)
		var dateDiff = ( (DtoDate - DfromDate) /86400000 )+1
		if (dateDiff > 182){
			alert("The period is greater than 182 days!")
			return false
		}
		if (dateDiff < 1){
			alert("Invalid date sequence!")
			return false
		}
 		var today = new Date()
                today.setHours(0)
                today.setMinutes(0)
                today.setSeconds(0)
		//alert(today + " " + DfromDate)
		if ( (DfromDate - today +1000) /86400000 <0){
			alert("The starting date is too close or before today! \n Please call us at +852 25117189 for urgent applications.")
			return false
		} 
	}else{
		alert("Invalid dates! From: "+DfromDate +" To: "+DtoDate)
		return false
	}

	return form.submit();

}//checkTvlForm()


function checkInsured(age, relation){
	var status = "Bad"
	if (isEmpty(age) && relation==0){
		status =  "Not here"
	}	
	if (isEmpty(age) && !relation==0){
		status =  "Bad"
	}	
	if (!isEmpty(age) && !relation==0){
		if (parseInt(age)<0 || parseInt(age)>256 || !isNumeric(age)){
			status = "Bad"
		}else{
			status =  "Good"
		}
	}	
	return status	
}//checkInsured


function preSelectDates(){
	var today = new Date()
	var preTo = new Date(today.getTime() + (7 * 86400000))

	var theAgent=navigator.appName;
	if(theAgent=="Netscape"){
		var ty_stpt0 =parseInt(document.form_iod.FYY[0].value) -1900
	}else{
		var ty_stpt0 = document.form_iod.FYY[0].value
	}
	//seccase
 
 	document.form_iod.FMM.selectedIndex = today.getMonth()
 	document.form_iod.FDD.selectedIndex = today.getDate() - 1
 	document.form_iod.FYY.selectedIndex = today.getFullYear() - ty_stpt0 

 	document.form_iod.TMM.selectedIndex = preTo.getMonth()
 	document.form_iod.TDD.selectedIndex = preTo.getDate() - 1
 	document.form_iod.TYY.selectedIndex = preTo.getFullYear() - ty_stpt0 

}//preSelectDates

function calcAge(bmm,bdd,byy){
	var age;

	//curent info
	today = new Date();
	year = today.getFullYear() ;
	month = today.getMonth() + 1;
	day = today.getDate();

	if ( month < bmm ){
		age = year - byy - 1;
	}else if ( month > bmm ){
		age = year - byy;
	}else if ( month == bmm ){
		if ( day < bdd ){
			age = year - byy - 1;
		}else if ( day > bdd ){
			age = year - byy;
		}else if ( day == bdd ){
			age = year - byy;
		}
  	}
	return age;
}//calcAge

function isValidDate(dateStr) {
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/

	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		return false;
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		return false;
	}
	if (day < 1 || day > 31) {
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return false
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			return false;
		}
	}
	return true;  // date is valid
}//isValidDate


function isNumeric(inputStr){
	for (var i = 0; i < inputStr.length; i++){
		var oneChar = inputStr.substring(i,i+1)
		if (oneChar < "0" || oneChar > "9"){
			return false
		}
	}
	return true
}//isNumeric

function isEmpty(inputStr) {
	if (inputStr == "" || inputStr == null) {
		return true
	}
	return false
}//isEmpty

