﻿var rules=new Array();
rules[0]='firma:Firma|maxlength|60';
rules[1]='abteilung:Abteilung|maxlength|60';
rules[2]='sex:Anrede|required';
rules[3]='vorname:Vorname|required';
rules[4]='vorname:Vorname|maxlength|60';
rules[5]='nachname:Nachname|required';
rules[6]='nachname:Nachname|maxlength|60';
rules[7]='strasse:Straße|required';
rules[8]='strasse:Straße|maxlength:60';
rules[9]='nr:Nummer|required';
//rules[10]='nr:Nummer|numeric';
rules[10]='nr:Nummer|maxlength|10';
rules[11]='plz:Postleitzahl|required';
rules[12]='plz:Postleitzahl|numeric';
rules[13]='plz:Postleitzahl|maxlength|5';
rules[14]='ort:Ort|required';
rules[15]='ort:Ort|maxlength|60';
rules[16]='vorwahl:Vorwahl|required';
rules[17]='vorwahl:Vorwahl|numeric';
rules[18]='vorwahl:Vorwahl|maxlength|5';
rules[19]='telNr:Telefon-Nr.|required';
rules[20]='telNr:Telefon-Nr.|numeric';
//rules[22]='telNr:Telefon-Nr.|maxlength|6';
//rules[23]='email:Email|required';
rules[21]='email:Email|email';
//rules[25]='foodIdList:Food List|required|Chua chon mon an';
var req;
var splitter1 = "------------------ Nächste Packung ------------------"
var splitter2 = "-----";
var mindest = 20;
var isEnoughMindest = false;
var noImageName = "noimage";

function getCurrentPointOfTime()
{
	var nd = new Date();
	var d = nd.getTime();
	return d;
}
function getAmount()
{
	tblCartList = document.getElementById('cartList');
	hidId = document.getElementById('amountList');
	str = "";
	for(i = 0; i < tblCartList.rows.length -1; i++)
	{
		cell = tblCartList.rows[i + 1].cells[0];
		//if(tblCartList.rows[i].cells[0].innerText == splitter1)
		if(cell.textContent == splitter1)
		{
			count = count + 1;
			//str = str + "hello";
			//alert(str);
			//str = str + "@" + cell.innerText + "@";
		}
		else
		{
			str = str + "@" + cell.textContent + "@";
		}
	}
	hidId.value = str;
}
function checkMonAn()
{
	a = trim(document.getElementById("foodIdList").value.replace(splitter2,""));
	if(a == "")
	{
		alert(FOOD_NOT_SELECTED);
		return false;
	}
	else
	{
		if(!isEnoughMindest)
		{
			alert(UNDER_20EUR);
			return false;
		}
		return true;
	}
}


function getFood(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest( );
    }
    else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("Get",url,true);
    req.onreadystatechange = callback;
    req.send(null);
}

function callback( ) {
    if (req.readyState==4) {
        if (req.status == 200) {			
            nonMSPopulate();            
        }
    }
}

function nonMSPopulate( ) {
	var resp = req.responseText;
	var parser = new DOMParser( );
	var xmlDoc = parser.parseFromString(resp,"text/xml");

	nos = xmlDoc.getElementsByTagName('no');
	nas = xmlDoc.getElementsByTagName('name');
	des = xmlDoc.getElementsByTagName('des');
	pris = xmlDoc.getElementsByTagName('price');	
	imgs = xmlDoc.getElementsByTagName('img');
    colors = xmlDoc.getElementsByTagName('color');
	gpImg = xmlDoc.getElementsByTagName('gpImg')[0].childNodes[0].nodeValue;
	gpColor = xmlDoc.getElementsByTagName('gpColor')[0].childNodes[0].nodeValue;
	tbl = document.getElementById('tblFood');
	count = tbl.rows.length;
	
	for(i = 0; i < count; i++)
	{
		tbl.deleteRow(0);
	}
	if(gpImg && trim(gpImg) != "noimage" && trim(gpImg) != "")
	{
		showImage(gpImg);
	}else
	{
		showImage(noImageName);
	}
	
	for(i = 0; i < nos.length; i++)
	{
		row = tbl.insertRow(tbl.rows.length);
		cell1 = row.insertCell(0);
		cell2 = row.insertCell(1);
		cell3 = row.insertCell(2);
		cell4 = row.insertCell(3);
		cell1.className = "stt";
		cell2.className = "name";
		cell3.className = "price";
		cell4.className = "hidden";
		cell1.innerHTML = "";//nos[i].childNodes[0].nodeValue;//(i + 1);// + '<input type="text" name="hiddenCartRowId" value="' + nos[i].childNodes[0].nodeValue + '"/>';
		if (imgs[i].childNodes[0].nodeValue == noImageName) {
            st = '<a href="" ';
		} else {			
			st = '<a href="" onmouseout="doUnTip();" onmouseover="showDescription(\'' + imgs[i].childNodes[0].nodeValue + '\');//showImage(\'' + imgs[i].childNodes[0].nodeValue + '\');"';            
		}
		if (colors[i].childNodes[0].nodeValue == DEFAULT_COLOR) {
			st = st + ' onclick="addCart(' + i + ',' + nos[i].childNodes[0].nodeValue + ');return false;" >';
		}  else {
			st = st + ' onclick="addCart(' + i + ',' + nos[i].childNodes[0].nodeValue + ');return false;" style="color:#' + colors[i].childNodes[0].nodeValue + '">';
		}
		
		st = st + nas[i].childNodes[0].nodeValue + '</a><div class="descriptionShow" id="descrp' + i + '">' + des[i].childNodes[0].nodeValue + '<div>';
		cell2.innerHTML = st;
		cell3.innerHTML = pris[i].childNodes[0].nodeValue + ' €';
		cell4.innerHTML = nas[i].childNodes[0].nodeValue;
	}
	document.getElementById("foodlist").scrollTop = 0;
}

function msPopulate( ) {
	var resp = req.responseText;
	var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false";
	xmlDoc.loadXML(resp);
	
	nodes = xmlDoc.documentElement.childNodes;
	nos = xmlDoc.getElementsByTagName('no');
	nas = xmlDoc.getElementsByTagName('name');
	des = xmlDoc.getElementsByTagName('des');
	pris = xmlDoc.getElementsByTagName('price');
	
	tbl = document.getElementById('tblFood');
	count = tbl.rows.length
	for(i = 0; i < count; i++)
	{
		tbl.deleteRow(0);
	}
	
	for(i = 0; i < nos.length; i++)
	{
		row = tbl.insertRow(tbl.rows.length);
		cell1 = row.insertCell(0);
		cell2 = row.insertCell(1);
		cell3 = row.insertCell(2);
		cell4 = row.insertCell(3);
		cell1.className = "stt";
		cell2.className = "name";
		cell3.className = "price";
		cell4.className = "hidden";
		cell1.innerHTML = (i + 1);// + '<input type="text" name="hiddenCartRowId" value="' + nos[i].childNodes[0].nodeValue + '"/>';
		if (colors[i].childNodes[0].nodeValue == DEFAULT_COLOR) {        
		    st = '<span onmouseover="showDescription(\'' + des[i].childNodes[0].nodeValue + '\');//showImage(\'' + imgs[i].childNodes[0].nodeValue + '\');"'
        } else {
			st = '<span style="color:#' + colors[i].childNodes[0].nodeValue + '" onmouseover="showDescription(\'' + des[i].childNodes[0].nodeValue + '\');//showImage(\'' + imgs[i].childNodes[0].nodeValue + '\');"'            
        }
		//
		st = st + ' onclick="addCart(' + i + ',' + nos[i].childNodes[0].nodeValue + ');" >';
		st = st + nas[i].childNodes[0].nodeValue + '</span>';
		cell2.innerHTML = st;
		cell3.innerHTML = pris[i].childNodes[0].nodeValue + ' €';
		cell4.innerHTML = nas[i].childNodes[0].nodeValue;
	}
	document.getElementById("foodlist").scrollTop = 0;
}

function showDescription(imgDir)
{
	if(trim(imgDir) != "noimage")
	{
		document.getElementById("spanToolTip").innerHTML = '<img id="foodImages" src="images/' + imgDir + '" width="424" height="166">';
		TagToTip('spanToolTip', 424, 166);
	}

}

function addCart(index, foodId)
{
    if (check_destance){        
        var obj ;
        obj = document.getElementById('plz') ;
        if (trim(obj.value)=="") {
            alert(INPUT_PLZ);
            obj.focus();
            return;
        }
        obj = document.getElementById('ort') ;
        if (trim(obj.value)=="") {
            alert(INPUT_ORT);
            obj.focus();
            return;
        }
        obj = document.getElementById('strasse') ;
        if (trim(obj.value)=="") {
            alert(INPUT_STR);
            obj.focus();
            return;
        }
        obj = document.getElementById('nr') ;
        if (trim(obj.value)=="") {
            alert(INPUT_NR);
            obj.focus();
            return;
        }
		
        if (distance == 0) {
            return;
        }
		
        if (accept_order == 0 ){
            //alert(CANNOT_ORDER);
            return;
        }
    }
    
	tblCartList = document.getElementById('cartList');
	tbl = document.getElementById('tblFood');
	hidId = document.getElementById('foodIdList');
	arrId = hidId.value.split(splitter2);
	strId = arrId[arrId.length - 1];
	uniqueId = getCurrentPointOfTime();
	if(strId.indexOf("@" + foodId + "@") < 0 )
	{
		row = tblCartList.insertRow(tblCartList.rows.length);
		cell1 = row.insertCell(0);
		cell2 = row.insertCell(1);
		cell3 = row.insertCell(2);
		cell4 = row.insertCell(3);
		cell1.className = "stt";
		cell2.className = "artikel";
		cell3.className = "price";
		cell4.className = "editieren";
		cell1.innerHTML = 1;
		cell2.innerHTML = trim(tbl.rows[index].cells[3].textContent) + '<input type="text" name="hiddenCartRowId" style="display:none;" value="' + foodId + '"/><input type="text" name="hiddenFoodExtra" style="display:none" value="" />';
		cell2.innerHTML = cell2.innerHTML + '<input type="text" name="hiddenUniqueId" value="' + uniqueId + '" style="display:none"/>';
		cell3.innerHTML = trim(tbl.rows[index].cells[2].textContent);
		st = '<button class="add" ondblclick="upCounter(' + uniqueId + ',' + foodId + ',1,' + arrId.length + ')" onclick="upCounter(' + uniqueId + ',' + foodId + ',1,' + arrId.length + ')"></button>';
		st = st + '<button class="minus" ondblclick="downCounter(' + uniqueId + ',' + foodId + ',1,' + arrId.length + ')" onclick="downCounter(' + uniqueId + ',' + foodId + ',1,' + arrId.length + ')"></button>';
		st = st + '<button class="cancel" onclick="removeCart(' + uniqueId + ',' + foodId + ',' + arrId.length + ');"></button><button class="anderung" onclick="addFoodExtra(' + uniqueId + ',' + foodId + ',' + arrId.length + ');"></button>';
		cell4.innerHTML = st;
		hidId.value = hidId.value + "@" + foodId + "@";
	}else
	{
		tblCartList = document.getElementById('cartList');
		hidId = document.getElementById('foodIdList');
		hex = document.getElementsByName('hiddenFoodExtra');
		hUnId = document.getElementsByName('hiddenUniqueId');
		index2 = foodId;
		unit = 1;
		zone = arrId.length;
		count = 0;
		idx1 = -1;
		idx2 = -1;
		for(i = 0; i < tblCartList.rows.length - 1; i++)
		{
			if(tblCartList.rows[i].cells[0].textContent == splitter1)
			{
				count = count + 1;
			}			
			if((document.getElementsByName("hiddenCartRowId")[i - count].value == index2)&&(count == (zone - 1)))
			{
				if(trim(hex[i - count].value) == "")
				{					
					idx1 = i - count;
				}
				else
				{			
					idx2 = i - count;
				}
				//break;
			}
		}
		if(idx1 != -1)
		{
			upCounter(hUnId[idx1].value , foodId, 1, arrId.length);
		}
		else
		{
			row = tblCartList.insertRow(tblCartList.rows.length);
			cell1 = row.insertCell(0);
			cell2 = row.insertCell(1);
			cell3 = row.insertCell(2);
			cell4 = row.insertCell(3);
			cell1.className = "stt";
			cell2.className = "artikel";
			cell3.className = "price";
			cell4.className = "editieren";
			cell1.innerHTML = 1;
			cell2.innerHTML = trim(tbl.rows[index].cells[3].textContent) + '<input type="text" name="hiddenCartRowId" style="display:none;" value="' + foodId + '"/><input type="text" name="hiddenFoodExtra" style="display:none" value="" />';
			cell2.innerHTML = cell2.innerHTML + '<input type="text" name="hiddenUniqueId" value="' + uniqueId + '" style="display:none"/>';
			cell3.innerHTML = trim(tbl.rows[index].cells[2].textContent);
			st = '<button class="add" ondblclick="upCounter(' + uniqueId + ',' + foodId + ',1,' + arrId.length + ')" onclick="upCounter(' + uniqueId + ',' + foodId + ',1,' + arrId.length + ')"></button>';
			st = st + '<button class="minus" ondblclick="downCounter(' + uniqueId + ',' + foodId + ',1,' + arrId.length + ')" onclick="downCounter(' + uniqueId + ',' + foodId + ',1,' + arrId.length + ')"></button>';
			st = st + '<button class="cancel" onclick="removeCart(' + uniqueId + ',' + foodId + ',' + arrId.length + ');"></button><button class="anderung" onclick="addFoodExtra(' + uniqueId + ',' + foodId + ',' + arrId.length + ');"></button>';
			cell4.innerHTML = st;
			hidId.value = hidId.value + "@" + foodId + "@";
		}
		//upCounter(foodId, 1, arrId.length);
	}
	sum();
}

function splitCart()
{
	tblCartList = document.getElementById('cartList');
	hidId = document.getElementById('foodIdList');
	if((trim(tblCartList.rows[tblCartList.rows.length - 1].cells[0].textContent) != splitter1)&&(trim(hidId.value) != ""))
	{
		row = tblCartList.insertRow(tblCartList.rows.length);
		cell1 = row.insertCell(0);
		cell1.innerHTML = splitter1;
		cell1.colSpan = "4";
		cell1.className="split";
		hidId.value = hidId.value + " " + splitter2 + " ";
	}
}

function removeCart(uniqueId, index, zone)
{
	tblCartList = document.getElementById('cartList');
	hidId = document.getElementById('foodIdList');
	hUnId = document.getElementsByName('hiddenUniqueId');
	count = 0;
	for(i = 0; i < tblCartList.rows.length; i++)
	{
		if(tblCartList.rows[i].cells[0].textContent == splitter1)
		{
			count = count + 1;
		}
		if((document.getElementsByName("hiddenCartRowId")[i - count].value == index)&&(count == (zone - 1))&&(uniqueId == hUnId[i - count].value))
		{
			//alert('prepare to delete row ' + (i + 1));
			//alert('@' + trim(document.getElementsByName("hiddenCartRowId")[i].value) + '@');
			hidId.value = hidId.value.replace('@' + trim(document.getElementsByName("hiddenCartRowId")[i - count].value) + '@','');
			tblCartList.deleteRow(i + 1);
			break;
		}
	}
	sum();
}

function clearCart()
{
	tblCartList = document.getElementById('cartList');
	
	count = tblCartList.rows.length;
	for(i = 1; i < count; i++)
	{
		tblCartList.deleteRow(1);
	}
	document.getElementById('foodIdList').value = '';
}

function trim(strToTrim)
{
    strToTrim = this != window? this : strToTrim;
    return strToTrim.replace(/^\s+/, '').replace(/\s+$/, '');
}

function upCounter(uniqueId, index, unit, zone)
{
	tblCartList = document.getElementById('cartList');
	hidId = document.getElementById('foodIdList');
	hUnId = document.getElementsByName('hiddenUniqueId');
	count = 0;
	for(i = 0; i < tblCartList.rows.length; i++)
	{
		if(tblCartList.rows[i].cells[0].textContent == splitter1)
		{
			count = count + 1;
		}
		if((document.getElementsByName("hiddenCartRowId")[i - count].value == index)&&(count == (zone - 1))&&(uniqueId == hUnId[i - count].value))
		{
			cell = tblCartList.rows[i + 1].cells[0];
			cell.textContent = eval(trim(cell.textContent)) + unit;
			break;
		}
	}
	sum();
}

function downCounter(uniqueId, index, unit, zone)
{
	tblCartList = document.getElementById('cartList');
	hidId = document.getElementById('foodIdList');
	hUnId = document.getElementsByName('hiddenUniqueId');
	count = 0;
	for(i = 0; i < tblCartList.rows.length; i++)
	{
		if(tblCartList.rows[i].cells[0].textContent == splitter1)
		{
			count = count + 1;
		}
		if((document.getElementsByName("hiddenCartRowId")[i - count].value == index)&&(count == (zone - 1))&&(uniqueId == hUnId[i - count].value))
		{
			cell = tblCartList.rows[i + 1].cells[0];
			if(eval(trim(cell.textContent)) > unit)
			{
				cell.textContent = eval(trim(cell.textContent)) - unit;
			}
			break;
		}
	}
	sum();
}

window.onresize = resize;
function resize()
{
	if(document.body.offsetWidth > 900)
	{
		document.getElementById("homebtn").style.left = (document.body.offsetWidth - 900) /2 + 785;
		//alert(document.getElementById("homebtn"));
	}
}
function sum()
{
	tblCartList = document.getElementById('cartList');
	var summe  = 0;
	for(i = 0; i < tblCartList.rows.length - 1; i++)
	{
		//alert(i);
		if(tblCartList.rows[i + 1].cells[0].textContent != splitter1)
		{
			summe = summe + eval(tblCartList.rows[i + 1].cells[0].textContent + "*" + convertDot(tblCartList.rows[i + 1].cells[2].textContent));
		}
	}
	summe = Math.round(summe * 100);
	document.getElementById("divSum").textContent = deconvertDot(summe/100);
    
	if((summe/100) >= mindest)
	{
		document.getElementById("mindest1").style.visibility = "hidden";
		document.getElementById("mindest2").style.visibility = "hidden";
		isEnoughMindest = true;
	}
	else
	{
		if(mindest == 0) {
			isEnoughMindest = true;
		} else {
			document.getElementById("mindest1").style.visibility = "visible";
			document.getElementById("mindest2").style.visibility = "visible";
			isEnoughMindest = false;
		}
	}
}
function convertDot(str)
{	
	return str.replace(' ', '').replace('€', '').replace('.', '').replace(',', '.');
}

function deconvertDot(str)
{
	str = str + "";
	if(str.indexOf(".") >= 0)
	{
		return trim(str.replace(".",",")) + " €";
	}
	else
	{
		return trim(str) + ",00 €";
	}
}
function showImage(str)
{
	if(trim(str) != "noimage")
	{		
		var obj = document.getElementById("foodimg");
		obj.innerHTML =  '<img id="foodImages" height="166" width="424" src="images/' + str +'"/>';
	}
}
function addFoodExtra(uniqueId, index, zone)
{
	tblCartList = document.getElementById('cartList');
	hidId = document.getElementById('foodIdList');
	hUnId = document.getElementsByName('hiddenUniqueId');
	count = 0;
	for(i = 0; i < tblCartList.rows.length; i++)
	{
		if(tblCartList.rows[i].cells[0].textContent == splitter1)
		{
			count = count + 1;
		}
		
		if((document.getElementsByName("hiddenCartRowId")[i - count].value == index)&&(count == (zone - 1))&&(uniqueId == hUnId[i - count].value))
		{
			//hiddenFoodExtra
			var objArg;// = new Object();
			objArg2 = document.getElementsByName("hiddenFoodExtra")[i - count].value;
			//objWin = window.showModalDialog("popup.htm", objArg, "dialogHeight:50px; dialogWidth:80px" );
			objArg = prompt(USE_FOOD_WITH, objArg2);
			if(objArg != null)
			{
				document.getElementsByName("hiddenFoodExtra")[i - count].value = objArg;
				if(trim(objArg) != "")
				{
					changeClass(uniqueId, index, zone, "anderung");
				}
				else
				{
					changeClass(uniqueId, index, zone, "artikel");
				}
			}
			break;
		}
	}
	//window.open("popup.htm");
}
function getExtra()
{
	//tblCartList = document.getElementById('cartList');
	hidId = document.getElementById('extraList');
	foodExtra = document.getElementsByName('hiddenFoodExtra');
	str = "";
	for(i = 0; i < foodExtra.length; i++)
	{
		str = str + foodExtra[i].value + " @@@";
	}
	//alert(str);
	hidId.value = str;
}
function changeClass(uniqueId, index, zone, clssName)
{
	tblCartList = document.getElementById('cartList');
	hUnId = document.getElementsByName('hiddenUniqueId');
	count = 0;
	for(i = 0; i < tblCartList.rows.length; i++)
	{
		if(tblCartList.rows[i].cells[0].textContent == splitter1)
		{
			count = count + 1;
		}
		//alert(document.getElementsByName("hiddenCartRowId").length + "v"+ (i - count));
		//alert(document.getElementsByName("hiddenCartRowId")[i - count].value + "__" + index + ", " + count + "___" + zone);
		if((document.getElementsByName("hiddenCartRowId")[i - count].value == index)&&(count == (zone - 1))&&(uniqueId == hUnId[i - count].value))
		{
			
			cell = tblCartList.rows[i + 1].cells[1];
			cell.className = clssName;
			break;
		}
	}
}
function doUnTip(descrp)
{
	UnTip();	
}
var reqOrt;
var backupPlz = "";
var listOrt = [];
var listStrasse = [];
function getOrt(plz) {
	//plz = document.getElementById('plz').value;//"52076;"//$('#suggestionsOrt').value;
    if (window.XMLHttpRequest) {
		reqOrt = new XMLHttpRequest( );
    }
    else if (window.ActiveXObject) {
        reqOrt = new ActiveXObject("Microsoft.XMLHTTP");
    }
	var url = "getOrt.php?plz=" + plz;
    reqOrt.open("Get", url,true);
    reqOrt.onreadystatechange = function ()
	{
		if (reqOrt.readyState==4) {
			if (reqOrt.status == 200) {
					suggestOrt();
			}
		}
	};
    reqOrt.send(null);
}
function suggestOrt()
{
	var resp = reqOrt.responseText;
	//var str = "listOrt = [" + resp + "]";
	var str = "listOrt = [" + resp + "]";
	//eval(str);
	if(ortSuggestionObj == null)
	{
		try
		{
			ortSuggestionObj = new Suggest.Local("ort", "suggestOrt", eval(str), {ignoreCase: true, prefix: true, highlight: true});
		}
		catch(err)
		{
			alert(DB_ERR); 
		}
	}else
	{
		ortSuggestionObj.candidateList = eval(str);
	}
	//alert(resp);
}
var reqStrasse;
function getStrasse(plz) {
	//plz = document.getElementById('plz').value;//"52076;"//$('#suggestionsStrasse').value;
    if (window.XMLHttpRequest) {
		reqStrasse = new XMLHttpRequest( );
    }
    else if (window.ActiveXObject) {
        reqStrasse = new ActiveXObject("Microsoft.XMLHTTP");
    }
	var url = "getStrasse.php?plz=" + plz;
    reqStrasse.open("Get", url,true);
    reqStrasse.onreadystatechange = function ()
	{
		if (reqStrasse.readyState==4) {
			if (reqStrasse.status == 200) {
					suggestStrasse();
			}
		}
	};
    reqStrasse.send(null);
}
function suggestStrasse()
{
	var resp = reqStrasse.responseText;
	//var str = "listStrasse = [" + resp + "]";
	var str = "[" + resp + "]";
	//eval(str);
	
	if(strasseSuggestionObj == null)
	{
		try
		{
			strasseSuggestionObj = new Suggest.Local("strasse", "suggestStrasse", eval(str), {ignoreCase: true, prefix: true, highlight: true});
		}
		catch(err)
		{
			alert(DB_ERR);
			/*if(err.message == "Syntax error")
			{
				alert("Database error!");
			}
			else
			{
				alert(err.message);
			}*/
		}
	}else
	{
		strasseSuggestionObj.candidateList = eval(str);
	}
	//alert(resp);
}
var ortSuggestionObj = null;
var strasseSuggestionObj = null;
function getOrtAndStrasse(plzValue)
{
	if(plzValue != null && plzValue != "undefined" && trim(plzValue) != "" && trim(plzValue) != backupPlz)
	{
		plzValue = trim(plzValue);
		backupPlz = plzValue;
		getOrt(plzValue);
		getStrasse(plzValue);
	}
}
function getOrtAndStrasse2()
{
	var plzValue = trim(document.getElementById("plz").value);
	if(trim(backupPlz) == "" && plzValue != "")
	{
		backupPlz = plzValue;
		getOrt(plzValue);
		getStrasse(plzValue);
	}
}
function lastCheck()
{
	if(!checkMonAn())
	{
		return false;
	}
	if(!performCheck('order', rules, 'classic'))
	{
		return false;
	}
	return true;
}
var registerRequest = null;
function doSubmit()
{
	getAmount(); getExtra();
	if(lastCheck())
	{
		registerRequest = createRequest();
		if (registerRequest == null) {
			alert(AJAX_ERR);
		} else {
			document.order.button2.disabled = true;  //disable bestellen btn
			var sex = document.forms[0].sex1.checked == true ? "Herr" : "Frau";
			var url = "order.php";
			var requestData = "firma=" + escape(document.getElementById("firma").value)
			+ "&abteilung=" + escape(document.getElementById("abteilung").value)
			+ "&vorname=" + escape(document.getElementById("vorname").value)
			+ "&nachname=" + escape(document.getElementById("nachname").value)
			+ "&plz=" + escape(document.getElementById("plz").value)
			+ "&ort=" + escape(document.getElementById("ort").value)
			+ "&strasse=" + escape(document.getElementById("strasse").value)
			+ "&nr=" + escape(document.getElementById("nr").value)
			+ "&vorwahl=" + escape(document.getElementById("vorwahl").value)
			+ "&telNr=" + escape(document.getElementById("telNr").value)
			+ "&email=" + escape(document.getElementById("email").value)
			+ "&sex=" + escape(sex)
			+ "&email=" + escape(document.getElementById("email").value)
			+ "&hinterhof=" + escape(document.getElementById("hinterhof").value)
			+ "&besonder=" + escape(document.getElementById("besonder").value)
			+ "&foodIdList=" + escape(document.getElementById("foodIdList").value)
			+ "&amountList=" + escape(document.getElementById("amountList").value)
			+ "&extraList=" + escape(document.getElementById("extraList").value);
			//alert(requestData);
			registerRequest.onreadystatechange = registrationProcessed;
			registerRequest.open("POST", url, true);
			registerRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			registerRequest.setRequestHeader("Content-length", params.length);
			registerRequest.setRequestHeader("Connection", "close");
			registerRequest.send(requestData);
		}
	}else{
		//alert("false last check");
	}
}
function createRequest() {
	var request = null;
	try {
	    request = new XMLHttpRequest();
	} catch (tryMS) {
	    try {
	      request = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (otherMS) {
	      try {
	        request = new ActiveXObject("Microsoft.XMLHTTP");
	      } catch (failed) {
	        request = null;
	      }
	    }
	}
	return request;
}
function registrationProcessed() {
	if(registerRequest.readyState == 4)
	{
		if(registerRequest.status == 200)
		{
			//alert(registerRequest.responseText);
			if(trim(registerRequest.responseText) == "OK")
			{
				document.order.button2.disabled = false;  //enable bestellen btn
				alert(FINISH_ORDER_SUCC);
				resetAll();
			} else {
				document.order.button2.disabled = false;  //enable bestellen btn
				alert(FAIL_ORDER);
			}
		}
		else{
			document.order.button2.disabled = false;  //enable bestellen btn
			alert(FAIL_ORDER);
		}
	}
}
function resetAll()
{
	isEnoughMindest = false;
	
	document.getElementById("firma").value = "";
	document.getElementById("abteilung").value = "";
	document.getElementById("vorname").value = "";
	document.getElementById("nachname").value = "";
	document.getElementById("plz").value = "";
	document.getElementById("ort").value = "";
	document.getElementById("strasse").value = "";
	document.getElementById("nr").value = "";
	document.getElementById("vorwahl").value = "";
	document.getElementById("telNr").value = "";
	document.getElementById("email").value = "";
	document.forms[0].sex1.checked = true;
	document.getElementById("email").value = "";
	document.getElementById("hinterhof").value = "";
	document.getElementById("besonder").value = "";
	document.getElementById("foodIdList").value = "";
	document.getElementById("amountList").value = "";
	document.getElementById("extraList").value = "";
	
	var tblCartList = document.getElementById('cartList');
	while(tblCartList.rows.length > 1)
	{
		tblCartList.deleteRow(1);
	}
	sum();
}
