function AutoSuggestControl(oTextbox,oProvider){
  this.cur=-1;
  this.itemList=10;
  this.layer=null;
  this.provider=oProvider;
  this.textbox=oTextbox;
  this.oTextboxVal=oTextbox.value;
  this.init();
}
AutoSuggestControl.prototype.autosuggest=function(aSuggestions,bTypeAhead){
  if(aSuggestions.length>0){
    if(bTypeAhead){
		this.typeAhead(aSuggestions[0]);
    }
    this.createSuggestions(aSuggestions);
  }else {
    this.hideSuggestions();this.layer.innerHTML="";this.cur=-1;
  }
};
AutoSuggestControl.prototype.createDropDown=function(){
  var oThis=this;
  this.layer=document.createElement("div");
  this.layer.className="suggestions";
  this.layer.style.visibility="hidden";
  this.layer.style.width=this.textbox.offsetWidth;
  this.layer.onmousedown=this.layer.onmouseup=this.layer.onmouseover=function(oEvent){
    oEvent=oEvent||window.event;
    oTarget=oEvent.target||oEvent.srcElement;
    if(oEvent.type=="mousedown"){
      oThis.textbox.value=oTarget.firstChild.nodeValue;
      oThis.hideSuggestions();
    }else {
      if(oEvent.type=="mouseover"){
        oThis.highlightSuggestion(oTarget);
      }else {
        oThis.textbox.focus();
      }
    }
  };
  document.body.appendChild(this.layer);
};
AutoSuggestControl.prototype.getLeft=function(){
  var oNode=this.textbox;
  var iLeft=0;
  while(oNode.tagName!="BODY"){
    iLeft+=oNode.offsetLeft;
    oNode=oNode.offsetParent;
  }
  return iLeft;
};
AutoSuggestControl.prototype.getTop=function(){
  var oNode=this.textbox;
  var iTop=0;
  while(oNode.tagName!="BODY"){
    iTop+=oNode.offsetTop;
    oNode=oNode.offsetParent;
  }
  return iTop;
};
AutoSuggestControl.prototype.handleKeyDown=function(oEvent){
  if (!this.layer||!this.layer.innerHTML)return false;
  switch(oEvent.keyCode){
  case 38:
	this.showSuggestions();
    this.previousSuggestion();
    break ;
  case 40:
	this.showSuggestions();
    this.nextSuggestion();
    break ;
  case 13:
    this.hideSuggestions();
    break ;
  }
};
AutoSuggestControl.prototype.handleKeyUp=function(oEvent){
  var iKeyCode=oEvent.keyCode;
  if(iKeyCode==8||iKeyCode==46){
	this.oTextboxVal=this.textbox.value;
    this.provider.requestSuggestions(this,false);
  }else {
    if(iKeyCode<32||(iKeyCode>=33&&iKeyCode<46)||(iKeyCode>=112&&iKeyCode<=123)){
    }else {
	  this.oTextboxVal=this.textbox.value;
      //this.provider.requestSuggestions(this,true);
      this.provider.requestSuggestions(this,false);
    }
  }
};
AutoSuggestControl.prototype.hideSuggestions=function(){
  this.layer.style.visibility="hidden";
};
AutoSuggestControl.prototype.showSuggestions=function(){
  if (this.layer)if(this.layer.innerHTML)this.layer.style.visibility="visible";
};
AutoSuggestControl.prototype.highlightSuggestion=function(oSuggestionNode){
	//form1.Remark.value+="\n"+(oSuggestionNode)
  for(var i=0;i<this.layer.childNodes.length;i++){
    var oNode=this.layer.childNodes[i];
    if(oNode==oSuggestionNode){
      oNode.className="current";this.cur=i;
    }else {
      if(oNode.className=="current"){
        oNode.className="";
      }
    }
  }
};
AutoSuggestControl.prototype.init=function(){
  var oThis=this;
  this.textbox.onkeyup=function(oEvent){
    if(!oEvent){
      oEvent=window.event;
    }
    oThis.handleKeyUp(oEvent);
  };
  this.textbox.onkeydown=function(oEvent){
    if(!oEvent){
      oEvent=window.event;
    }
    oThis.handleKeyDown(oEvent);
  };
  this.textbox.onblur=function(){
    oThis.hideSuggestions();
  };
  this.createDropDown();
};
AutoSuggestControl.prototype.nextSuggestion=function(){
  var cSuggestionNodes=this.layer.childNodes;
  if (this.cur==cSuggestionNodes.length-1){
	  this.cur=-1;this.textbox.value=this.oTextboxVal;
	  return this.highlightSuggestion(null);
  }else if(this.cur>cSuggestionNodes.length-1){
	this.cur=-1;
  }
  if(cSuggestionNodes.length>0){
    var oNode=cSuggestionNodes[++this.cur];
    this.highlightSuggestion(oNode);
    this.textbox.value=oNode.firstChild.nodeValue;
  }
};
AutoSuggestControl.prototype.previousSuggestion=function(){
  var cSuggestionNodes=this.layer.childNodes;
  if (this.cur<0)this.cur=cSuggestionNodes.length;
  else if(this.cur==0){
	  this.textbox.value=this.oTextboxVal;
	  this.cur=-1;//this.textbox.focus();
	  return this.highlightSuggestion(null);
	  }

  if(cSuggestionNodes.length>0){
    var oNode=cSuggestionNodes[--this.cur];
    this.highlightSuggestion(oNode);
    this.textbox.value=oNode.firstChild.nodeValue;
  }
};
AutoSuggestControl.prototype.selectRange=function(iStart,iLength){
  if(this.textbox.createTextRange){
    var oRange=this.textbox.createTextRange();
    oRange.moveStart("character",iStart);
    oRange.moveEnd("character",iLength-this.textbox.value.length);
    oRange.select();
  }else {
    if(this.textbox.setSelectionRange){
      this.textbox.setSelectionRange(iStart,iLength);
    }
  }
  this.textbox.focus();
};
AutoSuggestControl.prototype.createSuggestions=function(aSuggestions){
  var oDiv=null;
  this.layer.innerHTML="";this.cur=-1;
  for(var i=0;i<aSuggestions.length&&i<this.itemList;i++){
    oDiv=document.createElement("div");
    oDiv.appendChild(document.createTextNode(aSuggestions[i]));
    this.layer.appendChild(oDiv);
  }
  this.layer.style.left=this.getLeft()+"px";
  this.layer.style.top=(this.getTop()+this.textbox.offsetHeight)+"px";
  this.layer.style.visibility="visible";
};
AutoSuggestControl.prototype.typeAhead=function(sSuggestion){
  if(this.textbox.createTextRange||this.textbox.setSelectionRange){
    var iLen=this.textbox.value.length;
    this.textbox.value=sSuggestion;
    this.selectRange(iLen,sSuggestion.length);
  }
};



function StateSuggestions(){
  this.states=[
	  "AK","AL","AZ","AR","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VA","VT","WA","WI","WV","WY","PR","VI","GU",
	  "New South Wales","Western Australia","South Australia","Queensland","Victoria","Australian Capital Territory","Northern Territory",
	  "Berkshire","Cambridgeshire","Cheshire","Cleveland","Cornwall","Derbyshire","Devon","Durham","East Yorkshire","East Sussex","Essex","Gloucestershire","Greater Manchester","Hampshire","Hertfordshire","Kent","Lancashire","Lincolnshire","London","Merseyside","Norfolk","North Yorkshire","Northumberland","Nottinghamshire","Shropshire","Somerset","South Yorkshire","Suffolk","Suffolk","Tyne and Wear","West Midlands","West Sussex","West Yorkshire","Wiltshire","Worcestershire","Antrim","Argyll and Bute","East Lothian","Renfrewshire","Fife","North Lanarkshire","South Lanarkshire","Renfrewshire","West Lothian","Bridgend","Isle of Anglesey","The Vale of Glamorgan"
  ];
  this.countrys=["USA","UK","Australia","Argentina","Armenia","Albania","Algeria","Andorra","Angola","Anguilla","Antigua and Barbuda","Aruba","Austria","Azerbaijan Republic","Belgium","Brunei","Bolivia","Brazil","Bahamas","Bahrain","Barbados","Belize","Benin","Bermuda","Bhutan","Bosnia and Herzegovina","Botswana","British Virgin Islands","Bulgaria","Burkina Faso","Burundi","Canada","Cyprus","Czech Republic","Cambodia","Cape Verde","Cayman Islands","Chad","Chile","China","Colombia","Comoros","Cook Islands","Costa Rica","Croatia","Denmark","Democratic Republic of the Congo","Djibouti","Dominica","Dominican Republic","Ecuador","El Salvador","Eritrea","Estonia","Ethiopia","England, UK","France","Finland","Falkland Islands","Faroe Islands","Federated States of Micronesia","Fiji","French Guiana","French Polynesia","Greece","Germany","Guinea Bissau","Gabon Republic","Gambia","Gibraltar","Greenland","Grenada","Guadeloupe","Guatemala","Guinea","Guyana","Honduras","Hong Kong","Hungary","Italy","India","Ireland","Iceland","Indonesia","Israel","Jamaica","Japan","Jordan","Kazakhstan","Kenya","Kiribati","Kuwait","Kyrgyzstan","Laos","Latvia","Lesotho","Liechtenstein","Lithuania","Luxembourg","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Martinique","Mauritania","Mauritius","Mayotte","Mexico","Mongolia","Montserrat","Morocco","Mozambique","Netherlands","New Zealand","Norway","Namibia","Nauru","Nepal","Netherlands Antilles","New Caledonia","Nicaragua","Niger","Niue","Norfolk Island","Oman","Palau","Panama","Papua New Guinea","Peru","Philippines","Pitcairn Islands","Poland","Portugal","Qatar","Republic of the Congo","Reunion","Romania","Russia","Rwanda","Spain","Singapore","Sweden","Switzerland","South Africa","Saint Vincent and the Grenadines","Samoa","San Marino","Saudi Arabia","Senegal","Seychelles","Sierra Leone","Slovakia","Slovenia","Solomon Islands","Somalia","South Korea","Sri Lanka","Suriname","Svalbard and Jan Mayen Islands","Swaziland","Scotland, UK","Taiwan","Tajikistan","Tanzania","Thailand","Togo","Tonga","Trinidad and Tobago","Tunisia","Turkey","Turkmenistan","Turks and Caicos Islands","Tuvalu","Uganda","Ukraine","United Arab Emirates","Uruguay","United States","United Kingdom","Vanuatu","Vatican City State","Venezuela","Vietnam","Wallis and Futuna Islands","Yemen","Zambia","St. Helena","St. Kitts and Nevis","St. Lucia","St. Pierre and Miquelon"];
}
StateSuggestions.prototype.requestSuggestions=function(oAutoSuggestControl,bTypeAhead){
  var aSuggestions=[];
  var sTextboxValue=oAutoSuggestControl.textbox.value;
  var sugStrings=this.states;
  if(oAutoSuggestControl.textbox.name=="country")sugStrings=this.countrys;

  if(sTextboxValue.length>0){
    var sTextboxValueLC=sTextboxValue.toLowerCase();
    for(var i=0, index=0;i<sugStrings.length;i++){
      /*var sStateLC=this.states[i].toLowerCase();
      if(sStateLC.indexOf(sTextboxValueLC)==0){
        aSuggestions.push(sTextboxValue+this.states[i].substring(sTextboxValue.length));
      }*/
      var sStateLC=sugStrings[i];
      if(sStateLC.toLowerCase().indexOf(sTextboxValueLC)==0){
        aSuggestions.push(sugStrings[i]);
		if (++index>=10)break;
      }
    }
  }
  oAutoSuggestControl.autosuggest(aSuggestions,bTypeAhead);
};
function cpCty(stateVal) {
  this.country=document.getElementById("country").value;
  if (stateVal&&"||USA|UK|Australia|United States|United Kingdom|England, UK|Scotland, UK|".indexOf("|"+this.country+"|")!=-1){
	this.obj=new StateSuggestions();
	this.states=this.obj.states;
	this.country="";
    for (var i=0, l = this.states.length; i < l; ++i) {
      if(this.states[i]==stateVal){
		if (i<=53){
			this.country="USA";
		}else if (i>60) {
			this.country="UK";
		}else{
			this.country="Australia";
		}
		//form1.Remark.value+="\n"+(i)
		break;
      }
    }
	if(this.country)document.getElementById("country").value=this.country;
  }
}
