
//define globals
window.prices = new Array();
var selWidth = 0;
var selHeight = 0;
var group = '';
var extra = 0.00;
window.price = 0.00;
var total = 0.00;

//define data columns (numbers are mesurements in CM, corresponding to the prices array.)
var dimensions = new Array(
'280|100',
'240|100',
'200|100',
'300|100',
'140|100',
'100|100',
'120|100',
'260|100',
'160|100',
'180|100',
'220|100',
'120|120',
'240|120',
'220|120',
'180|120',
'300|120',
'280|120',
'100|120',
'140|120',
'200|120',
'160|120',
'260|120',
'280|140',
'160|140',
'240|140',
'100|140',
'140|140',
'180|140',
'260|140',
'220|140',
'300|140',
'200|140',
'120|140',
'260|160',
'200|160',
'140|160',
'180|160',
'100|160',
'120|160',
'160|160',
'220|160',
'300|160',
'240|160',
'280|160',
'260|180',
'200|180',
'300|180',
'140|180',
'160|180',
'280|180',
'120|180',
'220|180',
'240|180',
'100|180',
'180|180',
'300|200',
'100|200',
'180|200',
'120|200',
'260|200',
'200|200',
'140|200',
'220|200',
'160|200',
'240|200',
'280|200',
'160|220',
'100|220',
'200|220',
'280|220',
'220|220',
'260|220',
'120|220',
'140|220',
'180|220',
'240|220',
'300|220',
'280|240',
'200|240',
'260|240',
'240|240',
'220|240',
'120|240',
'100|240',
'160|240',
'300|240',
'180|240',
'140|240',
'280|260',
'300|260',
'220|260',
'200|260',
'180|260',
'240|260',
'140|260',
'120|260',
'100|260',
'160|260',
'260|260',
'100|280',
'280|280',
'300|280',
'240|280',
'220|280',
'140|280',
'200|280',
'180|280',
'160|280',
'120|280',
'260|280',
'280|300',
'120|300',
'140|300',
'160|300',
'180|300',
'200|300',
'220|300',
'240|300',
'260|300',
'100|300',
'300|300'
);
prices[0] = 283.784;
prices[1] = 244.094;
prices[2] = 210.357;
prices[3] = 305.613;
prices[4] = 170.888;
prices[5] = 148.838;
prices[6] = 159.863;
prices[7] = 262.946;
prices[8] = 181.913;
prices[9] = 194.481;
prices[10] = 226.233;
prices[11] = 170.888;
prices[12] = 265.923;
prices[13] = 247.070;
prices[14] = 212.342;
prices[15] = 331.412;
prices[16] = 307.598;
prices[17] = 159.863;
prices[18] = 182.574;
prices[19] = 229.210;
prices[20] = 196.466;
prices[21] = 285.768;
prices[22] = 334.388;
prices[23] = 214.326;
prices[24] = 288.745;
prices[25] = 170.888;
prices[26] = 198.450;
prices[27] = 231.194;
prices[28] = 311.567;
prices[29] = 267.908;
prices[30] = 360.187;
prices[31] = 249.055;
prices[32] = 184.559;
prices[33] = 337.365;
prices[34] = 269.892;
prices[35] = 216.311;
prices[36] = 251.039;
prices[37] = 186.543;
prices[38] = 201.427;
prices[39] = 233.179;
prices[40] = 290.729;
prices[41] = 390.947;
prices[42] = 313.551;
prices[43] = 363.164;
prices[44] = 368.125;
prices[45] = 294.698;
prices[46] = 425.675;
prices[47] = 235.163;
prices[48] = 253.024;
prices[49] = 395.908;
prices[50] = 219.287;
prices[51] = 316.528;
prices[52] = 341.334;
prices[53] = 203.411;
prices[54] = 273.861;
prices[55] = 465.365;
prices[56] = 221.272;
prices[57] = 297.675;
prices[58] = 238.140;
prices[59] = 400.869;
prices[60] = 321.489;
prices[61] = 256.993;
prices[62] = 345.303;
prices[63] = 276.838;
prices[64] = 372.094;
prices[65] = 432.621;
prices[66] = 301.644;
prices[67] = 241.117;
prices[68] = 349.272;
prices[69] = 469.334;
prices[70] = 376.063;
prices[71] = 435.598;
prices[72] = 259.970;
prices[73] = 279.815;
prices[74] = 324.466;
prices[75] = 404.838;
prices[76] = 504.063;
prices[77] = 509.024;
prices[78] = 380.032;
prices[79] = 473.303;
prices[80] = 440.559;
prices[81] = 408.807;
prices[82] = 281.799;
prices[83] = 261.954;
prices[84] = 327.443;
prices[85] = 547.722;
prices[86] = 353.241;
prices[87] = 279.815;
prices[88] = 551.691;
prices[89] = 593.366;
prices[90] = 442.544;
prices[91] = 410.792;
prices[92] = 382.016;
prices[93] = 475.288;
prices[94] = 304.621;
prices[95] = 306.605;
prices[96] = 284.776;
prices[97] = 355.226;
prices[98] = 512.001;
prices[99] = 292.714;
prices[100] = 567.567;
prices[101] = 611.226;
prices[102] = 490.172;
prices[103] = 455.443;
prices[104] = 330.419;
prices[105] = 423.691;
prices[106] = 393.923;
prices[107] = 366.140;
prices[108] = 315.536;
prices[109] = 527.877;
prices[110] = 579.474;
prices[111] = 322.481;
prices[112] = 340.342;
prices[113] = 374.078;
prices[114] = 401.861;
prices[115] = 431.629;
prices[116] = 464.373;
prices[117] = 500.094;
prices[118] = 537.800;
prices[119] = 298.667;
prices[120] = 623.133;
;
var widths = new Array(
100,
120,
140,
160,
180,
200,
220,
240,
260,
280,
300
);
var heights = new Array(
100,
120,
140,
160,
180,
200,
220,
240,
260,
280,
300
);

function showhideSubmit() {
	var breed = document.getElementById('breed');
	var hoog = document.getElementById('hoog');
	
	//var bedien = document.bestelform.bediening.value;
	//var lamel = document.bestelform.breedte.value;
	var bedien = '';
	var lamel = '';
	var balein = '';
	for (var i = 0;i < document.forms[0].bediening.length; i++) {
		if (document.forms[0].bediening[i].checked) {
			bedien = document.forms[0].bediening[i].value;
		}
	}
	
	var baleinengekozen = false;
	
	if(document.getElementById('baleinenmet').checked == false && document.getElementById('baleinenzonder').checked == false) {
		baleinengekozen = false;
	} else {
		baleinengekozen = true
	}
			
	var klr = document.bestelform.kleur.value;
	
	if ( (breed.value > 0) && (hoog.value > 0) && (bedien != '') && (klr != '') && (baleinengekozen == true) ) {
		var subber = document.getElementById('winkelknop');
		subber.innerHTML = '<input value=" Plaats in winkelmandje " type="submit">';
	} else {
		var subber = document.getElementById('winkelknop');
		var fields = 'Nog in te vullen velden:<br />';
		
		if (breed.value <= 0) {
			fields += '- breedte<br />';
		}
		
		if (hoog.value <= 0) {
			fields += '- hoogte<br />';
		}
		
		if (bedien == '') {
			fields += '- bediening<br />';
		}
		
		if (balein == '') {
			fields += '- baleinen<br />';
		}
		
		if (klr == '') {
			fields += '- kleur<br />';
		}
		
		subber.innerHTML = fields;
			
	}
	
	
}

function Calculate(){
	price = 0;
	showhideSubmit();
	if(selWidth > 0 && selHeight > 0){
		//get width key
		w = 0;
		for(i in widths){
			if(widths[i] >= selWidth){
				w = widths[i];
				break;
			}
		}

		//get height key
		h = 0;
		for(i in heights){
			if(heights[i] >= selHeight){
				h = heights[i];
	    		break;
	    	}
		}

		//do checks
		var checker = h + "|" + w;
		var arlength = dimensions.length;
		//var price = 0;
		
		for(q = 0; q < arlength; q++) {
			if (dimensions[q] == checker) {
				price = prices[q];
			}
		}
		
		if(price == 0){
	    	alert("Sorry, de door u opgegeven maat is niet leverbaar.");
	    	document.getElementById('breed').value = '';
	  		document.getElementById('hoog').value = '';
	  		document.getElementById('breed').focus();
	  		selHeight = 0;
	  		selWidth = 0;
	    		//alert("DEBUG: No price found?! prices["+h+"]["+w+"]");
	   } else {
			//Do some extra price calculations, depanding on the selected width
			if(selWidth < 30) {
							}
			
			if((selWidth >= 30 && selWidth < 40) || (selHeight >= 30 && selHeight < 40)) {
							}

		
			//parse to screen
			document.getElementById('price').innerHTML = "Basis: <span class=\"price\">&euro; " + roundTotal(price) + "</span>";
			document.getElementById('basisprice').innerHTML = "Basis: <span class=\"price\">&euro; " + roundTotal(price) + "</span>";


            //change price depanding on selected color group
			switch(group){
            	case 'PG1':
            		price = price * 1.0;            		//price = price * 1; //do nothing... just here as a dummie...
            		break;
            	case 'PG2':
            		price = price * 1.20;            		//price = price * 1.3; //+30%
            		break;
            	case 'PG3':
            		price = price * 1.20;            		//price = price * 1.4; //+40%
            		break;
			}

			//change total gui
			Total();

			//parse to screen
			document.getElementById('piece_price').innerHTML = "Basis + kleur: <span class=\"price\">&euro; " + roundTotal(price) + "</span>";
		}
	}
}

function Height(obj){
	h = parseFloat(obj.value.replace(',','.'));
	selHeight = Math.ceil(h);
	if(selHeight < 28) {
		alert("Sorry, onder de 28 cm is er niets leverbaar.");
		document.getElementById('breed').value = '';
	  document.getElementById('hoog').value = '';
	} else {
		document.getElementById('hoog').value = h;
		Calculate();
	}
}

function Width(obj){
	b = parseFloat(obj.value.replace(',','.'));
	selWidth = Math.ceil(b);
	if(selWidth < 28) {
		alert("Sorry, onder de 28 cm is er niets leverbaar.");
		document.getElementById('breed').value = '';
	  document.getElementById('hoog').value = '';
	} else {
		document.getElementById('breed').value = b;
		Calculate();
	}
}

function Extra(){
	extra = 0;
	if(document.getElementById('dimout').checked) {
		extra+= price * 0.5;
	}
	//change total gui
	Total();
}

function Total(){
	
    if(price > 0){
		total = price + extra;
		document.getElementById('total').innerHTML = "Totaal (incl. 19% BTW): <span class=\"price\">&euro; " + roundTotal(total) + "</span><input type=hidden name=TOTAAL value=\"&euro; " + roundTotal(total,true) + "\">";
//		document.getElementById('total').innerHTML = "Totaal (incl. 19% BTW): <span class=\"price\">&euro; " + roundTotal(total) + "</span>";
		adviesverkoopprijs(total);
	} else {
		document.getElementById('total').innerHTML = price
	}
}

function roundTotal(c,nohtml){
	if(c==0)
		return '0,00';
	var pennies = c * 100;
	pennies = Math.round(pennies);
	var strPennies = pennies.toString();
	var len = strPennies.length;
	e = strPennies.substring(0, len - 2);
	c = strPennies.substring(len - 2, len);
	if(!e)
		e = 0;
	if(!nohtml)
		return (e + ',<span class="cents">' + c + '</span>');
	else
		return (e + ',' + c);
}

function calcTax(price){
	return (price  / 119) * 19;
}

function SetColor(g){
	group = g;
	Calculate();
	Total();
}

function SetKleur(testkleur){
	document.getElementById('testkleur').innerHTML = "Kleur: " + testkleur;
	document.getElementById('kleur').value = testkleur; //insert into form
}

function roll_over(img_name, img_src){
	document[img_name].src = img_src;
}

function showBalein(d) {
	
	var bal = document.getElementById('baleinen');
	var met = document.getElementById('balmet');
	var zon = document.getElementById('balzon');
	
	if (d == 'met') {
		bal.style.display = 'block';
		met.checked = true;
		zon.checked = false;
	} else {
		bal.style.display = 'none';
		met.checked = false;
		zon.checked = true;
	}
		
}


function roundAdviesTotal(c){
	var a = Math.round(c/10);
	var b = a * 10;
	var c = b - 1;
	return c;
}
function adviesverkoopprijs(pr) {
	
	 var advprice = pr + (pr * (30/100));
	
	$('#adviesprijs').html("Adviesprijs: <span class='price'>&euro; " + roundTotal(roundAdviesTotal(advprice)) + "</span>");
		
}


