<!--
// javascript utility functions - mainly for help with form processing ....

// returns form errors as held in (string) array
function frmErrors(errArray) {
	if (errArray.length == 0) { return true; }
	var str = "Please provide the following information:";
	for (i in errArray) { str += "\n - " + errArray[i]; }
	alert(str);
	return false;
}

// returns the value of the radio button group identified be elem
function getRadioValue(radio_elem) {
	for (i=0; i < radio_elem.length; i++) {
		if (radio_elem[i].checked) { return radio_elem[i].value; }
      }
	return null;
}

// return the value of the selected option
function getSelectValue(select_elem) {
	var idx = select_elem.selectedIndex;
	return (idx<0) ? null : select_elem[idx].value;
}

// extend String object to provide regular expression matching against defined patterns:
var INT 		= /^-{0,1}\d+$/
var POS_INT 	= /^\d*$/;
var NEG_INT 	= /^-\d+$/;
var NONZERO_INT 	= /^[1-9]+\d*$/;
var NUMBER 		= /^-{0,1}\d*\.{0,1}\d+$/;
var POS_NUMBER 	= /^\d*\.{0,1}\d+$/;
var NEG_NUMBER 	= /^-\d*\.{0,1}\d+$/;
var ALPHA		= /^[A-Z]+$/i;
var ALPHPASPACE	= /^[A-Z ]+$/i;
var ALPHANUM	= /^[A-Z0-9]+$/i;
var ALPHANUMSPACE = /^[A-Z0-9 ]+$/i;
var EMAIL		= /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
var NAME		= /^[A-Z .'-]+$/i;

var FNAME_EXT     = /^[A-Z_]+[A-Z_-]+\.[A-Z]{3,4}$/i;
var FNAME_NO_EXT  = /^[A-Z_]+[A-Z0-9_-]+$/i;

var PRICE 		= /^\d+(\.\d{2})?$/;

String.prototype.isType = function(regex) {
	if (this.match(regex)) {
		return true;
	}
	else {
		return false;
	}
}

// extend String object to provide function to trim whitespace
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

// extend String object to provide function to trim whitespace
// and convert inner whitespace sequences to single spaces
String.prototype.trimAndCompact = function() {
	return this.replace(/^\s+|\s+$/g,"").replace(/\s+/g, ' ');
}

// extend String object to provide function to check if string ends with either:
// str_or_arr value (as String) or one of str_or_arr values (as array)
// ignorecase - optional parameter defaults to false
String.prototype.endsWith = function(str_or_arr, ignorecase) {
	var str = (isArray(str_or_arr) && str_or_arr.length > 0) 
	        ? "("+ str_or_arr.join("|") + ")" 
		  : String(str_or_arr);
	var pattern = (ignorecase === undefined || ignorecase != true) 
	            ? new RegExp(str+ "$") 
			: new RegExp(str+ "$", "i");
	return pattern.test(this);
	
}

// extend String object to provide function to check if string starts with either:
// str_or_arr value (as String) or one of str_or_arr values (as array)
// ignorecase - optional parameter defaults to false
String.prototype.startsWith = function(str_or_arr, ignorecase) {
	var str = (isArray(str_or_arr) && str_or_arr.length > 0) 
	        ? "("+ str_or_arr.join("|") + ")" 
		  : String(str_or_arr);
	var pattern = (ignorecase === undefined || ignorecase != true) 
	            ? new RegExp("^" + str) 
			: new RegExp("^" + str, "i");
	return pattern.test(this);
}

// determine if variable is string
function isString(s) {
	if (typeof s == 'string') { return true; }
	if (typeof s == 'object') {  
		var match = s.constructor.toString().match(/string/i); 
		return (match != null);
	}
	return false;
}

// determine if variable is array
function isArray(a) {
	if (typeof a == 'object') {  
		var match = a.constructor.toString().match(/array/i); 
		return (match != null);
	}
	return false;
}

//-->