
/**
 * Provides suggestions for state names (USA).
 * @class
 * @scope public
 */
function StateSuggestions() {
    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*/,
	                                                          bTypeAhead /*:boolean*/) {

    var aSuggestions = [];
    var sTextboxValue = oAutoSuggestControl.textbox.value;
	var intCountryId = '';
	var intRestrict = '';
	var intFiltered = '';
	
	if( document.getElementById('cid') != null ){ intFiltered = document.getElementById('ftd').value; }

    if (sTextboxValue.length > 3){
    
		if ( document.getElementById('cid') != null ){
		
			//if ( document.searchform.cres.checked ) {
				intCountryId = document.getElementById('cid').value;
				
			//}
			
		}	

		//Get the suggestions from the database
		var xmlhttp = getXmlHttp();
		xmlhttp.open("GET", '/xmlhttp/suggest-towns.asp?strbase=' + sTextboxValue + '&cid=' + intCountryId + '&ftd=' + intFiltered); 
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			  
			  //cb(xmlhttp.responseText);
			  
			  oAutoSuggestControl.autosuggest(xmlhttp.responseText.split("|"), bTypeAhead);
			  }
		  }
		  xmlhttp.send(null);
        /*search for matching states
        for (var i=0; i < this.states.length; i++) { 
            if (this.states[i].indexOf(sTextboxValue) == 0) {
                aSuggestions.push(this.states[i]);
            } 
        }*/
    }

    //provide suggestions to the control
    //oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
};

function getXmlHttp(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}


