// Prices
function wei_vol() {
	
	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";
	
	var temp = true;
	
	weight = parseFloat( f.total_weight.value );
	
	if ( ( weight == 0 || isNaN(weight) ) && (service != "CFS")) // || 
	{	
		temp = false;
	}
	*/
	weight = getFloat(f.total_weight.value);

	if(weight == 0)	temp = false;
	else			temp = true;
	return temp;
}

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

  s = str;
  if(s != null)
  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.booking_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);
	}

	var f = document.body_form;
  
/*	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);

  document.getElementById("b_total_pieces").innerHTML = total_count;
  document.getElementById("b_total_volume").innerHTML = FormatFloat(total_volume,3,false)+' cubic meters';
  document.getElementById("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;
		var RateCbm = Math.ceil(rate_cbm2);
		var floatRateCbm = RateCbm.toFixed(2);
		 document.getElementById("rate_cbm").innerHTML = floatRateCbm.toString();
		 document.getElementById("testValue").value =  Math.ceil(rate_cbm2).toString() + "1";
 		 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
	 {
			var RateCbm = Math.ceil(rate_cbm1);
			var floatRateCbm = RateCbm.toFixed(2);
		 document.getElementById("rate_cbm").innerHTML = floatRateCbm.toString();		 document.getElementById("testValue").value =  Math.ceil(rate_cbm1).toString() + "2";
 		 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 = FormatFloat(t_insurance.toString(),0,false);

  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)
    total = 0;

  f.calc_total.value = Math.ceil(total);
  // Show total
	document.getElementById("div_total").innerHTML = FormatFloat(total.toString(),2,false);
	OriginDetailsChange( );
	DestinationDetailsChange( );
}

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();
}
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( "Insurance", "insurance", false, false );
		s.options[3] = new Option( "Palletize & Shrink-Wrap", "skid", false, false );
		s.options[4] = new Option( "Insurance + Palletize & Shrink-Wrap", "skid+insurance", false, false );
	}
}

function copy_input_field() {
	var form1 = document.body_form;
	var form2 = document.booking_form;
	form2.pickup_type.value = form1.pickup_type.options[form1.pickup_type.selectedIndex].value;
	form2.pickup_time.value = form1.pickup_time.options[form1.pickup_time.selectedIndex].value;
	form2.pickup_spec.value = form1.pickup_spec.options[form1.pickup_spec.selectedIndex].value;
	form2.pickup_dock.value = form1.pickup_dock.options[form1.pickup_dock.selectedIndex].value;
	form2.pickup_exceed.value = form1.pickup_exceed.options[form1.pickup_exceed.selectedIndex].value;
	form2.services.value = form1.services.options[form1.services.selectedIndex].value;
	form2.skid_n.value = form1.skid_n.options[form1.skid_n.selectedIndex].value;
	form2.skid_n.value = form1.skid_n.options[form1.skid_n.selectedIndex].value;
	form2.insurance_value.value = form1.insurance_value.value;
	form2.total_volume.value = form1.total_volume.value;
	form2.total_weight.value = form1.total_weight.value;
	form2.volume_dim.value = form1.volume_dim.value;
	form2.weight_dim.value = form1.weight_dim.value;
	if(form2.f_quote_n != null)		form2.f_quote_n.value = form1.f_quote_n.value;
	if(form2.f_quote_email != null)	form2.f_quote_email.value = form1.f_quote_email.value;
	var str = document.getElementById( 'div_skid' ).innerHTML;
	str = str.substring( 3, str.length - 4 );
	form2.skid.value = str;
	//form2.total.value = form1.calc_total.value;
}

function copy_booking_information( ) {
	var form1 = document.body_form;
	var form2 = document.booking_form;
	for ( var i = 0; i < form2.elements.length; i++ ) {
		var FieldName = form2.elements[ i ].name;
		var Obj = form2.elements[ i ];
		if ( FieldName != 'do' && FieldName != 'skid' && FieldName != 'cfs' &&
			FieldName.substr( 0, 2 ) != 'u_' && FieldName != 'wei_in' && FieldName != 'total' &&
			FieldName != 'dim_in' && Obj.type != 'button' && Obj.type != 'hidden' && Obj.type != 'checkbox' ) {
			form1.elements[ FieldName ].value = form2.elements[ i ].value;
		}
	}
	form1.action.value = 'change';
//	form1.calc_total.value = form2.total.value;
}

function OriginDetailsChange( ) {
	for ( i = 10; i >= 1; i-- )
		if ( document.booking_form.origin_details_select.selectedIndex == 1 )
			document.getElementById( 'origin_details_row' + i ).style.display = '';
		else
			document.getElementById( 'origin_details_row' + i ).style.display = 'none';
}

function DestinationDetailsChange( ) {
	for ( i = 9; i >= 1; i-- )
		if ( document.booking_form.dest_details_select.selectedIndex )
			document.getElementById( 'dest_details_row' + i ).style.display = '';
		else
			document.getElementById( 'dest_details_row' + i ).style.display = 'none';
}

function booking_submit( ) {
	var f = document.booking_form;
	copy_input_field( );
	document.body_form.total_volume.style.backgroundColor = 'white';
	document.body_form.total_weight.style.backgroundColor = 'white';

	
	var fHearOther    = document.getElementById("f_hear_other").value;
	var f_hear_combo = document.getElementById("f_hear_combo").value;
	
	if( parseFloat( document.body_form.calc_total.value ) > 0 && wei_vol( ) && otherSpecified() )
		f.submit( );
	else if ( parseFloat( document.body_form.calc_total.value ) == 0 ) {
		alert( 'You should enter volume.' );
		document.body_form.total_volume.style.backgroundColor = '#FFCCCC';
		document.body_form.total_volume.focus();
		return false;
	}
	else if (!wei_vol()) {
		alert( 'You should enter aprx. weight.' );
		document.body_form.total_weight.style.backgroundColor = '#FFCCCC';
		document.body_form.total_weight.focus();
		return false;
	}
	else if(f_hear_combo == "Other" && fHearOther.length == 0 )
	{
		window.alert("Please specify How did you hear about us?");
		fHearOther.style.backgroundColor = '#FFCCCC';
		fHearOther.focus();
		return false;
	}
	
	return true;
}
function checkBoxes()
{
	f = document.booking_form;
	if(!f.chk_agree.checked)
	{
		alert( 'You must agree to the terms and conditions of service and acknowledged with Insurance policy, Conditions for Palletizing and Woodpacking Restrictions published in the web-site.' );
		return false;
	}
	if(!f.chk_ocean_transport.checked)
	{
		alert("You must confirm that you are NOT an unlicensed Ocean Transportation Intermediary and do NOT arrange oversea shipments for individual shippers.");
		return false;
	}
	return true;
}

function buttonCalc()
{
	var f = document.body_form;
	
	var calc_total_weight = 0;
	var calc_weight_dim;
	var calc_total_volume = 0;
	var calc_volume_dim;
	
	// VOLUME & WEIGHT
	
	// volume
	calc_total_volume = parseFloat(FormatFloat(f.total_volume.value,3,false));
	calc_total_weight = parseFloat(FormatFloat(f.total_weight.value,0,false));
	
	document.body_form.total_volume.style.backgroundColor = 'white';
	document.body_form.total_weight.style.backgroundColor = 'white';

	
	if( calc_total_volume == 0 || isNaN(calc_total_volume) )
	{
		alert( 'You should enter volume.' );
		document.body_form.total_volume.style.backgroundColor = '#FFCCCC';
		document.body_form.total_volume.focus();
		return false;
	}
	
	if (!wei_vol())
	{
		alert( 'You should enter aprx. weight.' );
		document.body_form.total_weight.style.backgroundColor = '#FFCCCC';
		document.body_form.total_weight.focus();
		return false;
	}
}
function otherSpecified()
{
	// Hear from
	fHear 			= document.getElementById("f_hear_combo");
	fHearOther    	= document.getElementById("f_hear_other");
	
	fHearOther.style.backgroundColor = '';
	
	if(fHear.value == "Other" && fHearOther.value == "")
		return false;
	
	return true;
}

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 fHearChanged()
{
	fHear = document.getElementById("f_hear_combo");
	
	if(fHear != null)
	{
		if(fHear.value == "Other")
			document.getElementById("f_hear_specify").style.display = "";
		else
			document.getElementById("f_hear_specify").style.display = "none";
	}
}
