// Prices

function getService( ) {
	var f = document.body_form;
	var zipcode = f.zipcode.value;
	var service = "DOOR";
	if ( zipcode.length == 0 ) {
		service = "CFS";
		if (f.old_origin.value.substring(f.old_origin.value.length - 1, f.old_origin.value.length) == "*")
			service = "Terminal";
	}
	else if ( zipcode.length > 0 && ( f.pickup_type.options[ f.pickup_type.selectedIndex ].value == "no" || f.pickup_type.options[ f.pickup_type.selectedIndex ].value == "" ) )
		service = "CFS";
	return service;
}

function wei_vol( ) {
	var f = document.body_form;
	var temp = true;
	if ( parseFloat( f.total_weight.value ) == 0 && getService( ) != "CFS" )
		temp = false;
	return temp;
}

function FormatFloat(str,digits,all_digits)
{
  var i,s,f,c,ch;

  s = str;
  for (i=0; i < s.length; i++)
    if (s.charAt(i) == ',')
      s = s.substring(0,i)+'.'+s.substring(i+1,s.length);

  if (isNaN(s) || str.length < 1)
    f = 0;
  else
    f = parseFloat(s);
  s = f.toString();
  if (s == 'NaN')
    s = '0';

  if (s.indexOf('.') > 0)
  {
    if (s.length -  s.indexOf('.')-1 > digits)
    {
      s = s.substring(0,s.indexOf('.')+digits+1);
      f = parseFloat(s);
      ch = '0.';
      for (i=0; i < digits-1; i++)
        ch = ch+'0';
      ch = ch+'1';
      f = f+parseFloat(ch);
      s = f.toString();
      if (s.indexOf('.') >= 0)
        s = s.substring(0,s.indexOf('.')+digits+1);
    }
    else
    {
      if (all_digits == true)
      {
        for (i=s.length-1; i <= s.indexOf('.')+digits; i++)
          s = s+'0';
      }
    }
  }
  else
  {
    if (digits > 0 && all_digits == true)
    {
      s = s+'.';
      for (i=0; i < digits; i++)
        s = s+'0';
    }
  }
  if (digits == 0 && s.indexOf('.') >= 0)
    s = s.substring(0,s.indexOf('.'));
  return s;
}

function CountTotal() {
  var i,u_count,u_width,u_length,u_height,u_value;
  var total_volume = 0, total_count = 0, total_weight = 0;
  var vol;
  var f = document.body_form;

  	for (i=0; i < parseInt(f.row_count.value); i++)
	{
	    eval("u_value = f.u_value"+i+";");
	    eval("u_count = f.u_count"+i+".value;");
    	eval("u_width = f.u_width"+i+".value;");
    	eval("u_length = f.u_length"+i+".value;");
    	eval("u_height = f.u_height"+i+".value;");
	    eval("u_weight = f.u_weight"+i+"");

    	u_width = FormatFloat(u_width,2,false);
    	u_length = FormatFloat(u_length,2,false);
    	u_height = FormatFloat(u_height,2,false);
    	u_value.value = FormatFloat(u_value.value,3,false);
		u_weight.value = FormatFloat(u_weight.value,0,false);

		vol = FormatFloat(u_value.value,3,false);
	//	lbs = FormatFloat(u_weight.value,2,false);
		if (parseFloat(u_width) > 0 && parseFloat(u_length) > 0 && parseFloat(u_height) > 0)
		{
			if (f.dim_in.selectedIndex == 1)
				vol = FormatFloat(u_width * u_length * u_height * u_count / Math.pow(100,3), 3, false);
			else
				vol = FormatFloat(u_width * u_length * u_height * u_count * 16.38706 / Math.pow(100,3), 3, false);
			eval("f.u_value"+i+".value = "+vol+";");
		}
		else
		{
			eval("f.u_width"+i+".value = 0;");
			eval("f.u_height"+i+".value = 0;");
			eval("f.u_length"+i+".value = 0;");
		}
		if (u_count == "")
		{
			eval("f.u_count"+i+".value = 0;");
			u_count = 0;
		}
		if (u_weight.value == 0)
			eval("f.u_weight"+i+".value = 0;");
		if (f.wei_in.selectedIndex == 1)
    		lbs = FormatFloat(u_weight.value * 2.205 * u_count,0,false);
		else
    		lbs = FormatFloat(u_weight.value * u_count,0,false);
		total_volume += parseFloat(vol);
		total_weight += parseFloat(lbs);
		total_count += parseInt(u_count);
	}

/*  if (f.volume_dim.options[f.volume_dim.selectedIndex].value == 'cbm')
		f.total_volume.value = FormatFloat(total_volume, 3, false);
  else
		f.total_volume.value = FormatFloat(total_volume / 35.31466, 3, false);

  if (f.weight_dim.options[f.weight_dim.selectedIndex].value == 'lbs')
    	f.total_weight.value = FormatFloat(total_weight,0,false);
  else
	    f.total_weight.value = FormatFloat(total_weight / 2.205, 0, false);*/
		
  if (f.volume_dim.value == 'cbm')
		f.total_volume.value = FormatFloat(total_volume, 3, false);
  else
		f.total_volume.value = FormatFloat(total_volume / 35.31466, 3, false);
  if (f.weight_dim.value == 'lbs')
    	f.total_weight.value = FormatFloat(total_weight,0,false);
  else
	    f.total_weight.value = FormatFloat(total_weight / 2.205, 0, false);

  b_total_pieces.innerHTML = total_count;
  b_total_volume.innerHTML = FormatFloat(total_volume,3,false)+' cubic meters';
  b_total_weight.innerHTML = FormatFloat(total_weight,0,false)+' lbs';
  
 	
}

function RecalcData()
{
  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;

  // Prepare volume data
  calc_total_volume = parseFloat(FormatFloat(f.total_volume.value,3,false));
  if (f.volume_dim.type == 'hidden')
    calc_volume_dim = f.volume_dim.value;
  else
    calc_volume_dim = f.volume_dim.options[f.volume_dim.selectedIndex].value;
  if (calc_volume_dim == 'cbf')
    calc_total_volume = calc_total_volume/35.31466;
  // Prepare weight data
  calc_total_weight = parseFloat(FormatFloat(f.total_weight.value,0,false));
  if (f.weight_dim.type == 'hidden')
    calc_weight_dim = f.weight_dim.value;
  else
    calc_weight_dim = f.weight_dim.options[f.weight_dim.selectedIndex].value;
  if (calc_weight_dim == 'kilos')
    calc_total_weight = calc_total_weight*2.205;

  // Calculate T2T rate
  t_seafreight = Math.ceil(rate_cbm1*calc_total_volume);
  if (t_seafreight < rate_min1)
    t_seafreight = Math.ceil(rate_min1);

  // Calculate Line Haul
  if (price_haul1 > 0 && calc_total_weight > 0)
  {
	 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(rate_cbm2*calc_total_volume), rate_min2);
	 
	 if(  (t_seafreight2 + t_haul2) < (t_haul + t_seafreight)  )
	 {
		 t_haul = t_haul2;
		 t_seafreight = t_seafreight2;
		 document.getElementById("rate_cbm").innerHTML = Math.ceil(rate_cbm2).toString();
 		 document.getElementById("rate_cbf").innerHTML = FormatFloat((rate_cbm2/35.31466).toString(), 2, false);
		 document.getElementById("rate_min").innerHTML = Math.ceil(rate_min2).toString();
		 document.getElementById("linehaul_city").innerHTML = "L.A.";
	 }
	 else
	 {
		 document.getElementById("rate_cbm").innerHTML = Math.ceil(rate_cbm1).toString();
 		 document.getElementById("rate_cbf").innerHTML = FormatFloat((rate_cbm1/35.31466).toString(), 2, false);
		 document.getElementById("rate_min").innerHTML = Math.ceil(rate_min1).toString();
		 document.getElementById("linehaul_city").innerHTML = "NYC";
	 }
	 
	 
	 document.getElementById("tr_haul").style.display = (t_haul > 0)?'':'none';
 	 document.getElementById("div_haul").innerHTML = '<b>$'+FormatFloat(t_haul.toString(),2,false)+'</b>';

  }

  // Pickup services
  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;
  document.getElementById('tr_pickup_spec').style.display = (f.pickup_type.options[f.pickup_type.selectedIndex].value == "res" ||
                                  f.pickup_type.options[f.pickup_type.selectedIndex].value == "com")?'':'none';
  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';
  if (document.getElementById('tr_pickup_dock').style.display == 'none')
    f.pickup_exceed.options[0].text = 'C. Verify Weight Class';
  else
    f.pickup_exceed.options[0].text = 'D. Verify Weight Class';
 
  if (f.pickup_type.options[f.pickup_type.selectedIndex].value == "res" ||
      f.pickup_type.options[f.pickup_type.selectedIndex].value == "com")
  {
          
		  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 = '<b>$'+FormatFloat(t_pickup.toString(),2,false)+'</b>';
    document.getElementById('div_pickup_spec').innerHTML = '<b>$'+FormatFloat(t_spec.toString(),2,false)+'</b>';
    document.getElementById('div_pickup_exceed').innerHTML = '<b>$'+FormatFloat(t_exceed.toString(),2,false)+'</b>';

          t_pickup += t_spec;
          t_pickup += t_exceed;
  }
  else
  {
    document.getElementById('div_pickup').innerHTML = '<b>$0</b>';
    document.getElementById('div_pickup_spec').innerHTML = '<b>$0</b>';
    document.getElementById('div_pickup_exceed').innerHTML = '<b>$0</b>';
  }

  // Additional Services
  if (calc_total_volume != 0 && Math.ceil(calc_total_volume/1.9) < 50)
    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);
  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 = FormatFloat(t_skid.toString(),0,false);
  document.getElementById('div_insurance').innerHTML = '<b>$'+FormatFloat(t_insurance.toString(),0,false)+'</b>';

  document.getElementById('div_sea_charge').innerHTML = '<b>$'+FormatFloat(t_seafreight.toString(),0,false)+'</b>';

  t_service = t_insurance+t_skid;

//  div_service.innerHTML = '<b>$'+FormatFloat(t_service.toString(),0,false)+'</b>';

  // Calculate totals
  total += t_seafreight;
  total += prices_docfee;
  total += prices_bftfee;
  total += t_haul;
  total += t_skid;
  total += t_insurance;
  total += t_pickup;

  if ( calc_total_volume <= 0 || !calculate )
    total = 0;
 
 f.calc_total.value = Math.ceil( total );
 document.getElementById( 'div_total' ).innerHTML = FormatFloat( total.toString( ), 2, false );
	
}

function ShowCalc( ) {
  document.body_form.show_calc.value = "Y";
  document.body_form.submit();
}

function HideCalc( ) {
  document.body_form.show_calc.value = "N";
  document.body_form.submit();
}


var wmCalc = 1;
function switchWMCalc()
{
	if(wmCalc == 0)
	{
		setVisObj("chargeable_wm", true);
		setVisObj("estimated_wm", false);
		wmCalc = 1;
		calcMes();
	}
	else
	{
		setVisObj("estimated_wm", true);
		setVisObj("chargeable_wm", false);
		wmCalc = 0;
	}
	
}
function CountTotal1( ) {
  CountTotal( );
  self.close( );
}

function TotalSubmit( ) {
  return true;
}

function no_skid() {
	var s = document.body_form.services;
	if (document.body_form.pickup_type.selectedIndex == 1) {
		s.options[2] = null;
		s.options[3] = null;
	}
	else {
		s.options[2] = null;
		s.options[2] = new Option("Skid & wrap", "skid", false, false);
		s.options[3] = new Option("Insurance", "insurance", false, false);
		s.options[4] = new Option("Insurance + Skid & wrap", "skid+insurance", false, false);
	}
}

function PickupChange( ) {
	var f = document.body_form;
	if ( ( f.pickup_type.selectedIndex == 2 || f.pickup_type.selectedIndex == 3 ) && f.total_weight.value == 0 ) {
		alert( "Pickup charges are based on weight of cargo.\nPLEASE, ENTER WEIGHT IN LBS OR KILOS" );
		f.pickup_type.selectedIndex = 0;
	}
	if ( getService( ) == "Terminal" && f.total_weight.value == 0 )
		calculate = false;
	if ( getService( ) == "Terminal" && f.total_weight.value != 0 )
		calculate = true;
	Calc();
	//RecalcData( );
}


function Calculate( ) {
	var f = document.body_form;
	if ( getService( ) == "Terminal" && f.total_weight.value == 0 ) {
		alert( 'You should enter weight in lbs or kilos.' );
		return;
	}
	else if ( getService( ) == "Terminal" && f.total_volume.value == 0 ) {
		alert( 'You should enter volume.' );
		return;
	}
	else if ( f.zipcode.value.length > 0 && f.total_volume.value == 0 && f.total_weight.value == 0 )
		alert( 'If PICKUP SERVICE REQUIRES then enter VOLUME and WEIGHT.\nIf NO PICKUP Service requires then leave the Origin Zip Code Blank and click OK.' );
	else if ( f.zipcode.value.length > 0 && f.total_volume.value == 0 && f.total_weight.value > 0 )
		alert( 'Ocean freight charges are based on total chargable VOLUME of cargo.\nPlease, enter VOLUME in Cubim Meters or Cubic Foot.' );
	else if ( f.zipcode.value.length > 0 && f.total_volume.value > 0 && f.total_weight.value == 0 ) {
		alert( 'Pickup charges are based on weight of cargo.\nPLEASE, ENTER WEIGHT IN LBS OR KILOS' );
		Calc( );
		// Recalc();
	}
	else if ( f.zipcode.value.length == 0 && f.total_volume.value == 0 )
		alert( '** For shipments self-delivered to CFS (Carrier\'s Container Freight Station) Weight is NOT a pricing factor. Please, enter total VOLUME of your cargo in Cubic Meters or Cubic Foot.' );
	else
		Calc();

}

function eQuote( ) {
	if( parseFloat( document.body_form.calc_total.value ) > 0 && wei_vol( ) ) {
		document.getElementById( 'email_td' ).style.display = '';
		document.body_form.email.focus( );
		document.getElementById( 'email_quote' ).style.visibility = 'hidden';
	}
	else {
		alert( 'You should complete the form at first.' );
		document.location = "#top";
	}
	
}
