function loadQB()
{
	document.getElementById("zipcode").focus();
}

function keyDown(e, provider)
{
	var code;
	if (document.all)
		code = window.event.keyCode;
	else code = e.which;

	if ( code == 13 )
		if ( provider == 1)
			goEsurance();
		else
			goInsuranceLeadz();
}

function continueQuote(zip)
{
	zip = zip.replace(/^\s+|\s+$/, '');

	if ( zip.length < 5 )
	{
		alert("Please enter a valid zip code.");
		return false;
	}

	zip = zip.substring(0,5);
	var i;
	for (i=0; i<5; i++)
	{
		if ( zip[i] < '0' || zip[i] > '9' )
		{
			alert("Please enter a valid zip code.");
			return false;	
		}
	}

	return true;
}

function goNextInsure()
{
	var zip = document.getElementById("zipcode").value;
	return continueQuote(zip);
}

function goEsurance()
{
	var zip = document.getElementById("zipcode").value;
	if ( continueQuote(zip) )
		document.location.href = "http://www.esurance.com/?promoid=JHOST002&zipcode=" + zip + "&submitted=true";
}

function goInsuranceLeadz()
{
	var zip = document.getElementById("zipcode").value;
	if ( continueQuote(zip) )
		document.location.href = "https://www.insuranceleadz.com/form/auto?zip=" + zip + "&a=37331&t=152";
}

function BookmarkThis() 
{
	var url = document.location.href;
	var title = document.title;

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}

function Delicious() 
{
	document.location.href = "http://del.icio.us/post?url=" + document.location.href + "&title=" + document.title;
}

/***********

Agent Finder

***********/

var afHttp;

function createRequestObject()
{
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

function findAddress()
{
	selectedAddress = document.getElementById("address").value;
	var url = "../../car-insurance-tools/google-address.php?address=" + escape(selectedAddress);
	afHttp = createRequestObject();
	afHttp.open("get", url);
	afHttp.onreadystatechange = readGoogleResponse;
	afHttp.send(null);
}

function readGoogleResponse() 
{
	var response;
	if (afHttp.readyState == 4) 
		response = afHttp.responseText;	
	else return;
	var tabs = response.split(",");
	
	var d = document.getElementById("afStatus");

	d.style.display = "block";
	if ( tabs[0] == "200" )
	{
		d.innerHTML = "Address Found.";
		populateAgents( parseFloat(tabs[2]), parseFloat(tabs[3]) );
	}
	else
		d.innerHTML = "Error: Google can't find that address";
}

function populateAgents(lat, lon)
{
	var url = "../../car-insurance-tools/get-agents2.php?lat=" + lat + "&lon=" + lon;

	var d = document.getElementById("afStatus");
	d.innerHTML = "Retrieving agents...";// + "<p>" + url;
	
	afHttp = createRequestObject();
	afHttp.open("get", url);
	afHttp.onreadystatechange = readAgents;
	afHttp.send(null);
}

function readAgents() 
{
	var response;

	if (afHttp.readyState == 4)
		response = afHttp.responseText;	
	else return;

	var lines = response.split("\r\n");
	agents = new Array();

	if ( lines[0] != "200" )
	{
		var d = document.getElementById("afStatus");
		alert( "An error occurred while processing your request." );
		return;
	}

	for (i=1; i<lines.length; i++)
	{
		var line = lines[i];
		line = line.replace(/^\s+|\s+$/, '');
		if ( line.length ==  0 )
			continue;
		var agent = new Agent(line);
		agents.push(agent);
	}

	if ( agents.length <= 0 )
	{
		var d = document.getElementById("afStatus");
		d.innerHTML = "No agents found";
		alert( "We couldn't find any agents within 20 miles of this address." );
		return;
	}
	else
	{
		var html = new Array();
		html.push("<div id=\"nearby\">Agents nearest to " + document.getElementById("address").value.toUpperCase() + "</div>");
		for (i=0; i<agents.length; i++)
		{
			var a = agents[i];
			html.push( "<div class=\"agent\">" );
			html.push( "<div class=\"m\">" + a.distance.toFixed(2) + " miles distant</div>" );
			html.push( "<div class=\"u\"><a href=\"../update.php?id=" + a.id + "\" rel=nofollow><img src=\"../files/u.gif\" border=\"0\"></a></div>" );
			if ( a.website != "" )
				html.push( "<div class=\"n\"><a href=\"" + a.website + "\" target=\"new\" rel=nofollow>" + a.name + "</a></div>" );
			else
				html.push( "<div class=\"n\">" + a.name + "</div>" );
			html.push( "<div class=\"a\">" + a.address + ", " + a.city + ", " + a.state + " " + a.zip + "</div>" );
			html.push( "<div class=\"p\">" + a.phone + "</div>");
			html.push( "<div class=\"g\">" + a.agencies + "</div>");
			if ( a.agents != null )
			{
				html.push( "<div class=\"g\">Agents: " );
				for ( var j=0; j<a.agents.length; j++ )
				{
					if ( j != 0 ) 
						html.push(", ");
					if ( a.agencyWebsites[j] != "" )
						html.push( "<a href=\"" + a.agencyWebsites[j] + "\" target=\"new\" class=\"nm\" rel=nofollow>" + a.agents[j] + "</a>" );
					else
						html.push( a.agents[j] );
				}
				html.push( "</div>" );
			}

			if ( a.description != "" )
				html.push( "<div class=\"d\">" + a.description + "</div>");

		}
		document.getElementById("agents").innerHTML = html.join("");
		document.getElementById("afStatus").innerHTML = agents.length + " agents found. Look left.";
	}
}

function Agent(line)
{
	var splits = line.split(";");
	this.id = splits[0];
	this.name = splits[1];
	this.agencies = splits[2];
	this.address = splits[3];
	this.city = splits[4];
	this.state = splits[5];
	this.zip = splits[6];
	this.website = splits[7];
	this.phone = splits[8];
	this.description = splits[9];
	if ( splits[10] != "" )
		this.agents = splits[10].split(",");
	else
		this.agents = null;
	if ( splits[11] != "" )
		this.agencyWebsites = splits[11].split(",");
	else
		this.agencyWebsites = null;
	this.distance = parseFloat(splits[12]);
}

/*****
*
*  Reminder Code
*
******/

var rmHttp;

function setReminder()
{
	var many = document.getElementById("rmMany").value;
	var often = document.getElementById("rmOften").value;
	var email = document.getElementById("rmEmail").value;

	if ( trim(email) == "" )
	{
		alert("Please enter an e-mail address for us to send the reminder to.");
		return;
	}

	rmHttp = createRequestObject();
	rmHttp.open("post", "../files/set-reminder.php", true);
	var params = "many=" + many + "&often=" + often + "&email=" + escape(email) + "&page=" + escape(document.location.href);
	rmHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	rmHttp.setRequestHeader("Content-length", params.length);
	rmHttp.setRequestHeader("Connection", "close");
	rmHttp.onreadystatechange = function() {
			var response;
			if( rmHttp.readyState == 4){
				response = rmHttp.responseText;
			}
			else return;
			alert( response );		
	}
	rmHttp.send(params);
}

function trim(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}