//save an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

//create an instance of XMLHttpRequest
function createXmlHttpRequestObject()
{
  //save the reference on an XmlHttpRequestObject
  var xmlHttp;
  try{
     //try to create a XmlHttpRequest-Object
     xmlHttp = new XMLHttpRequest();
     }
     catch(e){
             //for IE6 or older
             var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                             "MSXML2.XMLHTTP.5.0",
                                             "MSXML2.XMLHTTP.4.0",
                                             "MSXML2.XMLHTTP.3.0",
                                             "MSXML2.XMLHTTP",
                                             "Microsoft.XMLHTTP");
             //try Prog-IDs until it works
             for(var i=0; i< XmlHttpVersions.length && !xmlHttp; i++){
                try{
                   //try to create a XMLHttpRequest-Object
                   xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
                   }//END try
                   catch(e){}//no errors or exceptions were displayed
                }//END for(var i=0; i< XmlHttpVersions.length && !xmlHttp; i++)
             }//END catch(e)
   //returns the object or an errormessage
   if(!xmlHttp)
    alert("Error creating XMLHttpRequest object.");
    else
        return xmlHttp;
}//END function createXmlHttpRequestObject()



function CheckAddress(id)
{
  //if isn`t empty xmlHttp
  if(xmlHttp){
    //try to connect to server
    try{      
       var option = document.getElementById(id).value;           
       //create parameterstring
       var params = "key=" + id + "&value=" + option;
       xmlHttp.open("GET", "ajax_address.php?" + params, true);
       xmlHttp.onreadystatechange = handleRequestStateChangeAddress;
       xmlHttp.send(null);
       }//END try
       //shows an errormessage on failed request
       catch(e){
               //alert("Can`t connect to server: \n" + e.toString());
               }//END catch     
    }//END if(xmlHttp)
}//END function CheckMyAccount(id)



//Funktion zur Behandlung der HTTP-Antwort
function handleRequestStateChangeAddress()
{
  //shows status of the request
  if (xmlHttp.readyState == 4){
    //continue only if HTTP-Status "OK"
    if(xmlHttp.status == 200){
      try{
         handleServerResponseAddress();
         }//END try
         catch(e){
                 //display errors
                 //alert("Error reading the response: " + e.toString());
                 }//END catch
      }//END if(xmlHttp.status == 200)
      else{
          //display status message
          //alert("There was a problem retrieving the data: \n" + xmlHttp.statusText);
          }//END else(xmlHttp.status == 200)
    }//END if (xmlHttp.readyState == 4)
}//END function handleRequestStateChangeMyAccount()



function handleServerResponseAddress()
{
 //get serveranswer as XML-DOM Object
 var response  = xmlHttp.responseText;
 //display message to user
 var splitted = response.split(",");
 var id = splitted[0];
 var result = splitted[1];
 if(splitted[2].length != 0){
   var citylen = document.getElementById("city").value.length;
   }
 if(result != "" && result == 0){
   document.getElementById(id).style.border = "1px solid #f00";
   }//END if(result != "" && result == 0)
   else{
        document.getElementById(id).style.border = "1px solid #ccc";
        }//END else(result != "" && result == 0) 
 if(citylen == 0 && splitted[2].length != 0){
   document.getElementById("city").value = splitted[2];
   document.getElementById("city").style.border = "1px solid #ccc";
   }//END if(citylen == 0 && splitted[2].length != 0)
    
 
}//END function handleServerResponseMyAccount



