﻿// JScript File
function UpdateDropDown(county, city, community)
{
	var co = document.getElementById(county);
	var ci = document.getElementById(city);
	ci.options.length=0;

	if(co[co.selectedIndex].value == 'King')
	{
		var cities = new Array("Algona","Auburn","Baring","Beaux Arts","Bellevue","Black Diamond","Bothell","Burien","Carnation","Clyde Hill","Covington","Des Moines","Duvall","Enumclaw","Fall City","Federal Way","Greenwater","Hobart","Hunts Point","Issaquah","Kenmore","Kent","Kirkland","Lake Forest Park","Maple Valley","Medina","Mercer Island","Milton","Newcastle","Normandy Park","North Bend","Pacific","Preston","Ravensdale","Redmond","Renton","Sammamish","SeaTac","Seattle","Shoreline","Skykomish","Snoqualmie","Snoqualmie Pass","Tukwila","Vashon","Woodinville","Yarrow Point");
	}
	else if(co[co.selectedIndex].value == 'Pierce')
	{
		var cities  =  new Array("Anderson Island","Ashford","Auburn","Bonney Lake","Buckley","Carbonado","Dupont","Eatonville","Edgewood","Elbe","Fife","Fircrest","Fox Island","Gig Harbor","Graham","Greenwater","Kapowsin","Lake Tapps","Lakebay","Lakewood","Longbranch","McKenna","Milton","Orting","Pacific","Parkland","Purdy","Puyallup","Roy","Ruston","South Prairie","Spanaway","Steilacoom","Sumner","Tacoma","University Place","Vaughn","Wauna","Wilkeson");
	}
	else if(co[co.selectedIndex].value == 'Snohomish')
	{
		var cities=new Array("Arlington","Bothell","Brier","Clearview","Darrington","Edmonds","Everett","Gold Bar","Granite Falls","Hat Island","Index","Lake Stevens","Lynnwood","Machias","Maltby","Marysville","Mill Creek","Monroe","Mountlake Terrace","Mukilteo","Silvana","Snohomish","Stanwood","Startup","Sultan","Woodinville","Woodway");
	}
	else if(co[co.selectedIndex].value == 'Kitsap')
	{
        var cities=new Array("Bainbridge Island","Bremerton","Burley","Gorst","Hansville","Indianola","Keyport","Kingston","Manchester","Olalla","Port Gamble","Port Orchard","Poulsbo","Seabeck","Silverdale","South Colby","Southworth","Suquamish","Tracyton","Winslow");
	}
	for(i=0; i <= cities.length; i++)
		ci.options[i]= new Option(cities[i], cities[i]);

	UpdateCommunities(county, city, community);
}

function SearchForHomes(county, city, community, beds, baths, low, high, lowyr, highyr, sq, lsq, sort)
{
	var co = document.getElementById(county)[document.getElementById(county).selectedIndex].value;
	var ci = document.getElementById(city)[document.getElementById(city).selectedIndex].value;
	var com = document.getElementById(community);
	var be = document.getElementById(beds)[document.getElementById(beds).selectedIndex].value;
	var ba = document.getElementById(baths)[document.getElementById(baths).selectedIndex].value;
	var lo = document.getElementById(low)[document.getElementById(low).selectedIndex].value;
	var hi = document.getElementById(high)[document.getElementById(high).selectedIndex].value;   
	var loyr = document.getElementById(lowyr)[document.getElementById(lowyr).selectedIndex].value;    
	var hiyr = document.getElementById(highyr)[document.getElementById(highyr).selectedIndex].value;  
	var sqft = document.getElementById(sq)[document.getElementById(sq).selectedIndex].value;    
	var lotsqft = document.getElementById(lsq)[document.getElementById(lsq).selectedIndex].value;  
	var sort = document.getElementById(sort)[document.getElementById(sort).selectedIndex].value;  

	comv = ""
	for(i = 0; i < com.options.length; i++)
	{
		if(com.options[i].selected)
			comv += com.options[i].value + ",";

	}
	
	url = "Results.aspx?county=" + escape(co);
	url += "&city=" + escape(ci);
	url += "&community=" + escape(comv);
	url += "&low=" + lo;
	url += "&high=" + hi;
	url += "&beds=" + be;
	url += "&baths=" + ba;    
	url += "&lowyr=" + loyr;
	url += "&highyr=" + hiyr;    
	url += "&sqft=" + sqft;
	url += "&lotsqft=" + lotsqft;    
	url += "&orderby=" + sort;    
	window.location = url;
}

function isMlsNum(zip)
{
    if (zip.length == 0)
        return false;

    var re = /^\d{3,8}$/;
    if (!zip.match(re))
        return true;
    else
        return false;
}

function SearchByMLS(mls1, mls2, mls3, mls4)
{
	var m1 = document.getElementById(mls1).value;
	var m2 = document.getElementById(mls2).value;
	var m3 = document.getElementById(mls3).value;
	var m4 = document.getElementById(mls4).value;

    if(isMlsNum(m1))
    {
        alert(m1 + " is an invalid MLS #.");
        return;
    }

    if(isMlsNum(m2))
    {
        alert(m1 + " is an invalid MLS #.");
        return;
    }

    if(isMlsNum(m3))
    {
        alert(m1 + " is an invalid MLS #.");
        return;
    }

    if(isMlsNum(m4))
    {
        alert(m1 + " is an invalid MLS #.");
        return;
    }

    url = "Results.aspx?mls=" + escape(m1);
	url += "," + escape(m2);
	url += "," + escape(m3);
	url += "," + escape(m4);


	if(m1.length + m2.length + m3.length + m4.length != 0)
	{
		window.location = url;
	}
	else
	{
		alert("Please enter an MLS #");
	}
}

function isZipCode(zip)
{
    if (zip.length == 0)
        return false;

    var re = /^\d{5}$/;
    if (!zip.match(re))
        return true;
    else
        return false;
}

function SearchByZIP(zip1, zip2, zip3, zip4)
{
	var m1 = document.getElementById(zip1).value;
	var m2 = document.getElementById(zip2).value;
	var m3 = document.getElementById(zip3).value;
	var m4 = document.getElementById(zip4).value;


    if(isZipCode(m1))
    {
        alert(m1 + " is an invalid zip code.");
        return;
    }

    if(isZipCode(m2))
    {
        alert(m2 + " is an invalid zip code.");
        return;
    }

    if(isZipCode(m3))
    {
        alert(m3 + " is an invalid zip code.");
        return;
    }

    if(isZipCode(m4))
    {
        alert(m4 + " is an invalid zip code.");
        return;
    }



	url = "Results.aspx?zip=" + escape(m1);
	url += "," + escape(m2);
	url += "," + escape(m3);
	url += "," + escape(m4);


    if(m1.length + m2.length + m3.length + m4.length != 0)
	{
		window.location = url;
	}
	else
	{
		alert("Please enter a zip code");
	}

}


function UpdateCommunities(county, city, community)
{
	var c1 = document.getElementById(county).value;
	var c2 = document.getElementById(city).value;

	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = createTable;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {if (xmlDoc.readyState == 4) createTable(community)};
	}

	xmlDoc.load("http://www.preferredres.net/WebService.asmx/GetCommunities?County="+escape(c1)+"&City="+escape(c2)) 
}

function createTable(community)
{
	var x = xmlDoc.getElementsByTagName('string');
	var c3 = document.getElementById(community);
	c3.options.length=0;
	for (j=0; j < x.length ; j++)
	{
		 var theData = x[j].childNodes[0].nodeValue;
		 c3.options[j]= new Option(theData, theData);
	}    
}

