/*==============================================================================================*
 *                                                                                              *
 *  Creditor Functions                                                                         *
 *                                                                                              *
 *==============================================================================================*/

function GetCreditor(creditorId, contactId) {
    $("#creditorline0").html("");
    $("#creditorline1").html("");
    $("#creditorline2").html("");
    $("#creditorline3").html("");
    $("#creditorline4").html("");
    $("#creditorline5").html("");
    
    NewCreditor();
    
    if (creditorId > 0) {
        $("#EquipCreditorNbr").val(creditorId);
        $("#EquipCreditorContact").val(contactId);
	    $("#NewCreditorCustomer").val("false");
        $("#NewCreditorContact").val("false");

        $.ajax({
            url: apiUrl + "/creditors/get/?callback=?",
            cache: false,
            type: "GET",
            data: {
                id: creditorId,
                contactId: contactId,
                companyId: companyId
            },
            beforeSend: setHeader
        }).done(
                       function (data) {
                           $("#creditorline0").html('Creditor/Contact # ' + creditorId + ' / ' + contactId);
                           $("#creditorline1").html(data.CRNAME);
                           $("#creditorline2").html(data.CRFNAM + " " + data.CRLNAM);
                           $("#creditorline3").html(data.CRMAD1);
                           $("#creditorline4").html(data.CRMCTY + ", " + data.CRMSTA + " " + data.CRMZIP);
                           $("#creditorline5").html(FormatPhone(data.CRPHN_));
                           //$("#CreditorSort").val(data.CRCSRH);
                       });
    }
}

function SaveCreditor() {
    $("#EquipCreditorNbr").val($("#CreditorNumber").val());
    $("#EquipCreditorContact").val($("#CreditorContactNumber").val());
    
    $("#creditor-edit").dialog('close');
}

function ShowCreditors() {
    $(".creditorLoading").show();
    $(".creditors-table tbody").html("");
    
    $.ajax({
           url: apiUrl + "/creditors/getall/?callback=?",
           cache: false,
           type: "GET",
           data: {
           companyId: companyId,
           number: $("#CreditorSearchNumber").val(),
           company: $("#CreditorSearchCompany").val(),
           city: $("#CreditorSearchCity").val(),
           state: $("#CreditorSearchState").val(),
           zip: $("#CreditorSearchZip").val(),
           phone: $("#CreditorSearchPhone").val(),
           firstname: $("#CreditorSearchFirstName").val(),
           lastname: $("#CreditorSearchLastName").val(),
           email: $("#CreditorSearchEmail").val()
           },
           beforeSend: setHeader
           })
    .done(function (data) {
          var trClass = "";
          
          $.each(data, function (i, item) {
                 if (trClass == "odd") { trClass = ""; } else { trClass = "odd"; }
                 
                 var tr = "<tr class='" + trClass + "'><td style='width:35px;'><a onclick='SelectCreditor(" + item.CRCRDT + "," + item.CRCNTC + ", true)' class='select-icon'>select</a></td>" +
                 "<td>" + item.CRCSRH + "</td><td>" + item.CRNAME + "</td><td>" + item.CRMAD1 + "</td><td>" + item.CRMCTY + "</td><td>" + item.CRMSTA + "</td><td>" + item.CRMZIP + "</td><td>" + item.CRPHN_ + "</td></tr>";
                 
                 $(tr).appendTo(".creditors-table tbody");
                 });
          
                $("#CreditorSearchResults").trigger("update");
          })
    .always(function(){
            $(".creditorLoading").hide();
            });
}

function ClearCreditorSearch(){
    $("#CreditorSearchNumber").val("");
    $("#CreditorSearchCompany").val("");
    $("#CreditorSearchCity").val("");
    $("#CreditorSearchState").val("");
    $("#CreditorSearchZip").val("");
    $("#CreditorSearchPhone").val("");
    $("#CreditorSearchFirstName").val("");
    $("#CreditorSearchLastName").val("");
    $("#CreditorSearchEmail").val("");
}

function NewCreditor(){
    $("#NewCreditorCustomer").val("true");
    $("#NewCreditorContact").val("true");
    $("#EquipCreditorNbr").val("0");
    $("#EquipCreditorContact").val("0");
    $("#CreditorNumber").val("0");
    $("#CreditorContactNumber").val("0");
    $("#CreditorCompany").val("");
    $("#CreditorFirstName").val("");
    $("#CreditorLastName").val("");
    $("#CreditorMailingAddress1").val("");
    $("#CreditorMailingAddress2").val("");
    $("#CreditorMailingCity").val("");
    $("#CreditorMailingState").val("");
    $("#CreditorMailingZipCode").val("");
    $("#CreditorSort").val("");
    $("#CreditorPhone1").val("");
    $("#CreditorPhone2").val("");
    $("#CreditorPhone3").val("");
    $("#CreditorEmailAddress").val("");
    $("#CreditorMakeRecordChangePermanent").removeAttr("checked");
    $("#CreditorMakeRecordChangePermanent").val("");
    $("#CreditorRecordChangeReason").val(""); 
    
    $("#CreditorCompany").focus();
}

function NewCreditorContact() {
    contactCreditor = $("#EquipCreditorContact").val();
    
    $("#EquipCreditorContact").val("-1");
    $("#CreditorFirstName").val("");
    $("#CreditorLastName").val("");
    $("#CreditorPhone1").val("");
    $("#CreditorPhone2").val("");
    $("#CreditorPhone3").val("");
    $("#CreditorEmailAddress").val("");
    
    $("#NewCreditorContact").val("true");
    $("#NewCreditorContactMode").show();
}

function CancelNewCreditorContact() {
    $("#NewCreditorContactMode").hide();
    
    var creditorId = $("#EquipCreditorNbr").val();
    SelectCreditor(creditorId, contactCreditor, $("#EquipSale").val(), true);
}

function SelectCreditor(creditorId, contactId, display) {
    console.log("Select Creditor: " + creditorId + ":" + contactId);
    if (creditorId == "0") {
        NewCreditor();
        //Clear Creditor Fields
//        $("#EquipCreditorNbr").val(creditorId);
//        $("#EquipCreditorContact").val(contactId);
//        $("#CreditorNumber").val("0");
//        $("#CreditorContactNumber").val("0");
//        $("#CreditorCompany").val("");
//        $("#CreditorFirstName").val("");
//        $("#CreditorLastName").val("");
//        $("#CreditorMailingAddress1").val("");
//        $("#CreditorMailingAddress2").val("");
//        $("#CreditorMailingCity").val("");
//        $("#CreditorMailingState").val("");
//        $("#CreditorMailingZipCode").val("");
//        $("#CreditorPhone1").val("");
//        $("#CreditorPhone2").val("");
//        $("#CreditorPhone3").val("");
//        $("#CreditorEmailAddress").val("");

    } else {
        NewCreditor();
        $("#EquipCreditorNbr").val(creditorId);
        $("#EquipCreditorContact").val(contactId);
        $("#CreditorNumber").val(creditorId);
        $("#CreditorContactNumber").val(contactId);

        $("#NewCreditorCustomer").val("false");
        $("#NewCreditorContact").val("false");

        $.ajax({
               url: apiUrl + "/creditors/get/?callback=?",
               cache: false,
               type: "GET",
               data: {
               id: creditorId,
               companyId: companyId,
               contactId: contactId
               },
               beforeSend: setHeader
               }).done(
                         function (data) {
                             if (contactId > 0)
                                 $("#CreditorCompany").val(data.CRCSRH.trim());
                             else
                                 $("#CreditorCompany").val(data.CRNAME.trim());
                         $("#CreditorFirstName").val(data.CRFNAM.trim());
                         $("#CreditorLastName").val(data.CRLNAM.trim());
                         $("#CreditorMailingAddress1").val(data.CRMAD1.trim());
                         $("#CreditorMailingAddress2").val(data.CRMAD2.trim());
                         $("#CreditorMailingCity").val(data.CRMCTY.trim());
                         $("#CreditorMailingState").val(data.CRMSTA.trim());
                         $("#CreditorMailingZipCode").val(data.CRMZIP.trim());
                         $("#CreditorSort").val(data.CRCSRH);
                         $("#CreditorPhone1").val(data.CRPHN_);
                         $("#CreditorPhone2").val(data.CRFAX_);
                         $("#CreditorPhone3").val(data.CROPH_);
                         $("#CreditorEmailAddress").val(data.CREMAL.trim());
                       
                         });
    }
    
    if (display){
    $("#CreditorList").hide();
    $("#NewCreditor").show();
    $("#creditor-edit").dialog("open");
    }
}
