// JavaScript Document
var price_per_skid = 50;		// pricing to $50 per pallet.
var price_liftgate = 75;
var price_insurance = 0.02;
var old_volume = 0;

var calculate = true;
var Pricings = new Object();

var curDestCity = "";
var curDestCountry = "";
var calc_error = "";



// VARIABLE "PAGE" MUST BE SET IN PAGE GENERATION

function ordestChanged( from )
{	
	if(oBw.ie6)
	{
		curDestCity = "";
		curDestCountry = "";
		var curZipCity = getHTML("zip_city");
		
		if( curZipCity.length == 0 || from == "origin")
		{
			setVal("zipcode",   "", document.body_form);
			setHTML("zip_city", "");
			
			setVisObj("tr_pickup", false);
			setVisObj("tr_pickup_spec", false);
			setVisObj("tr_pickup_dock", false);
			setVisObj("tr_pickup_exceed", false);
			setVisObj("tr_pickup_time", false);
		}
		
		
		
		setVisObj("loading", true);
		setHTML("loading", "Loading...");
		
		ajax = new myAjax();
		ajaxObj = ajax.create();
		ajaxObj.open("post", "loaddata.php", true);
		ajaxObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		ajaxObj.onreadystatechange = function () {
			if(ajaxObj.readyState == 4)
			{
				if (ajaxObj.status == 200) {
					loadData(ajaxObj.responseText);
					
				} else {
					setHTML("loading", "An error occurred");
					setTimeout(ordestChanged, 500);
				}
			}
		};
		sForm = ajax.getForm(document.body_form);
		sForm += "&from_ajax=1";
		ajaxObj.send( sForm );
	}
	else
	{
		switch(PAGE)
		{
			case "estimate":
				document.body_form.submit();
				break;
			case "booking":
				copy_booking_information( );
				document.body_form.submit( );
				break;
		}
	}
	
}

function loadData(response)
{
	eval(response);
	
	
	
	if(PAGE == "booking")
		destCountryObj = getObj("dest_country", document.body_form);
	else
		destCountryObj = getObj("dest_country");
	
	// current selected (or returned from calc)
	if(curDestCountry == "") curDestCountry = destCountryObj.value;
	
	destCountryObj.length = destCountries.length;
	
	destCountryObj.selectedIndex = 0;
	
	opt = destCountryObj.options;
	
	for(var i = 0; i < destCountries.length; i++)
	{
		opt[i].text = destCountries[i];
		opt[i].value = destCountries[i];
		if(destCountries[i] == curDestCountry)
			destCountryObj.selectedIndex = i;
	}

	if(PAGE == "booking")
		destCityObj = getObj("dest_city", document.body_form);
	else
		destCityObj = getObj("dest_city");
	
	// current selected (or returned from calc)
	if(curDestCity == "")	curDestCity = destCityObj.value;
	
	destCityObj.length = destCities.length;
	destCityObj.selectedIndex = 0;
	
	opt = destCityObj.options;
	
	for(var i = 0; i < destCities.length; i++)
	{
		opt[i].text = destCities[i];
		opt[i].value = destCities[i];
		if(destCities[i] == curDestCity)
			destCityObj.selectedIndex = i;
	}
	
	// RATE INFO
	setVisObj("tr_transit_time", (tt>3));
	setHTML("transit_time", tt);
	for(var i = 0; i < 3; i++)
		setHTML("rates_cbm"+i, "$" + rate_cbm1[i]);
	setHTML("rates_min", "$" + rate_min1);
	
	
	// LINEHAUL
	setVisObj("tr_haul",(price_haul1 > 0));
	
	if(PAGE == "estimate")
	{
		var zipCode = getVal("zipcode");
		var curZipCity = getHTML("zip_city", document.body_form);
		
		var pickupRequired = (zipCode.length == 5 && curZipCity.length > 0);
		
		var showWeightInput = (price_haul1 > 0 || pickupRequired);
		setVisObj("weight_input", showWeightInput);
		setVisObj("without_weight_input", !showWeightInput);
	}
	
	setVal("old_zipcode", "", document.body_form);
	setVal("old_origin", getVal("origin_city", document.body_form), document.body_form);
	setVal("old_dest_country", getVal("dest_country", document.body_form), document.body_form);
	setVal("old_dest_city", getVal("dest_city", document.body_form), document.body_form);
	
	if(PAGE == "booking")
	{
		setVal("old_origin", getVal("origin_city", document.body_form), document.booking_form);
		setVal("old_dest_country", getVal("dest_country", document.body_form), document.booking_form);
		setVal("old_dest_city", getVal("dest_city", document.body_form), document.booking_form);
		
		setVal("origin_city", getVal("origin_city", document.body_form), document.booking_form);
		setVal("dest_country", getVal("dest_country", document.body_form), document.booking_form);
		setVal("dest_city", getVal("dest_city", document.body_form), document.booking_form);
	}

	setVisObj("loading", false);
	
	
	// VIA
	setVisObj("tr_via", (curVia.length > 0) );
	setHTML("dest_via", curVia);
	
	Calc();
	
	if(calc_error.length > 0)
		alert(calc_error);
}


function getLetterById(id)
{
	switch(id)
	{
		case 1: return "A";
		case 2: return "B";
		case 3: return "C";
		case 4: return "D";
		case 5: return "E";
		case 6: return "F";
		default: return "";
	}
}

function Calc()
{
	var f = document.body_form;
	var total = 0;
	var t_skid = 0, t_insurance = 0;
	var t_seafreight = 0;
	var t_haul = 0;
	var calc_total_weight = 0;
	var calc_weight_dim;
	var calc_total_volume = 0;
	var calc_volume_dim;
	var t_pickup = 0, t_spec = 0, t_exceed = 0;

	// RATE PRICES
	var calc_rate_cbm1;  // rate price
	var calc_rate_cbm2;  // rate price (only if linehaul selected) 
	var calc_rate_min;	 // rate min. charge
	var rate_index = 0; // index (1+ cbm = 0, 3+ cbm = 1, 7+ cbm =2)
	var show_total = 0;
	
	// VOLUME & WEIGHT
	
	// volume
	calc_total_volume = formatFloat(f.total_volume.value, 3);
	if(isNaN(calc_total_volume))	calc_total_volume = 0;
	calc_volume_dim = f.volume_dim.value;
	
	if (calc_volume_dim == 'cbf')	calc_total_volume = calc_total_volume/35.31466;
		
	// weight
	calc_total_weight = getFloatVal("total_weight_calc");
	calc_weight_dim = getVal("weight_dim_calc", document.body_form);
	
	if (calc_weight_dim == 'kilos')	calc_total_weight = calc_total_weight*2.205;
	
	// ####################
	// ### <SEAFREIGHT> ###
	// ####################

	if(calc_total_volume <  1) t_seafreight = rate_min1;
	if(calc_total_volume >= 1) rate_index = 0;
	if(calc_total_volume >= 3) rate_index = 1;
	if(calc_total_volume >= 7) rate_index = 2;
	
	calc_rate_cbm1 = rate_cbm1[rate_index];
	calc_rate_min  = rate_min1;

	t_seafreight = Math.max(calc_rate_cbm1 * calc_total_volume, rate_min1);
	
	/* 
		THREE LINES BELOW COULD BE DELETED LATER
		THERE ARE ONLY TO BE SURE THAT RATE DISPLAYS CORRECTLY
	*/
		calc_rate_cbm1 = parseFloat(calc_rate_cbm1);
	
	document.getElementById("rate_cbm").innerHTML 	= calc_rate_cbm1.toFixed(2);
	document.getElementById("rate_cbf").innerHTML 	= formatFloat(calc_rate_cbm1/35.31466, 2);
	document.getElementById("rate_min").innerHTML	= calc_rate_min;
	
	// to show or not show
	// getting elements
	Pricings.doc  			= document.getElementById("div_doc_fee");
	Pricings.ls  			= document.getElementById("div_ls_fee");
	Pricings.bft  			= document.getElementById("div_bft_fee");
	Pricings.haul 			= document.getElementById("div_haul");
	Pricings.pickup			= document.getElementById("div_pickup");
	Pricings.pickup_spec	= document.getElementById("div_pickup_spec");
	Pricings.pickup_exceed  = document.getElementById("div_pickup_exceed");
	Pricings.skid			= document.getElementById("div_skid");
	Pricings.insurance		= document.getElementById("div_insurance");
	
	if(calc_total_volume > 0)
	{
		document.getElementById("rate_price_titles").style.display 	= "none";
		document.getElementById("rate_prices").style.display 	 	= "";
		document.getElementById("seafreight_titles").style.display 	= "none";
		document.getElementById("seafreight").style.display 	 	= "";
		
		// setting black color for all prices
		for(var obj in Pricings)
			if(Pricings[obj] != null) Pricings[obj].className = "";	
		
	}
	else
	{
		document.getElementById("rate_price_titles").style.display 	= "";
		document.getElementById("rate_prices").style.display 	 	= "none";
		document.getElementById("seafreight_titles").style.display 	= "";
		document.getElementById("seafreight").style.display 	 	= "none";
		// setting gray color for all prices
		for(var obj in Pricings)
			if(Pricings[obj] != null) Pricings[obj].className = "gray";
	}
	// #####################
	// ### </SEAFREIGHT> ###
	// #####################

	
	// ##################
	// ### <LINEHAUL> ###
	// ##################
	
	if (price_haul1 > 0 && calc_total_weight > 0)
	{
		calc_rate_cbm2 = rate_cbm2[rate_index];

		t_haul = Math.ceil(price_haul1 * calc_total_weight);
		t_haul = Math.max(Math.ceil(price_haul1*300/10)*10, t_haul);
	
		t_haul2 = Math.ceil(price_haul2 * calc_total_weight);
		t_haul2 = Math.max(Math.ceil(price_haul2*300/10)*10, t_haul2);
		
		var t_seafreight2 = Math.max(Math.ceil(calc_rate_cbm2*calc_total_volume), rate_min2);
		
		if(  (t_seafreight2 + t_haul2) < (t_haul + t_seafreight))
		{
			t_haul = t_haul2;
			t_seafreight = t_seafreight2;
			prices_lsfee1 = prices_lsfee2;
			
			
			var RateCbm = Math.ceil(calc_rate_cbm2);
			var floatRateCbm = RateCbm.toFixed(2);
			document.getElementById("rate_cbm").innerHTML = floatRateCbm.toString();
			document.getElementById("rate_cbf").innerHTML = formatFloat(calc_rate_cbm2/35.31466, 2);
			document.getElementById("rate_min").innerHTML = Math.ceil(rate_min2).toString();
			//document.getElementById("linehaul_city").innerHTML = "L.A. "; //add by rkapil
			document.getElementById("div_ls_fee").innerHTML	= "$" + prices_lsfee2;
			
			// VIA
			setVisObj("tr_via", (dest_via2.length > 0) );
			setHTML("dest_via", dest_via2);

			
			calc_rate_cbm1 = calc_rate_cbm2;
			calc_rate_min = rate_min2;
		}
		else
		{
			var RateCbm = Math.ceil(calc_rate_cbm1);
			var floatRateCbm = RateCbm.toFixed(2);
			document.getElementById("rate_cbm").innerHTML = floatRateCbm.toString();
			document.getElementById("rate_cbf").innerHTML = formatFloat(calc_rate_cbm1/35.31466, 2);
			document.getElementById("rate_min").innerHTML = Math.ceil(rate_min1).toString();
			//document.getElementById("linehaul_city").innerHTML = "NYC ";//add by rkapil
			document.getElementById("div_ls_fee").innerHTML	= "$" + prices_lsfee1;
			
			// VIA
			setVisObj("tr_via", (dest_via1.length > 0) );
			setHTML("dest_via", dest_via1);

		}
		
		document.getElementById("tr_haul").style.display = (t_haul > 0)?'':'none';
		document.getElementById("div_haul").innerHTML = '$'+formatFloat(t_haul.toString(),2)+'';
	
	}
	
	// if weight is not entered
	if (price_haul1 > 0 && calc_total_weight == 0)	document.getElementById("div_haul").innerHTML = "$0";

	// ###################
	// ### </LINEHAUL> ###
	// ###################
	
	// RATES
	calc_rate_cbm1 = calc_rate_cbm1.toFixed(2);
	setHTML("rate_cbm", calc_rate_cbm1, document.body_form);
	setHTML("rate_cbf", formatFloat(calc_rate_cbm1/35.31466, 2), document.body_form);
	setHTML("rate_min", calc_rate_min, document.body_form);

	
	
	// BOSTON FEE
	setVisObj("tr_bftfee", (prices_bftfee > 0) );
	
	// LS
	setVisObj("tr_ls_fee",(prices_lsfee1 > 0));
	setHTML("div_ls_fee", "$" + prices_lsfee1);
	trLsFeeObj = getObj("tr_ls_fee");
	trLsFeeObj.className = (prices_lsfee1 > 0)?"":"gray";

	
	// SEAFREIGHT OUTPUT
	t_seafreight = Math.ceil(t_seafreight);
	document.getElementById("seafreight").innerHTML = "$" + Math.ceil(t_seafreight);
	
	
	// ################
	// ### <PICKUP> ###
	// ################
	// only reFormatted (no changes)
	
	zipcodeVal = getVal("zipcode", document.body_form);
	zipCity	   = getHTML("zip_city", document.body_form);
	
	if(zipcodeVal.length == 5 && zipCity.length > 0)
	{	
		var curLetter = 2;
		if (f.pickup_type.selectedIndex == 0 && calc_total_weight > 0 &&
		calc_total_volume > 0 && f.pickup_type.options.length > 2)
		{
			f.pickup_type.selectedIndex = 2;
		}
		
		if (f.pickup_type.options[f.pickup_type.selectedIndex].value != "res" &&
		f.pickup_type.options[f.pickup_type.selectedIndex].value != "com")
		{
			f.pickup_spec.selectedIndex = 0;
			f.pickup_dock.selectedIndex = 0;
			f.pickup_exceed.selectedIndex = 0;
		}
		if (f.pickup_type.options[f.pickup_type.selectedIndex].value != "com" ||
			f.pickup_spec.selectedIndex < 1)
			f.pickup_dock.selectedIndex = 0;
		
		if (f.pickup_type.options[f.pickup_type.selectedIndex].value != "res" &&
		f.pickup_dock.options[f.pickup_dock.selectedIndex].value != "N")
			f.pickup_exceed.selectedIndex = 0;
		
		trPickSpecObj = getObj("tr_pickup_spec");
		trPickSpecObj.style.display = (f.pickup_type.options[f.pickup_type.selectedIndex].value == "res" ||
		  f.pickup_type.options[f.pickup_type.selectedIndex].value == "com")?'':'none';
		
		// TIME WINDOW TABLE - display
		var showTimeWindowTable = false;
		pSVal = f.pickup_spec.value;
		trTimeTable = getObj("tr_pickup_time");
		if(trTimeTable != null)
		{
			showTimeWindowTable = (pSVal == "spec" || pSVal == "sat" || pSVal == "sun");
			setVisObj("tr_pickup_time", showTimeWindowTable);
		}
		// curLetter
		if(showTimeWindowTable) curLetter++;	
		
		
		document.getElementById('tr_pickup_dock').style.display = (f.pickup_type.options[f.pickup_type.selectedIndex].value == "com" &&
		  f.pickup_spec.selectedIndex > 0)?'':'none';
		document.getElementById('tr_pickup_exceed').style.display = (f.pickup_type.options[f.pickup_type.selectedIndex].value == "res" ||
			f.pickup_dock.options[f.pickup_dock.selectedIndex].value == "N")?'':'none';
		
		
		
		curLetter++;
		pickupDockObj = getObj("pickup_dock", document.body_form);
		
		pickupDockObj.options[0].text = getLetterById(curLetter) + ". Verify Loading Capabilites";
		
		if (document.getElementById('tr_pickup_dock').style.display != 'none')
			curLetter++;
	
		f.pickup_exceed.options[0].text = getLetterById(curLetter) + '. Verify Weight Class';
		/*else
			f.pickup_exceed.options[0].text = 'E. Verify Weight Class';*/
		
		if ((f.pickup_type.options[f.pickup_type.selectedIndex].value == "res" ||
				f.pickup_type.options[f.pickup_type.selectedIndex].value == "com") &&
				calc_total_volume > 0 && calc_total_weight > 0 )
		{
		 
			if (f.pickup_type.options[f.pickup_type.selectedIndex].value == "res")
			{
				if (calc_total_weight < 1000)
					t_pickup = calc_total_weight*(pr_lbs100_res+pr_lbs100_res_tr);
				else if (calc_total_weight >= 1000 && calc_total_weight < 3000)
					t_pickup = calc_total_weight*(pr_lbs1000_res+pr_lbs1000_res_tr);
				else
					t_pickup = calc_total_weight*(pr_lbs3000_res+pr_lbs3000_res_tr);
				if (t_pickup < pr_min_res && calc_total_weight > 0)
				t_pickup = pr_min_res;
			}
			else
			{
				if (calc_total_weight < 1000)
				{
					t_pickup = calc_total_weight*(pr_lbs100_com + pr_lbs100_com_tr);
				}
				else if (calc_total_weight >= 1000 && calc_total_weight < 3000)
					t_pickup = calc_total_weight*(pr_lbs1000_com + pr_lbs1000_com_tr);
				else
					t_pickup = calc_total_weight*(pr_lbs3000_com + pr_lbs3000_com_tr);
				if (t_pickup < pr_min_com && calc_total_weight > 0)
					t_pickup = pr_min_com;
			}
		
			if (f.pickup_spec.options[f.pickup_spec.selectedIndex].value == "spec")
				t_spec = pr_spec;
			else if (f.pickup_spec.options[f.pickup_spec.selectedIndex].value == "sat")
				t_spec = pr_sat;
			else if (f.pickup_spec.options[f.pickup_spec.selectedIndex].value == "sun")
				t_spec = pr_sun;
				
			
			
			if (f.pickup_exceed.options[f.pickup_exceed.selectedIndex].value == "Y")
				t_exceed = price_liftgate;
			
			t_spec   = Math.ceil(t_spec);
			t_exceed = Math.ceil(t_exceed);
			t_pickup = Math.ceil(t_pickup);
			
			document.getElementById('div_pickup').innerHTML = '$'+formatFloat(t_pickup.toString(),2);
			document.getElementById('div_pickup_spec').innerHTML = '$'+formatFloat(t_spec.toString(),2)+'';
			document.getElementById('div_pickup_exceed').innerHTML = '$'+formatFloat(t_exceed.toString(),2)+'';
			
			t_pickup += t_spec;
			t_pickup += t_exceed;
		}
		else
		{
			document.getElementById('div_pickup').innerHTML = '$0';
			document.getElementById('div_pickup_spec').innerHTML = '$0';
			document.getElementById('div_pickup_exceed').innerHTML = '$0';
		}
	}

	// #################
	// ### </PICKUP> ###
	// #################
	
	
	// ##################
	// ### <SERVICES> ###
	// ##################
	
	if (calc_total_volume != 0 && Math.ceil(calc_total_volume/1.9) < 50)
	if(f.skid_n.selectedIndex < 1)	f.skid_n.selectedIndex = Math.ceil(calc_total_volume/1.9);
	
	old_volume = calc_total_volume;
	
	document.getElementById('tr_skid').style.display = (f.services.options[f.services.selectedIndex].value.indexOf('skid') >= 0)?'':'none';
	document.getElementById('tr_insurance').style.display = (f.services.options[f.services.selectedIndex].value.indexOf('insurance') >= 0)?'':'none';
	t_skid = Math.ceil((f.services.options[f.services.selectedIndex].value.indexOf('skid') >= 0)?
	price_per_skid*parseInt(f.skid_n.options[f.skid_n.selectedIndex].text):0);
	
	if(f.insurance_value.value.length > 0 && f.insurance_value.value.indexOf("$ Replacement value") == -1)
		f.insurance_value.value = getFloat(f.insurance_value.value.replace(",", ""));
	
		
	t_insurance = Math.ceil((f.services.options[f.services.selectedIndex].value.indexOf('insurance') >= 0)?
	price_insurance*parseFloat(FormatFloat(f.insurance_value.value,2,false)):0);
	if (t_insurance > 0 && t_insurance < 30)
	t_insurance = 30;
	document.getElementById('div_skid').innerHTML ='$' + Math.ceil(t_skid);
	document.getElementById('div_insurance').innerHTML = '$'+Math.ceil(t_insurance);
	
	//document.getElementById('div_sea_charge').innerHTML = '<b>$'+FormatFloat(t_seafreight.toString(),0,false)+'</b>';
	
	t_service = t_insurance+t_skid;
	
	// ###################
	// ### </SERVICES> ###
	// ###################
	
	// ###############
	// ### <TOTAL> ###
	// ###############
	
	total += t_seafreight;
	total += prices_docfee;
	total += prices_lsfee1;
	total += prices_bftfee;
	total += t_haul;
	total += t_skid;
	total += t_insurance;
	total += t_pickup;
	
	total = Math.ceil(total);

	if ( calc_total_volume <= 0 || !calculate )
	total = 0;

	// ################
	// ### </TOTAL> ###
	// ################

	f.calc_total.value = Math.ceil( total );
	
	
	// SHOW OR NOT TO SHOW TOTAL PRICE
	show_total = ( calc_total_volume > 0 && wei_vol() );
	
	if(!show_total) total = 0;
	
	document.getElementById( 'div_total' ).innerHTML = FormatFloat( total.toString( ), 2, false );
}

function addRowToTable()
{
  var tbl = document.getElementById('wm_table');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow-1;
  var index		= lastRow - 3;
  var row = tbl.insertRow(iteration);
  row.id = "row" + index;
  row.className = "calc";
  row.style.color = "#000";
  
  // pieces cell
  var cellPieces = row.insertCell(0);
  var el = document.createElement("input");
  el.type = "text";
  el.name = "pieces" + index;
  el.id   = "pieces" + index;
  el.style.width = "30px";
  el.className = "text";
  el.onchange = calcMes;
  cellPieces.appendChild(el);
  
  // weight cell
  var cellWeight = row.insertCell(1);
  var el  = document.createElement("input");
  el.type = "text";
  el.name = "weight" + index;
  el.id   = "weight" + index;
  el.style.width = "30px";
  el.className = "text";
  el.onchange = calcMes;
  cellWeight.appendChild(el);
  
  // length
  var cellLength = row.insertCell(2);
  var el  = document.createElement("input");
  el.type = "text";
  el.name = "length" + index;
  el.id   = "length" + index;
  el.style.width = "30px";
  el.className = "text";
  el.onchange = calcMes;
  cellLength.appendChild(el);
  
  // width
  var cellWidth = row.insertCell(3);
  var el  = document.createElement("input");
  el.type = "text";
  el.name = "width" + index;
  el.id   = "width" + index;
  el.style.width = "30px";
  el.className = "text";
  el.onchange = calcMes;
  cellWidth.appendChild(el);
  
  // height
  var cellWeight = row.insertCell(4);
  var el  = document.createElement("input");
  el.type = "text";
  el.name = "height" + index;
  el.id   = "height" + index;
  el.style.width = "30px";
  el.className = "text";
  el.onchange = calcMes;
  cellWeight.appendChild(el);
  
  // volume output
  var cellDWeight = row.insertCell(5);
  cellDWeight.id  = "d_weight" + index;
  
  var cellCWeight = row.insertCell(6);  
  cellCWeight.id  = "c_weight" + index;
  
  // hidden element d_weight_row_total by rajiv
  var celld_weight_row_total = row.insertCell(7);
  var el  = document.createElement("input");
  el.type = "hidden";
  el.name = "d_weight_row_total" + index;
  el.id   = "d_weight_row_total" + index;
  celld_weight_row_total.appendChild(el);

  
}

function removeRowFromTable()
{
  var tbl = document.getElementById('wm_table');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 2);
}


function changeRowState()
{
	rows = getFloatVal("rows_to_add");
	shownRows += rows;
	
	if(!oBw.ie6)
		shownRows = Math.min(5, shownRows);
	else
		shownRows = Math.min(400, shownRows);
		
	for(var i = 2; i < shownRows+1; i++)
	{
		row = getObj("row" + i);
		if(row != null) setVisObj("row" + i, true)
		else if(oBw.ie6)	addRowToTable();
	}
	
	if(shownRows > 5 && !oBw.ie6)
	{
		setVisObj("calc_add_row", false);		
	}
	setVal("shown_rows", shownRows);
}
function calcMes()
{ 	var d_weight_row_total=0;
	var pieces = 0;
	var weight = 0;
	var dWeight = 0; // dimensional weight
	var cWeight = 0;
	var length = 0;
	var width = 0;
	var height = 0;
	var volume = 0;
	
	var volume_cbf = 0;
	
	var total_volume = 0;
	var total_weight = 0;
	var total_cweight = 0;
	var showWeight = false;
	
	var volume_dim = getVal("wm_volume_dim");
	var weight_dim = getVal("wm_weight_dim");
	
//alert("shownRows"+shownRows);
	for(var i = 0; i < (shownRows+1); i++)
	{	//alert("shownRowsA--"+shownRows);//edited by rkapil
		pieces = parseFloat(document.getElementById("pieces" + i).value);//;getFloatVal("pieces" + i,'');
		weight = parseFloat(document.getElementById("weight" + i).value);//getFloatVal("weight" + i,'');
		length =  parseFloat(document.getElementById("length" + i).value);//getFloatVal("length" + i,'');
		width  =  parseFloat(document.getElementById("width" + i).value);//getFloatVal("width" + i,'');
		height =  parseFloat(document.getElementById("height" + i).value);//getFloatVal("height" + i,'');
		
		
		
		volume  = length * width * height * pieces; 
		weight *= pieces;
		
		// units conversions
		if(volume_dim == "cm")		volume = convUnits(volume, "ccm", "cInch");
		if(weight_dim == "kgs") 	weight = convUnits(weight, "kgs", "lbs");
		
		
		
		// dimensional weight
		/*
		
		ANNOTATION: chargeable weight calculating is disabled.
					dweight id is used now to display Volume in CBM
					, not for Dimensional Weight (/ 250)
		
		
		dWeight = volume/250;
		
		// chargeable weight
		cWeight = Math.max(dWeight, weight);*/
		cWeight = weight;
		
		showWeight = (dWeight > 0 || weight > 0);
		
		// Weight output
		if(showWeight)
		{
			//setHTML("d_weight" + i, formatFloat(dWeight, 0) + " lbs"); // for chargeable weight
			setHTML("c_weight" + i, formatFloat(cWeight, 0));
		}
		else
		{
			//setHTML("d_weight" + i, "&nbsp;");
			setHTML("c_weight" + i, "&nbsp;");
		} 
		if(volume > 0)
		{
			setHTML("d_weight" + i, formatFloat(convUnits(volume, "cInch", "cbm"), 3));
			if("d_weight"+i)
			{	//alert("d_weight"+i);
				document.getElementById("d_weight_row_total"+i).value=formatFloat(convUnits(volume, "cInch", "cbm"), 3);
			}//added by rajiv
				//alert(document.getElementById("d_weight_row_total" + i).value);
				
		}
		else	{		
					setHTML("d_weight" + i, "&nbsp;");
		}
		
		// summ total volume and weight
		total_volume  += volume; //alert("volume:"+volume+"--"+"total_volume"+total_volume);
		total_weight  += weight;
		total_cweight += cWeight;


	


	//}//for loop

	
	if(total_weight > 0)
	{
		total_cweight = formatFloat(total_cweight, 0)
		setHTML("total_cweight", total_cweight + " lbs");
		
		weight_dim = getObj("weight_dim_calc");
		setVal("total_weight_calc",  formatFloat(total_cweight, 0));
		if(weight_dim.value != "lbs")	weight_dim.selectedIndex = "lbs";
		
	}
	else
	{
		//setVal("total_weight_calc", "0");
		//setHTML("total_cweight", "&nbsp;");
	}
	if(total_volume > 0)
	{
		volume_cbm = formatFloat(convUnits(total_volume, "cInch", "cbm"), 3);
		volume_cbf = formatFloat(convUnits(total_volume, "cInch", "cbf"), 1);
		
		vol_dim = getVal("volume_dim_calc");

		// Values for Estimated calc
		setVal("total_volume_calc", ((vol_dim == "cbm")?volume_cbm:volume_cbf));
		
		setHTML("total_volume_cbm", volume_cbm + " cbm");
		setHTML("total_volume_cbf", volume_cbf + " cbf");
	}
	else
	{
		//setHTML("total_volume_cbm", "&nbsp;");
		//setHTML("total_volume_cbf", "&nbsp;");
		//setVal("total_volume_calc", "0");
	}
	
	total_volume = getFloat(total_volume);
	
	weight_lbs = total_cweight;
	
	Calc();
}//for loop
}
