
createRequest = function ()
{
  var xmlHttp;
  // Microsoft Internet Explorer uses ActiveXObject
  if (window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Msxml12.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        //				this.log_write ("Could not create the ActiveXObject object", this.log.levels.FATAL);
      }
    }
  }
  // All other browsers use XMLHttpRequest
  else if (window.XMLHttpRequest)
  {
    try
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
      //			this.log_write ("Could not create the XMLHttpRequest object", this.log.levels.FATAL);
    }
  }
  return xmlHttp;
}


/**
 * The generic Ajax request
 */
function sendRequest(serverFile, callback, parameter)
{
  xmlHttpRequest = createRequest();

 
  xmlHttpRequest.open( "POST", "ajax/" + serverFile, true );
  xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

  // The callback function
  xmlHttpRequest.onreadystatechange = callback;

  // Send query
  xmlHttpRequest.send(parameter);
}




function searchFiliale(plz,land){
	sendRequest("../../ajax/searchFiliale.php", searchFilialeCallback, "plz=" + plz + "&land=" + land);
}

function searchFilialeCallback(){
	if (xmlHttpRequest.readyState == 4){
	  if (xmlHttpRequest.status == 200){
		  var r = xmlHttpRequest.responseText; 
		  var doc = document.getElementById('meinDiv');
		  doc.innerHTML = r;
		}
	}
}

function adminLaender(param){
	var doc = document.getElementById('meinDiv');
	doc.innerHTML = "moment bitte ...";
	sendRequest("../ajax/adminLaender.php", searchFilialeCallback, "param=" + param);
}

function adminFilialeCallback(){
	if (xmlHttpRequest.readyState == 4){
	  if (xmlHttpRequest.status == 200){
		  var r = xmlHttpRequest.responseText; 
		  var doc = document.getElementById('meinDiv');
		  doc.innerHTML = r;
		}
	}
}


function adminFiliale(plz,land){
	 var doc = document.getElementById('meinDiv');
	 doc.innerHTML = "moment bitte ...";
	sendRequest("../ajax/adminFiliale.php", searchFilialeCallback, "plz=" + plz + "&land=" + land);
}

function adminFilialeCallback(){
	if (xmlHttpRequest.readyState == 4){
	  if (xmlHttpRequest.status == 200){
		  var r = xmlHttpRequest.responseText; 
		  var doc = document.getElementById('meinDiv');
		  doc.innerHTML = r;
		}
	}
}

function delLand(param){
		var doc = document.getElementById('meinDiv');
		doc.innerHTML = "...";
		sendRequest("../ajax/delLand.php", delLandCallback, "param=" + param);	
}

function delLandCallback(){
	if (xmlHttpRequest.readyState == 4){
	  if (xmlHttpRequest.status == 200){
		 adminLaender();
		}
	}
}



function delFiliale(ask,param){
	if(ask == "yes"){
		sendRequest("../ajax/delFiliale.php", delFilialeCallback, "param=" + param);
	}
	else if (ask == "no"){adminFiliale();}
	else {
		var doc = document.getElementById('meinDiv');
		var text = 'Filiale wirklich loeschen: ';
	 	    text +='<a href="#" onClick=delFiliale("yes","' + param + '")>ja</a>&nbsp;&nbsp;';
			text +='<a href="#" onClick=delFiliale("no","' + param + '")>nein</a>';
		doc.innerHTML = text;
	}
	
}

function delFilialeCallback(){
	if (xmlHttpRequest.readyState == 4){
	  if (xmlHttpRequest.status == 200){
		 // var r = xmlHttpRequest.responseText; 
		 // var doc = document.getElementById('meinDiv');
		 // doc.innerHTML = r;
		 adminFiliale();
		}
	}
}


function updateLand(id,name,checked){
	var publish = 0;
	if( checked ) { publish = 1; }
	sendRequest("../ajax/updateLand.php", updateLandCallback, "id=" + id + "&name=" + name + "&publish=" + publish );
	
	//alert(" id " + id + " name " + name + " checked " + checked);

}
function updateLandCallback(){
	if (xmlHttpRequest.readyState == 4){
		if (xmlHttpRequest.status == 200){
			var r = xmlHttpRequest.responseText; 
		  	if(r == "ok"){adminLaender("alle");}
		 	else {
				var doc = document.getElementById('meinDiv');
				doc.innerHTML = r;
		  	}
	  	}
	}
}


function updateFiliale(id,land,name,street,plz,city,fon,web){
		
		var sendName = name.replace('&',"%26");
		//alert("Name : " + sendName);
		sendRequest("../ajax/updateFiliale.php", updateFilialeCallback, "id=" + id + "&land=" + land + "&name=" + sendName + "&street=" + 
					street + "&plz=" + plz + "&city=" + city + "&fon=" + fon + "&web=" + web);
}

function updateFilialeCallback(){
	if (xmlHttpRequest.readyState == 4){
		if (xmlHttpRequest.status == 200){
			var r = xmlHttpRequest.responseText; 
		  	if(r == "ok"){adminFiliale();}
		 	else {
				var doc = document.getElementById('meinDiv');
				doc.innerHTML = r;
		  	}
	  	}
	}
}

function newFiliale(land,name,street,plz,city,fon,web){
		var fehlt = "";
		var sendName = name.replace('&',"%26");
		//alert("Name : " + sendName);
		if(name.length < 1){fehlt="name ";}
		if(street.length < 1){fehlt += ", Strasse ";}
		//if(plz.length < 4){fehlt += ",PLZ ";}
		if(city.length < 1){fehlt += ",Stadt ";}
		
		if(fehlt.length>0){alert(" Bitte " + fehlt + " korrekt ausfüllen!");}
		else{
			sendRequest("../ajax/savFiliale.php", newFilialeCallback, "land=" + land + "&name=" + sendName + "&street=" + 
					street + "&plz=" + plz + "&city=" + city + "&fon=" + fon + "&web=" + web);
		}
}

function newFilialeCallback(){
	if (xmlHttpRequest.readyState == 4){
		if (xmlHttpRequest.status == 200){
			//var r = xmlHttpRequest.responseText; 
		  	adminFiliale();
	  	}
	}
}

function newLand(name,checked){
		var publish = 0;
		if( checked ) { publish = 1; }
		var fehlt = "";
		if(name.length < 1){ alert(" Bitte " + fehlt + " korrekt ausfüllen!");}
		else{
			var doc = document.getElementById('meinDiv');
			doc.innerHTML = "...";	
			sendRequest("../ajax/savLand.php", newLandCallback, "name=" + name + "&publish=" + publish);
		}
}

function newLandCallback(){
	if (xmlHttpRequest.readyState == 4){
		if (xmlHttpRequest.status == 200){
		  	adminLaender("alle");
	  	}
	}
}
