
isIE6 = RegExp('msie\\s(5\\.[5-9]|[6]\\.[0-9]*).*(win)','i').test(navigator.userAgent);
if(isIE6){
	try {
	  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}
onLoad = false;
function objCart(){
	this.element = document.getElementById('cart');
	this.btn = document.getElementById('btnOpenCart');
	//this.container = document.getElementById('container');
	this.ul = document.getElementById('cartUl');
	this.totalPrice = document.getElementById('cartTotalPrice');
	this.cartInfoNr = document.getElementById('cartInfoNr');
	this.cartInfoPrice = document.getElementById('cartInfoPrice');
	//this.en = document.getElementById('en');
	this.empty = document.getElementById('cartEmpty');
	this.delivery = document.getElementById('delivery');
	this.on;
	this.state;
	this.pds = new Array();
	this.pdsPrice = new Array();
	this.pdsQuantity = new Array();
	this.pdsTotalPrice = new Array();
	this.pdsQuantityInt = new Array();
	this.totalPriceInt = 0;
	this.totalQuantity = 0;
	
	this.getCookieOn = function(){
		var a = getCookie('cartOn');
		if(a == ''){
			//setCookie('cartOn',1);
			a = '1';
		}
		if(a == '1'){
			return true;
		}else{
			return false;
		}
	}
	this.getCookieState = function(){
		var a = getCookie('pds');
		if(a == '' || a == '0')
			return 'leeg';
		else
			return 'vol';
	}
	this.hide = function(){
		this.element.style.display = "none";
		this.on = false;
		this.btn.style.display = "inline";
		setCookie('cartOn',0);
		//this.container.style.margin = "0 auto 0 auto";
	}
	this.show = function(){
		/*if(this.state == 'vol'){
			this.on = true;
			window.onresize();
			this.element.style.display = "block";
			this.btn.style.display = "none";
			setCookie('cartOn',1);
		}*/
	}
	this.updatePos = function(w,h){
		var wcontainer = 893;//724
		
		
		var x = w/2+wcontainer/2;
		if(w > x + 167 + 20){//default
			if(!isIE6)
				this.element.style.position = "fixed";
			this.element.style.left = x+"px";
			//this.container.style.margin = "0 auto 0 auto";
			//this.element.style.display = "block";
			this.state = 'vol';
			
		}else if(w > wcontainer-7+167){//container to the left
			if(!isIE6)
				this.element.style.position = "fixed";
			var xx = (w-wcontainer-167)/2;
			this.element.style.left = (xx+wcontainer)+"px";
			//this.container.style.margin = "0 0 10px "+xx+"px";
			//this.element.style.display = "block";
			this.state = 'vol';
			
		}else{//dunno
			/*this.element.style.display = "none";
			this.state = 'noRoom';
			this.container.style.margin = "10px auto 0 auto";*/
			/*this.container.style.margin = "10px 500px 0 0";
			this.element.style.left = "727px";
			this.element.style.display = "block";*/
			//this.container.style.margin = "0 0 0 5px";
			this.element.style.position = "absolute";
			this.element.style.left = (5+wcontainer-3+3)+"px";
			//this.element.style.display = "block";
		}
		this.updatePosVertical(w,h,true);
	}
	this.updatePosVertical = function(w,h,force){
		//79 = default height cart without items, every item = 14 pixels
		//alert(this.element.style.top);
		var elements = this.ul.getElementsByTagName('li').length - 1;
		var height = 79 + 14 * elements;
		//alert(h + '   ' + height);
		if(height >= h){
			this.element.style.top = '0';
		}else if(force){
			var defaultTop = 200;//253
			if(defaultTop+height+20 < h)
				this.element.style.top = defaultTop+'px'
			else
				this.element.style.top = h/2-height/2+'px';
		}
	}
	this.pdAdd = function(pds,id,name,price,quantity){
		if(this.state == 'leeg'){
			this.state = 'vol';
			this.show();
		}
		if(!this.pds[pds]){
			this.pdNew(pds,id,name,price,quantity);
		}else{
			this.pdUpdate(pds,price,quantity);
		}
		
		this.cartInfoPrice.style.display = 'inline';
		this.cartInfoPrice.innerHTML = '&euro; '+priceFormat(this.totalPriceInt);
		this.pdsQuantityInt[pds] = parseInt(quantity);
		this.totalQuantity += this.pdsQuantityInt[pds];
		var l = this.totalQuantity;
		this.cartInfoNr.innerHTML = l;
		
		if(l == 0){
			this.empty.style.display = 'list-item';
			this.delivery.style.display = 'none';
			this.cartInfoPrice.style.display = 'none';
		}else{
			this.empty.style.display = 'none';
			this.delivery.style.display = 'list-item';
		}
		if(this.pdsQuantityInt[pds] == 0){
			this.pds[pds].parentNode.removeChild(this.pds[pds]);
			pdRemove(pds);
		}
	}
	this.pdNew = function(pds,id,name,price,quantity){
		price = parseInt(price)*parseInt(quantity);
		var li = document.createElement('li');
		var span = document.createElement('span');
		var spanQuantity = document.createElement('span');
		var spanName = document.createElement('span');
		
		span.className = 'price';
		span.innerHTML = '&euro; '+priceFormat(price);
		spanQuantity.innerHTML = quantity;
		spanName.innerHTML = 'x '+name;
		
		this.pds[pds] = this.ul.appendChild(li);
		this.pdsQuantity[pds] = this.pds[pds].appendChild(spanQuantity);
		this.pds[pds].appendChild(spanName);
		//this.pds[pds].innerHTML += 'x '+name;
		this.pdsPrice[pds] = this.pds[pds].appendChild(span);
		//this.pdsQuantity[pds].innerHTML = quantity.toString();
		this.pdsTotalPrice[pds] = price;
		this.totalPriceInt += price;
		this.totalPrice.innerHTML = priceFormat(this.totalPriceInt);
		
	}
	this.pdUpdate = function(pds,price,quantity){
		price = parseInt(price)*parseInt(quantity);
		this.totalPriceInt += price - this.pdsTotalPrice[pds];
		this.pdsTotalPrice[pds] = price;
		this.pdsQuantity[pds].innerHTML = quantity;
		this.pdsPrice[pds].innerHTML = '&euro; '+priceFormat(price);
		this.totalPrice.innerHTML = priceFormat(this.totalPriceInt);
		this.totalQuantity -= this.pdsQuantityInt[pds];
	}
	this.constructor = function(){
		this.on = this.getCookieOn();
		this.state = this.getCookieState();
		
		if(!this.on)
			this.btn.style.display = "inline";
		
		if(this.state != 'leeg'){
			var val, ar, l = getCookie('pds');
			for(var i = 0;i<l;i++){
				val = getCookie('cartSlot'+i)
				if(val != '' && val != 'empty'){
					//alert(val);
					ar = val.split('&');
					this.pdAdd(i, ar[0],unescape(ar[1]),ar[2],ar[3]);
				}	
			}
		}
		if(this.pds.length == 0){
			this.empty.style.display = 'list-item';
			this.delivery.style.display = 'none';
		}else{
			this.empty.style.display = 'none';
			this.delivery.style.display = 'list-item';
		}
			
		if(isIE6){
			this.element.style.position = 'absolute';
			/*window.onscroll = function(){
				//document.getElementById('cartTotalPrice').innerHTML = document.body.scrollTop;
				cart.element.style.display = 'none';
				cart.element.style.display = 'block';
			}*/
		}
	}
	this.constructor();
}

function clearDef(dit){
	if(dit.value == 'Zoeken...')
		dit.value = '';
}
var cart;
function cartIni(){
	
	cart = new objCart();
	
}

function getDetails(id){
	var s = '', e;
	for(var i = 0;i<2;i++){
		if(e=document.getElementById('pd'+id+'detail'+i)){
			if(i==1)
				s+= '_';
			s += e.options[e.selectedIndex].value;
		}
	}
	return s;
}

function pdAdd(id,name,price){
	var inputBox = document.getElementById('quantity'+id);
	if(inputBox == null)
		quantity = 1;
	else{
		quantity = inputBox.value;
		inputBox.value = '1';
	}
	price = price.toString().replace(',','');
	var pds, val, ar, details;
	
	details = getDetails(id);
	//alert(details);
	
	pds = getCookie('pds');
	quantity = parseInt(quantity);
	if(isNaN(quantity))
		return;
	if(pds == ''){
		setCookie('pds',1);
		pds = 0;
	}else{
		pds = parseInt(pds,10);
	}
	for(var i = 0;i<pds;i++){
		val = getCookie('cartSlot'+i)
		if(val != ''){
			//alert();
			ar = val.split('&');
			if(ar[0] == id && ar[4] == details){//miss escape(details)
				quantity += parseInt(ar[3]);
				pds = i;
				val = 'found';
				break;
			}
		}	
	}
	if(val != 'found')
		if(typeof(pds) == 'number'){
			setCookie('pds',pds+1);
		}else return;
		
	if(quantity < 0)
		quantity = 0;
	setCookie('cartSlot'+pds,id+"&"+escape(name)+"&"+price+"&"+quantity+"&"+details);
	cart.pdAdd(pds,id,name,price,quantity);
	//alert(getCookie('cartSlot'+pds));
}
function pdRemove(pds){
	setCookie('cartSlot'+pds,'empty');
}
function pdEditQty(pds,qty){
	var c = getCookie('cartSlot'+pds);
	if(c != '' && c != 'empty'){
		ar = c.split('&');
		if(ar.length == 5){
			//alert(ar[0]+"&"+ar[1]+"&"+ar[2]+"&"+qty);
			setCookie('cartSlot'+pds,ar[0]+"&"+ar[1]+"&"+ar[2]+"&"+qty+"&"+ar[4]);
		}
	}
}
function pdEditDet(pds,det){
	var c = getCookie('cartSlot'+pds);
	if(c != '' && c != 'empty'){
		ar = c.split('&');
		if(ar.length == 5){
			//alert(ar[0]+"&"+ar[1]+"&"+ar[2]+"&"+qty);
			setCookie('cartSlot'+pds,ar[0]+"&"+ar[1]+"&"+ar[2]+"&"+ar[3]+"&"+det);
		}
	}
}
function getCookie(c_name)
{
	var c_start,c_end;
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return ""
}
function setCookie(c_name,value)
{
	document.cookie=c_name+ "=" +escape(value)
}
/*function priceFormat(str){
	if(typeof(str) != 'string')
		str = str.toString()
	if(str.length == 0)
		str = '0';
	str = str.replace('.',',');
	var ix = str.indexOf(',');
	if(ix == -1){
		str += ',';
		ix = str.length - 1;
	}
	var keer = 2 - (str.length - ix - 1);
	for(var i = 0;i<keer;i++)
		str += '0';
	str = str.substring(0,ix+3);
	return str;
}*/
function priceFormat(str){
	if(typeof(str) != 'string')
		str = str.toString()
	var keer = 3 - str.length;
	for(var i = 0;i<keer;i++)
		str = '0'+str;
	var l = str.length;
	str = str.substr(0,l-2) + ',' + str.substr(l-2);
	return str;
}
function roundDec(x,y){
	y = Math.pow(10,y);
	return Math.round(x*y)/y;
}
