//-------------------------------------------------------------------------------
//                               U T I L I T I E S
//-------------------------------------------------------------------------------


// Dateformat messages
var msg001 = 'Wrong or incomplete ';
var msg002 = 'Use 4 figures for year ';
var msg003 = 'Invalid ';
var msg004 = 'Wrong month in ';
var msg005 = 'Wrong day in ';
var msg020 = "date";

/////////////////////////////////////////////////////////////////////////////////
// Check if date is correct
function checkDate (objDag, objMaand, objJaar, strDatumNaam)
{
	if ( isEmpty(objDag.value) && isEmpty(objMaand.value) && isEmpty(objJaar.value) )
		return 1;

	var dag		= objDag.value * 1;
	var maand	= objMaand.value * 1;
	var jaar	= objJaar.value * 1;

	if (isNaN(dag) || (dag < 1) || (dag > 31))
	{
		errorField(objDag, msg005 + strDatumNaam + '!');
		return 0;
	}

	if (isNaN(maand) || (maand < 1) || (maand > 12)) 
	{
		errorField(objMaand, msg004 + strDatumNaam + '!');
		return 0;
	}

	if (isNaN(jaar) || jaar < 1000)
	{
		errorField(objJaar, msg002 + strDatumNaam + '!');
		return 0;
	}

	var gb = new Date (jaar, maand-1, dag);
	if (gb.getDate() != dag || gb.getMonth() != (maand-1) || gb.getFullYear() != jaar)
	{
		errorField(objDag, msg003 + strDatumNaam + '!');
		return 0;
	}
	return 1;
}
		
/////////////////////////////////////////////////////////////////////////////////
// Check alphanumeric (A-Z,a-z,0-9,_ or space) value
var strAlpaNum = " _abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@$.,+";

function isAlpaNum(strValue)
{
	for (var i=0; i < strValue.length; i++)
	{
		if (strAlpaNum.indexOf(strValue.substring(i,i+1)) < 0)
			return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
// Check number value
function isNum(strValue)
{
	return !isNaN(strValue * 1);
}

/////////////////////////////////////////////////////////////////////////////////
// Check integer (0-9) value
function isInt(strValue)
{
	strValue = trim(strValue);
	for (var i=0; i < strValue.length; i++)
	{
		var ch = strValue.substring(i,i+1);
		if ("0123456789".indexOf (ch) < 0)
			return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
// Check a particular radiobutton
function setRadioCheck(theForm, strRadioName, strValue)
{
	for (var i=0; i < theForm.length; i++)
	{
		var elem = theForm.elements[i];
		if ( (elem.type == "radio") && (elem.name == strRadioName) && (elem.value == strValue) )
			elem.checked = true;
	}
}

/////////////////////////////////////////////////////////////////////////////////
// Get value of a particular checked radiobutton
function getRadioChecked(objRadioGroup)
{
	if (objRadioGroup.length == undefined) { // check if only 1 radiobutton in group
		if (objRadioGroup.checked)
			return objRadioGroup.value;
		else
			return "";
	}

	for (var i=0; i < objRadioGroup.length; i++)
	{
		var elem = objRadioGroup[i];
		if ( elem.checked )
			return elem.value;
	}
	return "";
}

/////////////////////////////////////////////////////////////////////////////////
// Select option if value match 
function selOption (objSelect, strMatch)
{
	for (var i=0; i < objSelect.options.length; i++)
	{
		var elem = objSelect.options[i];
		if ( elem.value ==  strMatch)
			elem.selected = true;
	}
}

function getOption (objSelect)
{
	var pos = objSelect.selectedIndex;
	if (pos < 0) return "";
	return objSelect.options[pos].value;
}

/////////////////////////////////////////////////////////////////////////////////
// Reset options in selectbox
function resetOptions (objSelect)
{
	for (var i=0; i < objSelect.options.length; i++) {
		var option = objSelect.options[i];
		option.selected = option.defaultSelected;
	}
}

/////////////////////////////////////////////////////////////////////////////////
// Check if a checkbox is checked
function anyCheckbox(objForm, grpname) {
	var box, i = 0, boxarray = objForm[grpname];
	if (!boxarray.length) return boxarray.checked; // there is only 1 checkbox
	for (i=0; box = boxarray[i]; i++) if (box.checked) return true;
	return false;
}

/////////////////////////////////////////////////////////////////////////////////
function isEmpty(theString)
{
	for (var i=0; i < theString.length; i++)
	{
		var ch = theString.substring(i,i+1);
		if (ch != " " && ch != "\t" && ch != "\n" && ch != "\r")
			return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
// ltrim: removes blanks from head of string
function ltrim(theString)
{
	var ch;
	while (theString.length > 0) {
		ch = theString.substring(0,1);
		if (ch != " " && ch != "\t" && ch != "\n" && ch != "\r") break;
		theString = theString.substring(1, theString.length);
	}
	return theString;
}

/////////////////////////////////////////////////////////////////////////////////
// rtrim: removes blanks from tail of string
function rtrim(theString)
{
	var ch;
	while (theString.length > 0) {
		ch = theString.substring(theString.length-1,theString.length);
		if (ch != " " && ch != "\t" && ch != "\n" && ch != "\r") break;
		theString = theString.substring(0, theString.length-1);
	}
	return theString;
}

/////////////////////////////////////////////////////////////////////////////////
// trim: removes blanks from both head and tail of string
function trim(theString)
{
	return ltrim (rtrim (theString));
}

/////////////////////////////////////////////////////////////////////////////////
// lpad: left pad a string with a character
function lpad(theString, theChar, Max)
{
	while (theString.length < Max)
		theString = theChar + theString;
	return theString;
}

/////////////////////////////////////////////////////////////////////////////////
// Display an error and set focus to formfield in error
function errorField (objElem, txtMsg)
{
	if (objElem != null)
		objElem.focus();
	alert (txtMsg);
}

/////////////////////////////////////////////////////////////////////////////////
// Check mandatory field
function checkMandatory(field, errmsg)
{
	if (isEmpty(field.value))
	{
		errorField(field, errmsg);
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
// Move one selected item in a selectbox to another selectbox
function moveBox2Box(source, dest)
{
	var pos = source.selectedIndex;
	if (pos < 0) return;
	var group_id = source.options[pos].value;
	var group_naam = source.options[pos].text;
	// remove from sourcebox
	source.options[pos] = null;
	if (pos < source.options.length) 
		source.selectedIndex = pos;
	else
		source.selectedIndex = source.options.length - 1;
	// add to destinationbox
	var newOption = new Option(group_naam, group_id, false, false);
	var length = dest.options.length;
	dest.options[length] = newOption;
	if (navigator.appName == "Netscape") history.go(0);
	dest.selectedIndex = length;
}

/////////////////////////////////////////////////////////////////////////////////
// Exclude items from one selectbox in another selectbox
function exclBox2Box(source, dest)
{
	for (i=0; i < source.options.length; i++)
	{
		for (j=0; j < dest.options.length; j++)
		{
			if (source.options[i].value == dest.options[j].value)
				dest.options[j] = null;
		}	
	}
}


/////////////////////////////////////////////////////////////////////////////////
// Return values of a selectbox as a space-separated string
function box2string(source)
{
	var values = ""
	var length = source.options.length;
	for (var i=0; i < length; i++)
	{
		values += " " + source.options[i].value
		//source.options[i].selected = true
	}
	values = values.substring (1, values.length); // remove first space
	return values;
}


/////////////////////////////////////////////////////////////////////////////////
// Get/Set Cookies
function setCookie(name,value) {
	var now = new Date();
	var then = new Date(now.getTime()+31536000000);
	document.cookie = name+"="+escape(value)+"; expires="+then.toGMTString()+"; path=/";
}

function getCookie(name) {
	var cookie = " "+document.cookie;
	var search = " "+name+"=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset);
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset,end));
		}
	}
	return(setStr);
}

/////////////////////////////////////////////////////////////////////////////////
// Popup a window
function popup(theURL,winName,features) 
{
	var hWin = window.open(theURL, winName, features);
	if (hWin) hWin.focus();
}

/////////////////////////////////////////////////////////////////////////////////
// Used to check and count characters in a big text field (textarea)
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	else 
		countfield.value = maxlimit - field.value.length;
}


/////////////////////////////////////////////////////////////////////////////////
// Disable all buttons of a form (names must start with 'bt'
var disabled_page = false;

function disableButtons(f) {
	disabled_page = true;
	for (i=0; i<f.elements.length; i++) {
		var e = f.elements[i];
		if (e.type == "button" && e.name.indexOf("bt") == 0) e.disabled=1;
		if (e.type == "submit" && e.name.indexOf("bt") == 0) e.disabled=1;
		if (e.type == "reset" && e.name.indexOf("bt") == 0) e.disabled=1;
	}
}

/////////////////////////////////////////////////////////////////////////////////
// Check if correct date
function isDate (year, month, day) {
	// month argument must be in the range 1 - 12
	month = month - 1; // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);
	
	if ( (year == tempDate.getFullYear()) &&
		(month == tempDate.getMonth()) &&
		(day == tempDate.getDate()) )
		return true;
	else
		return false
}
		

/////////////////////////////////////////////////////////////////////////////////
// Check if correct datestring (may be empty)
function checkDateStr(field, message) {
	field.value = trim(field.value);
	if (field.value == "") return true;

	var myDate=field.value.split("/");
	day = myDate[0];
	month = myDate[1];
	year = myDate[2];

	if (isDate(year, month, day)) {
		return true;	
	} else {
		errorField(field, message);
		return false;	
	}	
}

/////////////////////////////////////////////////////////////////////////////////
// Check if a form-element has changed (*hg* TODO: radiobuttons)
function isChanged(obj) {
	if (obj.type.indexOf("select") == 0) {
		for (var i=0; i < obj.options.length; i++) {
			var option = obj.options[i];
			if (option.selected != option.defaultSelected) return true;
		}
	} else if (obj.type == "checkbox") {
		return (obj.checked != obj.defaultChecked);
	} else {
		return (obj.value != obj.defaultValue);
	}
	return false;
}

/////////////////////////////////////////////////////////////////////////////////
// Check if anything (except for the trigger object) on a form has changed
function isFormChanged(f, trigger) {
	for (i=0; i<f.elements.length; i++) {
		var e = f.elements(i);
		if (!e.name) continue;
		if (trigger && e == trigger) continue;
		if (isChanged(e)) return true;
	}
	return false;
}

////////////////////////////////////////////////////////////////////////////////
function confirmation(theUrl,msg)
{
	var where_to = confirm(msg);
	if (where_to == true)
	{
		window.location=theUrl;
	}
}