function setFormData( data) {
//  var client_array = eval( "(" + data + ")" );
  for( field in data) {
    $("#" + field).val( data[ field]);
  }
}

function force_complete_map_refresh() {
  setTimeout( " $('#sourceCity').change()", 1);
  setTimeout( " $('#embankmentPort').change()", 1000);
  setTimeout( " $('#endDestination').change()", 2000);
  setTimeout( " $('#unloadingPort').change()", 3000);
  setTimeout( " fnAdjustMapAndResetZoom", 4000);
  
}

function refreshFormData() {
  $("#behalf_company").keyup();
  $("#behalf_role").change();
  $("#value").keyup();
  $("#goods_type").change();
  $("#shipping_date").keyup();
  $("#route_EP_UP_type").change();
  $("#ship_name").keyup();
  $("#incoterms").change();
  $("#sourceCity").keyup();
  $("#embankmentPort").keyup();
  $("#unloadingPort").keyup();
  $("#endDestination").keyup();

}

function get_shipping_quote() {

  $("#deal_status").html( "");
  $("#premium").html( "" );
  $("#computed_premium").val( "" );
  $("#computed_rate").val( "" );
  $("#cover_button").css("visibility", "hidden");

  //var values = $('#InsuranceProposal').values();
  var values = $('#InsuranceProposal').values({ useSelectValue: true});

  
/*
alert(
       values.ship_type + " " + values.route_EP_UP_type + " " +
       values.sourceCityNormalizedCountryCode    + " " + values.embankmentPortNormalizedCountryCode + " " +
       values.unloadingPortNormalizedCountryCode + " " + values.endDestinationNormalizedCountryCode );
*/


  if(  Number( values.value )        > 1     &&
       ( ( Number( values.ship_launch )  > 1800  && values.ship_type    != null && values.ship_type   != "" ) || values.route_EP_UP_type != "SEA" )&&
       values.sourceCityNormalizedCountryCode     != null && values.sourceCityNormalizedCountryCode     != "" &&
       values.embankmentPortNormalizedCountryCode != null && values.embankmentPortNormalizedCountryCode != "" &&
       values.unloadingPortNormalizedCountryCode  != null && values.unloadingPortNormalizedCountryCode  != "" &&
       values.endDestinationNormalizedCountryCode != null && values.endDestinationNormalizedCountryCode != ""    ) {

    values.action = "quote";
//alert( dump(values));
    $.post( "http://www.sacebi.com/soluzioni/spedizionieri/index.php", values, function(jsonText) {
      var dealData = eval("("+jsonText+")");
      if( dealData.deal_status == 'standard') {
        $("#deal_status").html(  dealData.deal_status );
      } else if ( dealData.deal_status == 'reserved') {
        $("#deal_status").html(  dealData.deal_status );
      }
      $("#premium").html(  dealData.premium );
      $("#computed_premium").val( dealData.premium );
      $("#computed_rate").val( dealData.rate );
      $("#cover_button").css("visibility", "visible");
    });

  } else {
    $("#deal_status").html('');
    $("#premium").html('');
    $("#cover_button").css("visibility", "hidden");
    if( this.id == 'offer_button' ) {
       if( ! ( Number( values.value ) > 1 ) ) { data_error = 'Valore deve essere un numero positivo'; }
       if( ! ( ( Number( values.ship_launch )  > 1800  && values.ship_type    != null && values.ship_type   != "" ) || values.route_EP_UP_type != "SEA" ) ) { data_error = 'Dati nave errati';}
       if( ! ( values.sourceCityNormalizedCountryCode     != null && values.sourceCityNormalizedCountryCode     != "" ) ) { data_error = 'Citta di origine mancante'; } 
       if( ! ( values.embankmentPortNormalizedCountryCode != null && values.embankmentPortNormalizedCountryCode != "" ) ) { data_error = 'Citta di carico mancante'; }
       if( ! ( values.unloadingPortNormalizedCountryCode  != null && values.unloadingPortNormalizedCountryCode  != "" ) ) { data_error = 'Citta di scarico mancante'; }
       if( ! ( values.endDestinationNormalizedCountryCode != null && values.endDestinationNormalizedCountryCode != "" ) ) { data_error = 'Citta di destinazione mancante'; }
      alert( 'Errore dati compilati:\n' + data_error);
    }
  }
}

function request_storage( ) {

  var values = $('#InsuranceProposal').values({ useSelectValue: true});

  values.action = "cover"; 

/*
  0 = annullata
  1 = copertura
  5 = facoltativo
 10 = bozza
 50 = template
*/
  switch( this.id) {
    case 'cover_button':    values.status = "1"; break;
    case 'template_button': values.status = "50"; break;
    case 'draft_button':    values.status = "10"; break;
  }
  if( values.status == "1" && $("#deal_status").html() != 'standard' ) { values.status = "5"; }

  $.get( "http://www.sacebi.com/soluzioni/spedizionieri/index.php", values, function(jsonText) {
    var coverData = eval("("+jsonText+")");
    if( coverData.cover == "ok" ) {
      alert("Richiesta riuscita");   
      $("#covers").trigger("reloadGrid");
      $("#reset_button").click();
    } else {
      alert("Errore database.\nCopertura non riuscita");   
    }
  });

}
	

function set_insured_routes() {

  if( $("#behalf_role").val() == 'seller' ) {
    ins_role_t=true; ins_role_f=false;
    ins_col_t="#4C7D62"; ins_col_f="#B9564C";
  } else {
    ins_role_t=false; ins_role_f=true;
    ins_col_t="#B9564C"; ins_col_f="#4C7D62";
  }
  switch( $("#incoterms").val() ) {
    case "EXW":
      fnRouteInsurance("route_SC_EP",ins_role_f) ; 
      fnRouteInsurance("route_EP_UP",ins_role_f) ; 
      fnRouteInsurance("route_UP_ED",ins_role_f) ;
      $("#route_SC_EP_insured_segment").css("backgroundColor", ins_col_f);
      $("#route_EP_UP_insured_segment").css("backgroundColor", ins_col_f); 
      $("#route_UP_ED_insured_segment").css("backgroundColor", ins_col_f);
      break;
    case "FCA":
    case "CPT":
    case "FAS":
    case "FOB":
    case "CFR":
      fnRouteInsurance("route_SC_EP",ins_role_t) ; 
      fnRouteInsurance("route_EP_UP",ins_role_f) ; 
      fnRouteInsurance("route_UP_ED",ins_role_f) ; 
      $("#route_SC_EP_insured_segment").css("backgroundColor", ins_col_t);
      $("#route_EP_UP_insured_segment").css("backgroundColor", ins_col_f); 
      $("#route_UP_ED_insured_segment").css("backgroundColor", ins_col_f);
      break;
    case "CIF":
    case "DAF":
    case "DES":
    case "DEQ":
      fnRouteInsurance("route_SC_EP",ins_role_t) ; 
      fnRouteInsurance("route_EP_UP",ins_role_t) ; 
      fnRouteInsurance("route_UP_ED",ins_role_f) ; 
      $("#route_SC_EP_insured_segment").css("backgroundColor", ins_col_t);
      $("#route_EP_UP_insured_segment").css("backgroundColor", ins_col_t); 
      $("#route_UP_ED_insured_segment").css("backgroundColor", ins_col_f);
      break;
    case "CIP":
    case "DDU":
    case "DDP":
      fnRouteInsurance("route_SC_EP",ins_role_t) ;
      fnRouteInsurance("route_EP_UP",ins_role_t) ;
      fnRouteInsurance("route_UP_ED",ins_role_t) ;
      $("#route_SC_EP_insured_segment").css("backgroundColor", ins_col_t);
      $("#route_EP_UP_insured_segment").css("backgroundColor", ins_col_t); 
      $("#route_UP_ED_insured_segment").css("backgroundColor", ins_col_t);
      break;
  }
}

function toggle_vehicle_details_field() {
  if( $("#route_EP_UP_type").val() == 'SEA' ) {
    $("#airwaybill").css("display", "none");
    $("#airwaybill").watermark({remove:true});
    $("#shipSearch").css("display", "inline");
    $("#shipSearch").watermark({html:'Nome Nave',cls:'watermark'});
    $("#ship_details").css("display", "inline");
    $("#report_row_ship_name").css("display", "table-row");
  } else if( $("#route_EP_UP_type").val() == 'AIR') {
    $("#airwaybill").css("display", "inline");
    $("#airwaybill").watermark({html:'Nome Nave',cls:'watermark'});
    $("#shipSearch").css("display", "none");
    $("#shipSearch").watermark({remove:true});
    $("#ship_details").css("display", "none");
    $("#report_row_ship_name").css("display", "none");
  } else if( $("#route_EP_UP_type").val() == 'LAND') {
    $("#airwaybill").css("display", "none");
    $("#airwaybill").watermark({remove:true});
    $("#shipSearch").css("display", "none");
    $("#shipSearch").watermark({remove:true});
    $("#ship_details").css("display", "none");
    $("#report_row_ship_name").css("display", "none");
  }
}


function dump(arr,level) {
        var dumped_text = "";
        if(!level) level = 0;

        //The padding given at the beginning of the line.
        var level_padding = "";
        for(var j=0;j<level+1;j++) level_padding += "    ";

        if(typeof(arr) == 'object') { //Array/Hashes/Objects
                for(var item in arr) {
                        var value = arr[item];

                        if(typeof(value) == 'object') { //If it is an array,
                                dumped_text += level_padding + "'" + item + "' ...\n";
                                dumped_text += dump(value,level+1);
                        } else {
                                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
                        }
                }
        } else { //Stings/Chars/Numbers etc.
                dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
        }
        return dumped_text;
}

