//Serialisierung der Requests
var queue = new Array();
var img = 0;
var cart = 0;
var cash = 0;

var debug = "";

//AjaxDatenabruf und Anzeige der Daten
//
//param string dataSource URL mit Query an den AjaxReplier
//param string objID LayerID dessen Inhalt aktualisiert werden soll
//param string streamType Flag für Gewichtung der Datenprozesse
//return void
//
function getData(dataSource,objID,streamType)
{
	//Initialisierung Ajax-Object
	var XMLHttpRequestObject = false;
	if(window.XMLHttpRequest) XMLHttpRequestObject = new XMLHttpRequest();
	else if(window.ActiveXObject) XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHttp");
	
	if(XMLHttpRequestObject)
	{
		//Call für zu ändernden Layer
		if(objID!="") var obj = document.getElementById(objID);
		//Request senden
		XMLHttpRequestObject.open("GET",dataSource);
		//Listener für Antwort
		XMLHttpRequestObject.onreadystatechange = function()
		{
			//Positive Antwort
			if(XMLHttpRequestObject.readyState==4 && XMLHttpRequestObject.status==200) 
			{
				//Ausgabe
				if(objID!="") obj.innerHTML = XMLHttpRequestObject.responseText;
				zeit = new Date();
				debug+= "getEnd:"+zeit.getHours()+":"+zeit.getMinutes()+":"+zeit.getSeconds()+":"+zeit.getMilliseconds()+" - "+dataSource+"<br />";
				processQueue(false);
			}	
		}
		XMLHttpRequestObject.send(null);
	}
}

//AjaxAufruf zum Senden der Formulardaten
//
//param string target URL mit Query an den AjaxReplier
//param string Formularname dessen Inhalt verarbeitet werden soll
//param string streamType Flag für Gewichtung der Datenprozesse momentan obsolet
//return void
//
function postData(target,postData,streamType)
{
	//Wenn das Formular nicht leer war
	if(postData!="")
	{
		//Initialisierung Ajax-Object
		var XMLHttpRequestObject = false;
		if(window.XMLHttpRequest) XMLHttpRequestObject = new XMLHttpRequest();
		else if(window.ActiveXObject) XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHttp");
		
		if(XMLHttpRequestObject)
		{
			//Senden der Datenvorbereiten
			XMLHttpRequestObject.open("POST",target);
			XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			//Warten auf Okay
			XMLHttpRequestObject.onreadystatechange = function()
			{
				if(XMLHttpRequestObject.readyState==4 && XMLHttpRequestObject.status==200) 
				{
					//alert(XMLHttpRequestObject.responseText+"1");
				}
			}
			//Senden
			XMLHttpRequestObject.send(postData);
			//Warten auf Empfangsbestätigung
			XMLHttpRequestObject.onreadystatechange = function()
			{
				if(XMLHttpRequestObject.readyState==4)
				{
					if(XMLHttpRequestObject.status==200) 
					{
						//alert(XMLHttpRequestObject.responseText);
					}
					zeit = new Date();
					debug+= "PostEnd:"+zeit.getHours()+":"+zeit.getMinutes()+":"+zeit.getSeconds()+":"+zeit.getMilliseconds()+" - "+target+"<br />";
					debug+= XMLHttpRequestObject.responseText+"<br />";
					//debug+= postData+"<br />";
					processQueue(false,XMLHttpRequestObject.responseText);
				}				
			}	
		}
	}
	else
	{
		zeit = new Date();
		debug+= "PostEnd:"+zeit.getHours()+":"+zeit.getMinutes()+":"+zeit.getSeconds()+":"+zeit.getMilliseconds()+" - "+target+"<br />";
		processQueue(false);	
	}
}

//Aufbereiten der Formulardaten
//
//param string form: 	Formularname
//return string:		die aufbereiteten Formulardaten
function buildPostData(form)
{
	var tagNames = new Array("input","textarea","select");
	var data = new Array(); 
	for(tagName in tagNames) 
	{
		for(i=0;i<document.forms[form].getElementsByTagName(tagNames[tagName]).length;i++)
		{
			elem = document.forms[form].getElementsByTagName(tagNames[tagName])[i];
			if(elem.type=="radio")
			{
				if(!data[elem.name]) data[elem.name] = -1;
				if(elem.checked) data[elem.name] = elem.value;
			}
			else if(elem.type=="checkbox") data[elem.name] = ((elem.checked)?elem.value:"");
			//aus irgendeinem Grund werden +Zeichen gefressen, also ersetzen
			else data[elem.name] = elem.value.split("+").join("[[PLUS]]").split("&").join("[[AND]]");
		}
	}
	var str = "";
	for(i in data) str+= ((str=="")?"":"&")+i+"="+data[i];
	debug+= "\n"+str+"\n";
	return str;
}

//Queue abarbeiten
//
//return void
//
function processQueue(init,txt)
{
	forwarder = false;
	if(!init && queue.length==1)
	{
		if(queue[0][2]=="addCart" || queue[0][2]=="validateCashpoint" || queue[0][2]=="validateCart" || queue[0][2]=="validateCartProceed") forwarder = queue[0][2];
	}
	if(!init) queue.shift();
	else
	{
		debug = "";
	}
	for(i in queue)	debug+= i+": "+queue[i][0]+" - "+queue[i][2]+"<br />";
	debug+= "&nbsp;<br />";
	if(queue.length>0)
	{
		zeit = new Date();
		debug+= "start:"+zeit.getHours()+":"+zeit.getMinutes()+":"+zeit.getSeconds()+":"+zeit.getMilliseconds()+" - "+queue[0][0]+" - "+queue[0][2]+"<br />";
	
		if(queue[0][2]=="post" || queue[0][2]=="addCart" || queue[0][2]=="validateCashpoint" || queue[0][2]=="validateCart" || queue[0][2]=="validateCartProceed") postData(queue[0][0],queue[0][1],queue[0][2]);
		else getData(queue[0][0],queue[0][1],queue[0][2]);	
	}
	else
	{
		if(forwarder=="addCart") processAddCartReply(txt);			
		else if(forwarder=="validateCashpoint") processValidateCashpoint(txt);			
		else if(forwarder=="validateCart" || forwarder=="validateCartProceed") processValidateCart(txt,forwarder);			
		//document.getElementById("loading").innerHTML = "&nbsp;";
		//document.getElementById("cart").innerHTML = debug+"<br />"+queue.length;
	}
}

function writeStart()
{
	var obj = getObj("cBackground");
	obj.style.backgroundImage = "url("+templatePath+"/images/hg_inhalt_breit.jpg)";
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=init","nav","std"));
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=nav","nav","std"));
	if(static==0)queue.push(new Array(serverPath+"/AjaxReplier.php?get=smallCart","cart","std"));
	if(cat!=0) queue.push(new Array(serverPath+"/AjaxReplier.php?get=cat&id="+cat,"content","std"));
	else if(art!=0) queue.push(new Array(serverPath+"/AjaxReplier.php?get=art&id="+art,"content","std"));
	else if(static!=0) queue.push(new Array(serverPath+"/AjaxReplier.php?get=static&static="+static,"content","std"));
	else queue.push(new Array(serverPath+"/AjaxReplier.php?get=cat&id=all","content","std"));
	processQueue(true);	
}

function getCategory(id)
{
	var obj = getObj("cBackground");
	obj.style.backgroundImage = "url("+templatePath+"/images/hg_inhalt_breit.jpg)";
	if(document.forms.length>0 && document.order.vorname)
	{
		queue.push(new Array(serverPath+"/AjaxReplier.php",buildPostData("order"),"post"));
		queue.push(new Array(serverPath+"/AjaxReplier.php?get=smallCart","cart","std"));
	}
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=smallCart","cart","std"));
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=cat&id="+id,"content","std"));
	processQueue(true);	
	cat = id;
	img = 0;
	art = 0;
	static = 0;
	cart = 0;
	cash = 0;
}

function getArticle(id)
{
	var obj = getObj("cBackground");
	obj.style.backgroundImage = "url("+templatePath+"/images/hg_inhalt.jpg)";
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=smallCart","cart","std"));
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=art&id="+id,"content","std"));
	processQueue(true);	
	art = id;
	img = 0;
	static = 0;
	cart = 0;
	cash = 0;
}

function editArticle(id,cartID)
{
	var obj = getObj("cBackground");
	obj.style.backgroundImage = "url("+templatePath+"/images/hg_inhalt.jpg)";
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=smallCart","cart","std"));
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=art&id="+id+"&rewrite="+cartID,"content","std"));
	processQueue(true);	
	art = id;
	img = 0;
	static = 0;
	cart = 0;
	cash = 0;
}

function changeImage(a_id,i_id)
{
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=img&a_id="+a_id+"&i_id="+i_id,"imgCell","std"));
	processQueue(true);	
	img = i_id;
}

function changeLang(l)
{
	if(lang!=l)
	{
		queue.push(new Array(serverPath+"/AjaxReplier.php?get=lang&lang="+l,"","std"));
		queue.push(new Array(serverPath+"/AjaxReplier.php?get=nav","nav","std"));
		if(art!=0)
		{
			queue.push(new Array(serverPath+"/AjaxReplier.php?get=art&id="+art,"content","std"));
			if(img!=0) queue.push(new Array(serverPath+"/AjaxReplier.php?get=img&a_id="+art+"&i_id="+img,"imgCell","std"));
		}
		else if(cat!=0) queue.push(new Array(serverPath+"/AjaxReplier.php?get=cat&id="+cat,"content","std"));
		else if(static!=0) queue.push(new Array(serverPath+"/AjaxReplier.php?get=static&static="+static,"content","std"));
		else if(cart!=0) queue.push(new Array(serverPath+"/AjaxReplier.php?get=cart","content","std"));
		else if(cash!=0) queue.push(new Array(serverPath+"/AjaxReplier.php?get=cashpoint","content","std"));
		else queue.push(new Array(serverPath+"/AjaxReplier.php?get=cat&id=all","content","std"));
		if(static==0 && cart==0 && cash==0) queue.push(new Array(serverPath+"/AjaxReplier.php?get=smallCart","cart","std"));
		processQueue(true);	
		lang = l;
	}
}

function getStatic(content)
{
	var obj = getObj("cBackground");
	obj.style.backgroundImage = "url("+templatePath+"/images/hg_inhalt_breit.jpg)";
	var obj = getObj("cart");
	obj.innerHTML = "";
	if(document.order && document.order.vorname)
	{
		queue.push(new Array(serverPath+"/AjaxReplier.php",buildPostData("order"),"post"));
		queue.push(new Array(serverPath+"/AjaxReplier.php?get=smallCart","cart","std"));
	}
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=static&static="+content,"content","std"));
	processQueue(true);	
	static = content;
	cat = 0;
	art = 0;
	img = 0;
	cart = 0;
	cash = 0;
}

function submitArtForm(art)
{
	queue.push(new Array(serverPath+"/AjaxReplier.php?add="+art,buildPostData("order"),"addCart"));
	processQueue(true);
}

function processAddCartReply(txt)
{
	if(txt.indexOf("Category")>-1)
	{
		txt = txt.split(":");
		var obj = getObj("cBackground");
		obj.style.backgroundImage = "url("+templatePath+"/images/hg_inhalt_breit.jpg)";
		queue.push(new Array(serverPath+"/AjaxReplier.php?get=smallCart","cart","std"));
		queue.push(new Array(serverPath+"/AjaxReplier.php?get=cat&id="+txt[1],"content","std"));
		processQueue(true);
		/*var obj = getObj("errorBox");
		obj.innerHTML = txt;
		move("errorBox");*/
	}
	else
	{
		var obj = getObj("errorBox");
		obj.innerHTML = txt;
		move("errorBox");
	}
}

function showCart()
{
	var obj = getObj("cBackground");
	obj.style.backgroundImage = "url("+templatePath+"/images/hg_inhalt_breit.jpg)";
	var obj = getObj("cart");
	obj.innerHTML = "";
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=cart","content","std"));
	processQueue(true);
	cart = 1;
	static = 0;
	cat = 0;
	art = 0;
	img = 0;
	cash = 0;
}

function delCartItem(a,b)
{
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=delCartItem&cart="+a+"&elem="+b,"cart","std"));
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=cart","content","std"));
	processQueue(true);
}

function updateCart(proceed)
{
	queue.push(new Array(serverPath+"/AjaxReplier.php?validate=true",buildPostData("order"),"validateCart"+((proceed)?"Proceed":"")));
	processQueue(true);
}

function processValidateCart(txt,type)
{
	if(txt.indexOf("Validated")>-1 && type=="validateCartProceed")
	{
		txt = txt.split(":");
		queue.push(new Array(serverPath+"/AjaxReplier.php?get=cashpoint","content","std"));
		processQueue(true);
		cart = 0;
		cash = 1;
		/*var obj = getObj("errorBox");
		obj.innerHTML = txt;
		move("errorBox");*/
	}
	else if(txt.indexOf("Validated")>-1 && type=="validateCart")
	{
		txt = txt.split(":");
		queue.push(new Array(serverPath+"/AjaxReplier.php?get=cart","content","std"));
		processQueue(true);
		/*var obj = getObj("errorBox");
		obj.innerHTML = txt;
		move("errorBox");*/
	}
	else
	{
		var obj = getObj("errorBox");
		obj.innerHTML = txt;
		move("errorBox");
	}
	//alert(txt);	
}

function showCashpoint()
{
	var obj = getObj("cBackground");
	obj.style.backgroundImage = "url("+templatePath+"/images/hg_inhalt_breit.jpg)";
	var obj = getObj("cart");
	obj.innerHTML = "";
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=cashpoint","content","std"));
	processQueue(true);
	cart = 0;
	static = 0;
	cat = 0;
	art = 0;
	img = 0;
	cash = 1;
}

function recalcCashpoint()
{
	queue.push(new Array(serverPath+"/AjaxReplier.php",buildPostData("order"),"post"));
	queue.push(new Array(serverPath+"/AjaxReplier.php?get=prices","preise","std"));
	processQueue(true);
}

function submitCashpoint()
{
	queue.push(new Array(serverPath+"/AjaxReplier.php?validate=true",buildPostData("order"),"validateCashpoint"));
	processQueue(true);
}

function processValidateCashpoint(txt)
{
	if(txt.indexOf("Validated")>-1)
	{
		txt = txt.split(":");
		var obj = getObj("cBackground");
		obj.style.backgroundImage = "url("+templatePath+"/images/hg_inhalt_breit.jpg)";
		queue.push(new Array(serverPath+"/AjaxReplier.php?get=finalize","content","std"));
		processQueue(true);
		/*var obj = getObj("errorBox");
		obj.innerHTML = txt;
		move("errorBox");*/
	}
	else
	{
		var obj = getObj("errorBox");
		obj.innerHTML = txt;
		move("errorBox");
	}
	//alert(txt);	
}
