function isEmpty(inputStr) {
	if (inputStr == "" || inputStr == null) {
		return true
	}
	return false
}

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
}

function getSelectedButton(buttonGroup){
	for (var i=0;i<buttonGroup.length;i++){
		if (buttonGroup[i].checked){
			return i
		}
	}
	return 0
}

function isValidDateYMD(dateStr) {
	var datePat = /^(\d{2}|\d{4})(\/|-)(\d{1,2})\2(\d{1,2})$/
	var matchArray = dateStr.match(datePat)
	if (matchArray == null) {
		return false
	}
	year = matchArray[1]
	month = matchArray[3]
	day = matchArray[4]

	var tmp_dt = new Date(Date.parse( year+"/"+month+"/"+day ))
      return ((tmp_dt=='NaN')?false:tmp_dt)
}

function isValidDate(dateStr) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/
	var matchArray = dateStr.match(datePat)
	if (matchArray == null) {
		return false
	}
	month = matchArray[1]
	day = matchArray[3]
	year = matchArray[4]
	if (month < 1 || month > 12) { 
		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) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
		if (day>29 || (day==29 && !isleap)) {
			return false
		}
	}
	return true
}
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	
}

function preSelectDates(){
	var today = new Date()
	var preTo = new Date(today.getTime() + (7 * 86400000))


/*
	var ty_stpt0 = document.form_iod.FYY[0].value
20020228
*/
	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.getYear() - ty_stpt0 

 	document.form_iod.TMM.selectedIndex = preTo.getMonth()
 	document.form_iod.TDD.selectedIndex = preTo.getDate() - 1
 	document.form_iod.TYY.selectedIndex = preTo.getYear() - ty_stpt0 

}

function checkForm(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 DfromDate = isValidDateYMD(form.dt_from.value)
	var DtoDate = isValidDateYMD(form.dt_to.value)
	if (DfromDate && DtoDate){
		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 true
}
