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 http = getHTTPObject();


function handleHttpResponse() {
    if (http.readyState == 4) {
        // Split the comma delimited response into an array
        results = http.responseText.split(",");
        document.getElementById('city').value = results[0];
        document.getElementById('state').value = results[1];
    }
}


function updateCityState() {
    if (document.getElementById("country").value == "USA") {
        var zipValue = document.getElementById("zip_code").value;
        http.open("GET", "doctorfinder/getCityState.php?z=" + escape(zipValue), true);
        http.onreadystatechange = handleHttpResponse; http.send(null);
    }
}


function new_freecap()
{
    // loads new freecap image
    if(document.getElementById) {
        // extract image name from image source (i.e. cut off ?randomness)
        thesrc = document.getElementById("cap").src;
        thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
        // add ?(random) to prevent caching
        document.getElementById("cap").src = thesrc+"?"+Math.round(Math.random()*100000);
    } else {
        alert("Sorry, cannot autoreload security image\nSubmit the form and a new security image will be loaded");
    }
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


function disableRadius() {
	if ((document.getElementById("last_name").value.length > 0) || (document.getElementById("country").value != "USA")) {
		document.getElementById("radius").disabled = true;
	} else {
		document.getElementById("radius").disabled = false;
	}
}


function getStates(){
	http.open("GET", "doctorfinder/getStateProvince.php?c=" + document.getElementById("country").value);

	http.onreadystatechange = handleStateResults;
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}


function handleStateResults(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element
			that we can find: innerHTML. */
		document.getElementById('state_selection').innerHTML = response;
	}
}

