
var D=document, W=window, StateAbbr, aCookies=null;
//------------------------------------------------------------------------

var aStates={'AL':'Alabama', 'AK':'Alaska', 'AZ':'Arizona', 'AR':'Arkansas', 'CA':'California', 'CO':'Colorado', 'CT':'Connecticut', 'DE':'Delaware', 'FL':'Florida', 'GA':'Georgia', 'HI':'Hawaii', 'ID':'Idaho', 'IL':'Illinois', 'IN':'Indiana', 'IA':'Iowa', 'KS':'Kansas', 'KY':'Kentucky', 'LA':'Louisiana', 'ME':'Maine', 'MD':'Maryland', 'MA':'Massachusetts', 'MI':'Michigan', 'MN':'Minnesota', 'MS':'Mississippi', 'MO':'Missouri', 'MT':'Montana', 'NE':'Nebraska', 'NV':'Nevada', 'NH':'New Hampshire', 'NJ':'New Jersey', 'NM':'New Mexico', 'NY':'New York', 'NC':'North Carolina', 'ND':'North Dakota', 'OH':'Ohio', 'OK':'Oklahoma', 'OR':'Oregon', 'PA':'Pennsylvania', 'RI':'Rhode Island', 'SC':'South Carolina', 'SD':'South Dakota', 'TN':'Tennessee', 'TX':'Texas', 'UT':'Utah', 'VT':'Vermont', 'VA':'Virginia', 'WA':'Washington', 'DC':'Washington DC', 'WV':'West Virginia', 'WI':'Wisconsin', 'WY':'Wyoming'};

//------------------------------------------------------------------------
//Results page part

ni_res_id = 2;
ni_alt_url = "https://www.nextinsure.com/search.asp";
ni_zc = null; //getQueryStringVariable('zipcode');
//ni_str_state_code is set later
//ni_var1 = 'rockyridgeranch.us'; //getQueryStringVariable('aid');
ni_var1=W.location.host;
if (ni_var1.substr(0, 4)=='www.') ni_var1=ni_var1.substr(4);

ni_display_width = 500;
ni_display_height = 1000;
ni_color_border = "";
ni_color_bg = "";
ni_color_link = "";
ni_color_url = "";
ni_color_text = "";
ni_lnt_id=null;

//------------------------------------------------------------------------

function ShowGeoText(bAuto){

//if (StateAbbr=='') return;
ShowStatesForm();
InitForm(); 
if (bAuto) DoShowGeoText(); 
if (StateAbbr!='' && StateAbbr!='-') ShowProviders();
}

function DoShowGeoText(){
var Text='', State, Pretext;

Pretext="Dear Customer, We value your business and do our best to save your time and efforts. Our system automatically identified you as a ";

if (StateAbbr!='' && StateAbbr!='-'){
	State=aStates[StateAbbr]; //GetSelOption(F.statecode, StateAbbr);
	Text=Pretext+"resident of "+State+". The Top Providers that you see below are delivered based on this information. If you are not a resident of "+State+", please select another state to see relevant offers.";
	}
else if (StateAbbr==''){
	Text=Pretext+"non-resident of the USA. Unfortunately we don't have relevant providers for your place of residence. If you are a resident of the USA, please select your state to see the Top Providers for your area.";
	}

if (!Text) return;

D.write('<DIV Class=GeoDiv><Img Src="/images/usa50.png" ALIGN="left">'+Text+'</DIV>');
}

function InitForm(){
var F;

F=FindForm();
if (StateAbbr!='' && StateAbbr!='-') F.statecode.value=StateAbbr; 
//Only for account 2667
if (ni_ad_client==2667){
	ni_lnt_id=getQueryStringVariable('loantype');
	if (ni_lnt_id) SetCookie('user_lnt_id', ni_lnt_id);
	else{
		GetCookies(); 
		ni_lnt_id=aCookies['user_lnt_id'];
		if (!ni_lnt_id) ni_lnt_id=1; //default
		}
	F.loantype.value=ni_lnt_id;
	}

}

function FindForm(){
var aForms, i;

aForms=D.getElementsByTagName('FORM'); 
if (aForms.length) return aForms[aForms.length-1];
return null;
}

function GetSelOption(Sel, Val){
var aOpt=Sel.options, OptNum=aOpt.length, i;

for(i=0; i<OptNum; i++){
	if (aOpt[i].value==Val) return aOpt[i].firstChild.data;
	}
return null;
}

function ShowProviders(){
var aVars, aVals=[], Var, Field, ValNum=0, bEsc;

//Stub for windows
if (1 && W.location.host.match(/\.loc$/)){
	D.write('<CENTER><B>results for '+StateAbbr+(ni_lnt_id ? ' loantype='+ni_lnt_id : '')+'...</B></CENTER>'); 
	return;
	}

if (!ni_ad_client) return;
ni_str_state_code=StateAbbr; //getQueryStringVariable('statecode');

aVars={
	'ni_ad_client':'src',
	'ni_str_state_code':'ssc',
	'ni_zc':'zc',
	'ni_max_display':'max',
	'ni_page':'pg',
	'ni_res_id':'res',
	'ni_lnt_id':'lnt',
	'ni_var1':'!var1',
	'ni_tkpixel':'!tkpixel',
	'ni_tkurl':'!tkurl',
	'ni_ds':'!ni_ds',
	'ni_uc':'!ni_uc',
	'ni_sc_guid':'ni_sc_guid'};

for(Var in aVars){
	if (W[Var]==null) continue;
	Field=aVars[Var];
	if (Field.charAt(0)=='!'){
		bEsc=true; Field=Field.substr(1);
		}
	else bEsc=false;
	aVals[ValNum++]=Field+'='+(bEsc ? escape(W[Var]) : W[Var]);
	
	if (Var=='ni_tkurl')
		aVals[ValNum++]='tkurlopt='+escape(ni_tkurl_options);
	}

aVals[ValNum++]='ref='+escape(W.location+'?statecode='+StateAbbr+
	'&aid='+W.ni_var1+'&x='+Math.round(Math.random()*85+15)
	+'&y='+Math.round(Math.random()*45+8));

D.write('<'+'Script Src="http://www.nextinsure.com/display_listings.asp?'+
	aVals.join('&')+'"><'+'/Script>');
setTimeout('ParseResults()', 100);
}

function DelCookie(Name, Path){
D.cookie=Name+"=;expires=Mon, 26 Jul 1997 05:00:00 GMT;path="+(Path ? Path : "/");
}

function GetStatePar(){

//DelCookie('user_state');

//1. Get
StateAbbr=getQueryStringVariable('statecode');
if (StateAbbr) SetCookie('user_state', StateAbbr);
//2. Cookies
else{
	GetCookies(); StateAbbr=aCookies['user_state'];
	}

if (StateAbbr){
	ShowGeoText(false); return;
	}

//None - try auto
D.write('<'+'Script Src="/ip2geo.php"><'+'/Script>'); 
return;
}

function SetCookie(Name, Value, Time, Path){
var Res;

Value=Value.toString(); if (Value=="") Time=-1;
if (Time==null) Time=3600*24*30*3; //3 months
if (Path==null) Path="/";

Res=Name+"="+Value+
	(Time!=0 ? ";expires="+_CookieDate(Time) : "")+";path="+Path;
D.cookie=Res; return false;
}

function _CookieDate(Time){
var DT=new Date();

if (Time!=null){
	End=new Date(Date.UTC(DT.getUTCFullYear(), DT.getUTCMonth(),
		 DT.getUTCDate(), DT.getUTCHours(), DT.getUTCMinutes(),
		 DT.getUTCSeconds())+Time*1000);
	}
else End=DT;
return End.toGMTString();
}

function GetCookies(){
if (aCookies==null) aCookies=ReadCookies();
}

function ReadCookies(){
var Ck=D.cookie, i, Crumb, Len, Cookies; 

if (!Ck) return [];
Ck=Ck.split('; '); Len=Ck.length; Cookies=new Array(Len);

for (i=0; i<Len; i++){
	Crumb=Ck[i].split("="); 
	if (Crumb.length==1) continue;
	Cookies[Crumb[0]]=unescape(Crumb[1].replace(/\+/g, ' '));
	}
return Cookies;
}

function getQueryStringVariable(variable){

var query=W.location.search.substring(1);
if (query.length<=0) return null;
var vars=query.split('&');

for (var i=0; i<vars.length; i++){
	var pair=vars[i].split('=');
	if (pair[0]==variable) return pair[1];
	}
return null;
}

function ShowStatesForm(){
var Html, aCfg, aHeight, aPageCfg, Id=ni_ad_client, BtmMrg;

aCfg={2663:[1, 200, 'car1', 62, 26], 2664:[1, 200, 'car2', 62, 26], 2665:[3, 213, 'doctor', 24, 18],
	2666:[5, 181, 'home', 18, 25], 2667:[6, 181, 'home_loan', 18, 18], 2669:[4, 181, 'moto', 18, 25],
	2670:[2, 181, 'business', 24, 20]};

aPageCfg=aCfg[Id];
if (!aPageCfg) alert('No page config for '+Id);
BtmMrg=(Id!=2667 ? 5 : 6);

Html='<form name="form1" id="form1" method="get" action="'+W.location.pathname+'">'+
	'<table width="648" border="0" cellpadding="0" cellspacing="0" class="bg_banner"'+
	' Style="background-image:url(images/'+aPageCfg[2]+'.jpg);">'+
	'<tr>'+
	'<td class="forma" Style="padding-right:'+aPageCfg[3]+'px; padding-bottom:'+aPageCfg[4]+'px;">'+
		(Id!=2667 ? '' :
		'<select class="form_banner" Style="margin-bottom:'+BtmMrg+'px;" name="loantype" id="loantype">'+
		'<option value="1">Home Purchase'+
		'<option value="2">Refinance'+
		'<option value="3">Home Equity'+
		'<option value="4">Mobile Home'+
		'<option value="5">All other home loans'+
		'</select><br />')+
		MakeStatesHtml()+'<br />'+
	'<input type="image" name="Submit" value="Submit" src="images/go'+aPageCfg[0]+'.gif "/>'+
	'</td>'+
	'<td><img src="images/spacer.gif" width="1" height="'+aPageCfg[1]+'" /></td>'+
	'</tr></table></form>';
	//<tr><td colspan=2><img src="images/spacer.gif" width="647" height="1" /></td></tr>

D.write(Html);
}

function MakeStatesHtml(){
var Abbr, State, Html;

Html='<select class="form_banner" name="statecode" id="statecode">'+
	'<option value=0>Choose State...';
for(Abbr in aStates) Html+='<option value='+Abbr+
	(Abbr==StateAbbr ? ' Selected' : '')+'>'+aStates[Abbr];
Html+='</Select>'; return Html;
}

function getbyid(id){return D.getElementById(id);}

function ParseResults(){
var oDiv;

oDiv=getbyid('listingResults');
if (!oDiv){
	setTimeout('ParseResults()', 100); return;
	}

FixResultsHtml(oDiv);
}

function FixResultsHtml(oDiv){
var aP, P, Child, Sample, Text, Html, RE, UL;

Sample=String.fromCharCode(160)+String.fromCharCode(8226);
RE=new RegExp("(<BR>)?&nbsp;"+String.fromCharCode(8226)+" ", "g");

aP=oDiv.getElementsByTagName('P');
for(i=0; i<aP.length; i++){
	P=aP[i]; Child=P.firstChild;
	if (!Child) continue;
	Text=Child.data;
	if (Text && Text.substr(0, 2)==Sample){
		Html=P.innerHTML;
		Html=Html.replace(RE, '<LI>');
		UL=D.createElement("UL"); UL.innerHTML=Html;
		P.parentNode.insertBefore(UL, P);
		P.parentNode.removeChild(P);
		}
	}
}

//------------------------------------------------------------------------

GetStatePar();
//setTimeout('ParseResults()', 100);
