// JavaScript Document
function checkTheDate()
{
	var letters=/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
	
	if(document.getElementById('std').value!='')
	{
		if(letters.test(document.getElementById('std').value)==false)  
		{
			alert('Start Date must be in the given Format');  
			document.getElementById('std').focus();  
			return false;  
		}
	}
	/*else
	{
		alert('Please Enter Start Date');  
		document.getElementById('std').focus();  
		return false; 
	}*/
	
	if(document.getElementById('edd').value!='')
	{
		if(letters.test(document.getElementById('edd').value)==false)  
		{
			alert('End Date must be in the given Format');  
			document.getElementById('edd').focus();  
			return false;  
		}
	}
	/*else	
	{
		alert('Please Enter End Date');  
		document.getElementById('edd').focus();  
		return false; 
	}*/
	
	var where_to= confirm(" Do you really want to Submit the selected items?");
	if (where_to== true)
	{
		document.acc_dat.submit();
	}
	else
	{
		return false;
	}
}
	

function reload_po_lst()
{	
	var party_id = document.getElementById('co_cod').value;
	
	window.location = 'collection_add.php?party_id=' + party_id ;
}


function clr_fm()
{
	window.location = 'collection_add.php';
}
function form_rest() //this is for reset the forms
{
	var where_to= confirm("Do you really want to Reset the Data?");
	if (where_to== true)
	{
		window.location.reload();
	}
	else
	{
	 	return false;	
	}
}

function pay_type_hid(vl,part_id) // hide bill part when changing the bill type
{
	var check_fl = document.getElementById('check_fl').value.trim();						
	if(document.getElementById('check_fl').value=='1')
	{
		document.getElementById('amt').disabled = false;		
		
	}
	else if(document.getElementById('check_fl').value=='0')
	{
		document.getElementById('ad_amt').disabled = false;
	}
	
	if(vl=='cash')
	{
		$('#cheq_det').css('display','none');
		document.getElementById('rece_div').style.display='block';
		document.getElementById('cheq_div').style.display='none';
		document.getElementById('bank_div').style.display='none';
		if(check_fl==1)
		{
			document.getElementById('amt').value = '0';
		}
		else if(check_fl==0)
		{
			document.getElementById('ad_amt').value = '0';
		}
		amt_calc(1);
	}
	else if(vl=='cheque')
	{
		var count_chk_sel = $('#count_chk_sel').val();
		if(count_chk_sel<=1){
			var gURL = 'ajax/get_cheque_AJAX.php?part_id='+part_id+'&flag=1';
			if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc
				xmlhttp=new XMLHttpRequest();
				if (xmlhttp.overrideMimeType) {
					xmlhttp.overrideMimeType('text/xml');
				}
				xmlhttp.onreadystatechange=loadmsg;
				xmlhttp.open("GET", gURL, true);
				xmlhttp.send(null);
			} else if (window.ActiveXObject) { //IE 
				xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
				if (xmlhttp) {
					xmlhttp.onreadystatechange=loadmsg;
					xmlhttp.open('GET', gURL, false);
					xmlhttp.send();
				}
			}

			function loadmsg() {
				if (xmlhttp.readyState==4) {
					if (xmlhttp.status==200) {
						var responseText = xmlhttp.responseText.split("###");
						var emp_name = responseText[4].trim();
						document.getElementById('rec').value = responseText[0];
						document.getElementById('chq_no').value = responseText[1];
						document.getElementById('bn_name').value = responseText[2];
						document.getElementById('bran_name').value = responseText[3];
						document.getElementById('chk_col_id').value = responseText[6];
						if(check_fl==1){
							document.getElementById('amt').value = responseText[5].trim();
							$('#amt_bt').css('display','block');
						}else if(check_fl==0){
							document.getElementById('ad_amt').value = responseText[5].trim();
						}
						$( "#search_emp #chk_rec_chzn" ).addClass( "chzn-container-active" );
						$('#search_emp #uniform-chk_rec span').text(emp_name);
						$('#search_emp #chk_rec_chzn .chzn-single span').text(emp_name);
						var chzn = $('#count_chk_rec').val();
						var i=1;
						while( i <= chzn)
						{
							var sle = $('#search_emp #chk_rec_chzn .chzn-drop .chzn-results #chk_rec_chzn_o_'+i).text();
							if(sle == emp_name)
							{
								$('#search_emp #chk_rec_chzn .chzn-drop .chzn-results #chk_rec_chzn_o_'+i).addClass('result-selected');
							}
							i++;
						}
					}
				}
			}
			document.getElementById('cheq_div').style.display='block';
		}else{
			$('#cheq_det').css('display','block');
			document.getElementById('cheq_div').style.display='none';
		}
		document.getElementById('rece_div').style.display='none';
		document.getElementById('bank_div').style.display='none';
		//alert(document.getElementById('chk_re').value);
	}
	else if(vl=='neft')
	{
		$('#cheq_det').css('display','none');
		document.getElementById('rece_div').style.display='none';
		document.getElementById('cheq_div').style.display='none';
		document.getElementById('bank_div').style.display='block';
		if(check_fl==1)
		{
			document.getElementById('amt').value = '0';
		}
		else if(check_fl==0)
		{
			document.getElementById('ad_amt').value = '0';
		}
		amt_calc(1);
	}
}
function cheque_select(id)
{
	if(!(id=='')){
			$('#cheq_div').css('display','block');
			var gURL = 'ajax/get_cheque_AJAX.php?_id='+id+'&flag=2';
			if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc
				xmlhttp=new XMLHttpRequest();
				if (xmlhttp.overrideMimeType) {
					xmlhttp.overrideMimeType('text/xml');
				}
				xmlhttp.onreadystatechange=loadmsg;
				xmlhttp.open("GET", gURL, true);
				xmlhttp.send(null);
			} else if (window.ActiveXObject) { //IE 
				xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
				if (xmlhttp) {
					xmlhttp.onreadystatechange=loadmsg;
					xmlhttp.open('GET', gURL, false);
					xmlhttp.send();
				}
			}

			function loadmsg() {
				if (xmlhttp.readyState==4) {
					if (xmlhttp.status==200) {
						var check_fl = document.getElementById('check_fl').value.trim();
						var responseText = xmlhttp.responseText.split("###");
						var emp_name = responseText[4].trim();
						document.getElementById('rec').value = responseText[0];
						document.getElementById('chq_no').value = responseText[1];
						document.getElementById('bn_name').value = responseText[2];
						document.getElementById('bran_name').value = responseText[3];
						document.getElementById('chk_col_id').value = responseText[6];
						if(check_fl==1){
							document.getElementById('amt').value = responseText[5].trim();
							$('#amt_bt').css('display','block');
						}else if(check_fl==0){
							document.getElementById('ad_amt').value = responseText[5].trim();
						}
						$( "#search_emp #chk_rec_chzn" ).addClass( "chzn-container-active" );
						$('#search_emp #uniform-chk_rec span').text(emp_name);
						$('#search_emp #chk_rec_chzn .chzn-single span').text(emp_name);
						var chzn = $('#count_chk_rec').val();
						var i=1;
						while( i <= chzn)
						{
							var sle = $('#search_emp #chk_rec_chzn .chzn-drop .chzn-results #chk_rec_chzn_o_'+i).text();
							if(sle == emp_name)
							{
								$('#search_emp #chk_rec_chzn .chzn-drop .chzn-results #chk_rec_chzn_o_'+i).addClass('result-selected');
							}
							i++;
						}
					}
				}
			}
		}
}
function inputOnlyNumbers(evt){  
    var e = window.event || evt; // for trans-browser compatibility  
    var charCode = e.which || e.keyCode;  
   if ((charCode > 47 && charCode < 58) || charCode == 8 || charCode == 45 || charCode == 46){  
        return true;  
    }  
    return false;  
}



function checkTheBox()
{
		//pod,cash,cheque,neft,e_name,chk_rec,chq_no,bn_name,bran_name,bn_name_ne,amt
		var letters=/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
		if(document.getElementById('pod').value=='')
		{
			alert('Please Enter the Payment Date');  
			document.getElementById('pod').focus();  
			return false; 
		}
		else if(letters.test(document.getElementById('pod').value)==false)  
		{
			alert('Payment Date must be in the given Format');  
			document.getElementById('pod').focus();  
			return false;  
		}
		
		if(document.getElementById('cash').checked==true)
		{
			if(document.getElementById('e_name').value=='')
			{
				alert('Please Select the Cash Receiver');
				window.scrollTo(50,70);
				document.getElementById('e_name').focus();  
				return false; 
			}
			
		}		
		else if(document.getElementById('cheque').checked==true)
		{
			if(document.getElementById('rec').value=='')
			{
				alert('Please Select the Cheque Receiver');  
				window.scrollTo(50,70);
				document.getElementById('rec').focus();  
				return false; 
			}
			var letters = /^[0-9]{6}$/;  
			if(document.getElementById('chq_no').value=='')
			{
				alert('Please Enter the Cheque No');  
				document.getElementById('chq_no').focus();  
				return false; 
			}
			else if(letters.test(document.getElementById('chq_no').value)==false)  
			{
				alert('Cheque No Should have only 6 Digits');  
				document.getElementById('chq_no').focus();  
				return false;  
			}
			
			var letters = /^[\s]+$/; 
			if(document.getElementById('bn_name').value=='')
			{
				alert('Please Enter the Bank Name');  
				document.getElementById('bn_name').focus();  
				return false; 
			}
			else if(letters.test(document.getElementById('bn_name').value)==true)  
			{
				alert('Bank Name Should Not be Empty');  
				document.getElementById('bn_name').focus();  
				return false;  
			}
			
			/*var letters = /^[\s]+$/; 
			if(document.getElementById('bran_name').value=='')
			{
				alert('Please Enter the Branch Name');  
				document.getElementById('bran_name').focus();  
				return false; 
			}
			else if(letters.test(document.getElementById('bran_name').value)==true)  
			{
				alert('Branch Name Should Not be Empty');  
				document.getElementById('bran_name').focus();  
				return false;  
			}*/
		}
		else if(document.getElementById('neft').checked==true)
		{
			var letters = /^[\s]+$/; 
			if(document.getElementById('bn_name_ne').value=='')
			{
				alert('Please Enter the Bank Name');  
				document.getElementById('bn_name_ne').focus();  
				return false; 
			}
			else if(letters.test(document.getElementById('bn_name_ne').value)==true)  
			{
				alert('Bank Name Should Not be Empty');  
				document.getElementById('bn_name_ne').focus();  
				return false;  
			}
		}
		else
		{
			alert('Please Select the Payment Type');  
			window.scrollTo(50,90);
			document.getElementById('cash').focus();  
			return false;
		}		
		if(document.getElementById('check_fl').value=='0')
		{
			var letters=/^\d+(?:\.\d+)?$/;
			if(document.getElementById('ad_amt').value=='')
			{
				alert('Please Enter the Advance Amount');  
				document.getElementById('ad_amt').focus();  
				return false;  
			}
			else if(letters.test(document.getElementById('ad_amt').value)==false)  
			{
				alert('Advance Amount Should have only Digits');  
				document.getElementById('ad_amt').focus();  
				return false;  
			}
		}		
		if(document.getElementById('check_fl').value=='1')
		{			
			var letters=/^\d+(?:\.\d+)?$/;
			if(document.getElementById('amt').value=='')
			{
				alert('Please Enter the Amount');  
				document.getElementById('amt').focus();  
				return false;  
			}
			else if(letters.test(document.getElementById('amt').value)==false)  
			{
				alert('Amount Should have only Digits');  
				document.getElementById('amt').focus();  
				return false;  
			}
		}
		
		var where_to= confirm("Do you really want to Submit the Data?");
		if (where_to== true)
		{
			document.checklist.submit();
		}
		else
		{
			return false;	
		}		
}

//tot_rows,pen_amt,col_amt,col_st,cl_amt_vl
function amt_calc(ids)
{
	var amt 	 = parseFloat(document.getElementById('amt').value);  	
	var tot_pen_amt 	 = parseFloat(document.getElementById('tot_pen_amt').value);  	 
	if(ids==2)
	{
		if(document.getElementById('disc_amt').value!='')
		{
			var disc_amt 	 = parseFloat(document.getElementById('disc_amt').value);  	 
			amt  = amt + disc_amt;
		}
	}
	
	if(document.getElementById('amt').value!='')//this is for checking the amt field empty
	{
		document.getElementById('dis_div').style.display='block'; //this is for show the discont dive
		document.getElementById('disc_amt').disabled =false;
	}
	
	//alert(amt)
	var rece_amt = amt;
	var tot_rows = document.getElementById('tot_rows').value;  	
	var tot_col_amt =0;
	for(i=1;i<tot_rows;i++)
	{
		var pen_amt = parseFloat(document.getElementById('pen_amt'+i).value);  	 
		if(Math.ceil(pen_amt) <= Math.ceil(amt)) //total given payment checking with each pending amt
		{
			document.getElementById('col_amt'+i).innerHTML = formatDollar(pen_amt);
			document.getElementById('cl_amt_vl'+i).value = pen_amt;
			document.getElementById('col_st'+i).innerHTML = "Closed";
			document.getElementById('pen_sts'+i).value = "Closed";
			amt = amt - pen_amt;
			tot_col_amt += pen_amt;//this is for calculating total collection amt
		}
		else if(Math.ceil(pen_amt) >= Math.ceil(amt))
		{
			/*alert(Math.ceil(pen_amt));
			alert(Math.ceil(amt));
			alert("aaa");*/
			document.getElementById('col_amt'+i).innerHTML = formatDollar(amt);
			document.getElementById('cl_amt_vl'+i).value = amt;
			document.getElementById('col_st'+i).innerHTML = "Pending";
			document.getElementById('pen_sts'+i).value = "Pending";
			tot_col_amt += amt; //this is for calculating total collection amt
			amt = amt - amt;
			
		}
		
	}
	
	document.getElementById('tot_col_amt').innerHTML = formatDollar(tot_col_amt);
	document.getElementById('tot_cl_amt').value = tot_col_amt;
	var bl_amt = parseFloat(tot_pen_amt) - parseFloat(tot_col_amt);
	document.getElementById('bal_amt').innerHTML = formatDollar(bl_amt);
	document.getElementById('bl_amt').value = bl_amt;
	if(tot_col_amt < rece_amt)
	{
		var tmpv = parseFloat(rece_amt) - parseFloat(tot_col_amt);
		document.getElementById('cred_bl_span').style.display='block';
		document.getElementById('cred_bl').innerHTML = formatDollar(tmpv);
		document.getElementById('cre_bl_amt').value = tmpv;
	}
	else
	{
		document.getElementById('cred_bl_span').style.display='none';
		document.getElementById('cred_bl').innerHTML = '0.00';
		document.getElementById('cre_bl_amt').value ='';
	}
}

function amt_calc_clr(ids) //this is for clearing amount calculation
{
	if(document.getElementById(ids).value!='')
	{
		document.getElementById(ids+'_bt').style.display='block';
	}
	else
	{
		document.getElementById(ids+'_bt').style.display='none';
	}

	var tot_rows = document.getElementById('tot_rows').value;  	
	
	for(i=1;i<tot_rows;i++)
	{
		document.getElementById('col_amt'+i).innerHTML = "0.00";
		document.getElementById('cl_amt_vl'+i).value =0.00;
		document.getElementById('col_st'+i).innerHTML = "Pending";
		document.getElementById('pen_sts'+i).value = "Pending";
	}
	document.getElementById('tot_col_amt').innerHTML = "0.00";
}

function formatDollar(num) {
    var p = num.toFixed(2).split(".");
    return p[0].split("").reverse().reduce(function(acc, num, i, orig) {
        return  num + (i && !(i % 3) ? "," : "") + acc;
    }, "") + "." + p[1];
}
function choose_emp(id){
	document.getElementById('rec').value = id;
}