/*
Title:		Global JavaScript functions
File: 		js/globals.js
Version: 	v2.00
Author:		Digitalization
Contact:	info@digitalization.nl
*/

//Set the style of an object
function setStyle(obj_id, style, value) {
	if (document.getElementById(obj_id)) {
		document.getElementById(obj_id).style[style] = value;
	}
	else alert('Object with ID "'+obj_id+'" does not exist. Could not set style.');
}

//Focus on an object
function setFocus(obj_id) {
	if (document.getElementById(obj_id)) {
		document.getElementById(obj_id).focus();
	}
	else alert('Object with ID "'+obj_id+'" does not exist. Could not set focus.');
}

//Function to change the inner HTML of an object.
function innerHTML(obj_id, txt) {
 	if (document.getElementById(obj_id)) {
		document.getElementById(obj_id).innerHTML = txt;
	}
	else alert('Object with ID "'+obj_id+'" does not exist. Could not set inner HTML.');
}

//Function to set the visibility of an object
function setVisibility(obj_id, visible) {
	if (document.getElementById(obj_id)) {
		var element = document.getElementById(obj_id);
		if (visible == true) {
			element.style.display = '';
		}
		else {
			element.style.display = 'none';
		}
	}
	else alert('Object with ID "'+obj_id+'" does not exist. Could not set visibility.');
}

//Toggle visibility
function toggleVisibility(obj_id, set_display) {
	if (set_display == null) set_display = '';
	if (document.getElementById(obj_id)) {
		var element = document.getElementById(obj_id);
		element.style.display = (element.style.display != 'none' ? 'none' : set_display );
	}
	else alert('Object with ID "'+obj_id+'" does not exist. Could not toggle visibility.');
}

//Function to make an array
function makeArray(n) {
	this.length = n;
	for(var i = 1; i <= n; i++)
	this[i] = 0;
	return this;
}

//As the PHP function in_array()
function in_array(needle, haystack) {
	for (i = 0; i < haystack.length; i++) {
		if (needle == haystack[i]) return true;
	}
	return false;
}

//Check if a number is an integer
function isInt(number) {
    var modulus = number % 1;
    if (modulus == 0) 	return true;
    else 				return false;
}

//Force the value of an input field to be a number
function force_number(obj) {
	if(obj.value.indexOf(',')) 				obj.value = obj.value.replace(',','.');
	if(isNaN(obj.value) || obj.value == '') obj.value = 0;
	if(obj.value != 0 && obj.value < 0.001) obj.value = 0;
	if(obj.value.indexOf('.')) 				obj.value = obj.value.replace('.',',');
}

//Force the value of an input field to be a positive integer
function force_positive_integer(obj, default_value) {
 	var value = parseInt(obj.value);
	if(isNaN(value) || obj.value == '') 	obj.value = default_value;
	else if (value < 0)						obj.value = value - (2*value);
	else 									obj.value = value;
}

//As the parseAmount() function defined in globals.php
function parseAmount(amount, sign) {
	
	amount = amount / 100;
	var amount_str = amount + '';
	
	pos = amount_str.indexOf('.');
	if (pos > -1) {
		var ending = amount_str.substring(pos+1);
		if (ending.length == 1) {
			amount_str += '0';
		}
	}
	else {
		amount_str += '.00';
	}
	
	amount_str = amount_str.replace('.',',');
	
	if (sign == true) 	return '&euro;'.amount_str;
	else				return amount_str;
}

//Function to color the background of an object
function colorBg(obj_id) {
	if (document.getElementById(obj_id)) {
		setStyle(obj_id, 'background', '#ffaa88');
		document.getElementById(obj_id).onkeypress = function() {
			setStyle(obj_id, 'background', '#ffffff');
		}
		document.getElementById(obj_id).onblur = function() {
			setStyle(obj_id, 'background', '#ffffff');
		}
	}
	else alert('Object with ID "'+obj_id+'" does not exist. Could not color the background.');
}

//Function to color the background of an object if empty
function colorBgIfEmpty(obj_id) {
	if (document.getElementById(obj_id)) {	
		if(!document.getElementById(obj_id).value) colorBg(obj_id);
	}
	else alert('Object with ID "'+obj_id+'" does not exist. Could not apply colorBgIfEmpty.');
}

//Function to expand/contract an element with the given contents (must be named expand_div_XX)
var showing = new Array();
function evalExpand(id, contents) {
	
	if (showing[id] != 1) {
		document.getElementById('expand_div_'+id).innerHTML = contents;
		showing[id] = 1;
	} else {
		document.getElementById('expand_div_'+id).innerHTML = '';
		showing[id] = 0;
	}
}

//Functions to mark/un-mark all checkboxes of a certain identifier on a page
function markAll(identifier) {
	var i = 1;
	
	while (document.getElementById(identifier+i)) {
		document.getElementById(identifier+i).checked = true;
		i++;	
	}
}

function markNone(identifier) {
	var i = 1;
	
	while (document.getElementById(identifier+i)) {
		document.getElementById(identifier+i).checked = false;
		i++;	
	}			
}

var marked_all = false;
function markCheckboxes(identifier) {
	if (marked_all == true) {
		markNone(identifier);
		marked_all = false;
	}
	else {
		markAll(identifier);
		marked_all = true;
	}
}
