/**************this is for vote.php page*****************************/

var xmlHttp
var destobj
var mycase

var formertext;
var formervalue;

function stripURL(myform, stripme) {
  URLArr = myform.action.split("?");
  myfilename = URLArr[0];

  if (URLArr.length > 1) {
    myParam = URLArr[1];
    URLArr2 = myParam.split("&");

    for (myx = 0; myx < URLArr2.length; myx++) {
      URLArr3 = URLArr2[myx].split("=");
      if (URLArr3[0] != stripme) {
        if (myfilename.indexOf("?") == -1) {
          myfilename = myfilename + "?";
        }
        else {
          myfilename = myfilename + "&";
        }
        myfilename = myfilename + URLArr3[0] + "=" + URLArr3[1];
      }
    }
  }
  myform.action = myfilename;
}

function addURL(myform, addme) {
  myfilename = myform.action;

  if (myform.action.indexOf("?") == -1) {
    myfilename = myfilename + "?";
  }
  else {
    myfilename = myfilename + "&";
  }
  myfilename = myfilename + addme;

  myform.action = myfilename;
}

function getRegion(mysrc, mydest, loadingtext) {
  destobj = mydest;
  xmlHttp = GetXmlHttpObject();

  if (xmlHttp==null) {
    //alert ("Your browser does not support AJAX!");
    doLoad(mysrc); //old style
    return;
  }

  //don't delete the first line which is "Select"
  formertext = mydest.options[0].text;
  formervalue = mydest.options[0].value;

  while (mydest.length > 1) {
    mydest.remove(mydest.length - 1);
  }

  mydest.disabled = true;
  mydest.options[0].text = loadingtext;

  document.getElementById("RegionIDimg").style.display = "";

  document.form1.CityID.disabled = true;
  document.form1.CityName.disabled = true;
  document.form1.CityFirst3Char.disabled = true;

  document.form1.CityID.value = "";
  document.form1.CityName.value = "";
  document.form1.CityFirst3Char.value = "";

  stripURL(document.form1, "Type");
  
  var url = "/ajax.php?CountryID=" + mysrc.options[mysrc.selectedIndex].value;
  xmlHttp.onreadystatechange = RegionChanged;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function RegionChanged() {
  if (xmlHttp.readyState==4) {
    if (xmlHttp.status==200) {
      var xmlDoc = xmlHttp.responseXML;
      var totalrow = xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue;

      for (var cnt = 1; cnt <= totalrow; cnt++) {
        myID = xmlDoc.getElementsByTagName("regionid" + cnt)[0].childNodes[0].nodeValue;
        myName = xmlDoc.getElementsByTagName("regionname" + cnt)[0].childNodes[0].nodeValue;
        myShortName = xmlDoc.getElementsByTagName("shortregionname" + cnt)[0].childNodes[0].nodeValue;
        insertOption2(destobj, myName, myShortName, myID);
      }

      destobj.options[0].text = formertext;
    }
    else {
      alert("Problem retrieving XML data: " + xmlHttp.statusText);
    }

    if (document.form1.CountryID.selectedIndex > 0) {
      destobj.disabled = false;
    }
  
    document.getElementById("RegionIDimg").style.display = "none";

    addURL(document.form1, "Type=CountryID");
  }
}

function getCity(mysrc, mydest, loadingtext) {
  destobj = mydest;
  xmlHttp = GetXmlHttpObject();

  if (xmlHttp==null) {
    //alert ("Your browser does not support AJAX!");
    doLoad(mysrc); //old style
    return;
  }

  document.form1.CityName.disabled = false;
  document.form1.CityID.disabled = false;
  document.form1.CityFirst3Char.disabled = false;

  document.form1.CityID.value = "";
  document.form1.CityName.value = "";
  document.form1.CityFirst3Char.value = "";

  stripURL(document.form1, "Type");
}

function GetXmlHttpObject() {
  var xmlHttp = null;

  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        //do nothing here;
      }
    }
  }

  return xmlHttp;
}

function insertOption(insertObj, insertText, insertValue) {
  var y = document.createElement('option');
  y.text = insertText;
  y.value = insertValue;

  try {
    insertObj.add(y,null); // standards compliant
  }
  catch(ex) {
    insertObj.add(y); // IE only
  }
}

function insertOption2(insertObj, insertText, shorttext, insertValue) {
  var y = document.createElement('option');
  y.text = shorttext;
  y.value = insertValue;

  try {
    insertObj.add(y,null); // standards compliant
  }
  catch(ex) {
    insertObj.add(y); // IE only
  }

  insertObj.options[insertObj.length - 1].setAttribute("full", insertText);
  insertObj.options[insertObj.length - 1].setAttribute("short", shorttext);
}

