$(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: "upd_svinvoice.php",
        type: "POST",
       data:   values1,
         success: function(msg){
              if(parseInt(msg)!=0)

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


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

   });
   });



        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
      $('li').has('ul').mouseover(function(){
        $(this).children('ul').show();
        }).mouseout(function(){
        $(this).children('ul').hide();
        })
          }
        });


    $(document).ready(function() {
        var cont_left = $("#container").position().left;
        $("a img").hover(function() {
            // hover in
            //$(this).parent().parent().css("z-index", 1);
            $(this).animate({
               height: "110",
               width: "110",

            }, "fast");
        }, function() {
            // hover out
         //   $(this).parent().parent().css("z-index", 0);
            $(this).animate({
                height: "80",
                width: "80",

            }, "fast");
        });

        $(".img").each(function(index) {
            var left = (index * 160) + cont_left;
            $(this).css("left", left + "px");
        });
    });

/*** function below for validate partname */


 function partnamevalid(pid,pval)
 {
//alert('xxx');
   if(pval=='')
    {
   alert('Please Enter The Itemname');

      document.getElementById(pid).focus();


    }
 }




function prtval(str,pos,flg,pos1)
{

//alert(pos);

document.getElementById(pos).value=str;
document.getElementById('typ'+pos1).value=flg;
document.getElementById(pos).focus();
document.getElementById("livesearch").innerHTML="";

}




function chkpart(prt,r)
 {

var i=r.parentNode.parentNode.rowIndex;
var part = document.getElementById("partname"+i).value;

  }


function qtycheck(str,r,qid)
{
   if(str.length==0)
     {
        str=0;
     }

   var fldid=document.getElementById(qid).value;
   if(isNaN(fldid))
    {
         alert('enter digit');
        document.getElementById(qid).value=0;
    }
  var i=r.parentNode.parentNode.rowIndex;
     var tbl = document.getElementById('myTable');
     var id1=tbl.rows[i].cells[0].innerHTML;
  var part = document.getElementById("partcode"+id1).value;
  var partnm = document.getElementById("partname"+id1).value;
  
  
   if(part=='')
     {
         alert('Enter ItemCode');
         document.getElementById("partcode"+id1).focus();
    }

  else
     {
         var n=document.getElementById('myTable').rows.length-1;

 
 
                       
                   var tamt=(document.getElementById("price"+id1).value)*(document.getElementById("supqty"+id1).value);
                  
                  document.getElementById("aprice"+id1).value=tamt;
                  
                  var taxmt=((document.getElementById("tax"+id1).value)*tamt)/100;
                  document.getElementById("taxamt"+id1).value=taxmt;
                    var totamt=parseFloat(tamt)+parseFloat(taxmt);
 
                 document.getElementById("tot"+id1).value=totamt;
                 
                  

                    
  }
}



function unitcal(val,r,qid)
  {
    var i=r.parentNode.parentNode.rowIndex;
       var tbl = document.getElementById('myTable');
       var id1=tbl.rows[i].cells[0].innerHTML;
     if(val.length==0)
       {
          val=0;
       }

    var fldid=document.getElementById('unitprice'+id1).value;
     if(isNaN(fldid))
      {
         alert('enter digit');
           document.getElementById('unitprice'+id1).value=0;
           val=0;
      }
    var qtyval=document.getElementById('qty'+id1).value;
    var totval=(qtyval)*(fldid);
    var part = document.getElementById("partcode"+id1).value;
    var partnm = document.getElementById("partname"+id1).value;
    var itemtyp=document.getElementById("itmtyp"+id1).value;
    var itmvl=document.getElementById("itmvl"+id1).value;
                var custnm=document.getElementById("cus").value;
     if(part=='')
       {
           alert('Enter ItemCode');
           document.getElementById("partcode"+id1).focus();
        }

    else
       {
           var n=document.getElementById('myTable').rows.length-1;
        if(i>=1)
             {
               var m;
               var str1=0;
               for(m=1;m<=n;m++)
                  {
                     var tbl1 = document.getElementById('myTable');
                     var id2=tbl1.rows[m].cells[0].innerHTML;
                    var part_pre = document.getElementById("partcode"+id2).value;
                    var qty_pre = document.getElementById("qty"+id2).value;
                      if((part==part_pre)&&(m!=i))
                        {

                           str1=parseFloat(str1)+parseFloat(qty_pre);

                        }
                   }

                                            }
                                  var stfdisc=document.getElementById("stfdisc"+id1).value;
           if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
            }
        else
            {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

        xmlhttp.onreadystatechange=function()
            {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                  {
                    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                var msg=document.getElementById("alertmsg").value;
                     if(msg<1)
                        {
                             alert('Sorry!...Quantity Exceeded');
                          document.getElementById("qty"+id1).value='';
                           var up=document.getElementById("untprice").value;
                           var tp=document.getElementById("totalamt").value;
                         document.getElementById("unitprice"+id1).value=up;
                          document.getElementById("tot"+id1).value=tp;
                         document.getElementById("qty"+id1).focus();
                        }
                      else
                        {

                          var tamt=(document.getElementById("unitprice"+id1).value)*(document.getElementById("qty"+id1).value);
                     document.getElementById("tot"+id1).value=tamt;
                     var redctpr=document.getElementById("redct_per").value;
                    document.getElementById("rdctpr"+id1).value=redctpr;
                    var reduction=document.getElementById("redct_val").value;
                    document.getElementById("redct"+id1).value=reduction;
                    var strebate=document.getElementById("strbt_val").value;
                    document.getElementById("rebat"+id1).value=strebate;
                    var grpkvi=document.getElementById("kvic_val").value;
                    document.getElementById("kvic"+id1).value=grpkvi;
                    var grpkvispl=document.getElementById("kvicspl_val").value;
                    document.getElementById("kvicspl"+id1).value=grpkvispl;
                    var bfrtxamt=document.getElementById("bfrtx_val").value;
                    document.getElementById("befrtx"+id1).value=bfrtxamt;
                    var txpr=document.getElementById("tx_val").value;
                    document.getElementById("tax"+id1).value=txpr;
                    var taxamount=document.getElementById("txprc_val").value;
                    document.getElementById("taxamt"+id1).value=taxamount;
                    var netamt=document.getElementById("totnw_val").value;
                    document.getElementById("ntamt"+id1).value=netamt;
                       }


                var k=0;
                   var ups = document.getElementsByName('ntamt[]'), 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('tot[]'), sum1 = 0, p;
                    for(p = ups1.length; p--;)
                                sum1 += parseFloat(ups1[p].value, 10);
                  document.getElementById('spare_total').value=sum1.toFixed(2);







                }
            }
                                   //var url="invo_untprc_ajax.php?qty="+qtyval+"&&part="+part+"&&ind="+id1+"&&preqty="+str1+"&&itmtp="+itemtyp+"&&itemvl="+itmvl+"&&untprc="+fldid;
                                   //alert(url);
      xmlhttp.open("GET","invo_untprc_ajax.php?qty="+qtyval+"&&part="+part+"&&ind="+id1+"&&preqty="+str1+"&&itmtp="+itemtyp+"&&itemvl="+itmvl+"&&untprc="+fldid+"&&stafdis="+stfdisc,true);
      xmlhttp.send();
    }
  }


function stfdischeck(str,r,qid)
{
   if(str.length==0)
     {
        str=0;
     }

  var fldid=document.getElementById(qid).value;
   if(isNaN(fldid))
    {
         alert('enter digit');
        document.getElementById(qid).value=0;
    }


  var i=r.parentNode.parentNode.rowIndex;


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

     var qtyval=document.getElementById('qty'+id1).value;
  var part = document.getElementById("partcode"+id1).value;
  var partnm = document.getElementById("partname"+id1).value;
  var untprc=document.getElementById("unitprice"+id1).value;
  var itemtyp=document.getElementById("itmtyp"+id1).value;
  var itmvl=document.getElementById("itmvl"+id1).value;
        var custnm=document.getElementById("cus").value;
  if(document.getElementById("ptype").value=='3')
    {
      document.getElementById("stfdisc"+id1).style.visibility = "visible";
                        document.getElementById("stfdiscvl"+id1).style.visibility = "visible";
    }
  else
    {
      document.getElementById("stfdisc"+id1).style.visibility = "hidden";
                        document.getElementById("stfdiscvl"+id1).style.visibility = "hidden";
    }
   if(part=='')
     {
         alert('Enter ItemCode');
         document.getElementById("partcode"+id1).focus();

      }

  else
     {


         var n=document.getElementById('myTable').rows.length-1;


      if(i>=1)
           {
             var m;
             var str1=0;

             for(m=1;m<=n;m++)
                {
                   var tbl1 = document.getElementById('myTable');
                   var id2=tbl1.rows[m].cells[0].innerHTML;

                  var part_pre = document.getElementById("partcode"+id2).value;
                  var qty_pre = document.getElementById("qty"+id2).value;
                    if((part==part_pre)&&(m!=i))
                      {

                         str1=parseFloat(str1)+parseFloat(qty_pre);

                      }
                 }

          }

        var stfdisc=document.getElementById("stfdisc"+id1).value;
         if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
            }
        else
            {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

           xmlhttp.onreadystatechange=function()
            {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                  {
                    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                var msg=document.getElementById("alertmsg").value;
                     if(msg<1)
                        {
                                                                            alert ('Sorry!...Quantity Exceeded');
                                                                            document.getElementById("qty"+id1).value='';
                                                                            var up=document.getElementById("untprice").value;
                                                                            var tp=document.getElementById("totalamt").value;
                                                                            document.getElementById("unitprice"+id1).value=up;
                                                                            document.getElementById("tot"+id1).value=tp;
                                                                            document.getElementById("qty"+id1).focus();
                        }
                      else
                        {

                                                                            var tamt=(document.getElementById("unitprice"+id1).value)*(document.getElementById("qty"+id1).value);
                                                                            document.getElementById("tot"+id1).value=tamt;
                                                                            var redctpr=document.getElementById("redct_per").value;
                                                                            var stfdvl=document.getElementById("stfds_val").value;
                                                                            document.getElementById("stfdiscvl"+id1).value=stfdvl;
                                                                            document.getElementById("rdctpr"+id1).value=redctpr;
                                                                            var reduction=document.getElementById("redct_val").value;
                                                                            document.getElementById("redct"+id1).value=reduction;
                                                                            var strebate=document.getElementById("strbt_val").value;
                                                                            document.getElementById("rebat"+id1).value=strebate;
                                                                            var grpkvi=document.getElementById("kvic_val").value;
                                                                            document.getElementById("kvic"+id1).value=grpkvi;
                                                                            var grpkvispl=document.getElementById("kvicspl_val").value;
                                                                            document.getElementById("kvicspl"+id1).value=grpkvispl;
                                                                            var bfrtxamt=document.getElementById("bfrtx_val").value;
                                                                            document.getElementById("befrtx"+id1).value=bfrtxamt;
                                                                            var txpr=document.getElementById("tx_val").value;
                                                                            document.getElementById("tax"+id1).value=txpr;
                                                                            var taxamount=document.getElementById("txprc_val").value;
                                                                            document.getElementById("taxamt"+id1).value=taxamount;
                                                                            var netamt=document.getElementById("totnw_val").value;
                                                                            document.getElementById("ntamt"+id1).value=netamt;
                      var disavl=document.getElementById("disclimit").value*0.10;
                      document.getElementById("dsvn").value=parseFloat(document.getElementById("stf_amt").value);
                      }


var l=0;
var ups5 = document.getElementsByName('stfdiscvl[]'), sum5 = 0, l;
 for(l = ups5.length; l--;)
 {
    sum5 += parseFloat(ups5[l].value, 10);
    if(id1==1)
    {
        document.getElementById("dsvn").value=parseFloat(document.getElementById("stf_amt").value)+parseFloat(document.getElementById("stfdiscvl"+id1).value);
    }
    var nedisv=parseFloat(document.getElementById("stf_amt").value)+parseFloat(sum5);
    //alert(nedisv);
    if(nedisv>disavl)
    {
        //alert(document.getElementById("dsvn").value);
        //alert(document.getElementById("stfdiscvl"+id1).value);
        //alert(disavl);
        alert("Discount Amount exceeds the Discount Limit");
        var allwbl=document.getElementById("stf_amt").value;
        if(id1>=1)
        {
            document.getElementById("dsvn").value=0;
            document.getElementById("dsvn").value=parseFloat(document.getElementById("stf_amt").value)+parseFloat(sum5)-parseFloat(document.getElementById("stfdiscvl"+id1).value);
            mdisc_avail =disavl-parseFloat(document.getElementById("dsvn").value);
            document.getElementById("stfdisc"+id1).value=(Math.round(parseFloat(mdisc_avail*100)/parseFloat(document.getElementById("tot"+id1).value)*100)/100.00)-.01;
            document.getElementById("stfdiscvl"+id1).value=parseFloat(document.getElementById("tot"+id1).value)*parseFloat(document.getElementById("stfdisc"+id1).value)/100;
        }
        else
        {
            document.getElementById("dsvn").value=document.getElementById("stf_amt").value;
            document.getElementById("stfdisc"+id1).value=0;
            document.getElementById("stfdiscvl"+id1).value=0;
        }
    }
    else
    {
        document.getElementById("dsvn").value=parseFloat(document.getElementById("stf_amt").value)+parseFloat(sum5);
    }

}









                var k=0;
                   var ups = document.getElementsByName('ntamt[]'), 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('tot[]'), sum1 = 0, p;

                    for(p = ups1.length; p--;)

                                sum1 += parseFloat(ups1[p].value, 10);
                                                                        var allwcr=document.getElementById("tot_crvl").value;
                                                                        var allwbl=document.getElementById("tot_crbl").value;
                                                                        allwbl=parseFloat(allwbl)+parseFloat(sum1);
                                                                        if(allwbl>allwcr)
                                                                            {
                                                                                alert("Bill Amount exceeds the Credit Limit");
                                                                                document.getElementById("stfdisc"+id1).value=0;
                                                                                document.getElementById("stfdiscvl"+id1).value=0;
                                                                            }
                  document.getElementById('spare_total').value=sum1.toFixed(2);

              }
            }


      xmlhttp.open("GET","invo_untprc_ajax.php?qty="+qtyval+"&&part="+part+"&&ind="+id1+"&&preqty="+str1+"&&itmtp="+itemtyp+"&&itemvl="+itmvl+"&&untprc="+untprc+"&&stafdis="+fldid+"&&custnme="+custnm,true);
      xmlhttp.send();
    }





}
function reductioncheck(str,r,qid)
{
   if(str.length==0)
     {
        str=0;
     }

   var fldid=document.getElementById(qid).value;
   if(isNaN(fldid))
    {
         alert('enter digit');
        document.getElementById(qid).value=0;
    }
  var i=r.parentNode.parentNode.rowIndex;
     var tbl = document.getElementById('myTable');
     var id1=tbl.rows[i].cells[0].innerHTML;
  var part = document.getElementById("partcode"+id1).value;
  var partnm = document.getElementById("partname"+id1).value;
  var itemtyp=document.getElementById("itmtyp"+id1).value;
  var itmvl=document.getElementById("itmvl"+id1).value;
        var quantit=document.getElementById("qty"+id1).value;
  if(document.getElementById("ptype").value=='3')
    {
      document.getElementById("stfdisc"+id1).style.visibility = "visible";
                        document.getElementById("stfdiscvl"+id1).style.visibility = "visible";
    }
  else
    {
      document.getElementById("stfdisc"+id1).style.visibility = "hidden";
                        document.getElementById("stfdiscvl"+id1).style.visibility = "hidden";
    }
   if(part=='')
     {
         alert('Enter ItemCode');
         document.getElementById("partcode"+id1).focus();
    }

  else
     {
         var n=document.getElementById('myTable').rows.length-1;

      if(i>=1)
           {
             var m;
             var str1=0;
             for(m=1;m<=n;m++)
                {
                   var tbl1 = document.getElementById('myTable');
                   var id2=tbl1.rows[m].cells[0].innerHTML;
                  var part_pre = document.getElementById("partcode"+id2).value;
                  var qty_pre = document.getElementById("qty"+id2).value;
                    if((part==part_pre)&&(m!=i))
                      {
                         str1=parseFloat(str1)+parseFloat(qty_pre);
                      }
                 }

          }

      var stfdisc=document.getElementById("stfdisc"+id1).value;

         if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
          }
      else
          {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }

      xmlhttp.onreadystatechange=function()
          {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
              var msg=document.getElementById("alertmsg").value;
                   if(msg<1)
                      {
                           alert ('Sorry!...Quantity Exceeded');
                        document.getElementById("qty"+id1).value='';
                         var up=document.getElementById("untprice").value;
                         var tp=document.getElementById("totalamt").value;
                                                                        document.getElementById("unitprice"+id1).value=up;
                        document.getElementById("tot"+id1).value=tp;
                                                                        document.getElementById("qty"+id1).focus();
                      }
                    else
                      {
                        var tp=document.getElementById("totalamt").value;
                        var tamt=(document.getElementById("unitprice"+id1).value)*(document.getElementById("qty"+id1).value);
                   document.getElementById("tot"+id1).value=tamt;
                  var redctpr=document.getElementById("redct_per").value;
				
                  document.getElementById("rdctpr"+id1).value=redctpr;
                  var reduction=document.getElementById("redct_val").value;
                  document.getElementById("redct"+id1).value=reduction;
				    if( document.getElementById("redct"+id1).value==0)
						  {
						alert('Reduction is Only For Polyster');
					 document.getElementById("itmvl"+id1).value='0';
						  }
                  var strebate=document.getElementById("strbt_val").value;
                  document.getElementById("rebat"+id1).value=strebate;
                  var grpkvi=document.getElementById("kvic_val").value;
                  document.getElementById("kvic"+id1).value=grpkvi;
                  var grpkvispl=document.getElementById("kvicspl_val").value;
                  document.getElementById("kvicspl"+id1).value=grpkvispl;
                  var bfrtxamt=document.getElementById("bfrtx_val").value;
                  document.getElementById("befrtx"+id1).value=bfrtxamt;
                  var txpr=document.getElementById("tx_val").value;
                  document.getElementById("tax"+id1).value=txpr;
                  var taxamount=document.getElementById("txprc_val").value;
                  document.getElementById("taxamt"+id1).value=taxamount;
                  var netamt=document.getElementById("totnw_val").value;
                  document.getElementById("ntamt"+id1).value=netamt;

                     }


              var k=0;
                 var ups = document.getElementsByName('ntamt[]'), 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('tot[]'), sum1 = 0, p;

                  for(p = ups1.length; p--;)

                              sum1 += parseFloat(ups1[p].value, 10);
                document.getElementById('spare_total').value=sum1.toFixed(2);
            }
      }

  xmlhttp.open("GET","invo_qty_ajax_work.php?qty="+quantit+"&&part="+part+"&&ind="+id1+"&&preqty="+str1+"&&itmtp="+itemtyp+"&&itemvl="+itmvl+"&&stafdis="+stfdisc,true);
  xmlhttp.send();

  }
}

/***********************************************
* 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-1].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;


              }


  }





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;
  }


function handleEnter3 (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 + 3) % 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 delrow(r)
  {

    var i=r.parentNode.parentNode.rowIndex;
     document.getElementById('myTable').deleteRow(i);


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

      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;
           stot = document.getElementById("tot"+id1).value;
             sum2+= parseFloat(stot);
            }
       document.getElementById('spare_total').value=sum2.toFixed(2);
     if(rowCount<=1)
       {
          document.getElementById('insrowbttn').type='button';
       }
    var k=0;
       var ups = document.getElementsByName('ntamt[]'), 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);
        cnt--;
         cur--;
   }

  function validnum(str)
{
  var fldid=document.getElementById(str).value;
 if(isNaN(fldid))
  {
  alert('enter digit');
  document.getElementById(str).value=0;
  }
}

        $('#default_notes_textarea, #to_address, #from_address').jqEasyCounter({
            'maxChars': 1000,
            'maxCharsWarning': 980,
            'msgFontSize': '10px',
            'msgFontColor': 'silver',
            'msgFontFamily': 'Arial',
            'msgTextAlign': 'right',
            'msgWarningColor': 'red',
            'msgAppendMethod': 'insertAfter',
            'allocateSpace': true
        });

        reCalculate(document.getElementById('sum_discount'),'dataTable','0.00');

        function loadReceiptLayer(obj){
            var to_id = $('#to_name').val();

            if (obj.checked){
                $('#receipts_layer').show(100, function(){
                    if (!$(this).attr('rloaded')){
                        $(this).attr('rloaded','true');
                        $('#receipts_layer').html('<img src="images/loadingsmall.gif">');

                        $.ajax({

                            url: "showExpenses.php?action=displayReceiptList&customer_id="+to_id+"&list=",
                            type: 'GET',
                            cache: false,
                            async: true,
                            dataType : 'json',
                            success:function(response){
                                if (response && response.success)
                                    $('#receipts_layer').html(response.html);
                                else
                                    $('#receipts_layer').html('Fail to load. Try Again');
                            },
                            error:function(){
                                $('#receipts_layer').html('There was an error. Try Again');
                            }
                        });




                    }
                }) ;
            }
            else{
                $('#receipts_layer').hide();
            }
        }

        $(function(){
            /*
            $('#to_name').change(function(){
                $('#receipts_layer').attr('rloaded','');
                document.getElementById('receipts_include').checked = false;
                $('#receipts_layer').hide();

            });
            */



        });
        $('#receipts_include').click(function(){


            var obj = $('#receipts_layer');
            if (document.getElementById('receipts_include').checked){


                if (obj.attr('rloaded') == 'true'){
                    obj.show();
                }
                else{
                    loadReceiptLayer(document.getElementById('receipts_include'));
                }
            }else{
                obj.hide();
            }



        })
