/*==============================================================================================*
 *                                                                                              *
 *  Consignor Functions                                                                         *
 *                                                                                              *
 *==============================================================================================*/
var contactConsignorId;
var consignorId;


function GetConsignor(consignorId, consignorIndex, sale) {
    $("#consignorline0").html("");
    $("#consignorline1").html("");
    $("#consignorline2").html("");
    $("#consignorline3").html("");
    $("#consignorline4").html("");
    $("#consignorline5 span").html("");
    
    NewConsignor();
    $("#NewConsignorMode").hide();

    console.log('GetConsignor');
    console.log(consignorId);
    console.log(consignorIndex);
    console.log(sale);
    console.log(companyId);

    if (consignorId > 0) {
        $("#EquipConsignorNbr").val(consignorId);
        $("#EquipConsignorContact").val(consignorIndex);
        $("#CustomerNumber").val(consignorId);
        $("#CustomerContactNumber").val(consignorIndex);
        $("#NewCustomer").val("false");
        $("#NewContact").val("false");

        $.ajax({
            url: apiUrl + "/consignors/get/?callback=?",
            cache: false,
            type: "GET",
            data: {
                id: consignorId,
                contactId: consignorIndex,
                companyId: companyId,
                sale: (sale != null ? sale : 0)
            },
            beforeSend: setHeader
        }).success(
                       function (data) {
                           if (data) {
                               $("#consignorline0").html('Customer/Contact # ' + consignorId + ' / ' + consignorIndex);
                               $("#lblCustContactNumber").html(consignorId + ' / ' + consignorIndex);

                               $("#consignorline1").html(data.EHCNTC);
                               $("#consignorline2").html(data.EHNAME);
                               $("#consignorline3").html(data.EHADR1);
                               $("#consignorline4").html(data.EHCITY + ", " + data.EHST + " " + data.EHZIPC);
                               $("#consignorline5 .main").html(FormatPhone(data.EHPHON));
                               $("#consignorline5 .fax").html(FormatPhone(data.EHFAX_));
                               $("#consignorline5 .other").html(FormatPhone(data.EHOTPH));

                               $("#CustomerCompany").val(data.EHNAME.trim());
                               $("#CustomerLastName").val(data.EHCNTC.trim());
                               $("#CustomerMailingAddress1").val(data.EHADR1.trim());
                               $("#CustomerMailingAddress2").val(data.EHADR2.trim());
                               $("#CustomerMailingCity").val(data.EHCITY.trim());
                               $("#CustomerMailingState").val(data.EHST.trim());
                               $("#CustomerMailingZipCode").val(data.EHZIPC.trim());
                               $("#CustomerPhone1").val(FormatPhone(data.EHPHON));
                               $("#CustomerPhone2").val(FormatPhone(data.EHFAX_));
                               $("#CustomerPhone3").val(FormatPhone(data.EHOTPH));
                               $("#CustomerEmailAddress").val(data.EHEMAIL.trim());
                               $("#CustomerSort").val(data.EHSRCH.trim());
                           }

                       });

    }
}

function ShowCustomers(target) {
    $(".consignorLoading").show();
    $(".consignors-table tbody").html("");
    
    $.ajax({
           url: apiUrl + "/customers/getall/?callback=?",
           cache: false,
           type: "GET",
           data: {
           companyId: companyId,
           number: $("#ConsignorSearchNumber").val(),
           company: $("#ConsignorSearchCompany").val(),
           city: $("#ConsignorSearchCity").val(),
           state: $("#ConsignorSearchState").val(),
           zip: $("#ConsignorSearchZip").val(),
           phone: $("#ConsignorSearchPhone").val(),
           firstname: $("#ConsignorSearchFirstName").val(),
           lastname: $("#ConsignorSearchLastName").val(),
           email: $("#ConsignorSearchEmail").val()
           },
           beforeSend: setHeader
           })
    .done(function (data) {
          var trClass = "";
          
          $.each(data, function (i, item) {
                 if (trClass == "odd") { trClass = ""; } else { trClass = "odd"; }

                 var commpct = "N/A";
                 if (!isNaN(parseFloat(item.CULSTCM))) {
                     commpct = parseFloat(item.CULSTCM).toFixed(2);
                 }


                 var tr = "<tr class='" + trClass + "'><td style='width:35px;'><a onclick='SelectCustomer(" + item.CUCUST + "," + item.CUCNTC + ", 0, true)' class='select-icon' data-target='" + target + "'>select</a></td>" +
                 "<td>" + item.CUCUST + "</td><td>" + item.CUCNTC + "</td><td>" + item.CUCSRH + "</td><td>" + item.CUNAME + "</td><td></td><td>" + item.CUMAD1 + "</td><td>" + item.CUMCTY + "</td><td>" + item.CUMSTA + "</td><td>" + item.CUMZIP + "</td><td>" + item.CUPHN_ + "</td><td>" + item.CUMFLG + "</td><td></td><td>" + commpct + "</td></tr>";
                 
                 $(tr).appendTo(".consignors-table tbody");
                 });
          
          $("#ConsignorSearchResults").trigger("update");
          
          })
    .always(function(){
            $(".consignorLoading").hide();
            });
}

function ShowConsignors(target) {
    $(".consignorLoading").show();
    $(".consignors-table tbody").html("");
    $.ajax({
           url: apiUrl + "/consignors/getall/?callback=?",
           cache: false,
           type: "GET",
           data: {
           companyId: companyId,
           number: $("#ConsignorSearchNumber").val(),
           company: $("#ConsignorSearchCompany").val(),
           city: $("#ConsignorSearchCity").val(),
           state: $("#ConsignorSearchState").val(),
           zip: $("#ConsignorSearchZip").val(),
           phone: $("#ConsignorSearchPhone").val(),
           firstname: $("#ConsignorSearchFirstName").val(),
           lastname: $("#ConsignorSearchLastName").val(),
           email: $("#ConsignorSearchEmail").val(),
           sale: $("#ConsignorSearchSale").val()
           },
           beforeSend: setHeader
           })
    .done(function (data) {
          var trClass = "";
          
          $.each(data, function (i, item) {
                 if (trClass == "odd") { trClass = ""; } else { trClass = "odd"; }
                 
                 var commpct = "N/A";
                 if (!isNaN(parseFloat(item.EHLSTCM))) {
                     commpct = parseFloat(item.EHLSTCM).toFixed(2);
                 }
                 
                 var tr = "<tr class='" + trClass + "'><td style='width:35px;'><a onclick='SelectConsignor(" + item.EHCSGN + "," + item.EHDCNT + "," + item.EHSAL_ + ", true)' class='select-icon' data-target='" + target + "'>select</a></td>" +
                 "<td>" + item.EHCSGN + "</td><td>" + item.EHDCNT + "</td><td>" + item.EHSRCH + "</td><td>" + item.EHCNTC + "</td><td>" + item.EHSAL_ + "</td><td>" + item.EHADR1 + "</td><td>" + item.EHCITY + "</td><td>" + item.EHST + "</td><td>" + item.EHZIPC + "</td><td>" + item.EHPHON + "</td><td></td><td>" + item.EHFLG2 + "</td><td>" + commpct + "</td></tr>";
                 
                 $(tr).appendTo(".consignors-table tbody");
                 });
          
          $("#ConsignorSearchResults").trigger("update");

          })
    .fail(function(data){
          console.log(JSON.stringify(data));
          alert(data);
          })
    .always(function(){
            $(".consignorLoading").hide();
            });
}

function ClearConsignorSearch(){
    $("#ConsignorSearchNumber").val("");
    $("#ConsignorSearchCompany").val("");
    $("#ConsignorSearchCity").val("");
    $("#ConsignorSearchState").val("");
    $("#ConsignorSearchZip").val("");
    $("#ConsignorSearchPhone").val("");
    $("#ConsignorSearchFirstName").val("");
    $("#ConsignorSearchLastName").val("");
    $("#ConsignorSearchEmail").val("");
}

function NewConsignor(showMode) {
    if (typeof showMode !== 'undefined' && showMode) {
        consignorId = $("#EquipConsignorNbr").val();
        contactConsignor = $("#EquipConsignorContact").val();
        $("#NewConsignorMode").show();
    }

    $("#NewCustomer").val("true");
    $("#NewContact").val("true");
    $("#EquipConsignorNbr").val("0");
    $("#EquipConsignorContact").val("0");
    $("#CustomerNumber").val("0");
    $("#lblCustContactNumber").html("");
    $("#CustomerContactNumber").val("0");
    $("#CustomerCompany").val("");
    $("#CustomerFirstName").val("");
    $("#CustomerLastName").val("");
    $("#CustomerMailingAddress1").val("");
    $("#CustomerMailingAddress2").val("");
    $("#CustomerMailingCity").val("");
    $("#CustomerMailingState").val("");
    $("#CustomerMailingZipCode").val("");
    $("#CustomerSort").val("");
    $("#CustomerPhone1").val("");
    $("#CustomerPhone2").val("");
    $("#CustomerPhone3").val("");
    $("#CustomerEmailAddress").val("");
    $("#CustomerMakeRecordChangePermanent").removeAttr("checked");
    $("#CustomerMakeRecordChangePermanent").val("");
    $("#CustomerRecordChangeReason").val(""); 
    
    $("#CustomerCompany").focus();
}

function NewContact() {
    contactConsignor = $("#EquipConsignorContact").val();
    
    $("#EquipConsignorContact").val("-1");
    $("#CustomerFirstName").val("");
    $("#CustomerLastName").val("");
    $("#CustomerPhone1").val("");
    $("#CustomerPhone2").val("");
    $("#CustomerPhone3").val("");
    $("#NewContact").val("true");
    $("#CustomerEmailAddress").val("");
    
    $("#NewContactMode").show();
}

function CancelNewContact() {
    consignorId = $("#EquipConsignorNbr").val();
    SelectConsignor(consignorId, contactConsignor, $("#EquipSale").val(), true);
    
    $("#NewContactMode").hide();
}

function CancelNewConsignor() {

    SelectConsignor(consignorId, contactConsignor, $("#EquipSale").val(), true);
    $("#NewConsignorMode").hide();
}

function SelectCustomer(consignorId, contactId, sale, display) {
    var $this = $(this);
    
    if (consignorId == "0") {
        NewConsignor();
    } else {
        $("#EquipConsignorNbr").val(consignorId);
        $("#EquipConsignorContact").val(contactId);
        $("#CustomerNumber").val(consignorId);
        $("#CustomerContactNumber").val(contactId);
        $("#NewCustomer").val("false");
        $("#NewContact").val("false");

        $.ajax({
            url: apiUrl + "/customers/get/?callback=?",
            cache: false,
            type: "GET",
            data: {
                id: consignorId,
                companyId: companyId,
                contactId: contactId,
                sale: sale
            },
            beforeSend: setHeader
        }).done(
            function (data) {
                //if($this.data("target") == "select"){
                if (true) {
                    $("#lblCustContactNumber").html(data.CUCUST + ' / ' + data.CUCNTC);
                    $("#EquipConsignorNbr").val(data.CUCUST);
                    $("#EquipConsignorContact").val(data.CUCNTC);
                    if (data.CUCNTC > 0) {
                        $("#CustomerCompany").val(data.CUCSRH.trim());
                        $("#CustomerSort").val(data.CUCSRH.trim());
                    } else {
                        $("#CustomerCompany").val(data.CUNAME.trim());
                        $("#CustomerSort").val(data.CUSRCH.trim());
                    }
                    $("#CustomerFirstName").val(data.CUFNAM.trim());
                    $("#CustomerLastName").val(data.CULNAM.trim());
                    $("#CustomerMailingAddress1").val(data.CUMAD1.trim());
                    $("#CustomerMailingAddress2").val(data.CUMAD2.trim());
                    $("#CustomerMailingCity").val(data.CUMCTY.trim());
                    $("#CustomerMailingState").val(data.CUMSTA.trim());
                    $("#CustomerMailingZipCode").val(data.CUMZIP.trim());
                    $("#CustomerPhone1").val(FormatPhone(data.CUPHN_));
                    $("#CustomerPhone2").val(FormatPhone(data.CUFAX_));
                    $("#CustomerPhone3").val(FormatPhone(data.CUCELL));
                    $("#CustomerEmailAddress").val(data.CUEMAL.trim());
                               

                    var commpct = "";
                    if (!isNaN(parseFloat(data.CULSTCM))) {
                        commpct = parseFloat(data.CULSTCM) > 0 ? parseFloat(data.CULSTCM).toFixed(2) : "";
                    }
                    $("#CustomerCommissionCode").val(commpct);
                    //$("#EquipCommissionCode").val(commpct);
                } else {
                    $("#CustomerNumber").html(data.CUCUST);
                    $("#ContactNumber").html(data.CUCNTC);
                    $("#ContactName").html(data.CUFNAM.trim() + " " + data.CULNAM.trim());
                    $("#CustomerAddress").html(data.CUMAD1.trim() + "<br />" + data.CUMAD2.trim() + "<br />" + data.CUMCTY.trim() + ", " + ata.CUMSTA.trim() + " " + data.CUMZIP.trim() + "<br />Phone: " + FormatPhone(data.CUPHN_) + "<br />Fax: " + FormatPhone(data.CUFAX_));
                    $("#CustomerDetails").dialog("open");
                }
            });
    }
    
    if (display){
        $("#ConsignorList").hide();
        $("#NewConsignor").show();
        $("#ConsignorSearchSale").val($("#EquipSale").val());
        $("#consignor-edit").dialog("open");
    }
}

function SelectConsignor(consignorId, contactId, sale, display) {
    var $this = $(this);

    NewCreditor();
    $("#creditorline0").html("");
    $("#creditorline1").html("");
    $("#creditorline2").html("");
    $("#creditorline3").html("");
    $("#creditorline4").html("");
    $("#creditorline5").html("");
    

    if (consignorId == "0") {
        NewConsignor();
        
    } else {
        NewConsignor();
        
        $("#EquipConsignorNbr").val(consignorId);
        $("#EquipConsignorContact").val(contactId);
        $("#CustomerNumber").val(consignorId);
        $("#CustomerContactNumber").val(contactId);
        $("#NewCustomer").val("false");
        $("#NewContact").val("false");
        
        $.ajax({
               url: apiUrl + "/consignors/get/?callback=?",
               cache: false,
               type: "GET",
               data: {
               id: consignorId,
               companyId: companyId,
               contactId: contactId,
               sale: sale
               },
               beforeSend: setHeader
               }).done(
                       function (data) {
                           //if($this.data("target") == "select"){
                           $("#lblCustContactNumber").html(data.EHCSGN + ' / ' + data.EHDCNT);
                       $("#EquipConsignorNbr").val(data.EHCSGN);
                       $("#EquipConsignorContact").val(data.EHDCNT);
                       $("#CustomerCompany").val(data.EHNAME.trim());
                       $("#CustomerLastName").val(data.EHCNTC.trim());
                       $("#CustomerMailingAddress1").val(data.EHADR1.trim());
                       $("#CustomerMailingAddress2").val(data.EHADR2.trim());
                       $("#CustomerMailingCity").val(data.EHCITY.trim());
                       $("#CustomerMailingState").val(data.EHST.trim());
                       $("#CustomerMailingZipCode").val(data.EHZIPC.trim());
                       $("#CustomerPhone1").val(FormatPhone(data.EHPHON));
                       $("#CustomerPhone2").val(FormatPhone(data.EHFAX_));
                       $("#CustomerPhone3").val(FormatPhone(data.EHOTPH));
                       $("#CustomerEmailAddress").val(data.EHEMAIL.trim());
                       $("#CustomerSort").val(data.EHSRCH.trim());

                       var commpct = "";
                       if (!isNaN(parseFloat(data.EHLSTCM))) {
                           commpct = parseFloat(data.EHLSTCM) > 0 ? parseFloat(data.EHLSTCM).toFixed(2) : "";
                       }
                       $("#CustomerCommissionCode").val(commpct);
                       //$("#EquipCommissionCode").val(commpct);
                       
//                       } else {
//                       $("#CustomerNumber").html(data.EHCSGN);
//                       $("#ContactNumber").html(data.EHDCNT);
//                       $("#ContactName").html(data.EHCNTC.trim());
//                       $("#CustomerAddress").html(data.EHADR1.trim() + "<br />" + data.EHADR2.trim() + "<br />" + data.EHCITY.trim() + ", " + ata.EHST.trim() + " " + data.EHZIPC.trim() + "<br />Phone: " + FormatPhone(data.EHPHON) + "<br />Fax: " + FormatPhone(data.EHFAX_));
//                       $("#CustomerDetails").dialog("open");
//                       }
                       });
    }
    
    if (display){
        $("#ConsignorList").hide();
        $("#NewConsignor").show();
        $("#ConsignorSearchSale").val($("#EquipSale").val());
        $("#consignor-edit").dialog("open");
    }
}
