var delCharges=new Array();
function init(){
	var TempPrice=0;
	var colorName='';
	var CategoryName='';
	//Make Delivery Array
	// UK (Over £100)
	delCharges[0]=new Array();
	delCharges[0][0]="FREE";
	delCharges[0][1]="FREE";
	delCharges[0][2]="FREE";
	// UK (Under £100)
	delCharges[1]=new Array();
	delCharges[1][0]=10;
	delCharges[1][1]="n/a"; //Shouldn't be possible
	delCharges[1][2]="n/a"; //Shouldn't be possible
	// EU
	delCharges[2]=new Array();
	delCharges[2][0]=35;
	delCharges[2][1]=55;
	delCharges[2][2]=75;
	// Rest of Europe
	delCharges[3]=new Array();
	delCharges[3][0]=40;
	delCharges[3][1]=60;
	delCharges[3][2]=80;
	// US/Canada
	delCharges[4]=new Array();
	delCharges[4][0]=75;
	delCharges[4][1]=110;
	delCharges[4][2]=130;
	delCharges[4][3]=160;
	delCharges[4][4]=190;
	delCharges[4][5]=220;
	delCharges[4][6]="POA";
	// Rest Of World
	delCharges[5]=new Array();
	delCharges[5][0]="POA";
	delCharges[5][1]="POA";
	delCharges[5][2]="POA";
	// None selected
	delCharges[6]=new Array();
	delCharges[6][0]=0;	

	for(var i=0,j=mat.length; i<j; i++){
		if(i==0){colorName = mat[i][2];}
		//alert(colorName);
		if(colorName != mat[i][2]){
			//alert(colorName+', '+TempPrice);
			document.getElementById('price_'+colorName).value = TempPrice.toFixed(2);
			TempPrice=0;
		}
		var temp2=document.getElementById(mat[i][0]);
//		TempPrice=parseFloat(TempPrice+((chkInput(temp2.value, temp2))*mat[i][3]));
		TempPrice=parseFloat(TempPrice)+parseFloat(fetchPrice(chkInput(temp2.value, temp2), mat[i][3], mat[i][7]));
		colorName = mat[i][2];
	}
	document.getElementById('price_'+colorName).value = TempPrice.toFixed(2);
	updateTotal();
}
function fixPrice(Temp){
	Temp = Temp.replace(/\<strong\>\&pound\;/gi,'');
	Temp = Temp.replace(/\<strong\>\£/gi,'');
	Temp = Temp.replace(/\<\/strong\>/gi,'');
	Temp = parseFloat(Temp);
	return Temp
}
function fetchPrice(metres, unitPrice, CategoryName){
	var Temp;
	if(metres<5 && (CategoryName=="Favourites" || CategoryName=="Standard" || CategoryName=="Metallic" || CategoryName=="Electric")){
		var NewPrice=parseFloat(unitPrice)+parseFloat(unitPrice*0.5);
		Temp=parseFloat(metres*(NewPrice));
		//if(metres>0){alert(metres+" "+unitPrice+" "+CategoryName+" = "+Temp);};
	}else{
		Temp=parseFloat(metres*unitPrice);
	}	
	return Temp
}
function updateTotal(){		
		var Temp='';
		var Working='';
		var colorName='';
		var skipper=0;
		var arrSizes=new Array();
		var y=0;
		var tmpLength=0;
		var discountRate=0;
		var disFigure=0;
	for(var i=0,j=mat.length; i<j; i++){
		if(i==0){colorName = mat[i][2];}
		if(colorName != mat[i][2]){skipper=0;colorName = mat[i][2];}
		//alert(colorName);
		if(colorName == mat[i][2] && skipper==0){
			//alert(colorName+', '+TempPrice);
			Temp = fixPrice(document.getElementById('price_'+mat[i][2]).value);
			Working = parseFloat(Working+Temp);
			Temp=0;
			skipper=1;
			y=0;
			//Discount applied to this total...
			if(mat[i][7]=="Standard" || mat[i][7]=="Metallic" || mat[i][7]=="Electric"){
				Temp = fixPrice(document.getElementById('price_'+mat[i][2]).value);
				disFigure=parseFloat(disFigure+Temp);
			}
			
		}
		//Do Size Totals
		if (isNaN(arrSizes[y])){arrSizes[y]=0;}
		//alert(tmpLength);
		arrSizes[y]=parseInt(arrSizes[y])+parseInt(chkInput(document.getElementById(mat[i][0]).value, document.getElementById(mat[i][0])));
//		alert(arrSizes[y]);
		y=y+1;
	}
	tmpLength=0;
	for(var y=0,z=arrSizes.length; y<z; y++){
//		alert(arrSizes[y]);
		//this line writes the rubber group total, groups lengths by thickness...
		tmpLength=parseInt(tmpLength)+parseInt(arrSizes[y]);
		//tmpLength now eguals the total length of the order...
		document.getElementById('Size_'+parseInt(y+1)).value = arrSizes[y];
	}

	//alert(Working);
	Working=Working.toFixed(2);
	//alert(Working);
	//this line produces the subtotal
	document.getElementById('priceSub').value = Working;

	//Figure out discount rate.
	if(tmpLength<50){discountRate=0;}
	if(tmpLength>49 && tmpLength<150){discountRate=5;}
	if(tmpLength>149 && tmpLength<300){discountRate=7.5;}
	if(tmpLength>299){discountRate=10;}
	//document.getElementById('priceDis').value = discountRate+"% off £"+disFigure+" = £"+(disFigure/100)*discountRate;
	document.getElementById('bulkDiscount').value = ((disFigure/100)*discountRate).toFixed(2);
	
	var TempWeight;
	var GrandTotal=Working;
	var Weight=0.00;
	var weightBand;
	var delBand;
	var destination;
	
	for(var i=0,j=mat.length; i<j; i++){
		var temp2=document.getElementById(mat[i][0]);
		TempWeight=parseInt(chkInput(temp2.value, temp2));
		if(TempWeight>0){
			Weight=parseFloat((parseFloat(TempWeight)*parseFloat(mat[i][5]))+parseFloat(Weight));
			Weight=Weight.toFixed(2);
		}
	}
	document.getElementById('priceWeight').value = Weight;
	destination = document.getElementById("destination").options[document.getElementById("destination").selectedIndex].value
//	alert(destination);
	

	if(destination == "none" && Working>1){
	document.getElementById('destination').style.backgroundColor='red';
	document.getElementById('priceDelivery').value = 0;
	GrandTotal=0;
	document.getElementById('priceSub').value =0;
	alert('Please select a shipping region from the "Ship To:" list');
	}
	else
	{
	document.getElementById('destination').style.backgroundColor='white';
	}
	
	if(destination == "none"){delBand = 6}{weightBand = 0}
	if(destination == "UK"){delBand = 0}
	if(destination == "EU"){delBand = 2}
	if(destination == "nonEU"){delBand = 3}
	if(destination == "USA"){delBand = 4}
	if(destination == "roW"){delBand = 5}
	if(destination == "UK" && GrandTotal<100){delBand = 1} //UK <£100	

	//alert(destination)
	//alert(Weight)

	if(destination == "USA")
	{
	if(Weight>0 && Weight<11){weightBand = 0}
	if(Weight>=11){weightBand = 1}
	if(Weight>=26){weightBand = 2}
	if(Weight>=36){weightBand = 3}
	if(Weight>=51){weightBand = 4}
	if(Weight>=76){weightBand = 5}
	if(Weight>=101){weightBand = 6}
	//alert('us weightband'+weightBand)
	}
	else
	{	
	if(Weight>0 && Weight<31){weightBand = 0}
	if(Weight>=31){weightBand = 1}
	if(Weight>=61){weightBand = 2}

	}
	
//alert('destination')
//alert('delBand'+delBand);
//alert('weightBand'+weightBand);

	delivery = delCharges[delBand][weightBand];
	if(GrandTotal<1){delivery = 0}
	document.getElementById('priceDelivery').value = delivery;
	if(isNaN(delivery)){delivery = 0}
	//Deal with discount
	disFigure=((disFigure/100)*parseFloat(discountRate));
	disFigure=disFigure.toFixed(2);
	//alert("Total discount: £"+disFigure);
	GrandTotal=parseFloat(GrandTotal)-disFigure;
	//Figure grand total
	GrandTotal=parseFloat(GrandTotal)+parseFloat(delivery);
	GrandTotal=GrandTotal.toFixed(2);
	document.getElementById('grandTotal').value = GrandTotal;
	document.getElementById('Calculating').style.visibility='hidden';
}
function calc(id)
{
	document.getElementById('Calculating').style.visibility='visible';
	setTimeout("calculator("+id+");",100);
}

function calculator(id)
{	
	//document.getElementById('Calculating').style.visibility='visible';
	inputval=document.getElementById(id).value;	
	mininputval=parseFloat(document.getElementById('min_'+id).value);
	var numericExpression = /^[0-9]+$/;
	var nameid = id;
	if(!inputval.match(numericExpression)){
	alert('Please enter whole numbers only.');
	document.getElementById(id).value=0;
	document.getElementById(id).style.backgroundColor='red';
	setTimeout("document.getElementById("+nameid+").focus();",100);
	setTimeout("document.getElementById("+nameid+").select();",100);
	document.getElementById('Calculating').style.visibility='hidden';
	return;
	}else{
	if (inputval>0){
	if(inputval<mininputval){
	alert('Minimum order of '+mininputval+' on this product');
	document.getElementById(id).style.backgroundColor='red';
	setTimeout("document.getElementById("+nameid+").focus();",100);
	setTimeout("document.getElementById("+nameid+").select();",100);
	document.getElementById('Calculating').style.visibility='hidden';
	}else{
	document.getElementById(id).style.backgroundColor='#CCC';
	document.getElementById(id).style.fontWeight = 'bold';
	}
	}else{
	document.getElementById(id).style.backgroundColor='white';
	}
	}
//Get ID's of same colour
	var boxIDs='';
	var colorName;
	var CategoryName;
	for(var i=0,j=mat.length; i<j; i++){
		if(id == mat[i][0]){
			colorName = mat[i][2];
			CategoryName = mat[i][7];
		}
	}
	for(var i=0,j=mat.length; i<j; i++){
		if(colorName == mat[i][2]){
			boxIDs=boxIDs+'|'+i;
		}
	}
	boxIDs=boxIDs.slice(1);
	boxIDs=boxIDs.split('|');
//	alert(colorName);
//	alert(boxIDs);
//Use ID array to read in metre values currently set
	var currentLengths='';
	for(var i=0,j=boxIDs.length; i<j; i++){
		var temp=parseInt(boxIDs[i]);
	//	alert(temp);
		temp=mat[temp][0];
//		alert(temp);
		var temp2=document.getElementById(temp);
		currentLengths=currentLengths+'|'+chkInput(temp2.value, temp2)+'-'+mat[(boxIDs[i])][3]+'-'+mat[(boxIDs[i])][4]+'-'+mat[(boxIDs[i])][5];
	}
	currentLengths=currentLengths.slice(1);
	currentLengths=currentLengths.split('|');
//	alert(currentLengths);
	//Make 2d Array with relevent info
	for(var i=0,j=currentLengths.length; i<j; i++){
		currentLengths[i]=currentLengths[i].split('-');
	//	alert(currentLengths[i]);
	}
		var TempPrice=0;
	for(var i=0,j=currentLengths.length; i<j; i++){
		//alert(currentLengths[i][0]+'*'+currentLengths[i][1]);
		TempPrice=parseFloat(TempPrice)+parseFloat(fetchPrice(currentLengths[i][0], currentLengths[i][1], CategoryName));
	}
//	alert(TempPrice);
	document.getElementById('price_'+colorName).value = TempPrice.toFixed(2);
	updateTotal();
}
function chkInput(val,loc){
	var val2;
	if(typeof(val)=='number' && /^\d*$/.test(val.toString(10))){
		val2 = val;
	}else{
		val2=parseInt(val);
		loc.value = val2;
		
	}
//	return parseInt(val);
	return parseInt(val2);
}

