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')  }
}

function checkPurpose()	{
	var selectedValue = document.mortgageApplicationForm.purpose.options[ document.mortgageApplicationForm.purpose.selectedIndex].value;

	if(selectedValue == '')
	{
			if(!document.all && document.getElementById){
				//thisbrowser="NN6";
				document.getElementById("MyLayer").style.display = '';
			}
		 	else if(document.all){
				//thisbrowser="ie"
				document.all["MyLayer"].style.display='';
			}

	}
	else if(selectedValue == '8' || selectedValue == '7')
	{
			if(!document.all && document.getElementById){
				//thisbrowser="NN6";
				document.getElementById("MyLayer").style.display = 'none';
			}
		 	else if(document.all){
				//thisbrowser="ie"
				document.all["MyLayer"].style.display='none';

			}
	}
	else
	{
			if(!document.all && document.getElementById){
				//thisbrowser="NN6";
				document.getElementById("MyLayer").style.display = '';
			}
		 	else if(document.all){
				//thisbrowser="ie"
				document.all["MyLayer"].style.display='';
			}

	}
	
}


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 validateApplication2()
{
	var ErrorMsg = '';
	var trimmed_string;

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

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

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

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

	//Check zipcode
	trimmed_string = trim(document.mortgageApplicationForm.zip.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Zipcode\n';
		highlightTextField(document.mortgageApplicationForm.zip);
		document.mortgageApplicationForm.zip.focus();
	}
	else
	{	nohighlightTextField(document.mortgageApplicationForm.zip);	}
	
	
	//Check 'house_type'
	if (document.mortgageApplicationForm.house_type.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Type of House\n';
		highlightSelectField(document.mortgageApplicationForm.house_type);
		document.mortgageApplicationForm.house_type.focus();
	}
	else
	{	nohighlightSelectField(document.mortgageApplicationForm.house_type); }
	


	//Check if any phone numbers have been entered
	if( ( (document.mortgageApplicationForm.phone1.value.length == 0) && (document.mortgageApplicationForm.phone2.value.length == 0) && (document.mortgageApplicationForm.phone3.value.length == 0) )  &&
		( (document.mortgageApplicationForm.workphone1.value.length == 0) && (document.mortgageApplicationForm.workphone2.value.length == 0) && (document.mortgageApplicationForm.workphone3.value.length == 0) && (document.mortgageApplicationForm.workphone4.value.length == 0))  )	
	{
		ErrorMsg = ErrorMsg + '- Home Phone and/or Work Phone\n';
		highlightTextField(document.mortgageApplicationForm.phone1);
		highlightTextField(document.mortgageApplicationForm.phone2);
		highlightTextField(document.mortgageApplicationForm.phone3);
		highlightTextField(document.mortgageApplicationForm.workphone1);
		highlightTextField(document.mortgageApplicationForm.workphone2);
		highlightTextField(document.mortgageApplicationForm.workphone3);
		highlightTextField(document.mortgageApplicationForm.workphone4);
	}
	else
	{
		//Check Home Phone
		if( (document.mortgageApplicationForm.phone1.value.length > 0) || (document.mortgageApplicationForm.phone2.value.length > 0) || (document.mortgageApplicationForm.phone3.value.length > 0) )
		{
			if( ( (document.mortgageApplicationForm.phone1.value.length != 3) || isNaN(document.mortgageApplicationForm.phone1.value) ) ||
			    ( (document.mortgageApplicationForm.phone2.value.length != 3) || isNaN(document.mortgageApplicationForm.phone2.value) ) ||
			    ( (document.mortgageApplicationForm.phone3.value.length != 4) || isNaN(document.mortgageApplicationForm.phone3.value) ) )	 
			{
				ErrorMsg = ErrorMsg + '- Home Phone\n';
				highlightTextField(document.mortgageApplicationForm.phone1);
				highlightTextField(document.mortgageApplicationForm.phone2);
				highlightTextField(document.mortgageApplicationForm.phone3);
				document.mortgageApplicationForm.phone1.focus();
			}
			else
			{	
				nohighlightTextField(document.mortgageApplicationForm.phone1);	
				nohighlightTextField(document.mortgageApplicationForm.phone2);	
				nohighlightTextField(document.mortgageApplicationForm.phone3);	
			}
		}
		else
		{	
			nohighlightTextField(document.mortgageApplicationForm.phone1);	
			nohighlightTextField(document.mortgageApplicationForm.phone2);	
			nohighlightTextField(document.mortgageApplicationForm.phone3);	
		}

		//Check Work Phone
		if( (document.mortgageApplicationForm.workphone1.value.length > 0) || (document.mortgageApplicationForm.workphone2.value.length > 0) || (document.mortgageApplicationForm.workphone3.value.length > 0) || (document.mortgageApplicationForm.workphone4.value.length > 0))
		{
			//Check Work Phone
			if( ( (document.mortgageApplicationForm.workphone1.value.length != 3) || isNaN(document.mortgageApplicationForm.workphone1.value) ) ||
			    ( (document.mortgageApplicationForm.workphone2.value.length != 3) || isNaN(document.mortgageApplicationForm.workphone2.value) ) ||
			    ( (document.mortgageApplicationForm.workphone3.value.length != 4) || isNaN(document.mortgageApplicationForm.workphone3.value) ) ||
				  isNaN(document.mortgageApplicationForm.workphone4.value))	 
			{
				ErrorMsg = ErrorMsg + '- Work Phone\n';
				highlightTextField(document.mortgageApplicationForm.workphone1);
				highlightTextField(document.mortgageApplicationForm.workphone2);
				highlightTextField(document.mortgageApplicationForm.workphone3);
				highlightTextField(document.mortgageApplicationForm.workphone4);
				document.mortgageApplicationForm.workphone1.focus();
			}
			else
			{	
				nohighlightTextField(document.mortgageApplicationForm.workphone1);	
				nohighlightTextField(document.mortgageApplicationForm.workphone2);	
				nohighlightTextField(document.mortgageApplicationForm.workphone3);	
				nohighlightTextField(document.mortgageApplicationForm.workphone4);	
			}
		}
		else
		{
			nohighlightTextField(document.mortgageApplicationForm.workphone1);	
			nohighlightTextField(document.mortgageApplicationForm.workphone2);	
			nohighlightTextField(document.mortgageApplicationForm.workphone3);	
			nohighlightTextField(document.mortgageApplicationForm.workphone4);	
		}
	}


	//Check the email address
	if (!isEmailAddr(document.mortgageApplicationForm.email.value))
	{
		ErrorMsg = ErrorMsg + '- Email Address\n';
		highlightTextField(document.mortgageApplicationForm.email);
		document.mortgageApplicationForm.email.focus();
	}
	else
	{	
		nohighlightTextField(document.mortgageApplicationForm.email); 
	}

	//Check best_time
	if (document.mortgageApplicationForm.best_time.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Best Time to call\n';
		highlightSelectField(document.mortgageApplicationForm.best_time);
		document.mortgageApplicationForm.best_time.focus();
	}
	else
	{
		nohighlightSelectField(document.mortgageApplicationForm.best_time); 
	}
	

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

}



function validateApplication1()
{
	var ErrorMsg = '';
	var trimmed_string;
	
	
	//Check 'purpose'
	if (document.mortgageApplicationForm.purpose.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Purpose of Loan\n';
		highlightSelectField(document.mortgageApplicationForm.purpose);
		document.mortgageApplicationForm.purpose.focus();
	}
	else
	{	nohighlightSelectField(document.mortgageApplicationForm.purpose); }
	
	//Check 'amount'
	if (document.mortgageApplicationForm.amount.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Desired Loan Amount\n';
		highlightSelectField(document.mortgageApplicationForm.amount);
		document.mortgageApplicationForm.amount.focus();
	}
	else
	{	nohighlightSelectField(document.mortgageApplicationForm.amount); }

	
	//Check home_value
	if (document.mortgageApplicationForm.home_value.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Home Value\n';
		highlightSelectField(document.mortgageApplicationForm.home_value);
		document.mortgageApplicationForm.home_value.focus();
	}
	else
	{	nohighlightSelectField(document.mortgageApplicationForm.home_value); }

	
	//Check Balance
	if (document.mortgageApplicationForm.balance.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Approximate 1st Mortgage Balance\n';
		highlightSelectField(document.mortgageApplicationForm.balance);
		document.mortgageApplicationForm.balance.focus();
	}
	else
	{	nohighlightSelectField(document.mortgageApplicationForm.balance); }

	//Check Credit
	if (document.mortgageApplicationForm.credit.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Your Credit Rating\n';
		highlightSelectField(document.mortgageApplicationForm.credit);
		document.mortgageApplicationForm.credit.focus();
	}
	else
	{	nohighlightSelectField(document.mortgageApplicationForm.credit); }
	
	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;
}


