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 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('refno').value="";  
document.getElementById('sdate').value="";  
document.getElementById('name').value="";  
document.getElementById('company').value="";  

    var url = "labour_data.php";
    url = url + "?Command=" + "new_inv"; 
 
    document.getElementById('msg_box').innerHTML = "";

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


function resu_filter() {
    var XMLAddress1;

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

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

    }
}

function save_inv() {
    
        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null)
        {
            alert("Browser does not support HTTP Request");
            return;
        }
        if (document.getElementById('refno').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Click New.</span></div>";
            return false;
        }
        if (document.getElementById('name').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Name Is Empty...</span></div>";
            return false;
        }
        if (document.getElementById('company').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Company Is Empty...</span></div>";
            return false;
        }

        var url = "labour_data.php";
        url = url + "?Command=" + "save_inv";
        url = url + "&sdate=" + document.getElementById('sdate').value;
        url = url + "&refno=" + document.getElementById('refno').value;
        url = url + "&name=" + document.getElementById('name').value;
        url = url + "&company=" + document.getElementById('company').value;

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

        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);", 1200);

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

    }
}
 
 
 
 function update_inv() {
    var msg = confirm("Do you want to Update.. ! ");
    if (msg == true) {
        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null)
        {
            alert("Browser does not support HTTP Request");
            return;
        }
       if (document.getElementById('refno').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Click New.</span></div>";
            return false;
        }
        if (document.getElementById('name').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Name Is Empty...</span></div>";
            return false;
        }
        if (document.getElementById('company').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Company Is Empty...</span></div>";
            return false;
        }

        var url = "labour_data.php";
        url = url + "?Command=" + "update_inv";
        url = url + "&sdate=" + document.getElementById('sdate').value;
        url = url + "&refno=" + document.getElementById('refno').value;
        url = url + "&name=" + document.getElementById('name').value;
        url = url + "&company=" + document.getElementById('company').value;

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

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


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);", 1200);

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

    }
}
 
 
 function cancel_inv() {
    var msg = confirm("Do you want to Cancel.. ! ");
    if (msg == true) {
        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null)
        {
            alert("Browser does not support HTTP Request");
            return;
        }
       if (document.getElementById('refno').value == "") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Find Labour.</span></div>";
            return false;
        }
        

        var url = "labour_data.php";
        url = url + "?Command=" + "cancel_inv"; 
        url = url + "&refno=" + document.getElementById('refno').value; 

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

        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'>Canceled.</span></div>";

            setTimeout("location.reload(true);", 1200);

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

    }
}


   function custno(code)
{
  
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    var url = "labour_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.CODE;  
        opener.document.getElementById('name').value = obj.DESCRIPTION;     
        opener.document.getElementById('company').value = obj.SCODE; 
         
         
        if (obj.cancel != "0") {
            window.opener.document.getElementById('msg_box').innerHTML = "<div class='alert alert-danger' role='alert'><span class='center-block'>Cancelled</span></div>";
        }else{
            window.opener.document.getElementById('msg_box').innerHTML = "";
        }

        self.close();
    }

}