// JavaScript Document
var curUnits = "cm";
function changeUnits(which){
	curUnits = which;
	calculateVolume_cubic()
}
function calculateWeight(){
	//
	var weight = document.forms["conv_calc"].weight.value;
	var selectList = document.forms["conv_calc"].weight_conv;
	//
	if (selectList.options[selectList.selectedIndex].value == "kg") {
		weight= (weight*0.4536);
	} else if (selectList.options[selectList.selectedIndex].value == "lbs") {
		weight= (weight*2.205);
	} else {
		weight= 0;
	}
	
	weight = Math.round(weight*10)/10;

	weight = weight + "";
	myNum = weight.substr(weight.length-1, 1);
	if(myNum <= 3){
		weight = Math.floor(weight);
	}else if(myNum >= 8){
		weight = Math.ceil(weight);
	}else{
		weight = weight.substr(0, weight.length-1) + "5";
		weight = weight *1;
	}	
	document.forms["conv_calc"].result_weight.value= weight;
	
}
function calculateLinear(){
	//
	var linear = document.forms["conv_calc"].linear.value;
	var selectList = document.forms["conv_calc"].linear_conv;
	//
	if (selectList.options[selectList.selectedIndex].value == "cm_in") {
		linear= (linear*0.3937);
	} else if (selectList.options[selectList.selectedIndex].value == "cm_m") {
		linear= (linear*0.0100);
	} else if (selectList.options[selectList.selectedIndex].value == "in_cm") {
		linear= (linear*2.54);
	} else if (selectList.options[selectList.selectedIndex].value == "ft_m") {
		linear= (linear*0.3048);
	} else if (selectList.options[selectList.selectedIndex].value == "m_cm") {
		linear= (linear*100);
	} else if (selectList.options[selectList.selectedIndex].value == "mm_in") {
		linear= (linear*0.0394);
	} else {
		linear= 0;
	}
	
	document.forms["conv_calc"].result_linear.value= Math.round(linear*100)/100;
}

function checkNum(which, evt){
	var key;
	var keychar;
	
	if(window.event){
	   key = window.event.keyCode;
	}else if (evt){
	   key = evt.which;
	}else{
	   return true;
	}

	keychar = String.fromCharCode(key);
	
	if ((("0123456789.").indexOf(keychar) > -1)){
	   	return true;
	}else if (key == "0" || key=="8" || key=="13"){
		return true;
	}else{
		alert("Entered values must be numbers");
	   	return false;
		
	}	
}

function calculateVolume() {
	which = total = "";	
	getElement('actual_weight').value =  Math.round(getElement('actual_weight').value*100)/100;
	if(document.all){
		//ie
		which = getElement('calcTable').childNodes[1];
		total = which.childNodes.length-4;
	}else{
		//gecko
		which = getElement('calcTable').childNodes[0];
		total = which.childNodes.length-9;
	}
		
	var cnt_plt = "";
	var vol_length = "";
	var vol_width = "";
	var vol_height = "";
	var vol_total = 0;
	var selectList = getElement('volume_conv');
	
	for(i=1; i<=total; i++){
		//Set all fields to right class
		if(document.all){
			getElement("ctn_plt_"+i).className = "convfield_cubic";
			getElement("vol_length_"+i).className = "convfield_cubic";
			getElement("vol_width_"+i).className = "convfield_cubic";
			getElement("vol_height_"+i).className = "convfield_cubic";
		}else{
			getElement("ctn_plt_"+i).setAttribute("class", "convfield_cubic");
			getElement("vol_length_"+i).setAttribute("class", "convfield_cubic");
			getElement("vol_width_"+i).setAttribute("class", "convfield_cubic");
			getElement("vol_height_"+i).setAttribute("class", "convfield_cubic");
		}
		
		ctn_plt = getElement("ctn_plt_"+i).value;		
		if(ctn_plt=="" || ctn_plt==0){
			if(document.all){
				getElement("ctn_plt_"+i).className = "convfield_cubic_empty";
			}else{
				getElement("ctn_plt_"+i).setAttribute("class", "convfield_cubic_empty");
			}
		}
		
		vol_length = getElement("vol_length_"+i).value;		
		if(vol_length=="" || vol_length==0){
			if(document.all){
				getElement("vol_length_"+i).className = "convfield_cubic_empty";
			}else{
				getElement("vol_length_"+i).setAttribute("class", "convfield_cubic_empty");
			}			
		}
		
		vol_width = getElement("vol_width_"+i).value;	
		if(vol_width=="" || vol_width==0){
			if(document.all){
				getElement("vol_width_"+i).className = "convfield_cubic_empty";
			}else{
				getElement("vol_width_"+i).setAttribute("class", "convfield_cubic_empty");
			}
		}
		
		vol_height = getElement("vol_height_"+i).value;
		if(vol_height=="" || vol_height==0){
			if(document.all){
				getElement("vol_height_"+i).className = "convfield_cubic_empty";
			}else{
				getElement("vol_height_"+i).setAttribute("class", "convfield_cubic_empty");
			}
		}
		
		if(ctn_plt != "" && vol_length != "" && vol_width !="" && vol_height != ""){
			tempTotal = ctn_plt*vol_length*vol_width*vol_height;
			vol_total = vol_total + tempTotal;	
		}
		
	}
	
	calc_weight = Math.round(vol_total*100)/100;
	getElement('vol_total').value = calc_weight;
	
	if (selectList[selectList.selectedIndex].value == "dom_in_lbs") {
		vol_total = (vol_total/194);
	} else if (selectList[selectList.selectedIndex].value == "int_in_lbs") {
		vol_total= (vol_total/166);
	} else if (selectList[selectList.selectedIndex].value == "int_cm_lbs") {
		vol_total= (vol_total/2722);
	} else if (selectList[selectList.selectedIndex].value == "int_in_kgs") {
		vol_total= (vol_total/366);
	} else if (selectList[selectList.selectedIndex].value == "int_cm_kgs") {
		vol_total= (vol_total/6000);
	} else {
		vol_total= 0;
	}
	
	dim_weight = Math.round(vol_total*100)/100;
	getElement('result_volume').value = dim_weight;
	
	act_weight = getElement('actual_weight').value;
	if(dim_weight>act_weight){
		getElement('charge_result').value = dim_weight;	
	}else{
		getElement('charge_result').value = act_weight;
	}
}
function calculateVolume_cubic() {
	which = total = "";		
	if(document.all){
		//ie
		which = getElement('calcTable_cubic').childNodes[0];
		total = which.childNodes.length-4;
	}else{
		//gecko
		which = getElement('calcTable_cubic').childNodes[1];
		total = which.childNodes.length-9;
	}
	
	var cnt2_plt = "";
	var vol2_length = "";
	var vol2_width = "";
	var vol2_height = "";
	var vol_total = 0;
	var selectList2 = getElement('volume_conv2');
	
	for(i=1; i<=total; i++){
		//Set all fields to right class
		if(document.all){
			getElement("ctn2_plt_"+i).className = "convfield_cubic";
			getElement("vol2_length_"+i).className = "convfield_cubic";
			getElement("vol2_width_"+i).className = "convfield_cubic";
			getElement("vol2_height_"+i).className = "convfield_cubic";
		}else{
			getElement("ctn2_plt_"+i).setAttribute("class", "convfield_cubic");
			getElement("vol2_length_"+i).setAttribute("class", "convfield_cubic");
			getElement("vol2_width_"+i).setAttribute("class", "convfield_cubic");
			getElement("vol2_height_"+i).setAttribute("class", "convfield_cubic");
		}
		
		ctn2_plt = getElement("ctn2_plt_"+i).value;		
		if(ctn2_plt=="" || ctn2_plt==0){
			if(document.all){
				getElement("ctn2_plt_"+i).className = "convfield_cubic_empty";
			}else{
				getElement("ctn2_plt_"+i).setAttribute("class", "convfield_cubic_empty");
			}
		}
		
		vol2_length = getElement("vol2_length_"+i).value;		
		if(vol2_length=="" || vol2_length==0){
			if(document.all){
				getElement("vol2_length_"+i).className = "convfield_cubic_empty";
			}else{
				getElement("vol2_length_"+i).setAttribute("class", "convfield_cubic_empty");
			}			
		}
		
		vol2_width = getElement("vol2_width_"+i).value;	
		if(vol2_width=="" || vol2_width==0){
			if(document.all){
				getElement("vol2_width_"+i).className = "convfield_cubic_empty";
			}else{
				getElement("vol2_width_"+i).setAttribute("class", "convfield_cubic_empty");
			}
		}
		
		vol2_height = getElement("vol2_height_"+i).value;
		if(vol2_height=="" || vol2_height==0){
			if(document.all){
				getElement("vol2_height_"+i).className = "convfield_cubic_empty";
			}else{
				getElement("vol2_height_"+i).setAttribute("class", "convfield_cubic_empty");
			}
		}
		
		if(ctn2_plt != "" && vol2_length != "" && vol2_width !="" && vol2_height != ""){
			tempTotal = ctn2_plt*vol2_length*vol2_width*vol2_height;
			vol_total = vol_total + tempTotal;	
		}
		
	}
	
	calc_weight = Math.round(vol_total*100)/100;
	getElement('vol2_total').value = calc_weight;

	if (selectList2[selectList2.selectedIndex].value == "ft3" && curUnits=='cm') {
		vol_total = ((vol_total/1000000)*35.314);
	} else if (selectList2[selectList2.selectedIndex].value == "ft3" && curUnits=='in') {		
		vol_total= (vol_total/1728);
	} else if (selectList2[selectList2.selectedIndex].value == "m3" && curUnits=='cm') {
		if(vol_total<5000){
			alert("NOTE: Total cubic centimeters must be at least 5000 in order to produce a result greater than 0.");	
		}
		vol_total= (vol_total/1000000);
	} else if (selectList2[selectList2.selectedIndex].value == "m3" && curUnits=='in') {
		vol_total= ((vol_total/1728)/35.314);
	} else {
		vol_total= 0;
	}
	
	dim_weight = Math.round(vol_total*100)/100;
	getElement('result_volume2').value = dim_weight;
}
function calculateOcean1() {
	//
	var ocean1_length = document.forms["conv_calc"].ocean1_length.value;
	var ocean1_width = document.forms["conv_calc"].ocean1_width.value;
	var ocean1_height = document.forms["conv_calc"].ocean1_height.value;
	var ocean1_total = document.forms["conv_calc"].ocean1_total.value;
	var selectList = document.forms["conv_calc"].ocean1_conv;
	//
	ocean1_total = ocean1_length*ocean1_width*ocean1_height;
	document.forms["conv_calc"].ocean1_total.value= ocean1_total;
	document.forms["conv_calc"].ocean1_total.readOnly = true;
	//
	if (selectList.options[selectList.selectedIndex].value == "in_ft3") {
		ocean1_total= (ocean1_total/1728);
	} else {
		ocean1_total= 0;
	}
	document.forms["conv_calc"].result_ocean1.value= ocean1_total;
	document.forms["conv_calc"].result_ocean1.readOnly = true;
}	
function calculateOcean2(){
	//
	var ocean2 = document.forms["conv_calc"].ocean2.value;
	var selectList = document.forms["conv_calc"].ocean2_conv;
	//
	if (selectList.options[selectList.selectedIndex].value == "ft3_m3") {
		ocean2= (ocean2/35.31);
	} else {
		ocean2= 0;
	}
	document.forms["conv_calc"].result_ocean2.value= ocean2;
	document.forms["conv_calc"].result_ocean2.readOnly = true;
}
function changeKPChoices(){
	my_conv_calc = getElement('conv_calc');
	which = my_conv_calc.actual_weight_choice[my_conv_calc.actual_weight_choice.selectedIndex].value;
	if(which=='p'){
		html = '<select name="volume_conv" id="volume_conv" class="ch_weight_select" onchange="calculateVolume();"><option value="Choose One" selected="selected">Choose One:</option><optgroup label="DOMESTIC" class="opt_label" id="dom_group"><option value="dom_in_lbs" id="ditop">Inches to Pounds</option></optgroup><optgroup label="INTERNATIONAL" class="opt_label" id="int_group"><option value="int_in_lbs" id="iitop">Inches to Pounds</option><option value="int_cm_lbs" id="ictop">Centimeters to Pounds</option></optgroup></select>';
	}else if(which=='k'){
		html = '<select name="volume_conv" id="volume_conv" class="ch_weight_select" onchange="calculateVolume();" ><option value="Choose One" selected="selected">Choose One:</option><option value="int_in_kgs" id="itok">Inches to Kilograms</option><option value="int_cm_kgs" id="ctok">Centimeters to Kilograms</option></select>';
	}else{
		html = '<select name="volume_conv" id="volume_conv" class="ch_weight_select" onchange="calculateVolume();" ><option value="Choose One" selected="selected">Choose One:</option><option value="int_in_kgs" id="itok">Inches to Kilograms</option><option value="int_cm_kgs" id="ctok">Centimeters to Kilograms</option><optgroup label="DOMESTIC" class="opt_label" id="dom_group"><option value="dom_in_lbs" id="ditop">Inches to Pounds</option></optgroup><optgroup label="INTERNATIONAL" class="opt_label" id="int_group"><option value="int_in_lbs" id="iitop">Inches to Pounds</option><option value="int_cm_lbs" id="ictop">Centimeters to Pounds</option></optgroup></select>';
	}
	setInnerHTML('volume_conv_box', html);
}
function addRow(){
	total = which = "";
	
	if(document.all){
		//ie
		which = getElement('calcTable').childNodes[1];
		total = which.childNodes.length-3;
		
		//TD 1
		var newInput1 = document.createElement('<input id="ctn_plt_'+total+'" name="ctn_plt_'+total+'" type="text" class="convfield_cubic" onchange="calculateVolume()" onkeypress="return checkNum(this, event)">');
		
		//TD 2
		var newInput2 = document.createElement('<input id="vol_length_'+total+'" name="vol_length_'+total+'" type="text" class="convfield_cubic" onchange="calculateVolume()"  onkeypress="return checkNum(this, event)">');	
		
		//TD 3
		var newInput3 = document.createElement('<input id="vol_width_'+total+'" name="vol_width_'+total+'" type="text" class="convfield_cubic" onchange="calculateVolume()"  onkeypress="return checkNum(this, event)">');
		
		//TD 4
		var newInput4 = document.createElement('<input id="vol_height_'+total+'" name="vol_height_'+total+'" type="text" class="convfield_cubic" onchange="calculateVolume()"  onkeypress="return checkNum(this, event)">');
	}else{
		//gecko
		which = getElement('calcTable').childNodes[0];
		total = which.childNodes.length-8;
		
		//TD 1
		var newInput1 = document.createElement('input');
		newInput1.setAttribute("name","ctn_plt_"+total);
		newInput1.setAttribute("id","ctn_plt_"+total);
		newInput1.setAttribute("type","text");
		newInput1.setAttribute("class","convfield_cubic");
		newInput1.setAttribute("onchange","calculateVolume();");
		newInput1.setAttribute("onkeypress","return checkNum(this, event)");
		
		//TD 2
		var newInput2 = document.createElement('input');
		newInput2.setAttribute("name","vol_length_"+total);
		newInput2.setAttribute("id","vol_length_"+total);
		newInput2.setAttribute("type","text");
		newInput2.setAttribute("class","convfield_cubic");
		newInput2.setAttribute("onchange","calculateVolume();");
		newInput2.setAttribute("onkeypress","return checkNum(this, event)");
				
		//TD 3
		var newInput3 = document.createElement('input');
		newInput3.setAttribute("name","vol_width_"+total);
		newInput3.setAttribute("id","vol_width_"+total);
		newInput3.setAttribute("type","text");
		newInput3.setAttribute("class","convfield_cubic");
		newInput3.setAttribute("onchange","calculateVolume();");
		newInput3.setAttribute("onkeypress","return checkNum(this, event)");
				
		//TD 4
		var newInput4 = document.createElement('input');
		newInput4.setAttribute("name","vol_height_"+total);
		newInput4.setAttribute("id","vol_height_"+total);
		newInput4.setAttribute("type","text");
		newInput4.setAttribute("class","convfield_cubic");
		newInput4.setAttribute("onchange","calculateVolume();");
		newInput4.setAttribute("onkeypress","return checkNum(this, event)");
	}

	var trElem, tdElem, txtNode;
	trElem = which.insertRow(1);
	trElem.id = "row_"+total;
	
	tdElem = trElem.insertCell(0);
	tdElem.appendChild(newInput1);
	
	tdElem = trElem.insertCell(1);
	tdElem.appendChild(newInput2);
	
	tdElem = trElem.insertCell(2);
	tdElem.appendChild(newInput3);
	
	tdElem = trElem.insertCell(3);
	tdElem.appendChild(newInput4);
	
	tdElem = trElem.insertCell(4);
	tdElem = trElem.insertCell(5);
	tdElem = trElem.insertCell(6);
	tdElem = trElem.insertCell(7);
}
function addRow2(){
	total = which = "";
	
	if(document.all){
		//ie
		which = getElement('calcTable_cubic').childNodes[0];
		total = which.childNodes.length-3;
		
		//TD 1
		var newInput1 = document.createElement('<input id="ctn2_plt_'+total+'" name="ctn2_plt_'+total+'" type="text" class="convfield_cubic" onchange="calculateVolume_cubic()" onkeypress="return checkNum(this, event)">');
		
		//TD 2
		var newInput2 = document.createElement('<input id="vol2_length_'+total+'" name="vol2_length_'+total+'" type="text" class="convfield_cubic" onchange="calculateVolume_cubic()"  onkeypress="return checkNum(this, event)">');	
		
		//TD 3
		var newInput3 = document.createElement('<input id="vol2_width_'+total+'" name="vol2_width_'+total+'" type="text" class="convfield_cubic" onchange="calculateVolume_cubic()"  onkeypress="return checkNum(this, event)">');
		
		//TD 4
		var newInput4 = document.createElement('<input id="vol2_height_'+total+'" name="vol2_height_'+total+'" type="text" class="convfield_cubic" onchange="calculateVolume_cubic()"  onkeypress="return checkNum(this, event)">');
	}else{
		//gecko
		which = getElement('calcTable_cubic').childNodes[1];
		total = which.childNodes.length-8;
		
		//TD 1
		var newInput1 = document.createElement('input');
		newInput1.setAttribute("name","ctn2_plt_"+total);
		newInput1.setAttribute("id","ctn2_plt_"+total);
		newInput1.setAttribute("type","text");
		newInput1.setAttribute("class","convfield_cubic");
		newInput1.setAttribute("onchange","calculateVolume_cubic();");
		newInput1.setAttribute("onkeypress","return checkNum(this, event)");
		
		//TD 2
		var newInput2 = document.createElement('input');
		newInput2.setAttribute("name","vol2_length_"+total);
		newInput2.setAttribute("id","vol2_length_"+total);
		newInput2.setAttribute("type","text");
		newInput2.setAttribute("class","convfield_cubic");
		newInput2.setAttribute("onchange","calculateVolume_cubic();");
		newInput2.setAttribute("onkeypress","return checkNum(this, event)");
				
		//TD 3
		var newInput3 = document.createElement('input');
		newInput3.setAttribute("name","vol2_width_"+total);
		newInput3.setAttribute("id","vol2_width_"+total);
		newInput3.setAttribute("type","text");
		newInput3.setAttribute("class","convfield_cubic");
		newInput3.setAttribute("onchange","calculateVolume_cubic();");
		newInput3.setAttribute("onkeypress","return checkNum(this, event)");
				
		//TD 4
		var newInput4 = document.createElement('input');
		newInput4.setAttribute("name","vol2_height_"+total);
		newInput4.setAttribute("id","vol2_height_"+total);
		newInput4.setAttribute("type","text");
		newInput4.setAttribute("class","convfield_cubic");
		newInput4.setAttribute("onchange","calculateVolume_cubic();");
		newInput4.setAttribute("onkeypress","return checkNum(this, event)");
	}

	var trElem, tdElem, txtNode;
	trElem = which.insertRow(1);
	trElem.id = "row_"+total;
	
	tdElem = trElem.insertCell(0);
	tdElem.appendChild(newInput1);
	
	tdElem = trElem.insertCell(1);
	tdElem.appendChild(newInput2);
	
	tdElem = trElem.insertCell(2);
	tdElem.appendChild(newInput3);
	
	tdElem = trElem.insertCell(3);
	tdElem.appendChild(newInput4);
	
	tdElem = trElem.insertCell(4);
	tdElem = trElem.insertCell(5);
	tdElem = trElem.insertCell(6);
	tdElem = trElem.insertCell(7);
}