function changelabel(theform) {
	label1.style.fontFamily = 'Arial';
	label1.innerText = "Your information is being processed. Please wait for confirmation page."
	label1.style.color = "#FF0000";
}

function loadList1() {
	var indexList1 = document.forms[0].CarMake1.selectedIndex
	var valueList1 = document.forms[0].CarMake1.value

	if (indexList1 == 0) {
		for (i = document.forms[0].CarModel1.options.length; i >= 0; i--) {
			document.forms[0].CarModel1.options[i] = null; 
		}
		document.forms[0].CarModel1.options[0] = new Option('Model');
		document.forms[0].CarModel1.options[0].selected = true;
		document.forms[0].CarModel1.options[0].value = 0;
	}
	else {
		for (i = document.forms[0].CarModel1.options.length; i >= 0; i--) {
			document.forms[0].CarModel1.options[i] = null; 
		}
		
		var cntModel1 = 0;
		document.forms[0].CarModel1.options[0] = new Option('Model');
		document.forms[0].CarModel1.options[0].selected = true;
		document.forms[0].CarModel1.options[0].value = 0;
		for (i=0;i<ModelArray1.length;i++) {
			if (valueList1==ModelArray1[i][0]) {
				cntModel1 = cntModel1 + 1;
				document.forms[0].CarModel1.options[cntModel1] = new Option(ModelArray1[i][1]);
				document.forms[0].CarModel1.options[cntModel1].value = ModelArray1[i][2];
			}
		}
	}
}

function loadList2() {
	var indexList2 = document.forms[0].CarMake2.selectedIndex
	var valueList2 = document.forms[0].CarMake2.value

	if (indexList2 == 0) {
		for (i = document.forms[0].CarModel2.options.length; i >= 0; i--) {
			document.forms[0].CarModel2.options[i] = null; 
		}
		document.forms[0].CarModel2.options[0] = new Option('Model');
		document.forms[0].CarModel2.options[0].selected = true;
		document.forms[0].CarModel2.options[0].value = 0;
	}
	else {
		for (i = document.forms[0].CarModel2.options.length; i >= 0; i--) {
			document.forms[0].CarModel2.options[i] = null; 
		}
		
		var cntModel2 = 0;
		document.forms[0].CarModel2.options[0] = new Option('Model');
		document.forms[0].CarModel2.options[0].selected = true;
		document.forms[0].CarModel2.options[0].value = 0;
		for (i=0;i<ModelArray1.length;i++) {
			if (valueList2==ModelArray1[i][0]) {
				cntModel2 = cntModel2 + 1;
				document.forms[0].CarModel2.options[cntModel2] = new Option(ModelArray1[i][1]);
				document.forms[0].CarModel2.options[cntModel2].value = ModelArray1[i][2];
			}
		}
	}
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function checkInternationalPhone(strPhone) {
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}     

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 result = true;
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=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) {
		result = false;
	}
	if (strMonth.length<1 || month<1 || month>12) {
		result = false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]) {
		result = false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear) {
		result = false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false) {
		result = false;
	}
	return result;
}

/*
Submit Once form validation- 
© Dynamic Drive (www.dynamicdrive.com)
For full source code, usage terms, and 100's more DHTML scripts, visit http://dynamicdrive.com
*/

function submitonce(theform) {
	if (!validateform(theform)) {
		return false;
	}
	else {
	//if IE 4+ or NS 6+
	if (document.all||document.getElementById) {
		//screen thru every element in the form, and hunt down "submit" and "reset"
		for (i=0;i<theform.length;i++) {
			var tempobj=theform.elements[i]
			if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset") {
				//disable em
				tempobj.disabled=true
			}
		}
	}
	concatenatelist(theform);
	changelabel();
	return true;
	}
}

function concatenatelist(theform) {
	var a=0;
	for (a=0;a<=2;a++) {
		if (theform.RadioStations[a].value!="") {
			theform.RadioStation.value += theform.RadioStations[a].value;
			theform.RadioStation.value += ",";
		}
	}
	
	for (a=0;a<=4;a++) {
		if (theform.PetTypes[a].value!="") {
			theform.PetType.value += theform.PetTypes[a].value;
			theform.PetType.value += ",";
		}
	}
	
	for (a=0;a<=4;a++) {
		if (theform.MedicalConditions[a].value) {
			theform.MedicalCondition.value += theform.MedicalConditions[a].value;
			theform.MedicalCondition.value += ",";
		}
	}

	for (a=0;a<=3;a++) {
		if (theform.CC[a].value!="") {
			theform.CreditCard.value += theform.CC[a].value;
			theform.CreditCard.value += ",";
		}
	}

	for (a=0;a<=3;a++) {
		if (theform.CCCompany[a].value!="") {
			theform.CreditCardCompany.value += theform.CCCompany[a].value;
			theform.CreditCardCompany.value += ",";
		}
	}
}

function validateform(theform) {
	var checks=0;
	var msg="";
	var statefilter=/([a-z]{2})/i
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	var emailvaild;
	var confirmemailvalid;
	var cdobfilter=/Invalid Child Date of Birth/
	var HPhone=theform.HomeNumber.value;
	var WPhone=theform.WorkNumber.value;
	var CPhone=theform.CellPhoneNumber.value;
	var PPhone=theform.PagerNumber.value;
	var FPhone=theform.FaxNumber.value;

	if (HPhone!="") {
		if (!checkInternationalPhone(HPhone)) {
			checks++;
			msg += "Invalid Home Phone Number\n";
		}
	}

	if (WPhone!="")	{
		if (!checkInternationalPhone(WPhone)) {
			checks++;
			msg += "Invalid Work Phone Number\n";
		}
	}

	if (CPhone!="") {
		if (!checkInternationalPhone(CPhone)) {
			checks++;
			msg += "Invalid Cell Phone Number\n";
		}
	}

	if (PPhone!="") {
		if (!checkInternationalPhone(PPhone)) {
			checks++;
			msg += "Invalid Pager Number\n";
		}
	}

	if (FPhone!="") {
		if (!checkInternationalPhone(FPhone)) {
			checks++;
			msg += "Invalid Fax Number\n";
		}
	}

	if (theform.State.value!="") {
		if (!statefilter.test(theform.State.value)) {
			checks++;
			msg += "Invalid State\n";
		}
	}
	
	if (theform.DOB.value!="") {
		if (!isDate(theform.DOB.value)) {
			checks++;
			msg += "Invalid Date of Birth\n";
		}
	}
	
	//check validity of email addresses
	if (theform.Email.value!="") {
		if (filter.test(theform.Email.value)) {
			emailvalid=true;
		}
		else {
			emailvalid=false;
			checks++;
			msg += "Invalid Email Address\n";
		}

		if (emailvalid) {
			if (theform.ConfirmEmail.value=="") {
				checks++;
				msg += "Email Addresses do not match!\n";
			}
			else
			if (theform.ConfirmEmail.value!=theform.Email.value) {
				checks++;
				msg += "Email Addresses do not match!\n";
			}
		}
	}
	else {
		if (theform.ConfirmEmail.value!="") {
			checks++;
			msg += "Email Addresses do not match!\n";
		}
	}

	if (theform.ChildDOB1.value!="") {
		if (!isDate(theform.ChildDOB1.value)) {
			checks++;
			if (!cdobfilter.test(msg)) {
				msg += "Invalid Child Date of Birth\n";
			}
		}
	}
	if (theform.ChildDOB2.value!="") {
		if (!isDate(theform.ChildDOB2.value)) {
			checks++;
			if (!cdobfilter.test(msg)) {
				msg += "Invalid Child Date of Birth\n";
			}
		}
	}
	if (theform.ChildDOB3.value!="") {
		if (!isDate(theform.ChildDOB3.value)) {
			checks++;
			if (!cdobfilter.test(msg))
			{
				msg += "Invalid Child Date of Birth\n";
			}
		}
	}
	if (theform.ChildDOB4.value!="") {
		if (!isDate(theform.ChildDOB4.value)) {
			checks++;
			if (!cdobfilter.test(msg)) {
				msg += "Invalid Child Date of Birth\n";
			}
		}
	}
	if (theform.ChildDOB5.value!="") {
		if (!isDate(theform.ChildDOB5.value)) {
			checks++;
			if (!cdobfilter.test(msg)) {
				msg += "Invalid Child Date of Birth\n";
			}
		}
	}
	if (theform.ChildDOB6.value!="") {
		if (!isDate(theform.ChildDOB6.value)) {
			checks++;
			if (!cdobfilter.test(msg)) {
				msg += "Invalid Child Date of Birth\n";
			}
		}
	}

	
	if (checks>0) {
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}