function validateOrder() {	
	//validate the input
	if (!validateOrderInput()) return;
	//everything seems to be in order
	document.order_form.submit();
}

function updatePrices() {
	var totalBooks = 0;
	var totalPrice = 0;
	//books
	for (var i = 1; i < 6; i++) {
		//get current correct quantity
		var qbInt = 0;
		var qb = document.getElementById('q_b' + i);
		var valid = isNumeric(qb);	
		if (valid) {
			qbInt = parseInt(qb.value);
		}
		//add the quantity to the total books quantity
		totalBooks += qbInt;
		//format the output
		subtotal = qbInt * 15.8; //Calculate the price (quantity * price per book)
		totalPrice += subtotal;
		var price = formatPrice(subtotal);
		//update prices
		var p = document.getElementById('tp_b' + i);
		p.innerHTML = price;
	}
	//bookbox
	{
		var qbInt = 0;
		var qb = document.getElementById('q_box');
		var valid = isNumeric(qb);	
		if (valid) {
			qbInt = parseInt(qb.value);
			qbInt = 5 * qbInt; //five books in the box!
		}
		//add the quantity to the total books quantity
		totalBooks += qbInt;
		//format the output
		subtotal = qbInt * 15.80; //Calculate the price (quantity * price per book)
		totalPrice += subtotal;
		var price = formatPrice(subtotal);
		//update prices
		var p = document.getElementById('tp_box');
		p.innerHTML = price;
	}
	//if (totalBooks != 0 && totalBooks < 5) {
	//	totalPrice += 2.2;
	//	var vp = document.getElementById('vp');
	//	vp.innerHTML = formatPrice(2.2);
	//} else if (totalBooks != 0) {
	//	var vp = document.getElementById('vp');
	//	vp.innerHTML = formatPrice(0);	
	//}
	var tp = document.getElementById('tp');
	tp.innerHTML = formatPrice(totalPrice);
	var mwst = document.getElementById('mwst');
	mwst.innerHTML = formatPrice(totalPrice * 0.07);
	
}

function formatPrice(price) {
	var cents = Math.floor((price* 100) % 100);
	if (cents < 10) {
		cents =	"0" + cents;
	}
	var euros = Math.floor(price);
	return (euros + "," + cents + " €"); 
}


function validateOrderInput() {		
	var totalBooks = 0;
	var totalPrice = 0;
	var validInput = true;	
	//books
	for (var i = 1; i < 6; i++) {
		//get current correct quantity
		var qbInt = 0;
		var qb = document.getElementById('q_b' + i);
		var isnum = isNumeric(qb);	
		if (isnum) {
			qbInt = parseInt(qb.value);
		}
		if (qb.value != "") validInput = validInput & isnum;
		//add the quantity to the total books quantity
		totalBooks += qbInt;
	}
	//bookbox
	{
		//get current correct quantity
		var qbInt = 0;
		var qb = document.getElementById('q_box');
		var isnum = isNumeric(qb);
		if (isnum) {
			qbInt = parseInt(qb.value);
		}
		if (qb.value != "") validInput = validInput & isnum;
		//add the quantity to the total books quantity
		totalBooks += qbInt * 5;	
	}
	
	if (!validInput) {
		alert("Bitte stellen sie sicher, dass sie alle farbig unterlegten Felder korrekt ausgefuellt haben.");	
		return false;
	
	}
	if (totalBooks == 0) {
		alert("Bitte wählen sie mindestens ein Buch aus.");
		return false;
	}
	
	return true;
	
}

function validateAddress() {
	//validate the input
	if (!validateAddressInput()) return;
	//TODO
	document.address_form.submit();
}

function validateAddressBack() {
	//validate the input
	if (!validateAddressInput()) return;
	document.address_form.action = "index.php?p=order&step=1";
	document.address_form.submit();
}

function validateAddressInput() {
	var valInput = true;
	//address
	var ad_firstname = document.address_form.ad_firstname;
	var ad_lastname = document.address_form.ad_lastname;
	var ad_company = document.address_form.ad_company;
	var ad_number = document.address_form.ad_number;
	var ad_plz = document.address_form.ad_plz;
	var ad_city = document.address_form.ad_city;
	var ad_street = document.address_form.ad_street;
	var ad_email = document.address_form.ad_email;
	var ad_phone = document.address_form.ad_phone;
	//postal address
	var pad_firstname = document.address_form.pad_firstname;
	var pad_lastname = document.address_form.pad_lastname;
	var pad_company = document.address_form.pad_company;
	var pad_street = document.address_form.pad_street;
	var pad_number = document.address_form.pad_number;
	var pad_plz = document.address_form.pad_plz;
	var pad_city = document.address_form.pad_city;
	var pad_street = document.address_form.pad_street;
	
	//Check elements
	valInput = valInput &
			   isNotEmpty(ad_firstname) & 
			   isNotEmpty(ad_lastname) & 
			   isNotEmpty(ad_number) & 
			   isPostal(ad_plz) & 
			   isNotEmpty(ad_city) & 
			   isNotEmpty(ad_street);
	
	var emptyPostalAddress = true;
	
	emptyPostalAddress = emptyPostalAddress & 
						 (pad_firstname.value == "") &
						 (pad_lastname.value == "") &
						 (pad_street.value == "") &
						 (pad_number.value == "") &
						 (pad_plz.value == "") &
						 (pad_city.value == "");
						
						 
	if (!emptyPostalAddress) {
		valInput = valInput &
			       isNotEmpty(pad_firstname) & 
				   isNotEmpty(pad_lastname) & 
				   isNotEmpty(pad_number) & 
				   isPostal(pad_plz) & 
				   isNotEmpty(pad_city) & 
				   isNotEmpty(pad_street);
	} else {
			pad_firstname.style.backgroundColor = "white";
			pad_lastname.style.backgroundColor = "white";
			pad_street.style.backgroundColor = "white";
			pad_number.style.backgroundColor = "white";
			pad_city.style.backgroundColor = "white";
			pad_plz.style.backgroundColor = "white";
	}
	
	if (!valInput) {
		alert ("Bitte füllen sie alle gelb markierten Bereiche aus.");	
		return false;
	}	
	
	return true;	
}

function order() {
	var agb = document.getElementById("agb");
	if (agb.checked == false) {
		//agb not accepted
		alert("Bitte akzeptieren sie die AGB, bevor sie bestellen.");
		return;
	}
	
	//Check Captcha
	var capt = document.getElementById('capt');
	if (capt.value == "") {
		capt.style.backgroundColor = "yellow";
		alert("Bitte fuellen sie die gelb markierten Bereiche aus.");
		return;		
	} else {
		capt.style.backgroundColor = "white";
	}
	
	document.confirm_form.submit();	
}

function isPostal(elem){
	var re5digit=/^\d{5}$/ //regular expression defining a 5 digit number
	if (elem.value.search(re5digit)==-1) {//if match failed
		elem.style.backgroundColor = "yellow";
		return false;
	} else {
		elem.style.backgroundColor = "white";
		return true;
	}
}


function isName(elem) {
	var charExpression = /^(([a-z]+)|([A-Z]+)|[.]+|[,]+|[-]+|[\/]+|[\s]+)+$/;
	if(elem.value.match(charExpression)){
		elem.style.backgroundColor = "white";
		return true;
	}else{
		elem.style.backgroundColor = "yellow";
		return false;
	}
}

// If the element's string matches the regular expression it is all numbers
function isNumeric(elem) {
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		elem.style.backgroundColor = "white";
		return true;
	}else{
		if (elem.value != "") elem.style.backgroundColor = "yellow";
		if (elem.value == "") elem.style.backgroundColor = "white";
		//alert(helperMsg);
		//elem.focus();
		return false;
	}
}

function isNumericAndNotEmpty(elem) {
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		elem.style.backgroundColor = "white";
		return true;
	}else{
		elem.style.backgroundColor = "yellow";		
		//alert(helperMsg);
		//elem.focus();
		return false;
	}
}

function isNotEmpty(elem) {
	if (elem.value == "") {
		elem.style.backgroundColor = "yellow";	
		return false;
	} else {
		elem.style.backgroundColor = "white";	
		return true;
	}
}

function printOrder(){  
	if (window.print) {
		window.print() ;  
	} 
}
