// =============================================================================
// FORM functions
// =============================================================================

// Test whether value of field is a date (in dd-mm-yyyy or dd/mm/yyyy format) 
//
function isDate( oField ) {
	var day, mo, yr;
	var entry = oField.value;
    var re = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;

    if (re.test(entry)) {
		var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
		var delim1 = entry.indexOf(delimChar);
		var delim2 = entry.lastIndexOf(delimChar);
		day = parseInt(entry.substring(0, delim1), 10);
		mo = parseInt(entry.substring(delim1+1, delim2), 10);
		yr = parseInt(entry.substring(delim2+1), 10);
		var testDate = new Date(yr, mo-1, day);
		if (testDate.getDate() == day && testDate.getMonth() + 1 == mo && testDate.getFullYear( ) == yr) {
			return true;
		}
	}
	return false;
}


// Test whether value of field is a valid email string (simple @ format)
//
function isEmail( oField ) {
	var email = oField.value;
	var re1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\,)/;
	var re2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

	return ( !re1.test(email) && re2.test(email) );
}


// v1.0 - 14.10.2004 -  Kemal Djakman - VVBS Web Services
// Test whether a radio or checkbox element is checked
// Parameter 'oField' must be an object of a form
//
function isChecked( oField ) {
	return ((oField.type=='radio' || oField.type=='checkbox') && oField.checked);
}


// v1.0 - 14.10.2004 -  Kemal Djakman - VVBS Web Services
// Count the number of checked elements of a form
// Parameter 'oForm' must be an object form
// Parameter 'sFieldname' must be a string containing the fieldname to be checked
//
function CountChecked( oForm, sFieldname ) {

	var F = oForm[sFieldname];

	if (typeof F == 'object') {
		var cnt = 0;
		if (F.length) {
			for (var i=0; i<F.length; i++) {
				if (isChecked(F[i])) cnt++;
			}
		} else {
			if (isChecked(F)) cnt++;
		}
		return cnt;
	}
}


// v1.0 -  4.10.2003 -  Kemal Djakman - VVBS Web Services
//	Test whether an input text, textarea or select field is empty
//	Also trim the input string from leading and trailing whitespace
//	Expect parameter f to be a field element of a form (object)
// v1.1 - 14.10.2004 -  Kemal Djakman - VVBS Web Services
//	Add radio and checkbox handling
// Note:
//  In MSIE, when <OPTION>text</OPTION> is used (without the VALUE attribute),
//  retrieving the value of SELECT object with javascript will fail ( Although
//  the FORM submission does send the text as a value to the server ). 
//
function isEmpty( f ) {

	if (f.type == 'text' || f.type == 'textarea' || f.type == 'password' || f.type == 'file' ) {
		return (!f.value.replace(/^\s+|\s+$/g, ''));
	} else if (f.type == 'select-one' || f.type == 'select-multiple') {
		return (!f.value);
	} else if (f.type == 'checkbox' || f.type=='radio') {
		return (!f.checked);
	} else if (f.length) {
		var cnt = 0;
		for (var i=0; i<f.length; i++) {
			if (isChecked(f[i])) cnt++;
		}
		return (cnt==0);
	}
	return false;
}


// v1.0 -  4.10.2003 -  Kemal Djakman - VVBS Web Services
//	Test whether none of the specified fields in form are empty
//	(test is limited to text, textarea and select fields)
//	1st parameter is an form object
//	2nd parameter is an array of field names
//  This function is usually used in the onsubmit attribute of a form:
//		onsubmit="return ValidateRequired(this, ['name1', 'name2', ...])"
// v1.1 - 14.10.2004 -  Kemal Djakman - VVBS Web Services
//	Add radio and checkbox handling
//
function ValidateRequired( oForm, aFieldnames ) {
	var sErrmsg = '';

	for (var i=0; i<aFieldnames.length; i++) {
		var F = oForm[aFieldnames[i]];
		if (typeof F == 'object' && isEmpty(F)) {
			if (!F.type && F.length) { F = F[0]; }
			sErrmsg += ('- ' + (F.title ? F.title : F.name) + "\n");
		}
	}

	if (sErrmsg) {
		alert('Error: foute of niet-ingevuld verplichte velden:\n' + sErrmsg);
		return false;
	}
	return true;
}


function ValidateEmail( oForm, aFieldnames ) {
	var sErrmsg = '';

	for (var i=0; i<aFieldnames.length; i++) {
		var F = oForm[aFieldnames[i]];
		if (typeof F == 'object' && !isEmpty(F) && !isEmail(F)) {
			sErrmsg += ('- ' + (F.title ? F.title : F.name) + "\n");
		}
	}

	if (sErrmsg) {
		alert('Error: foute email velden:\n' + sErrmsg);
		return false;
	}
	return true;
}


function ValidateDate( oForm, aFieldnames ) {
	var sErrmsg = '';

	for (var i=0; i<aFieldnames.length; i++) {
		var F = oForm[aFieldnames[i]];
		if (typeof F == 'object' && !isEmpty(F) && !isDate(F)) {
			sErrmsg += ('- ' + (F.title ? F.title : F.name) + "\n");
		}
	}

	if (sErrmsg) {
		alert('Error: foute datum velden:\n' + sErrmsg);
		return false;
	}
	return true;
}


function ValidateNumeric( oForm ) {
	var sErrmsg = '';
	var a = ValidateNumeric.arguments;
	
	if ( ((a.length - 4) < 0) || (((a.length - 1) % 3) != 0) ) { alert('[ValidateNumeric] Error: wrong number of arguments'); return false; }

	for (var i=1; i<(a.length-2); i+=3) {
		var fieldname = a[i];
		var minval = a[i+1];
		var maxval = a[i+2];

		var F = oForm[fieldname];

		if (typeof F == 'object') {
			if (F.type == 'select-one') {
				if (F.value) {
					var value = parseInt(F.value, 10);
				} else if (!F.options[F.selectedIndex].value && F.options[F.selectedIndex].text) {
					var value = parseInt(F.options[F.selectedIndex].text, 10);
				}
			} else if (F.type == 'text') {
				var value = parseInt(F.value, 10);
			}

			if (isNaN(value)) {
				sErrmsg += ('- ' + (F.title ? F.title : F.name) + ": is geen getal\n");
			} else if (!isNaN(minval) && value < minval) {
				sErrmsg += ('- ' + (F.title ? F.title : F.name) + ": is minder dan " + minval + "\n");
			} else if (!isNaN(maxval) && value > maxval) {
				sErrmsg += ('- ' + (F.title ? F.title : F.name) + ": is meer dan " + maxval + "\n");
			}
		}
	}

	if (sErrmsg) {
		alert('Error: foute numerieke velden:\n' + sErrmsg);
		return false;
	}
	return true;
}


function ConfirmDeletion( oForm, sFieldname ) {

	var cnt = CountChecked(oForm, sFieldname);

	if (cnt == 0) {
		alert("Selecteer eerst een of meer items die verwijderd moeten worden.");
		return false;
	} else if (cnt == 1) {
		return confirm("Weet u zeker dat u deze item wilt verwijderen?");
	} else {
		return confirm("Weet u zeker dat u " + cnt + " items wilt verwijderen?");
	}
}


// v1.0 - 14.10.2004 -  Kemal Djakman - VVBS Web Services
// Flip the state of all checkboxes with a certain name to all on or all off
//
function FlipCheckboxes( oForm, sFieldname ) {
	var F = oForm[sFieldname];

	if (typeof F == 'object') {
		status = !status;
		if (F.length) {
			for (var i=0; i<F.length; i++) {
				if (F[i].type=='checkbox') F[i].checked = status;
			}
		} else if (F.type=='checkbox') {
			F.checked = status;
		}
	}
}
FlipCheckboxes.status = false; // 'static' function variable 


function CheckboxConfirmChecked( oField, text ) {
	if (isChecked(oField)) {
		if (!confirm(text)) {
			oField.checked = false;
		}
	}
}



// =============================================================================
// MISC FUNCTIONS
// =============================================================================

// Reload page with optional arguments
//
function jumpTo( url, criteria_name, criteria_value ) {
	if (criteria_name) {
		document.location.href = url + '&' + criteria_name + '=' + criteria_value;
	} else {
		document.location.href = url;
	}

}


// Open a minimal popup window
//
function MinWinPopup( URL, width, height ) {

	var WinFeatures = "width=" + width + ",height=" + height + ",scrollbars,resizable";
	mw = window.open(URL, 'MinWin', WinFeatures);
	mw.focus();
	return mw;
}