function xtractFile(data){
	var m = data.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/);
	if(m != null){
		return m[2];
	} else return data;
}


function chkFilename (field) {
	filename = xtractFile(field.value);
	forbiddenChars = "9*:/\|?\"\')\"";
	match = false;
	for(i = 0;i < filename.length; i++)
	{
		for(j=0;j < forbiddenChars.length; j++)
		{
			if(filename.charAt(i) == forbiddenChars.charAt(j))
			{
				return false;
			}
		}
	}
	return true;	
}
function chkText (field) {
	if (field.value.replace(/\s/g,'').length==0) return false;
	return true;
} 

function chkUrl(field){
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
    if(RegExp.test(field.value)){
        return true;
    }else{
        return false;
    }
} 

function chkEmail (field) {
	/* RegExps are so convoluted.  it matches this pattern: xxxx[.xxxx]@xxxx[.xxx].[xxx|areo] */
	if (!field.value.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/) || numbers.length!=10) return false;
	return true;
}

function chkNumber (field) {
	return chkPattern(field, /\d+/);
}

function chkMoney (field) {
	return chkPattern(field, /^\d+\.\d{2}$/);
}

function chkPattern (field,pattern) {
	if (!field.value.match(pattern)) return false;
	return true;
}

function chkRange (field,minValue,maxValue) {
	if (!pchkRange(field.value,minValue,maxValue))  return false;
	return true;
}

/* 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 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, 31, 30, 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) {
	if (!field.value.match(/\d{2}:\d{2}/)) return false;
	return true;
}

function chkSelect (field) {
	if (field.selectedIndex==0 && field.options[0].value == "") 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;
	if(field.length) {
		for (i=0;i<field.length;i++) {
			if (field[i].checked) chk=true;
		}
	} else {
		if(field.checked) chk=true;
	}
	
	return chk;
}

function chkRadioLimit (field, limit) {
	var chk=0;
	if(field.length) {
		for (i=0;i<field.length;i++) {
			if (field[i].checked) chk++;
		}
	} else {
		if(field.checked) chk=true;
	}
	
	if(chk > limit || chk == 0) {return false;}
	else {return true;} 
}

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;
}

function chkZip (field) {
	if (!field.value.match(/\d{5}/)) return false;
	else return true;
}

/* 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;
}

/* on hold for now, but very interesting... */
function getLabel (fieldid) {
	allLabels = document.getElementsByTagName('label');
	for(var i=0;i<allLabels.length;i++) {
		if(allLabels[i].htmlFor == fieldid) {
			return allLabels[i].firstChild.nodeValue;
		}
	}
	return fieldid;
}

// var messageheader = "You missed some required fields:";
var messageheader = "You must address the following items before you can continue:";

function setMessageHeader(text) {
	messageheader = text;
}

function getMessageHeader() {
	return messageheader;
}

/* the message must be cleared and re-built after each alert. */
function clearMsg () {
	validateMsg=""; 
	addMsg(getMessageHeader());
	//addMsg ("You must correct the following");
	//addMsg ("mistakes before you 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;
}