//store previous unit
var previousUnit = "Cubic Feet";
//store cubic values
var cubicInches = 0;
var cubicCentimetres = 0;
//measurement symbols
var symbol = "Feet";
var subSymbol = "Inches";

//cubic feet to metres conversion values
var cftocm = 0.0283168466;
//cubic metres to feet conversion value

var cmtocf = 35.3146667;
//metres to feet conversion value

//Metres to feet conversion value
var mtof = 3.2808399;
//Feet to Metres conversion value
var ftom = 0.3048;

//Metres to inches conversion value
var mtoin = 39.3700787;

//Centimetres to inches conversion value
var cmtoin = 0.393700787;

//feet to centimetres
var ftocm = 30.48;
//inches to centimetres
var intocm = 2.54;


/** calculate the volume **/
function calcCubics()
{	
	if(previousUnit == "Cubic Feet")
	{
		var divider = 12;
	}else if(previousUnit == "Cubic Metres")
	{
		var divider = 100;
	}
	
	
	var cc_fwidth = document.getElementById('cc_width').value;
	var cc_fheight = document.getElementById('cc_height').value;
	var cc_flength = document.getElementById('cc_length').value;
	
	var cc_fwidthsub = document.getElementById('cc_widthsub').value;
	var cc_fheightsub = document.getElementById('cc_heightsub').value;
	var cc_flengthsub = document.getElementById('cc_lengthsub').value;
	
	var cc_fw_tot = (cc_fwidth * divider) + parseFloat(cc_fwidthsub);
	var cc_fh_tot = (cc_fheight * divider) + parseFloat(cc_fheightsub);
	var cc_fl_tot = (cc_flength * divider) + parseFloat(cc_flengthsub);
	
	if(!(cc_fw_tot>0)){cc_fw_tot=0;}
	if(!(cc_fh_tot>0)){cc_fh_tot=0;}
	if(!(cc_fl_tot>0)){cc_fl_tot=0;}
	
	var cc_ftotals = cc_fw_tot * cc_fh_tot * cc_fl_tot;

	//var cc_ftotals = parseInt(cc_fw_tot) + parseInt(cc_fh_tot) + parseInt(cc_fh_tot);
	
	//var cc_ftotals = ((cc_fwidthsub * divider) + cc_fwidth ) * ((cc_fheightsub * divider) + cc_fheight ) * ((cc_flengthsub * divider) + cc_flength);
	//var cc_ftotals = ((cc_fwidth * divider) * (cc_fheight * divider) * (cc_flength * divider)) + (cc_fwidthsub * cc_fheightsub * cc_flengthsub);
	//var cc_ftotals = ((cc_fwidth * divider) + parseFloat(cc_fwidthsub )) * ((cc_fheight * divider) + parseFloat(cc_fheightsub )) * ((cc_flength * divider) + parseFloat(cc_flengthsub));
	
	if(previousUnit == "Cubic Feet")
	{
		cubicInches = cc_ftotals;
		cubicCentimetres = 'recalculate';
	}else if(previousUnit == "Cubic Metres")
	{
		cubicInches = cc_ftotals * 0.0610237441;
		cubicCentimetres = cc_ftotals;
	}
	
	var cc_ftotal = (cc_ftotals / (divider * divider * divider));
	
	var cc_ftotalsub = (cc_ftotals % (divider * divider * divider));
	
	//fix sub, check if they round to next number
	if(roundNum(cc_ftotalsub, 1)==divider){cc_ftotalsub = 0;cc_ftotal++;}
	
	document.getElementById("cc_total").innerHTML = Math.floor(cc_ftotal, 2);
	document.getElementById("cc_totalsub").innerHTML = roundNum(cc_ftotalsub, 1);
	
	//update the costs
	updateCosts();
}

/** switch measurement **/
function switchCubicMeasurement()
{
	//Get value of unit dropdown from unit dropdown's selected index
	var cc_unit = document.getElementById('cc_unit').options[document.getElementById('cc_unit').selectedIndex].value;
	var cc_fwidth = document.getElementById('cc_width').value;
	var cc_fheight = document.getElementById('cc_height').value;
	var cc_flength = document.getElementById('cc_length').value;
	
	var cc_fwidthsub = document.getElementById('cc_widthsub').value;
	var cc_fheightsub = document.getElementById('cc_heightsub').value;
	var cc_flengthsub = document.getElementById('cc_lengthsub').value;
	var cc_ftotal; 
	
	if(cc_unit == "Cubic Feet")
	{
		//check if unit has changed from Metres to Feet
		if(previousUnit=="Cubic Metres")
		{
			var cc_w_submes = ( cc_fwidth * mtoin ) + ( cc_fwidthsub * cmtoin );
			var cc_h_submes = ( cc_fheight * mtoin ) + ( cc_fheightsub * cmtoin );
			var cc_l_submes = ( cc_flength * mtoin ) + ( cc_flengthsub * cmtoin );
			var cc_t_submes = cc_w_submes * cc_w_submes * cc_w_submes;
			
			var cc_unit_text = "Feet&#179;";
			var cc_unitsub_text = "Inches&#179;";
			//writeConsole("W Inches:"+cc_w_inches+"<br />H Inches:"+cc_h_inches+"<br />L Inches:"+cc_l_inches+"<br />Total Inches:"+cc_t_inches);
			
			//division variable
			var divider = 12;
			
			//set new measurement symbols
			symbol = "Feet";
			subSymbol = "Inches";
			
			//set previous unit to the now current unit
			previousUnit = "Cubic Feet";
		}else
		{
			//not changed, so don't bother doing anything
			return;
		}
	}else if(cc_unit == "Cubic Metres")
	{
		//check if unit has changed from Feet to Metres
		if(previousUnit=="Cubic Feet")
		{
			var cc_w_submes = ( cc_fwidth * ftocm ) + ( cc_fwidthsub * intocm );
			var cc_h_submes = ( cc_fheight * ftocm ) + ( cc_fheightsub * intocm );
			var cc_l_submes = ( cc_flength * ftocm ) + ( cc_flengthsub * intocm );
			var cc_t_submes = cc_w_submes * cc_w_submes * cc_w_submes;
			
			var cc_unit_text = "Metres&#179;";
			var cc_unitsub_text = "Centimetres&#179;";
			//Switch and convert Units
			//writeConsole("W Cm:"+cc_w_centimetres+"( cc_fwidth * intocm ):"+( cc_fwidth * intocm )+"( cc_fwidthsub * intocm ):"+( cc_fwidthsub * intocm ));

			//division variable
			var divider = 100;
			
			//set new measurement symbols
			symbol = "M";
			subSymbol = "cm";
			
			//set previous unit to the now current unit
			previousUnit = "Cubic Metres";
		}else
		{
			//not changed, so don't bother doing anything
			return;
		}
	}
	

	cc_fwidth = (cc_w_submes / divider);
	cc_fheight = (cc_h_submes / divider);
	cc_flength = (cc_l_submes / divider);
	
	cc_fwidthsub = (cc_w_submes % divider);
	cc_fheightsub = (cc_h_submes % divider);
	cc_flengthsub = (cc_l_submes % divider);
	cc_ftotal = cc_fwidth * cc_fheight * cc_flength;
	

	cc_ftotalsub = cc_fwidthsub * cc_fheightsub * cc_flengthsub;
	
	//fix sub, check if they round to next number
	if(roundNum(cc_fwidthsub, 1)==divider){cc_fwidthsub = 0;cc_fwidth++;}
	if(roundNum(cc_fheightsub, 1)==divider){cc_fheightsub = 0;cc_fheight++;}	
	if(roundNum(cc_flengthsub, 1)==divider){cc_flengthsub = 0;cc_flength++;}
	
	document.getElementById("cc_width").value = Math.floor(cc_fwidth);
	document.getElementById("cc_height").value = Math.floor(cc_fheight);
	document.getElementById("cc_length").value = Math.floor(cc_flength);
	document.getElementById("cc_widthsub").value = roundNum(cc_fwidthsub, 1);
	document.getElementById("cc_heightsub").value = roundNum(cc_fheightsub, 1);
	document.getElementById("cc_lengthsub").value = roundNum(cc_flengthsub, 1);
	//document.getElementById("cc_total").innerHTML = roundNum(cc_ftotal ,0);
	//document.getElementById("cc_totalsub").innerHTML = roundNum(cc_ftotalsub ,0);
	
	//update symbols
	updateSymbols(symbol, subSymbol);
	
	//update unit text
	document.getElementById("unitReq").innerHTML = cc_unit_text;
	document.getElementById("unitReqsub").innerHTML = cc_unitsub_text;
	
	//update the costs
	calcCubics();
}

// update the symbols
function updateSymbols(symbol, subSymbol)
{
	var i = 0;
	for(i = 1;i<4;i++)
	{
		document.getElementById("measureSymbol"+i).innerHTML = symbol;
		document.getElementById("subMeasureSymbol"+i).innerHTML = subSymbol;
	}
}


function updateCosts()
{
	if(document.getElementById("proList"))
	{
		var metreage;
		var outputText = "";
		if(previousUnit == "Cubic Metres")
		{	
			metreage = Math.ceil(cubicCentimetres / 1000000);
			feetage = Math.ceil(cubicCentimetres * 3.53146667);
		}else if(previousUnit == "Cubic Feet")
		{
			metreage = Math.ceil((cubicInches * 16.387064) / 1000000);
			feetage = Math.ceil(cubicInches * 0.000578703704);
		}
		//metreage = feetage;
		metreage = Math.ceil(cubicInches * 0.000578703704);
		metreage = Math.ceil(roundNum((cubicInches * 0.000578703704), 8));
		outputText = outputText = metreage+" (To the next whole cubic foot)";
		var theCost=costsArray[document.getElementById('proList').value];
		var totalCubicCost= metreage * theCost;
		if(metreage > 0)
		{
			outputText = outputText+" x "+theCost+" per square foot = &pound;"+roundNum(totalCubicCost, 2)+" (Exc. Vat)";
			outputText = outputText+'<a onclick="addToBasket('+metreage+')" style="cursor: pointer; cursor: hand;"> &gt;&gt;Add to Basket</a>';
		}else
		{
			outputText = "";
		}
		document.getElementById("costs").innerHTML = outputText;
	}else
	{
		document.getElementById("costs").innerHTML = "No";
	}
}

function addToBasket(quantity)
{
	document.getElementById("areaTotal").value = quantity;
	productSelect();
	document.getElementById("prodSelFrm").submit();
}

function initCubicCalculator()
{
	calcCubics();
}

function writeConsole(content) {
 top.consoleRef=window.open('','myconsole',
  'width=350,height=250'
   +',menubar=0'
   +',toolbar=1'
   +',status=0'
   +',scrollbars=1'
   +',resizable=1')
 top.consoleRef.document.writeln(
  '<html><head><title>Console</title></head>'
   +'<body bgcolor=white onLoad="self.focus()">'
   +content
   +'</body></html>'
 )
 top.consoleRef.document.close()
}

// round a number to specified decimal places
function roundNum(n, d)
{	return Math.round(n * Math.pow(10, d)) / Math.pow(10, d);
}

// check input for valid unit textbox key input - Usage example : onkeypress="return checkKeyPress(event, this.value);"
function checkUnitKey(e, i)
{	e = (e) ? e : event;
	var charCode = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));

	// do not allow anything that isn't a number, period or functionality key (tab, backspace, delete, cursors etc)
	if ( (charCode > 31) &&
		 (charCode < 48 || charCode > 57) &&
		 (charCode < 96 || charCode > 105) &&
		 (charCode < 35 || charCode > 40) &&
		 (charCode != 46) &&
		 (charCode != 110) &&
		 (charCode != 190) )
	{	return false;
	}

	// only allow one period to be entered
	if ( (charCode == 110 || charCode == 190) && (i.indexOf(".") != -1 || i.length < 1))
	{	return false;
	}
	return true;
	updateCosts();
}
initCubicCalculator();