// JavaScript Document


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 = "CityStateLookupByZip.do?billingPostalCode=";
var isWorking = false;
var http = getHTTPObject();

function handleHttpResponse() {
    if (http.readyState == 4) {
        isWorking = false;
        if (http.responseText.indexOf('invalid') == -1) {

            if (http.responseText.indexOf('colorado') > -1) {
                document.getElementById('billingCity').value = "";
                document.getElementById('billingState').value = "";
                alert("We're sorry, but there are no longer Free Trials available in your area.");
            } else {
                var xmlDocument = http.responseXML;
                var city = xmlDocument.getElementsByTagName('city').item(0).firstChild.data;
                var state = xmlDocument.getElementsByTagName('state').item(0).firstChild.data;
                document.getElementById('billingCity').value = city;
                document.getElementById('billingState').value = state;
                isWorking = false;
            }
        } else if (http.responseText.indexOf('invalid') > -1) {
            // reset form fields
            document.getElementById('billingCity').value = "";
            document.getElementById('billingState').value = "";
        }
    }
}

function updateCityState() {
    if (!isWorking) {
        var zipValue = document.getElementById("billingPostalCode").value;
        http.open("GET", url + escape(zipValue), true);
        isWorking = true;
        http.onreadystatechange = handleHttpResponse;
        http.send(null);
    }
}

function validateFiveDigitZipCode(zip) {

    if (zip.length != 5) {
        alert("Please enter your 5 digit ZIP code.");
        document.getElementById("billingPostalCode").value = "";
        document.getElementById("billingCity").value = "";
        document.getElementById("billingState").value = "";
        return false;
    }

    var zipString = new String(zip);
    var invalidChars = "!@#$%^&*()-~,'<.>/?;:\|";
    for (var i = 0; i < zipString.length; i++) {
        var c = zipString.charAt(i);
        if (invalidChars.indexOf(c) != "-1") {
            alert("Invalid characters in your zip code.  Please try again.");
            return false;
        }
    }

    return updateCityState(),show();
}

function show() {
    document.getElementById('hideState').style.display = "";
    document.getElementById('hideCity').style.display = "";
}

