function getLocationsForZip(theField, remotePath)
{
    var path;
    remotePath += "?simpleballot_action=submit";
    remotePath += "&zipcode="+theField.value;
    selectLocation = document.getElementById('select_location');
    selectLocation.options.length = 0;
    patchworkHTTPRequest.open("GET", remotePath, true);
    patchworkHTTPRequest.onreadystatechange=locationsByZipCallback;
    patchworkHTTPRequest.send(null);
    return false;
}

function locationsByZipCallback()
{
    if (patchworkHTTPRequest.readyState==4)
    {
        json = eval('(' + patchworkHTTPRequest.responseText + ')');
        selectLocation = document.getElementById('select_location');
        for(x=0; x < selectLocation.options.length; x++)
        {
            selectLocation.options[x] = null;
        }
        for(x=0; x < json.length; x++)
        {
            selectLocation.options[x] = new Option(json[x][3] + ' | ' + json[x][4] + ' | ' + json[x][1] + ', ' + json[x][2], json[x][0]);
        }
        if(json.length < 3)
        {
            selectLocation.size = 2;
        }
        else if(json.length < 20)
        {
            selectLocation.size = json.length;
        }
        else
        {
            selectLocation.size = 20;
        }
    }
}

function getStereotypeForZip(theField, remotePath)
{
    remotePath += "?zipcode="+theField.value;
    patchworkHTTPRequest.open("GET", remotePath, true);
    patchworkHTTPRequest.onreadystatechange=stereotypeByZipCallback;
    patchworkHTTPRequest.send(null);
    return false;
}

function stereotypeByZipCallback()
{
    if (patchworkHTTPRequest.readyState==4)
    {
        json = eval('(' + patchworkHTTPRequest.responseText + ')');
        icon = document.getElementById('community-icon');
        id = json[0][4];
        url = icon.src.substr(0, icon.src.lastIndexOf('/'));
        icon.src = json[0][7];
        icon.style.visibility = 'visible';
        text = document.getElementById('community-label');
        text.firstChild.nodeValue = json[0][5];
        text.style.visibility = 'visible';
        about = document.getElementById('community-about');
        about.style.visibility = 'visible';
        ahref = document.getElementById('community-href');
        ahref.href = ahref.href.substr(0, ahref.href.lastIndexOf('/') + 1) + json[0][6];
    }
}

function verifyZip(theField, remotePath)
{
    remotePath += "?zipcode="+theField.value;
    patchworkHTTPRequest.open("GET", remotePath, true);
    patchworkHTTPRequest.onreadystatechange=verifyZipCallback;
    patchworkHTTPRequest.send(null);
    return false;
}

function verifyZipCallback() {
    if (patchworkHTTPRequest.readyState==4)
    {
		json = eval('(' + patchworkHTTPRequest.responseText + ')');
		if(json[0])
		{
			document.getElementById('zip').style.backgroundColor = '';			
		}
		else
		{
			document.getElementById('zip').style.backgroundColor = 'red';
			alert('Error: Invalid Zipcode');
		}
	}
}

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;
}

patchworkHTTPRequest = new getHTTPObject();
