// Quick Search

//Enter the URL to the blank template hosted by Sonic Eagle here
//var urlPath = "http://devpub.porchlight.agentachieve.com";
var urlPath = "http://public.porchlight.agentachieve.com";
//Enter the County list from the MLS
var CountyList = "Teller,Summit,Morgan,Grand,Larimer,El Paso,Gilpin,Clear Creek,Broomfield,Park,Elbert,Boulder,Weld,Douglas,Adams,Jefferson,Arapahoe,Denver";

function openWin(url)
{
	winPopup.height = screen.availHeight - 50;
	winPopup.width = screen.availWidth - 50;
	winPopup.top = 25;
	winPopup.left = 25;
	winPopup.url = url;
	winPopup.open();
}
	
function clearForEntry(obj)
{
	if(obj.value == "City" || obj.value == "Zip" || obj.value == "MLS#")
		obj.value = "";
}

function testForValidEntry(obj)
{
	var confirmText = "You can only search by one of these fields (City, Zip Code or MLS) at a time.\n\nWould you like to clear out the other fields and keep the value that you just entered?";
	
	switch(obj.id)
	{
		case "txtCity":
			if(obj.value == "")
				obj.value = "City";
			else
			{
				if(obj.value != "City" && (document.getElementById('txtZipCode').value != "Zip" || document.getElementById('txtMLSID').value != "MLS#"))
				{
					if(confirm(confirmText))
					{
						document.getElementById('txtZipCode').value = "Zip";
						document.getElementById('txtMLSID').value = "MLS#";
					}
					else
						document.getElementById('txtCity').value = "City";
				}
			}
			break;
		case "txtZipCode":
			if(obj.value == "")
				obj.value = "Zip";
			else
			{
				if(obj.value != "Zip" && (document.getElementById('txtCity').value != "City" || document.getElementById('txtMLSID').value != "MLS#"))
				{
					if(confirm(confirmText))
					{
						document.getElementById('txtCity').value = "City";
						document.getElementById('txtMLSID').value = "MLS#";
						validateZipCode(obj.value);
					}
					else
						document.getElementById('txtZipCode').value = "Zip";
				}
				else
				{
					validateZipCode(obj.value);
				}
			}
			break;
		case "txtMLSID":
			if(obj.value == "")
				obj.value = "MLS#";
			else
			{
				if(obj.value != "MLS#" && (document.getElementById('txtCity').value != "City" || document.getElementById('txtZipCode').value != "Zip"))
				{
					if(confirm(confirmText))
					{
						document.getElementById('txtCity').value = "City";
						document.getElementById('txtZipCode').value = "Zip";
					}
					else
						document.getElementById('txtMLSID').value = "MLS#";
				}
			}
			break;
	}
}

function validateZipCode(number)
{
	if(number != "Zip" && (isNaN(number) || (number.toString().length != 5)))
	{
		alert("Please enter a valid zip code.");
		document.getElementById('txtZipCode').value = "Zip";
	}
}

function submitQuickSearch()
{
	var bolError = false;
	var errorMessage = "";
	
	if(document.getElementById('txtCity').value == "City" && document.getElementById('txtZipCode').value == "Zip" && document.getElementById('txtMLSID').value == "MLS#")
	{
		errorMessage += "Please enter a City, Zip Code or MLS number.\n";
		bolError = true;
	}
	
	if(document.getElementById("cboMinPrice").value != "" && document.getElementById("cboMaxPrice").value != "")
	{
		if(parseInt(document.getElementById("cboMinPrice").value) > parseInt(document.getElementById("cboMaxPrice").value))
		{
			errorMessage += "Your minimum price cannot be higher that your maximum price.\n";
			bolError = true;
		}
	}
	
	if(document.getElementById("cboMinBeds").value != "" && document.getElementById("cboMaxBeds").value != "")
	{
		if(parseInt(document.getElementById("cboMinBeds").value) > parseInt(document.getElementById("cboMaxBeds").value))
		{
			errorMessage += "Your minimum number of bedrooms cannot be higher that your maximum number of bedrooms.\n";
			bolError = true;
		}
	}
	
	if(document.getElementById("cboMinBaths").value != "" && document.getElementById("cboMaxBaths").value != "")
	{
		if(parseInt(document.getElementById("cboMinBaths").value) > parseInt(document.getElementById("cboMaxBaths").value))
		{
			errorMessage += "Your minimum number of bathrooms cannot be higher that your maximum number of bathrooms.\n";
			bolError = true;
		}
	}
	
	if(bolError)
		alert(errorMessage);
	else
	{
		document.formSearch.action = "search.php";
		document.formSearch.submit();
	}
}

function search()  {
	
		var l = "";

		if(document.frmHidden.txtMLSID.value == "MLS#") {
			l = urlPath + "/Search/Default.aspx?county=" + CountyList + "&type=R&submit=T" + (encodeURIComponent(document.frmHidden.txtCity.value) != "City" ? "&City=" + encodeURIComponent(document.frmHidden.txtCity.value) : "") + (document.frmHidden.txtZipCode.value != "Zip" ? "&zip=" + encodeURIComponent(document.frmHidden.txtZipCode.value) : "") + "&class=" + encodeURIComponent(document.frmHidden.cboPropertyType.value) + "&NPR=" + encodeURIComponent(document.frmHidden.cboMinPrice.value) + "&XPR=" + encodeURIComponent(document.frmHidden.cboMaxPrice.value) + "&NBD=" + encodeURIComponent(document.frmHidden.cboMinBeds.value) + "&XBD=" + encodeURIComponent(document.frmHidden.cboMaxBeds.value) + "&NBA=" + encodeURIComponent(document.frmHidden.cboMinBaths.value) + "&XBA=" + encodeURIComponent(document.frmHidden.cboMaxBaths.value);		
		}
		else  {
			l = urlPath + "/Search/Default.aspx?ID=" + userID + "&type=I&submit=T&LID=" + encodeURIComponent(document.frmHidden.txtMLSID.value) + "&class=" + encodeURIComponent(document.frmHidden.cboPropertyType.value) + "&NPR=" + encodeURIComponent(document.frmHidden.cboMinPrice.value) + "&XPR=" + encodeURIComponent(document.frmHidden.cboMaxPrice.value) + "&NBD=" + encodeURIComponent(document.frmHidden.cboMinBeds.value) + "&XBD=" + encodeURIComponent(document.frmHidden.cboMaxBeds.value) + "&NBA=" + encodeURIComponent(document.frmHidden.cboMinBaths.value) + "&XBA=" + encodeURIComponent(document.frmHidden.cboMaxBaths.value);
		}

		top.frames['main'].location.href = l;
}