// Default the search type to mls
var SearchType = "";

function onBlurFunc(stype)
{
	SearchType = stype;
}

function RemoveInputText(I)
{
	SearchType = "";
	ClearFields(false);
	I.value = "";
}

function InputMouseOut(T, text)
{
	
	T.value = text;
}

function ClearFields(SetToBlank)
{
	// 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("pCity")) document.getElementById("pCity").value = ""; 
		if (document.getElementById("pArea")) document.getElementById("pArea").value = "";
		if (document.getElementById("HomeID")) document.getElementById("HomeID").value = ""; 
		if (document.getElementById("AgentNameSearch")) document.getElementById("AgentNameSearch").value = ""; 
		if (document.getElementById("pSchoolDistrict")) document.getElementById("pSchoolDistrict").value = ""; 
		if (document.getElementById("pSubdivision")) document.getElementById("pSubdivision").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
		//if (document.getElementById("Search")) document.getElementById("Search").value = ""; 
		if (document.getElementById("pCity")) document.getElementById("pCity").value = "City"; 
		if (document.getElementById("pArea")) document.getElementById("pArea").value = "Area"; 
		if (document.getElementById("HomeID")) document.getElementById("HomeID").value = "Listing Number"; 
		if (document.getElementById("AgentNameSearch")) document.getElementById("AgentNameSearch").value = "Agent Name"; 
		if (document.getElementById("pSchoolDistrict")) document.getElementById("pSchoolDistrict").value = "School District"; 
		if (document.getElementById("pSubdivision")) document.getElementById("pSubdivision").value = "Subdivision"; 
	}
}

function searchkeydown(I)
{
	
    if (I.value == '')
    {
        // Don't do anything
    }
    else
    {
		switch (I.id)
	    {
            case "StreetName": 
                SearchType = "address";
                break;
            case "HomeID": 
                SearchType = "mls";
                break;
            case "AgentNameSearch": 
                SearchType = "agent";
                break;
            case "pCity": 
                SearchType = "city";
                break;
			case "pArea": 
                SearchType = "area";
				break;
            case "pSchoolDistrict":
				SearchType = "schooldistrict";
				break;
			case "pSubdivision":
				SearchType = "subdivision";
				break;
            default : 
                alert("Unknown Search" + I.id);
        }
	}
	if (window.event.keyCode == 13)
	{
	    //alert(SearchType);
		doSearch(SearchType);	
    }
}

function SearchClick()
{   
	doSearch(SearchType);
}

function doSearch(type)
{
	switch (type)
	{
        case "mls": 
            if (document.frm.HomeID.value == "")
            {
                document.frm.HomeID.focus();
                alert('Please fill out information on the MLS#');   
                return false;             
            }
            else
            {
                document.frm.action = "viewhome.asp";
            }
            break;
        case "address": 
            if (document.frm.StreetName.value == "")
            {
                document.frm.StreetName.focus();
                alert('Please fill out information on Street Name');
                return false;
            }
            else
            {
                document.frm.action = "listagenthomes.asp";
            }
            break;
        case "advanced": 
            document.frm.action = "findhomerequest.asp?TSearch=Advanced";
            break;
        case "agent": 
            if (document.frm.AgentNameSearch.value == "")
            {
                document.frm.AgentNameSearch.focus();
                alert('Please fill out information on Agent Name');
                return false;
            }
            else
            {
                document.frm.action = "menuagent.asp";
            }
            break;
			
		case "schooldistrict": 
            if (document.frm.pSchoolDistrict.value == "")
            {
                document.frm.pSchoolDistrict.focus();
                alert('Please fill out information on School District');
                return false;
            }
            else
            {
                document.frm.action = "listagenthomes.asp";
                document.frm.Search.value = "TRUE";
				document.frm.pSchoolDistrict.value = "'" + document.frm.pSchoolDistrict.value + "'";
                
            }
            break;
        case "city": 
        
            if (document.frm.pCity.value == "" )
            {
                document.frm.pCity.focus();
                alert('Please fill out information on City');
                return false;
            }
            else
            {
                document.frm.action = "listagenthomes.asp";
                document.frm.Search.value = "TRUE";
                if (document.frm.pCity.value == '')
                {
                    // Empty
                }
                else
                {
                    if (document.frm.pCity.value.substring(0,1) == "'")
                    {
                        // Good, the single quote is already there
                        //alert('No Add 1');
                    }
                    else
                    {
                        document.frm.pCity.value = "'" + document.frm.pCity.value;
                    }
                    if (document.frm.pCity.value.substring(document.frm.pCity.value.length-1,document.frm.pCity.value.length) == "'")
                    {
                        // Good, the single quote is already there
                        //alert('No Add 2');
                    }
                    else
                    {
                        document.frm.pCity.value = document.frm.pCity.value + "'";
                    }
                    //document.frm.pCity.value = "'" + document.frm.pCity.value + "'";
                }
                
            }
            break;   
		
		case "area": 
        
            if (document.frm.pArea.value == "" )
            {
                document.frm.pArea.focus();
                alert('Please fill out information on Suburb');
                return false;
            }
            else
            {
                document.frm.action = "listagenthomes.asp";
                document.frm.Search.value = "TRUE";
                if (document.frm.pArea.value == '')
                {
                    // Empty
                }
                else
                {
                    if (document.frm.pArea.value.substring(0,1) == "'")
                    {
                        // Good, the single quote is already there
                        //alert('No Add 1');
                    }
                    else
                    {
                        document.frm.pArea.value = "'" + document.frm.pArea.value;
                    }
                    if (document.frm.pArea.value.substring(document.frm.pArea.value.length-1,document.frm.pArea.value.length) == "'")
                    {
                        // Good, the single quote is already there
                        //alert('No Add 2');
                    }
                    else
                    {
                        document.frm.pArea.value = document.frm.pArea.value + "'";
                    }
                    //document.frm.pCity.value = "'" + document.frm.pCity.value + "'";
                }
                
            }
			break;
		
		case "subdivision": 
        
            if (document.frm.pSubdivision.value == "" )
            {
                document.frm.pSubdivision.focus();
                alert('Please fill out information on Subdivision');
                return false;
            }
            else
            {
                document.frm.action = "listagenthomes.asp";
                document.frm.Search.value = "TRUE";
                if (document.frm.pSubdivision.value == '')
                {
                    // Empty
                }
                else
                {
                    if (document.frm.pSubdivision.value.substring(0,1) == "'")
                    {
                        // Good, the single quote is already there
                        //alert('No Add 1');
                    }
                    else
                    {
                        document.frm.pSubdivision.value = "'" + document.frm.pSubdivision.value;
                    }
                    if (document.frm.pSubdivision.value.substring(document.frm.pSubdivision.value.length-1,document.frm.pSubdivision.value.length) == "'")
                    {
                        // Good, the single quote is already there
                        //alert('No Add 2');
                    }
                    else
                    {
                        document.frm.pSubdivision.value = document.frm.pSubdivision.value + "'";
                    }
                    //document.frm.pCity.value = "'" + document.frm.pCity.value + "'";
                }
                
            }
			break;
		
        default : 
			// For this client, allow search with no criteria to search for max/min price only
			ClearFields(false);
			document.frm.Search.value = "TRUE";
			document.frm.action = "listagenthomes.asp";
            //document.frm.pCity.focus();
            //alert('Please enter search criteria, then press Search.');
            //return false;
    }	
	//alert(SearchType);
	document.frm.submit();
}
