// Default the search type to mls
var searchStarted = false;

function $$(a) {
	if (document.getElementById(a)) {
		return document.getElementById(a);
	} else {
		return document.getElementsByName(a)[0];
	}

}

function OnBlurFunc(I) 
{
    if ($$("SearchType"))
    {
	    $$("SearchType").value = I.getAttribute("search_type");		
	}
}

function OnBlurDefaultFunc(I,DefaultText)
{
	if (I.value == "") {
		I.value = DefaultText;
	}
    if ($$("SearchType"))
    {
	    $$("SearchType").value = I.getAttribute("search_type");		
	}
}

function RemoveInputText(I) 
{	
	ClearFields(I, false);
}

function RemoveDefaultInputText(I,DefaultText) 
{	
	if (I.value == DefaultText) {
		ClearFields(I, false);
	}
}

function InputMouseOut(T, text) 
{
	T.value = text;
}

function ClearFields(T, SetToBlank) 
{
	if (searchStarted) 
	{
	    return;
	}
	
	// This will check the quick search fields and reset all of them except the current field passed in
	if (SetToBlank) 
	{
		// These need to remain set to empty string
		//if (document.getElementById("Search")) document.getElementById("Search").value = ""; 
		if (document.getElementById("tmpCity") && document.getElementById("tmpCity").type == "text") 
		{
		    document.getElementById("tmpCity").value = ""; 
		}
		if (document.getElementById("tmpCounty") && document.getElementById("tmpCounty").type == "text") 
		{
		    document.getElementById("tmpCounty").value = ""; 
		}
		if (document.getElementById("pCity") && document.getElementById("pCity").type == "text") 
		{
		    document.getElementById("pCity").value = ""; 
		}
		if (document.getElementById("pCounty") && document.getElementById("pCounty").type == "text") 
		{
		    document.getElementById("pCounty").value = ""; 
		}
		if (document.getElementById("pArea") && document.getElementById("pArea").type == "text") 
		{
		    document.getElementById("pArea").value = "";
		}
		if (document.getElementById("HomeID") && document.getElementById("HomeID").type == "text") 
		{
		    document.getElementById("HomeID").value = ""; 
		}
		if (document.getElementById("AgentNameSearch") && document.getElementById("AgentNameSearch").type == "text") 
		{
		    document.getElementById("AgentNameSearch").value = ""; 
		}
		if (document.getElementById("pSchoolDistrict") && document.getElementById("pSchoolDistrict").type == "text") 
		{
		    document.getElementById("pSchoolDistrict").value = ""; 
		}		
		if (document.getElementById("pSubdivision") && document.getElementById("pSubdivision").type == "text") 
		{
		    document.getElementById("pSubdivision").value = ""; 
		}
		if (document.getElementById("pSuburb") && document.getElementById("pSuburb").type == "text") 
		{
		    document.getElementById("pSubdivision").value = ""; 
		}
		if (document.getElementById("pZip") && document.getElementById("pZip").type == "text") 
		{
		    document.getElementById("pZip").value = "";
		}
		if (document.getElementById("pHSchool") && document.getElementById("pHSchool").type == "text") 
		{
		    document.getElementById("pHSchool").value = "";
		}
		if (document.getElementById("pNeighborhood") && document.getElementById("pNeighborhood").type == "text") 
		{
		    document.getElementById("pNeighborhood").value = "";
		}
		if (document.getElementById("minsqft") && document.getElementById("minsqft").type == "text") 
		{
		    document.getElementById("minsqft").value = ""; 
		}
	} 
	else 
	{
		// These are set to the default value to display when the user is not typing in a box.  
		// Search should remain set to empty string here		
		ClearField("HomeID", T);
		ClearField("StreetName", T);
		ClearField("pZip", T);
		ClearField("tmpCity", T);
		ClearField("tmpCounty", T);
		ClearField("pCity", T);
		ClearField("pCounty", T);
		ClearField("pArea", T);
		ClearField("AgentNameSearch", T);
		ClearField("pSchoolDistrict", T);
		ClearField("pSubdivision", T);
		ClearField("pSuburb", T);
		ClearField("pHSchool", T);
		ClearField("pNeighborhood", T);
		ClearField("minsqft", T);		
		ClearField("MLSArea", T);
	}
}

function ClearField(id, T) 
{
	// Check if the field exists and if it is a text box	
	if (document.getElementById(id) && document.getElementById(id).type == "text") 
	{
	    //alert(document.getElementById(id).getAttribute("search_type") + "==" +  T.getAttribute("search_type"));
	    
		if (document.getElementById(id).getAttribute("search_type") == T.getAttribute("search_type")) 
		{
			// Same search type, so only reset if it is blank
			if (document.getElementById(id).id == T.id) 
			{
				document.getElementById(id).value = "";
			} 
			else 
			{
				if (document.getElementById(id).value == "") 
				{
				    document.getElementById(id).value = document.getElementById(id).getAttribute("default_value");
				}
			}			
		} 
		else 
		{
			// Not the same search type, always reset this field
			document.getElementById(id).value = document.getElementById(id).getAttribute("default_value");			
		}	
	}
}

function searchkeydown(I, e)
{
	
	var sType = I.getAttribute("search_type");
	
    if (I.value == '')
    {
        // Don't do anything
    }
    else
    {
        if ($$("SearchType"))
        {
		    $$("SearchType").value = sType;		
		}
	}
	
	var keynum
	if(window.event) 
	{
		keynum = e.keyCode;
	} 
	else if (e.which) 
	{
		keynum = e.which;
	}

	if (keynum == 13)
	{
	    if ($$("SearchType"))
	    {
		    doSearch($$("SearchType").value);	
		}
		else
		{
		    doSearch("");
		}
    }
}

function SearchClick() 
{   
	if (document.getElementById("MapMyResults")) 
	{
	    document.getElementById("MapMyResults").value = "N";
	}
	if (document.getElementById("criteriaselector")) 
	{
	    SetSearchType(document.getElementById("criteriaselector"));
	}
	doSearch($$("SearchType").value);
}

function MapSearchClick() 
{   
	if (document.getElementById("MapMyResults")) 
	{
	    document.getElementById("MapMyResults").value = "Y";
	}
	doSearch($$("SearchType").value);
}

function doSearch(type)
{
	
	searchStarted = true;
	var doSearch = false;
	if (type == "" || type == null) 
	{
	    if ($$("SearchType"))
	    {
	        type = $$("SearchType").value;
	    }
	    if (type == "" || type == null) 
	    {
	        type = "all";
	        if ($$("SearchType"))
	        {
	            $$("SearchType").value = type;
	        }
	    }
	}
	else 
	{
	    if ($$("SearchType"))
	    {
	        $$("SearchType").value = type;
	    }
	}
	
	//alert("type=" + type);
		
	switch (type)
	{
        case "mls": 
            if (document.getElementById("MLSArea"))
            {
                document.getElementById("MLSArea").value = "";
            }
			if (document.getElementById("HomeID").value == "" || document.getElementById("HomeID").value == "MLS ID#")
            {
				searchStarted = false;
				document.getElementById("HomeID").focus();                
                alert('Please fill out information on the MLS#');   
                return false;             
            }
            else
            {
                document.frm.action = "viewhome.asp";
				doSearch = true;
            }
            break;
            					
		case "advanced": 
            document.frm.action = "findhomerequest.asp?TSearch=Advanced";
			doSearch = true;
            break;
        
		case "agent": 
            if (document.frm.AgentNameSearch.value == "")
            {
				searchStarted = false;
                document.frm.AgentNameSearch.focus();
                alert('Please fill out information on Agent Name');
                return false;
            }
            else
            {
                document.frm.action = "menuagent.asp";
				doSearch = true;
            }
            break;
		
		case "all":		
          	document.frm.action = "listingresults.asp";
			document.frm.Search.value = "TRUE";
			
			DoSearchField("StreetName", true);
			DoSearchField("pMLSArea", false);			
			DoSearchField("MLSArea", false);
			DoSearchField("minsqft", true);	
			
			DoSearchField("pSubdivision", false);
			DoSearchField("pSchoolDistrict", true);
			DoSearchField("pCity", false);
			DoSearchField("pArea", false);
			DoSearchField("pSuburb", false);
			DoSearchField("pZip", false);
			DoSearchField("pHSchool", false);
			DoSearchField("pNeighborhood", false);			
					
			doSearch = true;
            break;
        
		case "searchboxMLS":
			if (document.getElementById("searchbox").value == "" || document.getElementById("searchbox").value == document.getElementById("searchbox").getAttribute("default_value"))
            {
				searchStarted = false;
                document.getElementById("searchbox").focus();
                alert('Please fill out information on the MLS#');   
                return false;             
            }
            else
            {
                document.frm.action = "viewhome.asp";
				doSearch = true;
            }
			break;
			
		case "searchboxALL":
			document.frm.action = "listingresults.asp";
			document.frm.Search.value = "TRUE";
			
			DoSearchField("searchbox", false);
			doSearch = true;
			break;
			
		case "searchboxSTREET":
			document.frm.action = "listingresults.asp";
			document.frm.Search.value = "TRUE";
			
			DoSearchField("searchbox", true);
			doSearch = true;
			break;
    }	
	
	if (doSearch) 
	{
		if (document.getElementById("MapMyResults") && document.getElementById("MapMyResults").value == "Y") 
		{
			document.frm.action = "NewMap.asp";
		}
		document.frm.submit();
	} 
	else 
	{
		alert("Please specify search criteria");
	}
}

function DoSearchField(id, contains) 
{
    if (document.getElementById(id))
    {
        if (document.getElementById(id).value.indexOf("~") < 1)
        {    
	        // If the field is the same as the default value or the default value with quotes around it, then set the field to blank
	        if (document.getElementById(id) && (document.getElementById(id).value == document.getElementById(id).getAttribute("default_value") 
		        || document.getElementById(id).value == "'" + document.getElementById(id).getAttribute("default_value") + "'")) 
			        document.getElementById(id).value = "";
        	
	        // If the field is not blank, then remove single quotes from existing text and add new ones around the text
	        if (document.getElementById(id) && document.getElementById(id).value != "") 
	        {
		        if (contains) 
		        {
			        document.getElementById(id).value = document.getElementById(id).value.replace(/'/g, "");
		        } 
		        else 
		        {
			        document.getElementById(id).value = "'" + document.getElementById(id).value.replace(/'/g, "") + "'";
		        }
	        }
	    }
	    else
	    {
	        // Don't change this, it is a delimited value
	    }
	}
	
}

function DoHHLogin(T, e) 
{
	var keynum
	if(window.event) 
	{
		keynum = e.keyCode;
	} 
	else if (e.which) 
	{
		keynum = e.which;
	}
	
	if (keynum == 13)
	{
		document.getElementById("hhform").action = "login.asp?submit=Login";
	    document.getElementById("hhform").submit();
		
    }	
}

function DoHHLoginButton() 
{
	document.getElementById("hhform").action = "login.asp?submit=Login";
	document.getElementById("hhform").submit();
}

// This is used when there is a drop down to change the search type
function SetSearchType(ddl) 
{
	st = ddl.value;
	switch (st) 
	{
		case "all":
			document.getElementById("searchbox").name = "";
			$$("SearchType").value = "all";
			break;
		case "mls":
			document.getElementById("searchbox").name = "HomeID";
			$$("SearchType").value = "searchboxMLS";
			break;
		case "city":
			document.getElementById("searchbox").name = "pCity";
			$$("SearchType").value = "searchboxALL";
			break;
		case "county":
			document.getElementById("searchbox").name = "pCounty";
			$$("SearchType").value = "searchboxALL";
			break;
		case "state":
			document.getElementById("searchbox").name = "pState";
			$$("SearchType").value = "searchboxALL";
			break;
		case "address":
			document.getElementById("searchbox").name = "StreetName";
			$$("SearchType").value = "searchboxSTREET";
			break;
		case "zip":
			document.getElementById("searchbox").name = "pZip";
			$$("SearchType").value = "searchboxALL";
			break;
		case "neighborhood":
			document.getElementById("searchbox").name = "pNeighborhood";
			$$("SearchType").value = "searchboxALL";
			break;	
		case "schooldistrict":
			document.getElementById("searchbox").name = "pSchoolDistrict";
			$$("SearchType").value = "searchboxALL";
			break;
		case "subdivision":
			document.getElementById("searchbox").name = "pSubdivision";
			$$("SearchType").value = "searchboxALL";
			break;
		case "hschool":
			document.getElementById("searchbox").name = "pHschool";
			$$("SearchType").value = "searchboxALL";
			break;
	}	
}

function SetSearchTypeTextKeyDown(e) 
{
	var keynum
	if(window.event) 
	{
		keynum = e.keyCode;
	} 
	else if (e.which) 
	{
		keynum = e.which;
	}

	if (keynum == 13)
	{
		doSearch($$("SearchType").value);	
    }
}
