
// validation routines.
// copyright (c) 2000 alvin ward. all rights reserved.

function validText (name, txtbox, mandatory) {
	if ((mandatory) && (txtbox.value == '')) {
		txtbox.focus();
		alert (name + ' is a mandatory field');
		return false;
	}
	return true;
}

function validTextLen (name, txtbox, mandatory, len) {
	if ((mandatory) && (txtbox.value == '')) {
		txtbox.focus();
		alert (name + ' is a mandatory field');
		return false;
	}
	if ((txtbox.value != '') && (txtbox.value.length != len)) {
		txtbox.focus();
		alert (name + ' must be ' + len + ' characters long');
		return false;
	}
	return true;
}

function validNumeric (name, txtbox, mandatory) {
	if ((mandatory) && (txtbox.value == '')) {
		txtbox.focus();
		alert (name + ' is a mandatory field');
		return false;
	}
	//if ((txtbox.value != '') && (isNaN(parseFloat(txtbox.value)))) {
	if ((txtbox.value != '') && (isNaN(txtbox.value))) {
		txtbox.focus();
		alert (name + ' must be a valid numeric field');
		return false;
	}
	return true;
}

function validDate (name, txtbox, mandatory) {
	if ((mandatory) && (txtbox.value.length != 10)) {
		txtbox.focus();
		alert (name + ' is a mandatory field');
		return false;
	}
	if (txtbox.value != '') {
		var parts = txtbox.value.split("-");
		if ((txtbox.value.length != 10) || (parts.length != 3)) {
			txtbox.focus();
			alert (name + ' must be a valid date (yyyy-mm-dd)');
			return false;
		}
		if (isNaN(parts[0]) || isNaN(parts[1]) || isNaN(parts[2])) {
			txtbox.focus();
			alert (name + ' must be a valid date (yyyy-mm-dd)');
			return false;
		}
	}
	return true;
}

function validSelect (name, lstbox, mandatory) {
	if ((mandatory) && (lstbox.value == "")) {
		lstbox.focus();
		alert (name + ' is a mandatory field');
		return false;
	}
	return true;
}

function validRadio (name, optbutton, mandatory) {
	if (mandatory) {
		for (var count = 0; count < optbutton.length; count++) {
			if (optbutton[count].checked) { return true; }
		}
		optbutton[0].focus();
		alert (name + ' is a mandatory field');
		return false;
	}
	return true;
}

// utility functions
function val(value) {
	return (isNaN(parseFloat(value)) ? 0 : parseFloat(value));
}

function trim(str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function confirmRemove () {
	return confirm('Are you sure you want to remove this item?');
}

var previousSwapColor;

function swapColor(parent, color) {
	if (parent.children.length > 0) previousSwapColor = parent.children.item(0).className;
	for (var count = 0; count < parent.children.length; count++) {
		parent.children.item(count).className = color;
	}
}

function restoreColor(parent) {
	for (var count = 0; count < parent.children.length; count++) {
		parent.children.item(count).className = previousSwapColor;
	}
}

function createOption(code, dscr) {
	var opt = document.createElement("OPTION");
	opt.value = code;
	opt.text = dscr;
	return opt;
}

// extended validation routines.
function validMod10 (number) {
	var check;
	var sum = 0;
	var weight = 2;

	if (isNaN(number)) { return false; }
	for (var count = number.length - 2; count >= 0; count--) {
		check = number.charAt(count) * weight;
		if (check > 9) {
			check = check - 9;
		}
		sum = sum + check;
		weight = Math.abs(weight - 3);
	}
	return ((10 - sum % 10) % 10) == number.charAt(number.length - 1);
}
				
function validMod11 (number) {
	var check;
	var sum = 0;
	var weight = 1;

	if (isNaN(number)) { return false; }
	for (var count = number.length - 1; count >= 0; count--) {
		check = number.charAt(count) * weight;
		sum = sum + check;
		weight = (weight % 10) + 1;
	}
	return (sum % 11) == 0;
}
