function getHTTPObject() {
  var xmlhttp;
/*@cc_on
  @if (@_jscript_version >= 5)
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  @else
    xmlhttp = false;
  @end @*/  
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var url = "getstatelist.jsp?CountryCode=";
var isWorking = false;
var http = getHTTPObject();

function handleHttpResponse() {
	flag=0;
  if (http.readyState == 4) {
    isWorking = false;
    if (http.responseText.indexOf('invalid') == -1) {
      var xmlDocument = http.responseXML; 
	  arr=new Array();
	  arr=eval(Trim(http.responseText));
	  objState = document.getElementById("state");
	  objState.length = arr.length+1;
//	  document.getElementById("txt").value=obj.length;
	  for(i=0 ; i<arr.length ; i++)
	  {
      	objState[i].value = arr[i][0];
	    objState[i].text = arr[i][1];
		if(arr[i][0] == state_name)
		{
			objState[i].selected = true;
			flag=1;
		}
		else if(state_name == ""){
			flag=1;
		}
	  }
	  objState[i].value = "Other";
	  objState[i].text = "Other";
	  accountInfoFormFound = false;
	  for (j = 0; j < document.forms.length; j++) {
	  	if (document.forms[j].name == "accountInfoForm") {
	  		accountInfoFormFound = true;
	  		break;
	  	}
	  }
	  if(flag==0)
	  {
	  	  objState[i].selected = true;
	  	  if (accountInfoFormFound == true) {
			  //document.accountInfoForm.other.value="";
			  document.accountInfoForm.other.style.visibility="visible";
		  }
		  else {
			  //document.userInfoForm.other.value=state_name;
			  document.userInfoForm.other.style.visibility="visible";
		  }
	  }
	  else
	  {
	  	  if (accountInfoFormFound == true) {
		  	document.accountInfoForm.other.value="";
		  	document.accountInfoForm.other.style.visibility="hidden";
		  }
		  else {
		  	document.userInfoForm.other.value="";
		  	document.userInfoForm.other.style.visibility="hidden";
		  }
	  }
      isWorking = false;
    }
  }
}
function updateState() {
  if (!isWorking) {
    var countryValue = document.getElementById("country").value;
    http.open("GET", url + escape(countryValue), true);
//    http.open("GET", "http://192.168.32.60:8080/micrel/s1.xml" , true);
//    http.open("GET", "http://192.168.32.60:8080/micrel/test_xml.jsp" , true);
    isWorking = true;
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
  }
}

function Trim(s)
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}