$(document).ready(function() {
	var row_u;
	var curr_u;
	var undo_u;

  $("input:text,textarea").focus(function(){
    $(this).css("background-color","#cccccc");
  });
  $("input:text,textarea").blur(function(){
    $(this).css("background-color","#ffffff");
  });

  

$('#vchdate').datepick({
	onClose:function(){
		$('#refno').focus();
		}
	})	

$('#invdate').datepick({
	onClose:function(){
		$('#basic_amt').focus();
		}
	})


$('#vchtype').blur(function(){
			$("#new").trigger('click');
	})
$('#vchno').blur(function(){
	var vchno=$('#vchno').val();
	 var vouchertype    = $("#vchtype").val();
/*	if(vouchertype=='PAYMENT')
	vchno='none';*/
	if($.trim(vchno)!="")
	{
		//jSuccess(vchno+vouchertype);
		$('#vchsave').hide(); 
		  $('#transperent').show();
 $.post("vchdetails_fill.php",{vchno:vchno,vouchertype:vouchertype},function(result){

	$('#vchdetails').html(result); 
	//$('#vchdetails .amt').attr('disabled','disabled');
	//$('#vchdetails .amt').css({'cursor':'not-allowed'});
	$('#comcode').trigger('blur');
	
	inline_edit();
	vchfill_rel();
	focus_on_enter();
	  $('#transperent').hide();
	if($.trim($('#comcode').val())!='')
updatePendingBills($('#comcode').val()+'&mvch='+$('#vchtype').val()+'&account='+$('#account').val());


$.post("misc_fill.php?mode=angst",function(result){
				 			
							var html = "<option selected value=''>--select--</option>";
							$.each(result, function(key,value) {
							//console.log(value.list.desc);
								html += "<option  value='"+value.list.desc+"'>"+value.list.desc+"</option>";
								
							});
							
							$('.angst').html("").append(html);
							
 },'json');//post ends

 });
	}
	
});	
	
$('#update').click(function(){
	//if(!checkFinYear($('#vchdate')))
	//return;
	
	$('#message').html('');
	  $('#transperent').show();
	  
	//$.ajaxSetup({async:false});
	  
 $.post("voucher_crud.php?mode=update",$('#vchreg_det').serialize(),function(result){
	//$.ajaxSetup({async:false});
	  $( "#table_prod_details tbody tr div .prod_save,#table_prod_details tbody tr div .prod_savenew" ).each(function( index ) {
		  
		  			var row = $(this).closest('tr');
					var temp = row.find('.bno').val();
					var amt =  Number(row.find('.amt').val());
					var eid = row.find('.eid').val();
					//if($.trim(temp)!=''&&amt!=0)
					if($.trim(temp)=='')
					temp =row.find('.crbno').val(); 
					
					if($.trim(temp)!=''&&$.trim(eid)=='')
		    		$(this).trigger('click');
		});
	//	$.ajaxSetup({async:true});
	$('#message').html(result); 
   //console.log(result);
	  
	if ($('#vchtype').val() == 'RECEIPT'||$('#vchtype').val() == 'PURCHASE'||$('#vchtype').val() == 'PAYMENT'||$('#vchtype').val() == 'JOURNAL'){
		updatePendingBills($('#comcode').val()+'&mvch='+$('#vchtype').val()+'&account='+$('#account').val()); 
  }  
//$.ajaxSetup({async:true});
$('#transperent').hide();
 });
 
 
 
  
 
});		




$('#vchsave').click(function(){
if ($('#comcode').val() != '' && $('#account').val() != ''){
	//if(!checkFinYear($('#vchdate')))
	//return;
	if ($('#comcode').val() != '')
	$('#message').html('');
	  $('#transperent').show();
 $.post("voucher_crud.php?mode=insert",$('#vchreg_det').serialize(),function(result){
	 
	 var str=result.split('###');
	$('#update').fadeIn(2000);
	$('#vchsave').hide();
	$('#vchno').val(str[0]);
	$('#refno').val(str[1]);
	$('#message').html(str[2]); 
	$('#vchno').trigger('blur');
	//$('#refno').trigger('blur');
	  $('#transperent').hide();
	
  if ($('#vchtype').val() == 'RECEIPT'||$('#vchtype').val() == 'PURCHASE'||$('#vchtype').val() == 'PAYMENT'||$('#vchtype').val() == 'JOURNAL'){
		updatePendingBills($('#comcode').val()+'&mvch='+$('#vchtype').val()+'&account='+$('#account').val()); 
  }
  
 });
}
else{
jError("Unable To save Blank Ledger");
}
});

$('#tds').blur(function(){
	if ($('#tds').val() > 0) {
	$('#tdsamt').val(Math.round((Number($('#amount').val())- Number($('#sortpayment').val()))*Number($('#tds').val())/100));
	}
	$('#totamount').val(Math.round(Number($('#amount').val())- Number($('#sortpayment').val())-Number($('#tdsamt').val())));
})
$('#tdsamt').blur(function(){
	if ($('#tds').val() > 0) {
	$('#tdsamt').val(Math.round((Number($('#amount').val())- Number($('#sortpayment').val()))*Number($('#tds').val())/100));
	}
	$('#totamount').val(Math.round(Number($('#amount').val())- Number($('#sortpayment').val())-Number($('#tdsamt').val())));
})

$('#close').click(function(){
	window.close();
});

$('#new').click(function(){
	//window.location='vch_entry.php';
	
	 var vouchertype    = $("#vchtype").val();
  if(vouchertype=='RECEIPT')
  window.location="vch_entry.php?mode=RECEIPT";
  else  if(vouchertype=='PAYMENT')
  window.location="vch_entry.php?mode=PAYMENT";
  else  if(vouchertype=='PURCHASE')
  window.location="vch_entry.php?mode=PURCHASE";
    else  if(vouchertype=='CREDITNOTE')
  window.location="vch_entry.php?mode=CREDITNOTE";
  else  if(vouchertype=='JOURNAL')
  window.location="vch_entry.php?mode=JOURNAL";
  else  if(vouchertype=='CONTRA')
  window.location="vch_entry.php?mode=CONTRA";
    else  if(vouchertype=='SALES')
 //jSuccess("Please create sales voucher from billing");
 positionedPopup("vch_entry.php?mode=SALES",620);
});

focus_on_enter();
  var newwindow;
  $('#vchledadd').click(function(e) {
   newwindow=window.open('voucherlededit.php?mode='+$('#comcode').val(),'','height=200,width=400');
   
	if (window.focus) {newwindow.focus()}
});


focus_on_enter();
  var newwindow;
  $('#chequep').click(function(e) {
   newwindow=window.open('chequeedit.php?bookid='+$('#vchno').val(),'','height=250,width=650,top=200,left=300');
   
	if (window.focus) {newwindow.focus()}
});


 $('#print').click(function () {
	var vchno=$('#vchno').val();
	$('#transperent').show();
	
	$.post('voucher_export_pdf.php', {p_vchno:vchno,vchtype:$('#vchtype').val(),CmpLogo:$('#CmpLogo').prop('checked'),PrePrint:$('#PrePrint').prop('checked')}, function (html) {
	console.log({p_vchno:vchno,vchtype:$('#vchtype').val(),CmpLogo:$('#CmpLogo').prop('checked'),PrePrint:$('#PrePrint').prop('checked')});
	$('#transperent').hide();
		if($.trim(html)=='')
		window.open("invoice.pdf",'','width=500,height=400');
		else
		jSuccess(html);
	//jSuccess('Error!!');          
	});		
 });//print click ends
 
 $('#basic_amt,#ser_tax,#ser_per,#toll_park,#misamt,#subtotal,#sortpayment').blur(function(e) {
    var basic_amt = Number($('#basic_amt').val());
	var misamt = Number($('#misamt').val());
	var toll_park = Number($('#toll_park').val());
    var sortpayment = Number($('#sortpayment').val());
    var sbc = Number($('#sbc').val());
	var kkc = Number($('#kkc').val());
    var st=$('#ser_per').val();
      
if($('#taxontoll').prop('checked')){
	
	var ser_tax=Math.round((Number(toll_park)+Number(basic_amt))*Number(st)*0.01);
	var taxable=Math.round((Number(toll_park)+Number(basic_amt)));
	}
	else{
		
		var ser_tax=Math.round(Number(basic_amt)*Number(st)*0.01);
		var taxable=Math.round(Number(basic_amt));
		}

	if(Number(st)==4.8){
		var sc=Math.round(ser_tax*0.02);
	var hc=Math.round(ser_tax*0.01);
	}
	else {
		var sc=Math.round(taxable*Number(kkc)*0.01);
	var hc=Math.round(taxable*Number(sbc)*0.01);
		
		}

	
	
	var toll_park = Number($('#toll_park').val());
	$('#ser_tax').val(ser_tax.toFixed(2));
	$('#vsurchg').val(sc.toFixed(2));
	$('#vhecess').val(hc.toFixed(2));
    
if ($("#vchtype").val()=='PURCHASE')
	var amount = Math.round(basic_amt + misamt +  ser_tax + sc + hc + toll_park);
    else
	var amount = Math.round($('#amount').val());

	$('#amount').val(amount);
	$('#tds').trigger('blur');
    $('#subtotal').val(Math.round(amount - sortpayment));
});

$("#pedbilldet").on( "click",'table #checkall', function() {
	
	if($(this).prop('checked'))
	{
		$( "#pedbilldet table input:checkbox:not('#pedbilldet table input:checkbox#checkall')" ).each(function( index ) {
		$(this).prop('checked',true).trigger('click');
		$(this).prop('checked',true);
		console.log('test->'+index);
		});
	}
	else
	{
		$( "#pedbilldet table input:checkbox:not('#pedbilldet table input:checkbox#checkall')" ).each(function( index ) {
		$(this).prop('checked',false).trigger('click');
		$(this).prop('checked',false);
		});
	}
});


$("#pedbilldet").on( "click",'table input:checkbox', function() {
	if($(this).attr('id')=='checkall')
	return;
	var row = $(this).closest('tr');
    var vtype = row.find('.vtype').text();
	var srno = row.find('.pen_srno').text();
	var billno = row.find('.pen_billno').text();
	var total = row.find('.pen_total').text();
		var drcr = row.find('.pen_drcr').text();
	var temp = $('#agnstref').val();
	var subtotal = 0;
	
	if($(this).prop('checked'))
	{
		temp = temp  + srno + ',';	
		
		var clone_row = $('#table_prod_details tr:last').clone();
		var last_row =  $('#table_prod_details tr:last');
		var yyy = last_row.find('.vno').val();
		   $( ".amt" ).each(function() {
		  // $(this).removeAttr('disabled');
         
		   subtotal+=Number($(this).val());
	   });
        
		 var value=Number($('#amount').val())-Number(subtotal); 
		console.log('value='+value);
		// if(Number(total)>Number(value))
		// last_row.find('.amt').val(Number(value).toFixed(2));
		// else
		last_row.find('.vtype').val(vtype);
		last_row.find('.ptype').val('Agnst Ref');	
		 if(drcr=='CR'){
			
		 if((value > total||value==0))
		{
		last_row.find('.cramt').val(total);
		last_row.find('.crbno').val(billno);
		}	
		else if (value > 0)
		{	
		last_row.find('.cramt').val(value);
		last_row.find('.crbno').val(billno);
		}
		
			}	
			else{
				
				if($.trim($('#totamount').val())=='')
		{
		last_row.find('.amt').val(total);
		
		}
		else if((value > total||value==0))
		{
		last_row.find('.amt').val(total);
		
		}	
		else if (value > 0)
		{	
		last_row.find('.amt').val(value);
		
		}
		last_row.find('.bno').val(billno);		
				
				}
		
       
		var amttotal = Number($('#amount').val())+Number(total);
		if(Number($('#totamount').val())==0||$.trim($('#totamount').val())==''){
		  $('.amount').val(amttotal);	
		}

		$('#table_prod_details').append(clone_row);
		$('#comcode').trigger('blur');
		$('#agnstref').val(temp);
		vchamount_chk($('#vchtype').val(),$(this));
	}
	else
	{
		$( "#table_prod_details tr" ).each(function( index ) {
			if($(this).find('.bno').val()==billno||$(this).find('.crbno').val()==billno)
			{	
				var amttotal = Number($('.amount').val())-Number(total);
				if(Number($('#totamount').val())==0||$.trim($('#totamount').val())==''){
					$('.amount').val(amttotal);
				}
				$(this).remove();
				return;
			}
		});
		var req_row = 	$('#table_prod_details tr td').find('.bno');
		$("#agnstref").val($("#agnstref").val().replace(srno+',',""));	
	}
	console.log(vtype);
});

//amount add subtract by editing
//$( "#vchdetails" ).on( "click",'table tbody .amt', function() {
//	jSuccess('jjjj')
//	
//});

//$( "#vchdetails table tbody .amt" ).click(  function() {
//	jSuccess('jjjj')
//	
//});

	$('body').on('click','.prod_save',function(){			//using on function to solve multiple save issue
	//$('.prod_save').click(function(){
			$('#message').hide();
			 $("#table_prod_details tbody tr textarea ,#table_prod_details tbody tr input:text").css("background-color","");	
				$('#table_prod_details tbody textarea ,#table_prod_details tbody input:text').removeAttr('readonly');
	var row= $(this).closest('tr');	
	if(Number(row.find('.amt').val())==0&&Number(row.find('.cramt').val())==0)
		{
			if($('#vchtype').val()=='PURCHASE')
			return;
			jSuccess('Invalid Entry!!');
			row.find('.ptype,.bno,.amt').val('');
			row.find('.prod_savenew').hide();
			return;
			}
			else if(Number(row.find('.amt').val())>Number(row.find('.temp').val())&&$.trim(row.find('.temp').val())!='')
		{
			jSuccess("Invalid Entry!!\n Pending amt ="+Number(row.find('.temp').val()));
			return;
			}
			else
			{
				  $('#transperent').show();
				  //row.find('.amt').removeAttr('disabled').css({'cursor':'not-allowed'});
	 $.post("vchdetails_crud.php?mode=update",row.find('textarea,input:text,select').serialize(),function(html){
		// var str=html.split('###');
		 
		  $('#message').html(html).fadeIn(2000);
			$('#transperent').hide();
	  });
			}
		//$('.amt').attr('disabled','disabled').css({'cursor':'not-allowed'});
		updatePendingBills($('#comcode').val()+'&mvch='+$('#vchtype').val()+'&account='+$('#account').val());
		row.find('input:button').hide();
		$('#inline_edit').val('0');
	});



	$('body').on('click','.prod_savenew',function(){				//using on function to solve multiple save issue
		$('#message').hide();
	
			 $("#table_prod_details tbody tr textarea ,#table_prod_details tbody tr input:text").css("background-color","");
	$('#table_prod_details tbody tr textarea ,#table_prod_details tbody tr input:text').removeAttr('readonly');
	
	var row= $(this).closest('tr');	
//	//jSuccess(Number(row.find('.amt')));
//	if(Number(row.find('.amt').val())==0)
//	{
//		jSuccess('Invalid Entry!!');
//		row.find('.ptype,.bno,.amt').val('');
//		row.find('.prod_savenew').hide();
//		return;
//		}
//		else if(Number(row.find('.amt').val())>Number(row.find('.temp').val())&&$.trim(row.find('.temp').val())!='')
//	{
//		jSuccess("Invalid Entry!!\n Pending amt ="+Number(row.find('.temp').val()));
//		return;
//		}
//		else if(!vchamount_chk($('#vchtype').val()))
//	{
//		jSuccess('Invalid Entry!!');
//		row.find('.ptype,.bno,.amt').val('');
//		row.find('.prod_savenew').hide();
//		return;
//		}
//		else
//		{
			  $('#transperent').show();
			  row.find('.amt').removeAttr('disabled').css({'cursor':'not-allowed'});
	 $.post("vchdetails_crud.php?mode=insert&vchno="+$('#vchno').val()+"&vchtype="+$('#vchtype').val()+"&comcode="+$('#comcode').val(),row.find('textarea,input:text,select').serialize()+'&amount='+Number($('#amount').val()),function(html){
		console.log(html);
		$('#enquiry_details #enquiry_id').trigger('blur');
	 $('#message').html(html).fadeIn(2000);
	 $('#vchno').trigger('blur');
	// $('#transperent').hide();
	  
  });
		//}
		//$('.amt').attr('disabled','disabled').css({'cursor':'not-allowed'});
		updatePendingBills($('#comcode').val()+'&mvch='+$('#vchtype').val()+'&account='+$('#account').val());
		row.find('input:button').hide();
	$('#inline_edit').val('0');

	});
	
	
	$('body').on('click','.delete_voucher',function(){			//using on function to solve multiple save issue
	//$('.delete_voucher').click(function(){
		$('#message').hide();
		var row= $(this).closest('tr');	
		
		
		 
		  var r=confirm("Press OK to Delete");
		if (r==false)
		  {
			
		 return;
		  }
		  
		  
		   $('#transperent').show();
		   row.find('.amt').removeAttr('disabled').css({'cursor':'not-allowed'});
		   
		   var amt = Number(row.find('.amt').val());
		   if(amt>0)
		   {
			//	var headAmt=Number($('#amount').val());
//				var temp = headAmt - amt;
//				$('#amount').val(temp);
					   
			}
			
			var billno=row.find('.bno').val().split('/');
			var srno = billno[1];
			
			$("#agnstref").val($("#agnstref").val().replace(srno+',',""));
		   
		   var temp = row.find('input:text').serialize();
		 $.post("vchdetails_crud.php?mode=delete&vchno="+$('#vchno').val()+"&vchtype="+$('#vchtype').val()+"&comcode="+$('#comcode').val(),row.find('input:text,textarea').serialize(),function(html){
			// var str=html.split('###');
				row.remove();
			  $('#message').html(html).fadeIn(2000);
			   $('#vchno').trigger('blur');
			updatePendingBills($('#comcode').val()+'&mvch='+$('#vchtype').val()+'&account='+$('#account').val());
			  $('#transperent').hide();
		  });
});



$('#totamount').keydown(function(e){
	if(e.which==13)	
	$('#update').focus();
});

$('body').on('change','.cramt',function(){
	
	$(this).data("old", $(this).data("new") || "");
    $(this).data("new", $(this).val());
	
	var oldVal = Number($(this).data("old"));
    var newVal = Number($(this).data("new"));
	
	var row = $(this).closest('tr');
	
	var thisAmt = Number($.trim(row.find('.amt').val()));
	
	var thisVal = Number(newVal);
	
	var thisDed = thisAmt-thisVal;
	
	if(oldVal!=0)
	var oldDed =  thisAmt-oldVal;
	else
	var oldDed = 0;
	
	var currDed = Number($('#sortpayment').val());
	
	
	$('#sortpayment').val(currDed+thisDed-oldDed);
	
	
});


$('body').on('keydown','#_filter_2',function(e){
	if(e.which==13)
	{
		e.preventDefault();
		$('#pedbilldet table tbody tr[filtermatch!="false"] td input:checkbox').focus();
			
	}
	
});

$('body').on('keydown','#pedbilldet table tbody tr td input:checkbox',function(e){
	if(e.which==13)
	{
		e.preventDefault();
		$('#_filter_2').focus();
			
	}
	
});

});
//document ends




function inline_edit()
{
			var tableid='#table_prod_details ';
	$(tableid+'tbody textarea,'+tableid+' tbody input:text,'+tableid+' tbody select').click(function(event){
		if($('#inline_edit').val()==0){
			
			
		$(tableid+' tbody tr input:button').hide();
		
		var row= $(this).closest('tr');
		row_u= $(this).closest('tr');	
		curr_u=$(this);
  		undo_u=  $(row_u).clone(true);
		
		row.find('input:button').show();
			
		}
		else	
		$('#message').html("Please Save the changes made OR press ESC to UNDO").show();
	});
	
		$(tableid+' tbody textarea,'+tableid+' tbody input:text').keyup(function(event){
			if((event.keyCode==67 && event.ctrlKey)||event.ctrlKey||event.keyCode==9)
		 	return;
			if(event.keyCode==222){
			//event.preventdefault();
			  $(this).val($(this).val().substring(0,($(this).val().length-1))+"`");
			  }
			  if($('#inline_edit').val()==0){
			var row= $(this).closest('tr');
			$(tableid+' tbody tr  textarea').attr('readonly','readonly');
			row.find('textarea').removeAttr('readonly');
			row.find('textarea').css('background-color','#E0B99F');
			row.find('input:button').show();
			$('#inline_edit').val('1');
			  }
		});
	}

function vchfill_rel()
{
	
	$(".bno").focus(function(){
	if($('#comcode').val()=='')
	{
	jSuccess('Select Ledger');
	$('#comcode').focus();
	}
	});
	
	$('body').keydown(function(e){
		if(e.keyCode==27)
		{
			/*$('#message').hide();
		$temp=$('#table_pysview').attr('class');
		 // $('#transperent').show();
		$('#'+$temp).trigger('click');
		 // $('#transperent').hide();*/
		 
		 
		 $('#inline_edit').val('0');
		 curr_id=$('#table_pysview').attr('class');
		 row_u.replaceWith(undo_u);
		 undo_u.find('textarea').css('background-color','transparent');
		 undo_u.find('input:button').hide();
		$('#message').hide();
		
		 
		 
		}
		
		
		
		
	});


//	$('.prod_save').click(function(){
//			$('#message').hide();
//			 $("#table_prod_details tbody tr textarea ,#table_prod_details tbody tr input:text").css("background-color","");
//	
//				$('#table_prod_details tbody textarea ,#table_prod_details tbody input:text').removeAttr('readonly');
//	var row= $(this).closest('tr');	
//	if(Number(row.find('.amt').val())==0)
//		{
//			jSuccess('Invalid Entry!!');
//			row.find('.ptype,.bno,.amt').val('');
//			row.find('.prod_savenew').hide();
//			return;
//			}
//			else if(Number(row.find('.amt').val())>Number(row.find('.temp').val())&&$.trim(row.find('.temp').val())!='')
//		{
//			jSuccess("Invalid Entry!!\n Pending amt ="+Number(row.find('.temp').val()));
//			return;
//			}
//			else
//			{
//				  $('#transperent').show();
//				  //row.find('.amt').removeAttr('disabled').css({'cursor':'not-allowed'});
//	 $.post("vchdetails_crud.php?mode=update",row.find('textarea,input:text').serialize(),function(html){
//		// var str=html.split('###');
//		 
//		  $('#message').html(html).fadeIn(2000);
//			$('#transperent').hide();
//	  });
//			}
//		//$('.amt').attr('disabled','disabled').css({'cursor':'not-allowed'});
//		updatePendingBills($('#comcode').val()+'&mvch='+$('#vchtype').val());
//		row.find('input:button').hide();
//		$('#inline_edit').val('0');
//	});

//$('.prod_savenew').click(function(){
//	$('#message').hide();
//	
//			 $("#table_prod_details tbody tr textarea ,#table_prod_details tbody tr input:text").css("background-color","");
//	$('#table_prod_details tbody tr textarea ,#table_prod_details tbody tr input:text').removeAttr('readonly');
//	
//	var row= $(this).closest('tr');	
////	//jSuccess(Number(row.find('.amt')));
////	if(Number(row.find('.amt').val())==0)
////	{
////		jSuccess('Invalid Entry!!');
////		row.find('.ptype,.bno,.amt').val('');
////		row.find('.prod_savenew').hide();
////		return;
////		}
////		else if(Number(row.find('.amt').val())>Number(row.find('.temp').val())&&$.trim(row.find('.temp').val())!='')
////	{
////		jSuccess("Invalid Entry!!\n Pending amt ="+Number(row.find('.temp').val()));
////		return;
////		}
////		else if(!vchamount_chk($('#vchtype').val()))
////	{
////		jSuccess('Invalid Entry!!');
////		row.find('.ptype,.bno,.amt').val('');
////		row.find('.prod_savenew').hide();
////		return;
////		}
////		else
////		{
//			  $('#transperent').show();
//			  row.find('.amt').removeAttr('disabled').css({'cursor':'not-allowed'});
//	 $.post("vchdetails_crud.php?mode=insert&vchno="+$('#vchno').val()+"&vchtype="+$('#vchtype').val()+"&comcode="+$('#comcode').val(),row.find('textarea,input:text').serialize()+'&amount='+Number($('#amount').val()),function(html){
//		
//		//$('#enquiry_details #enquiry_id').trigger('blur');
//	 $('#message').html(html).fadeIn(2000);
//	 //$('#vchno').trigger('blur');
//	  
//  });
//		//}
//		//$('.amt').attr('disabled','disabled').css({'cursor':'not-allowed'});
//		updatePendingBills($('#comcode').val()+'&mvch='+$('#vchtype').val());
//		row.find('input:button').hide();
//	$('#inline_edit').val('0');
//
//});


//$('.delete_voucher').click(function(){
//		$('#message').hide();
//var row= $(this).closest('tr');	
//
//
// 
//  var r=confirm("Press OK to Delete");
//if (r==false)
//  {
//	
// return;
//  }
//   $('#transperent').show();
//   row.find('.amt').removeAttr('disabled').css({'cursor':'not-allowed'});
//   var temp = row.find('input:text').serialize();
// $.post("vchdetails_crud.php?mode=delete&vchno="+$('#vchno').val()+"&vchtype="+$('#vchtype').val()+"&comcode="+$('#comcode').val(),row.find('input:text').serialize(),function(html){
//	// var str=html.split('###');
//	 	row.remove();
//	  $('#message').html(html).fadeIn(2000);
//	updatePendingBills($('#comcode').val()+'&mvch='+$('#vchtype').val());
//	  $('#transperent').hide();
//  });
//	
//	
//});
	
// to display total amount
$('.amt').blur(function(amt){
	
	vchamount_chk($('#vchtype').val(),$(this))
});



	}


function vchamount_chk(type,amtfield)
{
	var row=amtfield.closest('tr');
		    var total = 0;
			var counter = 0;
    $('.amt').each(function(i){
        price = $.trim($(this).val());
		   var row= $(this).closest('tr');
		   var billno = $.trim(row.find('.bno').val());
			  if (!isNaN(price)&&row.find('.ptype').val()!='TDS') 
			  total += Number(price);
			   if (billno!='') 
			  counter++;
			  
		  });
		if(type=='RECEIPT' ||'PAYMENT' || 'JOURNAL')
		{
				
			
			//var iniAmt=row.find('.temp').val();
			var iniAmt = (Number($('#amount').val())+Number($('#tdsamt').val())+Number($('#sortpayment').val())); //Total Amount and TDS to check bill amounts
			var currAmt=amtfield.val();
			if((currAmt>iniAmt)&&!isNaN(currAmt))
			{
			  jSuccess("Amount entered exceeds(or is invalid) from bill amount");	
			  amtfield.val(iniAmt).focus();
			  return false;
			}
			
		}
	
	if(type=='RECEIPT')
	{
	//if(total>(Number($('#amount').val())+Number($('#tdsamt').val())+Number($('#sortpayment').val())))
//	{
//	jSuccess('Invalid Entry!!');
//	amtfield.val('0').focus();
//	return false;
//	}	
	
	  if(total!=(Number($('#amount').val())+Number($('#tdsamt').val())+Number($('#sortpayment').val())))
		  $('#chkamount').css({'color':'red'});
	  else
		  $('#chkamount').css({'color':'black'});
	}
	
	if(type=='PURCHASE'||type=='PAYMENT'||type=='CREDITNOTE')
	{
	if(total>(Number($('#amount').val())))
	{
	jSuccess('Amount Mismatch!!');
	}	
	
	if(total!=(Number($('#amount').val())))
	$('#chkamount').css({'color':'red'});
	else
	$('#chkamount').css({'color':'black'});
	}
	
	$('#chkamount').html('Total Amt:'+total.toFixed(2) + '| Total rows:'+counter);	
	return true;
	/*else if(type=='PURCHASE')
	$('#amount').val(total);*/
	}//vchamount_chk ENDS

function focus_on_enter(){
	
//for focusing on next field by pressing enter		
var altDown = false;
var n=0;  
$select=null;
					   
//$('input:text:first:not(.from)').focus();
    
$('input:text,textarea,select,input:button:not(#vchledadd)').keydown(function(e)
    {
		$select=$('input:text,textarea,select,input:button:not(#vchledadd)');
		  n = $select.length;
	
	if (e.which == 13&&!$(this).is('textarea')) 
   { 
	//Enter key
     e.preventDefault(); //to skip default behavior of the enter key
     var nextIndex = $select.index(this)+1;
     if(nextIndex < n)
       $select[nextIndex].focus();
     else
     {
       $select[nextIndex-1].blur();
     }
   }
		
		
        if (e.keyCode == 18&&$(this).is('textarea')) {
			altDown = true;
		}
    }).keyup(function(e) {
   if (e.which == 13&&altDown) 
   { 
   altDown = false;
   
   //Enter key
     e.preventDefault(); //to skip default behavior of the enter key
     var nextIndex = $select.index(this)+1;
     if(nextIndex < n)
       $select[nextIndex].focus();
     else
     {
       $select[nextIndex-1].blur();
     }
   }
});


$('input:button:not(#vchledadd)').keydown(function(e){
	if(e.which==13)
	$(this).trigger('click');
	});	
	
	return;
}

var winPop;



function positionedPopup(url,width,height){

	

	 if(winPop&&!winPop.closed&&winPop.document.location.href!=url){  //checks to see if window is open  

    winPop.close();  

  } 

 winPop =window.open (url,"mywindow","menubar=1,resizable=1,width="+width+",height="+height+",left=250,top=80");

  if(winPop&&winPop.document.location.href==url)

winPop.focus();



}