﻿// JScript File
function UpdateZindex(img, compare, show, duration)
{
    var c = document.getElementById(compare)[document.getElementById(compare).selectedIndex].value;
    var s = document.getElementById(show)[document.getElementById(show).selectedIndex].value;
    var d = document.getElementById(duration)[document.getElementById(duration).selectedIndex].value;
    var i = document.getElementById(img);

    i.src = "./images/zindex.ashx?" + c + s + d;
}

function FillCountyCityList(county, city)
{
	var co = document.getElementById(county);
	var ci = document.getElementById(city);
	var counties = new Array("King","Pierce","Snohomish");
	for(i=0; i < counties.length; i++)
		co.options[i]= new Option(counties[i], counties[i]);
	UpdateDropDown(county, city);
	ci.selectedIndex = 38;
}

function ToStringC2(n)
{
	var s=(''+n).split('.');
	s[0]=s[0].split('').reverse().join('').match(/\d{1,3}/gi).join(',').split('').reverse().join('');
	return( "$" + s.join('.'));
}

function PopulateDropDown(lo, hi, be, ba)
{
	var LowPrice = document.getElementById(lo);
	var HighPrice = document.getElementById(hi);
	var Beds = document.getElementById(be);
	var Baths = document.getElementById(ba);

	LowPrice.options[0] = new Option("No Min", "0");

	for (x = 50 * 1000; x < 1000 * 1000; x += 50 * 1000)
	{
		y = ToStringC2(x);
		LowPrice.options[LowPrice.options.length] = new Option(y, x);
		HighPrice.options[HighPrice.options.length] = new Option(y, x);
	}
	for (x = 1000 * 1000; x < 2 * 1000 * 1000; x += 100 * 1000)
	{
		y = ToStringC2(x);
		LowPrice.options[LowPrice.options.length] = new Option(y, x);
		HighPrice.options[HighPrice.options.length] = new Option(y, x);
	}
	for (x = 2 * 1000 * 1000; x < 5 * 1000 * 1000; x += 500 * 1000)
	{
		y = ToStringC2(x);
		LowPrice.options[LowPrice.options.length] = new Option(y, x);
		HighPrice.options[HighPrice.options.length] = new Option(y, x);
	}
	for (x = 0; x <= 5; x++)
	{
		y = x + "+";
		Beds.options[Beds.options.length] = new Option(y, x);
		Baths.options[Baths.options.length] = new Option(y, x);
	}

	HighPrice.options[HighPrice.options.length] =  new Option("No Max", "1000000000");
	HighPrice.selectedIndex = HighPrice.options.length-1;
}

function UpdateDropDown(county, city)
{
	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");
	}
	for(i=0; i < cities.length; i++)
		ci.options[i]= new Option(cities[i], cities[i]);
}

function SearchForHomes(county, city, beds, baths, low, high)
{    
    var co = document.getElementById(county)[document.getElementById(county).selectedIndex].value;
    var ci = document.getElementById(city)[document.getElementById(city).selectedIndex].value;
    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;   
    
    
    url = "Results.aspx?orderby=-ListPrice&county=" + escape(co);
    url += "&city=" + escape(ci);
    if (lo != "-") 
    {
        url += "&low=" + lo;
    }
    if (hi != "-") 
    {
        url += "&high=" + hi;
    }
    if (be != "-") 
    {
    url += "&beds=" + be;
    }
    if (ba != "-") 
    {
        url += "&baths=" + ba;    
    }
    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 #");
	}
}
