
// 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 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(parseInt(parts[0])) || isNaN(parseInt(parts[1])) || isNaN(parseInt(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 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 getCookie(name) {
	var cooki = document.cookie.split("; ");
	for (var count = 0; count < cooki.length; count++) {
		var crumb = cooki[count].split("=");
		if (name == crumb[0]) return unescape(crumb[1]);
	}
	return "";
}

function createOption(code, dscr) {
	var opt = document.createElement("OPTION");
	opt.value = code;
	opt.text = dscr;
	return opt;
}

// <img src="images/excel.gif" align="right" id="icoHtml" onclick="showExcel('icoHtml', 'tableHtml', 'icoExcel', 'objExcel')"/>
// <span id="tableHtml"><table /></span>

// <img src="images/html.gif" align="right" id="icoExcel" onclick="showHtml('icoHtml', 'tableHtml', 'icoExcel', 'objExcel')" style="display: none;"/>
// <object classid="clsid:0002E510-0000-0000-C000-000000000046" id="objExcel" height="400" width="85%" style="display: none;"></object>

function showExcel(imgHtml, spanTable, imgExcel, objectExcel) {
	imgHtml = document.all(imgHtml);
	spanTable = document.all(spanTable);
	imgExcel = document.all(imgExcel);
	objectExcel = document.all(objectExcel);

	objectExcel.Refresh();
	objectExcel.DataType = "HTMLData";
	objectExcel.HTMLData = spanTable.innerHTML;
	objectExcel.Refresh();
	objectExcel.ActiveSheet.UsedRange.AutoFitColumns();
	
	imgHtml.style.display = "none";
	spanTable.style.display = "none";
	imgExcel.style.display = "inline";
	objectExcel.style.display = "inline";
}

function showHtml(imgHtml, spanTable, imgExcel, objectExcel) {
	imgHtml = document.all(imgHtml);
	spanTable = document.all(spanTable);
	imgExcel = document.all(imgExcel);
	objectExcel = document.all(objectExcel);

	imgHtml.style.display = "inline";
	spanTable.style.display = "inline";
	imgExcel.style.display = "none";
	objectExcel.style.display = "none";
}

// 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;
}
