// Prices
var price_per_skid = 25;
var price_liftgate = 75;
var price_insurance = 0.02;
var old_volume = 0;

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;
	if ( parseFloat( f.total_weight.value ) == 0 && service == "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.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, 3, 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_cbm*calc_total_volume);
  if (t_seafreight < rate_min)
    t_seafreight = Math.ceil(rate_min);

  // Calculate Line Haul
  if (price_haul > 0 && calc_total_weight > 0)
  {
    t_haul = Math.ceil(calc_total_weight*price_haul/100);
    if (t_haul < price_haul)
      t_haul = price_haul;
    if (t_haul < Math.ceil(price_haul*2/10)*10)
      t_haul = Math.ceil(price_haul*2/10)*10;
  }
  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;
            else if (calc_total_weight >= 1000 && calc_total_weight < 3000)
              t_pickup = calc_total_weight*pr_lbs1000_res;
            else
              t_pickup = calc_total_weight*pr_lbs3000_res;
            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;
            else if (calc_total_weight >= 1000 && calc_total_weight < 3000)
              t_pickup = calc_total_weight*pr_lbs1000_com;
            else
              t_pickup = calc_total_weight*pr_lbs3000_com;
            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.5 ) < 50)
    f.skid_n.selectedIndex = Math.ceil(calc_total_volume/1.5);

  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( "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 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_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;
	form2.f_quote_n.value = form1.f_quote_n.value;
	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.substr( 0, 4 ) != 'row_' && 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_volume.style.backgroundColor = 'white';
	if( parseFloat( document.body_form.calc_total.value ) > 0 && wei_vol( ) )
		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';
		return false;
	}
	else {
		alert( 'You should enter aprx. weight.' );
		document.body_form.total_weight.style.backgroundColor = '#FFCCCC';
		return false;
	}
	return true;
}