﻿// JScript File

function alerting(str)
{
    alert (str);
}

function removeinput(x) 
{
    if (x.value == 'min' || x.value == 'max' || x.value == 'City & State or Zip Code' || x.value == 'type your search here')
    {
        x.value = '';
    }
}

function setinput(x) 
{
    if(x.id.indexOf("txtLocation") > -1)
    {
        if(x.value == "")
            x.value = 'City & State or Zip Code';
        return;
    }   
    
    if(x.id.indexOf("txtSearch") > -1)
    {
        if(x.value == "")
            x.value = 'type your search here';
        return;
    }    
}

function validateSearch()
{
    return true; // Search panel validation stopped on 02/21/2010 (since we should get results no matter what the user types. if didn't type, then show latest ads)
    
    var SearchForErr = "N",SectionErr = "N",CategoryErr = "N",LocationErr = "N",PriceErr = "N";
    var OriginalColor = "Black";
    
    // Use Prefix since we use master page in some of the forms.
    var txtPrefix = "Search$"; 
    var lblPrefix = "Search_";
    var txtSearchFor = document.getElementById("ctl00_ContentPlaceHolder1_Search_txtSearchFor");
    if(txtSearchFor != null) // ==> We use master page.
    {   
        txtPrefix = "ctl00_ContentPlaceHolder1_Search_";
        lblPrefix = "ctl00_ContentPlaceHolder1_Search_";
    }
    
    //var txtSearchFor = document.getElementById("ctl00_Search_txtSearchFor"); //To be used with MasterPage !!!
    var txtSearchFor = document.getElementById(txtPrefix + "txtSearchFor");
    var section = document.getElementById(txtPrefix + "cmbSection");
    var category = document.getElementById(txtPrefix + "cmbCategory");
    var location = document.getElementById(txtPrefix + "txtLocation");
    var minprice = document.getElementById(txtPrefix + "txtMin");
    var maxprice = document.getElementById(txtPrefix + "txtMax");
    
	if(txtSearchFor==null || txtSearchFor.value=="" || txtSearchFor.value.length < 1)
	{ 
	    document.getElementById(lblPrefix + "lblSearchFor").style.color = "Red";
	    SearchForErr = "Y";
	}
	else
	{
	    document.getElementById(lblPrefix + "lblSearchFor").style.color = OriginalColor;
	    SearchForErr = "N";
	}
	if(section == null || section.value == "")
	{   
	    document.getElementById(lblPrefix + "lblSection").style.color = "Red";
	    SectionErr = "Y";
	}
	else
	{
	    document.getElementById(lblPrefix + "lblSection").style.color = OriginalColor;
	    SectionErr = "N";
	}
	if(category == null || category.value == "")
	{
	    document.getElementById(lblPrefix + "lblCategory").style.color = "Red";
	    CategoryErr = "Y";
	}
	else
	{
	    document.getElementById(lblPrefix + "lblCategory").style.color = OriginalColor;
	    CategoryErr = "N";
	}
	if(IsLocation(location) == false )
	{
	    document.getElementById(lblPrefix + "lblLocation").style.color = "Red";
	    LocationErr = "Y";
	}
	else
	{
	    document.getElementById(lblPrefix + "lblLocation").style.color = OriginalColor;
	    LocationErr = "N";
	}
    if(minprice.value == "min" || IsNumeric(minprice.value) == false)
        minprice.value = "";
    if(maxprice.value == "max" || IsNumeric(maxprice.value) == false)
        maxprice.value = "";    

	
	if(SearchForErr == "Y" || SectionErr == "Y" || CategoryErr == "Y" || LocationErr == "Y" || PriceErr == "Y")
	{
	    document.getElementById(lblPrefix + "lblerrorMsg").style.color = "Red";
	    document.getElementById(lblPrefix + "lblerrorMsg").style.fontWeight = "bold";
        document.getElementById(lblPrefix + "lblerrorMsg").innerHTML = "Please correct the required fields on red !";
	    return false;
	}
	else
	{   
	    document.getElementById(lblPrefix + "lblerrorMsg").innerHTML = "&nbsp";
	    return true;
	}  
}


function IsNumeric(str)
{
	//var ValidChars = "0123456789."; With Decimal Point !
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char; 
	
	if(str.length == 0)
		IsNumber=false;
		
	for (i = 0; i < str.length; i++) 
	{ 
		Char = str.charAt(i); 
		//if ((i == 0) && (Char == "-")) // check first character for minus sign
		//	continue;
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
			break;
		}
	}
	return IsNumber;
}

function IsPrice(str)
{
	str = str.replace("$","");
	str = str.replace(/,/g,"");
	
	if(IsNumeric(str))
		return true;
    else
		return false;
}

function IsPhone(Phone)
{
    var len = Phone.length;
    
	if(len == 12 && Phone.charAt(3) == '-' && Phone.charAt(7) == '-')
	{
		return true;
	}
    return false;
}

function IsLocation(Location)
{
     var i=0,j=0;
     var cityFound = "", stateFound = "", zipFound = "";
     
     if(Location == null || Location.value == "" || Location.value == "City & State or Zip Code")
	    return false;
	
     var states = new Array("AB","AK","AL","AR","FL",
                            "MN","AZ","BC","CA",
                            "CO","CT","DC","DE",
                            "GA","HI","IA","ID","IL",
                            "IN","KS","KY","LA","MA",
                            "MB","MD","ME","MI","MO",
                            "MS","MT","NB","NC",
                            "ND","NE","NF","NH",
                            "NJ","NM","NS","NT",
                            "NV","NY","OH","OK","ON",
                            "OR","PA","PE","PQ",
                            "PR","RI","SC","SD",
                            "SK","TN","TX","UT","VA",
                            "VI","VT","WA","WI",
                            "WV","WY","YT","CD");
    
     var statesDesc = new Array("ALBERTA","ALASKA","ALABAMA","ARKANSAS","FLORIDA",
                            "MINNESOTA","ARIZONA","BRITISH COLUMBIA","CALIFORNIA",
                            "COLORADO","CONNECTICUT","DISTRICT OF COLUMBIA","DELAWARE",
                            "GEORGIA","HAWAII","IOWA","IDAHO","ILLINOIS",
                            "INDIANA","KANSAS","KENTUCKY","LOUISIANA","MASSACHUSETTS",
                            "MANITOBA","MARYLAND","MAINE","MICHIGAN","MISSOURI",
                            "MISSISSIPPI","MONTANA","NEW BRUNSWICK","NORTH CAROLINA",
                            "NORTH DAKOTA","NEBRASKA","NEW FOUNDLAND","NEW HAMPSHIRE",
                            "NEW JERSEY","NEW MEXICO","NOVA SCOTIA","NORTHERN TERRITORY",
                            "NEVADA","NEW YORK","OHIO","OKLAHOMA","ONTARIO",
                            "OREGON","PENNSYLVANIA","PRICE EDWARD ISLAND","PROVINCE QUEBEC",
                            "PRINCE RUPERT","RHODE ISLAND","SOUTH CAROLINA","SOUTH DAKOTA",
                            "SASKATCHEWAN","TENNESSEE","TEXAS","UTAH","VIRGINIA",
                            "VIRGIN ISLANDS","VERMONT","WASHINGTON","WISCONSIN",
                            "WEST VIRGINIA","WYOMING","YUKON TERRITORY","CANADA");                        

    
	Location.value = Location.value.replace(",", " ") 
	
	var locationArr = Location.value.split(" ");
    for(i = 0; i < locationArr.length; i++)
    {
	    if(IsUSAZip(locationArr[i]) == true)
	    {
	        zipFound = locationArr[i];
	        Location.value = Location.value.replace(zipFound,"");
	        break;
	    }
	}
    
    // Checks State.
    for(i = 0; i < locationArr.length; i++)
    {   
        for(j = 0; j < states.length; j++)
        {
            if(locationArr[i].toUpperCase() == states[j])
            {
                stateFound = states[j];    
                Location.value = Location.value.toUpperCase().replace(states[j],""); 
                break;
            }
        }
        if(stateFound.length > 0)
            break;
    }
    
    // User might type the full description of the state.
    if(stateFound.length == 0)
    {        
        for(j = 0; j < statesDesc.length; j++)
        {
            if((Location.value.toUpperCase()).search(statesDesc[j]) != -1)
            {
                stateFound = states[j];  
                Location.value = Location.value.toUpperCase().replace(statesDesc[j],""); 
                break;
            }
        }
    }
    
    //// Now Location contains only the city name !
    locationArr = Location.value.split(" ");
    for(i = 0; i < locationArr.length; i++)
    {
        if(locationArr[i].toUpperCase() != stateFound && locationArr[i].toUpperCase() != zipFound && IsNumeric(locationArr[i]) == false)
            cityFound += locationArr[i] + " ";
    }
    
    cityFound = rtrim(cityFound);
    Location.value = "";
    Location.value += cityFound;
    if(stateFound.length > 0)
        Location.value += " "; 
    Location.value += stateFound;
    if(zipFound.length > 0)
        Location.value += " "; 
    Location.value += zipFound;
    Location.value = Location.value.replace("  ", " "); 
    
    ////alert(Location.value)
    
    if(zipFound.length > 0 || (cityFound.length > 0 && stateFound.length > 0))
        return true;
    else
	    return false;
}

function IsUSAZip(Zip)
{
    var len = Zip.length;
    var IsZip = false;
    
	if(len == 5 || (len == 10 && Zip.charAt(5) == '-'))
	{
	    IsZip = true;
		var ValidChars = "0123456789-";
		var Char; 
		for (i = 0; i < len && IsZip == true; i++) 
	    { 
		    Char = Zip.charAt(i);
		    if (ValidChars.indexOf(Char) == -1) 
		    {
			    IsZip = false;
		    }
	    }
	}
	
    return IsZip;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


