
var shop;
var MWST_VAL = 0.19;

function Product(num,price,count) {
	this.num = num;
	this.price = price;
	this.count = count;
	
	this.setCount = function(c) {
		this.count = c;
	}
}

function Cart() {
	this.products = new Array();
	this.cur = 0;
	
	this.inCart = function(n) {
		for(var i = 0; i < this.products.length; i++)
			if(this.products[i].num == n)
				return this.products[i];
		return null;
	}
	
	this.nextProduct = function() {
		if(this.cur < this.products.length) {
			var tmp = this.cur;
			this.cur++;
			return this.products[tmp];
		} else {
			this.cur = 0;
			return null;
		}
	}
	
	this.isEmpty = function() {
		return (this.products.length == 0);
	}
	
	this.add = function(p) {
		this.products.push(p);
	}
	
	this.del = function(p) {
		for(var i = 0; i < this.products.length; i++) {
			if(this.products[i].num == p.num) {
				this.products.splice(i,1);
				break;
			}
		}
	}
	
	this.update = function(num) {
		var count = parseInt(document.getElementById("shop_count_"+num).value);
		if(isNaN(count)) {
			alert("Bitte geben Sie eine gueltige Zahl ein!");
		} else {
			var p = this.inCart(num);
			if(p != null) {
				if(count == 0)
					this.del(p);
				else
					p.setCount(count);
			} else if(count > 0) {
				var price = parseFloat(document.getElementById("shop_price_"+num).value);
				this.add(new Product(num,price,count));
			}
			shop.update();
		}
	}
}

function Shop() {
	this.sum_netto  = document.getElementById("shop_sum_netto");
	this.mwst       = document.getElementById("shop_mwst");
	this.sum_brutto = document.getElementById("shop_sum_brutto");
	
	this.form = document.getElementById("shop_form");
	
	this.cart = new Cart();
	
	this.update = function() {
		var p = null;
		var price = 0;
		while(p = this.cart.nextProduct())
			price += p.price * p.count;
			
		this.sum_brutto.value = this.formatPrice(price);
		this.mwst.value = this.formatPrice(price * MWST_VAL);
		this.sum_netto.value = this.formatPrice(price * (1 - MWST_VAL));
	}
	
	this.formatPrice = function(price) {
		var price = String(price);	
		var part = price.split(".");
		if(part.length == 2) {
			if(part[1].length >= 2)
				return part[0]+","+part[1].substr(0,2);
			if(part[1].length == 1)
				return part[0]+","+part[1]+"0";
			else
				return part[0]+",00";
		} else {
			return part+",00";
		}
	}
	
	this.check = function() {
		var check = true;
		var msg = null;
		var valid_mail = /^[a-z0-9-_\.]+@[a-z0-9_-]+\.+[a-z]{2,4}$/i;
		
		var re_name = document.getElementById("shop_re_name");
		var re_adr = document.getElementById("shop_re_adr");
		var re_zip = document.getElementById("shop_re_zip");
		var re_city = document.getElementById("shop_re_city");
		var re_email = document.getElementById("shop_re_email");
		var re_phone = document.getElementById("shop_re_phone");
		
		var num = 0;
		while(count_f = document.getElementById("shop_count_"+num)) {
			this.cart.update(num);
			num++;
		}
		
		if(this.cart.isEmpty()) {
			check = false;
			msg = "Sie haben keinen Artikel ausgewaehlt!";
		}
		
		if(check && re_name.value.replace(" ","") == "") {
			check = false;
			msg = "Bitte geben Sie einen Namen an!";
			re_name.focus();
		}
		
		if(check && re_adr.value.replace(" ","") == "") {
			check = false;
			msg = "Bitte geben Sie eine Strasse an!";
			re_adr.focus();
		}
		
		if(check && re_zip.value.replace(" ","") == "") {
			check = false;
			msg = "Bitte geben Sie eine Postleitzahl an!";
			re_zip.focus();
		}
		
		if(check && re_city.value.replace(" ","") == "") {
			check = false;
			msg = "Bitte geben Sie einen Ort an!";
			re_city.focus();
		}
		
		if(check && !valid_mail.test(re_email.value)) {
			check = false;
			msg = "Bitte geben Sie eine gueltige Emailadresse an!";
			re_email.focus();
		}
		
		if(check && re_phone.value.replace(" ","") == "") {
			check = false;
			msg = "Bitte geben Sie eine Telefonnummer an!";
			re_phone.focus();
		}
		
		if(msg)
			alert(msg);
		return check;
	}
	
}

//setOnChange(elem,num)
//@param elem input element to add listener to
//@param num  int   number in shoppinglist
//Adds eventlistener to element elem with number num
//
function setOnChange(elem,num) {
	elem.onchange = function()  { shop.cart.update(num); }
}

//initShop()
//Adds eventlistener to inputfields and initializes cart
//
function initShop() {
	shop = new Shop();
	var count_f = null;
	var num = 0;
	while(count_f = document.getElementById("shop_count_"+num)) {
		setOnChange(count_f,num);
		num++;
	}	
	shop.form.onsubmit = function() {return shop.check()};
	shop.form.style.display = "block";
}

// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}



addLoadEvent(initShop);	// run initShop onLoad