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;
    }
    document.getElementById('refno').value="";
    document.getElementById('cuscode').value=""; 
    document.getElementById('cusname').value="";
    document.getElementById('size').value="";
    document.getElementById('marker').value="";
    document.getElementById('adpayment').value="";
    document.getElementById('serialno').value="";
    document.getElementById('warranty').value="";
    document.getElementById('remark').value="";
    document.getElementById('uniq').value="";
    document.getElementById('jobno').value="";
    document.getElementById('cascost').value="";
    document.getElementById('selling').value="";
    document.getElementById('belt').value="";
    document.getElementById('department').selectedIndex = "0"; 
    document.getElementById('dagtype').value="";
    document.getElementById('msg_box').innerHTML="";
    document.getElementById('department').disabled=false;
    document.getElementById('invno').value="";
    document.getElementById('vatgroup_1').checked = true;
    document.getElementById('subtot').value="";
    document.getElementById('tax').value="";
    document.getElementById('gtot').value="";
    
    document.getElementById('itemdetails').innerHTML="";
    document.getElementById('itemdetails2').innerHTML="";
    var url = "dag_purchase_data.php";
    var params = "Command=" + "getdt";
    params = params + "&ls=" + "new";
    params = params + "&department=" + document.getElementById('department').value; 
    params = params + "&uniq=" + document.getElementById('uniq').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 = assign_dt;
    xmlHttp.send(params);
}



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

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("id");
        var idno = XMLAddress1[0].childNodes[0].nodeValue;
         

        document.getElementById("refno").value = idno;
        
       

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

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

function setjobno() {

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
   
     
    var url = "dag_purchase_data.php";
    var params = "Command=" + "setjobno"; 
    params = params + "&department=" + document.getElementById('department').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_setjobno;
    xmlHttp.send(params);
}


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

         XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("scode"); 
         
            document.getElementById("jobno").disabled=false;
        
        
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("jobno");
        document.getElementById("jobno").value = XMLAddress1[0].childNodes[0].nodeValue;
        
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("refno");
        document.getElementById("refno").value = XMLAddress1[0].childNodes[0].nodeValue;
    }
}

function pass_item_inv(code)
{
    //alert(code);
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "dag_purchase_data.php";
    url = url + "?Command=" + "pass_item_inv";
    url = url + "&custno=" + 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('cascost').value = obj.cascost;   
        opener.document.getElementById('dagtype').value = obj.dagtype;   
        opener.document.getElementById('size').value = obj.size;    
        opener.document.getElementById('marker').value = obj.marker;
        opener.document.getElementById('belt').value = obj.belt;   
        opener.document.getElementById('jobno').value = obj.jobno;   
        opener.document.getElementById('serialno').value = obj.serialno;   
        opener.document.getElementById('warranty').value = obj.warrenty;  

        self.close();
    }

}

function add_tmp()
{

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

    if (document.getElementById('cuscode').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Select Customer</span></div>";
        return false;
    }
    if (document.getElementById('cuscode').value == "C/00021") {
         if (document.getElementById('cascost').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Enter Casing Cost...</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'>Select jobno</span></div>";
        return false;
    }
    
     if (document.getElementById('belt').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Select Belt</span></div>";
        return false;
    }
  
    if (document.getElementById('size').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Select Size</span></div>";
        return false;
    }
     if (document.getElementById('marker').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Select Marker</span></div>";
        return false;
    }
      if (document.getElementById('serialno').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Select Serial No</span></div>";
        return false;
    }
     if (document.getElementById('warranty').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Select Warrenty</span></div>";
        return false;
    }
    
   
    
    var url = 'dag_purchase_data.php';
    var params = 'Command=' + 'add_tmp';
    params = params + "&Command1=add";  
    params = params + '&uniq=' + document.getElementById('uniq').value; 
    params = params + "&refno="+document.getElementById('refno').value; 
    params = params + "&cuscode="+document.getElementById('cuscode').value;
    params = params + "&cusname="+document.getElementById('cusname').value;
    params = params + "&size="+document.getElementById('size').value;
    params = params + "&marker="+document.getElementById('marker').value;
    params = params + "&adpayment="+document.getElementById('adpayment').value;
    params = params + "&serialno="+document.getElementById('serialno').value;
    params = params + "&warranty="+document.getElementById('warranty').value;
    params = params + "&remark="+document.getElementById('remark').value;
    params = params + "&jobno="+document.getElementById('jobno').value;
    params = params + "&sdate="+document.getElementById('sdate').value;
    params = params + "&belt="+document.getElementById('belt').value;
    params = params + "&department="+document.getElementById('department').value;
    params = params + "&cascost="+document.getElementById('cascost').value;
    params = params + "&dagtype="+document.getElementById('dagtype').value;
    params = params + "&selling="+document.getElementById('selling').value;
    
    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "1";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "0";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "2";
    } 

    params = params + "&vatmethod=" + vatmethod;
    
    document.getElementById('msg_box').innerHTML = "";
    
    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 = add;

    xmlHttp.send(params);



}

function add()
{
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
    //  if (xmlHttp.responseText == "ADDED") {
    XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table");
    document.getElementById('itemdetails').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;
    
    document.getElementById('department').disabled=true;
    XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("jobno");
    document.getElementById("jobno").value = XMLAddress1[0].childNodes[0].nodeValue;
    
    XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("gtot");
    document.getElementById("gtot").value = XMLAddress1[0].childNodes[0].nodeValue;
    
    XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tax");
    document.getElementById("tax").value = XMLAddress1[0].childNodes[0].nodeValue;
    
    XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("subtot");
    document.getElementById("subtot").value = XMLAddress1[0].childNodes[0].nodeValue;
    
    document.getElementById('warranty').value = "";
    document.getElementById('remark').value = ""; 
    document.getElementById('serialno').value = ""; 
    document.getElementById('marker').value = ""; 
    document.getElementById('size').value = ""; 
    document.getElementById('belt').value = ""; 
    document.getElementById('dagtype').value = ""; 
    document.getElementById('selling').value = ""; 

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

    //  } 

 }
}


function save_inv()
{

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

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

    var url = 'dag_purchase_data.php';
    var params = 'Command=' + 'save_item'; 
    params = params + '&refno=' + document.getElementById('refno').value; 
    params = params + '&uniq=' + document.getElementById('uniq').value;  
    params = params + '&sdate=' + document.getElementById('sdate').value;  
    params = params + "&cuscode="+document.getElementById('cuscode').value;
    params = params + "&cusname="+document.getElementById('cusname').value;
    params = params + '&adpayment=' + document.getElementById('adpayment').value;   
    params = params + '&cascost=' + document.getElementById('cascost').value;   
    params = params + '&department=' + document.getElementById('department').value;    
    params = params + '&invno=' + document.getElementById('invno').value;  
    params = params + '&invdate=' + document.getElementById('invdate').value; 
    params = params + '&subtot=' + document.getElementById('subtot').value; 
    params = params + '&tax=' + document.getElementById('tax').value; 
    params = params + '&gtot=' + document.getElementById('gtot').value; 
    
    if (document.getElementById('vatgroup_0').checked == true) {
        vatmethod = "1";
    } else if (document.getElementById('vatgroup_1').checked == true) {
        vatmethod = "0";
    } else if (document.getElementById('vatgroup_2').checked == true) {
        vatmethod = "2";
    } 

    params = params + "&vatmethod=" + vatmethod;
    
    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 = save;

    xmlHttp.send(params);



}

function save()
{
    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>";
            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(cdata) {

    var url = "dag_print.php";
    url = url + "?refno=" + document.getElementById('refno').value;
    url = url + "&action=" + cdata;

    window.open(url, '_blank');


}

function del_item(id)
{

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

    var url = 'dag_purchase_data.php';
    var params ="Command="+"add_tmp";  
    params = params + "&Command1=del"; 
    params = params + '&refno=' + document.getElementById('refno').value; 
    params = params + '&uniq=' + document.getElementById('uniq').value; 
    params = params + "&id=" + id; 

    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 = del;

    xmlHttp.send(params);



}


function del() {
    var XMLAddress1;

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

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

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

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

}


function passcusresult_quot()
{
    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('refno').value = obj.refno;  
        opener.document.getElementById('sdate').value = obj.sdate;   
        opener.document.getElementById('cuscode').value = obj.cuscode;   
        opener.document.getElementById('cusname').value = obj.cusname;    
        opener.document.getElementById('department').value = obj.department;
        opener.document.getElementById('invno').value = obj.invno;    
        opener.document.getElementById('subtot').value = obj.SUB_TOT;   
        opener.document.getElementById('tax').value = obj.VAT_VAL;   
        opener.document.getElementById('gtot').value = obj.GRAND_TOT;   
        
        if(obj.VAT =="1"){ 
            opener.document.getElementById('vatgroup_0').checked = true;
        }else if(obj.VAT =="2"){ 
            opener.document.getElementById('vatgroup_2').checked = true;
        }else {
            opener.document.getElementById('vatgroup_1').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("Do you want to Cancel this ! ");
    if (msg == true) {
        
        document.getElementById('msg_box').innerHTML = "";
        if (document.getElementById('refno').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Select Entry</span></div>";
            return false;
        }
    
    
    
        var url = 'dag_purchase_data.php';
        var params = 'Command=' + 'cancel_inv'; 
        params = params + '&refno=' + document.getElementById('refno').value; 
        params = params + '&uniq=' + document.getElementById('uniq').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_cancel;
    
        xmlHttp.send(params);
    }


}

function re_cancel()
{
    var XMLAddress1;

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


        if (xmlHttp.responseText == "Cancel") {
         document.getElementById('msg_box').innerHTML = "<div class='alert alert-success' role='alert'><span class='center-block'>Canceled</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>";

   }

}
}