function delConfirm(){
	msg = "Are you sure? This will permanently delete this record.\nIF you're sure you want to permanently remove this, click OK.";
	return confirm(msg);
}

function chkText (field) {
	if (field.value.replace(/\s/g,'').length==0) return false;
	return true;
} 
function chkLength (field,n) {
	if (field.value.replace(/\s/g,'').length < n) return false;
	return true;
} 

function chkNumber (field){
	if (isNaN(field.value)) return false;
	return true;
}

function chkEmail (field) {
	/* RegExps are so convoluted.  it matches this pattern: xxxx[.xxxx]@xxxx[.xxx].[xxx|areo] */
	email_address=field.value.toLowerCase();
	if (!email_address.match(/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$/)) return false;
	return true;
}

function chkPhone (field) {
	/* strip the valid delimiters ( ) - . (and white space) and then make sure we've got 10 numbers */
	validDelimiters=/[\(\)-\.\s]/g;
	numbers=field.value.replace(validDelimiters,'');
	if (numbers.match(/\D/)) return false;
	return true;
}

function chkPattern (field,pattern) {
	return field.value.match(pattern);
}

function chkRange (field,minValue,maxValue) {
	if (!pchkRange(field.value,minValue,maxValue))  return false;
	return true;
}

function chkMatch (field1,field2) {
	if (field1.value != field2.value) return false;
	else return true;
}

function chkCC(field,cardType)
{
	var result = false;
	cardNum = field.value;
	cardType = cardType.toUpperCase();
	
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);

	switch (cardType)
	{
		case "VISA":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AMEX":
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MASTERCARD":
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISCOVER":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DINERS":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}
	return result;
}

/* internal function, only to be used inside validation functions */
function pchkRange(value,minValue,maxValue) {
	if(!value.match(/\d/)) return false;
	/* ensure that we are working with numbers, not strings */
	value=parseInt(value,10);
	minValue=parseInt(minValue,10);
	maxValue=parseInt(maxValue,10);
	if (value < minValue || value > maxValue) return false;
	return true;
}

function chkCMSDate (field) {
	/* special for comsoft */
	if (field.value=='99999999') return true;
	if (!chkDate(field)) return false;
	return true
}

function chkDate (field) {
	if (!field.value.match(/\d{2}\/\d{2}\/\d{4}/)) return false;
	/* also check for a well formed date */
	var maxDaysInMonth = new Array(31, 29, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31);
	dateParts = field.value.split("/"); /* split date into array */
	if (!pchkRange(dateParts[0],1,12)) return false;
	if (!pchkRange(dateParts[1],1,maxDaysInMonth[parseInt(dateParts[0]) - 1])) return false;
	return true;
}

function chkTime (field) { // kfigy: modify RE to check ranges and for hh:mm:ss format
	if (!field.value.match(/[0-9]*:[0-5][0-9]/) && !field.value.match( /[0-9]*:[0-5][0-9]:[0-5][0-9]/ )) return false;
	return true;
}

function chkSelect (field) {
	if (field.selectedIndex==0) return false;
	return true;
}

function chkCrossSelect (field) {
	if (field.options.length==0) return false;
	return true;
}

function chkMultiSelect (field) {
	chk=false;
	for (i=0;i<field.options.length;i++) {
		if (field.options[i].selected) chk=true;
	}
	return chk;
}

function chkRadio (field) {
	var chk=false;
	for (i=0;i<field.length;i++) {
		if (field[i].checked) chk=true;
	}
	return chk;
}

function chkCheckbox () {
	/* this expects arguments that correspond to the checkbox field names. no qty limit. */
	chk=false;
	for (i=0;i<arguments.length;i++) {
		if (arguments[i].checked==true) chk=true;
	}
	return chk;
}

/* the following functions build the error message displayed to the user */

validateMsg="";
function addMsg (msg) {
	maxlength=60;
	
	/* if the msg is longer than [maxlength] characters, it will break it down at the closest [space] */
	if (msg.length > maxlength) { 
		aMsg = msg.split(" ");
		count=0;
		for (i=0;i<aMsg.length;i++) {
			count = count + aMsg[i].length;
			if (count > maxlength) {
				aMsg[i-1]=aMsg[i-1]+ "\n"
				count=aMsg[i].length;
			}
		}
		msg=aMsg.join(" ");
	} 
	validateMsg=validateMsg + msg + "\n";
}

bError=false; //track if an error was found

/* add delimiter to error message and flip bError */
function addError (msg) {
	msg = "- " + msg; // add a small delimiter in front of the error message
	addMsg (msg);
	bError=true;
}

/* the message must be cleared and re-built after each alert. */
function clearMsg () {
	validateMsg=""; 
	addMsg ("The following information is needed to continue:");
	addMsg ("__________________________________________________________");
	bError=false;
}
clearMsg ();

/* if an error occurred, alert the user, return false and log the errors */
function alertMsg () {
	if (bError) {
		alert (validateMsg);
		clearMsg ();
		return false;
	}
	else return true;
}
