/*!
 * Proxycom Basket JavaScript Library v1.4.2
 * http://www.proxycom.nl
 *
 * Copyright 2010, Marcel Koster
 * Licensed under GPL Version 2 license.
 * http://www.proxycom.nl/license
 *
 * Date: Wed Sat Feb 24 12:33:48 2010 +0100
 */

var pageTotal = 5;
var productCount = 7;
 
function confirmmessage(message, url) {
	if(window.confirm(message) )
	{
	    location.href = url;
	}
	return false;
}

function recalcTotals(line,col) {
	var pageTotal=0;
	var lineTotal=0;
	var qty=0;
	var qtydays=0;
	var salesPrice=0;
	var qtyheight=0;
	var qtywidth=0;
	var type=0;
	for(i=0;i<productCount;i++) {
		//[$current_id,$i,$product_type,0,$current_price,0,0,0]
		type=line[i][2];
		qty=line[i][3];
		salesPrice=line[i][4]; //getSalesPrice(i, qty, line);
		qtydays=line[i][5];
		qtyheight=line[i][6];
		qtywidth=line[i][7];
		switch(type) {
			case 1: // aantal dagen, aantal producten * huur per dag
				lineTotal=qtydays*qty*salesPrice;
				break;
			case 2:	// koop: aantal maal prijs
				lineTotal=qty*salesPrice;
				break;
			case 3: // oppervlakte: h in cm, b in cm, prijs in m2
				lineTotal = ((qtyheight * qtywidth)/10000) * qty * salesPrice;
				break;
			default:
				lineTotal=qty*salesPrice;
		}
		pageTotal=pageTotal+lineTotal;
		$("#linetotal"+line[i][col]).html(addSeparatorsNF(lineTotal.toFixed(2), '.', ',', '.'));
	}
	$("#pagetotal").html(addSeparatorsNF(pageTotal.toFixed(2), '.', ',', '.'));
	return false;
}

function getSalesPrice(index, qty, line) {
	// staffels?
	return line[index][4];
}

function addSeparatorsNF(nStr, inD, outD, sep)
{
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}

function changeQty(id, addQty, field, col, line) {
	var qty = parseInt( $("#"+field+id).val() ); 
	qty=qty+addQty;
	if (!isNaN( qty ) && (qty>=0)) { 
		for(i=0;i<line.length;i++) {
			if (line[i][0]==id) {
				line[i][col]=qty;
				break;
			}
		}
		$("#"+field+id).val(qty);
	}
	recalcTotals(line,0);
	return false;
}

function setQty(id, field, col, line) {
	var qty = parseInt( $("#"+field+id).val() ); 
	if (!isNaN( qty ) && (qty>=0)) { 
	} else {
		qty=0;
	}
	for(i=0;i<line.length;i++) {
		if (line[i][0]==id) {
			line[i][col]=qty;
			break;
		}
	}
	$("#"+field+id).val(qty);
	
	recalcTotals(line,0);
	return false;
}

function setQty2(id, field, col, line) {
	var qty = parseInt( $("#"+field+id).val() ); 
	if (!isNaN( qty ) && (qty>=0)) { 
	} else {
		qty=0;
	}
	
	for(i=0;i<line.length;i++) {
		if (line[i][1]==id) {
			line[i][col]=qty;
			break;
		}
	}
	$("#"+field+id).val(qty);
	
	recalcTotals(line,1);
	return false;
}
