var COOKIE_NAME = "globalshippingcosts";
var calc_volume_str = "<div id='calc_volume'><table><tr><td>"+CALC_LABEL_OCEAN_LCL_CALCULATE_VOL_HEIGHT+":</td><td><input type='text' id='lcl_height' name='lcl_height' size=5 value='0'> </td></tr><tr><td>"+CALC_LABEL_OCEAN_LCL_CALCULATE_VOL_LENGTH+":</td><td> <input type='text' id='lcl_length' name='lcl_length' size=5 value='0'> </td></tr><tr><td>"+CALC_LABEL_OCEAN_LCL_CALCULATE_VOL_WIDTH+":</td><td> <input type='text' id='lcl_width' name='lcl_width' size=5 value='0'> </td></tr></table><br><input type='button' id='calculate_volume_btn' value='"+CALC_LABEL_OCEAN_LCL_CALCULATE_VOL_BUTTON+"'></div>";
var calc_volume_ltl_str = "<div id='calc_volume'><table><tr><td>"+CALC_LABEL_GROUND_LTL_CALCULATE_VOL_HEIGHT+":</td><td><input type='text' id='ltl_height' name='ltl_height' size=5 value='0'> </td></tr><tr><td>"+CALC_LABEL_GROUND_LTL_CALCULATE_VOL_LENGTH+":</td><td> <input type='text' id='ltl_length' name='ltl_length' size=5 value='0'> </td></tr><tr><td>"+CALC_LABEL_GROUND_LTL_CALCULATE_VOL_WIDTH+":</td><td> <input type='text' id='ltl_width' name='ltl_width' size=5 value='0'> </td></tr></table><br><input type='button' id='calculate_volume_ltl_btn' value='"+CALC_LABEL_GROUND_LTL_CALCULATE_VOL_BUTTON+"'></div>";
	
var loading_img= new Image(16,16); 
loading_img.src= MAIN_URL + "/images/ajax-loader.gif";


function init_calc(){
	
	$('#container-1 > ul').tabs();
	
	//LCL Calculator
		$("#ship_type_lcl").click(function(){
			if ($("#ship_type_lcl:checked")){
				$("#lcl_options").show();
				
				$("#lcl_vol_calculate_ref").click(function(){
					if($("#calc_lcl_dialog").html() == ''){
						$("#calc_lcl_dialog").html(calc_volume_str);
							
						$("#calc_lcl_dialog").dialog({
							title: "", 
							height: 170 , 
							width: 180, 
							modal: true, 
							resizable: false, 
							draggable:false
						})
					
					}else{
						$("#calc_lcl_dialog").dialog('open');
						clearFields("lcl_height:0", "lcl_length:0", "lcl_width:0");
						
					}
					$("#calculate_volume_btn").click(function(){
						var height = parseInt ($("#lcl_height").val()) || 0;
						var length = parseInt ($("#lcl_length").val()) || 0;
						var width = parseInt ($("#lcl_width").val()) || 0;
						var result = height * length * width;
						
						$("#lcl_volume").val(result);
						$("#calc_lcl_dialog").dialog("close");
					});
					
				})
			}
		});
		
		$("#ship_type_cl").click(function(){
			if ($("#ship_type_lcl:checked")){
				$("#lcl_options").hide();
			}
			
		});
		
		//LTL Calculator
		$("#land_type_ltl").click(function(){
			if ($("#land_type_ltl:checked")){
				$("#ltl_options").show();
				
				$("#ltl_vol_calculate_ref").click(function(){
					
					if($("#calc_ltl_dialog").html() == ''){
						$("#calc_ltl_dialog").html(calc_volume_ltl_str);
						
						$("#calc_ltl_dialog").dialog({
							title: "Volume calculator", 
							height: 170 , 
							width: 180, 
							modal: true, 
							resizable: false, 
							draggable:false
						});
					}else{
						$("#calc_ltl_dialog").dialog('open');
						clearFields("ltl_height:0", "ltl_length:0", "ltl_width:0");
					}
						
					$("#calculate_volume_ltl_btn").click(function(){
						var height = parseInt ($("#ltl_height").val()) || 0;
						var length = parseInt ($("#ltl_length").val()) || 0;
						var width = parseInt ($("#ltl_width").val()) || 0;
						var result = height * length * width;
						
						$("#ltl_volume").val(result);
						$("#calc_ltl_dialog").dialog("close");
					});
					
				})
			}
		});
		
		$("#land_type_tl").click(function(){
			if ($("#land_type_lt:checked")){
				$("#ltl_options").hide();
			}
		});
		
		
		
		$("#lcl_volume_unit").change(function(){
			if ($("#lcl_volume_unit").val() == 'ft3'){
				var vol = roundNumber( convertCubicMeterToCubicFeet(  $("#lcl_volume").val() ) , 4);
				
				if (isNumeric(vol))
					$("#lcl_volume").val( vol );
				else{
					$("#lcl_volume").val( "0" );
				}
				
			}else if ($("#lcl_volume_unit").val() == 'm3'){
				var vol =  roundNumber( convertCubicFeetToCubicMeter(  $("#lcl_volume").val() ) , 4);
				if (isNumeric(vol))
					$("#lcl_volume").val( vol );
				else{
					$("#lcl_volume").val( "0" );
				}
			}
			
		});
		
		
		$("#lcl_weight_unit").change(function(){
			if ($("#lcl_weight_unit").val() == 'lbl'){

				var w = roundNumber( convertKilosToPounds(  $("#lcl_weight").val() ) , 4);
				if (isNumeric(w))
					$("#lcl_weight").val( w );
				else{
					$("#ltl_weight").val( "0" );
				}
				
			}else if ($("#lcl_weight_unit").val() == 'k'){
				var w =  roundNumber( convertPoundToKilos(  $("#lcl_weight").val() ) , 4);
				if (isNumeric(w))
					$("#lcl_weight").val( w );
				else{
					$("#ltl_weight").val( "0" );
				}
			}
			
		});
		
		
		
		$("#ltl_volume_unit").change(function(){
			if ($("#ltl_volume_unit").val() == 'ft3'){
				var vol = roundNumber( convertCubicMeterToCubicFeet(  $("#ltl_volume").val() ) , 4);
				if (isNumeric(vol))
					$("#ltl_volume").val( vol );
				
			}else if ($("#ltl_volume_unit").val() == 'm3'){
				var vol =  roundNumber( convertCubicFeetToCubicMeter(  $("#ltl_volume").val() ) , 4);
				if (isNumeric(vol))
					$("#ltl_volume").val( vol );
				else{
					$("#ltl_volume").val( "0" );
				}
			}
			
		});
		
		
		$("#ltl_weight_unit").change(function(){
			if ($("#ltl_weight_unit").val() == 'lbl'){
				var w = roundNumber( convertKilosToPounds(  $("#ltl_weight").val() ) , 4);
				if (isNumeric(w))
					$("#ltl_weight").val( w );
				
			}else if ($("#ltl_weight_unit").val() == 'k'){
				var w =  roundNumber( convertPoundToKilos(  $("#ltl_weight").val() ) , 4);
				if (isNumeric(w))
					$("#ltl_weight").val( w );
				else{
					$("#ltl_weight").val( "0" );
				}
			}
			
		});
		
		
		$("#help_reef").click(function(){
		
			show_help_dialog(CALC_REEF_HELP);
		});
		
		
		$("#help_commodity").click(function(){
		
			show_help_dialog(CALC_COMMODITY_HELP);
		});
		
		$('#clear_ocean').click(function() {
			
			origin1.setComboText("");
			
			destination1.setComboText("");
			
			clearFields("commodity_name", "container_size", "lcl_weight:0", "lcl_weight_unit:0", "lcl_volume:0", "lcl_volume_unit:0");
		
		});
		
		
		$('#clear_air').click(function() {
			
			origin_air.setComboText("");
			
			destination_air.setComboText("");
			
			clearFields("load_kilos:0");
		
		});
		
		$('#clear_land').click(function() {
			
			clearFields("origin_land", "destination_land", "ltl_weight:0", "ltl_weight_unit:0", "ltl_volume:0", "ltl_volume_unit:0");
		
		});
		
		
		
		
		$('#calculate_ocean').click(function() {
			var lbl1 = origin1.getSelectedValue();
			var lbl2 = destination1.getSelectedValue();
			
			if (lbl1!="" && lbl2!=""){
			
			
				if(document.getElementById("commodity_name").value=="" || document.getElementById("commodity_name").value==CALC_LABEL_OCEAN_COMMODITY_NAME_TYPE){
					alert(CALC_MSG_MISSING_COMMODITY);
					return;
				}
			
				var url = MAIN_URL +"/calc/index.php?type_cargo=ocean";
				var frm = document.getElementById("submitFrm");
				frm.action = url;
				frm.submit();
				
			}else{
				alert(CALC_LABEL_NOT_BLANK_ORIGIN_DESTINATION);
			}
		
		});
		
		$('#calculate_air').click(function() {
			var lbl1 = origin_air.getSelectedValue();
			var lbl2 = destination_air.getSelectedValue();
			var kilos = $('#load_kilos').val();
			
			if (lbl1!="" && lbl2!=""){
				if (kilos !="" && kilos!="0"){
				
					if(document.getElementById("commodity_name_air").value=="" || document.getElementById("commodity_name_air").value==CALC_LABEL_OCEAN_COMMODITY_NAME_TYPE){
						alert(CALC_MSG_MISSING_COMMODITY);
						return;
					}
					var url = MAIN_URL +"/calc/index.php?type_cargo=air";
					var frm = document.getElementById("submitFrm");
					frm.action = url;
					frm.submit();
				}else{
					alert("Please enter a value for the load.")
				}
			}else{
				alert(CALC_LABEL_NOT_BLANK_ORIGIN_DESTINATION);
			}
		});
		
		$('#calculate_land').click(function() {
			var lbl1 = $('#origin_land').val();
			var lbl2 = $('#destination_land').val();
			
			if (lbl1!="" && lbl2!=""){
				if(document.getElementById("commodity_name_land").value=="" || document.getElementById("commodity_name_land").value==CALC_LABEL_OCEAN_COMMODITY_NAME_TYPE){
					alert(CALC_MSG_MISSING_COMMODITY);
					return;
				}
			
				var url = MAIN_URL +"/calc/index.php?type_cargo=land";
				var frm = document.getElementById("submitFrm");
				frm.action = url;
				frm.submit();
				
			}else{
				alert(CALC_LABEL_NOT_BLANK_ORIGIN_DESTINATION);
			}
		});	
		
		
		
		$('#calc_reef').click(function() {
			var reef = document.getElementById('calc_reef').checked
		
		
			if(reef){ 
				removeAllOptions( document.getElementById('container_size') );
				for (var i=0; i < containers.length - 1 ;++i){

					addOption(document.getElementById('container_size'), containers[i] + " Ft", containers[i]);
				}
			
			}else{ //add all sizes
			
				removeAllOptions( document.getElementById('container_size') );
				for (var i=0; i < containers.length ;++i){

					addOption(document.getElementById('container_size'), containers[i] + " Ft", containers[i]);
				}
			}
		});	
		
		
		
}
