//facturen genereren
var aantalfac = 1;


function moreBills(){
	if (aantalfac<100) {
		aantalfac++;
		var row = document.getElementById('fac'+aantalfac);
		row.style.display = 'block';
	} 
}

function lessBills(){
	
	if (aantalfac>1) {
		var row = document.getElementById('fac'+aantalfac);
		var rRef = document.getElementById('ref'+aantalfac);
		var rDatum = document.getElementById('datum'+aantalfac);
		var rBedrag = document.getElementById('bedrag'+aantalfac);
		
		rRef.value='';
		rDatum.value='';
		rBedrag.value='';
		row.style.display = 'none';
		aantalfac--;
	} 
}

function currencyFormat(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	
	if (e.keyCode) {
		whichCode = e.keyCode;
	} else if (e.charCode) {
		whichCode = e.charCode;
	} else {
		whichCode = e.which;
	}
	
//	var whichCode = (window.Event) ?  e.keyCode : e.which;
	
	
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--) {
	if (j == 3) {
	aux2 += milSep;
	j = 0;
	}
	aux2 += aux.charAt(i);
	j++;
	}
	fld.value = '';
	len2 = aux2.length;
	for (i = len2 - 1; i >= 0; i--)
	fld.value += aux2.charAt(i);
	fld.value += decSep + aux.substr(len - 2, len);
	}
	//calculate(fld.value);
return false;
}



function submitForm(form) {
	var kill = false;
	var mes ='';
	for(var l=1;l<=25;l++) {
		var bedr = eval(document.getElementById('bedrag'+l));
		var dtm = eval(document.getElementById('datum'+l));
		var ref = eval(document.getElementById('ref'+l));
		
		
		
		if (bedr.value!='' && (dtm.value=='' || ref.value=='')) {mes +='- factuur '+l+' is niet volledig ingevuld.\n';kill=true;}
		else if (dtm.value!='' && (bedr.value=='' || ref.value=='')) {mes +='- factuur '+l+' is niet volledig ingevuld.\n';kill=true;}
		else if (ref.value!='' && (dtm.value=='' || bedr.value=='')) {mes +='- factuur '+l+' is niet volledig ingevuld.\n';kill=true;}
	}
	

	if (kill) {
		alert('U bent een aantal velden vergeten:\n' +mes);return false;
	} else if (document.getElementById('bedrag1').value=='' || document.getElementById('datum1').value=='' || document.getElementById('ref1').value=='') {
		alert('U moet minimaal factuur 1 invullen');return false;
	} else if(confirm('Opdracht verzenden aan Boiten, Luhrs & Van der Lubbe?')){
		return true;
	} else {
		return false;
	}

}

function calculate(waarde){ 
	var x = 1;
	var a = document.getElementById('bedrag1').value;
	var b = document.getElementById('bedrag2').value;
	//reformat all entered values.
	
	n1 = removeCommas(a);
	n2 = removeCommas(b);
	//calculate total amount
	amount = n1+n2;
	//alert(amount);
	//convert back to string
	amount = String(amount);

	
	//alert(amount);
	//check of 0 at end is not removed
	cpos = amount.lastIndexOf('.')
	if(cpos!=-1) {
		dec3 = amount.substr((cpos+2),3);
		if (!dec3) {
			//add 1 zero
			amount = amount +'0';
		}	
	} else {
		//2 zero's
		amount = amount +'.00'
	}
	//remove more than 2 decimals if exist
	
	
	
	//document.getElementById('billfooter').innerHTML = reformat(amount, '.', ',');
	
}

function removeCommas(aNum) {
	//remove any commas
	if (aNum!='') {
		len = aNum.length;
		//get comma position
		cpos = aNum.lastIndexOf(',');
		//store decimals
		dec = aNum.substr((cpos+1),3);
		//alert(dec);
		
		//remove decimals from string
		aNum=aNum.substr(0,cpos);

		//remove comma's
		aNum=aNum.replace(/,/g,"");
		
		//remove dots
		aNum=aNum.replace(/(\.)/g,"");
	
		//remove any spaces
		aNum=aNum.replace(/\s/g,"");
		
		//add decimals
		aNum = aNum+'.'+dec;
		
		//make a number out of it
		aNum = parseFloat(aNum);
		
		//add decimal
		
		
		
		//get last comma occurrence
		
		
		return aNum;
	} else {
		return 0;
	}
}


function reformat(money, milSep, decSep) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';

	len = money.length;
	for(i = 0; i < len; i++)
	if ((money.charAt(i) != '0') && (money.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(money.charAt(i))!=-1) aux += money.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) money = '';
	if (len == 1) money = '0'+ decSep + '0' + aux;
	if (len == 2) money = '0'+ decSep + aux;
	if (len > 2) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--) {
	if (j == 3) {
	aux2 += milSep;
	j = 0;
	}
	aux2 += aux.charAt(i);
	j++;
	}
	money = '';
	len2 = aux2.length;
	for (i = len2 - 1; i >= 0; i--)
	money += aux2.charAt(i);
	money += decSep + aux.substr(len - 2, len);
	}
	
return money;
}






function resetRow(nr) {
	var row1 = eval (document.getElementById('bedrag'+nr));
	var row2 = eval (document.getElementById('datum'+nr));
	var row3 = eval (document.getElementById('ref'+nr));
	row1.value='';row2.value='';row3.value='';


}

function nextPage(begin) {
	document.getElementById('page').value=begin;
	document.getElementById('PageSwitch').submit();

}

function centerPopUp( url, name, width, height, scrollbars ) { 
 
	if( scrollbars == null ) scrollbars = "0" 
 
	str  = ""; 
	str += "resizable=1,"; 
	str += "scrollbars=" + scrollbars + ","; 
	str += "width=" + width + ","; 
	str += "height=" + height + ","; 
 
	if ( window.screen ) { 
		var ah = screen.availHeight - 30; 
		var aw = screen.availWidth - 10; 
 
		var xc = ( aw - width ) / 2; 
		var yc = ( ah - height ) / 2; 
 
		str += ",left=" + xc + ",screenX=" + xc; 
		str += ",top=" + yc + ",screenY=" + yc; 
	} 
	window.open( url, name, str ); 
} 

function editOpdracht(oid) {
	form= eval(document.getElementById('bewerkOpdracht'+oid));
	form.submit();
}

function incOpdracht(oid) {
	form= eval(document.getElementById('incOpdracht'+oid));
	form.submit();
}



