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 keyset(key, e)
{   

   if(e.keyCode==13){
    document.getElementById(key).focus();
   }
}

function keychange(key)
{   

    document.getElementById(key).focus();
  
}

function got_focus(key)
{   
    document.getElementById(key).style.backgroundColor="#000066";
  
}

function lost_focus(key)
{   
    document.getElementById(key).style.backgroundColor="#000000";
  
}

function new_inv(){

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

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

    document.getElementById('uniq').value = "";
    document.getElementById('invno').value = "";
    document.getElementById('blno').value = "";
    document.getElementById('invno1').value = "";
    document.getElementById('indate').value = "";
    document.getElementById('total_dis').value = "";
    document.getElementById('sub_val').value = "";
    document.getElementById('invdate').value="";
    document.getElementById('department').selectedIndex = "0";
    
    document.getElementById('total_value').value = "";
    document.getElementById('lc_no').value = "";
    document.getElementById('brand').value = "";
    document.getElementById('dte_dor').value = "";
    document.getElementById('supplier_code').value = "";
    document.getElementById('supplier_name').value = "";
    document.getElementById('orderno1').value = "";
    document.getElementById('contsize').value = "";
    document.getElementById('pi_no').value = "";
    document.getElementById('salesrep').selectedIndex = "0";

    document.getElementById('purtype').selectedIndex = "1";
    document.getElementById('arpend').selectedIndex = "0";

    document.getElementById('country').value = "";
    document.getElementById('textarea').value = "";

    document.getElementById('vatgroup_0').checked =true;
    document.getElementById('vatgroup_1').checked =false;
    document.getElementById('vatgroup_2').checked =false;

    document.getElementById('count').value = "";
    document.getElementById('costcenter').value = "";

    document.getElementById('itemdetails').innerHTML = "";
    
    document.getElementById('total_o_value').value = "";
    document.getElementById('total_r_value').value = "";
    document.getElementById('total_vat_value').value = "";

    document.getElementById('itemcode').value = "";
    document.getElementById('itemname').value = "";
    document.getElementById('ord_qty').value = "";
    document.getElementById('qty').value = "";
    document.getElementById('reccost').value = "";
    document.getElementById('dis1').value = "";
    document.getElementById('dis2').value = "";
    document.getElementById('dis3').value = "";
    document.getElementById('cost').value = "";
    document.getElementById('tax').value = "";
    document.getElementById('selling').value = "";
    document.getElementById('cash').value = "";
    document.getElementById('credit').value = "";
    document.getElementById('margin').value = "";
    document.getElementById('subtotal').value = "";
    document.getElementById('pdebitnote').value = "";

    document.getElementById("orderno1").disabled = false;
    document.getElementById("department").disabled = false;
    document.getElementById("brand").disabled = false;
    document.getElementById("salesrep").disabled = false;
    document.getElementById("dte_dor").disabled = false;

    var url="grn_data.php";            
    url=url+"?Command="+"new_inv";
   url=url+"&brand="+document.getElementById('brand').value; 
    
    xmlHttp.onreadystatechange=re_new_inv;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function re_new_inv() {

    document.getElementById('invno').value = xmlHttp.responseText;

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

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

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

function brand_session()
{
	
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null)
			{
				alert ("Browser does not support HTTP Request");
				return;
			} 		
			
			
			var url="grn_data.php";			
			url=url+"?Command="+"brand_session";
			
			url=url+"&brand="+document.getElementById('brand').value; 
						
			
			xmlHttp.onreadystatechange=re_brand_session;
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);	
}


function re_brand_session()
{
	var XMLAddress1;
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
	}
}

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

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

function re_pass_po_inv()
{
    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('orderno1').value = obj.REFNO;
        opener.document.getElementById('dte_dor').value = obj.SDATE; 
        opener.document.getElementById('blno').value = obj.blno; 
        opener.document.getElementById('country').value = obj.COUNTRY;  
        opener.document.getElementById('supplier_name').value = obj.SUP_NAME;
        opener.document.getElementById('supplier_code').value = obj.SUP_CODE; 
        opener.document.getElementById('pi_no').value = obj.pi_no;
        opener.document.getElementById('lc_no').value = obj.LC_No;
        opener.document.getElementById('brand').value = obj.Brand;
        opener.document.getElementById('department').value = obj.DEP_CODE;
        opener.document.getElementById('salesrep').value = obj.REP_CODE;
        opener.document.getElementById('invno1').value = obj.invno;
        opener.document.getElementById('total_o_value').value = obj.TOTQTY;

        if(obj.VAT =="0"){ 
            opener.document.getElementById('vatgroup_0').checked = true;
        }else if(obj.VAT =="1"){ 
            opener.document.getElementById('vatgroup_1').checked = true;
        }else if(obj.VAT =="2"){ 
            opener.document.getElementById('vatgroup_2').checked = true;
        }

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

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


        opener.document.getElementById("orderno1").disabled = true;
        opener.document.getElementById("department").disabled = true;
        opener.document.getElementById("brand").disabled = true;
        opener.document.getElementById("salesrep").disabled = true;
        opener.document.getElementById("dte_dor").disabled = true;

        self.close();
    }
} 

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

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

function re_pass_supp_inv()
{
    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('supplier_code').value = obj.CODE;  
        opener.document.getElementById('supplier_name').value = obj.NAME; 

        self.close();
    }
}


function cal_subtot(count, totcount)
{
    var qty = 'qty' + count;
    var reccost = 'reccost' + count;
    var dis1_name = 'dis1_name' + count;
    var dis2_name = 'dis2_name' + count;
    var dis3_name = 'dis3_name' + count;
    var cost = 'cost' + count;
    var subtotal = 'subtotal' + count;
    var tot = 0;
    var total_dis = 0;
    var totqty = 0;

    if (document.getElementById(qty).value == "") {
        qty_val = 0;
    } else {
        qty_val = parseFloat(document.getElementById(qty).value);
    }

    if (document.getElementById(reccost).value == "") {
        reccost_val = 0;
    } else {
        reccost_val = parseFloat(document.getElementById(reccost).value);
    }
    
    
    var disper = 0;
    var disper1 = 0;
    var disper2 = 0; 
    var disper3 = 0; 

    if ((document.getElementById(dis1_name).value != '') && (document.getElementById(dis1_name).value != "0") && (document.getElementById(dis1_name).value != "0.00")) {
        disper = document.getElementById(dis1_name).value;
        disper1 = document.getElementById(dis1_name).value;
    }
    if ((document.getElementById(dis2_name).value != '') && (document.getElementById(dis2_name).value != "0") && (document.getElementById(dis2_name).value != "0.00")) {
        disper2 = (100 - document.getElementById(dis1_name).value) * (document.getElementById(dis2_name).value) / 100;
        disper = parseFloat(disper1) + parseFloat(disper2);
    }
    
    if ((document.getElementById(dis3_name).value != '') && (document.getElementById(dis3_name).value != "0") && (document.getElementById(dis3_name).value != "0.00")) {
        disper3 = (100 - disper) * (document.getElementById(dis3_name).value) / 100;
        disper = parseFloat(disper1) + parseFloat(disper2) + parseFloat(disper3);
    }
    
    
    reccost_val=reccost_val*(100-disper)/100;
    
    document.getElementById(cost).value = reccost_val;
    if (document.getElementById(cost).value == "") {
        cost_val = 0;
    } else {
        cost_val = parseFloat(document.getElementById(cost).value);
    }

    document.getElementById(subtotal).value = qty_val * cost_val;
    var i = 1;
    while (i < totcount) {
        var subtotalind = 'subtotal' + i;
        var subval = 'reccost' + i;
        var subqty = 'qty' + i;
        if (document.getElementById(subtotalind).value != '') {
            tot = parseFloat(tot) + parseFloat(document.getElementById(subtotalind).value);
        }
        if (document.getElementById(subval).value != '') {
            total_dis = parseFloat(total_dis) + parseFloat(document.getElementById(subval).value)*parseFloat(document.getElementById(subqty).value);
        }
        if (document.getElementById(subqty).value != '') {
            totqty = totqty + parseFloat(document.getElementById(subqty).value);
        }
        i = i + 1;
    }

    document.getElementById("total_value").value = tot;
    document.getElementById("total_dis").value = total_dis-tot;
    document.getElementById("sub_val").value = total_dis;
    document.getElementById("total_r_value").value = totqty;
    
}

function cal_subtot2(){


    if (document.getElementById("qty").value == "") {
      var  qty_val = 0;
    } else {
       var qty_val = parseFloat(document.getElementById("qty").value);
    }

    document.getElementById("ord_qty").value=parseFloat(document.getElementById("qty").value);

    if (document.getElementById("reccost").value == "") {
      var  reccost_val = 0;
    } else {
      var  reccost_val = parseFloat(document.getElementById("reccost").value);
    }
    
    
    var disper = 0;
    var disper1 = 0;
    var disper2 = 0; 
    var disper3 = 0; 

    var totcount = 0; 

    if ((document.getElementById("dis1").value != '') && (document.getElementById("dis1").value != "0") && (document.getElementById("dis1").value != "0.00")) {
        disper = document.getElementById("dis1").value;
        disper1 = document.getElementById("dis1").value;
    }
    if ((document.getElementById("dis2").value != '') && (document.getElementById("dis2").value != "0") && (document.getElementById("dis2").value != "0.00")) {
        disper2 = (100 - document.getElementById("dis1").value) * (document.getElementById("dis2").value) / 100;
        disper = parseFloat(disper1) + parseFloat(disper2);
    }
    if ((document.getElementById("dis3").value != '') && (document.getElementById("dis3").value != "0") && (document.getElementById("dis3").value != "0.00")) {
        disper3 = (100 - disper)   * (document.getElementById("dis3").value) / 100; 
        disper = parseFloat(disper1) + parseFloat(disper2)+ parseFloat(disper3);
    }
    
    
    reccost_val=reccost_val*(100-disper)/100;
    
    document.getElementById("cost").value = reccost_val;
    if (document.getElementById("cost").value == "") {
       var cost_val = 0;
    } else {
      var  cost_val = parseFloat(document.getElementById("cost").value);
    }

    document.getElementById("subtotal").value = qty_val * cost_val;

     
}

function cal_margine2()
{
    //var qty='qty'+count;
    var cost = 0;
    var selling =0 
    var margin = 0;

    //var subtotal='subtotal'+count;
    var tot = 0;

    //margine = (Val(Format(myGridGrn.TextMatrix(i, 7), General)) - Val(Format(myGridGrn.TextMatrix(i, 6), General))) / Val(Format(myGridGrn.TextMatrix(i, 6), General)) * 100

    margine = (parseFloat(document.getElementById("selling").value) - parseFloat(document.getElementById("cost").value)) / parseFloat(document.getElementById("cost").value) * 100;

    document.getElementById("margin").value = margine;
    //alert(totcount);

}

function cal_margine(count, totcount)
{
    //var qty='qty'+count;
    var cost = 'cost' + count;
    var selling = "selling" + count;
    var margin = "margin" + count;

    //var subtotal='subtotal'+count;
    var tot = 0;

    //margine = (Val(Format(myGridGrn.TextMatrix(i, 7), General)) - Val(Format(myGridGrn.TextMatrix(i, 6), General))) / Val(Format(myGridGrn.TextMatrix(i, 6), General)) * 100

    margine = (parseFloat(document.getElementById(selling).value) - parseFloat(document.getElementById(cost).value)) / parseFloat(document.getElementById(cost).value) * 100;

    document.getElementById(margin).value = margine;
    //alert(totcount);

}

function save_inv()
{

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

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

    if (document.getElementById('uniq').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('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('invdate').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Date</span></div>";
        return false;
    }

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

    if (document.getElementById("supplier_code").value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Supplier</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("brand").value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Brand</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 Rep</span></div>";
        return false;
    }

    var url = "grn_data.php";
    var params = "Command=" + "save_inv";

    params = params + "&invno=" + document.getElementById('invno').value;
    params = params + '&uniq=' + document.getElementById('uniq').value; 
    params = params + "&invdate=" + document.getElementById('invdate').value;
    params = params + "&lc_no=" + document.getElementById('lc_no').value;
    params = params + "&pi_no=" + document.getElementById('pi_no').value;
    params = params + "&brand=" + document.getElementById('brand').value;
    params = params + "&department=" + document.getElementById('department').value;
    params = params + "&dte_dor=" + document.getElementById('invdate').value;
    params = params + "&supplier_code=" + document.getElementById('supplier_code').value;
    params = params + "&supplier_name=" + document.getElementById('supplier_name').value;
    params = params + "&textarea=" + document.getElementById('textarea').value;
    params = params + "&salesrep=" + document.getElementById('salesrep').value;
    params = params + "&orderno1=" + document.getElementById('orderno1').value;

    params = params + "&costcenter=" + document.getElementById('costcenter').value;
    params = params + "&total_value=" + document.getElementById('total_value').value;
    params = params + "&country=" + document.getElementById('country').value;
    params = params + "&purtype=" + document.getElementById('purtype').value;
    params = params + "&count=" + document.getElementById('count').value;
    params = params + "&contsize=" + document.getElementById('contsize').value;
    params = params + "&arpend=" + document.getElementById('arpend').value;
    params = params + "&invno1=" + document.getElementById('invno1').value;
    params = params + "&blno=" + document.getElementById('blno').value;
    params = params + "&indate=" + document.getElementById('indate').value;
    params = params + "&total_dis=" + document.getElementById('total_dis').value;
    params = params + "&sub_val=" + document.getElementById('sub_val').value;

    params = params + "&total_o_value=" + document.getElementById('total_o_value').value;
    params = params + "&total_r_value=" + document.getElementById('total_r_value').value;
    params = params + "&total_vat_value=" + document.getElementById('total_vat_value').value;
    
    
    params = params + "&pdebitnote=" + document.getElementById('pdebitnote').value;

    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "0";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "1";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "2";
    }  
    params = params + "&vatmethod=" + vatmethod;

    var i = 1;
    while (i < document.getElementById('count').value) {
        itemcode = "itemcode" + i;
        itemname = "itemname" + i;
        ord_qty = "ord_qty" + i;
        qty = "qty" + i;
        reccost = "reccost" + i;
        dis1_name = "dis1_name" + i;
        dis2_name = "dis2_name" + i;
        dis3_name = "dis3_name" + i;
        cost = "cost" + i;
        tax = "tax" + i;
        selling = "selling" + i;
        cash = "cash" + i;
        credit = "credit" + i;
        margin = "margin" + i;
        subtotal = "subtotal" + i;

        params = params + "&" + itemcode + "=" + document.getElementById(itemcode).value;
        params = params + "&" + itemname + "=" + document.getElementById(itemname).value;
        params = params + "&" + ord_qty + "=" + document.getElementById(ord_qty).value;
        params = params + "&" + qty + "=" + document.getElementById(qty).value;
        params = params + "&" + reccost + "=" + document.getElementById(reccost).value;
        params = params + "&" + dis1_name + "=" + document.getElementById(dis1_name).value;
        params = params + "&" + dis2_name + "=" + document.getElementById(dis2_name).value;
        params = params + "&" + dis3_name + "=" + document.getElementById(dis3_name).value;
        params = params + "&" + cost + "=" + document.getElementById(cost).value;
        params = params + "&" + tax + "=" + document.getElementById(tax).value;
        params = params + "&" + selling + "=" + document.getElementById(selling).value;
        params = params + "&" + cash + "=" + document.getElementById(cash).value;
        params = params + "&" + credit + "=" + document.getElementById(credit).value;
        params = params + "&" + margin + "=" + document.getElementById(margin).value;
        params = params + "&" + subtotal + "=" + document.getElementById(subtotal).value;
        i = i + 1;
    }

    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_save_inv;
    xmlHttp.send(params);
}

function re_save_inv()
{
    var XMLAddress1;
    
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){

     if (xmlHttp.responseText == "Saved") {
         document.getElementById('msg_box').innerHTML = "<div class='alert alert-success' role='alert'><span class='center-block'>Saved</span></div>";
         print_inv();
         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 pass_inv(code){
  
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "grn_data.php";
    url = url + "?Command=" + "pass_inv";
    url = url + "&code=" + code;

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

function re_pass_inv()
{
    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('invno').value = obj.REFNO;  
        opener.document.getElementById('uniq').value = obj.tmp_no;
        opener.document.getElementById('invdate').value = obj.SDATE; 
        opener.document.getElementById('orderno1').value = obj.ORDNO; 
        opener.document.getElementById('lc_no').value = obj.LCNO;  
        opener.document.getElementById('supplier_name').value = obj.SUP_NAME;
        opener.document.getElementById('supplier_code').value = obj.SUP_CODE; 
        opener.document.getElementById('pi_no').value = obj.pi_no;
        opener.document.getElementById('country').value = obj.COUNTRY;
        opener.document.getElementById('textarea').value = obj.REMARK;
        opener.document.getElementById('department').value = obj.DEPARTMENT;
        opener.document.getElementById('total_value').value = obj.AMOUNT;
        opener.document.getElementById('dte_dor').value = obj.PUR_DATE;
        opener.document.getElementById('purtype').value = obj.TYPE;
        opener.document.getElementById('brand').value = obj.brand;
        opener.document.getElementById('contsize').value = obj.book_no;
        opener.document.getElementById('invno1').value = obj.invno;
        opener.document.getElementById('blno').value = obj.blno; 
        opener.document.getElementById('indate').value = obj.indate;
        opener.document.getElementById('total_dis').value = obj.total_dis;
        opener.document.getElementById('sub_val').value = obj.sub_value;
        opener.document.getElementById('total_vat_value').value = obj.VAT_AMOUNT;
        opener.document.getElementById('total_r_value').value = obj.TOT_RQTY;
        opener.document.getElementById('total_o_value').value = obj.TOT_OQTY;
        opener.document.getElementById('salesrep').value = obj.SAL_EX; 
        opener.document.getElementById('pdebitnote').value = obj.DEBITNOTE_BAL; 
 
        if (obj.CANCEL == "1") {
            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 = "";
        }
 
        if(obj.VAT =="0"){ 
            opener.document.getElementById('vatgroup_0').checked = true;
        }else if(obj.VAT =="1"){ 
            opener.document.getElementById('vatgroup_1').checked = true;
        }else if(obj.VAT =="2"){ 
            opener.document.getElementById('vatgroup_2').checked = true;
        }

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

        self.close();
    }
}


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

    var msg = confirm("Are you sure to CANCEL ! ");
    if (msg == true) {

        if (document.getElementById("invno").value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Entry</span></div>";
          return false;
        }
        
        var url="grn_data.php";            
        url=url+"?Command="+"cancel_inv";
    
        url = url + "&invno=" + document.getElementById('invno').value;
        url = url + "&orderno1=" + document.getElementById('orderno1').value;
        url = url + "&department=" + document.getElementById('department').value;
        url = url + "&invdate=" + document.getElementById('invdate').value; 
                        
        xmlHttp.onreadystatechange=re_cancel_inv;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    }
}

function re_cancel_inv(){

    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>";
    
            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 print_inv(){
 
    var url = "grn_print.php";    
    url = url + "?refno=" + document.getElementById('invno').value;
    window.open(url);
}

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

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

}

function re_pass_item_inv()
{
    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('itemcode').value = obj.STK_NO;  
        opener.document.getElementById('itemname').value = obj.DESCRIPT;
        opener.document.getElementById('reccost').value = obj.COST;
        opener.document.getElementById('selling').value = obj.SELLING;
        opener.document.getElementById('cash').value = obj.CA_SELLING;
        opener.document.getElementById('credit').value = obj.CR_SELLING;
        // opener.document.getElementById('partno').value = obj.PART_NO;

         opener.document.getElementById('ord_qty').value = "";  
         opener.document.getElementById('qty').value = ""; 
        // opener.document.getElementById('rate').value = ""; 
         opener.document.getElementById('subtotal').value = ""; 

        self.close();
    }
}

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

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

    if (document.getElementById('uniq').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('supplier_code').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Supplier</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 Qty</span></div>";
        return false;
    }
    if (document.getElementById("reccost").value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Cost</span></div>";
        return false;
    }
    if (document.getElementById("selling").value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter List Price</span></div>";
        return false;
    }


    
    var url="grn_data.php";            
    url=url+"?Command="+"add_tmp";      
    url=url+"&Command1="+"add";
    url=url+"&invno="+document.getElementById('invno').value;
    url=url+"&uniq="+document.getElementById('uniq').value;
    url=url+"&itemcode="+document.getElementById('itemcode').value;
    url=url+"&itemname="+document.getElementById('itemname').value;
    url=url+"&ord_qty="+document.getElementById('ord_qty').value;
    url=url+"&qty="+document.getElementById('qty').value;
    url=url+"&reccost="+document.getElementById('reccost').value;
    url=url+"&dis1="+document.getElementById('dis1').value;
    url=url+"&dis2="+document.getElementById('dis2').value;
    url=url+"&dis3="+document.getElementById('dis3').value;
    url=url+"&cost="+document.getElementById('cost').value;
    url=url+"&tax="+document.getElementById('tax').value;
    url=url+"&selling="+document.getElementById('selling').value;
    url=url+"&cash="+document.getElementById('cash').value;
    url=url+"&credit="+document.getElementById('credit').value;
    url=url+"&margin="+document.getElementById('margin').value;
    url=url+"&subtotal="+document.getElementById('subtotal').value;

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

}

function re_add_tmp()
{
    var XMLAddress1;
    
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
         
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table");  
        document.getElementById('itemdetails').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("msg");  
        if (XMLAddress1[0].childNodes[0].nodeValue !="") {
            alert(XMLAddress1[0].childNodes[0].nodeValue);   
        }
    
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("totqty1");   
        document.getElementById('total_o_value').value = XMLAddress1[0].childNodes[0].nodeValue;        

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

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

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

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

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

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("vatvalue"); 
        document.getElementById('total_vat_value').value = XMLAddress1[0].childNodes[0].nodeValue;
        
        document.getElementById('itemcode').value="";
        document.getElementById('itemname').value="";
        document.getElementById('ord_qty').value="";
        document.getElementById('qty').value="";
        document.getElementById('reccost').value="";
        document.getElementById('dis1').value="";
        document.getElementById('dis2').value="";
        document.getElementById('cost').value="";
        document.getElementById('tax').value="";
        document.getElementById('selling').value="";
        document.getElementById('subtotal').value="";
        document.getElementById('brand').disabled=false;
        
        document.getElementById('itemcode').focus();
    }
}

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

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

    if (document.getElementById('supplier_code').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select Supplier</span></div>";
        return false;
    }    
    
    var url="grn_data.php";            
    url=url+"?Command="+"add_tmp";          
    url=url+"&Command1="+"del";  
    url = url + "&id=" + id; 
    url=url+"&uniq="+document.getElementById('uniq').value;

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

}