function ConfirmDelete(itemName)
{
	return confirm("Are you sure you want to delete this " + itemName + "?");
}

function Trim(txt)
{
	var len = txt.length;
	for (i=0;i<len && txt.charAt(i)==" "; i++){} //the first non-blank char
	for (j=len-1;j>=0 && txt.charAt(j)==" "; j--){} //the last non-blank char
	if (i<=j) {return txt.substring(i,j+1)} else {return ""}
}

function CheckRadioIndex(grp)
{
	// This function checks a radio button group and returns the selected index (starting from 0).
	// If no radio button is selected, it returns -1.
	var idx = -1;
	if (grp.length) {
		// there are two or more radio buttons
		for (var i=0; i<grp.length; i++)
		{
			if (grp[i].checked == true) {idx = i; break;}
		}
	} else {
		// there is only one radio button
		if (grp.checked == true) {idx = 0;}
	}
	return idx;
}

function SimplifyPhoneNumber(obj)
{
	// "obj" is an input control in the form.
	var num = obj.value;
	re = /\(/g;
	num = num.replace(re, "");
	re = /\)/g;
	num = num.replace(re, "");
	re = /-/g;
	num = num.replace(re, "");
	re = / /g;
	num = num.replace(re, "");
	obj.value = num;
}

function UpdateCBGState(ctrl)
{
	// This function updates the related control of a checkbox (in a checkbox group).
	var nm = ctrl.name;
	var fm = ctrl.form;
	var ctrls = fm.elements[nm];	// retrieve all the elements that have the same name
	if (ctrls.length) {
		// process it as a collection
		// find the element's index first
		var idx = -1;
		for (var i=0; i<ctrls.length; i++)
		{
			if (ctrls[i] == ctrl) {idx = i; break;}
		}
		// process the related control of the same ordinal index
		fm.elements[nm + "_NewState"][idx].value = ((ctrl.checked) ? "1" : "0");
	} else {
		// process it as a single element
		fm.elements[nm + "_NewState"].value = ((ctrl.checked) ? "1" : "0");
	}
	//alert(idx);
	//alert(ctrl.checked);
}

function SyncText(sel, ctrl)
{
	// this function synchronizes the selected option text to another control
	ctrl.value = sel.options[sel.selectedIndex].text;
}

function MatchRegex(valString, regEx, regExType /* optional */)
{
	if (regEx == null) {
		switch (regExType) {
			case "date":
				// this is just a format match; doesn't gurantee date is valid.
				regEx = /^(\d{1,2})\/(\d{1,2})\/(\d+)$/;
				break;
			case "number":
				regEx = /^-?\d*\.?\d*$/;
				break;
			case "ssn":
				//could also be /^(\d{3})-?\d{2}-?\d{4}$ to ^\d{9}$/
				regEx = /^\d{3}\-?\d{2}\-?\d{4}$/;
				break;
			case "email":
				// some characters + @ + some characters + . + some characters (can have more dots)
				regEx = /^.+@.+\..+$/;
				break;
			case "phone":
				// this allows 10-digit phone number with some optional characters at the end (like ext. numbers)
				regEx = /^\(?\d{3}\)?\s?\d{3}\-?\d{4}.*$/;
				break;
			case "zip":
				// nnnnn or nnnnn-nnnn
				regEx = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
				break;
			default:
				// do nothing
		}
	}
	var matchArr = valString.match(regEx);
	return matchArr;
}

function ValidateDate(valString)
{
	// define the max number of days in a month
	var maxDayArr = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	// the presumed format is mm/dd/yyyy
	var matchArr = MatchRegex(valString, null, "date");
	var result = false;
	if (matchArr != null) {
		// retrieve submatches
		var m = parseInt(matchArr[1], 10);
		var d = parseInt(matchArr[2], 10);
		var y = parseInt(matchArr[3], 10);
		//alert("debug: " + m + ";" + d + ";" + y);
		if (y >= 1 && m >= 1 && m <= 12) {
			if (d >= 1 && d <= maxDayArr[m-1]) {
				result = true;			
			} else {
				result = false;			
			}
		} else {
			result = false;			
		}
	} else {
		result = false;
	}
	return result;
}

function GenerateErrorMessage(valString, msgTemplate)
{
	// If only targeting IE 5.5 and above,
	// we can also use msgTemplate.replace(regEx, function()) syntax.
	var regEx = /\{#get-html:(.+?)}/;
	var msg = "";
	if (msgTemplate == "") {
		msg = "'" + valString + "' is not in a valid format."
	} else {
		while (msgTemplate.match(regEx) != null) {
			if (RegExp.$1 == "value") {
				msgTemplate = RegExp.leftContext + valString + RegExp.rightContext;		
			} else {
				msgTemplate = RegExp.leftContext + eval(RegExp.$1) + RegExp.rightContext;		
			}
		}
		msg = msgTemplate;
	}
	return msg;
}
