var invoices='';
var runBalTotal=0;

function checkhasinvoices() {
	if (document.getElementById('paidinvoices').value=='') {
		var blnConfirm=confirm("You have not selected any invoices. The earliest invoices available will be paid. Click OK to confirm or Cancel to go back and select the invoices to pay and amend the amount");
		if (blnConfirm==false) {   
			return false;  
		} 
	}
}

function checkvalue(val) {
	if (val>0) {
		document.getElementById('pay_button').style.display="block";
		document.getElementById('pay_button2').style.display="none";
	} else {
		document.getElementById('pay_button2').style.display="block";
		document.getElementById('pay_button').style.display="none";
	}
}

function updatePay(invoice,value) {
	
	if(document.getElementById('paidinvoices')) {
		var invoicestext;
		invoicestext=document.getElementById('paidinvoices').value;
		//alert(invoicestext.indexOf(invoice));
		
		if (document.getElementById('invoice_'+invoice).checked) {
			invoicestext=invoicestext+invoice+' ';
		} else {
			invoicestext=invoicestext.replace(invoice+' ','');
		}
		
		document.getElementById('selectedinvoices').innerHTML=invoicestext;
		document.getElementById('paidinvoices').value=invoicestext;
	}
	
	if(document.getElementById('paidinvoicesprice')) {
		var invoicesprice;
		invoicesprice=document.getElementById('paidinvoicesprice').value;
		
		if (document.getElementById('invoice_'+invoice).checked) {
			invoicesprice=parseFloat(parseFloat(invoicesprice)+parseFloat(value.replace(',','')));
		} else {
			invoicesprice=parseFloat(parseFloat(invoicesprice)-parseFloat(value.replace(',','')));
		}
		
		//document.getElementById('totaltopay').innerHTML=invoicesprice.toFixed(2);
		document.getElementById('paidinvoicesprice').value=invoicesprice.toFixed(2);
		if (invoicesprice>0) {
			document.getElementById('pay_button').style.display="block";
			document.getElementById('pay_button2').style.display="none";
		}
		else {
			document.getElementById('pay_button2').style.display="block";
			document.getElementById('pay_button').style.display="none";
		}
	}   
	
	
}

function appendInvoices(invoice) {
	invoices=invoices+invoice+'||';
}

function checkAllBoxes() {
	var invoicesSplit = invoices.split('||');
	
	if(document.getElementById('paidinvoicesprice').value='0') {
	
		document.getElementById('paidinvoices').value='';
		document.getElementById('paidinvoicesprice').value='0';

		for(i = 0; i < eval(invoicesSplit.length-1); i++){
			document.getElementById('invoice_'+invoicesSplit[i]).checked=true;
			updatePay(invoicesSplit[i],document.getElementById('invoice_'+invoicesSplit[i]).value);
		}
	}
}

function unCheckAllBoxes() {
	
	var invoicesSplit = invoices.split('||');
	
	if(document.getElementById('paidinvoicesprice').value!='0') {

		for(i = 0; i < eval(invoicesSplit.length-1); i++){
			if (document.getElementById('invoice_'+invoicesSplit[i]).checked==true) {
				document.getElementById('invoice_'+invoicesSplit[i]).checked=false;
				updatePay(invoicesSplit[i],document.getElementById('invoice_'+invoicesSplit[i]).value);
			}
		}
		
	}
	
}

function runTotal(spanid,balance) {
	runBalTotal=runBalTotal+parseFloat(balance.replace(',',''));
	
	if(spanid!='') {
		document.getElementById(spanid).innerHTML=runBalTotal.toFixed(2);
	}
}
