// JavaScript Document
function autotab(original,destination){
	if (original.value.length==original.getAttribute("maxlength")) {
		destination.focus();
	}
}

function setPhone() {
	document.ChronoContact_GeneralLiabilityForm.business_phone.value = document.ChronoContact_GeneralLiabilityForm.business_phone_areacode.value+'-'+document.ChronoContact_GeneralLiabilityForm.business_phone_prefix.value+'-'+document.ChronoContact_GeneralLiabilityForm.business_phone_suffix.value;
}

function setFax() {
	document.ChronoContact_GeneralLiabilityForm.business_fax.value = document.ChronoContact_GeneralLiabilityForm.business_fax_areacode.value+'-'+document.ChronoContact_GeneralLiabilityForm.business_fax_prefix.value+'-'+document.ChronoContact_GeneralLiabilityForm.business_fax_suffix.value;
}

function setPolicyExpire() {
	var policyExpire = document.getElementById("policy_expire");
	var months = new Array( "January", "February", "March", "April", "May",  "June", "July", "August", "September", "October",     "November", "December" ); 
	var NextYearToday = new Date(policy_start_Object.picked.day+' '+policy_start_Object.picked.monthShort+' '+(policy_start_Object.picked.yearValue+1));
	
	//code to validate Policy Start for older dates
	var setexpdate = Boolean;
	var csstime=new Date();
	var cssmonth = csstime.getMonth() + 1;
	var cssdate=csstime.getDate();
	var cssyear=csstime.getYear();
	var cssdatepicked = new Date(policy_start_Object.picked.day+' '+policy_start_Object.picked.monthShort+' '+(policy_start_Object.	picked.yearValue));
	var cssdatepicked = new Date( NextYearToday.getTime());
	cssdatepickedMon = months[cssdatepicked.getMonth() ];
	cssdatepickedYear = cssdatepicked.getFullYear();
	cssdatepickedDate =cssdatepicked.getDate();
	var csscurrentdate = new Date(cssmonth +"/" + cssdate + "/" + cssyear);
	var cssdatepicked = new Date(cssdatepicked.getMonth()+1 + "/" + cssdatepickedDate + "/" + (cssdatepickedYear-1));
	//alert(policy_expire_Object.picked.day);
	if (cssdatepicked < csscurrentdate) 
	{	    
	alert('Event Start Date can not occur before today.');
	setexpdate = false;
	}
	else
	{
	setexpdate = true;	
	}

//code to validate Policy end for older dates ends here


	//code to validate Policy End for older dates
	//var setexpdate = Boolean;
	var NextYearToday_expire = new Date(policy_end_Object.picked.day+' '+policy_end_Object.picked.monthShort+' '+(policy_end_Object.picked.yearValue+1));
	var csstime_expire=new Date();
	var cssmonth_expire = csstime_expire.getMonth() + 1;
	var cssdate_expire=csstime_expire.getDate();
	var cssyear_expire=csstime_expire.getYear();
	var cssdatepicked_expire = new Date(policy_end_Object.picked.day+' '+policy_end_Object.picked.monthShort+' '+(policy_end_Object.picked.yearValue));
	var cssdatepicked_expire = new Date( NextYearToday_expire.getTime());
	cssdatepickedMon_expire = months[cssdatepicked_expire.getMonth() ];
	cssdatepickedYear_expire = cssdatepicked_expire.getFullYear();
	cssdatepickedDate_expire =cssdatepicked_expire.getDate();
	var csscurrentdate_expire = new Date(cssmonth_expire +"/" + cssdate_expire + "/" + cssyear_expire);
	var cssdatepicked_expire = new Date(cssdatepicked_expire.getMonth()+1 + "/" + cssdatepickedDate_expire + "/" + (cssdatepickedYear_expire-1));
	//alert(policy_expire_Object.picked.day);
	if (cssdatepicked_expire < csscurrentdate_expire) 
	{	    
	alert('Event End Date can not occur before today.');
	}
	else
	{
	//setexpdate = true;	
	}

//code to validate Policy end for older dates ends here

}

function cardValueStore() {
	var cardNumber = document.ChronoContact_GeneralLiabilityForm.card_number.value;
	document.ChronoContact_GeneralLiabilityForm.card_store.value = cardNumber.substring(cardNumber.length-4,cardNumber.length);
}

function calculateTotal() {
	var thisTotal = 0;
	var studentNumber = 0;
	var policyTotal = document.getElementById("order");
	if (parseInt(document.ChronoContact_GeneralLiabilityForm.students_number.value) > 0) {
		studentNumber = parseInt(document.ChronoContact_GeneralLiabilityForm.students_number.value);
	}
	if ((studentNumber * 4) <= 450) {
		thisTotal += 450;
	}
	else {
		thisTotal +=  (studentNumber * 4);
	}
	
	document.ChronoContact_GeneralLiabilityForm.students_total_expense.value = thisTotal+'.00';
	document.ChronoContact_GeneralLiabilityForm.students_total_cost.value = thisTotal+'.00';
	
	if (document.ChronoContact_GeneralLiabilityForm.policy_auto.checked == true) {
		thisTotal += parseInt(document.ChronoContact_GeneralLiabilityForm.policy_auto.value);
	}
	if (document.ChronoContact_GeneralLiabilityForm.policy_sexual.checked == true) {
		thisTotal += parseInt(document.ChronoContact_GeneralLiabilityForm.policy_sexual.value);
	}
	
	for (var i=0;i<document.ChronoContact_GeneralLiabilityForm.policy_liability.length;i++) {
		if (document.ChronoContact_GeneralLiabilityForm.policy_liability[i].checked == true) {
			thisTotal = parseInt(document.ChronoContact_GeneralLiabilityForm.policy_liability[i].value) + thisTotal;
		}
	}
	//document.ChronoContact_GeneralLiabilityForm.policy_total.value = thisTotal+'.00';
	policyTotal.firstChild.nodeValue = '  $   '+thisTotal+'.00';
	//document.ChronoContact_GeneralLiabilityForm.policy_total.value = '1.00';
	document.ChronoContact_GeneralLiabilityForm.order_total.value = thisTotal+'.00';
	//document.ChronoContact_GeneralLiabilityForm.order_total.value = '1.00';
	
	// if user has selected financing with 20% downpayment, change order_total for 
	// credit card.  policy_total remains the same.
	if (document.ChronoContact_GeneralLiabilityForm.finance_policy.checked == true) {
		document.ChronoContact_GeneralLiabilityForm.order_total.value =
		document.ChronoContact_GeneralLiabilityForm.order_total.value * .2;	
	}
}
