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('invdate').value="";
	document.getElementById('supplier_code').value="";
	document.getElementById('supplier_code').value="";
	document.getElementById('supplier_name').value="";
	document.getElementById('supplier_name').value="";
	document.getElementById('invno1').value="";
	document.getElementById('totqty').value="";
	document.getElementById('blno').value="";
	document.getElementById('country').value="";
	document.getElementById('brand').value="";
	document.getElementById('brand').disabled=false;
	document.getElementById('pi_no').value="";

	document.getElementById('salesrep').selectedIndex = "0";  
	document.getElementById('department').selectedIndex = "0";
	document.getElementById('brand').selectedIndex = "0";
	
	document.getElementById('remarks').value="";
	document.getElementById('dte_shedule').value="";
	document.getElementById('lc_no').value="";
	document.getElementById('qty').value="";
	document.getElementById('rate').value="";

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

	document.getElementById('itemd_hidden').value="";
	document.getElementById('itemd').value="";
	document.getElementById('partno').value="";
	document.getElementById('qty').value="";
	document.getElementById('rate').value="";
	document.getElementById('subtotal').value="";
	document.getElementById('ardate').value="";

	document.getElementById('tot').value="";
	document.getElementById('vatvalue').value="";

	document.getElementById('itemdetails').innerHTML="";
	document.getElementById('sales_provi').innerHTML="";
	document.getElementById('payhistory').innerHTML="";
	document.getElementById('edit_log').innerHTML="";

	var url="import_po_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(){
	
	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("sdate");
     document.getElementById("invdate").value = XMLAddress1[0].childNodes[0].nodeValue;

   }
}


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("rate").value == "") {
   		document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Rate</span></div>";
      	return false;
   	}
	
	var url="import_po_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('itemd_hidden').value;
	url=url+"&item="+document.getElementById('itemd').value;
	url=url+"&partno="+document.getElementById('partno').value;
	url=url+"&qty="+document.getElementById('qty').value;
	url=url+"&rate="+document.getElementById('rate').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("totqty");	
    	document.getElementById('totqty').value = XMLAddress1[0].childNodes[0].nodeValue;

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

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

		document.getElementById('itemd_hidden').value="";
		document.getElementById('itemd').value="";
		document.getElementById('partno').value="";
		document.getElementById('qty').value="";
		document.getElementById('rate').value="";
		document.getElementById('subtotal').value="";
// 		document.getElementById('brand').disabled=true;
		
		document.getElementById('itemd_hidden').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="import_po_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);

}

function cal_subtotal() {
  
    var subtot = 0;    
    subtot = parseFloat(document.getElementById('rate').value) * parseFloat(document.getElementById('qty').value);
    document.getElementById('subtotal').value = subtot; 
}


function save_inv(inv_status)
{   
	
	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 Enter Ref No</span></div>";
		return false;
	}

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


   // if (document.getElementById("tot").value >0) {
   // 	 document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Item .Your Total Is Minus or Zero</span></div>";
   //    return false;
   // }

	var url = 'import_po_data.php';
    var params = 'Command=' + 'save_inv'; 
    params = params + '&invno=' + document.getElementById('invno').value; 
    params = params + '&uniq=' + document.getElementById('uniq').value;  
    params = params + '&supplier_code=' + document.getElementById('supplier_code').value;  
    params = params + "&supplier_name="+document.getElementById('supplier_name').value;
    params = params + "&country="+document.getElementById('country').value;
    params = params + "&remarks="+document.getElementById('remarks').value;
    params = params + '&dte_shedule=' + document.getElementById('dte_shedule').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 + '&department=' + document.getElementById('department').value;   
    params = params + '&brand=' + document.getElementById('brand').value;   
    params = params + '&invno1=' + document.getElementById('invno1').value;   
    params = params + '&blno=' + document.getElementById('blno').value;   
    params = params + '&salesrep=' + document.getElementById('salesrep').value;  
    params = params + '&vatvalue=' + document.getElementById('vatvalue').value;  
    params = params + '&totqty=' + document.getElementById('totqty').value;  
    params = params + '&tot=' + document.getElementById('tot').value;  
    params = params + '&ardate=' + document.getElementById('ardate').value;  
    
    params = params + '&count=' + document.getElementById('count').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 ii = 1;
    while (ii < document.getElementById('count').value) {
        itemcode = "itemcode" + ii;
        itemname = "itemname" + ii;
        partno = "partno" + ii;
        qty = "qty" + ii;
        rate = "rate" + ii;
        subtotal = "subtotal" + ii; 

        params = params + "&" + itemcode + "=" + document.getElementById(itemcode).value;
        params = params + "&" + itemname + "=" + document.getElementById(itemname).value;
        params = params + "&" + partno + "=" + document.getElementById(partno).value;
        params = params + "&" + qty + "=" + document.getElementById(qty).value;
        params = params + "&" + rate + "=" + document.getElementById(rate).value;
        params = params + "&" + subtotal + "=" + document.getElementById(subtotal).value; 
            
        ii = ii + 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 update_inv(inv_status){   
	
	var msg = confirm("Do you want to UPDATE this import_po ! ");
  	if (msg == true) {
		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 Enter Ref No</span></div>";
		return false;
	}

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


   // if (document.getElementById("tot").value >0) {
   // 	 document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Item .Your Total Is Minus or Zero</span></div>";
   //    return false;
   // }

	var url = 'import_po_data.php';
    var params = 'Command=' + 'update_inv'; 
    params = params + '&invno=' + document.getElementById('invno').value; 
    params = params + '&uniq=' + document.getElementById('uniq').value;  
    params = params + '&supplier_code=' + document.getElementById('supplier_code').value;  
    params = params + "&supplier_name="+document.getElementById('supplier_name').value;
    params = params + "&country="+document.getElementById('country').value;
    params = params + "&remarks="+document.getElementById('remarks').value;
    params = params + '&dte_shedule=' + document.getElementById('dte_shedule').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 + '&department=' + document.getElementById('department').value;   
    params = params + '&brand=' + document.getElementById('brand').value;   
    params = params + '&invno1=' + document.getElementById('invno1').value;   
    params = params + '&blno=' + document.getElementById('blno').value;   
    params = params + '&salesrep=' + document.getElementById('salesrep').value;  
    params = params + '&vatvalue=' + document.getElementById('vatvalue').value;  
    params = params + '&totqty=' + document.getElementById('totqty').value;  
    params = params + '&tot=' + document.getElementById('tot').value;  
    params = params + '&ardate=' + document.getElementById('ardate').value;  
    
    params = params + '&count=' + document.getElementById('count').value;  
    params = params + '&countpro=' + document.getElementById('countpro').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 ii = 1;
    while (ii < document.getElementById('count').value) {
        itemcode = "itemcode" + ii;
        itemname = "itemname" + ii;
        partno = "partno" + ii;
        qty = "qty" + ii;
        rate = "rate" + ii;
        subtotal = "subtotal" + ii; 

        params = params + "&" + itemcode + "=" + document.getElementById(itemcode).value;
        params = params + "&" + itemname + "=" + document.getElementById(itemname).value;
        params = params + "&" + partno + "=" + document.getElementById(partno).value;
        params = params + "&" + qty + "=" + document.getElementById(qty).value;
        params = params + "&" + rate + "=" + document.getElementById(rate).value;
        params = params + "&" + subtotal + "=" + document.getElementById(subtotal).value; 
            
        ii = ii + 1;
    }
    
    var i = 1;
    while (i < document.getElementById('countpro').value) {
        name = "name" + i;
        amount = "amount" + i; 

        params = params + "&" + name + "=" + document.getElementById(name).value;
        params = params + "&" + amount + "=" + document.getElementById(amount).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_update_inv;
    xmlHttp.send(params);

	}
}

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

        if (xmlHttp.responseText == "Updated") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-success' role='alert'><span class='center-block'>Updated</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 pass_supp_inv(code){
  
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "import_po_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 pass_item_inv(code){
  
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "import_po_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('itemd_hidden').value = obj.STK_NO;  
        opener.document.getElementById('itemd').value = obj.DESCRIPT;
        opener.document.getElementById('partno').value = obj.PART_NO;

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

        self.close();
    }
}

function pass_inv(code){
  
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "import_po_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('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('invno1').value = obj.invno;
		opener.document.getElementById('lc_no').value = obj.LC_No;
		opener.document.getElementById('brand').value = obj.Brand;
// 		opener.document.getElementById('brand').disabled=true;
		opener.document.getElementById('department').value = obj.DEP_CODE;
		opener.document.getElementById('salesrep').value = obj.REP_CODE;
		opener.document.getElementById('remarks').value = obj.REMARK;
		opener.document.getElementById('dte_shedule').value = obj.S_date;

		opener.document.getElementById('vatvalue').value = obj.VAT_VALUE;
		opener.document.getElementById('totqty').value = obj.TOTQTY;
		opener.document.getElementById('tot').value = obj.AMOUNT;
		opener.document.getElementById('ardate').value = obj.S_date;
 
        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;
		
		XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_provi");	
		opener.document.getElementById('sales_provi').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;
		
		
		XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("payhistory");	
		opener.document.getElementById('payhistory').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;
		
		XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("countpro");	
		opener.document.getElementById('countpro').value = XMLAddress1[0].childNodes[0].nodeValue;
        
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("count");	
		opener.document.getElementById('count').value = XMLAddress1[0].childNodes[0].nodeValue;


		XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("log");	
		opener.document.getElementById('edit_log').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="import_po_data.php";			
		url=url+"?Command="+"cancel_inv";
	
		url=url+"&invno="+document.getElementById('invno').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 set_brand()
{
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 		
	
	var url="import_po_data.php";			
	url=url+"?Command="+"set_brand";
	
	url=url+"&brand="+document.getElementById('brand').value;
	url=url+"&department="+document.getElementById('department').value;
				
	xmlHttp.onreadystatechange=re_set_brand;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

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

	}
}
  
function print_inv(){
 
    var url = "../rep_pur_ord.php";    
    url = url + "?invno=" + document.getElementById('invno').value;
    window.open(url);
} 

// Set JavaScript variable for VAT status from PHP
let vatStatus = "<?php echo $vatStatus; ?>";

// Select the correct radio button based on VAT status
window.onload = function() {
	if (vatStatus === 'non') {
		document.getElementById("vatgroup_0").checked = true;
	} else if (vatStatus === 'vat') {
		document.getElementById("vatgroup_1").checked = true;
	} else if (vatStatus === 'svat') {
		document.getElementById("vatgroup_2").checked = true;
	}
};

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 cal_subtot(count)
{
    
    var totcount= document.getElementById('count').value;
    var qty = 'qty' + count;
    var rate = 'rate' + 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(rate).value == "") {
        reccost_val = 0;
    } else {
        reccost_val = parseFloat(document.getElementById(rate).value);
    }
     
    
    document.getElementById(subtotal).value = reccost_val*qty_val; 
    
    var i = 1;
    while (i < totcount) {
        var subtotalind = 'subtotal' + i;
        var subval = 'rate' + i;
        var subqty = 'qty' + i;
        
        
        if (document.getElementById(subtotalind).value != '') {
            tot = parseFloat(tot) + parseFloat(document.getElementById(subtotalind).value);
        }
        
        if (document.getElementById(subtotalind).value >0) {
            if (document.getElementById(subqty).value != '') {
                totqty = totqty + parseFloat(document.getElementById(subqty).value);
            }
        }
        i = i + 1;
    }

    document.getElementById("tot").value = tot; 
    document.getElementById("totqty").value = totqty;
    
}