// General functions for checking and manipulating input values in form fields
//
// Status: version 1.1
// Changed by: Jan-Willem Vaandrager (july 16, 2003)
//				functions isVisible and setVisible changed for use with Netscape
// Changed by: Boen Kie Hwan and Paul Hagg (march 18, 2003)
//				function formChanged
// Changed by: Jan-Willem Vaandrager (october 1, 2001)
// Changed by: Ingrid Stulp and Robert Guitink (november 14, 2001)

function isNumberLargerThan(inputObject,min) {
	return !isNaN(inputObject.value) && inputObject.value > min;
}
function isNumberLargerEqualThan(inputObject,min) {
	return !isNaN(inputObject.value) && inputObject.value >= min;
}

function isNumberSmallerThan(inputObject,max) {
	return !isNaN(inputObject.value) && inputObject.value < max;
}
function isNumberSmallerEqualThan(inputObject,max) {
	return !isNaN(inputObject.value) && inputObject.value <= max;
}

function isNumberBetween(inputObject,min,max) {
	return !isNaN(inputObject.value) && min <= inputObject.value && inputObject.value <= max;
}

function isFloat(inputObject) {
	return !isNaN(inputObject.value);
}

function isInteger(inputObject) {
	match = /^-?[0-9]*$/;
	return match.test(inputObject.value);
}

// is a time in 'uummss' or 'uu:mm:ss' format,
// where ":" may also be any other non-alphanumeric character
function isTime(inputObj) {
	var input = inputObj.value;
	var hour;
	var minute;
	var second;
	match1 = /^[0-9][0-9]\W[0-9][0-9]$/;
	match2 = /^[0-9][0-9]\W[0-9][0-9]\W[0-9][0-9]$/;
	match3 = /^[0-9][0-9][0-9][0-9]$/;
	match4 = /^[0-9][0-9][0-9][0-9][0-9][0-9]$/;

	match5 = /^[0-9]\W[0-9][0-9]$/;
	match6 = /^[0-9]\W[0-9][0-9]\W[0-9][0-9]$/;
	if (match5.test(input) || match6.test(input)) {
		input = "0" + input;
	}

	if (match1.test(input)) {
		hour = input.substring(0,2);
		minute = input.substring(3,5);
		second = "00";
	} else if (match2.test(input)) {
			hour = input.substring(0,2);
			minute = input.substring(3,5);
			second = input.substring(6,8);
	} else if (match3.test(input)) {
			hour = input.substring(0,2);
			minute = input.substring(2,4);
			second = "00";
	} else if (match4.test(input)) {
			hour = input.substring(0,2);
			minute = input.substring(2,4);
			second = input.substring(4,6);
	} else {
		return false;
	}

	if (hour > 23) {return false;}
	if (minute > 59) {return false;}
	if (second > 59) {return false;}
	inputObj.value = hour + ":" + minute + ":" + second;
	return true;
}

// is a date. The format parameter should be either:
// "dd-MM-yyyy" -> accepts input formats ddMMyyyy or d(d)-M(M)-yyyy,
// "yyyy-MM-dd" -> accepts input formats yyyyMMdd or yyyy-M(M)-d(d),
// where '-' may also be any other non-alphanumerical character
function isDate(inputObj, format) {
	var f;
	if (format == "dd-MM-yyyy"){f=1;}
	else{if (format == "yyyy-MM-dd"){f=2;}else {return false;}}
	var input = inputObj.value;
	var day;
	var month;
	var year;
	match1 = /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
	match2 = /^[0-9][0-9]?\W[0-9][0-9]?\W[0-9][0-9][0-9][0-9]$/;
	match3 = /^[0-9][0-9][0-9][0-9]\W[0-9][0-9]?\W[0-9][0-9]?$/;
	match4 = /^[0-9][0-9][0-9][0-9][0-9][0-9]$/;
	match5 = /^[0-9][0-9]?\W[0-9][0-9]?\W[0-9][0-9]$/;
	if (match1.test(input)) {
		if (f == 1){
			day = input.substring(0,2);
			month = input.substring(2,4);
			year = input.substring(4,8);
		}
		if (f == 2){
			day = input.substring(6,8);
			month = input.substring(4,6);
			year = input.substring(0,4);
		}
	}else if ((match2.test(input) && f==1) || (match3.test(input) && f==2)) {
			sep = /\W/;
			var sep1, sep2;
			var i = 0;
			for(i=0;i<input.length;i++){
				if (sep.test(input.charAt(i))){sep1=i; break;}
				}
			for(var j=i+1;j<input.length;j++){
				if (sep.test(input.charAt(j))){sep2=j; break;}
				}
			if (f == 1){
				day = input.substring(0,sep1);
				month = input.substring(sep1+1,sep2);
				year = input.substring(sep2+1,input.length);
			}
			if (f == 2){
				year = input.substring(0,sep1);
				month = input.substring(sep1+1,sep2);
				day = input.substring(sep2+1,input.length);
			}
	}else if (match4.test(input)) {
		if (f == 1){
			day = input.substring(0,2);
			month = input.substring(2,4);
			year = "20" + input.substring(4,6);
		}
	}else if (match5.test(input)) {
			sep = /\W/;
			var sep1, sep2;
			var i = 0;
			for(i=0;i<input.length;i++){
				if (sep.test(input.charAt(i))){sep1=i; break;}
				}
			for(var j=i+1;j<input.length;j++){
				if (sep.test(input.charAt(j))){sep2=j; break;}
				}
			if (f == 1){
				day = input.substring(0,sep1);
				month = input.substring(sep1+1,sep2);
				year = "20" + input.substring(sep2+1,input.length);
			}
	}else {return false;}

	//if (year < 1900) {return false;}
	if (month < 1 || month > 12) {return false;}
	var daymax;
	if (month==4 || month==6 || month==9 || month==11) {daymax = 30;}
	else {
		if (month==2) {
			if ((year%4==0 && year%100!=0) || year%400==0) {daymax = 29}
			else {daymax = 28;}
			}
		else {daymax = 31;}
	}
	if (day < 1 || day > daymax) {return false;}
	if (day.length == 1){day = "0" + day;}
	if (month.length == 1){month = "0" + month;}
	if (f == 1){inputObj.value = day + "-" + month + "-" + year;}
	if (f == 2){inputObj.value = year + "-" + month + "-" + day;}
	return true;
}

// Returns the current date as isDate(obj) would have filled it in it's input field
function currentDate() {
	// Get the current date&time
	var now   = new Date();

	// Get the day darts
	var day   = ''+now.getDate();
	var month = ''+now.getMonth();
	var year  = ''+now.getYear();

	// Adjust the day parts
	if (day.length == 1) {day = '0'+day;}
	if (month.length == 1) {month = '0'+month;}
	if (year < 100) {year = '19'+year;}

	// Return the current date
	return day + '-' + month + '-' + year;
}

// Removes white space from both ends of a string.
function trim(inputObject) {
	var input = inputObject.value;
	var left=0;
	var right = input.length;
	var i;
	var j;
	for(i=0;i<=right;i++){
		if (input.substring(i,i+1).search(/\u0020/)!=-1)
		{
			left++;
		} else {
			break;
		}
	}
	if (left != right)
	{
		for(j=input.length;j>left;j--){
			if (input.substring(right,right-1).search(/\u0020/)!=-1)
			{
				right--;
			} else {
				break;
			}
		}
	}
	inputObject.value = input.substring(left,right);
	return inputObject;
}

function isAlphaNumerical(inputObject) {
	var input = inputObject.value;
	//match = /^[A-Za-z0-9]+$/;			// Default
	match = /^[0-9a-zA-Z ,.-_\\s]+$/;	// Controle van inpact site voor London
	if (match.test(input)) {
		return true;
	}
	return false;
}

function isDutchMoney(inputObject) {
	var input = inputObject.value;

	var match = /^[0-9]{1,3}(\.?[0-9]{3})*(,[0-9]{1,2})?$/;

	if (match.test(input)) {
		return true;
	}
	return false;
}

function compareDates(dateValue1, dateValue2){
	if (dateValue1 == "" || dateValue2 == "") return 0;

	var day = dateValue1.substring(0,2);
	var month = dateValue1.substring(3,5);
	var year = dateValue1.substring(6,10);
	var date1 = year + month + day;

	day = dateValue2.substring(0,2);
	month = dateValue2.substring(3,5);
	year = dateValue2.substring(6,10);
	var date2 = year + month + day;

	if (date1 > date2) return 1;
	if (date1 < date2) return -1;
	if (date1 == date2) return 0;
}

function compareTimes(timeValue1, timeValue2){
	if (timeValue1 == "" || timeValue2 == "") return 0;

	var time1 = timeValue1;
	var time2 = timeValue2;

	if (time1 > time2) return 1;
	if (time1 < time2) return -1;
	if (time1 == time2) return 0;
}

// Checks if a number conforms to [0-9][0-9]* or [0-9][0-9]*\.[0-9][0-9]*
function isYearSequenceNumber(inputObj) {
	input = inputObj.value;
	year = new Date().getYear();
	if (year < 100) { year += 1900; }

	match1 = /^[0-9][0-9][0-9][0-9]\.[0-9]{1,4}$/;
	match2 = /^[0-9]{1,4}$/;
	if (match1.test(input)) {
		year = input.substr(0,4);
		sequenceNumber = "000"+input.substr(5);
		sequenceNumber = sequenceNumber.substr(sequenceNumber.length-4, sequenceNumber.length);
		inputObj.value = year+"."+sequenceNumber;
		return true;
	} else if (match2.test(input)) {
		sequenceNumber = "000"+input;
		sequenceNumber = sequenceNumber.substr(sequenceNumber.length-4, sequenceNumber.length);
		inputObj.value = year+"."+sequenceNumber;
		return true;
	} else {
		return false;
	}
}

function isDutchPhoneNumber(inputObject) {
	var input = inputObject.value;
	match1 = /^[0-9 -]+$/;
	if (!match1.test(input)) {
		return false;
	}

	var i;
	var num = 0;
	match2 = /^[0123456789]/;
	for(i=0;i<input.length;i++){
		var sub = input.substr(i,i+1);
		if (match2.test(sub)){num++;}
	}
	if (num != 10){return false;}
	return true;
}

// Emailaddress
function isEmailAddress(input){
	var atIndex = input.value.indexOf("@");
	if (atIndex == -1){
		return false;
	}
	var user = input.value.substring(0, atIndex);
	if (user.length == 0) return false;

	var domain = input.value.substring(atIndex+1);	
	if (domain.length == 0) return false;
	if (domain.indexOf(".") == -1) return false;

	var nonEmailCharacters = "!\"#$%^&*(),/:;<> ?[]{}'|+=~\\@\t";
	for (var j=0; j<nonEmailCharacters.length; j++ ){
		if (user.indexOf(nonEmailCharacters.charAt(j)) != -1) return false;
		if (domain.indexOf(nonEmailCharacters.charAt(j)) != -1) return false;
	}
	
	var index = 0;
	var lastIndex = -1;
	while(index != -1){
		index = user.indexOf(".", lastIndex+1);
		if (index == 0 || index == user.length-1) return false;
		if (index - lastIndex == 1) return false;
		lastIndex = index;
	}
	
	index = 0;
	lastIndex = -1;
	while(index != -1){
		index = domain.indexOf(".", lastIndex+1);
		if (index == 0 || index == domain.length-1) return false;
		if (index - lastIndex == 1) return false;
		lastIndex = index;
	}
	
	return true;
}

function isBankAccountNumber(inputObject) {
	if (!conformsToRegexp(inputObject, "^([0-9]{7}|[0-9]{6})([0-9]{3})?$"))
	{
		return false;
	}

	var accountNumberStr = inputObject.value;
	if (accountNumberStr.length > 8) {
		// We're dealing with a bank account number. Check againt the "11-proef":
		// Step 1: add these numbers:
		//     - The first digit times 10
		//     - The second digit times 9
		//       ...
		//     - The tenth digit times 1
		// Step 2: check if the result is divisible by 11. If so, the "11-proef" succeeds.

		// We need 10 digits; prefix a zero if nescessary.
		if (accountNumberStr.length == 9) {
			accountNumberStr = "0" + accountNumberStr;
		}

		// Step 1
		var count, multiply, digit, total, temp;
		total = 0; // When we start adding numbers, we have nothing.
		for (count = 0; count < 10; count++) {
			multiply = 10 - count;								// Counts 10, 9, 8, ..., 1
			digit = parseInt(accountNumberStr.charAt(count));	// Gets a single digit.
			temp = multiply * digit;
			total = total + temp;								// Add to the sum
		}

		// Step 2
		var remainder = total % 11; // If remainder==0, total is divisible by 11.
		if (remainder != 0) {
			return false;
		}
	}

	return true;
}

// Checks if the value of the given inputObj conforms
// to one of the 7 allowed licence plate number formats.
// If so, it formats the value.
function isDutchLicencePlate(inputObj) {
	// Sidecode 1: XX-00-00
	sc1a = /^[a-zA-Z]{2}-[0-9]{2}-[0-9]{2}$/;
	sc1b = /^[a-zA-Z]{2}[0-9]{2}[0-9]{2}$/;
	// Sidecode 2: 00-00-XX
	sc2a = /^[0-9]{2}-[0-9]{2}-[a-zA-Z]{2}$/;
	sc2b = /^[0-9]{2}[0-9]{2}[a-zA-Z]{2}$/;
	// Sidecode 3: 00-XX-00
	sc3a = /^[0-9]{2}-[a-zA-Z]{2}-[0-9]{2}$/;
	sc3b = /^[0-9]{2}[a-zA-Z]{2}[0-9]{2}$/;
	// Sidecode 4: XX-00-XX
	sc4a = /^[a-zA-Z]{2}-[0-9]{2}-[a-zA-Z]{2}$/;
	sc4b = /^[a-zA-Z]{2}[0-9]{2}[a-zA-Z]{2}$/;
	// Sidecode 5: XX-XX-00
	sc5a = /^[a-zA-Z]{2}-[a-zA-Z]{2}-[0-9]{2}$/;
	sc5b = /^[a-zA-Z]{2}[a-zA-Z]{2}[0-9]{2}$/;
	// Sidecode 6: 00-XX-XX
	sc6a = /^[0-9]{2}-[a-zA-Z]{2}-[a-zA-Z]{2}$/;
	sc6b = /^[0-9]{2}[a-zA-Z]{2}[a-zA-Z]{2}$/;
	// Sidecode 7: 00-XXX-0
	sc7a = /^[0-9]{2}-[a-zA-Z]{3}-[0-9]{1}$/;
	sc7b = /^[0-9]{2}[a-zA-Z]{3}[0-9]{1}$/;

	var input = inputObj.value;

	// If format with seperator
	if (sc1a.test(input) || 
		sc2a.test(input) || 
		sc3a.test(input) || 
		sc4a.test(input) || 
		sc5a.test(input) || 
		sc6a.test(input) || 
		sc7a.test(input)){
	// If format without seperator of type .._.._..
	}else if (sc1b.test(input) || 
		sc2b.test(input) || 
		sc3b.test(input) || 
		sc4b.test(input) || 
		sc5b.test(input) || 
		sc6b.test(input)){
		input = input.substring(0,2) + "-" + input.substring(2,4) + "-" + input.substring(4,6);
	// If format without seperator of type .._..._.
	}else if (sc7b.test(input)){
		input = input.substring(0,2) + "-" + input.substring(2,5) + "-" + input.substring(5,6);
		
	// If invalid format
	}else{
		return false;
	}
	// Convert whole string to uppercase.
	inputObj.value = input.toUpperCase()

	return true;
}

// Checks if a string conforms to a given regular expression regexp. regexp can look like this in your code:
// /^[a-zA-Z][a-zA-z]$/ . Don't forget the /'s. Don't put "'s around it in your code, just the regular expression.
function conformsToRegexp(inputObject, regexp) {
	var input = inputObject.value;
	match = new RegExp(regexp);
	if (match.test(input))
	{
		return true;
	}
	else
	{
		return false;
	}
}

// This function determines the element type of a form element
function getFormElementType(el) {
	// If nescessary, resolve the first element of an array
	// (radio buttons are typically ordered this way)
	var el0 = el;
	if (typeof el0.tagName == "undefined") {
		el0 = el[0];
	}

	// Resolve the element type
	var eltype = el0.tagName;
	if (eltype == "INPUT") {
		eltype = "INPUT/"+el0.type.toUpperCase();
	}

	return eltype;
}

// This function checks whether the user has changed an input field
function formChanged(document){
	for (d = 0; d < document.forms.length; d++) {
		var formObj = document.forms[d];
		for (i = 0; i < formObj.length; i++) {
			var el = formObj.elements[i];
			var eltype = getFormElementType(el);

			if (eltype == "INPUT/TEXT") {
				if (el.value != el.defaultValue) {
					return true;
				}
			}
			if (eltype == "INPUT/RADIO") {
				if (el.checked != el.defaultChecked) {
					return true;
				}
			}
			if (eltype == "INPUT/CHECKBOX") {
				if (el.checked != el.defaultChecked) {
					return true;
				}
			}
			if (eltype == "SELECT") {
				for (j = 0; j < el.length; j++) {
					if (el[j].selected != el[j].defaultSelected) {
						return true;
					}
				}
			}
			if (el.tagName == "TEXTAREA") {
				if (el.value != el.defaultValue) {
					return true;
				}
			}
		}
	}
	return false;
}


function clearField(el){
	var eltype = getFormElementType(el);

	if (eltype == "INPUT/TEXT") {
		el.value = "";
	}
	if (eltype == "INPUT/RADIO") {
		for (j=0; j<el.length; j++) {
			el[j].checked = false;
		}
		// A radio button has a hidden field with the same prefix.
		// (the radio buttons themselves are suffixed with "_radio")
		var elementName = el[0].name;
		var companionName = elementName.substring(0, elementName.length-6);
		el[0].form.elements[companionName].value="";
	}
	if (eltype == "INPUT/CHECKBOX") {
		// Instead of deselecting manually, let the element take care of it.
		// The element will set the inevitable hidden field to the right value.
		if (el.checked) {
			el.click();
		}
	}
	if (eltype == "SELECT") {
		for (j = 0; j < el.length; j++) {
			if (el[j].value == "") {
				el[j].selected = true;
			} else {
				el[j].selected = false;
			}
		}
	}
	if (eltype == "TEXTAREA") {
		el.value = "";
	}
}

function resetField(el){
	var eltype = getFormElementType(el);

	if (eltype == "INPUT/TEXT") {
		el.value = el.defaultValue;
	}
	if (eltype == "INPUT/RADIO") {
		// A radio button has a hidden field with the same prefix.
		// (the radio buttons themselves are suffixed with "_radio")
		var elementName = el[0].name;
		var companionName = elementName.substring(0, elementName.length-6);
		var companionElement = el[0].form.elements[companionName];
		companionElement.value = "";

		// Now fill the cleared field
		for (j=0; j<el.length; j++) {
			if (el[j].defaultChecked) {
				companionElement.value = el[j].value;
			}
			el[j].checked = el[j].defaultChecked;
		}
	}
	if (eltype == "INPUT/CHECKBOX") {
		// Instead of (de)selecting manually, let the element take care of it.
		// The element will set the inevitable hidden field to the right value.
		if (el.checked != el.defaultChecked) {
			el.click();
		}
	}
	if (eltype == "SELECT") {
		for (j = 0; j < el.length; j++) {
			el[j].selected = el[j].defaultSelected;
		}
	}
	if (eltype == "TEXTAREA") {
		el.value = el.defaultValue;
	}
}

// Function that tells whether a form element is enabled.
function isEnabled(el) {
	var eltype = getFormElementType(el);

	// Get the disabled status of the element.
	if (
		eltype == "INPUT/TEXT"     ||
		eltype == "INPUT/CHECKBOX" ||
		eltype == "SELECT"         ||
		eltype == "TEXTAREA"
	) {
		return !el.disabled;
	}
	if (eltype == "INPUT/RADIO") {
		// A radio button array is enabled is all radio buttons are enabled.
		var enabled = true;
		for (j=0; j < el.length; j++) {
			if (el[j].disabled) {enable = false;}
		}
		return enabled;
	}
}

// Function that enables or disabled a form element.
function setEnabled(el, newEnabled) {
	var eltype = getFormElementType(el);

	// Set disabled status of the element.
	if (
		eltype == "INPUT/TEXT"     ||
		eltype == "INPUT/CHECKBOX" ||
		eltype == "SELECT"         ||
		eltype == "TEXTAREA"
	) {
		el.disabled = !newEnabled;
	}
	if (eltype == "INPUT/RADIO") {
		for (j=0; j < el.length; j++) {
			el[j].disabled = !newEnabled;
		}
	}
}

// Sets the focus to the specified form element.
// If the form element is an array, sets the focus to the first element.
function setFocus(el) {
	var eltype = getFormElementType(el);
	if (el.length != null && eltype != "SELECT"){
		eltype = getFormElementType(el[0]);
	}
	if (eltype == "INPUT/TEXT"     ||
		eltype == "INPUT/CHECKBOX" ||
		eltype == "INPUT/BUTTON" ||
		eltype == "SELECT"         ||
		eltype == "TEXTAREA" ||
		eltype == "INPUT/RADIO") {
		// The timeout is to work around a bug in IE:
		// If you set the focus to a text field within 10ms from a focus switch, it doesn't work.
		// Result: you cannot refocus a field after an input check without this timeout.
		if (el.length != null && eltype != "SELECT") {
			setTimeout("document.forms['form']."+el[0].name+"[0].focus()", 10);
		}else{
			setTimeout("document.forms['form']."+el.name+".focus()", 10);
		}
	}
}


// Function to get the visibility of a named page element.
// A page element gets a name by speficying it's ID attribute: ID="name"
//
function isVisible(elementName) {
	var object;
	if (document.all){
		object = document.all(elementName);
	}
	if(!document.all && document.getElementById){
		object = document.getElementById(elementName);
	}

	if (object.style.visibility == "hidden") {
		return false;
	} else {
		return true;
	}
}

// Function to set the visibility of a named page element.
// A page element gets a name by speficying it's ID attribute: ID="name"
//
// The parameter usesSpaceIfHidden determines wether an invisible element
// is hidden (true), or not displayed at all (false).
//
function setVisible(elementName, usesSpaceIfHidden, isVisible) {
	var object;
	if (document.all){
		object = document.all(elementName);
	}
	if(!document.all && document.getElementById){
		object = document.getElementById(elementName);
	}

	object.style.display = (isVisible || usesSpaceIfHidden ? "inline" : "none");
	object.style.visibility = (isVisible ? "visible" : "hidden");
}

//Reset the fields on the form
function resetForm(resetButton){
	var formObj = resetButton.form;
	var i;
	for(i=0;i<formObj.elements.length;i++){
		if (formObj.elements[i].type == "text") formObj.elements[i].value = "";
		if (formObj.elements[i].type == "select-one") formObj.elements[i].value = "";
		if (formObj.elements[i].type == "radio") formObj.elements[i].checked = false;
		if (formObj.elements[i].type == "checkbox") formObj.elements[i].checked = false;
	}
}


