var product_popup_id = "";
var gepast_switch = "";

window.onload = function () {
	resizeWindows();

	var navi = navigator.appVersion;
	if(navi.indexOf("MSIE")>-1){

    	var navAr = navi.split(";"); 
    	if(parseFloat(navAr[1].split(" ")[2])<7) {	
    		var oldIEversion = true;
    	}
	}

    if (oldIEversion) {
	    var container = document.getElementById("sitecontainer");
		container.onscroll = function () {
			if(document.getElementById("linker_menu")) {
				var leftmenu = document.getElementById("linker_menu");
				leftmenu.style.top = container.scrollTop;
			}
			if (document.getElementById("besteloverzicht")) {
				var besteloverzicht = document.getElementById("besteloverzicht");
				besteloverzicht.style.top = container.scrollTop;
			}
		}
    }
    
}

window.onresize = function () {
	resizeWindows();
}


function CreateBookmarkLink(url, title) {

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if ( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); 
	} else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

function resizeWindows() {
	var container = document.getElementById("sitecontainer");
	
	// Browser hoogte en breedte bepalen
	if (window.innerWidth) {
		var browser_width		= window.innerWidth;
		var browser_height		= window.innerHeight;
	} else {
		var browser_width		= document.body.clientWidth;
		var browser_height		= document.body.clientHeight;
		document.getElementById("keuken").style.height = "19px";
		document.getElementById("restaurant").style.height = "19px";
	}
	
	var container_height = browser_height - document.getElementById('top').offsetHeight;
	container.style.height = container_height + "px";
}

function postcodeFocus () {
	var elmt = document.getElementById("postcode");
	postcode = elmt.value;
	elmt.value = "";
}

function postcodeListener (e) {
	var keynum;
	
	if (window.event) {		// IE
		keynum = e.keyCode;
	} else {				// Netscape / FF / Opera
		keynum = e.which;
	}

	if (keynum == 13) {
		// Enter
		postcodeChange();
	}
}

function postcodeChange () {
	var elmt = document.getElementById("postcode");
	if(is_postcode(elmt.value)) {
		postcode = formatPostcode(elmt.value);
		location.href = "/site.php?postcode=" + postcode;
	} else {
		elmt.value = postcode;
	}
}

function formatPostcode(postcode) {
	postcode = postcode.replace(" ", "");
	//postcode = postcode.replace (postcode[4], postcode[4].toUpperCase());
	//postcode = postcode.replace (postcode[5], postcode[5].toUpperCase());
	postcode = postcode.toUpperCase();
	return postcode;
}

function is_postcode(postcode) {
	if (postcode.match(/^[0-9]{4}[\ ]{0,1}([a-zA-Z]{2}){0,1}$/)) {
		return true;
	} else {
		return false;
	}
}


function is_price(prijs) {
	if (prijs.match(/^[0-9]{1,}(,[0-9]{2})?$/)) {
		return true;
	} else {
		return false;
	}
}

function laadKeuken(id) {
	location.href = "/site.php?keuken=" + id;
}

function keukenChange() {
	var elmt = document.getElementById("keuken");
	laadKeuken(elmt.value);
}

function restaurantChange() {
	var elmt = document.getElementById("restaurant");
	laadRestaurant(elmt.value);
}

function laadRestaurant(id) {
	location.href = "/site.php?restaurant=" + id;
}

function order(product_id) {
	if (product_popup_id !== "") {
		close_popup();
	}
	var url = "/ajax.php?ajax=order&id=" + product_id;
	product_popup_id = product_id;
	ajax (url, "orderReturn()");
}

function orderReturn() {
	var value = xmlHttp.responseText;
	if (value == "0") {
		addToCart();
		product_popup_id = "";
	} else {
		showSuboptions ();
	}
}

function close_popup () {
	if (product_popup_id !== "") {
		var div = document.getElementById("popup_" + product_popup_id);
		document.body.removeChild(div);
		product_popup_id = "";
	}
}

function showSuboptions () {
	var snippet		= xmlHttp.responseText;
	snippet = snippet.replace("PRODUCT_ID", "popup_" + product_popup_id);

	var div = document.createElement("div");
	div.style.position = "absolute";
	div.style.width = "487px";
	div.id = "popup_" + product_popup_id;
	div.style.left = "50%";
	div.style.marginLeft = "-258px";
	div.style.top = "200px";
	div.style.backgroundImage = "url(/img/gui/submenu.png)";
	div.style.borderStyle = "solid";
	div.style.borderWidth = "1px";
	div.style.borderColor = "#000000"; //"#BEDACB";
	div.style.padding = "1px";
	div.style.overflow = "hidden";
	div.innerHTML = snippet;
	div.style.overflow = "auto";
	
	document.body.appendChild(div);
	document.getElementById(div.id).className = 'optie_snippet';
}

function addToCart() {
	var url = "/ajax.php?ajax=toCart&product_id=" + product_popup_id;
	product_popup_id = "";
	ajax(url, "herladen()");
}

function subopties_addToCart() {
	var selectboxes = document.getElementById("selectboxes").value;
	var checkboxes = document.getElementById("checkboxes").value;
	var optieString = "";
	
	// Eerst de checkboxes verwerken
	var activeOption = "";
	if (checkboxes !== "") {
		checkboxes = checkboxes.split("||");
		for (i = 0; i < checkboxes.length; i ++) {
			var id = checkboxes[i];
			var elmt = document.getElementById(id);
			if (elmt.checked == true) {
				var oneChecked = true;
				var string = elmt.value.split("|");
				var naam = Url.encode(string[0]);
				var prijs = Url.encode(string[1]);
				var optie = Url.encode(string[2]);
				
				if (optie !== activeOption) {
					activeOption = optie;
					if (optieString !== "") {
						optieString += "</option>";
					}
					optieString += "<option name='" + optie + "'>";
				}
				optieString += "<value prijs='" + prijs + "'>" + naam + "</value>";
			}
		}
		if (oneChecked) {
			optieString += "</option>";
		}
	}
	
	// De selectboxes verwerken
	if (selectboxes !== "") {
		selectboxes = selectboxes.split("||");
		for (i = 0; i < selectboxes.length; i ++) {
			var id = selectboxes[i];
			var elmt = document.getElementById(id);
			var string = elmt.value.split("|");
			var naam = Url.encode(string[0]);
			var prijs = Url.encode(string[1]);
			var optie = Url.encode(string[2]);
			
			optieString += "<option name='" + optie + "'><value prijs='" + prijs + "'>" + naam + "</value></option>";
		}
	}
	
	url = "/ajax.php?ajax=subToCart&product_id=" + product_popup_id;
	close_popup();
	ajax(url, "herladen()", "POST", "string=" + Url.encode(optieString));
}

function herladen() {
	window.location.reload();
}

function delFromCart(key) {
	url = "/ajax.php?ajax=delFromCart&key=" + key;
	ajax(url, "herladen()");
}

function bestelling_afronden() {
	var postcode = document.getElementById("postcode").value;
	location.href = "/site.php?afronden=1&postcode=" + postcode;
}

function switchBetaalwijze (betaalwijze) {
	var tr = document.getElementById("gepast_nietgepast_tr");
	if (betaalwijze == "Contant") {
		tr.style.display = "table-row";
		showNietgepast (gepast_switch);
	} else {
		tr.style.display = "none";
		showNietgepast (false);
	}
}

function showNietgepast (state) {
	var tr = document.getElementById("nietgepast_tr");
	if (state == true) {
		//tr.style.display = 'table-row';
		tr.style.display = '';
	} else {
		tr.style.display = 'none';
	}
	
}

function karretjeRij(tabel, id, naam, aantal, prijs, string) {
	var tr = document.createElement("tr");
	
	// Aantal toevoegen
	var td = document.createElement("td");
	td.width = 24;
	td.appendChild(document.createTextNode(aantal));
	tr.appendChild(td);
	
	// Naam toevoegen
	var td = document.createElement("td");
	if (string !== "") {
		td.onmouseover = function () {
			return escape (string);
		}
	}
	td.appendChild(document.createTextNode(naam));
	tr.appendChild(td);
	
	tabel.appendChild(tr);
}

function laadKar() {
	var text = xmlHttp.responseText;
	var tabel = document.getElementById("winkelwagen");

	/*
	if (text == "0") {
		var tr 		= document.createElement("tr");
		var td	 	= document.createElement("td");
		td.appendChild(document.createTextNode("Uw winkelwagentje is leeg"));
		tr.appendChild(td);
		tabel.appendChild(tr);
	} else {
		alert(text);
		text = text.split("::");
		for (var i = 0; i < text.length; i ++) {
			var parts = text[i].split("|");
			var naam 	= parts[0];
			var id		= parts[1];
			var aantal	= parts[2];
			var prijs	= parts[3];
			var string	= parts[4];
			karretjeRij (tabel, id, naam, aantal, prijs, string)
		}
	}
	*/
}
