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 print_inv(cdata) {

    var url = "invoice_print.php";
    url = url + "?tmp_no=" + document.getElementById('tmpno').value;
    url = url + "&invno=" + document.getElementById('txt_entno').value;
    url = url + "&action=" + cdata;

    window.open(url, '_blank');


}


function dev_print_inv_ben()
	{ 

// 		if (document.getElementById('cmd_print').value==1){
			var itCount = document.getElementById("item_count").value;
			url = "";
			itCount = parseInt(itCount);
//                        alert(itCount);
if(itCount < 17){
	url="rep_inv_ben.php";
}else if (itCount < 32){
	url="rep_inv_ben_lmt30.php";
}else if (itCount < 47){
	url="rep_inv_ben_lmt45_dev.php";
}else if (itCount < 62){
	url="rep_inv_ben_lmt60.php";
}

url=url+"?invno="+document.getElementById('txt_entno').value;

myString=document.getElementById('c_name').value;
myString = myString.replace(/&/g,"~");
url=url+"&cus_name="+myString;

myString=document.getElementById('cus_address').value;
myString = myString.replace(/&/g,"~");
url=url+"&cus_address="+myString;

myString=document.getElementById('cus_address').value;
myString = myString.replace(/&/g,"~");
url=url+"&cus_address2="+myString;

// url=url+"&vat1="+document.getElementById('vat1').value;
// url=url+"&vat2="+document.getElementById('vat2').value;


// url=url+"&nonvat="+document.getElementById('vatgroup_1').checked;
// url=url+"&vat="+document.getElementById('vatgroup_0').checked;
// url=url+"&svat="+document.getElementById('vatgroup_2').checked;

// url=url+"&tax="+document.getElementById('tax').value;
// url=url+"&credper="+document.getElementById('credper').value;

// url=url+"&isHideAdt="+document.getElementById('isHideAdt').checked;

			//alert(url);
			window.open(url);
// 		} else {
// 			alert("Cannot Print");	
// 		}

	}
function add_tmp() {


    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    
    if (document.getElementById('c_name').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Customer Not Selected</span></div>";
        return false;
    }
    
    	var str = document.getElementById("itemCode").value;
		var res = str.substring(0, 1); 
    if ((parseFloat(document.getElementById("qty").value)>parseFloat(document.getElementById("stklevel").value)) && (res.toUpperCase()!="Z")){
		document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Insuffisant Stock In Your Department</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'>Qty Not Entered</span></div>";
        return false;
    }
    if (document.getElementById('dis').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Dis Not Entered</span></div>";
        return false;
    }
    if (document.getElementById('itemPrice').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Qty Not Entered</span></div>";
        return false;
    }
     if (document.getElementById('itemsubtot').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Calculate Error</span></div>";
        return false;
    } 
    
    if ((document.getElementById('tmpno').value != "")) {

        var url = "invoice_new_data.php";
        url = url + "?Command=" + "setitem";
        url = url + "&Command1=" + "add_tmp";
        url = url + "&invno=" + document.getElementById('txt_entno').value;
        url = url + "&itemCode=" + document.getElementById('itemCode').value;
        url = url + "&itemDesc=" + document.getElementById('itemDesc').value;
        url = url + "&itemPrice=" + document.getElementById('itemPrice').value;
        url = url + "&qty=" + document.getElementById('qty').value;
        url = url + "&dis=" + document.getElementById('dis').value;
        url = url + "&subtot=" + document.getElementById('itemsubtot').value;
        url = url + "&tmpno=" + document.getElementById('tmpno').value;
        url = url + "&department=" + document.getElementById('department').value;
        var vattype;
        if (document.getElementById('non').checked == true) {
            vattype = "non";
        }

        if (document.getElementById('svat').checked == true) {
            vattype = "svat";
        }
        url = url + "&vat=" + vattype;
        xmlHttp.onreadystatechange = showarmyresultdel;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
        
        document.getElementById('msg_box').innerHTML = "";
    }
}

function showarmyresultdel() {
    var XMLAddress1;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        
        // alert(xmlHttp.responseText);
       
        

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table");
        document.getElementById('itemdetails').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;
        
         XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("stock");
        if (XMLAddress1[0].childNodes[0].nodeValue == "Insuffisant Stock In Your Department") {
            document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Insuffisant Stock In Your Department</span></div>";
        }

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

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

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

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

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


        document.getElementById('itemCode').value = "Z";
        document.getElementById('itemDesc').value = "";
        document.getElementById('qty').value = "";
        document.getElementById('itemsubtot').value = "";
        document.getElementById('dis').value = "0";
        document.getElementById('itemPrice').value = "";
        document.getElementById('stklevel').value="0";
        
        document.getElementById('paymethod_0').disabled=true;
		document.getElementById('paymethod_1').disabled=true;

        document.getElementById('itemDesc').focus();
    }
}


function calbal() {


    var disper = 0;
    var disper1 = 0; 
    var subtot = 0;

    if ((document.getElementById('dis').value != '') && (document.getElementById('dis').value != "0") && (document.getElementById('dis').value != "0.00")) {

        disper = document.getElementById('dis').value;
        disper1 = document.getElementById('dis').value;

    }

    document.getElementById('dis').value = disper;

    subtot = parseFloat(document.getElementById('itemPrice').value) - (parseFloat(document.getElementById('itemPrice').value) * parseFloat(document.getElementById('dis').value) / 100);
    subtot = parseFloat(subtot) * parseFloat(document.getElementById('qty').value);


    document.getElementById('itemsubtot').value = subtot;

    // add_tmp('up');
}


function save_inv() {

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

    if (document.getElementById('tmpno').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>New Not Selected</span></div>";
        return false;
    }
    if (document.getElementById('c_name').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Customer Not Selected</span></div>";
        return false;
    }
    if (document.getElementById('subtot').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Amount Not Enterd</span></div>";
        return false;
    }



    var url = "invoice_new_data.php";
    url = url + "?Command=" + "save_item";

    url = url + "&txt_entno=" + document.getElementById('txt_entno').value;
    url = url + "&tmpno=" + document.getElementById('tmpno').value;
    url = url + "&customercode=" + document.getElementById('c_code').value;
    url = url + "&customername=" + document.getElementById('c_name').value;
    url = url + "&cont_p=" + document.getElementById('cus_address').value;
    url = url + "&txt_remarks=" + document.getElementById('txt_remarks').value;

    url = url + "&subtot=" + document.getElementById('subtot').value;
     url = url + "&gtot=" + document.getElementById('gtot').value;
    url = url + "&invdate=" + document.getElementById('invdate').value;

    url = url + "&currency=" + document.getElementById('currency').value;
    url = url + "&txt_rate=" + document.getElementById('txt_rate').value;

    url = url + "&salesrep=" + document.getElementById('salesrep').value;
    url = url + "&department=" + document.getElementById('department').value;
    url = url + "&DANO=" + document.getElementById('DANO').value;




    var vattype;
    if (document.getElementById('non').checked == true) {
        vattype = "non";
    }

    if (document.getElementById('svat').checked == true) {
        vattype = "svat";
    }
    url = url + "&vat=" + vattype;
    
    var paymethod;

    if (document.getElementById('paymethod_0').checked==true){
            paymethod="CR";
    } else if (document.getElementById('paymethod_1').checked==true){
            paymethod="CA";
    }
    url=url+"&paymethod="+paymethod;
    
    xmlHttp.onreadystatechange = salessaveresult;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}

function salessaveresult() {
    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('save');
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 new_inv() {

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

    var paymethod;
    
    document.getElementById('paymethod_0').disabled=false;
	document.getElementById('paymethod_1').disabled=false;
	 document.getElementById('dis').disabled=true;

    document.getElementById('txt_entno').value = "";
    document.getElementById('c_code').value = "";
    document.getElementById('c_name').value = "";
    document.getElementById('cus_address').value = "";
    document.getElementById('non').checked = true;
    document.getElementById('itemdetails').innerHTML = "";
    document.getElementById('subtot').value = "";
    document.getElementById('msg_box').innerHTML = "";
    document.getElementById('itemCode').value = "Z";
    document.getElementById('itemDesc').value = "";
    document.getElementById('itemPrice').value = "";
    document.getElementById('qty').value = "";
    document.getElementById('dis').value = "0";
    document.getElementById('itemsubtot').value = "";
    document.getElementById('gtot').value = "";
    document.getElementById('txt_remarks').value = "";
    document.getElementById('nbt').value = "";
    document.getElementById('DANO').value = "";
    document.getElementById('stklevel').value = "0";
    document.getElementById('paymethod_0').checked=true;

    document.getElementById('svattot').value = "";
    var url = "invoice_new_data.php";
    url = url + "?Command=" + "new_inv";

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

}

function assign_invno() {



    var XMLAddress1;

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

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

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

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



    }

}




function del_item(cdate) {

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    
    	    if(document.getElementById("paymethod_0").checked == true){
			    document.getElementById('dis').disabled=true; 
			}else{
                document.getElementById('dis').disabled=false; 
			}

    var url = "invoice_new_data.php";
    url = url + "?Command=" + "setitem";
    url = url + "&Command1=" + "del_item";
    url = url + "&itemCode=" + cdate;
    url = url + "&invno=" + document.getElementById('txt_entno').value;
    url = url + "&tmpno=" + document.getElementById('tmpno').value;
    var vattype;
    if (document.getElementById('non').checked == true) {
        vattype = "non";
    }

    if (document.getElementById('svat').checked == true) {
        vattype = "svat";
    }
    url = url + "&vat=" + vattype;

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

}




function update_list(stname) {

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

    var url = "invoice_new_data.php";
    url = url + "?Command=" + "update_list";
    url = url + "&refno=" + document.getElementById('cusno').value;
    url = url + "&refno1=" + document.getElementById('cusno1').value;
    url = url + "&cusname=" + document.getElementById('customername').value;
    url = url + "&stname=" + stname;

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

}


function showitemresult()
{
    var XMLAddress1;
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        document.getElementById('filt_table').innerHTML = xmlHttp.responseText;
    }
}



function crnview(custno, stname)
{
    try {


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

        var url = "invoice_new_data.php";
        url = url + "?Command=" + "pass_rec";
        url = url + "&refno=" + custno;
        url = url + "&stname=" + stname;

        xmlHttp.onreadystatechange = pass_rec_result;

        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    } catch (err) {
        alert(err.message);
    }
}

function pass_rec_result()
{
    var XMLAddress1;

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

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("tmp_no");
        opener.document.form1.tmpno.value = XMLAddress1[0].childNodes[0].nodeValue;
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("C_REFNO");

        opener.document.form1.txt_entno.value = XMLAddress1[0].childNodes[0].nodeValue;
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("C_CODE");

        opener.document.form1.c_code.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("name");
        opener.document.form1.c_name.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("C_DATE");
        opener.document.form1.invdate.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("txt_remarks");
        opener.document.form1.txt_remarks.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("Attn");
        opener.document.form1.cus_address.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("nbt");
        opener.document.form1.nbt.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("currency");
        opener.document.form1.currency.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("salesrep");
        opener.document.form1.salesrep.value = XMLAddress1[0].childNodes[0].nodeValue;
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("department");
        opener.document.form1.department.value = XMLAddress1[0].childNodes[0].nodeValue;
        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("DANO");
        opener.document.form1.DANO.value = XMLAddress1[0].childNodes[0].nodeValue;



        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("txt_rate");
        opener.document.form1.txt_rate.value = XMLAddress1[0].childNodes[0].nodeValue;

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

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


        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("item_count");
        opener.document.form1.item_count.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("subtot");
        opener.document.form1.subtot.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("gtot");
        opener.document.form1.gtot.value = XMLAddress1[0].childNodes[0].nodeValue;

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("vattot");
        opener.document.form1.svattot.value = XMLAddress1[0].childNodes[0].nodeValue;
        if (XMLAddress1[0].childNodes[0].nodeValue > 0) {
            window.opener.document.getElementById('svat').checked = true;
        } else {
            window.opener.document.getElementById('non').checked = true;
        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("str_crecash");	

        if(XMLAddress1[0].childNodes[0].nodeValue=='CR')
        {
                opener.document.form1.paymethod_0.checked=true;

        } else if(XMLAddress1[0].childNodes[0].nodeValue=='CA')
        {
                opener.document.form1.paymethod_1.checked=true;

        }

        XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("msg");
        if (XMLAddress1[0].childNodes[0].nodeValue != "") {
            window.opener.document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>" + XMLAddress1[0].childNodes[0].nodeValue + "</span></div>";
        }
        self.close();
    }
}


function cancel_inv() {
    $('#myModal_c').modal('hide');

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
 var msg = confirm("Are you sure to Cancel ! ");
    if (msg == true) {
    document.getElementById('msg_box').innerHTML = "";
    if (document.getElementById('c_name').value == "") {
        document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>Select Entry</span></div>";
        return false;
    }
    if (document.getElementById('subtot').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 = "invoice_new_data.php";
    url = url + "?Command=" + "del_inv";
    url = url + "&crnno=" + document.getElementById('txt_entno').value;
    url = url + "&tmpno=" + document.getElementById('tmpno').value;
    xmlHttp.onreadystatechange = cancel_result;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    }
}

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


        if (xmlHttp.responseText == "ok") {
            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 {
            if (xmlHttp.responseText != "") {
                document.getElementById('msg_box').innerHTML = "<div class='alert alert-warning' role='alert'><span class='center-block'>" + xmlHttp.responseText + "</span></div>";
            }
        }
    }
}