 

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 = "CREDIT";
    document.getElementById('type2').value = "NORMAL";
    document.getElementById('txtreturn').value = "0";
    //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('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('dano').value="";
    
    //  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("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;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("vattype");
        if(XMLAddress1[0].childNodes[0].nodeValue=="1"){ 
            document.getElementById('vatgroup_0').checked = true;
        }else{ 
            document.getElementById('vatgroup_1').checked = true;
        }
        
        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;
   
    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 = 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 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; 
    discountper = "discountper" + 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 dis1 = 0;
    var dis2 = 0;
    var dis3 = 0;
    var disper = 0;
    var dis1f = 0;
    var dis2f = 0;
    var dis3f = 0;


    subtotal = parseFloat(SELLING) - (parseFloat(SELLING) * parseFloat(document.getElementById(discountper).value) / 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 + "&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 + "&discountper=" + document.getElementById(discountper).value;
    url = url + "&discountper1=" + document.getElementById(discountper1).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('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('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;
        } 
        // alert ('OK');
    }
    
    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;
        }
        
        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;
        }
    }
    
  
    var url = "sales_inv_data.php";
    url = url + "?Command=" + "add_tmp";
    url = url + "&Command1=" + cdata;

    url = url + "&firstname_hidden=" + document.getElementById('firstname_hidden').value;
    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 + "&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("msg");
        if (XMLAddress1[0].childNodes[0].nodeValue != "ok") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);
            return ;
        }
        
        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 + "&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("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 != "CASH") && (document.getElementById('pay_type').value != "CREDIT")) {
 
         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('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('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('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('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;
             }

         }
        }

    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 + "&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 + "&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 + "&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 + "&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 + "&online_pay_bank_name=" + document.getElementById('online_pay_bank_name').value;
    url = url + "&online_pay_note=" + document.getElementById('online_pay_note').value;

    url = url + "&gift_card_no=" + document.getElementById('gift_card_no').value;
    url = url + "&dano=" + document.getElementById('dano').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;
        var str = res.replace("Saved", "");
        var n = res.endsWith("Saved");

        if (n == true) {

            document.getElementById('invno').value = str;
            print_inv();
            new_inv();
        } else {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>" + xmlHttp.responseText + "</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("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");
        // 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("sales_table");  
        opener.document.getElementById('itemdetails').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_dagtable");  
        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";

    }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";



    }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";

    }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";

    }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";


    }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";



    }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";


    }

}


function clear_pay_type_change(){

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

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

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

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

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

function customer_modelll() {


    $('#myModal_search').modal('show');
    
     
}

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

 
 
 
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 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.selling;   
            opener.document.getElementById('dag_subtotal').value = obj.selling;  
            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');
}

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_jobno=' + document.getElementById('s_jobno').value;
       params = params + '&s_partno=' + document.getElementById('s_partno').value;
       params = params + '&s_cat=' + document.getElementById('s_cat').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;
       params = params + '&firstname_hidden=' + document.getElementById('firstname_hidden').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_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 custno2(itno,custno)
{

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


    var url = "search_item_data.php";
    url = url + "?Command=" + "pass_itno";
    url = url + "&itno=" + itno;
    url = url + "&custno=" + custno;

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



}

function re_custno2()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        //alert(xmlHttp.responseText);
        
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("stname");
        if (XMLAddress1[0].childNodes[0].nodeValue =="invoice") {
             XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("type");
            if ((XMLAddress1[0].childNodes[0].nodeValue =="TO BE EDIT ITEM") || (XMLAddress1[0].childNodes[0].nodeValue =="TO BE EDIT ITEM RATE MINUS")) { 
                document.getElementById('itemd').disabled = false;
                document.form1.cat1.value = XMLAddress1[0].childNodes[0].nodeValue;
            }else{
                document.form1.cat1.value = XMLAddress1[0].childNodes[0].nodeValue;
            }    
                
                
            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_code");
            document.form1.itemd_hidden.value = XMLAddress1[0].childNodes[0].nodeValue;
            
            
            
            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_description");
            document.form1.itemd.value = XMLAddress1[0].childNodes[0].nodeValue;
            
            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("qtyinhand");
            document.form1.stklevel.value = XMLAddress1[0].childNodes[0].nodeValue;

            XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("stname");
            if (XMLAddress1[0].childNodes[0].nodeValue !="DEFINV") {
                XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("actual_selling");
                document.form1.rate.value = XMLAddress1[0].childNodes[0].nodeValue;
                document.form1.qty.focus();
            } else {
              XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tb");
              document.getElementById('itemdetail').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;       
          }
      }else {
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_code");
        document.form1.itemd_hidden.value = XMLAddress1[0].childNodes[0].nodeValue;
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_description");
        document.form1.itemd.value = XMLAddress1[0].childNodes[0].nodeValue;

    }
    
    document.getElementById('s_refno').value = ""; 
    document.getElementById('s_desc').value = ""; 
    document.getElementById('s_brand').value = ""; 
    document.getElementById('s_jobno').value = ""; 
    document.getElementById('s_partno').value = ""; 
    document.getElementById('s_cat').value = ""; 
    document.getElementById('s_credit').value = ""; 
    document.getElementById('s_qty').value = "";
     
    document.getElementById('serialno').value = ""; 
    document.getElementById('qty').value = "0"; 
    document.getElementById('discountper').value = "0"; 
    document.getElementById('discountper1').value = "0"; 
    document.getElementById('subtotal').value = "";
    
    $('#item_modal_search').modal('hide');
    // self.close();	

}
}


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 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=" + opener.document.form1.brand.value;
    // url = url + "&salesrep=" + opener.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") {
        console.log(xmlHttp.responseXML); // Debugging: Log the full XML response

        function getXMLValue(tagName) {
            var elements = xmlHttp.responseXML.getElementsByTagName(tagName);
            if (elements.length > 0 && elements[0].childNodes.length > 0) {
                return elements[0].childNodes[0].nodeValue.trim();
            }
            return "";
        }

        document.form1.firstname_hidden.value = getXMLValue("id");
        document.form1.firstname.value = getXMLValue("str_customername");
        document.form1.cus_address.value = getXMLValue("str_address");
        document.getElementById('tel').value = getXMLValue("tel");
        document.form1.vat1.value = getXMLValue("str_vatno");
        document.form1.vat2.value = getXMLValue("str_svatno");
        document.form1.c_subcode.value = getXMLValue("c_subcode");
        document.form1.salesrep.value = getXMLValue("salesrep");

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

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

        document.form1.crebal.value = getXMLValue("crebal");
        document.form1.creditlimit.value = getXMLValue("txt_crelimi");
        document.form1.balance.value = getXMLValue("txt_crebal");
        document.form1.over60.value = getXMLValue("over60_txt");
        document.form1.txtinvt.value = getXMLValue("netqty");

        if (getXMLValue("over60_qst") !== "") {
            document.form1.over60.value = "0";
        }

        let salesTable = getXMLValue("sales_table_acc");
        if (salesTable !== "") {
            document.getElementById('storgrid').innerHTML = salesTable;
        }

        let alertMessages = ["AltMessage", "bank_message", "over60_message", "chq_message", "msg_return"];
        alertMessages.forEach(msg => {
            let message = getXMLValue(msg);
            if (message !== "") {
                alert(message);
            }
        });

        let chqMessageQue = getXMLValue("chq_message_que");
        if (chqMessageQue !== "") {
            confirm(chqMessageQue);
        }

        if (getXMLValue("blacklist") === "1") {
            alert("Blocked For Invoicing");
        }

        if (getXMLValue("stname") !== "sales_order") {
            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 = "";

        // Attempt to hide the modal safely
        if ($('#customer_modal_search').length) {
            setTimeout(function() {
                $('#customer_modal_search').modal('hide');
            }, 200);
        }

        // Vanilla JavaScript Fallback
        let modalElement = document.getElementById('customer_modal_search');
        if (modalElement) {
            modalElement.classList.remove('show');
            modalElement.setAttribute('aria-hidden', 'true');
            modalElement.style.display = 'none';

            let modalBackdrop = document.querySelector('.modal-backdrop');
            if (modalBackdrop) {
                modalBackdrop.remove();
            }
        }
    }
}



function sales_inv_search() {
    
    $('#sales_inv_modal_search').modal('show');
    
    search_sales_inv_search(''); 
}

function search_sales_inv_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_sales_inv_search';  
      params = params + '&i_refno=' + document.getElementById('i_refno').value; 
       params = params + '&i_date=' + document.getElementById('i_date').value;
       params = params + '&i_cus=' + document.getElementById('i_cus').value;
       params = params + '&i_po=' + document.getElementById('i_po').value;
       params = params + '&i_jobno=' + document.getElementById('i_jobno').value;
       params = params + '&i_vehicale=' + document.getElementById('i_vehicale').value;
       params = params + '&i_tele=' + document.getElementById('i_tele').value;
       params = params + '&i_value=' + document.getElementById('i_value').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_sales_inv_search;

    xmlHttp.send(params);

}

function re_search_sales_inv_search() {
     
  var XMLAddress1;

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

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


function custno4(custno){
    
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "search_inv_data.php";
    url = url + "?Command=" + "pass_invno";
    url = url + "&invno=" + custno;
    // 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 = re_custno4;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function re_custno4()
{
    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);
        document.form1.invno.value = XMLAddress1[0].childNodes[0].nodeValue; 
        //document.form1.invno.value = "111111111";

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("SDATE");
        document.form1.invdate.value = XMLAddress1[0].childNodes[0].nodeValue;
        
        document.getElementById('remarks').style.visibility = "visible";
        document.getElementById('remarkslbl').style.visibility = "visible";
        
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("REMARK");
        document.form1.remarks.value = XMLAddress1[0].childNodes[0].nodeValue;
        
         XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("labour");
        document.form1.labour.value = XMLAddress1[0].childNodes[0].nodeValue;
        
        
         XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("dele_no");
        document.form1.dano.value = XMLAddress1[0].childNodes[0].nodeValue;
        
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("inquiry");
        document.form1.inquiry.value = XMLAddress1[0].childNodes[0].nodeValue;

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

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_customecode");
        document.form1.firstname_hidden.value = XMLAddress1[0].childNodes[0].nodeValue;
        
         XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("pay_type");
        document.form1.pay_type.value = XMLAddress1[0].childNodes[0].nodeValue;
        
         XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("type2");
        document.form1.type2.value = XMLAddress1[0].childNodes[0].nodeValue;
        
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("TEL");
        document.form1.tel.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("str_address2");
        document.form1.cus_address2.value = XMLAddress1[0].childNodes[0].nodeValue;

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

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


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


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


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

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

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

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

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


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

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

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

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

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

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


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

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

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

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

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

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

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

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

        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("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("sales_table_acc");
        window.document.getElementById('storgrid').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

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

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

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

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

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

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

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

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("cred_per");
        document.form1.credper.value = XMLAddress1[0].childNodes[0].nodeValue;
        
       
        document.getElementById('i_refno').value = "";
        document.getElementById('i_date').value = "";
        document.getElementById('i_cus').value = "";
        document.getElementById('i_po').value = "";
        document.getElementById('i_jobno').value = "";
        document.getElementById('i_vehicale').value = "";
        document.getElementById('i_tele').value = "";
        document.getElementById('i_value').value = "";
$('#sales_inv_modal_search').modal('hide');
        // self.close();

    }
}
