///variables
var targetRedirect = "http://www.smartquote.com/abandon.html";
var popQuestionText = "No time to fill out a form? \nJust click OK below to go straight to today's mortgage rates from some of the nation's top lenders.";
var popupTimes =1;
var interv;
var brous=navigator.appName;

/////

var internalClick = false;
var popped;
var cokName="expop";
var doRedirection = false;
var doRedirectionIE = false;

function checkPopupPresent()
{       
	var nameEQ = cokName+"=";
	var cok = document.cookie.split(";");
	for(var i=0;i<cok.length;i++)
	{
		var ca = cok[i];
		while(ca.charAt(0)==' ') ca=ca.substring(1,ca.length);
		if(ca.indexOf(cokName)==0) return ca.substring(nameEQ.length,ca.length);
	}
	return null;
}

function doRedirect()
{
if(doRedirection){
	              doRedirection = false;
var tt = targetRedirect;	
if(document.getElementById("proloc")) tt+="?statecode="+document.getElementById("proloc").value;
if(document.getElementById("loan")){
var lt = document.getElementById("loan").value;
var rrr = "rp=1";
if(lt.indexOf("Ref")==0||lt.indexOf("ref")==0) rrr="rp=2";
if(tt.indexOf("?")!=-1) tt+="&"+rrr;
else tt+="?"+rrr;
}
if(document.getElementById("mBa"))
{
var bbb = document.getElementById("mBa").value;
if(bbb.length>0){
if(tt.indexOf("?")!=-1) tt+="&la="+bbb;
else tt+="?la="+bbb;
}
}			  
window.location=tt;//targetRedirect;
//window.clearInterval(interv);

				  //window.open(targetRedirect,"","width=400px,height=400px");
/*var u = targetRedirect;
var w = window.open('', '','width=755,height=520,left=1,right=1,top=0,toolbar=yes,menubar=yes,scrollbars=yes,status=yes,resizable=yes,status=yes,location=yes');
if (w) {
w.blur();
w.location = u;
window.focus();
if ( (w==null)
||(typeof(w)=="undefined")
||(typeof(w.location.hash)!="string")
) {
var t=window.open('', '','width=755,height=520,left=1,right=1,top=0,toolbar=yes,menubar=yes,scrollbars=yes,status=yes,resizable=yes,status=yes,location=yes');
t.location=u;
}
else { w.blur();
      }
window.focus();
}*/
				  
                  }

}

function askQuit()
{
	
	popupTimes=popupTimes-1;
	document.cookie = cokName+"="+popupTimes+"; path=/;";
	if(confirm(popQuestionText)) return true;
	return false;
}


function exitCheck(e)
{
	var eve = window.event ? window.event : e;
	if(!internalClick){
		              popped = checkPopupPresent();
					  if(!popped)
					            {
						          if(askQuit())
								     {  
										 doRedirection = true;
										//setTimeout("doRedirect();",0);
										//doRedirect();
										if(eve){
											    // if(eve.stopPropagation) eve.stopPropagation();
												// else eve.cancelBubble = true;
											    // if(eve.preventDefault) eve.preventDefault();
												// else eve.returnValue = false;
												  //doRedirect();
												  
										        }
									 }
								}
					//  else{
					//	   var no = parseInt(popped);
						//   if(!isNaN(no) && no>0) { popupTimes = no;if(askQuit()) doRedirection = true;}
					   //   }
	                 }
}

function setInternalClick()
{
	internalClick = true;
}

function initCode(e)
{
if (typeof e == 'undefined') {
    e = window.event;
  }
	if(!internalClick){
		        popped = checkPopupPresent();
                        if(popped) return;
			if(brous=="Microsoft Internet Explorer"){
				if (quitIE(e) ) setRedIE();
			}else{
				exitCheck(e);//if (quitff(e) )	exit_page();
			}
		
	}
}

interv=setInterval("doRedirect();",100);
var intervIE = setInterval("redirectIE();",100);
//window.onbeforeunload = exitCheck;
//window.document.onmousedown = setInternalClick;
//window.onunload = exitCheck;

window.onbeforeunload = initCode;



function quitIE(e){
	        document.cookie = cokName+"="+popupTimes+"; path=/;";
		message ='';
		//message +="For your security, the browser's back button is not able to save your information at this point in the Quote Request.\nPlease select from the following options:\n\n";
		message += "1. Click CANCEL to see rates from top lenders.\n";
		message += "2. Click OK to end your session.";
		if (e) {
			e.returnValue = message;
		}
		return message;
	
}

function setRedIE()
{
doRedirectionIE = true;
}

function redirectIE() {
	if(doRedirectionIE) {
                 internalClick = true;
                 var tt = targetRedirect;	
if(document.getElementById("proloc")) tt+="?statecode="+document.getElementById("proloc").value;
if(document.getElementById("loan")){
var lt = document.getElementById("loan").value;
var rrr = "rp=1";
if(lt.indexOf("Ref")==0||lt.indexOf("ref")==0) rrr="rp=2";
if(tt.indexOf("?")!=-1) tt+="&"+rrr;
else tt+="?"+rrr;
}
if(document.getElementById("mBa"))
{
var bbb = document.getElementById("mBa").value;
if(bbb.length>0){
if(tt.indexOf("?")!=-1) tt+="&la="+bbb;
else tt+="?la="+bbb;
}
}			  
window.location=tt;//targetRedirect;
		 //window.location = targetRedirect;
		 window.clearInterval(intervIE);
	}
}



var State = new Array(); var Code = new Array();

 State[0] = "Alabama";         Code[0] = "AL";
 State[1] = "Alaska";          Code[1] = "AK";
 State[2] = "Arizona";         Code[2] = "AZ";
 State[3] = "Arkansas";        Code[3] = "AR";
 State[4] = "California";      Code[4] = "CA";
 State[5] = "Colorado";        Code[5] = "CO";
 State[6] = "Connecticut";     Code[6] = "CT";
 State[7] = "Delaware";        Code[7] = "DE";
 State[8] = "Florida";         Code[8] = "FL";
 State[9] = "Georgia";         Code[9] = "GA";
State[10] = "Hawaii";         Code[10] = "HI";
State[11] = "Idaho";          Code[11] = "ID";
State[12] = "Illinois";       Code[12] = "IL";
State[13] = "Indiana";        Code[13] = "IN";
State[14] = "Iowa";           Code[14] = "IA";
State[15] = "Kansas";         Code[15] = "KS";
State[16] = "Kentucky";       Code[16] = "KY";
State[17] = "Louisiana";      Code[17] = "LA";
State[18] = "Maine";          Code[18] = "ME";
State[19] = "Maryland";       Code[19] = "MD";
State[20] = "Massachusetts";  Code[20] = "MA";
State[21] = "Michigan";       Code[21] = "MI";
State[22] = "Minnesota";      Code[22] = "MN";
State[23] = "Mississippi";    Code[23] = "MS";
State[24] = "Missouri";       Code[24] = "MO";
State[25] = "Montana";        Code[25] = "MT";
State[26] = "Nebraska";       Code[26] = "NE";
State[27] = "Nevada";         Code[27] = "NV";
State[28] = "New Hampshire";  Code[28] = "NH";
State[29] = "New Jersey";     Code[29] = "NJ";
State[30] = "New Mexico";     Code[30] = "NM";
State[31] = "New York";       Code[31] = "NY";
State[32] = "North Carolina"; Code[32] = "NC";
State[33] = "North Dakota";   Code[33] = "ND";
State[34] = "Ohio";           Code[34] = "OH";
State[35] = "Oklahoma";       Code[35] = "OK";
State[36] = "Oregon";         Code[36] = "OR";
State[37] = "Pennsylvania";   Code[37] = "PA";
State[38] = "Rhode Island";   Code[38] = "RI";
State[39] = "South Carolina"; Code[39] = "SC";
State[40] = "South Dakota";   Code[40] = "SD";
State[41] = "Tennessee";      Code[41] = "TN";
State[42] = "Texas";          Code[42] = "TX";
State[43] = "Utah";           Code[43] = "UT";
State[44] = "Vermont";        Code[44] = "VT";
State[45] = "Virginia";       Code[45] = "VA";
State[46] = "Washington";     Code[46] = "WA";
State[47] = "West Virginia";  Code[47] = "WV";
State[48] = "Wisconsin";      Code[48] = "WI";
State[49] = "Wyoming";        Code[49] = "WY";

function getStateCode(statename){
for(var i=0;i<State.length;i++){
if(State[i]==statename) return Code[i];
}
}