
var url = "http://www.mortgages-magazine.com";
var who = "Mortgages Magazine - Your Source for Mortgage News";
var ver = navigator.appName;
var num = parseInt(navigator.appVersion);

/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}

function mailForm() {
 if(document.mail.email_address.value == "Enter Email to Join")
 document.mail.email_address.value = "";
 }

 
//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;
}



function checkEmail()
{

	// create error message string variable, with nothing in it
	var message = ""; 
	var trimmed_string;
	var form = "";      //The form return of true or false

	//Check the email address
	if (!isEmailAddr(document.mail.email_address.value))
	{
		message = message + "Email Address is invalid\n";		
		document.mail.email_address.focus();
	}

	// is there an error message?   		
	if ( message.length > 0 ) 
	{ 
		   message = "The following error(s) occurred:" + "\n\n" + message;	
		   alert( message ); // display error message 
	       return false; // return false, not ok to process 
    }
	else 
	{
		   return true; // no error message to display, return ok to process 
	} 
}


var cursorX = 0;
var cursorY = 0;

function openPop(e, url) {
		
	var h = 150;
	var w = 450;
	
	
	if (navigator.appName.indexOf("Microsoft") != -1)
	{
		e = window.event;
	}

	cursorX = e.screenX;
	cursorY = e.screenY;

	var info = "dependent,width="+w+",height="+h+",top=" + (cursorY-h-22) + ",left=" + cursorX;
	
	window.open(url, 'lookup', info);
	
}

function setZip(zip, city, state) {
	
	var x = window.opener.document.getElementsByName("zip");
	var y = window.opener.document.getElementsByName("city");
	var z = window.opener.document.getElementsByName("state");
	
	var zip_field = x[0];
	var city_field = y[0];
	var state_field = z[0];
	
	zip_field.value=zip;
	city_field.value=city;
	
	for (var i=0; i<state_field.options.length; i++) {
		if (state_field.options[i].value==state) {
			state_field.options[i].selected = true;
		}
	}
	
	window.close();
	
}




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='';
			}

	}
	
}


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 'amount'
	trimmed_string = trim(document.mortgageApplicationForm.amount.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Desired Loan Amount\n';
		highlightTextField(mortgageApplicationForm.amount);
		document.mortgageApplicationForm.amount.focus;
	}
	else
	{	nohighlightTextField(mortgageApplicationForm.amount);	}

	//Check 'purpose'
	if (document.mortgageApplicationForm.purpose.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Purpose of Loan\n';
		highlightSelectField(mortgageApplicationForm.purpose);
		document.mortgageApplicationForm.purpose.focus();
	}
	else
	{	nohighlightSelectField(mortgageApplicationForm.purpose); }

	//Check 'house_type'
	if (document.mortgageApplicationForm.house_type.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Type of House\n';
		highlightSelectField(mortgageApplicationForm.house_type);
		document.mortgageApplicationForm.house_type.focus();
	}
	else
	{	nohighlightSelectField(mortgageApplicationForm.house_type); }

	//Check home_value
	trimmed_string = trim(document.mortgageApplicationForm.home_value.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Home Value\n';
		highlightTextField(mortgageApplicationForm.home_value);
		document.mortgageApplicationForm.home_value.focus();
	}
	else
	{	nohighlightTextField(mortgageApplicationForm.home_value);	}

	//Check Credit
	if (document.mortgageApplicationForm.credit.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Your Credit Rating\n';
		highlightSelectField(mortgageApplicationForm.credit);
		document.mortgageApplicationForm.credit.focus();
	}
	else
	{	nohighlightSelectField(mortgageApplicationForm.credit); }
	

	//If the purpose of the loan hides the layer then don't validate
	if( document.mortgageApplicationForm.purpose.value != '8' && document.mortgageApplicationForm.purpose.value != '7')
	{
		//Check Balance
		trimmed_string = trim(document.mortgageApplicationForm.balance.value);
		if (trimmed_string.length == 0) 
		{
			ErrorMsg = ErrorMsg + '- Approximate 1st Mortgage Balance\n';
			highlightTextField(mortgageApplicationForm.balance);
			document.mortgageApplicationForm.balance.focus();
		}
		else
		{	nohighlightTextField(mortgageApplicationForm.balance);	}
	
		//Check Credit
		if (document.mortgageApplicationForm.interest.value == '') 
		{
			ErrorMsg = ErrorMsg + '- Current Interest Rate\n';
			highlightSelectField(mortgageApplicationForm.interest);
			document.mortgageApplicationForm.interest.focus();
		}
		else
		{	nohighlightSelectField(mortgageApplicationForm.interest); }
	}

	//Check firstname
	trimmed_string = trim(document.mortgageApplicationForm.firstname.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- First Name\n';
		highlightTextField(mortgageApplicationForm.firstname);
		document.mortgageApplicationForm.firstname.focus();
	}
	else
	{	nohighlightTextField(mortgageApplicationForm.firstname);	}
	
	//Check lastname
	trimmed_string = trim(document.mortgageApplicationForm.lastname.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Last Name\n';
		highlightTextField(mortgageApplicationForm.lastname);
		document.mortgageApplicationForm.lastname.focus();
	}
	else
	{	nohighlightTextField(mortgageApplicationForm.lastname);	}

	//Check address
	trimmed_string = trim(document.mortgageApplicationForm.address.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Address\n';
		highlightTextField(mortgageApplicationForm.address);
		document.mortgageApplicationForm.address.focus();
	}
	else
	{	nohighlightTextField(mortgageApplicationForm.address);	}

	//Check city
	trimmed_string = trim(document.mortgageApplicationForm.city.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- City\n';
		highlightTextField(mortgageApplicationForm.city);
		document.mortgageApplicationForm.city.focus();
	}
	else
	{	nohighlightTextField(mortgageApplicationForm.city);	}

	//Check State
	if (document.mortgageApplicationForm.state.value == '') 
	{
		ErrorMsg = ErrorMsg + '- State\n';
		highlightSelectField(mortgageApplicationForm.state);
		document.mortgageApplicationForm.state.focus();
	}
	else
	{	nohighlightSelectField(mortgageApplicationForm.state); }

	//Check zip
	trimmed_string = trim(document.mortgageApplicationForm.zip.value);
	if (trimmed_string.length == 0) 
	{
		ErrorMsg = ErrorMsg + '- Zipcode\n';
		highlightTextField(mortgageApplicationForm.zip);
		document.mortgageApplicationForm.zip.focus();
	}
	else
	{	nohighlightTextField(mortgageApplicationForm.zip);	}

	//Check Home Phone
	if( ( (document.mortgageApplicationForm.homephone1.value.length != 3) || isNaN(document.mortgageApplicationForm.homephone1.value) ) ||
	    ( (document.mortgageApplicationForm.homephone2.value.length != 3) || isNaN(document.mortgageApplicationForm.homephone2.value) ) ||
	    ( (document.mortgageApplicationForm.homephone3.value.length != 4) || isNaN(document.mortgageApplicationForm.homephone3.value) ) )	 
	{
		ErrorMsg = ErrorMsg + '- Home Phone\n';
		highlightTextField(mortgageApplicationForm.homephone1);
		highlightTextField(mortgageApplicationForm.homephone2);
		highlightTextField(mortgageApplicationForm.homephone3);
		document.mortgageApplicationForm.homephone1.focus();
	}
	else
	{	
		nohighlightTextField(mortgageApplicationForm.homephone1);	
		nohighlightTextField(mortgageApplicationForm.homephone2);	
		nohighlightTextField(mortgageApplicationForm.homephone3);	
	}

	//Check the email address
	if (!isEmailAddr(document.mortgageApplicationForm.email.value))
	{
		ErrorMsg = ErrorMsg + '- Email Address\n';
		highlightTextField(mortgageApplicationForm.email);
		document.mortgageApplicationForm.email.focus();
	}
	else
	{	nohighlightTextField(mortgageApplicationForm.email); }

	//Check best_time
	if (document.mortgageApplicationForm.best_time.value == '') 
	{
		ErrorMsg = ErrorMsg + '- Best Time to call\n';
		highlightSelectField(mortgageApplicationForm.best_time);
		document.mortgageApplicationForm.best_time.focus();
	}
	else
	{	nohighlightSelectField(mortgageApplicationForm.best_time); }

	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;
}



//Function for setting the cookie
function setCookie(name) 
{
	var quickexpdate = new Date();
	quickexpdate.setTime(quickexpdate.getTime() + 1*60*60*1000); //for one hour
    document.cookie = name + "=On;expires=" + quickexpdate.toGMTString() + ";path=/;";
}

//Function to get the cookie
function getCookie(name) 
{
   var index = document.cookie.indexOf(name + "=");
   if (index == -1) return null;
      index = document.cookie.indexOf("=", index) + 1;
   var endstr = document.cookie.indexOf(";", index);
   if (endstr == -1) endstr = document.cookie.length;
      return unescape(document.cookie.substring(index, endstr));
}

//Function for pop under
function goNewWin() 
{
	TheNewWin = window.open("http://www.mortgages-magazine.com/pop/pop1.htm","","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,height=350,width=450"); 
	TheNewWin.blur();	
}

//checks for the existance of the cookie
//Requires goNewWin, getCookie, setCookie
function CheckCook(name)
{
	if(!getCookie(name)) 
	{
		setCookie(name);
		goNewWin();
	}
}

/*
function checkZip()
{
	var ErrorMsg = '';
	var trimmed_string;

	//Check zip
	trimmed_string = trim(document.zip_form.zip.value);
	if ( (trimmed_string.length < 4) || ( isNaN(trimmed_string) ) )
	{
		ErrorMsg = ErrorMsg + 'The Zip Code is invalid, please use the Zip Code Lookup to locate a Zip Code by City and State.\n';
		highlightTextField(zip_form.zip);
		document.zip_form.zip.focus();
	}
	else
	{	
		nohighlightTextField(zip_form.zip);	
	}

	if(ErrorMsg != '') 
	{
		alert(ErrorMsg);
		return false;
	} 
	else 
	{
		return true;
	}
return false;
}*/

function checkZip_small()
{
	var ErrorMsg = '';
	var trimmed_string;

	//Check zip
	trimmed_string = trim(document.zip_form.zip.value);
	if ( (trimmed_string.length < 4) || ( isNaN(trimmed_string) ) )
	{
		ErrorMsg = ErrorMsg + 'The Zip Code is invalid\n';
		highlightTextField(zip_form.zip);
		document.zip_form.zip.focus();
	}
	else
	{	
		nohighlightTextField(zip_form.zip);	
	}

	if(ErrorMsg != '') 
	{
		alert(ErrorMsg);
		return false;
	} 
	else 
	{
		return true;
	}
return false;
}

//Js links
function build_toplk(dir, page, ext, anchor_text)
{
	var domain = ''; domain_ext = ''; lk = ''; href = '';
	
	domain = "mortgages-magazine";
	domain_ext = "com";
	lk = "http://www." + domain + '.' + domain_ext + dir + page + "." + ext;
	href = '<a href="' + lk + '" class="toplinks"><strong>' + anchor_text + '</strong></a>';
	document.write(href);
	return false;
}

function build_lk(dir, page, ext, anchor_text)
{
	var domain = ''; domain_ext = ''; lk = ''; href = '';
	
	domain = "mortgages-magazine";
	domain_ext = "com";
	lk = "http://www." + domain + '.' + domain_ext + dir + page + "." + ext;
	href = '<a href="' + lk + '">' + anchor_text + '</a>';
	document.write(href);
	return false;
}



var cursorX;
var cursorY;
function saveCursorLocation(e) {

	if (navigator.appName.indexOf("Microsoft") != -1)
	{
		e = window.event;
	}

	cursorX = e.screenX;
	cursorY = e.screenY;
	//alert('['+cursorX+',' + cursorY+']');
}

function ShowPopup(hoveritem, id)
{
	//alert(id);
	hp = document.getElementById("hoverpopup_"+id);
	
	// Set position of hover-over popup
	hp.style.top = hoveritem.posTop + 18;
	//alert(hoveritem.offsetTop+18);
	hp.style.left = hoveritem.posLeft + 20;
	
	// Set popup to visible
	hp.style.visibility = "Visible";	
}

function HidePopup(id)
{	
hp = document.getElementById("hoverpopup_"+id);
hp.style.visibility = "Hidden";

}

function popComments(b_id, s_id) {
	
	var h = 150;
	var w = 225;
	var info = "dependent,resizable,statusbar=no,width="+w+",height="+h+",top=" + (cursorY-h-22) + ",left=" + cursorX;

	window.open("rate-search.htm?pop=comments&b_id="+b_id+"&s_id="+s_id, 'new', info);
	
	
}

var prequalifyLimit = 0;
function checkPreQualifyLimit(thisbox) {
	
	if (thisbox.checked == true && prequalifyLimit < 4) {
		prequalifyLimit++;
	//	alert(prequalifyLimit);
	}
	else if (thisbox.checked == false && prequalifyLimit > 0) {
		prequalifyLimit--;
		//alert(prequalifyLimit);
	}
	else if (thisbox.checked == true && prequalifyLimit == 4) {
		alert("You can only prequalify with up to 4 brokers");
		thisbox.checked = false;
	}
	
}