// [VAT INVOICE NO - NEW] =====================================================
// Fetches a PREVIEW invoice number whenever the user clicks a VAT radio button
// on a new (unsaved) invoice. Calls the read-only get_invno_preview endpoint —
// no counters are touched. Uses a dedicated XHR object so it never collides
// with the shared xmlHttp used by other operations.
//
// Guard: if item_count > 0 AND invno is already filled, this is a loaded
// existing invoice — do NOT overwrite the invoice number.
// =============================================================================
var _vatInvXhr = null;

function vat_change() {

    var invnoEl   = document.getElementById('invno');
    var itemCntEl = document.getElementById('item_count');
    var itemCount = itemCntEl ? (parseInt(itemCntEl.value, 10) || 0) : 0;

    // Don't overwrite a loaded existing invoice
    if (invnoEl && invnoEl.value !== '' && itemCount > 0) {
        return;
    }

    var vatmethod = 'non';
    if (document.getElementById('vatgroup_0').checked)      vatmethod = 'vat';
    else if (document.getElementById('vatgroup_1').checked) vatmethod = 'non';
    else if (document.getElementById('vatgroup_2').checked) vatmethod = 'svat';
    else if (document.getElementById('vatgroup_3').checked) vatmethod = 'evat';

    var type2El = document.getElementById('type2');
    var type2   = type2El ? type2El.value : 'NORMAL';

    _vatInvXhr = GetXmlHttpObject();
    if (_vatInvXhr == null) { return; }

    var url = 'sales_inv_data.php'
            + '?Command=get_invno_preview'
            + '&vatmethod=' + encodeURIComponent(vatmethod)
            + '&type2='     + encodeURIComponent(type2);

    _vatInvXhr.onreadystatechange = function() {
        if (_vatInvXhr.readyState == 4 || _vatInvXhr.readyState == 'complete') {
            try {
                var resp = JSON.parse(_vatInvXhr.responseText);
                if (resp.invno && resp.invno !== '') {
                    document.getElementById('invno').value = resp.invno;
                }
            } catch (e) {
                // JSON parse failed — server error or session expired; ignore
            }
        }
    };

    _vatInvXhr.open('GET', url, true);
    _vatInvXhr.send(null);
}
// [VAT INVOICE NO - NEW] =====================================================



function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}



function new_inv()
{


    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }

    var paymethod;

    document.getElementById('msg_box').innerHTML = "";
    document.getElementById('transp').innerHTML = "";

    document.getElementById('invno').disabled=true;
    document.getElementById('itemd').disabled=true;

    document.getElementById('salesord1').value = "";
    document.getElementById('inquiry').value = "";
    document.getElementById('overpay').value = "0";
    document.getElementById('pay_type').value = "CASH";
    document.getElementById('type2').value = "NORMAL";
    document.getElementById('txtreturn').value = "0";
    
    // --- FIX: Force default to Non-VAT BEFORE generating the new invoice number ---
    document.getElementById('vatgroup_1').checked = true;
    // ------------------------------------------------------------------------------
    
    //document.getElementById('salesord2').value="";
    document.getElementById('invno').value = "";

    document.getElementById('crebal').value = "";
    document.getElementById('remarks').value = "";
    document.getElementById('remarks').style.visibility = "visible";
    document.getElementById('remarkslbl').style.visibility = "visible";

    document.getElementById('tel').value = "";
    document.getElementById('vehino').value = "";
    document.getElementById('jobno').value = "";
    document.getElementById('millage').value = "";

    document.getElementById('firstname_hidden').value = "";
    document.getElementById('c_subcode').value = "";
    document.getElementById('firstname').value = "";


    document.getElementById('cus_address').value = "";
    document.getElementById('cus_address2').value = "";

    document.getElementById('vat1').value = "";
    document.getElementById('vat2').value = "";
    document.getElementById('creditlimit').value = "";
    document.getElementById('balance').value = "";
    document.getElementById('txtinvt').value = "";
    document.getElementById('labour').value = "";
    document.getElementById("cardfee").selectedIndex = "0";
    document.getElementById('cardfee').disabled = false;



    document.getElementById('dag_jobno').value = "";
    document.getElementById('dag_make').value = "";
    document.getElementById('dag_design').value = "";
    document.getElementById('dag_size').value = "";
    document.getElementById('dag_serialno').value = "";
    document.getElementById('dag_cost').value = "";
    document.getElementById('dag_selling').value = "";
    document.getElementById('dag_dis').value = "";
    document.getElementById('dag_subtotal').value = "";


    document.getElementById('over60').value = "0";
    document.getElementById("promotion").checked = false;


    // var objdepartment = document.getElementById("department");
    // objdepartment.options[0].selected = true;

    // var objbrand = document.getElementById("brand");
    // objbrand.options[0].selected = true;



    document.getElementById('discount_org1').value = "";
    document.getElementById('discount_org2').value = "";
    document.getElementById('discount_org3').value = "";
    document.getElementById('itemdetails').innerHTML = "";
    document.getElementById('dag_table').innerHTML = "";
    document.getElementById('purchase_table').innerHTML = "";
    document.getElementById('subtot').value = "";
    document.getElementById('totdiscount').value = "";
    document.getElementById('tax').value = "";
    document.getElementById('invtot').value = "";
    document.getElementById('subtotal').value = "";

    document.getElementById('itemd_hidden').value = "";
    document.getElementById('cat1').value = "";
    document.getElementById('itemd').value = "";
    document.getElementById('serialno').value = "";
    document.getElementById('rate').value = "";
    document.getElementById('qty').value = "";
    document.getElementById('discountper').value = "0";
    document.getElementById('discountper1').value = "0";
    document.getElementById('stklevel').value = "";
    document.getElementById('storgrid').innerHTML = "";


    document.getElementById('delitype').value = "NORMAL";

    document.getElementById("deli_chk").checked = false;
    document.getElementById("deli_label").style.visibility = 'hidden';
    document.getElementById("deli_name").style.visibility = 'hidden';
    document.getElementById("deli_address").style.visibility = 'hidden';
    document.getElementById("deli_upbut").style.visibility = 'hidden';
    document.getElementById('msg_boxdeliup').innerHTML = "";
    document.getElementById('deli_name').value = "";
    document.getElementById('deli_address').value = "";
    document.getElementById('credper').value = "30";
    document.getElementById("cred_up_but").style.visibility = 'hidden';

    document.getElementById('msg_box_customer').innerHTML = "";
    document.getElementById('customer_name').value = "";
    document.getElementById('customer_tel').value = "";
    document.getElementById('customer_address').value = "";
    document.getElementById('customer_vat_no').value = "";
    document.getElementById('customer_svat_no').value = "";


    document.getElementById('s_refno').value = "";
    document.getElementById('s_desc').value = "";
    document.getElementById('s_brand').value = "";
    document.getElementById('s_partno').value = "";
    document.getElementById('s_cat').value = "";
    document.getElementById('s_list').value = "";
    document.getElementById('s_cash').value = "";
    document.getElementById('s_credit').value = "";
    document.getElementById('s_qty').value = "";

    document.getElementById('c_refno').value = "";
    document.getElementById('c_name').value = "";
    document.getElementById('c_add').value = "";
    document.getElementById('c_tel').value = "";
    document.getElementById('c_subcus').value = "";

    document.getElementById("printdeli").style.visibility = 'hidden';
    document.getElementById('msg_boxcredup').innerHTML = "";

    var url = "sales_inv_data.php";
    url = url + "?Command=" + "new_inv";
    url = url + "&salesrep=" + document.getElementById('salesrep').value;
    url = url + "&brand=" + document.getElementById('brand').value;
    url = url + "&department=" + document.getElementById('department').value;

    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "vat";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "non";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "svat";
    } else if (document.getElementById('vatgroup_3').checked == true) {
        vatmethod = "evat";
    }

    url = url + "&vatmethod=" + vatmethod;

    if (document.getElementById('paymethod_0').checked == true) {
        paymethod = "CR";
    } else if (document.getElementById('paymethod_1').checked == true) {
        paymethod = "CA";
    }
    url = url + "&paymethod=" + paymethod;
    url = url + "&type2=" + document.getElementById('type2').value;

    xmlHttp.onreadystatechange = assign_invno;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}

function assign_invno() {
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("invno");
        document.getElementById('invno').value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sdate");
        document.getElementById('invdate').value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tmpno");
        document.getElementById('tmpno').value = XMLAddress1[0].childNodes[0].nodeValue;

       // --- UPDATED SECTION: Force default to Non VAT ---
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("vattype");
        document.getElementById('vatgroup_1').checked = true;
        // -------------------------------------------------
        
        // (The setTimeout hack causing the screen flash was safely removed. 
        // The backend `new_inv` command now strictly generates the correctly formatted invoice number natively).
        // [VAT INVOICE NO - NEW] =====================================================

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("paytype");
        if(XMLAddress1[0].childNodes[0].nodeValue=="CA"){
            document.getElementById('paymethod_1').checked = true;

            document.getElementById('firstname_hidden').value = "V/0001";
            document.getElementById('c_subcode').value = "V/0001";
            document.getElementById('firstname').value = "CASH SALE";

        }else{
            document.getElementById('paymethod_0').checked = true;

            document.getElementById('firstname_hidden').value = "";
            document.getElementById('c_subcode').value = "";
            document.getElementById('firstname').value = "";
        }


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("select_dep");
        document.getElementById('department').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("select_rep");
        document.getElementById('salesrep').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("select_lab");
        document.getElementById('labour').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        pay_type_change();

    }
}



function paytype()
{


    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }

    var url = "sales_inv_data.php";
    url = url + "?Command=" + "paytype";

    if (document.getElementById('paymethod_0').checked == true) {
        paymethod = "CR";
    } else if (document.getElementById('paymethod_1').checked == true) {
        paymethod = "CA";
    }
    url = url + "&paymethod=" + paymethod;
    
    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "vat";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "non";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "svat";
    } else if (document.getElementById('vatgroup_3').checked == true) {
        vatmethod = "evat";
    }

    url = url + "&vatmethod=" + vatmethod;

    url = url + "&department=" + document.getElementById('department').value;
    url = url + "&type2=" + document.getElementById('type2').value;


    xmlHttp.onreadystatechange = re_paytype;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}


function re_paytype()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("paytype");
        if(XMLAddress1[0].childNodes[0].nodeValue=="CA"){

            document.getElementById('firstname_hidden').value = "V/0001";
            document.getElementById('c_subcode').value = "V/0001";
            document.getElementById('firstname').value = "CASH SALE";

        }else{

            document.getElementById('firstname_hidden').value = "";
            document.getElementById('c_subcode').value = "";
            document.getElementById('firstname').value = "";
        }


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("invno");
        document.form1.invno.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("select_dep");
        document.getElementById('department').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("select_rep");
        document.getElementById('salesrep').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("select_lab");
        document.getElementById('labour').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

    }
}


function calc() {


    if (document.getElementById('vatgroup_0').checked == true) {
        if (document.getElementById('vat1').value == "") {
            document.getElementById('vatgroup_1').checked = false;
        }
    }
    if (document.getElementById('vatgroup_2').checked == true) {
        if (document.getElementById('vat2').value == "") {
            document.getElementById('vatgroup_1').checked = true;
        }
    }


    var disper = 0;
    var disper1 = 0;
    var disper2 = 0;
    var disper3 = 0;
    var subtot = 0;

    // if ((document.getElementById('discount_org1').value != '') && (document.getElementById('discount_org1').value != "0") && (document.getElementById('discount_org1').value != "0.00")) {
    //     disper = document.getElementById('discount_org1').value;
    //     disper1 = document.getElementById('discount_org1').value;
    // }

    // if ((document.getElementById('discount_org2').value != '') && (document.getElementById('discount_org2').value != "0") && (document.getElementById('discount_org2').value != "0.00")) {
    //     disper2 = (100 - document.getElementById('discount_org1').value) * (document.getElementById('discount_org2').value) / 100;
    //     disper = parseFloat(disper1) + parseFloat(disper2);
    // }

    // if ((document.getElementById('discount_org3').value != '') && (document.getElementById('discount_org3').value != "0") && (document.getElementById('discount_org3').value != "0.00")) {
    //     disper3 = (100 - (parseFloat(disper1) + parseFloat(disper2))) * (document.getElementById('discount_org3').value) / 100;
    //     disper = parseFloat(disper1) + parseFloat(disper2) + parseFloat(disper3);
    // }

    // document.getElementById('discountper').value = disper;


    if ((document.getElementById('discountper').value != '') && (document.getElementById('discountper').value != "0") && (document.getElementById('discountper').value != "0.00")) {

        disper = document.getElementById('discountper').value;
        disper1 = document.getElementById('discountper').value;

    }


    if ((document.getElementById('discountper1').value != '') && (document.getElementById('discountper1').value != "0") && (document.getElementById('discountper1').value != "0.00")) {


        disper2 = (100 - document.getElementById('discountper').value) * (document.getElementById('discountper1').value) / 100;
        disper = parseFloat(disper1) + parseFloat(disper2);

    }



    if(document.getElementById('cat1').value=="TO BE EDIT ITEM RATE MINUS"){
        subtot = parseFloat(document.getElementById('rate').value*-1) - (parseFloat(document.getElementById('rate').value*-1) * parseFloat(disper) / 100);
    }else{
        subtot = parseFloat(document.getElementById('rate').value) - (parseFloat(document.getElementById('rate').value) * parseFloat(disper) / 100);
    }

    subtot = parseFloat(subtot) * parseFloat(document.getElementById('qty').value);


    document.getElementById('subtotal').value = subtot;

    add_tmp('up');
}

function calcnew() {


    if (document.getElementById('vatgroup_0').checked == true) {
        if (document.getElementById('vat1').value == "") {
            document.getElementById('vatgroup_1').checked = true;
        }
    }
    if (document.getElementById('vatgroup_2').checked == true) {
        if (document.getElementById('vat2').value == "") {
            document.getElementById('vatgroup_1').checked = true;
        }
    }


    var disper = 0;
    var disper1 = 0;
    var disper2 = 0;
    var subtot = 0;

    if ((document.getElementById('discountper').value != '') && (document.getElementById('discountper').value != "0") && (document.getElementById('discountper').value != "0.00")) {

        disper = document.getElementById('discountper').value;
        disper1 = document.getElementById('discountper').value;

    }


    if ((document.getElementById('discountper1').value != '') && (document.getElementById('discountper1').value != "0") && (document.getElementById('discountper1').value != "0.00")) {


        disper2 = (100 - document.getElementById('discountper').value) * (document.getElementById('discountper1').value) / 100;
        disper = parseFloat(disper1) + parseFloat(disper2);

    }


    // disper =document.getElementById('discountper').value ;

    if(document.getElementById('cat1').value=="TO BE EDIT ITEM RATE MINUS"){
        subtot = parseFloat(document.getElementById('rate').value*-1) - (parseFloat(document.getElementById('rate').value*-1) * parseFloat(disper) / 100);
    }else{
        subtot = parseFloat(document.getElementById('rate').value) - (parseFloat(document.getElementById('rate').value) * parseFloat(disper) / 100);
    }

    subtot = parseFloat(subtot) * parseFloat(document.getElementById('qty').value);


    document.getElementById('subtotal').value = subtot;

    add_tmp('up');
}


function itno_ind()
{

    if (document.getElementById('itemd_hidden').value != "") {


        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null)
        {
            alert("Browser does not support HTTP Request");
            return;
        }


        var url = "sales_inv_data.php";
        url = url + "?Command=" + "pass_itno";
        url = url + "&itno=" + document.getElementById('itemd_hidden').value;
        url = url + "&brand=" + document.getElementById('brand').value;
        url = url + "&department=" + document.getElementById('department').value;

        xmlHttp.onreadystatechange = passitresult_ind;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);

    }

}

function passitresult_ind()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        //alert(xmlHttp.responseText);
        var str = xmlHttp.responseText;
        if (str.length > 70) {
            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_code");
            document.getElementById('itemd_hidden').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_description");
            document.getElementById('itemd').value = XMLAddress1[0].childNodes[0].nodeValue;



            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("qtyinhand");
            document.getElementById('stklevel').value = XMLAddress1[0].childNodes[0].nodeValue;


            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("actual_selling");
            document.getElementById('actual_selling').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_selpri");
            document.getElementById('rate').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("costcenter");
            if (XMLAddress1[0].childNodes[0].nodeValue == "1") {
                document.getElementById('rate').disabled = false;
            } else {
                document.getElementById('rate').disabled = "disabled";
            }

            document.getElementById('qty').focus();


        } else {


            document.getElementById('searchitem').focus();
        }


    }
}


function load_ord_lst()
{

    NewWindow('search_ord.php?stname=inv_mast', 'mywin', '800', '700', 'yes', 'center');

}

function update_list(stname)
{


    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }


    var url = "search_ord_data.php";
    url = url + "?Command=" + "search_inv";

    if (document.getElementById('invno').value != "") {
        url = url + "&mstatus=invno";
    } else if (document.getElementById('customername').value != "") {
        url = url + "&mstatus=customername";
    } else if (document.getElementById('invdate').value != "") {
        url = url + "&mstatus=rep";
    } else {
        url = url + "&mstatus=";
    }

    url = url + "&invno=" + document.getElementById('invno').value;
    url = url + "&customername=" + document.getElementById('customername').value;
    url = url + "&rep=" + document.getElementById('invdate').value;

    url = url + "&Option1=" + document.getElementById('Option1').checked;
    url = url + "&Option2=" + document.getElementById('Option2').checked;
    url = url + "&Option3=" + document.getElementById('Option3').checked;
    url = url + "&Option4=" + document.getElementById('Option4').checked;
    url = url + "&Option5=" + document.getElementById('Option5').checked;
    url = url + "&stname=" + stname;
    //alert(url);

    xmlHttp.onreadystatechange = showinvresult;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}

function showinvresult()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        //alert(xmlHttp.responseText);
        //setTimeout("location.reload(true);",500);
        //if (xmlHttp.responseText=="exist"){
        //	alert("Already Exists");
        //}

        //XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("inv_table");
        //document.getElementById('filt_table').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        document.getElementById('filt_table').innerHTML = xmlHttp.responseText;
    }
}


function cancel_inv() {


    var msg = confirm("Do you want to CANCEL this invoice ! ");
    if (msg == true) {


        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null)
        {
            alert("Browser does not support HTTP Request");
            return;
        }

        if (document.getElementById('remarks').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Your Reason</span></div>";
            return false;
        }

        var url = "sales_inv_data.php";
        url = url + "?Command=" + "cancel_inv";
        url = url + "&invno=" + document.getElementById('invno').value;
        url = url + "&invtot=" + document.getElementById('invtot').value;
        url = url + "&customer_code=" + document.getElementById('firstname_hidden').value;
        url = url + "&salesrep=" + document.getElementById('salesrep').value;
        url = url + "&department=" + document.getElementById('department').value;
        url = url + "&tmpno=" + document.getElementById('tmpno').value;
        url = url + "&remarks=" + document.getElementById('remarks').value;


        xmlHttp.onreadystatechange = cancel_inv_result;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    }


}

function cancel_inv_result()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        if (xmlHttp.responseText == "Canceled") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-success' role='alert'><span class='center-block'>Cancelled</span></div>";
//            location.reload(true);
            setTimeout("location.reload(true);", 500);
        } else {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>" + xmlHttp.responseText + "</span></div>";
        }
    }
}

function calc1_table(i)
{


    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }

    id = "id" + i;
    code = "code" + i;
    itemd = "itemd" + i;
    serialno = "serialno" + i;
    rate = "rate" + i;
    actual_selling = "actual_selling" + i;
    qty = "qty" + i;
    discount = "discount" + i;
    discount1per = "discount1per" + i;
    discount2per = "discount2per" + i;
    subtotalnew = "subtotal" + i;



    var str = document.getElementById(rate).value;
    var n = str.replace(/,/gi, "");
    var vatrate = 0.18; //vatrate
    var SELLING = 0;

    var actual_rate = n;

    if (document.getElementById('vatgroup_1').checked == true) {
        SELLING = n;
    } else {
        SELLING = n;
        // SELLING = n / (vatrate + 1);
    }
    //alert(SELLING);
    document.getElementById(rate).value = SELLING;

    var subtotal = parseFloat(SELLING) * parseFloat(document.getElementById(qty).value);




    var dis = 0;
    var disper1 = 0;
    var disper2 = 0;
    var dis3 = 0;
    var disper = 0;
    var dis1f = 0;
    var dis2f = 0;
    var dis3f = 0;


    if ((document.getElementById(discount1per).value != '') && (document.getElementById(discount1per).value != "0") && (document.getElementById(discount1per).value != "0.00")) {

        disper = document.getElementById(discount1per).value;
        disper1 = document.getElementById(discount1per).value;

    }


    if ((document.getElementById(discount2per).value != '') && (document.getElementById(discount2per).value != "0") && (document.getElementById(discount2per).value != "0.00")) {


        disper2 = (100 - document.getElementById(discount1per).value) * (document.getElementById(discount2per).value) / 100;
        disper = parseFloat(disper1) + parseFloat(disper2);

    }


    subtotal = parseFloat(SELLING) - (parseFloat(SELLING) * parseFloat(disper) / 100);
    subtotal = parseFloat(subtotal) * parseFloat(document.getElementById(qty).value);


    document.getElementById(subtotalnew).innerHTML = subtotal.toFixed(2);





    // //////////////////////////////////////////////////////////////////////////////
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }



    var url = "sales_inv_data.php";
    url = url + "?Command=" + "add_tmp";
    url = url + "&Command1=" + "editrate";

    url = url + "&invno=" + document.getElementById('invno').value;
    url = url + "&tmpno=" + document.getElementById('tmpno').value;
    url = url + "&department=" + document.getElementById('department').value;
    url = url + "&cardfee=" + document.getElementById('cardfee').value;
    url = url + "&id=" + document.getElementById(id).innerHTML;

    url = url + "&itemcode=" + document.getElementById(code).innerHTML;

    url = url + "&item=" + document.getElementById(itemd).innerHTML;
    url = url + "&serialno=" + document.getElementById(serialno).innerHTML;
    url = url + "&rate=" + SELLING;
    url = url + "&actual_selling=" + actual_rate;

    url = url + "&qty=" + document.getElementById(qty).value;
    url = url + "&discount=" + document.getElementById(discount).innerHTML;
    url = url + "&discount1per=" + document.getElementById(discount1per).value;
    url = url + "&discount2per=" + document.getElementById(discount2per).value;
    url = url + "&subtotal=" + document.getElementById(subtotalnew).innerHTML;




    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "vat";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "non";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "svat";
    } else if (document.getElementById('vatgroup_3').checked == true) {
        vatmethod = "evat";
    }
    url = url + "&vatmethod=" + vatmethod;

    xmlHttp.onreadystatechange = showarmyresultdel;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}


function add_tmp(cdata)
{


    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    document.getElementById('msg_box').innerHTML = "";

    if (cdata == "add") {

        // if (parseFloat(document.getElementById('qty').value) > parseFloat(document.getElementById('stklevel').value)) {
        //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Insufficient Quantity in this Department</span></div>";
        //     return false;
        // }

        if (document.getElementById('firstname_hidden').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Customer</span></div>";
            return false;
        }

        if (document.getElementById('itemd_hidden').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Item</span></div>";
            return false;
        }

        if (document.getElementById('itemd').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Item Description</span></div>";
            return false;
        }
        if (document.getElementById('rate').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Price</span></div>";
            return false;
        }

        if (document.getElementById('qty').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Item Qty</span></div>";
            return false;
        }


        if ((document.getElementById('discountper').value == '') && (document.getElementById('discountper').value == "0") && (document.getElementById('discountper').value == "0.00")) {

            if ((document.getElementById('discountper1').value != '') && (document.getElementById('discountper1').value != "0") && (document.getElementById('discountper1').value != "0.00")) {
                document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Discount 1</span></div>";
                return false;
            }

        }


        if (document.getElementById('subtotal').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Calculation Wrong.</span></div>";
            return false;
        }
    }

    if (cdata == "dag") {

        if (document.getElementById('firstname_hidden').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Customer Not Selected</span></div>";
            return false;
        }
        if (document.getElementById('dag_jobno').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Job Not Selected</span></div>";
            return false;
        }
        if (document.getElementById('dag_cost').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Cost Not Entered</span></div>";
            return false;
        }
        if (document.getElementById('dag_selling').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Selling Not Entered</span></div>";
            return false;
        }
        if (document.getElementById('dag_subtotal').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Calculation Wrong.</span></div>";
            return false;
        }
    }






    var url = "sales_inv_data.php";
    url = url + "?Command=" + "add_tmp";
    url = url + "&Command1=" + cdata;

    url = url + "&invno=" + document.getElementById('invno').value;
    url = url + "&tmpno=" + document.getElementById('tmpno').value;
    url = url + "&itemcode=" + document.getElementById('itemd_hidden').value;
    url = url + "&item=" + document.getElementById('itemd').value;
    url = url + "&serialno=" + document.getElementById('serialno').value;
    url = url + "&rate=" + document.getElementById('rate').value;
    url = url + "&actual_selling=" + document.getElementById('actual_selling').value;
    url = url + "&qty=" + document.getElementById('qty').value;

    url = url + "&cardfee=" + document.getElementById('cardfee').value;


    url = url + "&dag_jobno=" + document.getElementById('dag_jobno').value;
    url = url + "&dag_make=" + document.getElementById('dag_make').value;
    url = url + "&dag_design=" + document.getElementById('dag_design').value;
    url = url + "&dag_size=" + document.getElementById('dag_size').value;
    url = url + "&dag_serialno=" + document.getElementById('dag_serialno').value;
    url = url + "&dag_cost=" + document.getElementById('dag_cost').value;
    url = url + "&dag_selling=" + document.getElementById('dag_selling').value;
    url = url + "&dag_dis=" + document.getElementById('dag_dis').value;
    url = url + "&dag_subtotal=" + document.getElementById('dag_subtotal').value;

    url = url + "&discountper=" + document.getElementById('discountper').value;
    url = url + "&discountper1=" + document.getElementById('discountper1').value;
    url = url + "&subtotal=" + document.getElementById('subtotal').value;
    url = url + "&department=" + document.getElementById('department').value;
    url = url + "&brand=" + document.getElementById('brand').value;


    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "vat";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "non";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "svat";
    } else if (document.getElementById('vatgroup_3').checked == true) {
        vatmethod = "evat";
    }
    url = url + "&vatmethod=" + vatmethod;

    if (document.getElementById('paymethod_0').checked == true) {
        paymethod = "CR";
    } else if (document.getElementById('paymethod_1').checked == true) {
        paymethod = "CA";
    }
    url = url + "&paymethod=" + paymethod;

    xmlHttp.onreadystatechange = showarmyresultdel;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}




function showarmyresultdel()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {



        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table");
        document.getElementById('itemdetails').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_dagtable");
        document.getElementById('dag_table').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("item_count");
        document.getElementById('item_count').value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("subtot");
        document.getElementById('subtot').value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("totdiscount");
        document.getElementById('totdiscount').value = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tax");
        document.getElementById('tax').value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("invtot");
        document.getElementById('invtot').value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("invno");
        document.getElementById('invno').value = XMLAddress1[0].childNodes[0].nodeValue;



        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("Command1");
        if (XMLAddress1[0].childNodes[0].nodeValue == "add") {
            document.getElementById('itemd_hidden').value = "";
            document.getElementById('cat1').value = "";
            document.getElementById('itemd').value = "";
            document.getElementById('serialno').value = "";
            document.getElementById('rate').value = "";
            document.getElementById('qty').value = "";

            document.getElementById('itemd').disabled = true;

            document.getElementById('discountper').value = "0";
            document.getElementById('discountper1').value = "0";
            document.getElementById('subtotal').value = "";

            document.getElementById('itemd_hidden').focus();
        }

        if (XMLAddress1[0].childNodes[0].nodeValue == "dag") {

            document.getElementById('dag_jobno').value = "";
            document.getElementById('dag_make').value = "";
            document.getElementById('dag_design').value = "";
            document.getElementById('dag_size').value = "";
            document.getElementById('dag_serialno').value = "";
            document.getElementById('dag_cost').value = "";
            document.getElementById('dag_selling').value = "";
            document.getElementById('dag_dis').value = "";
            document.getElementById('dag_subtotal').value = "";

            document.getElementById('dag_jobno').focus();
        }
    }
}

function del_item(cdata) {


    // var disper = 0;
    // var disper1 = 0;
    // var disper2 = 0;
    // var disper3 = 0;
    // var subtot = 0;

    // if ((document.getElementById('discount_org1').value != '') && (document.getElementById('discount_org1').value != "0") && (document.getElementById('discount_org1').value != "0.00")) {

    //     disper = document.getElementById('discount_org1').value;
    //     disper1 = document.getElementById('discount_org1').value;

    // }


    // if ((document.getElementById('discount_org2').value != '') && (document.getElementById('discount_org2').value != "0") && (document.getElementById('discount_org2').value != "0.00")) {


    //     disper2 = (100 - document.getElementById('discount_org1').value) * (document.getElementById('discount_org2').value) / 100;
    //     disper = parseFloat(disper1) + parseFloat(disper2);

    // }


    // if ((document.getElementById('discount_org3').value != '') && (document.getElementById('discount_org3').value != "0") && (document.getElementById('discount_org3').value != "0.00")) {


    //     disper3 = (100 - (parseFloat(disper1) + parseFloat(disper2))) * (document.getElementById('discount_org3').value) / 100;

    //     disper = parseFloat(disper1) + parseFloat(disper2) + parseFloat(disper3);

    // }

    // document.getElementById('discountper').value = disper;


    var url = "sales_inv_data.php";
    url = url + "?Command=add_tmp";
    url = url + "&Command1=del";
    url = url + "&id=" + cdata;
    url = url + "&tmpno=" + document.getElementById('tmpno').value;
    url = url + "&department=" + document.getElementById('department').value;
    url = url + "&brand=" + document.getElementById('brand').value;
    url = url + "&discountper=" + document.getElementById('discountper').value;
    url = url + "&discountper1=" + document.getElementById('discountper1').value;

    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "vat";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "non";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "svat";
    } else if (document.getElementById('vatgroup_3').checked == true) {
        vatmethod = "evat";
    }
    url = url + "&vatmethod=" + vatmethod;

    xmlHttp.onreadystatechange = showarmyresultdel;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}

function brandsession()
{


    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }


    var url = "sales_inv_data.php";
    url = url + "?Command=" + "brandsession";
    url = url + "&brand=" + document.getElementById('brand').value;

    // xmlHttp.onreadystatechange = assign_invno;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}


function invno(invno, stname)
{


    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }





    if (stname == "inv_mast") {

        if (opener.document.form1.invno.value != "") {
            opener.document.form1.txtreturn.value = "0";

            var url = "search_ord_data.php";
            url = url + "?Command=" + "pass_invno_to_inv";
            url = url + "&invno=" + invno;
            url = url + "&stname=" + stname;
            url = url + "&newinvno=" + opener.document.form1.invno.value
            url = url + "&tmpno=" + opener.document.form1.tmpno.value
            url = url + "&invdate=" + opener.document.form1.invdate.value
            url = url + "&salesrep=" + opener.document.form1.salesrep.value

            url = url + "&custno=" + opener.document.form1.firstname_hidden.value;
            url = url + "&brand=" + opener.document.form1.brand.value;
            url = url + "&department=" + opener.document.form1.department.value;

            xmlHttp.onreadystatechange = passinvresult;
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
            //alert(url);
        } else {
            alert("Please insert invoice no");
            self.close();
        }

    } else {
        var url = "search_ord_data.php";
        url = url + "?Command=" + "pass_invno";
        url = url + "&invno=" + invno;
        url = url + "&stname=" + stname;

        url = url + "&custno=" + opener.document.form1.firstname_hidden.value;
        url = url + "&brand=" + opener.document.form1.brand.value;
        url = url + "&department=" + opener.document.form1.department.value;

        xmlHttp.onreadystatechange = passinvresult_ord;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    }





    //alert(url);




}


function passinvresult()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        //alert(xmlHttp.responseText);

        if (xmlHttp.responseText == "Please click New again!!!") {
            alert(xmlHttp.responseText);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("item_count");
        opener.document.form1.item_count.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tmpno");
        opener.document.form1.tmpno.value = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sdate");
        opener.document.form1.invdate.value = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_invoiceno");
        opener.document.form1.invno.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("Result");
        opener.document.form1.Result.value = XMLAddress1[0].childNodes[0].nodeValue;




        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("trans");
        if (XMLAddress1[0].childNodes[0].nodeValue == "true") {
            window.opener.document.getElementById('transp').innerHTML = '<div style="background-color:#FF0000"><font size="+3">Transport</font></div>';
        } else {
            window.opener.document.getElementById('transp').innerHTML = "";
        }


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("TEL");
        opener.document.getElementById("tel").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("vehino");
        opener.document.getElementById("vehino").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("jobno");
        opener.document.getElementById("jobno").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("millage");
        opener.document.getElementById("millage").value = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("remarks");
        opener.document.getElementById("remarks").value = XMLAddress1[0].childNodes[0].nodeValue;



        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_order_no");

        opener.document.form1.salesord1.value = XMLAddress1[0].childNodes[0].nodeValue;
        opener.document.form1.ordno.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_customecode");
        opener.document.form1.firstname_hidden.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_customername");
        opener.document.form1.firstname.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_address");
        opener.document.form1.cus_address.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_address2");
        opener.document.form1.cus_address2.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("c_subcode");
        opener.document.form1.c_subcode.value = XMLAddress1[0].childNodes[0].nodeValue;


        opener.document.form1.vatgroup_1.checked = true;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_vat");
        if (XMLAddress1[0].childNodes[0].nodeValue == "0") {
            opener.document.form1.vatgroup_1.checked = true;
        } else if (XMLAddress1[0].childNodes[0].nodeValue == "1") {
            opener.document.form1.vatgroup_0.checked = true;
        } else if (XMLAddress1[0].childNodes[0].nodeValue == "2") {
            opener.document.form1.vatgroup_2.checked = true;
        } else if (XMLAddress1[0].childNodes[0].nodeValue == "3") {
            opener.document.form1.vatgroup_3.checked = true;
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_vatno1");
        opener.document.form1.vat1.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_vatno2");
        opener.document.form1.vat2.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_salesrep");
        opener.document.getElementById("salesrep").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("creditbalance");
        opener.document.form1.balance.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("crebal");
        opener.document.form1.crebal.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("netqty");
        opener.document.form1.txtinvt.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_department");
        opener.document.getElementById("department").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_brand");
        opener.document.getElementById("brand").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("dis");
        opener.document.form1.discount_org1.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("dis1");
        opener.document.form1.discount_org2.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("dis2");
        opener.document.form1.discount_org3.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_subtotal");
        opener.document.form1.subtot.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_discount");
        opener.document.form1.totdiscount.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_tax");
        opener.document.form1.tax.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_invoiceval");
        opener.document.form1.invtot.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table");
        window.opener.document.getElementById('itemdetails').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("item_count");
        opener.document.form1.item_count.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("txt_crelimi");
        opener.document.form1.creditlimit.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("dis");
        opener.document.form1.discount_org1.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("over60_txt");
        opener.document.form1.over60.value = XMLAddress1[0].childNodes[0].nodeValue;



        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("over60_qst");
        if (XMLAddress1[0].childNodes[0].nodeValue != "") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
        }
        if ((XMLAddress1[0].childNodes[0].nodeValue == "You have approved for proceed") || (XMLAddress1[0].childNodes[0].nodeValue == "")) {
            opener.document.form1.over60.value = "0";
        }


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table_acc");
        window.opener.document.getElementById('storgrid').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

//edited 18.05.10 praweenaka
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("type1");
        var cre1 = (XMLAddress1[0].childNodes[0].nodeValue).toString();

        if (cre1 === "TBR") {
            opener.document.form1.credper.value = 60;
        } else {
            opener.document.form1.credper.value = 60;
        }
//
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("AltMessage");
        if (XMLAddress1[0].childNodes[0].nodeValue != "") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("bank_message");
        if (XMLAddress1[0].childNodes[0].nodeValue != "") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("over60_message");
        if (XMLAddress1[0].childNodes[0].nodeValue != "") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("chq_message");
        if (XMLAddress1[0].childNodes[0].nodeValue != "") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("chq_message_que");
        if (XMLAddress1[0].childNodes[0].nodeValue != "") {
            var ans = confirm(XMLAddress1[0].childNodes[0].nodeValue);
            if (ans == true) {
                opener.document.form1.txtreturn.value = "0";
            } else {
                opener.document.form1.txtreturn.value = "1";
            }
        }


        self.close();

    }
}


function save_inv() {


    document.getElementById('msg_box').innerHTML = "";

    // if (document.getElementById('salesord1').value == "") {
    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Cannot Save without Order No</span></div>";
    //     return false;
    // }
    if (document.getElementById('invno').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Click New</span></div>";
        return false;
    }
    if (document.getElementById('firstname_hidden').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Customer</span></div>";
        return false;
    }
    if (document.getElementById('firstname').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Customer Name</span></div>";
        return false;
    }
    if (document.getElementById('department').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Department</span></div>";
        return false;
    }
    if (document.getElementById('salesrep').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Sales Ex</span></div>";
        return false;
    }

    // if ((document.getElementById('txtreturn').value != "0") && (document.getElementById('Result').value != "OK")) {
    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Return Cheque Available !!!</span></div>";
    //     return false;
    // }

    // if ((document.getElementById('over60').value == "60") && (document.getElementById('Result').value != "OK")) {
    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Over 60 outstanding available !!!</span></div>";
    //     return false;
    // }

    if (parseFloat(document.getElementById('item_count').value) < 1) {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please insert items</span></div>";
        return false;
    }

    // if (document.getElementById('tel').value == "") {
    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Tel</span></div>";
    //     return false;
    // }

    // if (document.getElementById('vehino').value == "") {
    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Vehicle No</span></div>";
    //     return false;
    // }
    // if (document.getElementById('jobno').value == "") {
    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Job No</span></div>";
    //     return false;
    // }
    // if (document.getElementById('millage').value == "") {
    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Millage</span></div>";
    //     return false;
    // }
    if (document.getElementById('labour').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Labour</span></div>";
        return false;
    }
    if (document.getElementById('type2').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Company Type</span></div>";
        return false;
    }

//   if ((document.getElementById('pay_type').value != "NORMAL") &&  (document.getElementById('pay_type').value != "CREDIT")) {

//          if (document.getElementById('pay_type').value == "CASH") {

//               if (document.getElementById('cash_amount').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Cash Amount</span></div>";
//                  return false;
//              }

//              if (document.getElementById('cash_amount').value > document.getElementById('invtot').value) {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Amount is Grater Than Total</span></div>";
//                  return false;
//              }

//          }

//          if (document.getElementById('pay_type').value == "CARD") {

//              if (document.getElementById('card_type').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Card Type</span></div>";
//                  return false;
//              }

//              if (document.getElementById('card_number').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Card No</span></div>";
//                  return false;
//              }
//              if (document.getElementById('card_date').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Card Date</span></div>";
//                  return false;
//              }
//               if (document.getElementById('card_amount').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Cash Amount</span></div>";
//                  return false;
//              }

//              if (document.getElementById('card_amount').value > document.getElementById('invtot').value) {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Amount is Grater Than Total</span></div>";
//                  return false;
//              }
//          }

//          if (document.getElementById('pay_type').value == "CHEQUE") {

//              if (document.getElementById('cheque_no').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Cheque No</span></div>";
//                  return false;
//              }

//              if (document.getElementById('cheque_bank_name').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Bank</span></div>";
//                  return false;
//              }

//              if (document.getElementById('cheque_date').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Cheque Date</span></div>";
//                  return false;
//              }
//               if (document.getElementById('cheque_amount').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Cash Amount</span></div>";
//                  return false;
//              }

//              if (document.getElementById('cheque_amount').value > document.getElementById('invtot').value) {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Amount is Grater Than Total</span></div>";
//                  return false;
//              }


//          }

//          if (document.getElementById('pay_type').value == "GIFT CARD") {

//               if (document.getElementById('gift_card_no').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Gift Card NO</span></div>";
//                  return false;
//              }
//               if (document.getElementById('gift_card_amount').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Cash Amount</span></div>";
//                  return false;
//              }

//              if (document.getElementById('gift_card_amount').value > document.getElementById('invtot').value) {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Amount is Grater Than Total</span></div>";
//                  return false;
//              }

//          }

//          if (document.getElementById('pay_type').value == "BANK DEPOSIT") {

//              if (document.getElementById('bank_deposit_bank_name').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Bank</span></div>";
//                  return false;
//              }

//              if (document.getElementById('bank_deposit_note').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Bank Deposit Note</span></div>";
//                  return false;
//              }

//              if (document.getElementById('bank_deposit_date').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select TT Date</span></div>";
//                  return false;
//              }
//               if (document.getElementById('bank_deposit_amount').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Cash Amount</span></div>";
//                  return false;
//              }

//              if (document.getElementById('bank_deposit_amount').value > document.getElementById('invtot').value) {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Amount is Grater Than Total</span></div>";
//                  return false;
//              }

//          }

//          if (document.getElementById('pay_type').value == "ONLINE PAYMENT") {

//              if (document.getElementById('online_pay_bank_name').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Online Payment Bank</span></div>";
//                  return false;
//              }
//              if (document.getElementById('online_pay_note').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Online Payment Note</span></div>";
//                  return false;
//              }
//               if (document.getElementById('online_pay_amount').value == "") {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Cash Amount</span></div>";
//                  return false;
//              }

//              if (document.getElementById('online_pay_amount').value > document.getElementById('invtot').value) {
//                  document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Amount is Grater Than Total</span></div>";
//                  return false;
//              }

//          }
//         }

    var balance = document.getElementById('balance').value;
    var crebal = 0;
    if (document.getElementById('crebal').value != "") {
        crebal = document.getElementById('crebal').value;
    }

    i = 0;
    while (i < 4) {
        balance = balance.replace(",", "");
        i = i + 1;
    }

    var invtot = document.getElementById('invtot').value;
    i = 0;
    while (i < 4) {
        invtot = invtot.replace(",", "");
        i = i + 1;
    }

    var crebal = document.getElementById('crebal').value;
    i = 0;
    while (i < 4) {
        crebal = crebal.replace(",", "");
        i = i + 1;
    }

    // if (((parseFloat(crebal) < 0) || (parseFloat(crebal) < parseFloat(invtot))) && (document.getElementById('Result').value != "OK"))
    // {
    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Credit Limit Exceeded</span></div>";
    //     return false;
    // }

    // calc1_table(1);


    var url = "sales_inv_data.php";
    url = url + "?Command=" + "save_inv";
    if (document.getElementById('paymethod_0').checked == true) {
        paymethod = "CR";
    } else if (document.getElementById('paymethod_1').checked == true) {
        paymethod = "CA";
    }
    url = url + "&paymethod=" + paymethod;
    url = url + "&salesord1=" + document.getElementById('salesord1').value;
    url = url + "&inquiry=" + document.getElementById('inquiry').value;
    url = url + "&ordno=" + document.getElementById('ordno').value;
    url = url + "&invno=" + document.getElementById('invno').value;
    url = url + "&tmpno=" + document.getElementById('tmpno').value;
    url = url + "&labour=" + document.getElementById('labour').value;
    url = url + "&cardfee=" + document.getElementById('cardfee').value;

    url = url + "&invdate=" + document.getElementById('invdate').value;
    url = url + "&tel=" + document.getElementById('tel').value;
    url = url + "&customercode=" + document.getElementById('firstname_hidden').value;
    url = url + "&c_subcode=" + document.getElementById('c_subcode').value;
    url = url + "&delitype=" + document.getElementById('delitype').value;


    myString = document.getElementById('firstname').value;
    myString = myString.replace(/&/g, "~");
    url = url + "&customername=" + myString;

    myString = document.getElementById('cus_address').value;
    myString = myString.replace(/&/g, "~");
    url = url + "&cus_address=" + myString;
    //	url=url+"&orderno1="+document.getElementById('orderno1').value;
    //	url=url+"&orderdate="+document.getElementById('orderdate').value;
    url = url + "&vat1=" + document.getElementById('vat1').value;
    url = url + "&vat2=" + document.getElementById('vat2').value;
    url = url + "&creditlimit=" + document.getElementById('creditlimit').value;
    url = url + "&balance=" + document.getElementById('balance').value;
    url = url + "&salesrep=" + document.getElementById('salesrep').value;
    url = url + "&department=" + document.getElementById('department').value;
    url = url + "&brand=" + document.getElementById('brand').value;
    // url = url + "&pay_type=" + document.getElementById('pay_type').value;
    url = url + "&type2=" + document.getElementById('type2').value;


    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "vat";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "non";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "svat";
    } else if (document.getElementById('vatgroup_3').checked == true) {
        vatmethod = "evat";
    }

    url = url + "&vatmethod=" + vatmethod;
    url = url + "&discount_org1=" + document.getElementById('discount_org1').value;
    url = url + "&discount_org2=" + document.getElementById('discount_org2').value;
    url = url + "&discount_org3=" + document.getElementById('discount_org3').value;


    url = url + "&subtot=" + document.getElementById('subtot').value;
    url = url + "&totdiscount=" + document.getElementById('totdiscount').value;
    url = url + "&tax=" + document.getElementById('tax').value;
    url = url + "&invtot=" + document.getElementById('invtot').value;
    url = url + "&credper=" + document.getElementById('credper').value;

    url = url + "&vehino=" + document.getElementById('vehino').value;
    url = url + "&jobno=" + document.getElementById('jobno').value;
    url = url + "&millage=" + document.getElementById('millage').value;
    url = url + "&remarks=" + document.getElementById('remarks').value;


    url = url + "&cash_amount=" + document.getElementById('cash_amount').value;

    url = url + "&cheque_no=" + document.getElementById('cheque_no').value;
    url = url + "&cheque_bank_name=" + document.getElementById('cheque_bank_name').value;
    url = url + "&cheque_date=" + document.getElementById('cheque_date').value;
    url = url + "&cheque_amount=" + document.getElementById('cheque_amount').value;

    url = url + "&card_type=" + document.getElementById('card_type').value;
    url = url + "&card_number=" + document.getElementById('card_number').value;
    url = url + "&card_date=" + document.getElementById('card_date').value;
    url = url + "&card_amount=" + document.getElementById('card_amount').value;

    url = url + "&bank_deposit_bank_name=" + document.getElementById('bank_deposit_bank_name').value;
    url = url + "&bank_deposit_note=" + document.getElementById('bank_deposit_note').value;
    url = url + "&bank_deposit_date=" + document.getElementById('bank_deposit_date').value;
    url = url + "&bank_deposit_amount=" + document.getElementById('bank_deposit_amount').value;

    url = url + "&online_pay_bank_name=" + document.getElementById('online_pay_bank_name').value;
    url = url + "&online_pay_note=" + document.getElementById('online_pay_note').value;
    url = url + "&online_pay_amount=" + document.getElementById('online_pay_amount').value;

    url = url + "&gift_card_no=" + document.getElementById('gift_card_no').value;
    url = url + "&gift_card_amount=" + document.getElementById('gift_card_amount').value;


    url = url + "&pay_type=" + document.getElementById('pay_type').value;



    url = url + "&payamount=0";

    if (document.getElementById('pay_type').value == "CASH") {
        url = url + "&payamount=" + document.getElementById('cash_amount').value;
    }

    if (document.getElementById('pay_type').value == "CARD") {
        url = url + "&payamount=" + document.getElementById('card_amount').value;
    }

    if (document.getElementById('pay_type').value == "CHEQUE") {
        url = url + "&payamount=" + document.getElementById('cheque_amount').value;
    }

    if (document.getElementById('pay_type').value == "GIFT CARD") {
        url = url + "&payamount=" + document.getElementById('gift_card_amount').value;
    }

    if (document.getElementById('pay_type').value == "BANK DEPOSIT") {
        url = url + "&payamount=" + document.getElementById('bank_deposit_amount').value;
    }

    if (document.getElementById('pay_type').value == "ONLINE PAYMENT") {
        url = url + "&payamount=" + document.getElementById('online_pay_amount').value;
    }




    if (document.getElementById('promotion').checked == true) {
        url = url + "&promotion=1";
    } else {
        url = url + "&promotion=0";
    }


    url = url + "&deli_name=" + document.getElementById('deli_name').value;
    url = url + "&deli_address=" + document.getElementById('deli_address').value;

    xmlHttp.onreadystatechange = salessaveresult;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);





}

function salessaveresult()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        var res = xmlHttp.responseText;

        // [VAT INVOICE NO - NEW] =====================================================
        // Server now returns: "P/INV/V/002823 Saved|NEXT:P/INV/V/002824"
        // Split off the NEXT portion before any existing Saved checks.
        var nextInvno = '';
        var pipeIdx   = res.indexOf('|NEXT:');
        if (pipeIdx !== -1) {
            nextInvno = res.substring(pipeIdx + 6); // text after "|NEXT:"
            res       = res.substring(0, pipeIdx);  // strip it so endsWith still works
        }
        // [VAT INVOICE NO - NEW] =====================================================

        var str = res.replace("Saved", "");
        var n = res.endsWith("Saved");

        $('#exampleModal-2').modal('hide');

        if (n == true) {

            document.getElementById('invno').value = str;
            print_inv();

            // [VAT INVOICE NO - NEW] =================================================
            // After new_inv() clears the invno field, restore the next number so
            // the user sees it immediately without waiting for the new_inv AJAX.
            // The setTimeout lets new_inv() finish resetting the field first.
            if (nextInvno !== '') {
                setTimeout(function() {
                    var el = document.getElementById('invno');
                    if (el && el.value === '') {
                        el.value = nextInvno;
                    }
                }, 350);
            }
            // [VAT INVOICE NO - NEW] =================================================

            new_inv();
        } else {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>" + res + "</span></div>";
        }
    }
}

function update_list_inv(stname)
{


    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }


    var url = "search_inv_data.php";
    url = url + "?Command=" + "search_inv";

    if (document.getElementById('invno').value != "") {
        url = url + "&mstatus=invno";
    } else if (document.getElementById('customername').value != "") {
        url = url + "&mstatus=customername";
    } else {
        url = url + "&mstatus=";
    }

    url = url + "&invno=" + document.getElementById('invno').value;
    url = url + "&customername=" + document.getElementById('customername').value;
    url = url + "&stname=" + stname;


    xmlHttp.onreadystatechange = showinvresult_inv;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}

function showinvresult_inv()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        document.getElementById('filt_table').innerHTML = xmlHttp.responseText;
    }
}


function invno_inv(invno, stname)
{


    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }

    if (stname == "grn") {
        var url = "search_inv_data.php";
        url = url + "?Command=" + "pass_grn";
        url = url + "&invno=" + invno;

        //alert(url);
        xmlHttp.onreadystatechange = passinvresult_grn;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    } else if (stname == "search_grn") {
        var url = "search_inv_data.php";
        url = url + "?Command=" + "pass_search_grn";
        url = url + "&invno=" + invno;

        //alert(url);
        xmlHttp.onreadystatechange = passinvresult_search_grn;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    } else if (stname == "crn") {
        var url = "search_inv_data.php";
        url = url + "?Command=" + "pass_crn";
        url = url + "&invno=" + invno;

        //alert(url);
        xmlHttp.onreadystatechange = passinvresult_crn;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    } else {
        var url = "search_inv_data.php";
        url = url + "?Command=" + "pass_invno";
        url = url + "&invno=" + invno;
        url = url + "&custno=" + opener.document.form1.firstname_hidden.value;
        url = url + "&brand=" + opener.document.form1.brand.value;
        url = url + "&department=" + opener.document.form1.department.value;


        xmlHttp.onreadystatechange = passinvresult_inv;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    }
    //alert(url);




}

function passinvresult_inv()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        //  alert(xmlHttp.responseText);

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_invoiceno");
//		document.getElementById('invno1').value = XMLAddress1[0].childNodes[0].nodeValue;
        //alert(XMLAddress1[0].childNodes[0].nodeValue);
        opener.document.form1.invno.value = XMLAddress1[0].childNodes[0].nodeValue;
        //opener.document.form1.invno.value = "111111111";

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("SDATE");
        opener.document.form1.invdate.value = XMLAddress1[0].childNodes[0].nodeValue;

        opener.document.getElementById('remarks').style.visibility = "visible";
        opener.document.getElementById('remarkslbl').style.visibility = "visible";

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("REMARK");
        opener.document.form1.remarks.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("labour");
        opener.document.form1.labour.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("inquiry");
        opener.document.form1.inquiry.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cardfee");
        opener.document.form1.cardfee.value = XMLAddress1[0].childNodes[0].nodeValue;


        opener.document.getElementById('cardfee').disabled = true;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("TYPE");
        if (XMLAddress1[0].childNodes[0].nodeValue == "CR") {
            opener.document.form1.paymethod_0.checked = true;
        } else {
            opener.document.form1.paymethod_1.checked = true;
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cancel");
        if (XMLAddress1[0].childNodes[0].nodeValue != "0") {
            window.opener.document.getElementById('msg_box').innerHTML = "<div class='alert alert-danger' role='alert'><span class='center-block'>Cancelled</span></div>";
        }else{
            window.opener.document.getElementById('msg_box').innerHTML = "";
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_customecode");
        opener.document.form1.firstname_hidden.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("pay_type");
        opener.document.form1.pay_type.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("type2");
        opener.document.form1.type2.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("TEL");
        opener.document.form1.tel.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_customername");
        opener.document.form1.firstname.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_address");
        opener.document.form1.cus_address.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_address2");
        opener.document.form1.cus_address2.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_vatno1");
        opener.document.form1.vat1.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_vatno2");
        opener.document.form1.vat2.value = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_salesrep");
        opener.document.getElementById("salesrep").value = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("delitype");
        opener.document.getElementById("delitype").value = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_orderno1");
        opener.document.form1.salesord1.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_department");
        opener.document.getElementById("department").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("vehino");
        opener.document.getElementById("vehino").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("jobno");
        opener.document.getElementById("jobno").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("millage");
        opener.document.getElementById("millage").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_brand");
        opener.document.getElementById("brand").value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("promotion");
        if (XMLAddress1[0].childNodes[0].nodeValue == "1") {
            opener.document.getElementById("promotion").checked = true;
        } else {
            opener.document.getElementById("promotion").checked = false;
        }


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_vat");

        if (XMLAddress1[0].childNodes[0].nodeValue == '1')
        {
            opener.document.form1.vatgroup_0.checked = true;

        } else if (XMLAddress1[0].childNodes[0].nodeValue == '2')
        {
            opener.document.form1.vatgroup_2.checked = true;

        } else if (XMLAddress1[0].childNodes[0].nodeValue == '3')
        {
            opener.document.form1.vatgroup_3.checked = true;

        } else {
            opener.document.form1.vatgroup_1.checked = true;
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("SVAT");
        if (XMLAddress1[0].childNodes[0].nodeValue > 0) {
            opener.document.form1.vatgroup_2.checked = true;
        }


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_subtotal");
        opener.document.form1.subtot.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_discount");
        opener.document.form1.totdiscount.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_discount1");
        opener.document.form1.discount_org1.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_discount2");
        opener.document.form1.discount_org2.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_discount3");
        opener.document.form1.discount_org3.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_tax");
        opener.document.form1.tax.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cur_invoiceval");
        opener.document.form1.invtot.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table");
        window.opener.document.getElementById('itemdetails').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_dagtable");
        window.opener.document.getElementById('dag_table').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;



        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("txt_crelimi");
        opener.document.form1.creditlimit.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("txt_crebal");
        opener.document.form1.balance.value = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table_acc");
        window.opener.document.getElementById('storgrid').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("deli_chk1");

        if (XMLAddress1[0].childNodes[0].nodeValue == 'Y')
        {
            opener.document.getElementById("deli_chk").checked = true;

        } else {
            opener.document.getElementById("deli_chk").checked = false;
        }
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cred_but");

        if (XMLAddress1[0].childNodes[0].nodeValue == 'Y')
        {
            opener.document.getElementById("cred_up_but").style.visibility = 'visible';
        } else {
            opener.document.getElementById("cred_up_but").style.visibility = 'hidden';
        }

        opener.document.getElementById("deli_label").style.visibility = 'visible';
        opener.document.getElementById("printdeli").style.visibility = 'visible';
        opener.document.getElementById("deli_name").style.visibility = 'visible';
        opener.document.getElementById("deli_address").style.visibility = 'visible';
        opener.document.getElementById("deli_upbut").style.visibility = 'visible';

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("deli_name");
        opener.document.form1.deli_name.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("deli_add");
        opener.document.form1.deli_address.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cred_per");
        opener.document.form1.credper.value = XMLAddress1[0].childNodes[0].nodeValue;



        self.close();

    }
}


function print_inv()
{

    if (document.getElementById('promotion').checked == true) {
        // var url = "rep_inv_1.php";
        var url = "rep_inv_new.php";
    } else {
        var url = "rep_inv_new.php";
    }
    url = url + "?invno=" + document.getElementById('invno').value;

    if (document.getElementById('deli_chk').checked == true) {
        url = url + "&delivachk=Y";
    } else {
        url = url + "&delivachk=N";
    }

    myString = document.getElementById('firstname').value;
    myString = myString.replace(/&/g, "~");
    url = url + "&cus_name=" + myString;

    myString = document.getElementById('cus_address').value;
    myString = myString.replace(/&/g, "~");
    url = url + "&cus_address=" + myString;

    url = url + "&vat1=" + document.getElementById('vat1').value;
    url = url + "&vat2=" + document.getElementById('vat2').value;

    window.open(url);

}

function print_inv1()
{

    var url = "rep_inv_new_1.php";
    url = url + "?invno=" + document.getElementById('invno').value;


    window.open(url);

}

function print_inv2()
{

    var url = "rep_inv_new_2.php";
    url = url + "?invno=" + document.getElementById('invno').value;


    window.open(url);


}


function print_inv_net()
{

    var url = "rep_inv_1.php";
    url = url + "?invno=" + document.getElementById('invno').value;


    myString = document.getElementById('firstname').value;
    myString = myString.replace(/&/g, "~");
    url = url + "&cus_name=" + myString;

    myString = document.getElementById('cus_address').value;
    myString = myString.replace(/&/g, "~");
    url = url + "&cus_address=" + myString;

    url = url + "&vat1=" + document.getElementById('vat1').value;
    url = url + "&vat2=" + document.getElementById('vat2').value;

    window.open(url);

}



//function delichk() {
//    document.getElementById("deli_label").style.visibility = 'visible';
//    document.getElementById("deli_name").style.visibility = 'visible';
//    document.getElementById("deli_address").style.visibility = 'visible';
//}
function delichk() {
    if (document.getElementById('itemdetails').innerHTML == "") {
        document.getElementById("deli_label").style.visibility = 'visible';
        document.getElementById("deli_name").style.visibility = 'visible';
        document.getElementById("deli_address").style.visibility = 'visible';

    } else {
        document.getElementById("deli_label").style.visibility = 'visible';
        document.getElementById("deli_name").style.visibility = 'visible';
        document.getElementById("deli_address").style.visibility = 'visible';
        document.getElementById("deli_upbut").style.visibility = 'visible';
        document.getElementById("printdeli").style.visibility = 'visible';

    }

}

function deli_update() {

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    if (document.getElementById('remarks').value == "") {
        document.getElementById('msg_boxdeliup').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Empty Remarks...</span></div>";
        return false;
    }


    var url = "sales_inv_data.php";
    url = url + "?Command=" + "deli_update";

    url = url + "&invno=" + document.getElementById('invno').value;
    url = url + "&deli_name=" + document.getElementById('deli_name').value;
    url = url + "&deli_address=" + document.getElementById('deli_address').value;
    url = url + "&remarks=" + document.getElementById('remarks').value;
    url = url + "&vehino=" + document.getElementById('vehino').value;
    url = url + "&jobno=" + document.getElementById('jobno').value;
    url = url + "&millage=" + document.getElementById('millage').value;
    url = url + "&salesord1=" + document.getElementById('salesord1').value;

    xmlHttp.onreadystatechange = salessaveresult_deliup;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);


}

function salessaveresult_deliup()
{

    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        document.getElementById('msg_boxdeliup').innerHTML = "<div class='alert alert-success' role='alert'><span class='center-block'>" + xmlHttp.responseText + "</span></div>";
        setTimeout(function () {
            new_inv();
        }, 1000);
    }
}


function credupdate() {

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    if (document.getElementById('credper').value == "") {
        document.getElementById('msg_boxcredup').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Empty Credit Period...</span></div>";
        return false;
    }
    var url = "sales_inv_data.php";
    url = url + "?Command=" + "cred_update";

    url = url + "&invno=" + document.getElementById('invno').value;
    url = url + "&credper=" + document.getElementById('credper').value;

    xmlHttp.onreadystatechange = credupdate_result;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);


}

function credupdate_result()
{

    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        document.getElementById('msg_boxcredup').innerHTML = "<div class='alert alert-success' role='alert'><span class='center-block'>" + xmlHttp.responseText + "</span></div>";
        setTimeout(function () {
            new_inv();
        }, 1000);
    }
}

function print_deli()
{

    var url = "rep_inv_deli.php";
    url = url + "?invno=" + document.getElementById('invno').value;

    if (document.getElementById('deli_chk').checked == true) {
        url = url + "&delivachk=Y";
    } else {
        url = url + "&delivachk=N";
    }

    window.open(url);

}


function pay_type_change(){

    clear_pay_type_change();

    if (document.getElementById('pay_type').value == "CREDIT") {

        var card_pay = document.getElementById("card_view");
        card_pay.style.display = "none";

        var cheque_pay = document.getElementById("cheque_view");
        cheque_pay.style.display = "none";

        var bank_deposit_pay = document.getElementById("bank_deposit_view");
        bank_deposit_pay.style.display = "none";

        var online_pay = document.getElementById("online_pay_view");
        online_pay.style.display = "none";

        var gift_card_pay = document.getElementById("gift_card_view");
        gift_card_pay.style.display = "none";

        var cash_pay = document.getElementById("cash_view");
        cash_pay.style.display = "none";

    }else if(document.getElementById('pay_type').value == "CASH"){

        var card_pay = document.getElementById("card_view");
        card_pay.style.display = "none";

        var cheque_pay = document.getElementById("cheque_view");
        cheque_pay.style.display = "none";

        var bank_deposit_pay = document.getElementById("bank_deposit_view");
        bank_deposit_pay.style.display = "none";

        var online_pay = document.getElementById("online_pay_view");
        online_pay.style.display = "none";

        var gift_card_pay = document.getElementById("gift_card_view");
        gift_card_pay.style.display = "none";

        var cash_pay = document.getElementById("cash_view");
        cash_pay.style.display = "inherit";



    }else if(document.getElementById('pay_type').value == "CARD"){

        var card_pay = document.getElementById("card_view");
        card_pay.style.display = "inherit";

        var cheque_pay = document.getElementById("cheque_view");
        cheque_pay.style.display = "none";

        var bank_deposit_pay = document.getElementById("bank_deposit_view");
        bank_deposit_pay.style.display = "none";

        var online_pay = document.getElementById("online_pay_view");
        online_pay.style.display = "none";

        var gift_card_pay = document.getElementById("gift_card_view");
        gift_card_pay.style.display = "none";

        var cash_pay = document.getElementById("cash_view");
        cash_pay.style.display = "none";

    }else if(document.getElementById('pay_type').value == "CHEQUE"){

        var card_pay = document.getElementById("card_view");
        card_pay.style.display = "none";

        var cheque_pay = document.getElementById("cheque_view");
        cheque_pay.style.display = "inherit";

        var bank_deposit_pay = document.getElementById("bank_deposit_view");
        bank_deposit_pay.style.display = "none";

        var online_pay = document.getElementById("online_pay_view");
        online_pay.style.display = "none";

        var gift_card_pay = document.getElementById("gift_card_view");
        gift_card_pay.style.display = "none";

        var cash_pay = document.getElementById("cash_view");
        cash_pay.style.display = "none";

    }else if(document.getElementById('pay_type').value == "GIFT CARD"){

        var card_pay = document.getElementById("card_view");
        card_pay.style.display = "none";

        var cheque_pay = document.getElementById("cheque_view");
        cheque_pay.style.display = "none";

        var bank_deposit_pay = document.getElementById("bank_deposit_view");
        bank_deposit_pay.style.display = "none";

        var online_pay = document.getElementById("online_pay_view");
        online_pay.style.display = "none";

        var gift_card_pay = document.getElementById("gift_card_view");
        gift_card_pay.style.display = "inherit";

        var cash_pay = document.getElementById("cash_view");
        cash_pay.style.display = "none";


    }else if(document.getElementById('pay_type').value == "BANK DEPOSIT"){

        var card_pay = document.getElementById("card_view");
        card_pay.style.display = "none";

        var cheque_pay = document.getElementById("cheque_view");
        cheque_pay.style.display = "none";

        var bank_deposit_pay = document.getElementById("bank_deposit_view");
        bank_deposit_pay.style.display = "inherit";

        var online_pay = document.getElementById("online_pay_view");
        online_pay.style.display = "none";

        var gift_card_pay = document.getElementById("gift_card_view");
        gift_card_pay.style.display = "none";

        var cash_pay = document.getElementById("cash_view");
        cash_pay.style.display = "none";


    }else if(document.getElementById('pay_type').value == "ONLINE PAYMENT"){

        var card_pay = document.getElementById("card_view");
        card_pay.style.display = "none";

        var cheque_pay = document.getElementById("cheque_view");
        cheque_pay.style.display = "none";

        var bank_deposit_pay = document.getElementById("bank_deposit_view");
        bank_deposit_pay.style.display = "none";

        var online_pay = document.getElementById("online_pay_view");
        online_pay.style.display = "inherit";

        var gift_card_pay = document.getElementById("gift_card_view");
        gift_card_pay.style.display = "none";

        var cash_pay = document.getElementById("cash_view");
        cash_pay.style.display = "none";


    }

}


function clear_pay_type_change(){

    document.getElementById('cheque_no').value="";
    document.getElementById('cheque_bank_name').value="";
    document.getElementById('cheque_date').value= "";
    document.getElementById('cheque_amount').value= "";

    document.getElementById('card_type').value="";
    document.getElementById('card_number').value="";
    document.getElementById('card_date').value="";
    document.getElementById('card_amount').value= "";

    document.getElementById('bank_deposit_bank_name').value="";
    document.getElementById('bank_deposit_note').value="";
    document.getElementById('bank_deposit_date').value= "";
    document.getElementById('bank_deposit_amount').value= "";

    document.getElementById('online_pay_bank_name').value="";
    document.getElementById('online_pay_note').value="";
    document.getElementById('online_pay_amount').value="";

    document.getElementById('gift_card_no').value="";
    document.getElementById('gift_card_amount').value="";

    document.getElementById('cash_amount').value="";

}

function customer_model() {
    document.getElementById('msg_box_customer').innerHTML = "";
    document.getElementById('customer_name').value = "";
    document.getElementById('customer_tel').value = "";
    document.getElementById('customer_address').value = "";
    document.getElementById('customer_vat_no').value = "";
    document.getElementById('customer_svat_no').value = "";
    $('#myModal_search').modal('show');

}

function item_model() {
    document.getElementById('msg_box_item').innerHTML = "";
    document.getElementById('item_name').value = "";
    document.getElementById('item_brand').value = "";
    document.getElementById('item_partno').value = "";
    document.getElementById('item_cost').value = "";
    document.getElementById('item_selling').value = "";
    $('#myModal_searchitem').modal('show');

}

function vendor_save() {

    document.getElementById('msg_box_customer').innerHTML = "";
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    if (document.getElementById('customer_name').value == "") {
        document.getElementById('msg_box_customer').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Name</span></div>";
        return false;
    }
    if (document.getElementById('customer_address').value == "") {
        document.getElementById('msg_box_customer').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Address</span></div>";
        return false;
    }

    var url = "sales_inv_data.php";
    url = url + "?Command=" + "vendor_save";

    url = url + "&customer_name=" + document.getElementById('customer_name').value;
    url = url + "&customer_tel=" + document.getElementById('customer_tel').value;
    url = url + "&customer_address=" + document.getElementById('customer_address').value;
    url = url + "&customer_vat_no=" + document.getElementById('customer_vat_no').value;
    url = url + "&customer_svat_no=" + document.getElementById('customer_svat_no').value;

    xmlHttp.onreadystatechange = re_vendor_save;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);


}

function re_vendor_save()
{

    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("msg");
        if(XMLAddress1[0].childNodes[0].nodeValue=="Saved"){
            document.getElementById('msg_box_customer').innerHTML = "<div class='alert alert-success' role='alert'><span class='center-block'>Saved</span></div>";
            $('#myModal_search').modal('hide');

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("customer_code");
            document.getElementById('firstname_hidden').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("customer_name");
            document.getElementById('firstname').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("customer_tel");
            document.getElementById('tel').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("customer_address");
            document.getElementById('cus_address').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("customer_vat_no");
            vat1 = XMLAddress1[0].childNodes[0].nodeValue;
            document.getElementById('vat1').value = vat1;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("customer_svat_no");
            vat2 = XMLAddress1[0].childNodes[0].nodeValue;
            document.getElementById('vat2').value = vat2;


            if ((vat1.trim() == '') && (vat2.trim() == '')) {
                document.form1.vatgroup_1.checked = true;
            } else if ((vat1.trim() != '') && (vat2.trim() == '')) {
                document.form1.vatgroup_0.checked = true;
            } else if ((vat1.trim() != '') && (vat2.trim() != '')) {
                document.form1.vatgroup_2.checked = true;
            }
            document.form1.vatgroup_3.disabled = true;

        }else if(XMLAddress1[0].childNodes[0].nodeValue=="Already Found Customer..!!!!"){
            document.getElementById('msg_box_customer').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Already Found Customer..!!!!</span></div>";
        }else if(XMLAddress1[0].childNodes[0].nodeValue=="Error Result.."){
            document.getElementById('msg_box_customer').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Error Result..</span></div>";
        }else if(XMLAddress1[0].childNodes[0].nodeValue=="Please Enter Vat No"){
            document.getElementById('msg_box_customer').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Vat No</span></div>";
        }else if(XMLAddress1[0].childNodes[0].nodeValue=="Can't Add Vat Customer.Please Clear Vat No & Svat No"){
            document.getElementById('msg_box_customer').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Can't Add Vat Customer.Please Clear Vat No & Svat No</span></div>";
        }

    }
}

function item_save() {

    document.getElementById('msg_box_item').innerHTML = "";
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    if (document.getElementById('item_name').value == "") {
        document.getElementById('msg_box_item').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Name</span></div>";
        return false;
    }
    if (document.getElementById('item_brand').value == "") {
        document.getElementById('msg_box_item').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Brand</span></div>";
        return false;
    }
    if (document.getElementById('item_partno').value == "") {
        document.getElementById('msg_box_item').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Part No</span></div>";
        return false;
    }
    if (document.getElementById('item_cost').value == "") {
        document.getElementById('msg_box_item').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Cost</span></div>";
        return false;
    }
    if (document.getElementById('item_selling').value == "") {
        document.getElementById('msg_box_item').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Selling</span></div>";
        return false;
    }

    var url = "sales_inv_data.php";
    url = url + "?Command=" + "item_save";

    url = url + "&item_name=" + document.getElementById('item_name').value;
    url = url + "&item_brand=" + document.getElementById('item_brand').value;
    url = url + "&item_partno=" + document.getElementById('item_partno').value;
    url = url + "&item_cost=" + document.getElementById('item_cost').value;
    url = url + "&item_selling=" + document.getElementById('item_selling').value;

    xmlHttp.onreadystatechange = re_item_save;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);


}

function re_item_save()
{

    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("msg");
        if(XMLAddress1[0].childNodes[0].nodeValue=="Saved"){
            document.getElementById('msg_box_item').innerHTML = "<div class='alert alert-success' role='alert'><span class='center-block'>Saved</span></div>";
            $('#myModal_searchitem').modal('hide');

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("item");
            document.getElementById('itemd_hidden').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("name");
            document.getElementById('itemd').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("part_no");
            document.getElementById('serialno').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("rate");
            document.getElementById('rate').value = XMLAddress1[0].childNodes[0].nodeValue;



        }else {
            document.getElementById('msg_box_item').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>" + xmlHttp.responseText + "</span></div>";
        }

    }
}

function find_customer() {

    document.getElementById('msg_box_customer').innerHTML = "";
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }


    var url = "sales_inv_data.php";
    url = url + "?Command=" + "find_customer";

    url = url + "&customer_tel=" + document.getElementById('customer_tel').value;

    xmlHttp.onreadystatechange = re_find_customer;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);


}


function re_find_customer()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {

        if (xmlHttp.responseText != "") {
            document.getElementById('msg_box_customer').innerHTML = "<div class='alert alert-success' role='alert'><span class='center-block'>" + xmlHttp.responseText + "</span></div>";
        } else {
            document.getElementById('msg_box_customer').innerHTML = "";
        }



    }
}

function load_inquiry()
{

    NewWindow('sales_inv_inq_search.php', 'mywin', '800', '700', 'yes', 'center');

}

function custno5(code)
{
    //alert(code);
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "sales_inv_data.php";
    url = url + "?Command=" + "pass_quot5";
    url = url + "&custno=" + code;

    xmlHttp.onreadystatechange = re_pass_quot5;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}

function re_pass_quot5()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("id");
        var obj = JSON.parse(XMLAddress1[0].childNodes[0].nodeValue);

        opener.document.getElementById('inquiry').value= obj.REFNO ;
        opener.document.getElementById('firstname_hidden').value= "V/0001" ;
        opener.document.getElementById('c_subcode').value = "V/0001";
        opener.document.getElementById('firstname').value= obj.NAME ;

        self.close();
    }


}



function custno1(code,jobno,serialno)
{
    //alert(code);
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    if (opener.document.getElementById('firstname_hidden').value == "") {
        alert("Customer Not Selected");
        return false;
    }

    var url = "sales_inv_data.php";
    url = url + "?Command=" + "pass_dag";
    url = url + "&custno=" + code;
    url = url + "&jobno=" + jobno;
    url = url + "&serialno=" + serialno;

    xmlHttp.onreadystatechange = passcusresult_quot1;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}


function passcusresult_quot1()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("val");
        if (XMLAddress1[0].childNodes[0].nodeValue == "no") {
            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("id");
            var obj = JSON.parse(XMLAddress1[0].childNodes[0].nodeValue);

            opener.document.getElementById('dag_jobno').value = obj.jobno;
            opener.document.getElementById('dag_make').value = obj.marker;
            opener.document.getElementById('dag_design').value = obj.belt;
            opener.document.getElementById('dag_size').value = obj.size;
            opener.document.getElementById('dag_serialno').value = obj.serialno;
            opener.document.getElementById('dag_cost').value = obj.cost;
            opener.document.getElementById('dag_selling').value = obj.total;
            opener.document.getElementById('dag_subtotal').value = obj.total;
            opener.document.getElementById('dag_dis').value = 0;
        }else{
            alert("ALready Added Dag...");
            return false;
        }






        self.close();
    }

}

function calc_dag() {


    var disper = 0;
    var disper1 = 0;
    var subtot = 0;
    var subtot1 = 0;

    if ((document.getElementById('dag_dis').value != '') && (document.getElementById('dag_dis').value != "0") && (document.getElementById('dag_dis').value != "0.00")) {

        disper = document.getElementById('dag_dis').value;
        disper1 = document.getElementById('dag_dis').value;

    }

    document.getElementById('dag_dis').value = disper;
    subtot1= document.getElementById('dag_selling').value*1;
    subtot = subtot1 - (subtot1  * parseFloat(document.getElementById('dag_dis').value) / 100);



    document.getElementById('dag_subtotal').value = subtot;

    // add_tmp('up');
}

// ======================================== pay model=================

function pay_model() {

    document.getElementById('msg_box').innerHTML ="";
    document.getElementById('msg_box_payment').innerHTML ="";
    document.getElementById('msg_box').innerHTML ="";

    document.getElementById('pay_remark').value ="";


    if (document.getElementById('tmpno').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please refresh page</span></div>";
        return false;
    }

    if (document.getElementById('invdate').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Date</span></div>";
        showDangerToast('top-left');
        return false;
    }
    if (document.getElementById('department').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Department</span></div>";
        showDangerToast('top-left');
        return false;
    }

    if (document.getElementById('salesrep').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Rep</span></div>";
        showDangerToast('top-left');
        return false;
    }

    if (document.getElementById('pay_type').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Payment Type</span></div>";
        return false;
    }

    // if (document.getElementById('regi_vat_type').value == "") {

    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>please Select VAT Type</span></div>";
    //     return false;
    // }
    // if (document.getElementById('regi_vat_type').value == "Vat") {

    //     if (document.getElementById('regi_vat_no').value == "") {
    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>please Enter VAT No</span></div>";
    //     return false;
    //     }
    // }
    // if (document.getElementById('regi_vat_type').value == "Svat") {

    //     if (document.getElementById('regi_vat_no').value == "" | document.getElementById('regi_s_vat_no').value == "") {
    //     document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>please Enter VAT No And SVAT No</span></div>";
    //     return false;
    //     }
    // }

    if(document.getElementById('pay_type').value == "CREDIT"){

        var msg = confirm("Are you sure to Credit Payment  ! ");
        if (msg == true) {
            save_inv();
        }

    }else{

        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null) {
            alert("Browser does not support HTTP Request");
            return;
        }


        var url = "sales_inv_data.php";
        var params = "Command=" + "pay_model";
        params=params+"&tmp_uniq="+document.getElementById('tmpno').value;

        params=params+"&pay_type="+document.getElementById('pay_type').value;


        xmlHttp.open("POST", url, true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.onreadystatechange = re_pay_model;
        xmlHttp.send(params);

        // $('#exampleModal-2').modal('show');
    }

}


function re_pay_model() {

    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")



        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("row_count");
    document.getElementById('row_count').value = XMLAddress1[0].childNodes[0].nodeValue;

    if (document.getElementById('row_count').value == "0") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>invoice Items Is Empty</span></div>";
        return false;
    }

    // XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tot_count");
    // document.getElementById('model_all_itemtot').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

    // XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("all_itemdis");
    // document.getElementById('model_all_itemdis').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

    // XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tot_count");
    // document.getElementById('model_all_grand_total').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

    // XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tot_count");
    // document.getElementById('pay_grand_total').value = XMLAddress1[0].childNodes[0].nodeValue;


    //   XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("pay_card");
    // document.getElementById('pay_card').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;


    $('#exampleModal-2').modal('show');

    $('#model_payment_type').val(document.getElementById('pay_type').value);
    $('#model_payment_type').trigger('change');

    new_card();

}


function new_card(){

    document.getElementById('msg_box_payment').innerHTML ="";
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }


    var url = "sales_inv_data.php";
    var params = "Command=" + "new_card";
    // params = params + "&Command1=" + "del_item";

    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "vat";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "Non Vat";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "svat";
    } else if (document.getElementById('vatgroup_3').checked == true) {
        vatmethod = "evat";
    }

    params=params+"&regi_vat_type="+ vatmethod;
    params=params+"&tmp_uniq="+document.getElementById('tmpno').value;
    params = params + "&cardfee=" + document.getElementById('cardfee').value;
    params=params+"&payment_type="+document.getElementById('model_payment_type').value;


    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = re_new_card;
    xmlHttp.send(params);
}

function re_new_card() {

    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("msg_ex");
        if (XMLAddress1[0].childNodes[0].nodeValue == "This Card Paid Amount Is 0.00") {
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>In Some Payment Card Not Saved</span></div>";
            return false;
        }else{
            document.getElementById('msg_box_payment').innerHTML ="";
            // XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("row_count");
            // document.getElementById('row_count').value = XMLAddress1[0].childNodes[0].nodeValue;

            // if (document.getElementById('row_count').value == "") {
            //     alert(document.getElementById('row_count').value);
            //      document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>invoice Items Is Empty</span></div>";
            //      return false;
            // }

            // XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tot_count");
            // document.getElementById('model_all_itemtot').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

            // XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("all_itemdis");
            // document.getElementById('model_all_itemdis').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

            // XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tot_count");
            // document.getElementById('model_all_grand_total').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

            // XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tot_count");
            // document.getElementById('pay_grand_total').value = XMLAddress1[0].childNodes[0].nodeValue;


            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("pay_card");
            document.getElementById('pay_card').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("count");
            document.getElementById('count').value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("summary");
            document.getElementById('summary').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;


            $('#exampleModal-2').modal('show');

        }
    }

}

function delete_card(refno){

    document.getElementById('msg_box').innerHTML ="";
    var msg = confirm("Are you sure to delete  ! ");
    if (msg == true) {
        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null) {
            alert("Browser does not support HTTP Request");
            return;
        }

        document.getElementById('msg_box_payment').innerHTML ="";
        var url = "sales_inv_data.php";
        var params = "Command=" + "new_card";
        params = params + "&delete_card=" + "del_item";
        params=params+"&refno="+refno;
        params=params+"&tmp_uniq="+document.getElementById('tmpno').value;
        // alert(params);


        xmlHttp.open("POST", url, true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.onreadystatechange = re_new_card;
        xmlHttp.send(params);
    }
}


function tmp_save_card(id , refno){


    // alert("Over Payment Found. Do You Wan't To Process...");

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }

    document.getElementById('msg_box_payment').innerHTML = "";
    document.getElementById('error_msg').value = "";

    var card_paid_amount = "card_paid_amount" + id;
    var card_name = "card_name" + id;
    var card_number = "card_number" + id;
    var card_expiry_date = "card_expiry_date" + id;

    var cash_amount = "cash_amount" + id;

    var cheque_amount = "cheque_amount" + id;
    var cheque_no = "cheque_no" + id;
    var cheque_bank_name = "cheque_bank_name" + id;
    var cheque_date = "cheque_date" + id;

    var bank_transfer_amount = "bank_transfer_amount" + id;
    var bank_transfer_bank_name = "bank_transfer_bank_name" + id;
    var bank_transfer_note = "bank_transfer_note" + id;
    var bank_transfer_date = "bank_transfer_date" + id;

    var online_pay_amount = "online_pay_amount" + id;
    var online_pay_note = "online_pay_note" + id;

    var gift_card_no_amount = "gift_card_no_amount" + id;
    var gift_card_no = "gift_card_no" + id;
    var boder_coler = "boder_coler" + id;


    var paytype = "paytype" + id;
    var status = "status" + id;
    var pay_card_msg = "pay_card_msg" + id;


    document.getElementById(pay_card_msg).innerHTML ="";

    if (document.getElementById(paytype).value == "") {
        document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Select Payment Type</span></div>";
        showDangerToast('top-left');
        return false;
    }



    if(document.getElementById(paytype).value == "CARD"){

        if(document.getElementById(card_paid_amount).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Card Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Card Amount</span>";
            return false;

        }
        if(document.getElementById(card_paid_amount).value <= 0){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Card Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Card Amount</span>";
            return false;

        }

        if(document.getElementById(card_name).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Card Name</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Card Name</span>";
            return false;
        }

        if(document.getElementById(card_number).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Card Number</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Card Number</span>";

            return false;

        }

        //  if(document.getElementById(card_expiry_date).value == ""){
        //      change_status_save(id,refno);
        //     document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter  Expiry Date</span></div>";
        //      document.getElementById(boder_coler).style.border="solid red";
        //     document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Expiry Date</span>";
        //     return false;

        //  }

    }if(document.getElementById(paytype).value == "CHEQUE"){

        if(document.getElementById(cheque_amount).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Cheque Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Cheque Amount</span>";
            return false;

        }

        if(document.getElementById(cheque_amount).value <= 0){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Cheque Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Cheque Amount</span>";
            return false;

        }


        if(document.getElementById(cheque_no).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Cheque No</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Cheque Not</span>";
            return false;
        }

        if(document.getElementById(cheque_bank_name).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Select Cheque Bank Name</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Select Cheque Bank Name</span>";
            return false;

        }

        if(document.getElementById(cheque_date).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Select Cheque Date</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Select Cheque Date</span>";
            return false;

        }

    }


    if(document.getElementById(paytype).value == "CASH"){

        if(document.getElementById(cash_amount).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Cash Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Cash Amount</span>";
            return false;

        }

        if(document.getElementById(cash_amount).value <= 0){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Cash Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Cheque Amount</span>";
            return false;
        }


    }


    if(document.getElementById(paytype).value == "BANK TRANSFER"){

        if(document.getElementById(bank_transfer_amount).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Bank Transfer Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Bank Transfer Amount</span>";
            return false;

        }

        if(document.getElementById(bank_transfer_amount).value <= 0){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Transfer Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Bank Transfer Amount</span>";
            return false;
        }

        if(document.getElementById(bank_transfer_bank_name).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Transfer Bank Name</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Select Transfer Bank Name</span>";
            return false;
        }

        //  if(document.getElementById(bank_transfer_note).value == ""){
        //      change_status_save(id,refno);
        //     document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Transfer Note</span></div>";
        //     document.getElementById(boder_coler).style.border="solid red";
        //     document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Transfer Note</span>";
        //     return false;

        //  }

        if(document.getElementById(bank_transfer_date).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Select Bank Transfer Date</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Select Bank Transfer Date</span>";
            return false;

        }

    }

    if(document.getElementById(paytype).value == "ONLINE PAYMENT"){

        if(document.getElementById(online_pay_amount).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Online Pay Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Online Pay Amount</span>";
            return false;

        }

        if(document.getElementById(online_pay_amount).value <= 0){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Online Pay Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Online Pay Amount</span>";
            return false;
        }

        if(document.getElementById(online_pay_note).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Online Pay Note</span></div>";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Online Pay Note</span>";
            return false;
        }


    }

    if(document.getElementById(paytype).value == "GIFT CARD"){

        if(document.getElementById(gift_card_no_amount).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Gift Card Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Gift Card Amount</span>";
            return false;

        }

        if(document.getElementById(gift_card_no_amount).value <= 0){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Gift Card Amount</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Gift Card Amount</span>";
            return false;
        }

        if(document.getElementById(gift_card_no).value == ""){
            change_status_save(id,refno);
            document.getElementById('msg_box_payment').innerHTML = "<div class='alert alert-warning m-0' role='alert'><span class='center-block'>Please Enter Gift Card No</span></div>";
            document.getElementById(boder_coler).style.border="solid red";
            document.getElementById(pay_card_msg).innerHTML = "<span class='alert alert-danger ' role='alert' style='padding :0.43rem 1.25rem !important'> Payment Not Saved : Please Enter Gift Card No</span>";
            return false;
        }

    }



    // }
    document.getElementById('msg_box_payment').innerHTML = "";
    document.getElementById(pay_card_msg).innerHTML = "";

    var url = "sales_inv_data.php";
    var params = "Command=" + "tmp_save_card";

    var amount=0.00

    params=params+"&refno="+refno;
    params=params+"&tmp_uniq="+document.getElementById('tmpno').value;

    params = params + "&cardfee=" + document.getElementById('cardfee').value;

    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "vat";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "Non Vat";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "svat";
    } else if (document.getElementById('vatgroup_3').checked == true) {
        vatmethod = "evat";
    }

    params=params+"&regi_vat_type="+ vatmethod;

    if(document.getElementById(paytype).value == "CARD"){

        amount = document.getElementById(card_paid_amount).value;
        params=params+"&card_paid_amount="+document.getElementById(card_paid_amount).value;
        params=params+"&card_name="+document.getElementById(card_name).value;
        params=params+"&card_number="+document.getElementById(card_number).value;
        params=params+"&card_expiry_date="+document.getElementById(card_expiry_date).value;

    }
    if(document.getElementById(paytype).value == "CASH"){

        amount = document.getElementById(cash_amount).value;
        params=params+"&cash_amount="+document.getElementById(cash_amount).value;

    }
    if(document.getElementById(paytype).value == "CHEQUE"){

        amount = document.getElementById(cheque_amount).value;
        params=params+"&cheque_amount="+document.getElementById(cheque_amount).value;
        params=params+"&cheque_no="+document.getElementById(cheque_no).value;
        params=params+"&cheque_bank_name="+document.getElementById(cheque_bank_name).value;
        params=params+"&cheque_date="+document.getElementById(cheque_date).value;

    }
    if(document.getElementById(paytype).value == "BANK TRANSFER"){

        amount = document.getElementById(bank_transfer_amount).value;
        params=params+"&bank_transfer_amount="+document.getElementById(bank_transfer_amount).value;
        params=params+"&bank_transfer_bank_name="+document.getElementById(bank_transfer_bank_name).value;
        params=params+"&bank_transfer_note="+document.getElementById(bank_transfer_note).value;
        params=params+"&bank_transfer_date="+document.getElementById(bank_transfer_date).value;

    }

    if(document.getElementById(paytype).value == "ONLINE PAYMENT"){

        amount = document.getElementById(online_pay_amount).value;
        params=params+"&online_pay_amount="+document.getElementById(online_pay_amount).value;
        params=params+"&online_pay_note="+document.getElementById(online_pay_note).value;

    }

    if(document.getElementById(paytype).value == "GIFT CARD"){

        amount = document.getElementById(gift_card_no_amount).value;
        params=params+"&gift_card_no_amount="+document.getElementById(gift_card_no_amount).value;
        params=params+"&gift_card_no="+document.getElementById(gift_card_no).value;


    }

    params=params+"&regi_date="+document.getElementById('invdate').value;
    params=params+"&paytype="+document.getElementById(paytype).value;
    params=params+"&status="+document.getElementById(status).value;
    params=params+"&amount="+amount;

    document.getElementById(boder_coler).style.border="solid #49c152";

    document.getElementById(pay_card_msg).innerHTML="<span class='alert alert-success' role='alert' style='padding :0.43rem  1.25rem !important;'> Payment Temporary Saved </span>";

    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = re_tmp_save_card;
    xmlHttp.send(params);



}

function re_tmp_save_card() {

    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("summary");
        document.getElementById('summary').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        showSuccessToast();

        $('#model_payment_type').val('');
        $('#model_payment_type').trigger('change');

    }

}

function change_status_save(id,refno){

    var card_paid_amount = "card_paid_amount" + id;
    var card_name = "card_name" + id;
    var card_number = "card_number" + id;
    var card_expiry_date = "card_expiry_date" + id;
    var cash_amount = "cash_amount" + id;
    var cheque_amount = "cheque_amount" + id;
    var cheque_no = "cheque_no" + id;
    var cheque_bank_name = "cheque_bank_name" + id;
    var cheque_date = "cheque_date" + id;
    var bank_transfer_amount = "bank_transfer_amount" + id;
    var bank_transfer_bank_name = "bank_transfer_bank_name" + id;
    var bank_transfer_note = "bank_transfer_note" + id;
    var bank_transfer_date = "bank_transfer_date" + id;
    var online_pay_amount = "online_pay_amount" + id;
    var online_pay_note = "online_pay_note" + id;
    var gift_card_no_amount = "gift_card_no_amount" + id;
    var gift_card_no = "gift_card_no" + id;
    var paytype = "paytype" + id;

    var url = "sales_inv_data.php";
    var params = "Command=" + "change_status_save";

    params=params+"&refno="+refno;
    params=params+"&tmp_uniq="+document.getElementById('tmpno').value;
    params = params + "&cardfee=" + document.getElementById('cardfee').value;
    if(document.getElementById(paytype).value == "CARD"){

        amount = document.getElementById(card_paid_amount).value;

        params=params+"&card_paid_amount="+document.getElementById(card_paid_amount).value;
        params=params+"&card_name="+document.getElementById(card_name).value;
        params=params+"&card_number="+document.getElementById(card_number).value;
        params=params+"&card_expiry_date="+document.getElementById(card_expiry_date).value;

    }
    if(document.getElementById(paytype).value == "CASH"){

        amount = document.getElementById(cash_amount).value;
        params=params+"&cash_amount="+document.getElementById(cash_amount).value;

    }
    if(document.getElementById(paytype).value == "CHEQUE"){

        amount = document.getElementById(cheque_amount).value;
        params=params+"&cheque_amount="+document.getElementById(cheque_amount).value;
        params=params+"&cheque_no="+document.getElementById(cheque_no).value;
        params=params+"&cheque_bank_name="+document.getElementById(cheque_bank_name).value;
        params=params+"&cheque_date="+document.getElementById(cheque_date).value;

    }
    if(document.getElementById(paytype).value == "BANK TRANSFER"){

        amount = document.getElementById(bank_transfer_amount).value;
        params=params+"&bank_transfer_amount="+document.getElementById(bank_transfer_amount).value;
        params=params+"&bank_transfer_bank_name="+document.getElementById(bank_transfer_bank_name).value;
        params=params+"&bank_transfer_note="+document.getElementById(bank_transfer_note).value;
        params=params+"&bank_transfer_date="+document.getElementById(bank_transfer_date).value;

    }

    if(document.getElementById(paytype).value == "ONLINE PAYMENT"){

        amount = document.getElementById(online_pay_amount).value;
        params=params+"&online_pay_amount="+document.getElementById(online_pay_amount).value;
        params=params+"&online_pay_note="+document.getElementById(online_pay_note).value;

    }

    if(document.getElementById(paytype).value == "GIFT CARD"){

        amount = document.getElementById(gift_card_no_amount).value;
        params=params+"&gift_card_no_amount="+document.getElementById(gift_card_no_amount).value;
        params=params+"&gift_card_no="+document.getElementById(gift_card_no).value;

    }
    params=params+"&paytype="+document.getElementById(paytype).value;
    params=params+"&amount="+amount;

    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = re_change_status_save;
    xmlHttp.send(params);

}

function re_change_status_save() {

    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("summary");
        document.getElementById('summary').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

    }

}


function search_item() {
    $('#item_modal_search').modal('show');
    // document.getElementById('stk_no').focus;
    // document.getElementById('chk_stock').checked = false;

    search_item_search('');
}


function search_item_search(cdata) {

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }

    var url = 'sales_inv_data.php';
    var params = 'Command=' + 'search_item_search';
    params = params + '&s_refno=' + document.getElementById('s_refno').value;
    params = params + '&s_desc=' + document.getElementById('s_desc').value;
    params = params + '&s_brand=' + document.getElementById('s_brand').value;
    params = params + '&s_partno=' + document.getElementById('s_partno').value;
    params = params + '&s_cat=' + document.getElementById('s_cat').value;
    params = params + '&s_list=' + document.getElementById('s_list').value;
    params = params + '&s_cash=' + document.getElementById('s_cash').value;
    params = params + '&s_credit=' + document.getElementById('s_credit').value;
    params = params + '&s_qty=' + document.getElementById('s_qty').value;

    if (document.getElementById('chk_stock').checked == true) {
        stkonly = "0";
    } else if (document.getElementById('chk_stock').checked == false) {
        stkonly = "1";
    }
    params = params + "&stkonly=" + stkonly;

    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");

    xmlHttp.onreadystatechange = re_search_item_search;

    xmlHttp.send(params);

}

function re_search_item_search() {

    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table");
        document.getElementById('searchtable').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;



    }
}

function search_customer() {

    $('#customer_modal_search').modal('show');

    search_customer_search('');
}

function search_customer_search(cdata) {

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }

    var url = 'sales_inv_data.php';
    var params = 'Command=' + 'search_customer_search';
    params = params + '&c_refno=' + document.getElementById('c_refno').value;
    params = params + '&c_name=' + document.getElementById('c_name').value;
    params = params + '&c_add=' + document.getElementById('c_add').value;
    params = params + '&c_tel=' + document.getElementById('c_tel').value;
    params = params + '&c_subcus=' + document.getElementById('c_subcus').value;
    // alert("ok");
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");

    xmlHttp.onreadystatechange = re_search_customer_search;

    xmlHttp.send(params);

}

function re_search_customer_search() {

    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table");
        document.getElementById('search_cus_table').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;



    }
}


function custno2(itno)
{
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }

    // 1. Get the Pay Type text (Cash or Credit)
    var payElement = document.getElementById("pay_type");
    var pay_name = payElement ? payElement.options[payElement.selectedIndex].text : "Credit";

    var url = "search_item_data.php";
    url = url + "?Command=" + "pass_itno";
    url = url + "&itno=" + itno;
    
    // 2. Append pay_name to the URL so the backend knows which price to pick
    url = url + "&pay_name=" + encodeURIComponent(pay_name);

    xmlHttp.onreadystatechange = re_custno2;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function re_custno2()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        document.getElementById('itemd_hidden').value = "";
        document.getElementById('itemd').value = "";
        document.getElementById('stklevel').value = "";
        document.getElementById('rate').value = "";
        document.getElementById('qty').value = "";
        document.getElementById('discountper').value = "0";
        document.getElementById('discountper1').value = "0";
        document.getElementById('subtotal').value = "";

        // Safe extraction function
        function getNodeValue(tagName) {
            var node = xmlHttp.responseXML.getElementsByTagName(tagName);
            if (node && node[0] && node[0].childNodes.length > 0) {
                return node[0].childNodes[0].nodeValue;
            }
            return "";
        }

        var type_val = getNodeValue("type").trim();
        
        if ((type_val =="TO BE EDIT ITEM") || (type_val =="TO BE EDIT ITEM RATE MINUS")) {
            document.getElementById('itemd').disabled = false;
            document.form1.cat1.value = type_val;
        } else {
            document.form1.cat1.value = type_val;
        }

        document.form1.itemd_hidden.value = getNodeValue("str_code");
        document.form1.itemd.value = getNodeValue("str_description");
        document.form1.stklevel.value = getNodeValue("qtyinhand");
        
        // This will now dynamically pull the Cash or Credit price!
        document.form1.rate.value = getNodeValue("actual_selling"); 

        document.getElementById('qty').focus();

        document.getElementById('s_refno').value = "";
        document.getElementById('s_desc').value = "";
        document.getElementById('s_brand').value = "";
        document.getElementById('s_partno').value = "";
        document.getElementById('s_cat').value = "";
        document.getElementById('s_list').value = "";
        document.getElementById('s_cash').value = "";
        document.getElementById('s_credit').value = "";
        document.getElementById('s_qty').value = "";

        $('#item_modal_search').modal('hide');
    }
}

function custno3(custno){

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }

    var url = "../search_custom_data.php";

    url = url + "?Command=" + "pass_cusno";
    url = url + "&custno=" + custno;
    // url = url + "&brand=" + document.form1.brand.value;
    // url = url + "&salesrep=" + document.form1.salesrep.value;
    //alert(url);
    xmlHttp.onreadystatechange = re_custno3;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}

function re_custno3()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        // alert(xmlHttp.responseText);


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("id");
        document.form1.firstname_hidden.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_customername");
        document.form1.firstname.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_address");
        document.form1.cus_address.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tel");
        document.getElementById('tel').value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_vatno");
        document.form1.vat1.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_svatno");
        document.form1.vat2.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("c_subcode");
        document.form1.c_subcode.value = XMLAddress1[0].childNodes[0].nodeValue;


        vat1 = document.form1.vat1.value;
        vat2 = document.form1.vat2.value;

        if ((vat1.trim() == '') && (vat2.trim() == '')) {
            document.form1.vatgroup_1.checked = true;
//            document.form1.vatgroup_0.disabled = true;
//            document.form1.vatgroup_1.disabled = false;
//            document.form1.vatgroup_2.disabled = true;
//            document.form1.vatgroup_3.disabled = true;
        } else if ((vat1.trim() != '') && (vat2.trim() == '')) {
            document.form1.vatgroup_0.checked = true;
            //            document.form1.vatgroup_0.disabled = false;
            //            document.form1.vatgroup_1.disabled = false;
            //            document.form1.vatgroup_2.disabled = true;
            //            document.form1.vatgroup_3.disabled = true;
        } else if ((vat1.trim() != '') && (vat2.trim() != '')) {
            document.form1.vatgroup_2.checked = true;
            //            document.form1.vatgroup_0.disabled = false;
            //            document.form1.vatgroup_1.disabled = false;
            //            document.form1.vatgroup_2.disabled = false;
            //            document.form1.vatgroup_3.disabled = true;
        }
        document.form1.vatgroup_3.disabled = true;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("crebal");
        document.form1.crebal.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("txt_crelimi");
        document.form1.creditlimit.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("txt_crebal");
        document.form1.balance.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("over60_txt");
        document.form1.over60.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("netqty");
        document.form1.txtinvt.value = XMLAddress1[0].childNodes[0].nodeValue;


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("over60_qst");
        if (XMLAddress1[0].childNodes[0].nodeValue != "") {
            document.form1.over60.value = "0";
        }



        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table_acc");
        window.document.getElementById('storgrid').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;



        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("AltMessage");
        var a = XMLAddress1[0].childNodes[0].nodeValue;
        if (a.trim() != "") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("bank_message");
        var a = XMLAddress1[0].childNodes[0].nodeValue;
        if (a.trim() != "") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("over60_message");
        var a = XMLAddress1[0].childNodes[0].nodeValue;
        if (a.trim() != "") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("chq_message");
        var a = XMLAddress1[0].childNodes[0].nodeValue;
        if (a.trim() != "") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("chq_message_que");
        var a = XMLAddress1[0].childNodes[0].nodeValue;
        if (a.trim() != "") {
            var ans = confirm(XMLAddress1[0].childNodes[0].nodeValue);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("msg_return");
        var a = XMLAddress1[0].childNodes[0].nodeValue;
        if (a.trim() != "") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("blacklist");
        var a = XMLAddress1[0].childNodes[0].nodeValue;
        if (a.trim() == "1") {
            alert("Blocked For Invoicing");
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("stname");
        if (XMLAddress1[0].childNodes[0].nodeValue == "sales_order") {
            //            self.close();
        } else {
            calc();
        }

        document.getElementById('c_refno').value = "";
        document.getElementById('c_name').value = "";
        document.getElementById('c_add').value = "";
        document.getElementById('c_tel').value = "";
        document.getElementById('c_subcus').value = "";

        $('#customer_modal_search').modal('hide');

// self.close();

    }
    
    function pass_itno(item_code) { // Your actual function name may vary
    // 1. Get the current selected Pay Type
    var pay_type = document.getElementById("pay_type").value; 

    // 2. Initialize AJAX
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }

    // 3. Append pay_type to the URL string
    var url = "search_item_data.php";
    url = url + "?Command=pass_itno";
    url = url + "&itno=" + item_code;
    url = url + "&pay_type=" + encodeURIComponent(pay_type); // Pass it here

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
            // Your existing logic to parse the XML and add to div id="product"
            // e.g., document.getElementById('price').value = xmlHttp.responseXML.getElementsByTagName("price")[0].childNodes[0].nodeValue;
        }
    };
    
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}
}


function checkPayTypeChange() {
    var table = document.getElementById("product"); // Or check your temporary invoice array/table
    
    // If there are already items in the grid, warn the user that prices need refreshing
    if (table.rows && table.rows.length > 1) { 
        alert("Warning: You changed the Pay Type. Please clear and re-add items to apply the new pricing tier, or update prices manually.");
        // Alternatively, write an AJAX function to loop through tmp_inv_data and update all prices automatically.
    }
}