function checkAGB() {
	if(document.getElementById('agb')) {
		if(!document.getElementById('agb').checked) {
			alert("Bitte bestätigen Sie, das Sie unsere AGB gelesen und verstanden haben!");
			return false;
		}
		else return true;
	}
	return false;
}
function confirmOrder() {
	if(confirm('Wolle Sie die Bestellung jetzt absenden?')) {
		return true;
	}
	return false;
}
function removeProdukt(id,rubrikId) {
		//var rubrikId = document.getElementById('param_rubrik_id'+id).value;
		
    if (!http_request) {
        alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
        return false;
    }
    try {
    	http_request.open('GET', "/warenkorb/changeAmount.php?ie=" + new Date() + "&pid="+id+"&rubrik_id="+rubrikId, true);
    	http_request.onreadystatechange = msgRequest;    	
    	http_request.send(null);
    } catch(e) {top.location.href=location.href;}
}

function msgRequest() {		
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {       	
        	  warenkorb_amount = http_request.responseText;
        	  updateWarenkorbAmount();
        	  
            return true;
        } else {
            alert('Bei dem Request ist ein Problem aufgetreten [' + http_request.status + '].');
        }
    }

}

function updateProduktPreis(id,value) {		
	if(document.getElementById('produkt_preis'+id)) {
		var preisDiv = document.getElementById('produkt_preis'+id);	
		preisDiv.firstChild.data = value;
	}
}

function produktConfigRequest(id,material_preisgruppe_id,groesse_id,rubrik_id,menge,addto) {
    try {
    	http_request.open('GET', "/warenkorb/changeProduktConfig.php?ie=" + new Date() + "&pid="+id+"&material_preisgruppe_id="+material_preisgruppe_id+"&groesse_id="+groesse_id+"&rubrik_id="+rubrik_id+"&menge="+menge+"&addto="+addto, true);
    	http_request.onreadystatechange = msg_produktConfigRequest;    	
    	http_request.send(null);
    } catch(e) {top.location.href=location.href;}
}

function msg_produktConfigRequest() {		
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
        		responseText = http_request.responseText.replace(/\s/g,"");
						if(responseText != "") {
							var pdata = responseText.split(";");							
							var warenkorbCount = pdata[0].replace(/\s/g,"");
		        		
		        		// Wenn das erste Produkt in den Warenkorb gelegt wird,
		        		// gibt es einen Hinweis-Text
		        		if(warenkorb_amount == 0 && warenkorbCount == 1) {
									alert('Das Produkt wurde in den Warenkorb gelegt.');        	
								}
								
		        	  warenkorb_amount = warenkorbCount;
		        	  updateWarenkorbAmount();							
		        	  
							var id = pdata[1].replace(/\s/g,"");
							//var preis = pdata[3].replace(/\s/g,"");
							var preis = pdata[8].replace(/\s/g,"");
							var rubrikId = pdata[4].replace(/\s/g,"");

							var produktWarenkorbId = id+"_"+rubrikId;							
							
							updateProduktPreis(produktWarenkorbId,preis);
            	return true;
            }
        } else {
            alert('Bei dem Request ist ein Problem aufgetreten [' + http_request.status + '].');
        }
    }
   return false;
}

function setProduktConfig(id,rubrik_id) {
	var rubrikId = "";
	if(!rubrik_id)
		rubrikId = document.getElementById('param_rubrik_id'+id).value;
	else 
		rubrikId = rubrik_id;
		
	var produktWarenkorbId = id+"_"+rubrikId;
	var comboMaterial = document.getElementById('param_shop_material'+produktWarenkorbId);
	var menge = 1;
	var groesseId = -1;
	var rbox = null;
	
	if(document.getElementById('param_shop_menge'+produktWarenkorbId)) {
		menge = document.getElementById('param_shop_menge'+produktWarenkorbId).value;
	}
	
	if(document.getElementById('param_shop_groesse'+produktWarenkorbId)) {
		//rbox = document.getElementById('param_shop_groesse'+id);
		eval('var rbox = document.form_shop.param_shop_groesse'+produktWarenkorbId);
	
		if(rbox.length) {
			for(idx=0;idx<rbox.length;idx++) {			
					if(rbox[idx]) {
						if(rbox[idx].checked) {
							groesseId = rbox[idx].value;
							break;
						}
					}
					else
						break;
			}
		}
		else {
			groesseId = rbox.value;
		}
	}	
		
	if(groesseId != -1) {
		var addto=0;
		if(document.getElementById('merken'+id).checked) {
			addto=1;
		}				
		produktConfigRequest(id,comboMaterial[comboMaterial.selectedIndex].value,
												 groesseId,rubrikId,menge,addto);
	}	
}

function updateWarenkorbAmount() {
	var amountDiv = document.getElementById('warenkorb_amount');	
	amountDiv.firstChild.data = warenkorb_amount;

		if(warenkorb_amount > 0) {
			document.getElementById('awarenkorb').className='topmenu_selected';		
		}
		else
			document.getElementById('awarenkorb').className='topmenu';			
}
function wo(dateiname, winname, top, left, width, height, scrolling) {
   w = window.open(dateiname,winname,'top='+top+',left='+left+',alwaysRaised=no,location=no,toolbar=no,directories=no,status=no,menubar=no,scrollbars='+scrolling+',resizable=yes,width='+width+',height='+height);
   w.focus();
}
function printWarenkorb() {
	wo("/print/warenkorb.php","kbl_print",20,20,640,720,"yes");
}
function openServiceWindow() {
	wo("/kunde/","kbl_service",20,20,640,720,"yes");
}
function findProduct() {
	var material 		= document.getElementById('select_material');
	material = material[material.selectedIndex].value;
	var schnittform = document.getElementById('select_schnittform');	
	schnittform = schnittform[schnittform.selectedIndex].value;
	
		if(material != 0 || schnittform != 0) {
			document.getElementById('material').value = material;
			document.getElementById('schnittform').value = schnittform;
			document.getElementById('search').action = urlFinden;
			document.getElementById('search').submit();			
		}
}
function searchProduct() {
	var tagValue = document.getElementById('search_value').value;	
	var searchParams = "";	
		if(tagValue) {			
			if(urlParams && urlParams.length > 1)
				searchParams = urlParams;							
			document.getElementById('search').action = urlSearch + tagValue.toLowerCase() + searchParams;
			document.getElementById('search').submit();			
		}
}

var logincredits=false;
function login() {
	document.getElementById('usr').value = document.getElementById('user').value;
	document.getElementById('pwd').value = document.getElementById('pass').value;
	
	var user = document.getElementById('usr').value;
	var pass = document.getElementById('pwd').value;
	
		if(user != "" && pass != ""  && logincredits != false) {
			document.getElementById('authenticate').action = urlLogin;
			document.getElementById('authenticate').submit();
		}
		else {
			//document.getElementById('authenticate').action = urlAnmeldung;
			//document.getElementById('authenticate').submit();
			location.href=urlAnmeldung;			
		}
}
function logout() {
			document.getElementById('authenticate').action = urlLogout;
			document.getElementById('authenticate').submit();	
}
function focusLogin() {
	if(document.getElementById('login')) {
	}
}
function setPage(num) {	
	document.getElementById('page').value = num;	
	document.getElementById('search').action = location.href;
	document.getElementById('search').submit();	
}
function setRowsPerPage(rows) {
	if(!rows || rows < 0) rows=0;
	else document.getElementById('param_rows_per_page').value = rows;	
	setPage(1);	
}
function refresh() {
	top.location.href = location.href;
}

function checkEMail(string) {
	if(string.length > 0)
		return (string.indexOf("@") > 0);
	else
		return false;
}

// TYPEO3: decrypt helper function
function decryptCharcode(n,start,end,offset)	{
	n = n + offset;
	if (offset > 0 && n > end)	{
		n = start + (n - end - 1);
	} else if (offset < 0 && n < start)	{
		n = end - (start - n - 1);
	}
	return String.fromCharCode(n);
}
// TYPEO3: decrypt string
function decryptString(enc,offset)	{
	var dec = "";
	var len = enc.length;
	for(var i=0; i < len; i++)	{
		var n = enc.charCodeAt(i);
		if (n >= 0x2B && n <= 0x3A)	{
			dec += decryptCharcode(n,0x2B,0x3A,offset);	// 0-9 . , - + / :
		} else if (n >= 0x40 && n <= 0x5A)	{
			dec += decryptCharcode(n,0x40,0x5A,offset);	// A-Z @
		} else if (n >= 0x61 && n <= 0x7A)	{
			dec += decryptCharcode(n,0x61,0x7A,offset);	// a-z
		} else {
			dec += enc.charAt(i);
		}
	}
	return dec;
}
// TYPEO3: decrypt spam-protected emails
function linkTo_UnCryptMailto(s)	{
	location.href = decryptString(s,-2);
}