Array.prototype.contains = function (elem) {
	var i;
	for (i = 0; i < this.length; i++) {
		if (this[i] === elem) {
			return true;
		}
	}
	
	return false;
};


ajaxRequest = function(u,f,m,b,h,s)
{
	this.url      = u;
	this.wState   = f || function() { };
	this.method   = m || "GET";
	this.body     = b || null;
	this.headers  = h || false;
	this.sync     = s || true;
	this.abortReq = false;
	
	this.req = (window.XMLHttpRequest) 
	?
	new XMLHttpRequest()
	:
	((window.ActiveXObject)
	 ?
	 new ActiveXObject("Microsoft.XMLHTTP")
	 :
	 false
	 );
	
	this.doRequest = function()
	{
		this.req.open(this.method,this.url,this.sync);
		if (this.headers)
		{
			for (var i=0; i<this.headers.length; i+=2)
			{
				this.req.setRequestHeader(
							  this.headers[i],this.headers[i+1]
							  );
			}
		}
		this.req.onreadystatechange = this.wState;
		(!this.abortReq) ? this.req.send(this.body) : this.req.abort();
	}
}



function userlogin(formular) {
	
	loading = document.getElementById("transmissionstatus");
	anmeldebutton = document.getElementById("anmelden");
	myemail = formular.email.value;
	mypasswort =  formular.passwort.value;
	
	var query = 'myemail='+myemail+'&mypasswort='+mypasswort;
	
	var xmlhttp = new ajaxRequest('../wp-content/themes/vukarts/functions/ajaxlistener.php',
				      function() {
					      var status = xmlhttp.req;
					      
					      if (status.readyState==4) {
						      if (status.status == 200) {
							      if (status.responseXML) {
								      var response = status.responseXML;
								      parseXML(response);
								      loading.style.display="none";
								      document.getElementById('neukunden').style.display='block';
								      document.getElementById('bestandskunden').style.display='none';
							      }
							      else {
								      loading.style.display="none";
								      alert (status.responseText);
								      anmeldebutton.style.display="inline";
							      }
							      
							      loading.style.display="none";
							      anmeldebutton.style.display="inline";
							      
							      
						      }
						      else {
							      loading.style.display="none";
							      anmeldebutton.style.display="inline";
						      }
					      }
				      }
				      ,"POST",
				      query,["Content-Type","application/x-www-form-urlencoded"]);
	
	loading.style.display="block";
	anmeldebutton.style.display="none";
	xmlhttp.doRequest();
}

function parseXML(xml)
{
	
	var root = xml.getElementsByTagName('root')[0];
		
	var nod1 = root.childNodes;
	
	var xmlzeilen = new Array('name','vorname','strasse','plz','ort','email2');
    	
	for (i=0;i<nod1.length;i++)
	{
		if (nod1[i].nodeType != 1) continue;
		for (j=0;j<nod1[i].childNodes.length;j++)
		{
			if (nod1[i].childNodes[j].nodeType != 1) continue;
			
			if (xmlzeilen.contains(nod1[i].childNodes[j].nodeName) ) {
				
				document.getElementById(nod1[i].childNodes[j].nodeName).value=nod1[i].childNodes[j].firstChild.nodeValue;	
			}
			
		}
	}
	
}  
