var PattDict = new Object();
PattDict.postalCodeUS = /^\d{5}(-?\d{4})?$/;
PattDict.currencyUSD = /^\$?\d{1,3}(,?\d{3})*\.\d{2}$/;
PattDict.time12 = /^((0{0,1}\d)|(1[0-2])):[0-5]\d$/;
PattDict.time24 = /^(([0-1]{0,1}\d)|(2[0-4])):[0-5]\d$/;
PattDict.emailStd = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
PattDict.notEmpty = /.+/;
PattDict.dateMDY = /^[0-1]?\d(\/|-|\.)[0-3]?\d(\/|-|\.)(\d{2}|\d{4})$/;
PattDict.numberNoDec = /^\d+$/;

// just validate the value
function valueValidate(theVal,thePat){
if(PattDict[thePat]) {
		var testPat = PattDict[thePat]; // select the validating regular expr
		return  testPat.test(theVal); // run it on value of elArr[i]
	}
}

// validate the value of the object
function objValidate(theObj, thePat) {
	return valueValidate(theObj.value, thePat);
}

// If the object validation fails it focues to the object
function objValidateFocus(theObj, thePat) {
	if (! objValidate (theObj,thePat)) {
		theObj.focus();
		return false;
	}
	return true;
}

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
	if (! supported) {
    	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	}
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

// Returns index of checked radio button in radio set,
// or -1 if no radio buttons are checked
// See this page for more good functions
// http://www.breakingpar.com/bkp/home.nsf/Doc?OpenNavigator&U=CA99375CC06FB52687256AFB0013E5E9
function getCheckedRadioButton (radioSet) {
	if (radioSet[0]) {
		for (var i = 0; i < radioSet.length; i++) {
			if (radioSet[i].checked) {
      return i;
			}
		}
	}
	else {
		if (radioSet.checked) {
			return 0;
		}
	}
  return -1;
  }

// Returns the number of check box options that are selected
// or 0 if no check boxes are checked
function getCheckBoxesSelected (checkBoxSet) {
	var checked = 0;
	if (checkBoxSet[0]) {
		for (var i = 0; i < checkBoxSet.length; i++) {
			if (checkBoxSet[i].checked) {
				checked++;
			}
		}
	}
	else {
		if (checkBoxSet.checked) {
			checked++;
		}
	}
	return checked;
}

// Usage with FORM SUBMIT: onClick="return confirmDelete ();"
// Usage with FORM BUTTON: onClick="if (confirmDelete ()) {document.frmItem.submit ();}"
function confirmDelete () {
	var agree = confirm ("Are you sure you want to delete this item?");
	if (agree) {
		return true;
	}
	else {
		return false;
	}
}

// Usage with FORM SUBMIT: onClick="return confirmArchive ();"
// Usage with FORM BUTTON: onClick="if (confirmArchive ()) {document.frmItem.submit ();}"
function confirmArchive () {
	var agree = confirm ("Are you sure you want to archive this item?");
	if (agree) {
		return true;
	}
	else {
		return false;
	}
}

//
//
function CountWords (this_field, show_word_count, show_char_count) {
	if (show_word_count == null) {
		show_word_count = true;
	}
	if (show_char_count == null) {
		show_char_count = false;
	}
	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace (initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace (non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split (" ");
	var word_count = splitString.length -1;
	if (fullStr.length < 2) {
		word_count = 0;
	}
	if (word_count == 1) {
		wordOrWords = " word";
	}
	else {
		wordOrWords = " words";
	}
	if (char_count == 1) {
		charOrChars = " character";
	}
	else {
		charOrChars = " characters";
	}
	if (show_word_count & show_char_count) {
		alert ("Word Count:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
	}
	else {
		if (show_word_count) {
			alert ("Word Count:  " + word_count + wordOrWords);
		}
		else {
			if (show_char_count) {
				alert ("Character Count:  " + char_count + charOrChars);
			}
		}
	}
	return word_count;
}
//
// Usage: textCounter (document.form[0].input_element, true, false, 325);
function textCounter (field, boolAlert, boolTrim, maxlimit) {
	if (field.value.length > maxlimit) { // if too long...trim it!
		if (boolAlert) {
			alert ('The data you entered is too long');
			field.focus ();
		}
		if (boolTrim) {
			field.value = field.value.substring (0, maxlimit);
		}
		return false;
	}
	else {
		return true;
	}
}