var C_JOB_QUOTATION_FILTER       = "1901";
var C_ADD_JOB_QUOTATION_VIEW     = "1902";
var C_UPDATE_JOB_QUOTATION_VIEW  = "1903";
var C_SHOW_JOB_QUOTATION_REPORT  = "1904";
var C_SHOW_JOB_QUOTATION_EXPORT  = "1905";

var C_ACCOUNT_MANAGER               = "1";
var C_PROJECT_MANAGER               = "2";
var C_TECHNICAL_CONSULTANT          = "3";
var C_APPLICATION_CONSULTANT        = "4";
var C_DEVELOPER                     = "5";
var C_CUST_PROJECT_SPONSOR          = "6";
var C_CUST_PROJECT_ADVISOR          = "7";
var C_CUST_PROJECT_MANAGER          = "8";
var C_CUST_PROJECT_MEMBER           = "9";

var jobquotation_obj = null ;
var job_quotation_idx = 2000;
var acc_mgr_idx = 0;
var proj_mgr_idx = 0;
var tech_consultant_idx = 0;
var app_consultant_idx = 0;
var dev_idx = 0;
var cust_sponsor_idx = 0;
var cust_advisor_idx = 0;
var cust_mgr_idx = 0;
var cust_member_idx = 0;

function changeTempOption() {
    if($('#chkTempOption').is(':checked')){
        $('#divTemplateList').hide();
        $('#divTemplateName').show();

        $('#txtChkTempOption').val("1");
    }else{
        $('#divTemplateList').show();
        $('#divTemplateName').hide();

        $('#txtChkTempOption').val("0");
    }

    populateTemplateList();
}

function selectAllCheckbox() {
    if($('#chkSelectAll').is(':checked')) {
        $('#contohdata-body tr').each(function() {
            $('.eq_check').prop('checked', true);
        });
    } else {
        $('#contohdata-body tr').each(function() {
            $('.eq_check').prop('checked', false);
        });
    }
}

function populateCustDetailQuotation() {
  var custId = $("#cobJobQuotationCustCode").val();
  
  var data = { "type": C_GET + "_CUSTDETAIL", "custId" : custId} ;
  var url = "request.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
  callServer(url,"json",data,onJobQuotationResponse,jobquotation_obj) ;
}

function populateLocationDetail() {
  var locId = $("#cobQuotationLocCode").val();
  var data = { "type": C_GET + "_LOCDETAIL", "locId" : locId} ;
  var url = "request.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
  callServer(url,"json",data,onJobQuotationResponse,jobquotation_obj) ;
}

// function populateModelbyBrandAndLoc() {
//   var brandId = $("#txtQuotationBrand").val();
//   var locId = $("#cobQuotationLocCode").val();
//   var data = { "type": C_GET + "_MODEL", "brandId" : brandId, "locId" : locId} ;
//   var url = "request.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
//   callServer(url,"json",data,onJobQuotationResponse,jobquotation_obj) ;
// }

// function populateSerialNumByBrandLocModel() {
//   var brandId = $("#txtQuotationBrand").val();
//   var locId = $("#cobQuotationLocCode").val();
//   var modelId = $("#txtQuotationModel").val();
//   var data = { "type": C_GET + "_SERIALNUMB", "brandId" : brandId, "locId" : locId, "modelId" : modelId} ;
//   var url = "request.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
//   callServer(url,"json",data,onJobQuotationResponse,jobquotation_obj) ;
// }

function filterJobQuotation(){
    var data = { 
        "type": C_JOB_QUOTATION_FILTER, 
        "status": 'all',
        "sorts": sorts,
        "index": screen_index,
    };
    var url  = "index.pzx?c=" + jobquotation_url + "&t=" + C_JOB_QUOTATION_FILTER  + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onJobQuotationResponse,null) ;
    return false;
}

function showAddJobQuotation(){
   

    screen_index ++;
    var data = { "type": C_ADD_JOB_QUOTATION_VIEW, "index":screen_index} ;
    var url  = "index.pzx?c=" + jobquotation_url + "&t=" + C_ADD_JOB_QUOTATION_VIEW  + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onJobQuotationResponse,null) ;
}

function get_table_value() {
    var lines = "" ;
    var sep = "<tr>" ;
    var bsep = "</tr>" ;

    $('#chkSelectAll').hide();
    $('#contohdata-body tr').each(function() { 
        
        
        checklist = ($('.eq_check',this).is(':checked') ? '1' : '0');

        if($('.eq_check',this).is(':checked')){
            $('.eq_check',this).remove();
            td_html = $(this).html();
            lines = lines + sep + td_html + bsep ; 
        }
        
    }); 
    
    $('#equipmentData-body').html(lines);

    var target = $(".nav-tabs li.active");
    var sibbling;
    
        sibbling = target.next();
    
    if (sibbling.is("li")) {
        sibbling.children("a").tab("show");
    }
}

function showJobEquipment() {
    $('#equipmentModal').modal('show');
}

function closeJobEquipment() {
    $('#equipmentModal').modal('hide');
}


//===========================================================
function showContentIndex(html) {
    if(screen_index == 0 ){

        $("#screen_container").html(html);
    
    }else{

        /**hide the previous index */
        $('#screen'+(screen_index-1)+'').hide();
        
        /**fill the index-1a or the next screen with HTML from controller */
        $('#screen'+(screen_index-1)+'a').html(html);
        /**show the main power */
        $('#screen'+(screen_index-1)+'a').show();

    }

    return;
}

function onJobQuotationResponse(obj,resp){
	//$('#activityLoadingModal').modal('hide');
    if (resp.status != C_OK ) {
        Swal.fire("Error",resp.mesg,"error");
        return false;
    }

    if(resp.type == C_JOB_QUOTATION_FILTER) {
        var wrapper = $('#jobquotation_row_filter');
        wrapper.nextAll().remove();
        wrapper.after(resp.data.htmlStr);

        filterData($('#txtcolstatusplan'),5);
    } else if (resp.type == C_ADD_JOB_QUOTATION_VIEW || resp.type == C_UPDATE_JOB_QUOTATION_VIEW) {                
        //$("#addJobQuotationView").modal("show");
        //$('#modalAddJobQuotationHeaderViewContent').html(resp.data.all);

        showContentIndex(resp.data.all);

        var functionName = "addJobQuotation()";
        var functionName2 = "#";
        var disableConvert = "disabled";
        var hidesavebutton = false;
        if (resp.type == C_UPDATE_JOB_QUOTATION_VIEW) {
            $('#txtJobQuotationId').val(resp.data.headerData.id) ;
            $('#cobJobQuotationBU').val(resp.data.headerData.bu) ;
            $('#cobJobQuotationCustCode').val(resp.data.headerData.cust) ;
            
            $('#txtJobQuotationPMName').html(resp.data.headerData.contact_list);
            $('#txtJobQuotationPMName').val(resp.data.headerData.contactname);
            $('#txtJobQuotationCoyName').val(resp.data.headerData.coyname) ;
            
            $('#txtQuotationJobNumber').val(resp.data.headerData.jobnumber) ;
            $('#txtQuotationJobName').val(resp.data.headerData.jobname) ;
            $('#txtQuotationJobEndDate').val(resp.data.headerData.jobenddate) ;
            $('#txtQuotationJobStartDate').val(resp.data.headerData.jobstartdate) ;

            $('#txtQuotationJobType').val(resp.data.headerData.jobtype) ;
            $('#txtQuotationProductType').val(resp.data.headerData.producttype) ;
            $('#txtJobQuotationTotalMandays').val(resp.data.headerData.totalmandays) ;
            $('#txtJobQuotationTotalHours').val(parseFloat(resp.data.headerData.totalmandays*8)) ;
            
            // $('#txtQuotationLocContactName').val(resp.data.headerData.contactname) ;
            // $('#txtQuotationLocContactPhone').val(resp.data.headerData.contactphone) ;
            // $('#txtQuotationLocContactEmail').val(resp.data.headerData.contactemail) ;
            // $('#txtQuotationLocContactAddress').val(resp.data.headerData.address) ;
            //$('#txtQuotationJobPlanNo').val(resp.data.headerData.plan_no) ;

            $('#cmbCustProjectSponsor0').html(resp.data.headerData.contact_list);
            $('#cmbCustProjectAdvisor0').html(resp.data.headerData.contact_list);
            $('#cmbCustProjectManager0').html(resp.data.headerData.contact_list);
            $('#cmbCustProjectMember0').html(resp.data.headerData.contact_list);          
          
            var myObj = JSON.parse(resp.data.headerData.jobcentre_task);
            var myResources = JSON.parse(resp.data.headerData.resources);
            
            showJobTask(myObj);
            showResources(myResources);
            calculateTotalMandays();

            var functionName = "updateJobQuotation()";
            var functionName2 = "convertToPlan("+resp.data.headerData.id+")";
            if(resp.data.headerData.converted != 1){
                var disableConvert = "";
                
            }else{
               var hidesavebutton =true;
            }
        }
       
        var strBtn = "<button id=\"ConvertToPlanBtn\" "+disableConvert+" type=\"button\" class=\"btn btn-success btn-elevate\" onclick=\"return "+functionName2+";\">Convert to Plan</button>&nbsp;" +
                         "<button type=\"button\" class=\"btn btn-secondary btn-elevate\" onclick=\"return prevscreen();\">Cancel</button>&nbsp;" +
                        "<button id=\"btnMasterAdd\" style=\""+( hidesavebutton ? "display:none":"" )+"\" class=\"btn btn-primary btn-elevate\" onclick=\"return "+functionName+";\">Save</button>";
       

        //var strBtn = '<button type="button" class="btn btn-success btn-elevate btn-elevate-air">Success</button>&nbsp;'
        $('#btnJobQuotation').html(strBtn); 
    } else if (resp.type == C_GET + '_CUSTDETAIL') {
      $("#txtJobQuotationCoyName").val(resp.data.coyName);
      $("#txtJobQuotationPMName").html(resp.data.name);      
      
      $('#cmbCustProjectSponsor0').html(resp.data.name);
      $('#cmbCustProjectAdvisor0').html(resp.data.name);
      $('#cmbCustProjectManager0').html(resp.data.name);
      $('#cmbCustProjectMember0').html(resp.data.name);  
    }else if (resp.type == C_ADD) {
        Swal.fire(
            "Success",
            resp.mesg,
            "success"
        );        
        prevscreen();
        filterJobQuotation();        
    } else if (resp.type == C_UPDATE) {
        Swal.fire("Success",resp.mesg,"success");
        // editCustomer(resp.data.headerData.id,obj);
        prevscreen();
        filterJobQuotation();        
    } else if (resp.type == C_DELETE_JOB_CENTRE) {
        Swal.fire("Success",resp.mesg,"success");
        filterJobQuotation();
    }else if(resp.type == "C_CONVERT_PLAN"){
        Swal.fire("Success",resp.mesg,"success");
        prevscreen();
        filterJobQuotation();        
    }else if(resp.type == "C_SAVE_QUOTATION_TEMPLATE"){
        $('#quotationTemplateName').val("");
        $('#chkTempOption').prop('checked', true);
        $('#QuotationTemplateList').val(0);
        // $('#quotationTemplateNewName').val("");

        $('#quotationTemplateModal').modal("hide");
        Swal.fire("Success",resp.mesg,"success");
    }else if(resp.type == "C_VIEW_LOAD_QUOTATION_TEMPLATE"){
        // var strBtn = "<button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Cancel</button>" +
        //              "<button onclick=\"LoadTemplateQuotation();\" class=\"btn btn-primary\" > Load </button>";
        //$('#modalLoadQuotationTemplateConfirmationButton').html(strBtn);
        $("#QuotationTemplateLoad").html(resp.data.load_option);
        $("#quotationTemplateLoadModal").modal("show");
      
    } else if(resp.type == "C_VIEW_LOAD_QUOTATION_TEMPLATE_LIST"){
        $("#QuotationTemplateList").html(resp.data.load_option);
      
    }else if(resp.type == "C_LOAD_QUOTATION_TEMPLATE"){
         var myObj = JSON.parse(resp.data.jobquotation_task);
            
         showJobTasktemplate(myObj);
        $('#quotationTemplateLoadModal').modal('hide')
    } else if(resp.type == "C_DELETE_QUOTATION_TEMPLATE"){
        Swal.fire("Success",resp.mesg,"success");
        $('#quotationTemplateLoadModal').modal('hide')
    }
}

function addJobQuotation() {
    saveJobQuotation(C_ADD);
}

function updateJobQuotation() {
    saveJobQuotation(C_UPDATE) ;
}

function saveJobQuotation(type){
    if (validateJobQuotation()){
        var data = {    "type"              : type,
                        "id"                : $('#txtJobQuotationId').val(),
                        "coyId"             : $('#cobJobQuotationBU').val(),
                    
                        "idcust"            : $('#cobJobQuotationCustCode').val(), 
                        "idcontact"         : $('#txtJobQuotationPMName').val(), 
                        "coyname"           : $('#txtJobQuotationCoyName').val(),
                        "jobnumber"         : $('#txtQuotationJobNumber').val(),
                        "jobname"           : $('#txtQuotationJobName').val(),
                        "jobtype"           : $('#txtQuotationJobType').val(),
                        "jobenddate"        : $('#txtQuotationJobEndDate').val(),
                        "jobstartdate"      : $('#txtQuotationJobStartDate').val(),
                        "producttype"       : $('#txtQuotationProductType').val(),
                        "totalmandays"      : $('#txtJobQuotationTotalMandays').val(),
                        "job_task"           : getJobTask(),
                        "resources"         : getResources(),
                    };

                        
        var url  = "index.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
        callServer(url,"json",data,onJobQuotationResponse,jobquotation_obj) ;
    } 
}
function popupDeleteJobQuotation (id, obj){    
        Swal.fire({
            title: "Delete Confirmation",
            text: "Confirm you want to delete the record?",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "Yes",
            // cancelButtonColor: '#d33',
            confirmButtonColor: '#d33',
        }).then(function(result) {
            if (result.value) {
                var data = { 
                    "type": C_DELETE_JOB_CENTRE,
                    "id": id
                } ;
                var url = "index.pzx?c=" + jobquotation_url + "&t=" + C_DELETE_JOB_CENTRE  + "&d=" + new Date().getTime() ; 
                callServer(url,"json",data,onJobQuotationResponse,null) ;
            }
        });
}

function validateJobQuotation(){
    if ($('#cobJobQuotationCustCode').val()==0) {        
        Swal.fire({
            title:"Warning",
            text: "Customer Code cannot be blank",
            type: "warning"
        });
        return false;
    }

    if ($('#txtQuotationJobName').blank()) {
        Swal.fire({
            title:"Warning",
            text: "Job Title cannot be blank",
            type: "warning"
        });
       
        return false;
    }

    $("#saveJobQuotationConfirmationView").modal("hide");

    return true;
}

function showEditJobQuotation(id, obj) {
    jobquotation_obj = obj ;
    screen_index++;
    var data  = { "type": C_UPDATE_JOB_QUOTATION_VIEW, "id" : id, "index":screen_index} ;
    var url   = "index.pzx?c=" + jobquotation_url + "&t=" + C_UPDATE_JOB_QUOTATION_VIEW  + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onJobQuotationResponse,null) ;
}

function lineRemarksJobTask(idx,status) {
    $("#lineRemakrsJobTaskView").modal("show");

    $("#modalLineRemakrsJobTaskContent").html('<textarea class="form-control" rows="3" maxlength="250" id="txtLineRemarksTask'+idx+'"></textarea>');

    if($('#remark-job-task'+idx).val() !="") {
        $("#txtLineRemarksTask"+idx).val($('#remark-job-task'+idx).val()); 
    }

    var strBtn = "";
    if(status==0) {
        strBtn = "<button type=\"button\" class=\"btn btn-secondary\" data-dismiss=\"modal\">Cancel</button>" +
                        "<button id=\"btnSaveLC\" class=\"btn btn-primary ml-2\" onclick=\"saveLineRemarksTask("+idx+");\">Save</button>" ;

        $('#modalLineRemakrsJobTaskButton').html(strBtn); 
    } else {
        $('#modalLineRemakrsJobTaskButton').html(strBtn); 
    }    
}

function saveLineRemarksTask(idx) {
    $("#lineRemakrsJobTaskView").modal("hide");

    remarks = $("#txtLineRemarksTask"+idx).val();
    $("#remark-job-task"+idx).val(remarks);
}

function addJobTask(obj) {
    
    job_quotation_idx++;

    var date = $('#txtQuotationJobStartDate').val();
    
    var cmbJobVenue = '<select id="cmbJobVenue'+job_quotation_idx+'" class="form-control cmbJobVenue">'+$('#cmbJobVenue').html()+'</select>';

   $('#job-task-table').append(

            '<tr id="trJobTask' + job_quotation_idx + '" class="dnd-moved kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;" valign="top">' + 
                '<input  class="col-lg-12 form-control txtIdJobTask" id="txtIdJobTask'+job_quotation_idx+'" value="-1">' + 
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<input type="text" class="col-lg-12 form-control txtPhaseJobTask" id="phase-task'+job_quotation_idx+'">'+
            '</td>'+

            '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
                '<input type="text" class="col-lg-12 form-control txtTaskJobTask" id="task-task'+job_quotation_idx+'">' + 
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
            '<textarea class="col-lg-12 form-control txtRemarkJobTask" id="remark-task'+job_quotation_idx+'" rows="2"></textarea>'+                
            '</td>' +
    
            '<td width="15.5%" class="kt-datatable__cell" style="display:none;vertical-align:top;">' + 
                '<input type="text" class="form-control txtLineRemarkJobTask" id="remark-job-task'+job_quotation_idx+'">' + 
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<div class="input-group date">'+
                    '<input type="text" class="col-lg-12 form-control txtStartDateJobTask"  placeholder="Select date" id="startdate-task' + job_quotation_idx + '" maxlength="10" onchange="setTaskDate(' + job_quotation_idx + ');" onclick="calculateTotalMandays();" value="'+date+'"/>'+
                    '<div class="input-group-append">'+
                        '<span class="input-group-text">'+
                            '<i class="la la-calendar-check-o"></i>'+
                        '</span>'+
                    '</div>'+
                '</div>'+
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<div class="input-group date">'+
                    '<input type="text" class="col-lg-12 form-control txtEndDateJobTask"  placeholder="Select date" id="enddate-task' + job_quotation_idx + '" maxlength="10" value="'+date+'" onchange="setTaskDate(' + job_quotation_idx + ');" onclick="calculateTotalMandays();"/>'+
                    '<div class="input-group-append">'+
                        '<span class="input-group-text">'+
                            '<i class="la la-calendar-check-o"></i>'+
                        '</span>'+
                    '</div>'+
                '</div>'+
            '</td>' +

        
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<input onkeyup="calculateTotalMandays()" id="hours-task' + job_quotation_idx + '" min="0" type="number" class="col-lg-12 form-control text-right txtHoursJobTask">'+
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
            cmbJobVenue +
            '</td>' +

            '<td  class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;padding-right:0;">'+
                '<button title="Line Remarks" id="btn-remark-'+job_quotation_idx+'" onclick="lineRemarksJobTask('+job_quotation_idx+', 0);" id="btnRemarks" class="btn btn-warning btn-sm btn-icon btn-icon-md"><i class="fa fa-comments" aria-hidden="true"></i></button>'+
            '</td>'+
            
            '<td class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;padding-right:0;" >'+
                '<button title="Copy" onclick="javascript: copyJobTask('+job_quotation_idx+', this);" id="btnCopy" class="btn btn-info btn-sm btn-icon btn-icon-md"><i class="fa fa-copy"></i></button>'+
            '</td>'+

            '<td class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;" >'+
                '<button title="Delete" onclick="javascript: deleteJobTask('+job_quotation_idx+', this);" id="btnDelete" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>'+
            '</tr>'
        ); 
        

        $('.txtStartDateJobTask').datepicker({
            format: "dd/mm/yyyy",
            useCurrent : true,
            todayHighlight: true
        });

        $('.txtEndDateJobTask').datepicker({
            format: "dd/mm/yyyy",
            useCurrent : true,
            todayHighlight: false,
            onSelect: function() {
                minDate : $('#startdate-task' + job_quotation_idx).val()
            }
        });

        $('#enddate-task' + job_quotation_idx).datepicker().datepicker('setDate', $('#enddate-task' + job_quotation_idx).val() );
        
        calculateTotalMandays();
    
}
function copyJobTask(idx) {
    $('#trJobTask'+idx).each(function() { 
        phase       = $('.txtPhaseJobTask',this).val()  ;
        task        = $('.txtTaskJobTask',this).val()  ;
        desc        = $('.txtRemarkJobTask',this).val()  ;        
        start_date  = $('.txtStartDateJobTask',this).val()  ;
        end_date    = $('.txtEndDateJobTask',this).val()  ;
        hours       = $('.txtHoursJobTask',this).val()  ;
        venue       = $('.cmbJobVenue',this).val()  ;
        remarks     = $('.txtLineRemarkJobTask',this).val()  ;
    }); 

    job_quotation_idx++;  

    var cmbJobVenue = '<select id="cmbJobVenue'+job_quotation_idx+'" class="form-control cmbJobVenue">'+$('#cmbJobVenue').html()+'</select>';

    $('#job-task-table').append(
        '<tr id="trJobTask' + job_quotation_idx + '" class="dnd-moved kt-datatable__row">'+
        '<td class="kt-datatable__cell" style="display:none;" valign="top">' + 
            '<input  class="col-lg-12 form-control txtIdJobTask" id="txtIdJobTask'+job_quotation_idx+'" value="-1">' + 
        '</td>' +

        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            '<input type="text" class="col-lg-12 form-control txtPhaseJobTask" id="phase-task'+job_quotation_idx+'" value="'+phase+'">'+
        '</td>'+

        '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
            '<input type="text" class="col-lg-12 form-control txtTaskJobTask" id="task-task'+job_quotation_idx+'" value="'+task+'">' + 
        '</td>' +
    
        '<td width="15.5%" class="kt-datatable__cell" style="display:none;vertical-align:top;">' + 
            '<input type="text" class="form-control txtLineRemarkJobTask" id="remark-job-task'+job_quotation_idx+'" value="'+remarks+'">' + 
        '</td>' +

        '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
        '<textarea class="col-lg-12 form-control txtRemarkJobTask" id="remark-task'+job_quotation_idx+'" rows="2" >'+desc+'</textarea>'+            
        '</td>' +

        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            '<div class="input-group date">'+
                '<input type="text" class="col-lg-12 form-control txtStartDateJobTask"  placeholder="Select date" id="startdate-task' + job_quotation_idx + '" maxlength="10" onchange="setTaskDate(' + job_quotation_idx + ');" onclick="calculateTotalMandays();" value="'+start_date+'"/>'+
                '<div class="input-group-append">'+
                    '<span class="input-group-text">'+
                        '<i class="la la-calendar-check-o"></i>'+
                    '</span>'+
                '</div>'+
            '</div>'+
        '</td>' +

        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            '<div class="input-group date">'+
                '<input type="text" class="col-lg-12 form-control txtEndDateJobTask"  placeholder="Select date" id="enddate-task' + job_quotation_idx + '" maxlength="10" onchange="setTaskDate(' + job_quotation_idx + ');" onclick="calculateTotalMandays();"  value="'+end_date+'"/>'+
                '<div class="input-group-append">'+
                    '<span class="input-group-text">'+
                        '<i class="la la-calendar-check-o"></i>'+
                    '</span>'+
                '</div>'+
            '</div>'+
        '</td>' +

        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            '<input onkeyup="calculateTotalMandays()" id="hours-task' + job_quotation_idx + '" min="0" type="number" class="col-lg-12 form-control text-right txtHoursJobTask" value="'+hours+'">'+
        '</td>' +

        '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
        cmbJobVenue +
        '</td>' +

        '<td  class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;padding-right:0;">'+
            '<button title="Line Remarks" id="btn-remark-'+job_quotation_idx+'" onclick="lineRemarksJobTask('+job_quotation_idx+', 0);" id="btnRemarks" class="btn btn-warning btn-sm btn-icon btn-icon-md"><i class="fa fa-comments" aria-hidden="true"></i></button>'+
        '</td>'+

        '<td class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;padding-right:0;" >'+
            '<button title="Copy" onclick="javascript: copyJobTask('+job_quotation_idx+', this);" id="btnCopy" class="btn btn-info btn-sm btn-icon btn-icon-md"><i class="fa fa-copy"></i></button>'+
        '</td>'+

        '<td class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;" >'+
            '<button title="Delete" onclick="javascript: deleteJobTask('+job_quotation_idx+', this);" id="btnDelete" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
        '</td>'+
        '</tr>'
    );
    
    $('.txtStartDateJobTask').datepicker({
        format: "dd/mm/yyyy",
            useCurrent : true,
            todayHighlight: true
    });

    $('.txtEndDateJobTask').datepicker({
        format: "dd/mm/yyyy",
        useCurrent : true,
        todayHighlight: false,
        onSelect: function() {
            minDate : $('#startdate-task' + job_quotation_idx).val()
        }
    });

    $('#enddate-task' + job_quotation_idx).datepicker().datepicker('setDate', $('#enddate-task' + job_quotation_idx).val() );
   
    /**calculate after copy the data */
    calculateTotalMandays();
}

function deleteJobTask(idx) {
    $('#trJobTask'+idx).remove() ;
    calculateTotalMandays();
}

function getJobTask(){
    var arrays = [];
    $('#job-task-table tr').each(function() { 

        var data = {     
           

                    "id"            :$('.txtIdJobTask',this).val()  ,
                    "phase"         :$('.txtPhaseJobTask',this).val()  ,
                    "task"          :$('.txtTaskJobTask',this).val()  ,
                    "scope"         :$('.txtRemarkJobTask',this).val()  ,  
                    "start_date"    :$('.txtStartDateJobTask',this).val()  ,
                    "end_date"      :$('.txtEndDateJobTask',this).val()  ,
                    "hours"         :$('.txtHoursJobTask',this).val()  ,  
                    "venue"         :$('.cmbJobVenue',this).val()  ,    
                    "remarks"        :$('.txtLineRemarkJobTask',this).val()  ,  
                    
                   };   

        arrays.push(data);
    }); 
    
    return JSON.stringify(arrays) ;
}

function showJobTask(myObj){
    job_quotation_idx = 0;
    var id_max_q = [2000];
        
    
    for (i in myObj) {

        id = myObj[i].id;

        phase = myObj[i].phase;
        task = myObj[i].task;
        scope = myObj[i].scope;
        start_date = myObj[i].date;
        hours = myObj[i].hours;
        end_date = myObj[i].date_end;
        venue = myObj[i].venue;
        converted = myObj[i].converted;
        remarks = myObj[i].remarks;

        id_max_q.push(id);
    
        var cmbJobVenue = '<select id="cmbJobVenue'+id+'" class="form-control cmbJobVenue">'+$('#cmbJobVenue').html()+'</select>';

        if(hours == 0) {
            val_hours = '';
        } else {
            val_hours = 'value="'+hours+'"';
        }

        $('#job-task-table').append(
            '<tr id="trJobTask' + id + '" class="dnd-moved kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;" valign="top">' + 
                '<input  class="col-lg-12 form-control txtIdJobTask" id="txtIdJobTask'+id+'" value="'+id+'">' + 
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<input type="text" class="col-lg-12 form-control txtPhaseJobTask" id="phase-task'+id+'" value="'+phase+'">'+
            '</td>'+

            '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
                '<input type="text" class="col-lg-12 form-control txtTaskJobTask" id="task-task'+id+'" value="'+task+'">' + 
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
            '<textarea class="col-lg-12 form-control txtRemarkJobTask" id="remark-task'+id+'" rows="2" >'+scope+'</textarea>'+                
            '</td>' +
    
            '<td width="15.5%" class="kt-datatable__cell" style="display:none;vertical-align:top;">' + 
                //'<input type="text" class="form-control txtLineRemarkJobTask" id="remark-job-task'+id+'" value="'+remarks+'">' + 
                '<textarea class="form-control txtLineRemarkJobTask" rows="3" maxlength="250" id="remark-job-task'+id+'">'+remarks+'</textarea>'+
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<div class="input-group date">'+
                    '<input type="text" class="col-lg-12 form-control txtStartDateJobTask"  placeholder="Select date" id="startdate-task' + id + '" maxlength="10" onchange="setTaskDate(' + id + ');" onclick="calculateTotalMandays();" value="'+start_date+'"/>'+
                    '<div class="input-group-append">'+
                        '<span class="input-group-text">'+
                            '<i class="la la-calendar-check-o"></i>'+
                        '</span>'+
                    '</div>'+
                '</div>'+
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<div class="input-group date">'+
                    '<input type="text" class="col-lg-12 form-control txtEndDateJobTask"  placeholder="Select date" id="enddate-task' + id + '" maxlength="10" onchange="setTaskDate(' + id + ');" onclick="calculateTotalMandays();"  value="'+end_date+'"/>'+
                    '<div class="input-group-append">'+
                        '<span class="input-group-text">'+
                            '<i class="la la-calendar-check-o"></i>'+
                        '</span>'+
                    '</div>'+
                '</div>'+
            '</td>' +
        
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<input onkeyup="calculateTotalMandays()" id="hours-task' + id + '" min="0" type="number" class="col-lg-12 form-control text-right txtHoursJobTask" '+val_hours+'>'+
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
            cmbJobVenue +
            '</td>' +

            '<td  class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;padding-right:0;">'+
                '<button title="Line Remarks" id="btn-remark-'+id+'" onclick="lineRemarksJobTask('+id+', '+converted+');" id="btnRemarks" class="btn btn-warning btn-sm btn-icon btn-icon-md"><i class="fa fa-comments" aria-hidden="true"></i></button>'+
            '</td>'+
            
            '<td class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;padding-right:0;" >'+
                '<button title="Copy" onclick="javascript: copyJobTask('+id+', this);" id="btnCopy" class="btn btn-info btn-sm btn-icon btn-icon-md"><i class="fa fa-copy"></i></button>'+
            '</td>' +
            '<td class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;" >'+
                '<button title="Delete" onclick="javascript: deleteJobTask('+id+', this);" id="btnDelete" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>'+
            '</tr>'
         );
         
        $('#cmbJobVenue'+id).val(venue);
     
        $('.txtStartDateJobTask').datepicker({
            format: "dd/mm/yyyy",
            useCurrent : true,
            todayHighlight: true
        });       
        
        $('.txtEndDateJobTask').datepicker({
            format: "dd/mm/yyyy",
            useCurrent : true,
            todayHighlight: false,
            minDate : $('#startdate-task' + id).val()
        });

        $('#enddate-task' + id).datepicker().datepicker('setDate', $('#enddate-task' + id).val() );

        job_quotation_idx = parseInt(id) +1;
    }

    job_quotation_idx = Math.max.apply(null, id_max_q);    
}

function showJobTasktemplate(myObj){
    job_quotation_idx = 2000;

    var id_max_q = [2000];

    var date = $('#txtQuotationJobStartDate').val();
    $('#job-task-table').html("");
    for (i in myObj) {

        id = myObj[i].id;
        phase = myObj[i].phase;
        task = myObj[i].task;
        scope = myObj[i].scope;       
        hours = myObj[i].hours;
        venue = myObj[i].venue;
        converted = myObj[i].converted;
        remarks = myObj[i].remarks;
       
        id_max_q.push(id);
        
        var cmbJobVenue = '<select id="cmbJobVenue'+id+'" class="form-control cmbJobVenue">'+$('#cmbJobVenue').html()+'</select>';

        if(hours == 0) {
            val_hours = '';
        } else {
            val_hours = 'value="'+hours+'"';
        }

        $('#job-task-table').append(
            '<tr id="trJobTask' + id + '" class="dnd-moved kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;" valign="top">' + 
                '<input  class="col-lg-12 form-control txtIdJobTask" id="txtIdJobTask'+id+'" value="'+id+'">' + 
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<input type="text" class="col-lg-12 form-control txtPhaseJobTask" id="phase-task'+id+'" value="'+phase+'">'+
            '</td>'+

            '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
                '<input type="text" class="col-lg-12 form-control txtTaskJobTask" id="task-task'+id+'" value="'+task+'">' + 
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
            '<textarea class="col-lg-12 form-control txtRemarkJobTask" id="remark-task'+id+'" rows="2" >'+scope+'</textarea>'+                
            '</td>' +
    
            '<td width="15.5%" class="kt-datatable__cell" style="display:none;vertical-align:top;">' + 
                //'<input type="text" class="form-control txtLineRemarkJobTask" id="remark-job-task'+id+'" value="'+remarks+'">' + 
                '<textarea class="form-control txtLineRemarkJobTask" rows="3" maxlength="250" id="remark-job-task'+id+'">'+remarks+'</textarea>'+
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<div class="input-group date">'+
                    '<input type="text" class="col-lg-12 form-control txtStartDateJobTask"  placeholder="Select date" id="startdate-task' + id + '" maxlength="10" onchange="setTaskDate(' + id + ');" onclick="calculateTotalMandays();" value="'+date+'"/>'+
                    '<div class="input-group-append">'+
                        '<span class="input-group-text">'+
                            '<i class="la la-calendar-check-o"></i>'+
                        '</span>'+
                    '</div>'+
                '</div>'+
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<div class="input-group date">'+
                    '<input type="text" class="col-lg-12 form-control txtEndDateJobTask"  placeholder="Select date" id="enddate-task' + id + '" maxlength="10" onchange="setTaskDate(' + id + ');" onclick="calculateTotalMandays();"  value="'+date+'"/>'+
                    '<div class="input-group-append">'+
                        '<span class="input-group-text">'+
                            '<i class="la la-calendar-check-o"></i>'+
                        '</span>'+
                    '</div>'+
                '</div>'+
            '</td>' +
        
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                '<input onkeyup="calculateTotalMandays()" id="hours-task' + id + '" min="0" type="number" class="col-lg-12 form-control text-right txtHoursJobTask" '+val_hours+'>'+
            '</td>' +

            '<td class="kt-datatable__cell" style="vertical-align:top;">' + 
            cmbJobVenue +
            '</td>' +

            '<td  class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;padding-right:0;">'+
                '<button title="Line Remarks" id="btn-remark-'+id+'" onclick="lineRemarksJobTask('+id+', '+converted+');" id="btnRemarks" class="btn btn-warning btn-sm btn-icon btn-icon-md"><i class="fa fa-comments" aria-hidden="true"></i></button>'+
            '</td>'+
            
            '<td class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;padding-right:0;" >'+
                '<button title="Copy" onclick="javascript: copyJobTask('+id+', this);" id="btnCopy" class="btn btn-info btn-sm btn-icon btn-icon-md"><i class="fa fa-copy"></i></button>'+
            '</td>'+

            '<td class=\"kt-datatable__cell\" style="vertical-align:top;width:2%;" >'+
                '<button title="Delete" onclick="javascript: deleteJobTask('+id+', this);" id="btnDelete" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>'+
            '</tr>'
         );

         $('#cmbJobVenue'+id).val(venue);
          
        //  $('.txtStartDateJobTask').datepicker({
        //     format: "dd/mm/yyyy"
        // });
        // $('.txtStartDateJobTask').on('changeDate', function(ev){
        //     $(this).datepicker('hide');
         
        // });
        
        // $('.txtEndDateJobTask').datepicker({
        //     format: "dd/mm/yyyy"
        // });
        // $('.txtEndDateJobTask').on('changeDate', function(ev){
        //     $(this).datepicker('hide');
            
        // });

        $('.txtStartDateJobTask').datepicker({
            format: "dd/mm/yyyy",
            useCurrent : true,
            todayHighlight: true
        });       
        
        $('.txtEndDateJobTask').datepicker({
            format: "dd/mm/yyyy",
            useCurrent : true,
            todayHighlight: false,
            minDate : $('#startdate-task' + id).val()
        });       

         job_quotation_idx = parseInt(id) +1;
    }

    job_quotation_idx = Math.max.apply(null, id_max_q);
    calculateTotalMandays();
}

function setTaskDate(idx) {
    //Calculate Day
    var startDate = moment($('#startdate-task'+idx).val(), "DD/MM/YYYY");
    var endDate   = moment($('#enddate-task'+idx).val(), "DD/MM/YYYY");
    Days          = endDate.diff(startDate, 'days');

    if (Days+1<=0) {
        Swal.fire('Error', "Date not valid, End Date must more than Start Date.", "error");

        $('#enddate-task' + idx).val($('#startdate-task' + idx).val());
        $('#enddate-task' + idx).datepicker({
            format: "dd/mm/yyyy",
            useCurrent : true,
            todayHighlight: false,
            minDate : $('#startdate-task' + idx).val()
        });

        $('#enddate-task' + idx).datepicker().datepicker('setDate', $('#enddate-task' + idx).val() );
        return false;
    }
}

function convertToPlan(id) {
    $('#ConvertToPlanBtn').prop('disabled','disabled');
    var data = { "type"         : "C_CONVERT_PLAN",
                 "id"           : id,
                 "job_task"     : getJobTask(),
                 "resources"    : getResources(),
                };
    var url  = "index.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onJobQuotationResponse,null) ;
}

function getTimeTrackerDaysUpdateQuotation(start,end){
    //Calculate Day
    var startDate = moment(start, "DD/MM/YYYY");
    var endDate = moment(end, "DD/MM/YYYY");
    var Days = endDate.diff(startDate, 'days');
        
    return Days;
}

function calculateTotalMandays(){
    var total_hours = 0;
    var total_mandays = 0;
    $('#job-task-table tr').each(function() {
        var hours = $('.txtHoursJobTask',this).val();
        if(hours == "") {
            hours = 0;
        }       

        var mandays = parseFloat(hours);
        total_hours += mandays;
    }); 
    total_mandays = parseFloat(total_hours/8);
    $('#txtAllocatedHours').val(total_hours);
    // $('#txtJobQuotationTotalHours').val(total_hours);
    // $('#txtJobQuotationTotalMandays').val(total_mandays);    
}

function calculateMandaysToHours(){    
    var total_mandays = $('#txtJobQuotationTotalMandays').val();
    var total_hours = parseFloat(total_mandays*8);    
    $('#txtJobQuotationTotalHours').val(total_hours);
}

function calculateHoursToMandays(){
    var total_hours = $('#txtJobQuotationTotalHours').val();
    var total_mandays = parseFloat(total_hours/8);    
    $('#txtJobQuotationTotalMandays').val(total_mandays);
}

function popupSaveTemplateQuotation() {
    var strBtn = "<button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Cancel</button>" +
                     "<button onclick=\"saveTemplateQuotation();\" class=\"btn btn-primary\" > Save </button>";
    $('#modalSaveQuotationTemplateConfirmationButton').html(strBtn);    
    $("#SaveQuotationTemplateConfirmationView").modal("show");
}

function saveTemplateQuotation() {
    $("#SaveQuotationTemplateConfirmationView").modal("hide");
    var data = { 
            "name"              : $('#quotationTemplateName').val(),
            "data"              : getJobTask(),
            "option_template"   : $('#txtChkTempOption').val(),
            "template_id"       : $('#QuotationTemplateList').val(),
            // "new_name"          : $('#quotationTemplateNewName').val(),    
            "type"              : "C_SAVE_QUOTATION_TEMPLATE"
            };
    var url = "index.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onJobQuotationResponse,null) ;
}
function popupLoadTemplateQuotation() {
       
    var data = { "type"                 : "C_VIEW_LOAD_QUOTATION_TEMPLATE" };
    var url = "index.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onJobQuotationResponse,null) ;
}

function LoadTemplateQuotation() {
    $("#LoadQuotationTemplateConfirmationView").modal("hide");
    var data = { 
            "template_id"          : $('#QuotationTemplateLoad').val(),        
            "type"                 : "C_LOAD_QUOTATION_TEMPLATE"
            };
    var url = "index.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onJobQuotationResponse,null) ;
}

function DeleteTemplateQuotation() {
    $("#LoadQuotationTemplateConfirmationView").modal("hide");
    var data = { 
            "template_id"          : $('#QuotationTemplateLoad').val(),
            "type"                 : "C_DELETE_QUOTATION_TEMPLATE"
            };
    var url = "index.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onJobQuotationResponse,null) ;
}

function populateTemplateList() {
    var data = { "type"                 : "C_VIEW_LOAD_QUOTATION_TEMPLATE_LIST" };
    var url = "index.pzx?c=" + jobquotation_url + "&d=" + new Date().getTime() ;
  
    callServer(url,"json",data,onJobQuotationResponse,null) ;
}

function showJobQuotationPrintPreview (id){
    var url = "index.pzx?c=" + jobquotation_url + "&id=" + id + "&t=" + C_SHOW_JOB_QUOTATION_REPORT+"&r=1";
    showReport(url) ;
}

function showJobQuotationExport (id){
    var url = "index.pzx?c=" + jobquotation_url + "&id=" + id + "&t=" + C_SHOW_JOB_QUOTATION_EXPORT+"&r=1";
    showReport(url) ;
}

function getResources() {
    var arrays = [];
    var acc_mgr_array = getAccountManager();
    arrays.push(acc_mgr_array);
    
    var cust_sponsor_array = getCustProjectSponsor();
    arrays.push(cust_sponsor_array);

    var proj_mgr_array = getProjectManager();
    arrays.push(proj_mgr_array);
    
    var cust_advisor_array = getCustProjectAdvisor();
    arrays.push(cust_advisor_array);

    var tech_consultant_array = getTechnicalConsultant();
    arrays.push(tech_consultant_array);
    
    var cust_manager_array = getCustProjectManager();
    arrays.push(cust_manager_array);
    
    var app_consultant_array = getApplicationConsultant();
    arrays.push(app_consultant_array);
    
    var cust_member_array = getCustProjectMember();
    arrays.push(cust_member_array);
    
    var developer_array = getProjectDeveloper();
    arrays.push(developer_array);

    // console.log(arrays);
    return JSON.stringify(arrays) ;
}

function showResources(resources) {
    // console.log(resources);
    showAccountManager(resources.account_manager);
    showProjectManager(resources.project_manager);
    showTechnicalConsultant(resources.tech_consultant);
    showApplicationConsultant(resources.app_consultant);
    showProjectDeveloper(resources.developer);
    showCustProjectSponsor(resources.cust_proj_sponsor);
    showCustProjectAdvisor(resources.cust_proj_advisor);
    showCustProjectManager(resources.cust_proj_manager);
    showCustProjectMember(resources.cust_proj_member);
}

function showAccountManager(obj) {
    if(obj.length > 0) {
        acc_mgr_idx = obj.length-1;
        $('#txtIdAccountManager0').val(obj[0].resourceid);
        $('#cmbAccountManager0').val(obj[0].resourceuser);
        
        if(obj.length > 1) {
            for(var i=1; i<obj.length; i++) {
                var acc_mgr = '<select id="cmbAccountManager'+i+'" class="col-lg-12 form-control">'+$('#cmbAccountManager0').html()+'</select>';
                $('#acc-mgr-table').append(
                    '<tr id="trAccountManager-' + i + '" class="kt-datatable__row">'+
                        '<td class="kt-datatable__cell" style="display:none;">' + 
                            '<input  class="form-control" id="txtIdAccountManager'+i+'" value="'+obj[i].resourceid+'">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                            acc_mgr +
                        '</td>' +
                        '<td class="kt-datatable__cell">'+
                            '<button title="Delete" onclick="javascript: deleteAccountManager('+i+', this);" id="btnDeleteAccountManager" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
                        '</td>' +
                    '</tr>'
                );                
                $('#cmbAccountManager'+i).val(obj[i].resourceuser);
            }
        }
    }
}
function addAccountManager() {
    acc_mgr_idx++;
    var acc_mgr = '<select id="cmbAccountManager'+acc_mgr_idx+'" class="col-lg-12 form-control">'+$('#cmbAccountManager0').html()+'</select>';
    $('#acc-mgr-table').append(
        '<tr id="trAccountManager-' + acc_mgr_idx + '" class="kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;">' + 
                '<input  class="form-control" id="txtIdAccountManager'+acc_mgr_idx+'" value="-1">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                acc_mgr +
            '</td>' +
            '<td class="kt-datatable__cell">'+
                '<button title="Delete" onclick="javascript: deleteAccountManager('+acc_mgr_idx+', this);" id="btnDeleteAccountManager" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>' +
        '</tr>'
    ); 
}
function deleteAccountManager(idx) {
    $('#trAccountManager-'+idx).remove() ;
}
function getAccountManager(){
    var arrays = [];
    var data = {};
    $('#acc-mgr-table tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);
        if($('#cmbAccountManager'+idx).val() != 0) {
            data = { 
                "resourceid"        : $('#txtIdAccountManager'+idx).val()  ,
                "resourcetype"      : C_ACCOUNT_MANAGER,
                "resourceuser"      : $('#cmbAccountManager'+idx).val()  ,      
                };
            
            arrays.push(data);
        }
    });
    
    return arrays;
}

function showProjectManager(obj) {
    if(obj.length > 0) {
        proj_mgr_idx = obj.length-1;
        $('#txtIdProjectManager0').val(obj[0].resourceid);
        $('#cmbProjectManager0').val(obj[0].resourceuser);
        
        if(obj.length > 1) {
            for(var i=1; i<obj.length; i++) {
                var proj_mgr = '<select id="cmbProjectManager'+i+'" class="col-lg-12 form-control">'+$('#cmbProjectManager0').html()+'</select>';
                $('#proj-mgr-table').append(
                    '<tr id="trProjectManager-' + i + '" class="kt-datatable__row">'+
                        '<td class="kt-datatable__cell" style="display:none;">' + 
                            '<input  class="form-control" id="txtIdProjectManager'+i+'" value="'+obj[i].resourceid+'">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                        proj_mgr +
                        '</td>' +
                        '<td class="kt-datatable__cell">'+
                        '<button title="Delete" onclick="javascript: deleteProjectManager('+i+', this);" id="btnDeleteProjectManager" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
                        '</td>' +
                    '</tr>'
                );                
                $('#cmbProjectManager'+i).val(obj[i].resourceuser);
            }
        }
    }
}
function addProjectManager() {
    proj_mgr_idx++;
    var proj_mgr = '<select id="cmbProjectManager'+proj_mgr_idx+'" class="col-lg-12 form-control">'+$('#cmbProjectManager0').html()+'</select>';
    $('#proj-mgr-table').append(
        '<tr id="trProjectManager-' + proj_mgr_idx + '" class="kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;">' + 
                '<input  class="form-control" id="txtIdProjectManager'+proj_mgr_idx+'" value="-1">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            proj_mgr +
            '</td>' +
            '<td class="kt-datatable__cell">'+
                '<button title="Delete" onclick="javascript: deleteProjectManager('+proj_mgr_idx+', this);" id="btnDeleteProjectManager" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>' +
        '</tr>'
    ); 
}
function deleteProjectManager(idx) {
    $('#trProjectManager-'+idx).remove() ;
}
function getProjectManager(){
    var arrays = [];
    var data = {};
    $('#proj-mgr-table tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);
        if($('#cmbProjectManager'+idx).val() != 0) {
            data = { 
                "resourceid"        : $('#txtIdProjectManager'+idx).val()  ,
                "resourcetype"      : C_PROJECT_MANAGER,
                "resourceuser"      : $('#cmbProjectManager'+idx).val()  ,      
                };
            
            arrays.push(data);
        }
    });
    
    return arrays;
}

function showTechnicalConsultant(obj) {
    if(obj.length > 0) {
        tech_consultant_idx = obj.length-1;
        $('#txtIdTechnicalConsultant0').val(obj[0].resourceid);
        $('#cmbTechnicalConsultant0').val(obj[0].resourceuser);
        
        if(obj.length > 1) {
            for(var i=1; i<obj.length; i++) {
                var tech_consultant = '<select id="cmbTechnicalConsultant'+ i +'" class="col-lg-12 form-control">'+$('#cmbTechnicalConsultant0').html()+'</select>';
                $('#tech-consultant-table').append(
                    '<tr id="trTechnicalConsultant-' + i + '" class="kt-datatable__row">'+
                        '<td class="kt-datatable__cell" style="display:none;">' + 
                            '<input  class="form-control" id="txtIdTechnicalConsultant'+i+'" value="'+obj[i].resourceid+'">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                        tech_consultant +
                        '</td>' +
                        '<td class="kt-datatable__cell">'+
                            '<button title="Delete" onclick="javascript: deleteTechnicalConsultant('+i+', this);" id="btnDeleteTechnicalConsultant" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
                        '</td>' +
                    '</tr>'
                );                
                $('#cmbTechnicalConsultant'+i).val(obj[i].resourceuser);
            }
        }
    }
}
function addTechnicalConsultant() {
    tech_consultant_idx++;
    var tech_consultant = '<select id="cmbTechnicalConsultant'+ tech_consultant_idx +'" class="col-lg-12 form-control">'+$('#cmbTechnicalConsultant0').html()+'</select>';
    $('#tech-consultant-table').append(
        '<tr id="trTechnicalConsultant-' + tech_consultant_idx + '" class="kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;">' + 
                '<input  class="form-control" id="txtIdTechnicalConsultant'+tech_consultant_idx+'" value="-1">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            tech_consultant +
            '</td>' +
            '<td class="kt-datatable__cell">'+
                '<button title="Delete" onclick="javascript: deleteTechnicalConsultant('+tech_consultant_idx+', this);" id="btnDeleteTechnicalConsultant" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>' +
        '</tr>'
    ); 
}
function deleteTechnicalConsultant(idx) {
    $('#trTechnicalConsultant-'+idx).remove() ;
}
function getTechnicalConsultant(){
    var arrays = [];
    var data = {};
    $('#tech-consultant-table tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);
        if($('#cmbTechnicalConsultant'+idx).val() != 0) {
            data = { 
                "resourceid"        : $('#txtIdTechnicalConsultant'+idx).val()  ,
                "resourcetype"      : C_TECHNICAL_CONSULTANT,
                "resourceuser"      : $('#cmbTechnicalConsultant'+idx).val()  ,      
                };
            
            arrays.push(data);
        }
    });
    
    return arrays;
}

function showApplicationConsultant(obj) {
    if(obj.length > 0) {
        app_consultant_idx = obj.length-1;
        $('#txtIdApplicationConsultant0').val(obj[0].resourceid);
        $('#cmbApplicationConsultant0').val(obj[0].resourceuser);
        
        if(obj.length > 1) {
            for(var i=1; i<obj.length; i++) {
                var app_consultant = '<select id="cmbApplicationConsultant'+ i +'" class="col-lg-12 form-control">'+$('#cmbApplicationConsultant0').html()+'</select>';
                $('#app-consultant-table').append(
                    '<tr id="trApplicationConsultant-' + i + '" class="kt-datatable__row">'+
                        '<td class="kt-datatable__cell" style="display:none;">' + 
                            '<input  class="form-control" id="txtIdApplicationConsultant'+ i +'" value="'+obj[i].resourceid+'">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                        app_consultant +
                        '</td>' +
                        '<td class="kt-datatable__cell">'+
                            '<button title="Delete" onclick="javascript: deleteApplicationConsultant('+i+', this);" id="btnDeleteApplicationConsultant" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
                        '</td>' +
                    '</tr>'
                );                 
                $('#cmbApplicationConsultant'+i).val(obj[i].resourceuser);
            }
        }
    }
}
function addApplicationConsultant() {
    app_consultant_idx++;
    var app_consultant = '<select id="cmbApplicationConsultant'+ app_consultant_idx +'" class="col-lg-12 form-control">'+$('#cmbApplicationConsultant0').html()+'</select>';
    $('#app-consultant-table').append(
        '<tr id="trApplicationConsultant-' + app_consultant_idx + '" class="kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;">' + 
                '<input  class="form-control" id="txtIdApplicationConsultant'+ app_consultant_idx +'" value="-1">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            app_consultant +
            '</td>' +
            '<td class="kt-datatable__cell">'+
                '<button title="Delete" onclick="javascript: deleteApplicationConsultant('+ app_consultant_idx +', this);" id="btnDeleteApplicationConsultant" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>' +
        '</tr>'
    ); 
}
function deleteApplicationConsultant(idx) {
    $('#trApplicationConsultant-'+idx).remove() ;
}
function getApplicationConsultant(){
    var arrays = [];
    var data = {};
    $('#app-consultant-table tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);
        if($('#cmbApplicationConsultant'+idx).val() != 0) {
            data = { 
                "resourceid"        : $('#txtIdApplicationConsultant'+idx).val()  ,
                "resourcetype"      : C_APPLICATION_CONSULTANT,
                "resourceuser"      : $('#cmbApplicationConsultant'+idx).val()  ,      
                };
            
            arrays.push(data);
        }
    });
    
    return arrays;
}

function showProjectDeveloper(obj) {
    if(obj.length > 0) {
        dev_idx = obj.length-1;
        $('#txtIdProjectDeveloper0').val(obj[0].resourceid);
        $('#cmbProjectDeveloper0').val(obj[0].resourceuser);
        
        if(obj.length > 1) {
            for(var i=1; i<obj.length; i++) {
                var dev = '<select id="cmbProjectDeveloper'+i+'" class="col-lg-12 form-control">'+$('#cmbProjectDeveloper0').html()+'</select>';
                $('#developer-table').append(
                    '<tr id="trProjectDeveloper-' + i + '" class="kt-datatable__row">'+
                        '<td class="kt-datatable__cell" style="display:none;">' + 
                            '<input  class="form-control" id="txtIdProjectDeveloper'+i+'" value="'+obj[i].resourceid+'">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                        dev +
                        '</td>' +
                        '<td class="kt-datatable__cell">'+
                            '<button title="Delete" onclick="javascript: deleteProjectDeveloper('+i+', this);" id="btnDeleteProjectDeveloper" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
                        '</td>' +
                    '</tr>'
                );
                $('#cmbProjectDeveloper'+i).val(obj[i].resourceuser);
            }
        }
    }
}
function addProjectDeveloper() {
    dev_idx++;
    var dev = '<select id="cmbProjectDeveloper'+dev_idx+'" class="col-lg-12 form-control">'+$('#cmbProjectDeveloper0').html()+'</select>';
    $('#developer-table').append(
        '<tr id="trProjectDeveloper-' + dev_idx + '" class="kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;">' + 
                '<input  class="form-control" id="txtIdProjectDeveloper'+dev_idx+'" value="-1">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            dev +
            '</td>' +
            '<td class="kt-datatable__cell">'+
                '<button title="Delete" onclick="javascript: deleteProjectDeveloper('+dev_idx+', this);" id="btnDeleteProjectDeveloper" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>' +
        '</tr>'
    ); 
}
function deleteProjectDeveloper(idx) {
    $('#trProjectDeveloper-'+idx).remove() ;
}
function getProjectDeveloper(){
    var arrays = [];
    var data = {};
    $('#developer-table tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);
        if($('#cmbProjectDeveloper'+idx).val() != 0) {
            data = { 
                "resourceid"        : $('#txtIdProjectDeveloper'+idx).val()  ,
                "resourcetype"      : C_DEVELOPER,
                "resourceuser"      : $('#cmbProjectDeveloper'+idx).val()  ,      
                };
            
            arrays.push(data);
        }
    });
    
    return arrays;
}

function showCustProjectSponsor(obj) {
    if(obj.length > 0) {
        cust_sponsor_idx = obj.length-1;
        $('#txtIdCustProjectSponsor0').val(obj[0].resourceid);
        $('#cmbCustProjectSponsor0').val(obj[0].resourceuser);
        
        if(obj.length > 1) {
            for(var i=1; i<obj.length; i++) {
                var cust_sponsor = '<select id="cmbCustProjectSponsor'+i+'" class="col-lg-12 form-control">'+$('#cmbCustProjectSponsor0').html()+'</select>';
                $('#cust-proj-sponsor-table').append(
                    '<tr id="trCustProjectSponsor-' + i + '" class="kt-datatable__row">'+
                        '<td class="kt-datatable__cell" style="display:none;">' + 
                            '<input  class="form-control" id="txtIdCustProjectSponsor'+i+'" value="'+obj[i].resourceid+'">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                        cust_sponsor +
                        '</td>' +
                        '<td class="kt-datatable__cell">'+
                            '<button title="Delete" onclick="javascript: deleteCustProjectSponsor('+i+', this);" id="btnDeleteCustProjectSponsor" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
                        '</td>' +
                    '</tr>'
                ); 
                $('#cmbCustProjectSponsor'+i).val(obj[i].resourceuser);
            }
        }
    }
}
function addCustProjectSponsor() {
    cust_sponsor_idx++;
    var cust_sponsor = '<select id="cmbCustProjectSponsor'+cust_sponsor_idx+'" class="col-lg-12 form-control">'+$('#cmbCustProjectSponsor0').html()+'</select>';
    $('#cust-proj-sponsor-table').append(
        '<tr id="trCustProjectSponsor-' + cust_sponsor_idx + '" class="kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;">' + 
                '<input  class="form-control" id="txtIdCustProjectSponsor'+cust_sponsor_idx+'" value="-1">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            cust_sponsor +
            '</td>' +
            '<td class="kt-datatable__cell">'+
                '<button title="Delete" onclick="javascript: deleteCustProjectSponsor('+cust_sponsor_idx+', this);" id="btnDeleteCustProjectSponsor" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>' +
        '</tr>'
    ); 
}
function deleteCustProjectSponsor(idx) {
    $('#trCustProjectSponsor-'+idx).remove() ;
}
function getCustProjectSponsor(){
    var arrays = [];
    var data = {};
    $('#cust-proj-sponsor-table tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);
        if($('#cmbCustProjectSponsor'+idx).val() != 0) {
            data = { 
                "resourceid"        : $('#txtIdCustProjectSponsor'+idx).val()  ,
                "resourcetype"      : C_CUST_PROJECT_SPONSOR,
                "resourceuser"      : $('#cmbCustProjectSponsor'+idx).val()  ,      
                };
            
            arrays.push(data);
        }
    });
    
    return arrays;
}

function showCustProjectAdvisor(obj) {
    if(obj.length > 0) {
        cust_advisor_idx = obj.length-1;
        $('#txtIdCustProjectAdvisor0').val(obj[0].resourceid);
        $('#cmbCustProjectAdvisor0').val(obj[0].resourceuser);
        
        if(obj.length > 1) {
            for(var i=1; i<obj.length; i++) {
                var cust_advisor = '<select id="cmbCustProjectAdvisor'+i+'" class="col-lg-12 form-control">'+$('#cmbCustProjectAdvisor0').html()+'</select>';
                $('#cust-proj-advisor-table').append(
                    '<tr id="trCustProjectAdvisor-' + i + '" class="kt-datatable__row">'+
                        '<td class="kt-datatable__cell" style="display:none;">' + 
                            '<input  class="form-control" id="txtIdCustProjectAdvisor'+i+'" value="'+obj[i].resourceid+'">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                        cust_advisor +
                        '</td>' +
                        '<td class="kt-datatable__cell">'+
                            '<button title="Delete" onclick="javascript: deleteCustProjectAdvisor('+i+', this);" id="btnDeleteCustProjectAdvisor" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
                        '</td>' +
                    '</tr>'
                );
                $('#cmbCustProjectAdvisor'+i).val(obj[i].resourceuser);
            }
        }
    } 
}
function addCustProjectAdvisor() {
    cust_advisor_idx++;
    var cust_advisor = '<select id="cmbCustProjectAdvisor'+cust_advisor_idx+'" class="col-lg-12 form-control">'+$('#cmbCustProjectAdvisor0').html()+'</select>';
    $('#cust-proj-advisor-table').append(
        '<tr id="trCustProjectAdvisor-' + cust_advisor_idx + '" class="kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;">' + 
                '<input  class="form-control" id="txtIdCustProjectAdvisor'+cust_advisor_idx+'" value="-1">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            cust_advisor +
            '</td>' +
            '<td class="kt-datatable__cell">'+
                '<button title="Delete" onclick="javascript: deleteCustProjectAdvisor('+cust_advisor_idx+', this);" id="btnDeleteCustProjectAdvisor" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>' +
        '</tr>'
    ); 
}
function deleteCustProjectAdvisor(idx) {
    $('#trCustProjectAdvisor-'+idx).remove() ;
}
function getCustProjectAdvisor(){
    var arrays = [];
    var data = {};
    $('#cust-proj-advisor-table tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);
        if($('#cmbCustProjectAdvisor'+idx).val() != 0) {
            data = { 
                "resourceid"        : $('#txtIdCustProjectAdvisor'+idx).val()  ,
                "resourcetype"      : C_CUST_PROJECT_ADVISOR,
                "resourceuser"      : $('#cmbCustProjectAdvisor'+idx).val()  ,      
                };
            
            arrays.push(data);
        }
    });
    
    return arrays;
}

function showCustProjectManager(obj) {
    if(obj.length > 0) {
        cust_mgr_idx = obj.length-1;
        $('#txtIdCustProjectManager0').val(obj[0].resourceid);
        $('#cmbCustProjectManager0').val(obj[0].resourceuser);
        
        if(obj.length > 1) {
            for(var i=1; i<obj.length; i++) {
                var cust_mgr = '<select id="cmbCustProjectManager'+i+'" class="col-lg-12 form-control">'+$('#cmbCustProjectManager0').html()+'</select>';
                $('#cust-proj-mgr-table').append(
                    '<tr id="trCustProjectManager-' + i + '" class="kt-datatable__row">'+
                        '<td class="kt-datatable__cell" style="display:none;">' + 
                            '<input  class="form-control" id="txtIdCustProjectManager'+i+'" value="'+obj[i].resourceid+'">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                        cust_mgr +
                        '</td>' +
                        '<td class="kt-datatable__cell">'+
                            '<button title="Delete" onclick="javascript: deleteCustProjectManager('+i+', this);" id="btnDeleteCustProjectManager" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
                        '</td>' +
                    '</tr>'
                );
                $('#cmbCustProjectManager'+i).val(obj[i].resourceuser);
            }
        }
    }  
}
function addCustProjectManager() {
    cust_mgr_idx++;
    var cust_mgr = '<select id="cmbCustProjectManager'+cust_mgr_idx+'" class="col-lg-12 form-control">'+$('#cmbCustProjectManager0').html()+'</select>';
    $('#cust-proj-mgr-table').append(
        '<tr id="trCustProjectManager-' + cust_mgr_idx + '" class="kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;">' + 
                '<input  class="form-control" id="txtIdCustProjectManager'+cust_mgr_idx+'" value="-1">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            cust_mgr +
            '</td>' +
            '<td class="kt-datatable__cell">'+
                '<button title="Delete" onclick="javascript: deleteCustProjectManager('+cust_mgr_idx+', this);" id="btnDeleteCustProjectManager" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>' +
        '</tr>'
    ); 
}
function deleteCustProjectManager(idx) {
    $('#trCustProjectManager-'+idx).remove() ;
}
function getCustProjectManager(){
    var arrays = [];
    var data = {};
    $('#cust-proj-mgr-table tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);
        if($('#cmbCustProjectManager'+idx).val() != 0) {
            data = { 
                "resourceid"        : $('#txtIdCustProjectManager'+idx).val()  ,
                "resourcetype"      : C_CUST_PROJECT_MANAGER,
                "resourceuser"      : $('#cmbCustProjectManager'+idx).val()  ,      
                };
            
            arrays.push(data);
        }
    });
    
    return arrays;
}

function showCustProjectMember(obj) {
    if(obj.length > 0) {
        cust_member_idx = obj.length-1;
        $('#txtIdCustProjectMember0').val(obj[0].resourceid);
        $('#cmbCustProjectMember0').val(obj[0].resourceuser);
        
        if(obj.length > 1) {
            for(var i=1; i<obj.length; i++) {
                var cust_member = '<select id="cmbCustProjectMember'+i+'" class="col-lg-12 form-control">'+$('#cmbCustProjectMember0').html()+'</select>';
                $('#cust-proj-member-table').append(
                    '<tr id="trCustProjectMember-' + i + '" class="kt-datatable__row">'+
                        '<td class="kt-datatable__cell" style="display:none;">' + 
                            '<input  class="form-control" id="txtIdCustProjectMember'+i+'" value="'+obj[i].resourceid+'">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell">' + 
                        '</td>' +
                        '<td class="kt-datatable__cell" style="vertical-align:top;">'+
                        cust_member+
                        '</td>' +
                        '<td class="kt-datatable__cell">'+
                            '<button title="Delete" onclick="javascript: deleteCustProjectMember('+i+', this);" id="btnDeleteCustProjectMember" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
                        '</td>' +
                    '</tr>'
                ); 
                $('#cmbCustProjectMember'+i).val(obj[i].resourceuser);
            }
        }
    }
}
function addCustProjectMember() {
    cust_member_idx++;
    var cust_member = '<select id="cmbCustProjectMember'+cust_member_idx+'" class="col-lg-12 form-control">'+$('#cmbCustProjectMember0').html()+'</select>';
    $('#cust-proj-member-table').append(
        '<tr id="trCustProjectMember-' + cust_member_idx + '" class="kt-datatable__row">'+
            '<td class="kt-datatable__cell" style="display:none;">' + 
                '<input  class="form-control" id="txtIdCustProjectMember'+cust_member_idx+'" value="-1">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell">' + 
            '</td>' +
            '<td class="kt-datatable__cell" style="vertical-align:top;">'+
            cust_member+
            '</td>' +
            '<td class="kt-datatable__cell">'+
                '<button title="Delete" onclick="javascript: deleteCustProjectMember('+cust_member_idx+', this);" id="btnDeleteCustProjectMember" class="btn btn-danger btn-sm btn-icon btn-icon-md"><i class="fa fa-trash"></button>'+
            '</td>' +
        '</tr>'
    ); 
}
function deleteCustProjectMember(idx) {
    $('#trCustProjectMember-'+idx).remove() ;
}
function getCustProjectMember(){
    var arrays = [];
    var data = {};
    $('#cust-proj-member-table tr').each(function() {
        var idx = $(this).attr('id');
        idx = idx.substring(idx.lastIndexOf('-')+1);
        if($('#cmbCustProjectMember'+idx).val() != 0) {
            data = { 
                "resourceid"        : $('#txtIdCustProjectMember'+idx).val()  ,
                "resourcetype"      : C_CUST_PROJECT_MEMBER,
                "resourceuser"      : $('#cmbCustProjectMember'+idx).val()  ,      
                };
            
            arrays.push(data);
        }
    });
    
    return arrays;
}
