var splitIndex = 0;
var splitArray = new Array();

function showHide_extraline() {
	if (document.getElementById("extralines_header").style.display == 'none') {
		document.getElementById("extralines_header").style.display = 'block';
		document.getElementById("extralines_text").style.display = 'block';
		document.getElementById("extralines_submit").style.display = 'block';
	} else {
		document.getElementById("extralines_header").style.display = 'none';
		document.getElementById("extralines_text").style.display = 'none';
		document.getElementById("extralines_submit").style.display = 'none';
	}
}

function addPension() {
	teller = document.getElementById('pension').getElementsByTagName('div').length;
	if (teller>0){
		pensionDiv = document.getElementById('pension');
		newDiv = document.getElementById("pension_nr"+teller).cloneNode(true);
		newDiv.id="pension_nr"+(teller+1);
	    pensionDiv.appendChild(newDiv);
	    teller +=1;
	}
}

function resizeImage() {
	var logo = document.getElementById('logo');
	if (logo != undefined) {
		breedte = logo.clientWidth;
		if (breedte > 400) {
			logo.width = 400;	
		}
	}
}

function check_holidaySeason() {
	var field = document.getElementById('holidaySeason');
	if (field.checked) {
		document.getElementById('holidaySeasonRate').innerHTML="factor normale uren&nbsp;<input type='text' name='factorNormal' class='formtext'/></td>";
	} else {
		document.getElementById('holidaySeasonRate').innerHTML="";		
	}
}

// Splits string at text
function splits(string,text) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return;
    if (i == -1) {
        splitArray[splitIndex++] = string;
        return;
    }

    splitArray[splitIndex++] = string.substring(0,i);
    
    if (i+txtLength < strLength)
        splits(string.substring(i+txtLength,strLength),text);

    return;
}

function showHide_row(resourceid, ratecount){
	if (document.getElementById("row"+resourceid+"_nummer0").style.display == 'none') {
	   	for (i=0; i < ratecount; i=i+1) {
       		document.getElementById("row"+resourceid+"_nummer"+i).style.display = '';
       	}		
	} else {
	   	for (i=0; i < ratecount; i=i+1) {
       		document.getElementById("row"+resourceid+"_nummer"+i).style.display = 'none';
       	}				
	}
}

// Splits string and sets index
function split(string,text) {
    splitArray = string.split(text);
    splitIndex = splitArray.length;
}

//Checks if the correct time format is used
function checkTime(time) {
	var match = /^((24[:.]00)|(([01]?\d|20|21|22|23)[:.][012345]\d)?)$/.test(time);
	if (!match) {
		alert("U heeft een ongeldige tijd ingevoerd, tijd moet als uu:mm of als uu.mm worden ingevoerd.");
	}	
	return match;
}

//Check of het total aantal uren formaat correct gebruikt is.
function checkTotalTime(time) {
	var match = /^((24[.,]00)|(([01]?\d|20|21|22|23)([.,](\d|\d\d))?)?)$/.test(time);
	if (!match) {
		alert("U heeft een ongeldige totaaltijd ingevoerd, de totaaltijd moet als decimaal getal ingevoerd worden. Voorbeeld: 3:30 uur moet als 3,5 uur ingevoerd worden. U mag maximaal 2 cijfers achter de komma gebruiken.");
	}	
	return match;
}

function calculateTotal(fieldname, nr) {
	//Prevent older browsers from getting any further.
	if(!document.getElementById) return;

    var total = 0;
    for (i=0; i < 7; i=i+1) {
        // Set the id
        id = fieldname + nr + i;
    	
    	// Get the value of the field with the id
    	if (field = document.getElementById(id)) {
	    	value = field.value
	    	
	        // If the value != '' then convert to float and add to total
	    	if (value != '') {
	
	    		// If the value contains a ',' then substitute it for a '.'
	    		splitArray = value.split(',');
	    		if (splitArray.length > 1) {
	    			value = splitArray[0] + '.' + splitArray[1];
	    		}
	        	
	            hours = parseFloat(value);
	        	total += hours;
	    	}
	    }
    }

    // Set the total field of the row to the total amount
	id = fieldname + 'Total' + nr;
	field = document.getElementById(id);
    field.value = total;
}

//Calculates the total time between 2 start and endtimes
function calculateTime(nr) {
	//Prevent older browsers from getting any further.
	if(!document.getElementById) return;
	var id = 'startTimeA' + nr;
	var field = document.getElementById(id);

	splitArray = field.value.split(/[:.]/);
	splitIndex = splitArray.length;

    for (var i=splitIndex-1, j=60, startTimeA=0; i>=0; i=i-1, j=j*60)
        startTimeA += splitArray[i]*j - 0;

	id = 'endTimeA' + nr;
	field = document.getElementById(id);
	splitArray = field.value.split(/[:.]/);
	splitIndex = splitArray.length;

    for (var i=splitIndex-1, j=60, endTimeA=0; i>=0; i=i-1, j=j*60)
        endTimeA += splitArray[i]*j - 0;

	id = 'startTimeB' + nr;
	field = document.getElementById(id);
	splitArray = field.value.split(/[:.]/);
	splitIndex = splitArray.length;

    for (var i=splitIndex-1, j=60, startTimeB=0; i>=0; i=i-1, j=j*60)
        startTimeB += splitArray[i]*j - 0;

	id = 'endTimeB' + nr;
	field = document.getElementById(id);
	splitArray = field.value.split(/[:.]/);
	splitIndex = splitArray.length;

    for (var i=splitIndex-1, j=60, endTimeB=0; i>=0; i=i-1, j=j*60)
        endTimeB += splitArray[i]*j - 0;

 	difference=endTimeA-startTimeA+endTimeB-startTimeB;
 	if(difference >= 0) {
		total = difference/3600;
        
        // Round the numbers to .00 / .25 / .50 / .75
        mod = total%1;
        div = total - mod;
        if (mod > 0.875) {
            total = div + 1;
        } else if (mod > 0.625) {
            total = div + 0.75;
        } else if (mod > 0.375) {
            total = div + 0.50;
        } else if (mod > 0.125) {
            total = div + 0.25;
        } else {
            total = div;
        }
		id = 'total' + nr;
		field = document.getElementById(id);
	    field.value = total;
	 }
}

function roundTotal(total, nr) {
		total = total.replace(/,/, ".");
		mod = total%1;
        div = total - mod;
        if (mod > 0.875) {
            total = div + 1;
        } else if (mod > 0.625) {
            total = div + 0.75;
        } else if (mod > 0.375) {
            total = div + 0.50;
        } else if (mod > 0.125) {
            total = div + 0.25;
        } else {
            total = div;
        }
		id = 'total' + nr;
		field = document.getElementById(id);
	    field.value = total;
}


//(un)checks all checkboxes in a form if the checkfield is (un)checked
function checkAll(form, checkfield) {
	count = form.elements.length;
    
    if(checkfield.checked == 1) {
    	for (i=1; i < count; i++) {
	    	form.elements[i].checked = 1; 
		}
	} else {
		for (i=1; i < count; i++) {
			form.elements[i].checked = 0;
		}
	}
}

//Autocomplete from a select box
function autoComplete (field, select, property, forcematch) {
	var found = false;
	for (var i = 0; i < select.options.length; i++) {
	if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
		found=true; break;
		}
	}
	if (found) { select.selectedIndex = i; }
	else { select.selectedIndex = -1; }
	if (field.createTextRange) {
		if (forcematch && !found) {
			field.value=field.value.substring(0,field.value.length-1); 
			return;
			}
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
			}
		}
	}
}

//Add more fields dynamically.
function addField(area,field,limit) {
    //Prevent older browsers from getting any further.
	if(!document.getElementById) return;
	var field_area = document.getElementById(area);
	//Get all the input fields in the given area.
	var all_inputs = field_area.getElementsByTagName("input");

	//Find the count of the last element of the list. It will be in the format '<field><number>'. If the 
	//field given in the argument is 'friend_' the last id will be 'friend_4'.

	var last_item = all_inputs.length - 1;
	if (last_item > 0) {
		var last = all_inputs[last_item].id;
		var count = Number(last.split("_")[1]) + 1;
	} else {
		var count = 1;
	}
	
	//If the maximum number of elements have been reached, exit the function.
	//		If the given limit is lower than 0, infinite number of fields can be created.
	if(count > limit && limit > 0) return;

 	var newDiv=document.createElement("DIV");
	newDiv.innerHTML += "<table class='noborder'><tr><td class='formhead' style='width: 125px;'>Inlener "+count+"</td><td><input type='text' class='formtext' style='width: 200px;' id='"+(field+count)+"' name='"+(field)+(count)+"' />*</td></tr></table>";
	field_area.appendChild(newDiv);
}

// Set the value of a hidden form element
function setHidden(element, waarde) 
{
	element = document.getElementById(element);
	if (!(element)) return false;
	element.value = waarde;
	return true;
}

function createEvents()
{
	// Hover highlights for TR elements
	nodes = document.getElementsByTagName("tr");	
	for (var i=0; i<nodes.length; i++) {
		node = nodes[i];
		if(node.className == "even") {
			node.onmouseover=function(){this.className="hoverover";};
			node.onmouseout=function(){this.className="even";};
		} else if(node.className == "odd") {
			node.onmouseover=function(){this.className="hoverover";};
			node.onmouseout=function(){this.className="odd";};
		}
	}	
	
	// Autofocus on login field
	nodes = document.getElementsByTagName("input");
	for (var i=0; i<nodes.length; i++) {
		node = nodes[i];
	
		if ((node.name == "login") && (node.type != 'hidden')) {
			node.focus();
		} 
	}	

	return true;
}
