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

    var url="employee_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;

    }
}

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

    var url = "employee_data.php";
    url = url + "?Command=" + "save_inv";
   
    url = url + "&uniq=" + document.getElementById('uniq').value; 
    url = url + "&refno=" + document.getElementById('refno').value; 
    url = url + "&name=" + document.getElementById('name').value;
    url = url + "&add=" + document.getElementById('add'). value;
    url = url + "&tel=" + document.getElementById('tel'). value;
    url = url + "&company=" + document.getElementById('company'). 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 = "employee_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('name').value = obj.NAME; 
        opener.document.getElementById('add').value = obj.ADDRESS; 
        opener.document.getElementById('tel').value = obj.TEL;  
        opener.document.getElementById('company').value = obj.COMPANY;

        if (obj.CANCELL == "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 update_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('name').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Please Enter Name</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'>Please Select Company</span></div>";
        return false;
    }


	var url="employee_data.php";			
	url=url+"?Command="+"update_inv";
    url = url + "&uniq=" + document.getElementById('uniq').value; 
    url = url + "&refno=" + document.getElementById('refno').value; 
    url = url + "&name=" + document.getElementById('name').value;
    url = url + "&add=" + document.getElementById('add'). value;
    url = url + "&tel=" + document.getElementById('tel'). value;
    url = url + "&company=" + document.getElementById('company'). value;
	
	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);", 500);

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

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="employee_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>";
        }
    }
} 