var previousBeginsWith='';

function setUserDetailsCookies()
{


}

function getXMLHTTPRequest() {
   var req =  false;
   try {
      /* for Firefox */
      req = new XMLHttpRequest(); 
   } catch (err) {
      try {
         /* for some versions of IE */
         req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (err) {
         try {
            /* for some other versions of IE */
            req = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (err) {
            req = false;
         }
     }
   }
   
   return req;
}

function getServerTime() {
  var thePage = 'servertime.php';
  myRand = parseInt(Math.random()*999999999999999);
  var theURL = thePage +"?rand="+myRand;
  myReq.open("GET", theURL, true);
  myReq.onreadystatechange = theHTTPResponse;
  myReq.send(null);

}

var sourceOfCall='';

function getTown(country,beginsWith,source) 
{

  var thePage = 'http://www.alaba.com/app/getTownsInCountry.php';
  var theURL = thePage + "?beginsWith=" + beginsWith + "&country=" + country;

  if ((beginsWith.length>1) && (document.getElementById("previousBeginsWith").value.indexOf(beginsWith,0)!=0)||(beginsWith.length==2)) 
  {
      previousBeginsWith=beginsWith;
  	myReq.open("GET", theURL, true);
  	sourceOfCall = source;

  	myReq.onreadystatechange = theHTTPResponse;
  	myReq.send(null);

        
  }

}

function makeChoice(evt)
{
	if (evt)
	{
		var thisDiv=evt.target;
	} else
	{
		var thisDiv= window.event.srcElement;
	}
	document.getElementById("pTown").value=thisDiv.innerHTML;
	document.getElementById("popup").innerHTML="";
}

function theHTTPResponse() {
var townsArray = new Array();
  
  if (myReq.readyState == 4) {
    if(myReq.status == 200) 
    {
       if (myReq.responseXML)
       {
       
  		if (sourceOfCall="userdetails") 
                {

			var allTowns=myReq.responseXML.getElementsByTagName("atown");
                        for (var i=0; i<allTowns.length; i++)
                        {
				townsArray[i]=allTowns[i].getElementsByTagName("label")[0].firstChild;
                        }

  	var str=document.getElementById("pTown").text;
  	document.getElementById("pTown").className="";
  	if (str != "")
  	{
		document.getElementById("popup").innerHTML="";

		for (var i=0; i<townsArray.length; i++)
 		{
			var thisTown=townsArray[i].nodeValue;
                        if (true)
			{
				var tempDiv=document.createElement("div");
 				tempDiv.innerHTML=thisTown;
				tempDiv.onclick=makeChoice;
				tempDiv.className="suggestions";
				document.getElementById("popup").appendChild(tempDiv);
				
			}
		}
		var foundCt=document.getElementById("popup").childNodes.length;
		if (foundCt==0)
		{
			document.getElementById("pTown").className="error";
			document.getElementById("previousBeginsWith").value="";
		}
		if (foundCt==1)
		{
			document.getElementById("pTown").value=document.getElementById("popup").firstChild.innerHTML;
			document.getElementById("previousBeginsWith").value=document.getElementById("popup").firstChild.innerHTML;
			document.getElementById("popup").innerHML="";
		}
	}

        	}
      }
    }
  } else {

       if (sourceOfCall="userdetails") {
       		document.getElementById("popup").innerHTML = '<img src="ajax-loader.gif"/>';
	} 
  }
  
}