var DontShowMap = false;
var strWebsite = '';
var strName = '';
var strPhone = '';
var strCell = '';
var strCompany = '';
var strPhoto = '';
var nListingID = 0;

function Load()
{
	SetControls();	
	SetMap();
}

function SetMap()
{
	if(showmap || (cno.length == 0 && ci.length == 0 && z.length == 0))
	{
		LoadMap();		
	}
	else if(st.length > 0 && (cno.length > 0 || ci.length > 0 || z.length > 0))
	{
		DontShowMap = true;
		Search();
	}
}

function LoadMap()
{
	var state;
	if(st.length == 0)
		state = "usa";
	else
		state = st;	
			
	Include("js/maps/" + state + ".js", ShowMap);
}

function MapClicked(inState, inCNO)
{
	st = inState;
	cno = inCNO;
	
	if(cno.length == 0)
		showmap = 1;
	else
		showmap = 0;
		
	Load();
}

function ShowMap()
{
	if(DontShowMap)
	{
		DontShowMap = false;
		return;
	}
	
	pg = 1;
	var map = getMap();
		
	var txt = '';
	if(st.length == 0)
		txt = 'To get started, simply click a state below.<br>';
	else
		txt = 'Now select a county.<br><br>';

	document.getElementById("divMap").innerHTML = txt + map;
	document.getElementById("divMap").style.display = '';
	document.getElementById('divResults').style.display = 'none';
	
	if(window.parent != null && window.parent.resizeFrame) window.parent.resizeFrame(575,650);
}

function SetControls()
{
	LoadStates();
	
	if(ci.length > 0) document.getElementById('txtCity').value = ci;
	if(z.length > 0) document.getElementById('txtZip').value = z;		
	
	LoadBeds();
	LoadBaths();
	LoadMinPrice();
	LoadMaxPrice();
	LoadOrderBy();
	LoadDisplay();	
	
	if(ph == 'yes') document.getElementById('chkPhoto').checked = true;
}

function LoadBeds()
{
	var ddl = document.getElementById('ddlBeds');
	ddl.options[0] = new Option('', '');
	ddl.options[1] = new Option('1+', '1');
	ddl.options[2] = new Option('2+', '2');
	ddl.options[3] = new Option('3+', '3');
	ddl.options[4] = new Option('4+', '4');
	ddl.options[5] = new Option('5+', '5');
	
	if(bdi.length > 0)
	{
		for(i=0; i<ddl.options.length; i++)
			if(ddl.options[i].value == bdi)
				ddl.options[i].selected = true;
	}
}

function LoadBaths()
{
	var ddl = document.getElementById('ddlBaths');
	ddl.options[0] = new Option('', '');
	ddl.options[1] = new Option('1+', '1');
	ddl.options[2] = new Option('2+', '2');
	ddl.options[3] = new Option('3+', '3');
	ddl.options[4] = new Option('4+', '4');
	ddl.options[5] = new Option('5+', '5');
	
	if(bhi.length > 0)
	{
		for(i=0; i<ddl.options.length; i++)
			if(ddl.options[i].value == bhi)
				ddl.options[i].selected = true;
	}
}

function LoadMinPrice()
{
	var ddl = document.getElementById('ddlMin');
	ddl.options[0] = new Option('', '');
	ddl.options[1] = new Option('20K+', '20000');
	ddl.options[2] = new Option('40K+', '40000');
	ddl.options[3] = new Option('60K+', '60000');
	ddl.options[4] = new Option('80K+', '80000');
	ddl.options[5] = new Option('100K+', '100000');
	ddl.options[6] = new Option('120K+', '120000');
	ddl.options[7] = new Option('150K+', '150000');
	ddl.options[8] = new Option('175K+', '175000');
	ddl.options[9] = new Option('200K+', '200000');
	ddl.options[10] = new Option('250K+', '250000');
	ddl.options[11] = new Option('300K+', '300000');
	ddl.options[12] = new Option('350K+', '350000');
	ddl.options[13] = new Option('400K+', '400000');
	ddl.options[14] = new Option('500K+', '500000');
	ddl.options[15] = new Option('600K+', '600000');
	ddl.options[16] = new Option('700K+', '700000');
	ddl.options[17] = new Option('800K+', '800000');
	ddl.options[18] = new Option('900K+', '900000');
	
	if(pi.length > 0)
	{
		for(i=0; i<ddl.options.length; i++)
			if(ddl.options[i].value == pi)
				ddl.options[i].selected = true;
	}
}

function LoadMaxPrice()
{
	var ddl = document.getElementById('ddlMax');
	ddl.options[0] = new Option('', '');
	ddl.options[1] = new Option('20K+', '20000');
	ddl.options[2] = new Option('40K+', '40000');
	ddl.options[3] = new Option('60K+', '60000');
	ddl.options[4] = new Option('80K+', '80000');
	ddl.options[5] = new Option('100K+', '100000');
	ddl.options[6] = new Option('120K+', '120000');
	ddl.options[7] = new Option('150K+', '150000');
	ddl.options[8] = new Option('175K+', '175000');
	ddl.options[9] = new Option('200K+', '200000');
	ddl.options[10] = new Option('250K+', '250000');
	ddl.options[11] = new Option('300K+', '300000');
	ddl.options[12] = new Option('350K+', '350000');
	ddl.options[13] = new Option('400K+', '400000');
	ddl.options[14] = new Option('500K+', '500000');
	ddl.options[15] = new Option('600K+', '600000');
	ddl.options[16] = new Option('700K+', '700000');
	ddl.options[17] = new Option('800K+', '800000');
	ddl.options[18] = new Option('900K+', '900000');
	
	if(pa.length > 0)
	{
		for(i=0; i<ddl.options.length; i++)
			if(ddl.options[i].value == pa)
				ddl.options[i].selected = true;
	}
}

function LoadOrderBy()
{
	var ddl = document.getElementById('ddlOrder');
	ddl.options[0] = new Option('', '');
	ddl.options[1] = new Option('Has Photograph', 'ph');
	ddl.options[2] = new Option('Has Video', 'vd');
	ddl.options[3] = new Option('Days on Site', 'dos');
	ddl.options[4] = new Option('Listing Type', 'tl');
	ddl.options[5] = new Option('Street', 's');
	ddl.options[6] = new Option('City', 'ci');
	ddl.options[7] = new Option('State', 'st');
	ddl.options[8] = new Option('Zip Code', 'z');
	ddl.options[9] = new Option('Bedrooms', 'bd');
	ddl.options[10] = new Option('Bathrooms', 'dh');
	ddl.options[11] = new Option('Price', 'p');
	ddl.options[12] = new Option('Zestimate', 'va');
	ddl.options[13] = new Option('Property Type', 'pt');
	
	if(o.length > 0)
	{
		for(i=0; i<ddl.options.length; i++)
			if(ddl.options[i].value == o)
				ddl.options[i].selected = true;
	}
}

function LoadDisplay()
{
	var ddl = document.getElementById('ddlDisplay');
	ddl.options[0] = new Option('', '');
	ddl.options[1] = new Option('15', '15');
	ddl.options[2] = new Option('25', '25');
	ddl.options[3] = new Option('50', '50');
	
	if(ps.length > 0)
	{
		for(i=0; i<ddl.options.length; i++)
			if(ddl.options[i].value == ps)
				ddl.options[i].selected = true;
	}
}

function LoadStates()
{
	var states = getStates();
	var ddl = document.getElementById('ddlStates');
	var i = 1;
	
	ddl.options[0] = new Option('', '');
	for(s in states)
		ddl.options[i++] = new Option(states[s], s);
		
	AutoSelectState();
}

function AutoSelectState()
{
	var ddl = document.getElementById('ddlStates');
	if(st.length > 0)
	{
		for(i = 0; i < ddl.options.length; i++)
		{
			if(ddl.options[i].value.toLowerCase() == st.toLowerCase())
			{
				ddl.options[i].selected = true;
				StateChanged();
				
				if(cno.length > 0)
				{
					ddl = document.getElementById('ddlCounties');
					
					for(j = 0; j < ddl.options.length; j++)
					{
						if(ddl.options[j].value.toLowerCase() == cno.toLowerCase())
						{
							ddl.options[j].selected = true;
							break;
						}
					}
				}
											
				break;
			}
		}
	}
}

function StateChanged()
{
	DontShowMap = false;
	
	st = document.getElementById('ddlStates').value;
	
	var counties = getCounties(st);
	var ddl = document.getElementById('ddlCounties');
	ddl.options.length = 0;
	var i = 1;
	
	ddl.options[0] = new Option('', '');				
	for(c in counties)
		ddl.options[i++] = new Option(counties[c], c.substring(1,4));

	ddl.options[0].selected = true;
	
	if(st.length == 0)
	{
		document.getElementById('lblCounty').disabled = true;
		document.getElementById('ddlCounties').disabled = true;
		document.getElementById('lblCity').disabled = true;
		document.getElementById('txtCity').disabled = true;
		document.getElementById('lblZip').disabled = true;
		document.getElementById('txtZip').disabled = true;
	}
	else
	{
		document.getElementById('lblCounty').disabled = false;
		document.getElementById('ddlCounties').disabled = false;
		document.getElementById('lblCity').disabled = false;
		document.getElementById('txtCity').disabled = false;
		document.getElementById('lblZip').disabled = false;
		document.getElementById('txtZip').disabled = false;
	}
	
	LoadMap();
}

function ShowAdvanced()
{
	var lnk = document.getElementById('lnkAdv');
	if(lnk.innerHTML == 'Advanced Search')
	{
		lnk.innerHTML = 'Simple Search';
		document.getElementById('trAdvanced').style.display = '';
	}
	else
	{
		lnk.innerHTML = 'Advanced Search';
		document.getElementById('trAdvanced').style.display = 'none';
	}
}

function GetParams()
{
	st = document.getElementById('ddlStates').value;
	cno = document.getElementById('ddlCounties').value;
	ci = escape(document.getElementById('txtCity').value);
	z = document.getElementById('txtZip').value;
	
	if(st.length == 0 || (cno.length == 0 && ci.length == 0 && z.length == 0)) return;
	
	bdi = document.getElementById('ddlBeds').value;
	bhi = document.getElementById('ddlBaths').value;
	pi = document.getElementById('ddlMin').value;
	pa = document.getElementById('ddlMax').value;
	o = document.getElementById('ddlOrder').value;
	ps = document.getElementById('ddlDisplay').value;
	if(document.getElementById('chkPhoto').checked)
		ph = 'yes';
	else
		ph = '';
			
	return '?st='+st+'&cno='+cno+'&ci='+ci+'&z='+z+'&bdi='+bdi+'&bhi='+bhi+'&pi='+pi+'&pa='+pa+'&o='+o+'&ps='+ps+'&ph='+ph+'&pg='+pg+'&tab='+tab;	
}

function Search()
{
	var params = GetParams();
	
	var ajax = new GLM.AJAX();
	ajax.callPage('GetListings.aspx' + params, SearchComplete);		
}

function SearchComplete(response)
{
	var results = eval('(' + response + ')');
	
	document.getElementById('lblDisplay').innerHTML = 'Displaying ' + results.start + ' - ' + results.stop + ' of ' + results.total;
	
	var divs = '';
	for(i in results.listings)
	{
		divs += CreateListing(results.listings[i], i%2 == 0);
	}
	
	if(results.start == 0 || results.stop == 0 || (results.start == 1 && results.stop == results.total))
	{
		document.getElementById('imgNext').style.display = 'none';
		document.getElementById('imgPrev').style.display = 'none';
	}
	else if(results.start == 1)
	{
		document.getElementById('imgPrev').style.display = 'none';
		document.getElementById('imgNext').style.display = '';
	}
	else if(results.stop == results.total)
	{
		document.getElementById('imgNext').style.display = 'none';
		document.getElementById('imgPrev').style.display = '';
	}				
	else
	{
		document.getElementById('imgNext').style.display = '';
		document.getElementById('imgPrev').style.display = '';
	}
	
	document.getElementById('divMap').style.display = 'none';
	
	document.getElementById('divListings').innerHTML = divs;
	document.getElementById('divResults').style.display = '';
	
	if(window.parent != null && window.parent.resizeFrame) window.parent.resizeFrame(575, 300 + results.listings.length * 75);
}

function CreateListing(listing, alt)
{
	var ret = '';	
	if(alt)
		ret += '<div id=' + listing.listingSourceId + ' class=\'listingdiv2\' onclick=\'ViewListing(' + listing.listingId + ');\' onmouseover=\'HighlightListing(true, this, true);\' onmouseout=\'HighlightListing(false, this, true);\'>';				
	else
		ret += '<div id=' + listing.listingSourceId + ' class=\'listingdiv1\' onclick=\'ViewListing(' + listing.listingId + ');\' onmouseover=\'HighlightListing(true, this, false);\' onmouseout=\'HighlightListing(false, this, false);\'>';	
	ret += '<table width=100% height=100% cellpadding=0 cellspacing=0 border=0><tr>';
	
	ret += '<td class=\'listingimage\' valign=middle align=center>';
	if(listing.hasPhoto)
		ret += '<img src=\'http://static-mirror.foreclosure.com/publiclistingthumbnail.html?listingId=' + listing.listingId + '\' height=51 width=51 border=0>';
	else
		ret += '<img src=\'images/noimage.gif\' height=51 width=51 border=0>';
	ret += '</td>';
	
	ret += '<td height=100% valign=middle class=\'listingstreet\'><label class=\'normal\'>';
	ret += listing.street + '<br>' + listing.city + ', ' + listing.state + ' ' + listing.zip;
	ret += '</label></td>';
	
	ret += '<td valign=bottom class=\'infotd\'>';
	ret += '<a href=\'#\' class=\'normal\'>More information...</a><br><br>';
	ret += '</td>';
	
	ret += '<td class=\'pricetd\' valign=middle align=center><div class=\'pricediv\'><label class=\'normal\'>';
	if(listing.price.length > 0)
		ret += listing.price;
	else
		ret += 'No Price';
	ret += '</label></div></td>';
	
	ret += '</tr></table>';
	ret += '</div></div>';
	
	return ret;
}

function HighlightListing(over, div, alt)
{
	if(over)
	{
		if(alt)
			div.className = 'listingdiv2hover';
		else
			div.className = 'listingdiv1hover';
	}
	else
	{
		if(alt)
			div.className = 'listingdiv2';
		else
			div.className = 'listingdiv1';
	}
}

function Next()
{
	if(pg == '') pg = 1;
	
	pg = parseInt(pg) + 1;
	Search();
}

function Previous()
{
	if(pg == '') pg = 1;
	
	pg = parseInt(pg) - 1;
	Search();
}

function ChangeTab(idx)
{
	document.getElementById('tab1').src = 'images/1-all-results.gif';
	document.getElementById('tab2').src = 'images/2-foreclosures.gif';
	document.getElementById('tab3').src = 'images/3-preforeclosures.gif';
	document.getElementById('tab4').src = 'images/4-sheriff-sales.gif';
	document.getElementById('tab5').src = 'images/5-bankruptcies.gif';
	document.getElementById('tab6').src = 'images/6-FSBOs.gif';
	document.getElementById('tab7').src = 'images/7-tax-liens.gif';
	document.getElementById('tab8').src = 'images/8-deals.gif';
	
	if(idx == 1)
	{
		tab = '';
		document.getElementById('tab1').src = 'images/1-all-results-over.gif';
	}
	else if(idx == 2)
	{
		tab = 'f';
		document.getElementById('tab2').src = 'images/2-foreclosures-over.gif';
	}
	else if(idx == 3)
	{
		tab = 'p';
		document.getElementById('tab3').src = 'images/3-preforeclosures-over.gif';
	}
	else if(idx == 4)
	{
		tab = 'h';
		document.getElementById('tab4').src = 'images/4-sheriff-sales-over.gif';
	}
	else if(idx == 5)
	{
		tab = 'b';
		document.getElementById('tab5').src = 'images/5-bankruptcies-over.gif';
	}
	else if(idx == 6)
	{
		tab = 'o';
		document.getElementById('tab6').src = 'images/6-FSBOs-over.gif';
	}
	else if(idx == 7)
	{
		tab = 't';
		document.getElementById('tab7').src = 'images/7-tax-liens-over.gif';
	}
	else if(idx == 8)
	{
		tab = 'd';
		document.getElementById('tab8').src = 'images/8-deals-over.gif';
	}
		
	pg = 1;
	Search();
}

function ViewListing(id)
{
	if(document.cookie.indexOf('foreclosurelead=') == -1)
	{
		var params = GetParams() + "&listingid=" + id;
		
		window.location.href = "LeadCapture.aspx" + params;
	}
	else
	{
		nListingID = id;
		ShowForeclosureWindow();
	}
}

function LeadFormLoad()
{
	document.getElementById('optPhone').onclick = PhoneChanged;
	document.getElementById('optEmail').onclick = PhoneChanged;
	
	document.getElementById('txtFirst').focus();
}

function LeadFormValidate()
{
	var txt = document.getElementById('txtFirst');
	if(txt.value.length == 0)
	{
		alert('Please enter your first name.');
		txt.focus();
		return false;
	}
	
	txt = document.getElementById('txtLast');
	if(txt.value.length == 0)
	{
		alert('Please enter your last name.');
		txt.focus();
		return false;
	}
	
	txt = document.getElementById('txtEmail');
	if(txt.value.length == 0)
	{
		alert('Please enter your e-mail address.');
		txt.focus();
		return false;
	}
		
	var phone = true;
	if(document.getElementById('optEmail').checked)
		phone = false;
		
	if(phone)
	{
		txt = document.getElementById('txtPhone');
		if(txt.value.length == 0)
		{
			alert('Please enter your phone number.');
			txt.focus();
			return false;
		}
	}
	
	ShowForeclosureWindow();
	return true;
}

function PhoneChanged()
{
	if(document.getElementById('optPhone').checked)
		document.getElementById('lblPhoneReq').style.display = '';
	else
		document.getElementById('lblPhoneReq').style.display = 'none';
}

function ShowForeclosureWindow()
{
	var args = "?listingid=" + nListingID;
	args += "&refUrl=" + escape(strWebsite);
	args += "&agentName=" + escape(strName);
	args += "&agentPhone=" + escape(strPhone);
	args += "&agentMobile=" + escape(strCell);
	args += "&agencyName=" + escape(strCompany);
	if(strPhoto.length > 0)
		args += "&agentPhotoURL=" + strPhoto;
	args += "&st=" + st;
	args += "&cno=" + cno;
		
	window.open('http://alamode.foreclosure.com/listingdetails.html' + args);
}
