<link rel="stylesheet" href="css/themes/redmond/jquery-ui-1.10.3.custom.css" />

<!-- Main content -->
<section class="content">

    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title">Cost Update</h3>
        </div>
        <form role="form" name ="form1" class="form-horizontal">
            <div class="box-body">

                <div class="form-group">
                    <a onclick="location.reload();" class="btn btn-default">
                        <span class="fa fa-user-plus"></span> &nbsp; New
                    </a>
                    <a onclick="save_inv();" class="btn btn-primary">
                        <span class="fa fa-save"></span> &nbsp; Save
                    </a>
                    <a onclick="process();" class="btn btn-default">
                        <span class="fa fa-print"></span> &nbsp; Process
                    </a>	
                </div>
                <div id="msg_box"  class="span12 text-center"  ></div>

                <input type="hidden" id="tmpno" class="form-control">
                <input type="hidden" id="item_count" class="form-control">


                <!--=======================-->
                <div class="form-group">
                    <label class="col-md-1 control-label  " for="txt_usernm" style="text-align:left">Date From</label>
                    <div class="col-md-3">
                        <input   id="dtfrom" name="dtfrom" value="<?php echo date('Y-m-d'); ?>" class="form-control input-sm" type="date" onclick='process();' onchange='process();'  > 
                    </div> 
                    <label class="col-md-1 control-label  " for="txt_usernm" style="text-align:left">Date To</label>
                    <div class="col-md-3">
                        <input  id="dtto"  name="dtto"  value="<?php echo date('Y-m-d'); ?>" class="form-control input-sm" type="date" onclick='process();' onchange='process();'  > 
                    </div> 
                </div>   
                
                <div class="form-group">
                    <label class="col-md-1 control-label  " for="txt_usernm" style="text-align:left">Department</label>
                    <div class="col-md-3">
                        <select id="company" name="company"  class="form-control input-sm"  onclick='process();' onchange='process();'  > 
                            <!--<option value="">ALL</option>-->
                            <?php   
                             $sql = "select * from s_stomas where act='1'";
                             foreach ($conn->query($sql) as $row) {
                                echo "<option value='" . $row["CODE"] . "' class=\"opt\">" . $row["DESCRIPTION"] . "</option>";
                            }
                            ?>  
                       </select>
                    </div> 
                    <label class="col-md-1 control-label  " for="txt_usernm" style="text-align:left">Brand</label>
                    <div class="col-md-3">
                        <select id="brand" name="brand"  class="form-control input-sm" onclick='process();' onchange='process();'  > 
                            <option value="All">All</option>
                            <?php   
                             $sql = "select * from brand_mas where act ='1' order by barnd_name";
                             foreach ($conn->query($sql) as $row) {
                                echo "<option value='" . $row["barnd_name"] . "' class=\"opt\">" . $row["barnd_name"] . "</option>";
                            }
                            ?> 
                       </select>
                    </div>
                </div>   
                


                <div class="col-sm-12">


                    <div id="itemdetails" class="col-sm-12" >

                    </div>
                </div>


            </div>

    </div>

    <div  class='space' >
        <br>&nbsp;
        <br>&nbsp;
        <br>&nbsp;

    </div>

</form>
</div>

</section>
  

<script>

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 process()
{   
	
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null)
			{
				alert ("Browser does not support HTTP Request");
				return;
			} 
			
			document.getElementById('msg_box').innerHTML = "";
			
			
				var url="costup_data.php";	
				url=url+"?Command="+"process";
				url=url+"&dtfrom="+document.getElementById('dtfrom').value;
				url=url+"&dtto="+document.getElementById('dtto').value;
				url=url+"&company="+document.getElementById('company').value;
				url=url+"&brand="+document.getElementById('brand').value;
				//alert(url);
				xmlHttp.onreadystatechange=re_process;
				xmlHttp.open("GET",url,true);
				xmlHttp.send(null);
				
			
			
			
			
			
}

function re_process()
{
	var XMLAddress1;
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		 		
	  
 
		
		
		XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("sales_table");
	    document.getElementById('itemdetails').innerHTML = XMLAddress1[0].childNodes[0].nodeValue;
		
	 XMLAddress1 = xmlHttp.responseXML.getElementsByTagName("itm_count");
	    document.getElementById('item_count').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 = "";

    var url = "costup_data.php";
    var params = "Command=" + "save_inv";
  
	var count = document.getElementById('item_count').value;
	params = params + "&count=" + count; 
    var i = 1;
	while (count > i) {
	  
        
        var id = "id" + i;
        var invno = "invno" + i;
        var date = "date" + i;
        var stk_no = "stk_no" + i;
        var des = "des" + i;
        var cost = "cost" + i;
		 
				
		params = params + '&' + id + '=' + document.getElementById(id).value;
		params = params + '&' + invno + '=' + document.getElementById(invno).value;
        params = params + '&' + date + '=' + document.getElementById(date).value;
        params = params + '&' + stk_no + '=' + document.getElementById(stk_no).value;
        params = params + '&' + des + '=' + document.getElementById(des).value;
        params = params + '&' + cost + '=' + document.getElementById(cost).value;
		
		i = i +1;	
    }
    
    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 = salessaveresult;

    xmlHttp.send(params);
}

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


</script>

 
  