



$(document).ready(function(){
 
        $("#msg").hide();
      //  $(".svbttn").show();
 
    $('.svbttn').click(function(){
          $("#msg").show(500);
    });
  $('.hide_show').click(function(){
          $("#msg").hide();
    });
 
});
 
$(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(){

    $("#svbttn2").click(function(){
     $('#upd_flag').val('1');
  
  });   

$("#svbttn1").click(function(){
     $('#upd_flag').val('2');
  
  }); 
 }); 




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

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

   
  	 var values1 = $("#jforms").serialize();
 	

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

 

           	 if(parseInt(msg)!=0)    

          	  {

                $('#msgbox').html(msg);
               
		}
           

      	},
      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");
        });
    });




$(document).bind('keydown', 'Alt+n',function (e){

      
    if(e.which==78 && e.altKey){
	 
       shortkey_tblopen();
        return false;
    } 
     if(e.which==83 && e.altKey){
	 
         $( "#svbttn1" ).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(); 
 }






//functions below to calculate total amount


function qutychecking(val,r){

   var i      =r.parentNode.parentNode.rowIndex;
   var tbl    =document.getElementById('myTable');
   var id1    =tbl.rows[i].cells[0].innerHTML;
   
   var qty_stk=document.getElementById('stok'+id1).value;
   var part_nm=document.getElementById('partname'+id1).value;
   
    if(isNaN(val)){
     
      alert('enter digit');
      document.getElementById('qty'+id1).value=0;
      document.getElementById('qty'+id1).focus();
      document.getElementById('qty'+id1).select();
    }       
    if((qty_stk-val)<0){

      alert('Sorry..!! Quantity exceeded the limit');
      document.getElementById('qty'+id1).value=0;
      document.getElementById('qty'+id1).focus();
      document.getElementById('qty'+id1).select();
    }
   
    var a;
    for(a=1; a<count; a++){
            
        if(part_nm==ptnm[a]){
     
             if(val<5){
               document.getElementById('rate'+id1).value=uprice2[a];
             }
             else
               document.getElementById('rate'+id1).value=uprice1[a];
        }
    }
    var uprice=document.getElementById('rate'+id1).value;
    var hnd   =document.getElementById('hnd'+id1).value;
    var dis   =document.getElementById('disc'+id1).value;
    var tot   = parseFloat((val*uprice))+ parseFloat(hnd)- parseFloat(dis);
    document.getElementById('gtot'+id1).value=tot;
    
    var p=0;
    var ups1= document.getElementsByName('gtot[]'), sum1 = 0, p;
   
    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);

    check_stock();
           
}

        

function calctotal(r){

   var i      =r.parentNode.parentNode.rowIndex;
   var tbl    =document.getElementById('myTable');
   var id1    =tbl.rows[i].cells[0].innerHTML;
   var dis=document.getElementById('disc'+id1).value;
   var hnd=document.getElementById('hnd'+id1).value;
   var uprice=document.getElementById('rate'+id1).value;
 
     if(isNaN(uprice)){
     
      alert('enter digit');
      document.getElementById('rate'+id1).value=0;
      document.getElementById('rate'+id1).focus();
      document.getElementById('rate'+id1).select();
    }    
 
     if(isNaN(dis)){
     
      alert('enter digit');
      document.getElementById('disc'+id1).value=0;
      document.getElementById('disc'+id1).focus();
      document.getElementById('disc'+id1).select();
    }       

    if(isNaN(hnd)){
     
      alert('enter digit');
      document.getElementById('hnd'+id1).value=0;
      document.getElementById('hnd'+id1).focus();
      document.getElementById('hnd'+id1).select();
    }       
     
    var qty   =document.getElementById('qty'+id1).value;
    var tot   = parseFloat((qty*uprice))+ parseFloat(hnd)- parseFloat(dis);
    document.getElementById('gtot'+id1).value=tot;
    
    var p=0;
    var ups1= document.getElementsByName('gtot[]'), sum1 = 0, p;
   
    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 check_stock(){
   
   var rowcount=document.getElementById('myTable').rows.length;
   var qty_tot;
   var rem_stock;
   var act_stk;
   for(var p=1;p<rowcount;p++){
 
     var tbl3 = document.getElementById('myTable');
     var id3=tbl3.rows[p].cells[0].innerHTML;
     var part=document.getElementById('partname'+id3).value;
     qty_tot=0;
     rem_stock=0;

     for(var q=1; q<count;q++) {
     
          if(part==ptnm[q]){

             act_stk=st_ock[q];
          }
      }

      for(var r=1; r<rowcount; r++){

          var tbl4 = document.getElementById('myTable');
          var id4=tbl4.rows[r].cells[0].innerHTML;
          var part1=document.getElementById('partname'+id4).value;
          var qty1=document.getElementById('qty'+id4).value;

          if (part==part1) {

            document.getElementById('stok'+id4).value=parseFloat(act_stk)-parseFloat(qty_tot);
            qty_tot=parseFloat(qty_tot)+parseFloat(qty1);
          }

       } 

    }

 }//end function check stock



function labtotal()
 {
  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);
 }




function delrow(r)
{


var i=r.parentNode.parentNode.rowIndex;




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

  check_stock();


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


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



/***********************************************
* 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();
			field.form.elements[i].select();                   
			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();
			field.form.elements[i].select();
			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 chkval(val,this1)
{

var i=this1.parentNode.parentNode.rowIndex;


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

 var amtval= document.getElementById('servamt'+id1).value;




if((amtval-val)<0)
{

alert (" Sublet amt should be less than service amt.");
document.getElementById('subamt'+id1).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();
            }



        })




    
