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('msg_box').innerHTML = "";

    document.getElementById('uniq').value = "";
    document.getElementById('refno').value = "";
    document.getElementById('sdate').value = "";
    document.getElementById('type').value = "";
    document.getElementById('department').selectedIndex = "0";
    document.getElementById('employee').selectedIndex = "0";
    document.getElementById('amount').value = "";
    document.getElementById('remark').value = "";

    var url="expenses_data.php";            
    url=url+"?Command="+"new_inv";

    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('refno').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('sdate').value = XMLAddress1[0].childNodes[0].nodeValue;
  
    }
}

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('refno').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('sdate').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('type').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Select  Type</span></div>";
        return false;
    }

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

    var url = "expenses_data.php";
    url = url + "?Command=" + "save_inv";
   
    url = url + "&uniq=" + document.getElementById('uniq').value; 
    url = url + "&refno=" + document.getElementById('refno').value; 
    url = url + "&sdate=" + document.getElementById('sdate').value;
    url = url + "&type=" + document.getElementById('type'). value;
    url = url + "&department=" + document.getElementById('department'). value;
    url = url + "&employee=" + document.getElementById('employee'). value;
    url = url + "&amount=" + document.getElementById('amount').value;
    url = url + "&remark=" + document.getElementById('remark').value;

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


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>";
         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 = "expenses_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('uniq').value = obj.TMP_NO;  
        opener.document.getElementById('refno').value = obj.REFNO;
        opener.document.getElementById('sdate').value = obj.SDATE; 
        opener.document.getElementById('type').value = obj.TYPE; 
        opener.document.getElementById('department').value = obj.DEPARTMENT;  
        opener.document.getElementById('employee').value = obj.EMPLOYEE;
        opener.document.getElementById('amount').value = obj.AMOUNT; 
        opener.document.getElementById('remark').value = obj.REMARK; 
 
        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 = "";
        }

        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("refno").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="expenses_data.php";            
        url=url+"?Command="+"cancel_inv";
    
        url = url + "&refno=" + document.getElementById('refno').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-danger' 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(cdata) {

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

    window.open(url, '_blank');


}