function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}


function openWindow(theChoice)
{
	if (theChoice == 'Contact')
	{	popupWin = window.open('popup.htm#contact', 'popup', 'scrollbars,dependent,width=568,height=400')  }
	if (theChoice == 'About')
	{	popupWin = window.open('popup.htm#about', 'popup', 'scrollbars,dependent,width=568,height=400')  }
	if (theChoice == 'Privacy')
	{ 	popupWin = window.open('popup.htm#privacy', 'popup', 'scrollbars,dependent,width=568,height=400')  }
}


var submitcount=0;
function highlightTextField(field) {
	field.style.borderColor = 'red';
	field.style.borderStyle = 'solid';
	field.style.borderWidth = '1px;';
}

function nohighlightTextField(field) {
	field.style.borderColor = '';
	field.style.borderStyle = '';
	field.style.borderWidth = '';
}

function highlightSelectField(field) {
	field.style.backgroundColor = 'red';
	field.style.color = 'black';
}

function nohighlightSelectField(field) {
	field.style.backgroundColor = '';
	field.style.color = '';
}

function validateApplication() {

	var ErrorMsg = '';
	var trimmed_string;
	
	
	//Check firstname ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.firstname.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- First Name\n';
		highlightTextField(document.applicationForm.firstname);
		document.applicationForm.firstname.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.firstname);
	}

	//Check lastname ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.lastname.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- Last Name\n';
		highlightTextField(document.applicationForm.lastname);
		document.applicationForm.lastname.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.lastname);
	}

	//Check address ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.address.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- Address\n';
		highlightTextField(document.applicationForm.address);
		document.applicationForm.address.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.address);
	}

	//Check city ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.city.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- City\n';
		highlightTextField(document.applicationForm.city);
		document.applicationForm.city.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.city);
	}
	
		//Check if any phone numbers have been entered
	if ( (document.applicationForm.phone1.value.length == 0) && (document.applicationForm.phone2.value.length == 0) && (document.applicationForm.phone3.value.length == 0) )
	{
		ErrorMsg = ErrorMsg + '- Home Phone and/or Work Phone\n';
		highlightTextField(document.applicationForm.phone1);
		highlightTextField(document.applicationForm.phone2);
		highlightTextField(document.applicationForm.phone3);
	}
	else {
		nohighlightSelectField(document.applicationForm.phone1);
		nohighlightSelectField(document.applicationForm.phone2);
		nohighlightSelectField(document.applicationForm.phone3);
	}

	//Check State ---------------------------------------------------------------
	if (document.applicationForm.state.value == '')
	{
		ErrorMsg = ErrorMsg + '- State\n';
		highlightSelectField(document.applicationForm.state);
		document.applicationForm.state.focus();
	}
	else
	{
		nohighlightSelectField(document.applicationForm.state);
	}

	//Check zipcode ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.zip.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- Zipcode\n';
		highlightTextField(document.applicationForm.zip);
		document.applicationForm.zip.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.zip);
	}
		
	//Check best_time
	if (document.applicationForm.best_time.value == '')
	{
		ErrorMsg = ErrorMsg + '- Best Time to call\n';
		highlightSelectField(document.applicationForm.best_time);
		document.applicationForm.best_time.focus();
	}
	else
	{
		nohighlightSelectField(document.applicationForm.best_time);
	}

	/*
	//Check age range ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.age_id.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- Age Range\n';
		highlightTextField(document.applicationForm.age_id);
		document.applicationForm.age_id.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.age_id);
	}
	
	
	//Check policy type ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.policy_type.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- Policy Type\n';
		highlightTextField(document.applicationForm.policy_type);
		document.applicationForm.policy_type.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.policy_type);
	}
	
	//Check sex ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.sex.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- Sex\n';
		highlightTextField(document.applicationForm.sex);
		document.applicationForm.sex.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.policy_type);
	}
	
	//Check medical condition ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.medical_condition.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- Medical Condition\n';
		highlightTextField(document.applicationForm.medical_condition);
		document.applicationForm.medical_condition.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.policy_type);
	}
	
	//Check has loan ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.has_loan.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- Has Loan\n';
		highlightTextField(document.applicationForm.has_loan);
		document.applicationForm.has_loan.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.has_loan);
	}
	
	//Check policy type ---------------------------------------------------------------
	trimmed_string = trim(document.applicationForm.smoking_status.value);
	if (trimmed_string.length == 0)
	{
		ErrorMsg = ErrorMsg + '- Smoking Status\n';
		highlightTextField(document.applicationForm.smoking_status);
		document.applicationForm.smoking_status.focus();
	}
	else
	{
		nohighlightTextField(document.applicationForm.smoking_status);
	}

	
	*/
	
	if(ErrorMsg != '')
	{
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		return false;
	}
	else
	{
		return true;
	}
}

function trim(inputString)
{
	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces it with one space. If something besides
	// a string is passed in (null, custom object, etc.) then return the input.

	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { // Check for spaces at the beginning of the string
	retValue = retValue.substring(1, retValue.length);
	ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { // Check for spaces at the end of the string
	retValue = retValue.substring(0, retValue.length-1);
	ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
	retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user

} // Ends the "trim" function


//Check to validate the email address
function isEmailAddr(email)
{
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	}
	return result;
}


