$(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: "sv_purchrtn_work.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');
      }   
   	 }); 

	 }); 
	 }); 


















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












/** code below for search field *******/

function showResult(str,r,pos)
{
var i=pos.parentNode.parentNode.rowIndex;


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






if (str.length==0)
  {
  
  document.getElementById("livesearch").innerHTML="";
  document.getElementById("livesearch").style.border="0px";
  return;
  }
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("livesearch").innerHTML=xmlhttp.responseText;
    document.getElementById("livesearch").style.border="1px solid #A5ACB2";

     var ptvld=document.getElementById("prtvald").value;

     if(ptvld=='invalid')
      {
     alert('Invalid Partname');
     document.getElementById(r).value='';
     document.getElementById('qty'+id1).value='';
     document.getElementById('unitprice'+id1).value='';
     document.getElementById('tot'+id1).value='';
      document.getElementById(r).focus();

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





      }
    }
  }
xmlhttp.open("GET","livesearch.php?q="+str+"&&pos="+r+"&&pos1="+id1,true);
xmlhttp.send();
}



/*** function below for validate partname */


 function partnamevalid(pid,pval)
 {
alert('xxx');
   if(pval=='')
    {
   alert('Please Enter The Partname');
     
      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)
{


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

   

 



 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 part1 = document.getElementById("partname"+id1).value;
var dis = document.getElementById("dis"+id1).value;
 if(part=='')
 {
   alert('enter PartName');
   document.getElementById("partcode"+id1).focus();
   
  }

else
 {

   var n=document.getElementById('myTable').rows.length-1;
//alert(n);
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("rtnqty"+id2).value;
      if((part==part_pre)&&(m!=i))
        {
         str1=parseFloat(str1)+parseFloat(qty_pre); 
        }
     }

  }


   
   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("rtnqty"+id1).value='';

	var tp=document.getElementById("totalamt").value;

      
        document.getElementById("tot"+id1).value=tp;
       document.getElementById("rtnqty"+id1).focus();
      }
      else
      {
       
         
	
         var tp=document.getElementById("unitprice"+id1).value*document.getElementById("rtnqty"+id1).value;
         
      
        document.getElementById("tot"+id1).value=tp;
        //document.getElementById("unitprice"+id1).focus();





       }









    


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


 /*  var ct;
  var  sum_sp=0;
  var tbl2;
  var id2;
  var tp;
  var typ;
  for(ct=1;ct<rowCounts;ct++)
     {

   tbl2= document.getElementById('myTable');
   id2=tbl2.rows[ct].cells[0].innerHTML;
   typ= document.getElementById("typ"+id2);
 
     if(typ.value == 'labour') 
       {
   tp=document.getElementById("tot"+id2).value;
     sum_sp += parseFloat(tp);
        
   return true;
     alert(typ.value);
       }
     
 
        }
  document.getElementById('spare_total').value=sum_sp.toFixed(2);  */
                   
 

     var p1=0;
   var ups2 = document.getElementsByName('tot[]'),sum2 = 0, p1;
    var t2 = document.getElementsByName('typ[]');
   
    for(p1 = ups2.length; p1--;)
             
        if((ups2[p1].value)&&(t2[p1].value=='labour'))
           
            sum2 += parseFloat(ups2[p1].value, 10);
          
			
                    document.getElementById('labour_total').value=sum2.toFixed(2);




    }
  }
xmlhttp.open("GET","invo_qty_ajax_purchrtn.php?qty="+str+"&&part="+part+"&&ind="+id1+"&&preqty="+str1+"&&dis="+dis+"&&part1="+part1,true);
xmlhttp.send();



}
}





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('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 disc=document.getElementById('dis'+id1).value;
   var totval=(qtyval)*(fldid);

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


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

           
             var p1=0;
   var ups2 = document.getElementsByName('tot[]'),sum2 = 0, p1;
    var t2 = document.getElementsByName('typ[]');
   
    for(p1 = ups2.length; p1--;)
             
        if((ups2[p1].value)&&(t2[p1].value=='labour'))
           
            sum2 += parseFloat(ups2[p1].value, 10);
          
			
                    document.getElementById('labour_total').value=sum2.toFixed(2);

        
         }


}



/**** code below to calculate discount  ***/

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('dis'+id1).value;
 if(isNaN(fldid))
  {
   alert('enter digit');
  document.getElementById('dis'+id1).value=0;
  val=0;
  }
 



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

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


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

           
             var p1=0;
   var ups2 = document.getElementsByName('tot[]'),sum2 = 0, p1;
    var t2 = document.getElementsByName('typ[]');
   
    for(p1 = ups2.length; p1--;)
             
        if((ups2[p1].value)&&(t2[p1].value=='labour'))
           
            sum2 += parseFloat(ups2[p1].value, 10);
          
			
                    document.getElementById('labour_total').value=sum2.toFixed(2);

        
         }


         }
            

      
   





/***********************************************
* 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;
		} 
		else
		return true;
	}      


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

		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;
     part = document.getElementById("typ"+id1).value;
     
     if(part=='spares')
      {
       stot = document.getElementById("tot"+id1).value;
       sum2+= parseFloat(stot);
      }
      else
      { 
      ltot = document.getElementById("tot"+id1).value;
       sum3+= parseFloat(ltot);
      }
      
    }
   document.getElementById('spare_total').value=sum2.toFixed(2);
    document.getElementById('labour_total').value=sum3.toFixed(2);




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





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

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



        })




    
