<?
session_start();
include("test.php");
include("opendb.php");
include("includes/function.php");
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}


?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>PSN Telcon</title>
    <script type="text/javascript" src="scripts/jquery-latest.js"></script>
	<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form1.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>

<script type="text/javascript" src="js/jquery.js"></script>


<style>


#mask {
	display: none;
	background:url(images/bgimg1.gif) repeat;
	//background: #000; 
	position: fixed; left: 0; top: 0; 
	z-index: 10;
	width: 100%; height: 100%;
	opacity: 0.8;
	z-index: 999;
}

.login-popup{
		display:none;
	background:white;
	padding: 10px; 	
	border: 2px solid #ddd;
	float: left;
	font-size: 1.2em;
	position: fixed;
	top: 4%; left: 45%;
	z-index: 99999;
	box-shadow: 0px 0px 20px #999;
	-moz-box-shadow: 0px 0px 20px #999; /* Firefox */
    -webkit-box-shadow: 0px 0px 20px #999; /* Safari, Chrome */
	border-radius:3px 3px 3px 3px;
    -moz-border-radius: 3px; /* Firefox */
    -webkit-border-radius: 3px; /* Safari, Chrome */
}
}

img.btn_close {
	float: right; 
	margin: -28px -28px 0 0;
}

fieldset { 
	border:none; 
}

form.signin .textbox label { 
	display:block; 
	padding-bottom:7px; 
}

form.signin .textbox span { 
	display:block;
}

form.signin p, form.signin span { 
	color:white; 
	font-size:11px; 
	line-height:18px;
} 

form.signin .textbox input { 
	background:#666666; 
	border-bottom:1px solid #333;
	border-left:1px solid #000;
	border-right:1px solid #333;
	border-top:1px solid #000;
	color:white; 
	border-radius: 3px 3px 3px 3px;
	-moz-border-radius: 3px;
    -webkit-border-radius: 3px;
	font:13px Arial, Helvetica, sans-serif;
	padding:6px 6px 4px;
	width:200px;
}

form.signin input:-moz-placeholder { color:white; text-shadow:0 0 2px #000; }
form.signin input::-webkit-input-placeholder { color:hwite; text-shadow:0 0 2px #000;  }

.submit button { 
	background: -moz-linear-gradient(center top, #f3f3f3, #dddddd);
	background: -webkit-gradient(linear, left top, left bottom, from(#f3f3f3), to(#dddddd));
	background:  -o-linear-gradient(top, #f3f3f3, #dddddd);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#f3f3f3', EndColorStr='#dddddd');
	border-color:#000; 
	border-width:1px;
	border-radius:4px 4px 4px 4px;
	-moz-border-radius: 4px;
    -webkit-border-radius: 4px;
	color:#333;
	cursor:pointer;
	display:inline-block;
	padding:6px 6px 4px;
	margin-top:10px;
	font:12px; 
	width:100px;
}

.button:hover { background:#ddd; }


.del{
      border: none;
     background: url("images/delete.png") no-repeat scroll 0 0 transparent;
}


/*  code below for search button* /


/* reset webkit search input browser style */
input[type=search] {
	outline: none;
}
input[type=search] {
	-webkit-appearance: textfield;
	-webkit-box-sizing: content-box;
	font-family: inherit;
	font-size: 100%;
}
input[type=search]::-webkit-search-decoration,
input[type=search]::-webkit-search-cancel-button {
	display: none; /* remove the search and cancel icon */
}

/* search input field */
input[type=search] {
	background: #ededed url(images/search-icon.png) no-repeat 9px center;
	border: solid 1px #ccc;
	padding: 9px 10px 9px 32px;
	width: 45px;
	
	-webkit-border-radius: 10em;
	-moz-border-radius: 10em;
	border-radius: 10em;
	
	-webkit-transition: all .5s;
	-moz-transition: all .5s;
	transition: all .5s;
}
input[type=search]:focus {
	width: 130px;
	background-color: #fff;
	border-color: #6dcff6;
	
	-webkit-box-shadow: 0 0 5px rgba(109,207,246,.5);
	-moz-box-shadow: 0 0 5px rgba(109,207,246,.5);
	box-shadow: 0 0 5px rgba(109,207,246,.5);
}

/* placeholder */
input[type=search]:-moz-placeholder {
	color: #999;
}
input[type=search]::-webkit-input-placeholder {
	color: #999;
}



.sel{
     padding:9px;
    margin: 0;
    border-radius:4px;
    -webkit-box-shadow: 
        0 0px 0 #ccc,
        0 0px #fff inset;
   
    color:black;
    border:none;
    display: inline-block;
   // -webkit-appearance:none; 
    cursor:pointer;
    border: 1px solid #ccc;
     background: -moz-linear-gradient(top, #FBFBFB 0%, #E9E9E9 100%);
    background:  -webkit-gradient(linear, left top, left bottom, color-stop(0%,#FBFBFB), color-stop(100%,#E9E9E9));
    width:183px;
    }

.sub_btn{
    padding:9px;
    margin: 0;
    border-radius:4px;
    -webkit-box-shadow: 
        0 0px 0 #ccc,
        0 0px #fff inset;
  
    color:black;
    border:none;
    outline:none;
    display: inline-block;
    -webkit-appearance:none; 
    cursor:pointer;
    border: 1px solid #ccc;

    }
</style>



    <script type="text/javascript">
       

$(document).ready(function() {
	$('a.login-window').click(function() {
		
		// Getting the variable's value from a link 
		var loginBox = $(this).attr('href');

		//Fade in the Popup and add close button
		$(loginBox).fadeIn(300);
		
		//Set the center alignment padding + border
		var popMargTop = ($(loginBox).height() + 24) / 2; 
		var popMargLeft = ($(loginBox).width() + 24) / 2; 
		
		$(loginBox).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		// Add the mask to body
		$('body').append('<div id="mask"></div>');
		$('#mask').fadeIn(300);
		
		return false;
	});
	
	// When clicking on the button close or the mask layer the popup closed
	$('a.close, #mask').live('click', function() { 
	  $('#mask , .login-popup').fadeOut(300 , function() {
		$('#mask').remove();  
	}); 
	return false;
	});
});


 $(document).ready(function(){
  $("#svbttn").click(function(){

$("#msgbox").html('<small>Loading...</small>');
      

    	// var name= $(this).attr('name');

    	 //$('#type_name').val(name);
   
  	 var values1 = $("#jforms").serialize();
 	// $(".typeselecter").attr("disabled", true);
 	 // $("#td1").html('');

  	  $.ajax({
   	   url: "porder2.php",
    	  type: "POST",
   	  data:   values1,
     	  success: function(msg){

 

           	 if(parseInt(msg)!=0)    

          	  {

                $('#msgbox').html(msg);
            
               // $("#load").html(''); 
                //$(".typeselecter").attr("disabled", false);  
		}
           

      	},
      error:function(){
          alert("failure");
          $("#msgbox").html('there is error while submit');
      }   
   	 }); 

	 }); 
	 }); 




$(document).bind('keydown', 'Alt+n',function (e){
      
    if(e.which==78 && e.altKey){
	 //alert (e.which);
        shortkey_tblopen();
        return false;
    } 
     if(e.which==83 && e.altKey){
	 
         $( "#svbttn" ).click();
        return false;
    } 
   if(e.which==82 && e.altKey){
	 
         window.location.reload(true);
        return false;
    } 
      
    
});



  function shortkey_tblopen(){
               
               var rowCounts = document.getElementById('myTable').rows.length;
		 if(rowCounts<=1){
		          
			document.getElementById('insrowbttn').click(); 
               }		   					
 		 else
			document.getElementById('insrowbttn2').click(); 
 }




/***********************************************
* Disable "Enter" key in Form script
* This notice must stay intact for use
***********************************************/
    

function handleEnter (field, event) {
               
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 


                if (keyCode == 38){
                    var row=field.parentNode.parentNode.rowIndex;
			 var col  =field.parentNode.cellIndex ;
			 
                     document.getElementById('myTable').rows[row-1].cells[col].getElementsByTagName("input")[0].focus();
			
			 
              }

                  if (keyCode == 40){
                    var row=field.parentNode.parentNode.rowIndex;
			 var col  =field.parentNode.cellIndex ;
			 
                     document.getElementById('myTable').rows[row+1].cells[col].getElementsByTagName("input")[0].focus();
			
			 
              }
                     if (keyCode == 37){


                    /* var row=field.parentNode.parentNode.rowIndex;
			 var col  =field.parentNode.cellIndex ;
			 
                     document.getElementById('myTable').rows[row].cells[col-1].getElementsByTagName("input")[0].focus();*/

                   var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i - 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
			
			 
              }
               
                   	 if (keyCode == 46){
			  var i=field.parentNode.parentNode.rowIndex;
			  
                       var r=confirm("Do you want to delete the row?");
			  if (r==true)
 				 {
 					//document.getElementById('myTable').deleteRow(i);
					delrow(field);
                                  document.getElementById('myTable').rows[i].cells[1].getElementsByTagName("input")[0].focus();
                                  
					//var nxt =document.getElementById('myTable').rows[i].cells[0].innerHTML;
					//alert(nxt);
  				 }
			  
        }
                   
		else
		return true;
	}      





function handleEnter2 (field, event) {
               
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 


                if (keyCode == 38){
                    var row=field.parentNode.parentNode.rowIndex;
			 var col  =field.parentNode.cellIndex ;
			 
                     document.getElementById('myTable2').rows[row-1].cells[col].getElementsByTagName("input")[0].focus();
			
			 
              }

                  if (keyCode == 40){
                    var row=field.parentNode.parentNode.rowIndex;
			 var col  =field.parentNode.cellIndex ;
			 
                     document.getElementById('myTable2').rows[row+1].cells[col].getElementsByTagName("input")[0].focus();
			
			 
              }
                     if (keyCode == 37){


                    /* var row=field.parentNode.parentNode.rowIndex;
			 var col  =field.parentNode.cellIndex ;
			 
                     document.getElementById('myTable2').rows[row].cells[col-1].getElementsByTagName("input")[0].focus();*/

                   var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i - 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
			
			 
              }
                     	  if (keyCode == 46){
			  var i=field.parentNode.parentNode.rowIndex;
			  
                       var r=confirm("Do you want to delete the row?");
			  if (r==true)
 				 {
 					//document.getElementById('myTable2').deleteRow(i);
					delrow2(field);
                                  document.getElementById('myTable2').rows[i].cells[1].getElementsByTagName("input")[0].focus();
                                  
					//var nxt =document.getElementById('myTable2').rows[i].cells[0].innerHTML;
					//alert(nxt);
  				 }
			   
                       }
                   
		else
		return true;
	}    




/**** functions below to calculate total amount  ***/


function unitcal(val,r)
{
var i=r.parentNode.parentNode.rowIndex;


   var tbl = document.getElementById('myTable');
   var id1=tbl.rows[i].cells[0].innerHTML;
var fldid=document.getElementById('rate'+id1).value;
  if(isNaN(fldid))
  {
   alert('enter digit');
  document.getElementById('rate'+id1).value=0;
 
  val=0;
  }


    var qtyval=document.getElementById('qty'+id1).value;
    var disc=document.getElementById('disc'+id1).value;
   var totval=(qtyval)*(fldid);

      if(disc> totval)
        {
        document.getElementById('disc'+id1).value='0';
        
         }
      else
        {
            document.getElementById('gtot'+id1).value=totval-disc;


            var k=0;
           var ups = document.getElementsByName('gtot[]'), sum = 0, k;
            for(k = ups.length; k--;)
            if(ups[k].value)
            sum += parseFloat(ups[k].value, 10);
			document.getElementById('sum_total').value=sum.toFixed(2);






   var p=0;
   var ups1 = document.getElementsByName('gtot[]'), sum1 = 0, p;
    var t1 = document.getElementsByName('typ[]');
   
    for(p = ups1.length; p--;)
        if(ups1[p].value)
     
            sum1 += parseFloat(ups1[p].value, 10);
			document.getElementById('spare_total').value=sum1.toFixed(2);

           
                    document.getElementById('sum_total').value=parseFloat(document.getElementById('spare_total').value)+parseFloat(document.getElementById('labour_total').value);
        
         }


}



function discountcal(val,r)
  {
   

 var i=r.parentNode.parentNode.rowIndex;


   var tbl = document.getElementById('myTable');
   var id1=tbl.rows[i].cells[0].innerHTML;

  var fldid=document.getElementById('disc'+id1).value;
 if(isNaN(fldid))
  {
   alert('enter digit');
  document.getElementById('disc'+id1).value=0;
  val=0;
  }
 



    var qtyval=document.getElementById('qty'+id1).value;
    var uprice=document.getElementById('rate'+id1).value;
   var totval=(qtyval)*(uprice);

      if(val> totval)
        {
        document.getElementById('disc'+id1).value='0';
        
         }
      else
        {
            document.getElementById('gtot'+id1).value=totval-val;








   var p=0;
   var ups1 = document.getElementsByName('gtot[]'), sum1 = 0, p;
    var t1 = document.getElementsByName('typ[]');
   
    for(p = ups1.length; p--;)
        if(ups1[p].value)
     
            sum1 += parseFloat(ups1[p].value, 10);
			document.getElementById('spare_total').value=sum1.toFixed(2);

                  document.getElementById('sum_total').value=(parseFloat(document.getElementById('spare_total').value)+parseFloat(document.getElementById('labour_total').value)).toFixed(2);
        
         }
 }
      
  

function othercal(val,r)
  {
   

 var i=r.parentNode.parentNode.rowIndex;


   var tbl = document.getElementById('myTable');
   var id1=tbl.rows[i].cells[0].innerHTML;

  var fldid=document.getElementById('hnd'+id1).value;
 if(isNaN(fldid))
  {
   alert('enter digit');
  document.getElementById('hnd'+id1).value=0;
  val=0;
  }
  
var qtyval=document.getElementById('qty'+id1).value;
    var uprice=document.getElementById('rate'+id1).value;
   var totval=(qtyval)*(uprice);

          document.getElementById('gtot'+id1).value=parseFloat(document.getElementById('gtot'+id1).value)+parseFloat(val);
            

            var k=0;
           var ups = document.getElementsByName('gtot[]'), sum = 0, k;
            for(k = ups.length; k--;)
            if(ups[k].value)
            sum += parseFloat(ups[k].value, 10);
			document.getElementById('sum_total').value=sum.toFixed(2);

            



            var p=0;
   var ups1 = document.getElementsByName('gtot[]'), sum1 = 0, p;
    var t1 = document.getElementsByName('typ[]');
   
    for(p = ups1.length; p--;)
        if(ups1[p].value)
     
            sum1 += parseFloat(ups1[p].value, 10);
			document.getElementById('spare_total').value=sum1.toFixed(2);

           

        document.getElementById('sum_total').value=parseFloat(document.getElementById('spare_total').value)+parseFloat(document.getElementById('labour_total').value);
         


}




function servtotal()
 {
  var rowCount = document.getElementById('myTable2').rows.length;
   var tbl = document.getElementById('myTable2');
    

    var q;
  var id1;
  var labr; 
var sum=0;
  var sum3=0;
  
  var stot;

   for(q=1;q<rowCount;q++)
    {
     id1=tbl.rows[q].cells[0].innerHTML;
    
    
       stot = document.getElementById("servamt"+id1).value;
       sum3+= parseFloat(stot);
     
      
    }
   document.getElementById('labour_total').value=sum3.toFixed(2);
    

  var k=0;
   var ups = document.getElementsByName('servamt[]'), sum = 0, k;
    for(k = ups.length; k--;)
        if(ups[k].value)
            sum += parseFloat(ups[k].value, 10);
			document.getElementById('sum_total').value=parseFloat(document.getElementById('spare_total').value)+parseFloat(document.getElementById('labour_total').value);
 }
   


 </script>



<script>
var cur=1;
var cnt=1;
var partcode=[];
var partname=[];
var spareengineer=[];
var qty=[];
var hnd=[];
var rate=[];
var disc=[];
var gtot=[];
var exqty=[];
var day=[];
var month=[];
var year=[];



function delrow(r)
{


var i=r.parentNode.parentNode.rowIndex;
 document.getElementById('myTable').deleteRow(i);
cnt--;
  var rowCount = document.getElementById('myTable').rows.length;
   var tbl = document.getElementById('myTable');
 if(rowCount<=1)
 {
  document.getElementById('insrowbttn').type='button';
 }
   
  var q;
  var id1;
  var part; 
  var sum2=0;
   var sum3=0;
  var stot;
  var ltot;
   for(q=1;q<rowCount;q++)
    {
     id1=tbl.rows[q].cells[0].innerHTML;
    
    
       ptot = document.getElementById("gtot"+id1).value;
       sum2+= parseFloat(ptot);
     
      
    }
   document.getElementById('spare_total').value=sum2.toFixed(2);
    
var k=0;
   var ups = document.getElementsByName('gtot[]'), sum = 0, k;
    for(k = ups.length; k--;)
        if(ups[k].value)
            sum += parseFloat(ups[k].value, 10);
			document.getElementById('sum_total').value=sum.toFixed(2);


 //document.getElementById('sum_total').value=parseFloat(document.getElementById('spare_total').value)+parseFloat(document.getElementById('labour_total').value);

     
 }



function insRow(event,i1,id2)
{

var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

var cur_val=i1.value;

switch(keyCode)
{
case 38:
   var row=i1.parentNode.parentNode.rowIndex;
   var col  =i1.parentNode.cellIndex ;
			 
   document.getElementById('myTable').rows[row-1].cells[col].getElementsByTagName("input")[0].focus();
  break;
case 40:
  var row=field.parentNode.parentNode.rowIndex;
  var col  =field.parentNode.cellIndex ;
			 
   document.getElementById('myTable').rows[row+1].cells[col].getElementsByTagName("input")[0].focus();
			
  break;
case 37:
  var row=field.parentNode.parentNode.rowIndex;
  var col  =field.parentNode.cellIndex ;
			 
  document.getElementById('myTable').rows[row].cells[col-1].getElementsByTagName("input")[0].focus();
	
}

if(id2!="insrowbttn")
  {
   var i2=i1.parentNode.parentNode.rowIndex;
   var i3=parseFloat(i2)+parseFloat(1);
   var rowCounts = document.getElementById('myTable').rows.length;
   var rowCounts1= rowCounts-1;
   
  
  }
else
  {
   var i2=0;
   var rowCounts1=0;
  }


		if(((keyCode == 13) && (i2==rowCounts1))||(cnt==1))
 {

var rowCount = document.getElementById('myTable').rows.length;
var rowcount1=parseFloat(rowCount)+parseFloat(1);

var x=document.getElementById('myTable').insertRow(rowCount);


if(rowCount>=1)
 {
  document.getElementById('insrowbttn').type='hidden';
 }

var a=x.insertCell(0)
var y=x.insertCell(1)
var z=x.insertCell(2)	
var eng=x.insertCell(3);

var ag=x.insertCell(4)
var z1=x.insertCell(5)	
var dis=x.insertCell(6)
var hnd=x.insertCell(7);
var tot=x.insertCell(8);
var qqty=x.insertCell(9);
var dd1=x.insertCell(10);
var mm1=x.insertCell(11);
var yy1=x.insertCell(12);

var dd=x.insertCell(13);

a.innerHTML=cur;
y.innerHTML="<input type=text  name=partcode[] id=partcode"+cur+"  class='partcode' size=20 onKeydown='return handleEnter(this, event)'>";
z.innerHTML="<input type=text name=partname[] id=partname"+cur+" size=20 class='partname' onKeydown='return handleEnter(this, event)'>"
ag.innerHTML="<input type=text name='qty[]' class='xx' id=qty"+cur+" size=4 onChange=calcamt(this) onKeydown='return handleEnter(this, event)' onKeyup=validnum(this.id)>";
z1.innerHTML="<input type=text  id='rate"+cur+"' name=rate[] size=6 class='rate' onchange='unitcal(this.value,this)' onKeydown='return handleEnter(this, event)' >"
dis.innerHTML="<input type=text name='disc[]' id=disc"+cur+" size=4 onchange='discountcal(this.value,this)' onKeydown='return handleEnter(this, event)' onKeyup=validnum(this.id)>";
hnd.innerHTML="<input type=text value='' name='hnd[]' id=hnd"+cur+" size=3 onchange='othercal(this.value,this)' onKeydown='return handleEnter(this, event)'>";
tot.innerHTML="<input type=text readonly value='' name='gtot[]' id=gtot"+cur+" size=8 onKeydown='return handleEnter(this, event)'>";
qqty.innerHTML="<input type=text readonly name='exqty[]' id=exqty"+cur+" size=4 class='supqty' onKeydown='return handleEnter(this, event)' >";
var xv="";
xv="<select name='spareengineer[]' id=spareengineer"+cur+" onKeydown='return handleEnter(this, event)'><option>Orignial</option><option>Commercial</option><option>Used</option>\n";


eng.innerHTML=xv+"</select> ";
dd1.innerHTML="<input type=text value='<?print date('d')?>' name='day[]' id=day"+cur+" size=2 onKeydown='return handleEnter(this, event)'>";
mm1.innerHTML="<input type=text value='<?print date('m')?>' name='month[]' id=month"+cur+" size=2 onKeydown='return handleEnter(this, event)'>";
yy1.innerHTML="<input type=text value='<?print date('Y')?>' name='year[]' id=year"+cur+" size=4 onKeyup='insRow(event,this,this.id)'>";

dd.innerHTML="<input type=button class='del' id=del"+cur+" onClick='delrow(this)'>";

document.getElementById("partcode"+cur).focus();
  cur++; 

cnt++;



var table = document.getElementById("myTable");
var row = table.rows[index];
console.log(secondRow.id);
}


if(keyCode == 13)
{

var tbl = document.getElementById('myTable');
   var id3=tbl.rows[i3].cells[0].innerHTML;
document.getElementById("partcode"+id3).focus();
} 

}// end function insrow



var cur2=1;
var cnt2=1;
var service=[];
var servamt=[];
var engineer1=[];
var jobs=[];
var day1=[];
var month1=[];
var year1=[];



function delrow2(r)
{


var i=r.parentNode.parentNode.rowIndex;




 document.getElementById('myTable2').deleteRow(i);

  var rowCount = document.getElementById('myTable2').rows.length;
   var tbl = document.getElementById('myTable2');

 if(rowCount<=1)
 {
  document.getElementById('insrowbttn2').type='button';
 }


 var q;
  var id1;
  var labr; 
var sum=0;
  var sum3=0;
  
  var stot;

   for(q=1;q<rowCount;q++)
    {
     id1=tbl.rows[q].cells[0].innerHTML;
    
    
       stot = document.getElementById("servamt"+id1).value;
       sum3+= parseFloat(stot);
     
      
    }
   document.getElementById('labour_total').value=sum3.toFixed(2);
    

  var k=0;
   var ups = document.getElementsByName('servamt[]'), sum = 0, k;
    for(k = ups.length; k--;)
        if(ups[k].value)
            sum += parseFloat(ups[k].value, 10);

document.getElementById('sum_total').value=parseFloat(document.getElementById('spare_total').value)+parseFloat(document.getElementById('labour_total').value);
cnt2--;
}



function insRow2(event,i1,id2)
{

var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

var cur_val=i1.value;

switch(keyCode)
{
case 38:
   var row=i1.parentNode.parentNode.rowIndex;
   var col  =i1.parentNode.cellIndex ;
			 
   document.getElementById('myTable2').rows[row-1].cells[col].getElementsByTagName("input")[0].focus();
  break;
case 40:
  var row=field.parentNode.parentNode.rowIndex;
  var col  =field.parentNode.cellIndex ;
			 
   document.getElementById('myTable2').rows[row+1].cells[col].getElementsByTagName("input")[0].focus();
			
  break;
case 37:
  var row=field.parentNode.parentNode.rowIndex;
  var col  =field.parentNode.cellIndex ;
			 
  document.getElementById('myTable2').rows[row].cells[col-1].getElementsByTagName("input")[0].focus();
	
}

if(id2!="insrowbttn2")
  {
   var i2=i1.parentNode.parentNode.rowIndex;
   var i3=parseFloat(i2)+parseFloat(1);
   var rowCounts = document.getElementById('myTable2').rows.length;
   var rowCounts1= rowCounts-1;
   
  
  }
else
  {
   var i2=0;
   var rowCounts1=0;
  }


		if(((keyCode == 13) && (i2==rowCounts1))||(cnt2==1))
 {
var rowCount = document.getElementById('myTable2').rows.length;
var rowcount1=parseFloat(rowCount)+parseFloat(1);
//alert(rowCount);
var x=document.getElementById('myTable2').insertRow(rowCount);


if(rowCount>=1)
 {
  document.getElementById('insrowbttn2').type='hidden';
 }
var a=x.insertCell(0)
var y=x.insertCell(1)
var job=x.insertCell(2)
var z=x.insertCell(3)	

var engs=x.insertCell(4)
var dd3=x.insertCell(5);
var mm3=x.insertCell(6)	
var yy3=x.insertCell(7);
var dd2=x.insertCell(8);
a.innerHTML=cur2;
y.innerHTML="<input type=text name=service[] id=service"+cur2+" size=20 class='service' onKeydown='return handleEnter2(this, event)'>"
z.innerHTML="<input type=text name=servamt[] id=servamt"+cur2+" size=6 class='servamt' onkeyup='servtotal()' onKeydown='return handleEnter2(this, event)'>"

dd2.innerHTML="<input type=button  class='del' id=del"+cur2+" onClick='delrow2(this)'>";
var v="";
v="<select name='engineer1[]' id=engineer1"+cur2+" onKeydown='return handleEnter2(this, event)'  style='width:150px;'><option></option>\n";

<?
 $empre1=mysqli_query($db,"select ename from empprofile where status!='releived' order by ename");
 while($emprow1=mysqli_fetch_row($empre1))
{
print "v=v+'<option>$emprow1[0]</option>' \n";
}
?>
engs.innerHTML=v+"</select> ";

var vx="";
vx="<select name='jobs[]' id=jobs"+cur2+" onKeydown='return handleEnter2(this, event)'><option>Change</option><option>Repair</option>\n";

job.innerHTML=vx+"</select> ";

dd3.innerHTML="<input type=text value='<?print date('d')?>' name='day1[]' id=day"+cur2+" size=2 onKeydown='return handleEnter2(this, event)'>";
mm3.innerHTML="<input type=text value='<?print date('m')?>' name='month1[]' id=month"+cur2+" size=2 onKeydown='return handleEnter2(this, event)'>";
yy3.innerHTML="<input type=text value='<?print date('Y')?>' name='year1[]' id=year"+cur2+" size=4 onKeyup='insRow2(event,this,this.id)'  >";


document.getElementById("service"+cur2).focus();
  cur2++; 

cnt2++;



var table = document.getElementById("myTable2");
var row = table.rows[index];
console.log(secondRow.id);
}


if(keyCode == 13)
{

var tbl = document.getElementById('myTable2');
   var id3=tbl.rows[i3].cells[0].innerHTML;
document.getElementById("service"+id3).focus();
} 

}// end function insrow2


</script>


<style>
.tble1 input {
background-color:transparent;
border: 0px solid;
height:20px;
//width:160px;
color:black;
}
.tble1
{
border-collapse:collapse;
border:1px solid silver;
margin-top:20px;
}	
.tble1 td, th
{
border-collapse:collapse;
border:1px solid silver;
}


#tbl1{
 margin-top:40px;
min-width:65%;
 text-align:center;
}

#tbltd1{
vertical-align: top;
margin-top:20px;
 height:400px;
background-color:white;
-moz-box-shadow: 0 0 5px -1px #000;
-webkit-box-shadow: 0 0 5px -1px #000;
box-shadow: 0 0 5px -1px #000;

}
#tbl1 td{
 text-align:left;

}
#tbl2{
 //margin:50px 20px 10px 20px;
 width:100%;
}
#tbl3{
 width:100%;
border-collapse:collapse;
}
#myTable{
width:100%;
border-collapse:collapse;
}
#myTable2{
width:100%;
border-collapse:collapse;
}
#myTable td,th{
border:1px solid grey;
}
#myTable th{
font-color:black;
}
#myTable2 td,th{
border:1px solid grey;
}
#tbl3 td,th{
border:1px solid grey;
}
</style>



<?php include('includes/top.php'); ?>
<body style="text-align: center;"  >


         <table class='tbl1' id='tbl1'  align='center'><tr><td align='left' colspan='6'>
		<h1 style="font-size:20px;font-weight:bold;font-family:verdana;color:#FFFFFD;margin:0px; padding:0px;">
		PRIMARY INVESTIGATION</h1></td></tr>
		
		<tr>

             <!--<td  style="text-align:right;"><input style="height:32px;width:110px;" value="Edit"  type="submit" onClick="window.open('editestimate1.php')">  
           <input style="height:32px;width:110px;" value="Print"  type="submit" onClick="window.open('printest1.php')">--> 
          <td  style="text-align:right;"><a href="#login-box" class="login-window" style="text-decoration:none;"><input type='button' style="height:32px;width:110px;"  value='Save' id='svbttn' style="font-size:15; font-weight:bold" ></a>
<!--<a href="#" class='svbttn' id='svbttn2' style="text-decoration:none;"><input type='button' style="height:32px;width:200px;"  value='Update form and Print Bill' name='updprn' style="font-size:15; font-weight:bold"></a>-->
</td></tr>
	
<tr><td id='tbltd1' colspan='6'>

<div>
 <fieldset style="border:2px solid grey; border-style:dotted;">
            <legend style="text-align:center;">Primary Investigation</legend>
<br>

<form name=f2 action='<?print $PHP_SELF?>' method='post' onSubmit='return valid1()'>
<select name=jcno id='sel' class='sel'><option>Select Jobcard No.</option>
<?
$re=mysqli_query($db,"select jcno,regno from gateentry where brcode='$brcode' and invno<=0  order by jcno desc");
while($row=mysqli_fetch_row($re))
{
 print "<option value='$row[0]'>".round($row[0])."-$row[1]</option>";
}
?>
</select>

<input type=submit value='Go' name='show' id='show' class='sub_btn'>
</form>
<br>
<table><tr><td id='sea_td'><form name='f2' action='porder.php' method='post'><input type='search' name='sea_reg' placeholder='Search By Regno'>&nbsp;<input type='search' name='sea_jcno' placeholder='Search By Jcno'>&nbsp;<input type='submit' value='Go' id='reg_sea_bttn' class='sub_btn' name='search_bttn'/>
</form></td></tr></table>

<?
if($search_bttn){
 if($sea_reg !=''){
  $fldnm='regno';
  $fldval=$sea_reg;
 $r=mysqli_query($db,"select max(jcno) from gateentry where $fldnm='$fldval' and brcode='$brcode' ");
  $r1=mysqli_fetch_row($r);
  $res=mysqli_query($db,"select * from gateentry where $fldnm='$fldval'  and jcno='$r1[0]' and brcode='$brcode'");
 
  }
 if($sea_jcno !=''){
  $fldnm='jcno';
  $fldval=$sea_jcno;
   $res=mysqli_query($db,"select * from gateentry where $fldnm='$fldval' and brcode='$brcode'");
  
  }
if(mysqli_num_rows($res)<=0)
   {
    print "<script>alert ('Invalid Register/Jobcard Number');</script>";
   }
}
 
if($show)
{
$res=mysqli_query($db,"select * from gateentry where jcno='$jcno' and brcode='$brcode'");
if(mysqli_num_rows($res)<=0)
   {
    print "<script>alert ('Invalid Jobcard Number');</script>";
   }
 }
if(($search_bttn)||($show))
{
$jrow=mysqli_fetch_array($res);
$jcno=(int)$jrow[jcno];

?>

<form id="jforms" name="invoice_form" >
<input type='hidden' name='updprint' id='upd_flag' value='2'>

<table class='tbl2' id='tbl2'><tr><td style="width:100%;">
   <table border=1 id='tbl3'>


<h4>JCNo: <input type=text readonly name='jcno' size=5 value='<?print $jcno?>'></h4>
<tr><td>Date </td><td><input type=text readonly name='date' value= '<?print datetohtml($jrow[entrydate])?>' size=8></td>
    <td>RegNo </td><td><input type=text readonly name='msno' value='<?print $jrow[regno]?>' size=12></td>

	<td>Delivery Date</td><td><input type=text readonly name='dcomm' value='<?print datetohtml($jrow[estdate])?>' size=8></td>
	<td>Model </td><td><input type=text readonly name='model' value='<?print $jrow[model]?>' size=8></td>
	<td>Owner </td><td colspan='2'><input type=text readonly name='cname' value='<?print $jrow[owner]?>'></td>
</tr>
<tr>	<td>Make </td><td><input type=text readonly name='stdate' value='<?print $jrow[vehiclename]?>'></td>
	<td>Ins type </td><td><input type=text readonly name='instype' value='<?print $jrow[insurance]?>' size=28></td>
	<td>Job Type </td><td><input type=text readonly name='wtystdate' value='<?print $jrow[jobtype]?>' size=8></td>
	<td>ReceivedBy </td><td><input type=text readonly name='wtyenddate' value='<?print $jrow[receivedby]?>' size=8></td>
	<td>KMS </td><td colspan='2'><input type=text name='hmr' size=8 value='<?print $jrow[hmr]?>'></td>

</tr>
<tr>
			<td>User </td><td><input type=text readonly name='user' value='<?print $user?>' size=8></td>
<td>Entry Date </td><td colspan=3>
 <input type=text readonly name='entrdate1' size=1 value='<?print date('d')?>'>
- <input type=text readonly name='entrdate2' size=1 value='<?print date('m')?>'>
- <input type=text readonly name='entrdate3' size=2 value='<?print date('Y')?>'>
</td>
<td>ChasisNO </td><td><input type=text name='chasisno' value='<?print $jrow[chasisno]?>'></td>

<td></td>
<td></td>
<td></td>

</tr>
</table>
</td></tr>

  <tr><td>
<h4>Parts Request </h4>

<input type="button" id='insrowbttn' onclick="insRow(event,this,this.id)" value=" Add Item ">

<table border=1  id="myTable" >


<th> No <th>Part Code<th> Part Name<th>Part type <th>Qty<th>Rate<th>Discount<th> Other Charges<th>Total<th>Ref<th colspan=3>OrderDate<th>Delete


<?
$tbl="stockrepdet10".$brcode.$user;
mysqli_query($db,"delete from $tbl where supqty<=0 ");

 $res=mysqli_query($db,"select * from prime_jcardparts where jcno='$jcno' and brcode='$brcode'");
 $i=1;
 $j=1;
 $partot=0;
 while($jrow=mysqli_fetch_array($res))
  {
$edate1=explode('-',$jrow[edate]);

print "<tr><td>".$i."</td>";
  print "<td><input type=text  name='partcode[]' id='partcode$j' value='$jrow[2]' size=20 onKeydown='return handleEnter(this, event)'></td>";
 print "<td><input type=text size=20 name='partname[]' value='$jrow[3]' id='partname$j onKeydown='return handleEnter(this, event)'></td>";
 print "<td><select   name='spareengineer[]' id='spareengineer$j'  onKeydown='return handleEnter(this, event)'> <option>$jrow[engineer]</option> <option>Orignial</option><option>Commercial</option><option>Used</option></select></td>";
 
 print "<td><input type=text size=4 name='qty[]' id='qty$j' value='$jrow[qty]' onChange=calcamt2($j) onKeydown='return handleEnter(this, event)' onKeyup=validnum(this.id)></td>";

  print "<td><input type=text size=6  name='rate[]' id='rate$j' value='$jrow[rate]' onchange='unitcal(this.value,this)' onKeydown='return handleEnter(this, event)' > </td>";
   print "<td><input type=text size=4 name='disc[]' id='disc$j' value='$jrow[disc]'  onchange='discountcal(this.value,this)' onKeydown='return handleEnter(this, event)' ></td>";


   print "<td><input type=text size=3 name='hnd[]' id='hnd$j' value='$jrow[hnd]'  onchange='othercal(this.value,this)' onKeydown='return handleEnter(this, event)'></td>";

   print "<td><input type=text readonly size=8 name='gtot[]' id='gtot$i' value='$jrow[gtot]' onKeydown='return handleEnter(this, event)'></td>";


 $qtres=mysqli_query($db,"select supqty from $tbl where partcode='$jrow[2]'");
 $qtrow=mysqli_fetch_row($qtres);
   $qqty=($qtrow[0]+$jrow[qty]);


   print "<td><input type=text  readonly name='exqty[]' id='exqty$j' value='$qqty' size=4></td>";
      print "<td><input type=text   name='day[]' id='day$j' value='$edate1[2]' size=2 onKeydown='return handleEnter(this, event)'></td>";
    print "<td><input type=text   name='month[]' id='month$j' value='$edate1[1]' size=2 onKeydown='return handleEnter(this, event)'></td>";
    print "<td><input type=text   name='year[]' id='year$j' value='$edate1[0]' size=4 onKeyup='insRow(event,this,this.id)'></td>";
 print "<td><input type=button class='del' onClick='delrow(this)'></td></tr>";

$partot+=$jrow[gtot];
   $i++;
   $j++;
print"<script>cur++;</script>";
print"<script>cnt++;</script>";

  }
?>
</table> 
</td></tr>
<tr><td>
<h4>Service Details </h4>
 <input type="button" id='insrowbttn2' onclick="insRow2(event,this,this.id)" value=" Add Service ">
<table border=1 id="myTable2" >

<th>No.<th>Service  <th>Repair/Replace<th>Amt <th>Assigned to<th colspan=3>EnteredOn<th>Delete
<?
 $res=mysqli_query($db,"select * from prime_jcardserv where jcno='$jcno' and brcode='$brcode'");
 $servtot=0;
 $i=1;
 while($jrow=mysqli_fetch_array($res))


  {
$edate2=explode('-',$jrow[edate]);


  print "<tr><td>".$i."</td>";
   print "<td><input type=text size=20 name='service[]' id='service$i' value='$jrow[2]'  onKeydown='return handleEnter2(this, event)'></td>";
   print "<td><select name='jobs[]' id='jobs$i'  onKeydown='return handleEnter2(this, event)'><option>$jrow[4]</option><option>Change</option><option>Repair</option></td>";
  
  print "<td><input type=text size=6 name='servamt[]' value='$jrow[3]' id='servamt$i' onkeyup='servtotal()'  onKeydown='return handleEnter2(this, event)' ></td>";
   


   print "<td><select name='engineer1[]' id='engineer1$j' onKeydown='return handleEnter2(this, event)' style='width:150px;'><option>$jrow[engineer]</option>";

 $empre1=mysqli_query($db,"select ename from empprofile where status!='releived' order by ename");
 while($emprow1=mysqli_fetch_row($empre1))
{
print "<option>$emprow1[0]</option>";
}
print "</select></td>";

    print "<td><input type=text   name='day1[]' id='day1$j' value='$edate2[2]' size=2  onKeydown='return handleEnter2(this, event)'></td>";
    print "<td><input type=text   name='month1[]' id='month1$j' value='$edate2[1]' size=2  onKeydown='return handleEnter2(this, event)'></td>";
    print "<td><input type=text   name='year1[]' id='year1$j' value='$edate2[0]' size=4  onKeyup='insRow2(event,this,this.id)'></td>";

print "<td><input type='button' class='del' onClick='delrow2(this)'></td></tr>";




 print "</tr>";
$servtot+=$jrow[3];
   $i++;
print"<script>cur2++;</script>";
print"<script>cnt2++;</script>";
  }
$ntot=$partot+$servtot;
?>
</table><br>

<datalist id="htmlListTags"></datalist>



<input type='hidden' name='brcode' id='brcode' value='<? print $brcode;?>'>
<input type='hidden' name='curqty' id='curqty' value='0'>
<input type='hidden' name='prtname' id='prtname' value='0'>
<input type='hidden' name='savecount' id='savecount' value='0'>
<div id='txtHint'></div>
<div id='partvalid'></div>




 <div id="login-box" class="login-popup">
        <a href="#" class="close"><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>
          <div id='msgbox'></div>
		</div>



</td></tr>
<tr>   
<td  colspan='5' style="text-align:right;">
                      
                    Parts Total
                <input  style="border:0px solid white;text-align:right; width:100px; padding-right:5px; font-weight:normal; font-family:verdana;font-size:13px;color:#3c3c3c;" readonly="readonly" name="spare_total" id="spare_total" value=<? echo $partot; ?> type="text">
</td></tr>
<tr><td colspan='5' style="text-align:right;">
              Service Total
                <input  style="border:0px solid white;text-align:right; width:100px; padding-right:5px; font-weight:normal; font-family:verdana;font-size:13px;color:#3c3c3c;" readonly="readonly"  name="labour_total" id="labour_total" value=<? echo $servtot; ?> type="text">
</td></tr>
<tr><td  colspan='5' style="text-align:right;">                  
              Total
                <input onclick="alert('The total is calculated automatically as soon as you have entered something into both the unit price and the quantity field in the lines above.\n\nExample: If you wish to invoice a service for $100.00, you should enter 100.00 in the unit price field and 1.00 in the quantity field.')" style="border:0px solid white;text-align:right; width:100px; padding-right:5px; font-weight:normal; font-family:verdana;font-size:13px;color:#3c3c3c;" readonly="readonly" name="sum_total" id="sum_total" value=<? echo $ntot; ?> type="text">

</td></tr>

<tr><td colspan='6'><hr></hr></td></tr>
<?
$res=mysqli_query($db,"select * from payment_mode  where  status='yes' order by mode");


?>

<tr><td colspan='5' style="text-align:right;"><b>Payment Mode:</b><select name='pmode' onchange="showtype(this.value)"><option>--select--</option>
<?
while($row=mysqli_fetch_array($res))
{
PRINT "<option >$row[1]</option>";
}
?>
</select>

 </form>

   </td></tr>
</table>
   
</div>
</fieldset> 

</td></tr></table>
<?
}
?>	

