function forwardParams(){
var theForm = document.forms[0]
    for(i=0; i<theForm.elements.length; i++){
    theForm.elements[i].value = getURLParam(theForm.elements[i].name);
    }
}

function getURLParam(name){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ) return "";
else return unescape(results[1]);
} 

function hov(loc,cls) { 
	  if(loc.className) 
	    loc.className=cls; 
	} 

function $(element) {
	if (typeof element == 'string')
		return document.getElementById(element);
}

function blankField(fld, def) {
	if (fld.value == def) {
		fld.value = '';
	}
}

function autotab(original,destination){
	if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
		destination.focus()
}

function showSpouse(visible) {
	if (visible)
		$('spouserow').style.display = ""
	else
		$('spouserow').style.display = "none";
}

function showChildren(){
	for (var show = 1; show <= $('child_count').value; show++) {
		$('childrow_' + show).style.display = "";
	}

	for (var hide = parseInt($('child_count').value) + 1; hide <= 6; hide++) {
		setChildDefaults(hide);
		$('childrow_' + hide).style.display = "none";
	}
}

function setChildDefaults(childNum) {
	childkey = childNum + 2;
	$('bdate_month_0' + childkey).value='mm';
	$('bdate_day_0' + childkey).value='dd';
	$('bdate_year_0' + childkey).value='yyyy';
	$('height_feet_0' + childkey).value='';
	$('height_inches_0' + childkey).value='';
	$('weight_0' + childkey).value='lbs';
	$('tobacco_0' + childkey).checked=false;
	$('bmi_0' + childkey).value='';
}

function checkContentField(fieldname) {
	return $(fieldname).value.length > 0;
}

function checkSelection(ss) {
	for(var i = 0; i < ss.length; i++){
		if(ss[i].selected){
			if(ss[i].value.length){
				return true;
			}
		}
	}
	return false;
}

function checkEmail(ss) {
	var pattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,3}(?:\.[a-z]{2})?)$/i;
	return pattern.test(ss);
}

function hasSpouse() {
	if ($('spouse_yes') && $('spouse_no'))
		return $('spouse_yes').checked;
	return $('gender_02').value.length > 0
		|| ($('bdate_month_02').value != 'mm' && $('bdate_month_02').value.length != 0)
		|| ($('bdate_day_02').value != 'dd' && $('bdate_day_02').value.length != 0)
		|| ($('bdate_year_02').value != 'yyyy' && $('bdate_year_02').value.length !=0)
		|| $('height_feet_02').value.length > 0 || $('height_inches_02').value.length > 0
		|| ($('weight_02').value != 'lbs' && $('weight_02').value.length !=0) || $('tobacco_02').checked;
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function isInteger(s){
	for (var 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 returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (var i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1)
			returnString += c;
	}
	return returnString;
}

function removeAllOptions(selectbox) {
	for (var i = selectbox.options.length - 1; i >= 1; i--) {
		selectbox.remove(i);
	}
}

function dobDayOptions(dayFieldID, monthField, yearFieldID) {
	// By default, assume 31 days.
	var days = 31;
	// See what month has been chosen, if any.
	if (monthField.selectedIndex != null && monthField.selectedIndex != 0) {
		var chosenMonth = monthField.options[monthField.selectedIndex].value;
		if (chosenMonth==4 || chosenMonth==6 || chosenMonth==9 || chosenMonth==11) {
			days = 30
		} else if (chosenMonth==2) {
			var yearField = document.getElementById(yearFieldID);
			if (yearField.selectedIndex != null && yearField.selectedIndex != "") {
				days = daysInFebruary(yearField.options[yearField.selectedIndex].value);
			} else {
				// If the year hasn't been chosen yet, just display 29 days.
				days = 29;
			}
		}
	}

	// Set the dayField to have the appropriate number of days.
	var dayField = document.getElementById(dayFieldID);
	removeAllOptions(dayField);
	for (var i = 1; i <= days; i++) {
		var optn = document.createElement('option');
		optn.text = i;
		if (i < 10) {
			optn.value = '0' + i;		// make sure a zero is prepended to single digit dates
		} else {
			optn.value = i;
		}
		dayField.options.add(optn);
	}
}

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 isDate(dtStr, dtCh){
	var daysInMonth = [null, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	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);
	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 (strYear.charAt(0) == "0" && strYear.length > 1)
			strYear = strYear.substring(1);
	}

	month = parseInt(strMonth);
	day = parseInt(strDay);
	year = parseInt(strYear);
	if (pos1 == -1 || pos2 == -1){
		alert("The date format should be : mm/dd/yyyy");
		return false;
	}

	if (strMonth.length<1 || month<1 || month>12){
		return false;
	}

	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;
	}

	if (strYear.length != 4 || year==0){
		return false;
	}

	if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false){
		return false;
	}

	return true;
}

function getBMI(weight, feet, inches) {
	var height_inches = feet * 12 + inches;
	return Math.round((weight * 703) / (height_inches * height_inches));
}

function validateInfo(num) {
	var str = '';
	var ext='';
	if(num == 1) {
		str = 'main applicant\'s'
	} else if(num == 2) {
		str = 'spouse\'s'
		ext = '_02';
	} else {
		childnum = num - 2;
		str = 'child ' + childnum;
		ext = '_0' + num;
	}

	var errormessage = new String();
	if(!checkSelection($('gender' + ext))) {
		errormessage += "\nPlease specify the " + str + " gender.";
	 }

	if(!checkContentField('bdate_month' + ext) || isNaN($('bdate_month' + ext).value) || !checkContentField('bdate_day' + ext) || isNaN($('bdate_day' + ext).value) || !checkContentField('bdate_year' + ext) || isNaN($('bdate_year' + ext).value)) {
		errormessage += "\nPlease specify the " + str + " birth.";
	} else if (!isDate($('bdate_month' + ext).value + '/'  + $('bdate_day' + ext).value + '/'  + $('bdate_year' + ext).value, '/')) {
		errormessage += "\nThe " + str + " birthday is not a valid date.";
	} else if (new Date($('bdate_year' + ext).value, $('bdate_month' + ext).value, $('bdate_day' + ext).value) > new Date() ) {
		errormessage += "\nThe " + str + " birthday cannot be in the future.";
	}

	if(!checkSelection($('height_feet' + ext)) || !checkSelection($('height_inches' + ext))){
		errormessage += "\nPlease specify the " + str + " height.";
	}

	if(!checkContentField('weight' + ext) || isNaN($('weight' + ext).value)) {
		errormessage += "\nPlease specify the " + str + " weight.";
	} else if (parseInt($('weight' + ext).value) == 0) {
		errormessage += "\nThe " + str + " weight cannot be 0 lbs";
	}else if(num <= 2 && parseInt($('weight' + ext).value) <10) {
		errormessage += "\nThe " + str + " weight needs to be at least 10 lbs or higher";
	} else {
		$('bmi' + ext).value = getBMI(parseInt($('weight' + ext).value), parseInt($('height_feet' + ext).value), parseInt($('height_inches' + ext).value));
	}
	return errormessage;
}

function validateZipCode() {
	var errormessage = new String();
	var pattern = /^[A-Z. '-]+$/i;
	
	if (!checkContentField('mail_postalcode')){
		errormessage += "\nPlease enter your zip code.";
	} else if(isNaN($('mail_postalcode').value) || $('mail_postalcode').value.length != 5 || ($('mail_postalcode').value == '00000')) {
		errormessage += "\n\nInvalid zip code.";
	}
	
	return errormessage;
}

function validateName() {
	var errormessage = new String();
	var pattern = /^[A-Z. '-]+$/i;	
	

	if(!checkContentField('first')){
		//errormessage += "\nPlease enter your first name.";
	} else if (!pattern.test($('first').value)) {
		errormessage += "\nPlease enter a valid first name.";
	}

	if(!checkContentField('last')){
		//errormessage += "\nPlease enter your last name.";
	} else if (!pattern.test($('last').value)) {
		errormessage += "\nPlease enter a valid last name.";
	}
	
	return errormessage;
}

function validatePhone(){
	var errormessage = new String();

	if(!checkContentField('home_area_code')){
		//errormessage += "\nPlease enter your home area code.";
	} else if(isNaN($('home_area_code').value) || $('home_area_code').value.length != 3) {
		errormessage += "\n\nInvalid home area code.";
	}

	if(!checkContentField('home_prefix')){
		//errormessage += "\nPlease enter your home prefix number.";
	} else if(isNaN($('home_prefix').value) || $('home_prefix').value.length != 3) {
		errormessage += "\nInvalid home phone number.";
	}

	if(!checkContentField('home_suffix')){
		//errormessage += "\nPlease enter your home suffix number.";
	} else if(isNaN($('home_suffix').value) || $('home_suffix').value.length != 4) {
		errormessage += "\nInvalid home phone number.";
	}	

	return errormessage;
}

function hideDefaults() {
	for(var i=2; i<=8; i++) {
		if($('bdate_month_0' + i).value == 'mm') $('bdate_month_0' + i).value='';
		if($('bdate_day_0' + i).value == 'dd') $('bdate_day_0' + i).value='';
		if($('bdate_year_0' + i).value == 'yyyy') $('bdate_year_0' + i).value='';
		if($('weight_0' + i).value == 'lbs') $('weight_0' + i).value='';
	}
}

function validateZipForm() {
	if (! checkContentField('mail_postalcode')) {
		alert('Please enter your zip code.');
		return false;
	}
	if (isNaN($('mail_postalcode').value) || $('mail_postalcode').value.length != 5 || $('mail_postalcode').value == '00000') {
		alert('Please enter a valid zip code.');
		return false;
	}
	return true;
}

function validateForm() {
	var errormessage = new String();
	errormessage+=validateInfo(1);
	if(hasSpouse()) {
		errormessage+=validateInfo(2);
	}

	for(var i = 1; i<=$('child_count').value; i++) {
		var childnum = i + 2;
		errormessage+=validateInfo(childnum);
	}	
	
	errormessage+=validateZipCode();

	if(errormessage.length > 2) {
		alert('Please fix the following errors:' + errormessage);
		return false;
	}

	hideDefaults();

	return true;
}
function validateForm_2() {
	var errormessage = new String();
	
	errormessage+=validateName();	
	if(!checkContentField('email')){
		//errormessage += "\nPlease enter your email.";
	} else if(!checkEmail($('email').value)){
			errormessage += "\nPlease enter a valid email address.";
	}	
	errormessage+=validatePhone();
	
	if(errormessage.length > 2) {
		alert('Please fix the following errors:' + errormessage);
		return false;
	}

	return true;
}