
/**
 * Provides suggestions for state names (USA).
 * @class
 * @scope public
 */
function StateSuggestions() {
	this.states =
	[
		"Adelanto", "Agoura Hills", "Alameda", "Alameda County", "Albany", "Alhambra", "Aliso Viejo", "Alturas", "Amador", "Amador County", "American Canyon", "Anaheim", "Anderson", "Angels Camp", "Antioch", "Apple Valley", "Arcadia","Arcata","Arroyo Grande","Artesia","Arvin","Atascadero","Atherton","Atwater","Auburn","Avalon","Avenal","Azusa",
		"Bakersfield", "Baldwin Park", "Banning", "Barstow", "Beaumont","Bell","Bell Gardens","Bellflower","Belmont","Belvedere","Benicia","Berkeley","Beverly Hills","Big Bear Lake","Biggs","Bishop","Blue Lake","Blythe","Bradbury","Brawley","Brea","Brentwood","Brisbane","Buellton","Buena Park","Burbank","Burlingame","Butte County",
		"Calabasas", "Calaveras", "Calexico","California City", "Calimesa","Calipatria","Calistoga","Camarillo","Campbell","Canyon Lake","Capitola","Carlsbad","CarmelByTheSea","Carpinteria","Carson","Cathedral City","Ceres","Cerritos","Chico","Chino","Chino Hills","Chowchilla","Chula Vista","Citrus Heights","Claremont","Clayton","Clearlake","Cloverdale","Clovis","Coachella","Coalinga","Colfax","Colma","Colton","Colusa","Colusa County","Commerce","Compton","Concord","Contra Costa County","Corcoran","Corning","Corona","Coronado","Corte Madera","Costa Mesa","Cotati","Covina","Crescent City","Cudahy","Culver City","Cupertino","Cypress",
		"Daly City", "Dana Point", "Danville", "Davis", "Del Mar", "Del Norte County","Del Rey Oaks","Delano","Desert Hot Springs","Diamond Bar","Dinuba","Dixon","Dorris","Dos Palos","Downey","Duarte","Dublin","Dunsmuir",
		"East Palo Alto", "El Cajon", "El Centro", "El Cerrito", "El Dorado County","El Monte","El Paso De Robles","El Segundo","Elk Grove","Emeryville","Encinitas","Escalon","Escondido","Etna","Eureka","Exeter",
		"Fairfax", "Fairfield", "Farmersville", "Ferndale", "Fillmore","Firebaugh","Folsom","Fontana","Fort Bragg","Fort Jones","Fortuna","Foster City","Fountain Valley","Fowler","Fremont","Fresno","Fresno County","Fullerton",
		"Galt", "Garden Grove", "Gardena", "Gilroy", "Glendale", "Glendora","Glenn County","Goleta","Gonzales","Grand Terrace","Grass Valley","Greenfield","Gridley","Grover Beach","Guadalupe","Gustine",
		"Half Moon Bay", "Hanford","Hawaiian Gardens", "Hawthorne", "Hayward","Healdsburg","Hemet","Hercules","Hermosa Beach","Hesperia","Hidden Hills","Highland","Hillsborough","Hollister","Holtville","Hughson","Humboldt County","Huntington Beach","Huntington Park","Huron",
		"Imperial", "Imperial County", "Imperial Beach", "Indian Wells", "Indio", "Industry", "Inglewood", "Inyo County", "Ione", "Irvine", "Irwindale", "Isleton",
		"Jackson",
		"Kerman", "Kern County", "King City", "Kings County", "Kingsburg",
		"La Canada Flintridge", "La Habra", "La Habra Heights","La Mesa","La Mirada","La Palma", "La Puente","La Quinta", "La Verne", "Lafayette", "Laguna Beach", "Laguna Hills", "Laguna Niguel","Laguna Woods","Lake County","Lake Elsinore","Lake Forest","Lakeport","Lakewood","Lancaster","Larkspur","Lassen County","Lathrop","Lawndale","Lemon Grove","Lemoore","Lincoln","Lindsay","Live Oak","Livermore","Livingston","Lodi","Loma Linda","Lomita","Lompoc","Long Beach","Loomis","Los Alamitos","Los Altos","Los Altos Hills","Los Angeles","Los Angeles County","Los Banos","Los Gatos","Loyalton","Lynwood",
		"Madera", "Madera County", "Malibu", "Mammoth Lakes","Manhattan Beach","Manteca","Maricopa", "Marin","Marin County","Marina","Martinez","Marysville","Maywood","Mcfarland", "Mendocino County","Mendota","Menlo Park","Merced","Merced County","Mill Valley","Millbrae","Milpitas","Mission Viejo","Modesto","Modoc County","Mono County","Monrovia","Montague","Montclair","Monte Sereno","Montebello","Monterey","Monterey County","Monterey Park","Moorpark","Moraga","Moreno Valley","Morgan Hill","Morro Bay","Mount Shasta","Mountain View","Murrieta",
		"Napa", "Napa County", "National City", "Needles","Nevada County","Nevada City","Newark", "Newman","Newport Beach","Norco","Norwalk","Novato",
		"Oakdale", "Oakland","Oakley","Oceanside", "Ojai","Ontario","Orange","Orange County", "Orange Cove","Orinda","Orland","Oroville","Oxnard",
		"Pacific Grove", "Pacifica", "Palm Desert", "Palm Springs","Palmdale","Palo Alto", "Palos Verdes Estates","Paradise","Paramount","Parlier","Pasadena","Patterson", "Perris", "Petaluma","Pico Rivera", "Piedmont","Pinole","Pismo Beach","Pittsburg","Placentia","Placer County","Placerville","Pleasant Hill","Pleasanton","Plumas County","Plymouth","Point Arena","Pomona","Port Hueneme","Porterville","Portola","Portola Valley","Poway",
		"Rancho Cordova", "Rancho Cucamonga", "Rancho Mirage", "Rancho Palos Verdes", "Rancho Santa Margarita","Red Bluff","Redding","Redlands","Redondo Beach","Redwood City", "Reedley", "Rialto", "Richmond","Ridgecrest","Rio Dell","Rio Vista","Ripon","Riverbank","Riverside","Riverside County","Rocklin","Rohnert Park","Rolling Hills","Rolling Hills Estates","Rosemead","Roseville","Ross",
		"Sacramento", "Sacramento County", "Salinas", "San Anselmo", "San Benito County", "San Bernardino", "San Bruno","San Buenaventura","San Carlos","San Clemente", "San Diego","San Diego County", "San Dimas","San Fernando","San Francisco","San Francisco County","San Gabriel","San Jacinto","San Joaquin County","San Jose","San Juan Bautista","San Juan Capistrano","San Leandro","San Luis Obispo","San Luis Obispo","San Marcos","San Marino","San Mateo","San Mateo","San Pablo","San Rafael","San Ramon","Sand City","Sanger","Santa Ana","Santa Barbara","Santa Barbara County","Santa Clara","Santa Clara County","Santa Clarita","Santa Cruz","Santa Cruz County","Santa Fe Springs","Santa Maria","Santa Monica","Santa Paula","Santa Rosa","Santee","Saratoga","Sausalito","Scotts Valley","Seal Beach","Seaside","Sebastopol","Selma","Shafter","Shasta County","Shasta Lake","Sierra County","Sierra Madre","Signal Hill","Simi Valley","Siskiyou County","Solana Beach","Solano County","Soledad","Solvang","Sonoma","Sonoma County","Sonora","South El Monte","South Gate","South Lake Tahoe","South Pasadena","South San Francisco","St Helena","Stanislaus County","Stanton","Stockton","Suisun City","Sunnyvale","Susanville","Sutter County","Sutter Creek",
		"Taft", "Tehachapi", "Tehama", "Tehama County", "Temecula", "Temple City", "Thousand Oaks", "Tiburon", "Torrance","Tracy","Trinidad","Truckee","Tulare","Tulare County","Tulelake","Tuolumne County", "Turlock","Tustin","Twentynine Palms",
		"Ukiah", "Union City", "Upland",
		"Vacaville", "Vallejo","Ventura County","Vernon","Victorville", "Villa Park", "Visalia", "Vista",
		"Walnut", "Walnut Creek", "Wasco", "Waterford", "Watsonville", "Weed", "West Covina", "West Hollywood", "West Sacramento", "Westlake Village", "Westminster", "Westmorland", "Whittier", "Williams", "Willits", "Willows","Windsor","Winters","Woodlake","Woodland","Woodside",
		"Yolo County", "Yorba Linda", "Yountville", "Yreka", "Yuba County", "Yuba City", "Yucaipa", "Yucca Valley"
	];
    /*this.states =
    [
        "Alabama", "Alaska", "Arizona", "Arkansas",
        "California", "Colorado", "Connecticut",
        "Delaware", "Florida", "Georgia", "Hawaii",
        "Idaho", "Illinois", "Indiana", "Iowa",
        "Kansas", "Kentucky", "Louisiana",
        "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", 
        "Mississippi", "Missouri", "Montana",
        "Nebraska", "Nevada", "New Hampshire", "New Mexico", "New York",
        "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", 
        "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
        "Tennessee", "Texas", "Utah", "Vermont", "Virginia", 
        "Washington", "West Virginia", "Wisconsin", "Wyoming"  
    ];*/
}

/**
 * Request suggestions for the given autosuggest control. 
 * @scope protected
 * @param oAutoSuggestControl The autosuggest control to provide suggestions for.
 */
StateSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/) {
    var aSuggestions = [];
    var sTextboxValue = oAutoSuggestControl.textbox.value.toLowerCase();
    
    if (sTextboxValue.length > 0){
    
        //search for matching states
        for (var i=0; i < this.states.length; i++) { 
            if (this.states[i].toLowerCase().indexOf(sTextboxValue) == 0) {
                aSuggestions.push(this.states[i]);
            } 
        }
    }

    //provide suggestions to the control
    oAutoSuggestControl.autosuggest(aSuggestions);
};

/**
 * An autosuggest textbox control.
 * @class
 * @scope public
 */
function AutoSuggestControl(oTextbox /*:HTMLInputElement*/, oProvider /*:SuggestionProvider*/) {
       
    /**
     * Suggestion provider for the autosuggest feature.
     * @scope private.
     */
    this.provider /*:SuggestionProvider*/ = oProvider;
    
    /**
     * The textbox to capture.
     * @scope private
     */
    this.textbox /*:HTMLInputElement*/ = oTextbox;
    
    //initialize the control
    this.init();
    
}

/**
 * Autosuggests one or more suggestions for what the user has typed.
 * If no suggestions are passed in, then no autosuggest occurs.
 * @scope private
 * @param aSuggestions An array of suggestion strings.
 */
AutoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/) {
    
    //make sure there's at least one suggestion
    if (aSuggestions.length > 0) {
        this.typeAhead(aSuggestions[0]);
    }
};


/**
 * Handles keyup events.
 * @scope private
 * @param oEvent The event object for the keyup event.
 */
AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {

    var iKeyCode = oEvent.keyCode;

    //make sure not to interfere with non-character keys
    if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignore
    } else {
        //request suggestions from the suggestion provider
        this.provider.requestSuggestions(this);
    }
};

/**
 * Initializes the textbox with event handlers for
 * auto suggest functionality.
 * @scope private
 */
AutoSuggestControl.prototype.init = function () {

    //save a reference to this object
    var oThis = this;
    
    //assign the onkeyup event handler
    this.textbox.onkeyup = function (oEvent) {
    
        //check for the proper location of the event object
        if (!oEvent) {
            oEvent = window.event;
        }    
        
        //call the handleKeyUp() method with the event object
        oThis.handleKeyUp(oEvent);
    };
    
};

/**
 * Selects a range of text in the textbox.
 * @scope public
 * @param iStart The start index (base 0) of the selection.
 * @param iLength The number of characters to select.
 */
AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) {

    //use text ranges for Internet Explorer
    if (this.textbox.createTextRange) {
        var oRange = this.textbox.createTextRange(); 
        oRange.moveStart("character", iStart); 
        oRange.moveEnd("character", iLength - this.textbox.value.length);      
        oRange.select();
        
    //use setSelectionRange() for Mozilla
    } else if (this.textbox.setSelectionRange) {
        this.textbox.setSelectionRange(iStart, iLength);
    }     

    //set focus back to the textbox
    this.textbox.focus();      
}; 

/**
 * Inserts a suggestion into the textbox, highlighting the 
 * suggested part of the text.
 * @scope private
 * @param sSuggestion The suggestion for the textbox.
 */
AutoSuggestControl.prototype.typeAhead = function (sSuggestion /*:String*/) {

    //check for support of typeahead functionality
    if (this.textbox.createTextRange || this.textbox.setSelectionRange){
        var iLen = this.textbox.value.length; 
        this.textbox.value = sSuggestion; 
        this.selectRange(iLen, sSuggestion.length);
    }
};

function showPic(picid, picname)
{
	document.getElementById(picid).src = picname;
}

function getElementWidth(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}

function getImageWidth(myImage) {
	var x, obj;
	if (document.layers) {
		var img = getImage(myImage);
		return img.width;
	} else {
		return getElementWidth(myImage);
	}
	return -1;
}

function smallPicClick(image)
{
	window.open('http://openhouses.rereader.com/image_preview/?pic='+image,'','width=200,height=200,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
}

function changePageMenu(menu, link)
{
	var newPage = menu.value;
	var newLink = link.replace('***', newPage);
	
	window.location = newLink;
}

function changeItemsPerPageMenu(menu, link)
{
	var newPage = menu.value;
	var newLink = link.replace('***', newPage);
	
	window.location = newLink;
}

function changeSortMenu(link)
{
	var sortMenu = document.getElementById('sort');
	var countyMenu = document.getElementById('county');
	
	var newLink = link + '?sort=' + sortMenu.value + '&county=' + countyMenu.value;
	
	window.location = newLink;
}

function setDates()
{
	var currentTime = new Date();
	var currentMonth = currentTime.getMonth();
	var currentDay = currentTime.getDate();
	
	document.getElementById('searchStartDateMonth').options[currentMonth + 1].selected = true;
	document.getElementById('searchStartDateDay').options[currentDay].selected = true;
	document.getElementById('searchEndDateDay').options[currentDay].selected = true;
	switch (currentMonth)
	{
		case 11:
			document.getElementById('searchEndDateMonth').options[0].selected = true;
			break;
		default:
			document.getElementById('searchEndDateMonth').options[currentMonth + 2].selected = true;
			break;
	}
}

function jumpFields(field)
{
	if (document.getElementById(field).value.length == 3)
	{
		switch (field)
		{
			case "phone1":
				document.contactForm['phone2'].focus();
				break;
			case "phone2":
				document.contactForm['phone3'].focus();
				break;
		}
	}
}
