

	var units = 10; // extra dvd's gaan per "units"
	var minUnits = 20; // minimale aantal "units"
	var minPhotos = 5; // minimale aantal foto's
	var maxPhotos = 25; // maximale aantal foto's
	var photoUnits = 20;  // extra foto's gaan per photoUnits
	
	var minPrice = parseFloat(765); // startprijs
	var pricePerUnit = parseFloat(120);  // prijs per extra 'units' DVD's
	var pricePerPhotoUnit = parseFloat(25); // prijs per extra 'unit' foto's
	var inSub = false;
	var deliveries = [
	                  	{
	                  		dbname:'expres',
	                  		name : 'Express-levering, 2 werkdagen',
	                  		price : 7.99
	                  	},
	                  	{
	                  		dbname:'standaard',
	                  		name : 'Standaard-levering, 5 werkdagen',
	                  		price : 0.00
	                  	}
//	                  	{
//	                  		dbname:'high',
//	                  		name : 'highpriority,superdupersnel.. per koerier',
//	                  		price : 49.99
//	                  	}        	
	                  ]
	var orderController = '';

	checkNumber = function(value, elem, callBack){
		if (!callBack) callBack = function(){}
		value = parseInt(value);
		if (!isnumber(value)){
			$(elem.name+"_error").show();
			$(elem.name+"_error").update("Dat is geen geldig getal");
			return false;
		}else{
			elem.order[elem.name] = value;
			if (elem.id == 'ORDER_stampsAmount') {
				$(elem.name+"_error").update("");
				return true;
			}
			// check if amount is above minUnits:
			elem.value = value;
			if (value < minUnits){
				$(elem.name+"_error").show();
				$(elem.name+"_error").update("Minimale aantal DVD's/wallets/enveloppen is " + minUnits);
				return false;
			}
			// check if number is modulus of 20:
			var tmpvalue = value - minUnits;
			if (tmpvalue%units != 0){
				$(elem.name+"_error").show();
				$(elem.name+"_error").update("Het aantal extra DVD's gaat per " + units + ", bijvoorbeeld; "+minUnits+"+ (2x"+units+") =" + (minUnits + (2*units) ) +" dvd's");
				return false;
			}
			$(elem.name+"_error").update("");
		}
		if (callBack) callBack(value, elem);
	}
	
	isnumber = function(a){
		return typeof a == 'number' && isFinite(a);
	}
	
	copyVal = function(value){
		$('ORDER_walletAmount').value = value;
		$('ORDER_envelopeAmount').value = value;
		$('ORDER_stampsAmount').value = parseInt(value)*2;
		
	}
	
	setOrderPrice = function(container, price){
		container.update("");
		container.insert( getPriceRow('Photobeat', price.photoBeat) );
		container.insert( getPriceRow('Extra afbeeldingen', price.extraImages) );
		container.insert( getPriceRow('Extra DVD\'s', price.extraDVD) );
//		container.insert( getPriceRow('BTW', price.VAT) );
		container.insert( getPriceRow('Postzegels', price.stamps) );
		container.insert( getPriceRow('Bezorgkosten', price.delivery) );
		
		var total = getPriceRow('Totaal', price.total);
		total.className="priceTotal";
		container.insert(total);
	}
	
	getPriceRow = function(title, priceValue){
		var priceRow = new Element('div', {className:'priceRow'});
		var title = new Element('div', {className:'priceTitle'}).update(title);
		priceRow.insert(title);

		var value = new Element('div', {className:'priceValue'}).update("&euro; " + priceValue);
		priceRow.insert(value);
		
		return priceRow;
	}
	
	calcPrice = function(order){
		var func = function(req){
			setPrice(req, order);
		}
		var wpsRPC = new wps.rpc;
		wpsRPC.debug=true;
		wpsRPC.createCall('order', func);
		wpsRPC.call('getPrice', 'kortingscode='+$('kortingscode').getValue());		
	}
	
	setPrice = function(req, order){
		if (req.responseJSON.stampPrice) {
			stampPrice = parseFloat(req.responseJSON.stampPrice);
		}

		if (req.responseJSON.minPrice) {
			minPrice = parseFloat(req.responseJSON.minPrice);
		}
		if (req.responseJSON.pricePerUnit) {
			pricePerUnit = parseFloat(req.responseJSON.pricePerUnit);
		}
		if (req.responseJSON.pricePerPhotoUnit) {
			pricePerPhotoUnit = parseFloat(req.responseJSON.pricePerPhotoUnit);
		}

		_calcPrice(order)
	}
	
	_calcPrice = function(order){
		var price = {
						photoBeat:minPrice,
						total:minPrice,
						extraImages:0,
						extraDVD:0,
						stamps :stampPrice,
						delivery:0,
						VAT : 0
					};
		// check amount of images:
		var imgCount = order.media.length;
		if (imgCount > maxPhotos){
			// more than max amount of pictures, calculate the units;
			var diffImages = (imgCount - maxPhotos);
			var extraPhotoUnits = (diffImages/photoUnits);
			var extraPhotoUnits = Math.ceil(extraPhotoUnits);
			price.extraImages = (pricePerPhotoUnit*extraPhotoUnits);
			price.total = price.total + price.extraImages;
		}
		var dvdCount = order.dvdAmount;
		price.stamps = parseInt($('ORDER_stampsAmount').getValue()) * (stampPrice / 100);
		price.total = price.total + price.stamps;
			
		if (dvdCount > minUnits){
			var diffDVD = (dvdCount - minUnits);
			var extraDVD = (diffDVD / units);
			extraDVD = Math.ceil(extraDVD);
			price.extraDVD = (extraDVD * pricePerUnit);
			price.total = price.total + price.extraDVD;
		}
		var VAT = price.total*0.19;
		price.VAT = VAT;
		
		var delivery = order.delivery;
		deliveries.each(
			function(del){
				if (del.dbname == delivery){
					price.delivery = del.price;
					price.total = price.total + del.price;		
				} 
			}
		)
		//price.total = price.total + price.VAT;
		setOrderPrice($('priceContainer'), price);
		return price;
	}
	
	getPriceObject = function(order){
		var price = {
						photoBeat:minPrice,
						stamps :stampPrice,
						total:minPrice,
						extraImages:0,
						extraDVD:0,
						delivery:0,
						VAT : 0
					};
		// check amount of images:
		var imgCount = order.media.length;
		if (imgCount > maxPhotos){
			// more than max amount of pictures, calculate the units;
			var diffImages = (imgCount - maxPhotos);
			var extraPhotoUnits = (diffImages/photoUnits);
			var extraPhotoUnits = Math.ceil(extraPhotoUnits);
			price.extraImages = (pricePerPhotoUnit*extraPhotoUnits);
			price.total = price.total + price.extraImages;
		}
		var dvdCount = order.dvdAmount;
		if (dvdCount > minUnits){
			var diffDVD = (dvdCount - minUnits);
			var extraDVD = (diffDVD / units);
			extraDVD = Math.ceil(extraDVD);
			price.extraDVD = (extraDVD * pricePerUnit);
			price.total = price.total + price.extraDVD;
		}
		var VAT = price.total*0.19;
		price.VAT = VAT;
		
		var delivery = order.delivery;
		deliveries.each(
			function(del){
				if (del.dbname == delivery){
					price.delivery = del.price;
					price.total = price.total + del.price;		
				} 
			}
		)
		price.stamps = parseInt($('ORDER_stampsAmount').getValue()) * (stampPrice / 100);
		price.total = price.total + price.stamps;
		//price.total = price.total + price.VAT;
		setOrderPrice($('priceContainer'), price);
		return price;		
	}

	
	
	setSubNavigation = function(){
		$$('.clickable').each(
			function(node){
				var subNav = $(node.parentNode).select('.subnav')[0];
				node.observe('click', toggleSubNav);
				node.observe('mouseover', function(e){showNav(e,subNav)});
				node.observe('mouseout', function(e){hideNav(e,subNav)});
			}
		)
		$$('.subnav').each(
			function(subNav){
				var parent = $(subNav.parentNode);
				subNav.select('a').each(
					function(item){
						$(item).observe('mouseover', function(e){
							inSub = true;
							showNav(e, subNav);
						});
						$(item).observe('mouseout', 
								function(e){
									inSub = false;
									hideNav(e, subNav);
								}
							);
					}
				)
			}
		)
	}
	
	function toggleSubNav(e){
		var elem = e.element();
		var parent = elem.parentNode;
		var subNav = $(parent).select('.subnav')[0];
		if (subNav){
			if (subNav.visible()){
				_hideElem(subNav);
			}else{
				_showElem(subNav);
			}
		}
	}
	
	function showNav(e, elem){
		_showElem(elem);

		if (e.element().className=="subnavItem"){
			elem.mouseIn = true;
		}else{
			setTimeout(function(){elem.mouseIn = false;}, 250);
		}
	}

	function hideNav(e, elem){
		if (e.element().className=="clickable"){
			// set timeout, maybe user goes into subnav:
			setTimeout(function(){
				if (!inSub){
					_hideElem(elem);
				}
			}, 250);
			return;
		}
		_hideElem(elem);
	}

	_showElem = function(elem){
		elem.show();
	}
	
	_hideElem = function(elem){
		elem.hide();
	}
	
	function getWidestElement(parent){
		var width = 0;
		parent.select('.subnav a').each(
			function(elem){
				var elemWidth = ( (elem.innerHTML.length) * 8)
				if (elemWidth > width) width = elemWidth;
			}
		)
		return width;
	}	
	
	
	
	
	checkContactForm = function(){
		var ok = true;
		$('contactFormErrors').update('');
		if ($('contactName').getValue() == ''){
			$('contactFormErrors').insert("Naam is verplicht<br/>");
			ok = false;
		}
		if ($('contactAdres').getValue() == ''){
			$('contactFormErrors').insert("Email is verplicht<br/>");
			ok = false;
		}else{
			if (!is_email($('contactAdres').getValue())){
				$('contactFormErrors').insert("Ongeldig e-mail-adres.<br/>");
				ok = false;
			}
		}
		if (ok) $('contactForm').submit();
	}
	
	is_email = function(emailStr) {
		var checkTLD=1;
		var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
		var emailPat=/^(.+)@(.+)$/;
		var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]";
		var quotedUser="(\"[^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+';
		var word="(" + atom + "|" + quotedUser + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray=emailStr.match(emailPat);

		if (matchArray==null) {
			return false;
		}

		var user=matchArray[1];
		var domain=matchArray[2];

		for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
				return false;
			}
		}

		for (i=0; i<domain.length; i++) {
			if (domain.charCodeAt(i)>127) {
				return false;
			}
		}
		
		if (user.match(userPat)==null) {
			return false;
		}

		var IPArray=domain.match(ipDomainPat);
		
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					return false;
		   		}
			}
			return true;
		}
		
		 
		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;

		for (i=0;i<len;i++) {
			if (domArr[i].search(atomPat)==-1) {
				return false;
		   }
		}

		if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
			return false;
		}

		if (len<2) {
			return false;
		}
		
		return true;
	}	
