// Default the search type to mls
var SearchType = "";

function RemoveInputText(I)
{
	I.value = "";
}


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 "pCity": 
				SearchType = "city";

				break;
			case "pArea": 
				SearchType = "area";

				break;
			case "pschooldistrict":
				SearchType = "schooldistrict";

				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 "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 "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";
                if (document.frm.pschooldistrict.value == '')
                {
                    // Empty
                }
                else
                {
                    if (document.frm.pschooldistrict.value.substring(0,1) == "'")
                    {
                        // Good, the single quote is already there
                        //alert('No Add 1');
                    }
                    else
                    {
                        document.frm.pschooldistrict.value = "'" + document.frm.pschooldistrict.value;
                    }
                    if (document.frm.pschooldistrict.value.substring(document.frm.pschooldistrict.value.length-1,document.frm.pschooldistrict.value.length) == "'")
                    {
                        // Good, the single quote is already there
                        //alert('No Add 2');
                    }
                    else
                    {
                        document.frm.pschooldistrict.value = document.frm.pschooldistrict.value + "'";
                    }
                    //document.frm.pCity.value = "'" + document.frm.pCity.value + "'";
                }
                
            }
            break;   
			
        default : 
            document.frm.pCity.focus();
            alert('Please enter search criteria, then press Search.');
            return false;
    }	
	//alert(SearchType);
	document.frm.submit();
}
