
/**
 * Returns the value of the given form field.
 * This function first detects what type of
 * field the object is, and then based on the
 * type... it grabs the value.
 * @param form - the form object.
 * @param field - the name of the field in the
 * form, or the atual object
 */
function getFieldValue(form, field) {

	// try as a string
	var element = form.elements[field];

	// try as an object
	element = (!element) ? field : element;

	// get the type
	var type = (!element.type) ? "NO TYPE DEFINED" :  element.type;

	// figure out what it is and get the value
	switch(type) {

		// regular select box
		case "select-one": {
			return element.value;
		}

		// multiple select box
		case "select-multiple": {
			var options = element.options;
			var ret = new Array();
			for (var i=0; i<options.length; i++) {
				if (options[i].selected) {
					ret[ret.length] = options[i].value;
				}
			}
			return ret;
		}

		// radio buttons
		case "radio": {
			return (element.checked) ? element.value : "";
		}

		//  check boxes
		case "checkbox": {
			// checkbox
			return (element.checked) ? element.value : "";
		}

		// text boxes
		case "text": {
			return element.value;
		}

		// hidden fields
		case "hidden": {
			return element.value;
		}

		// wtf?
		default: {
			// is it a node list? if soo lets iterate
			// through it and return the values.
			if (element.length) {

				// radio node list?
				if (element[0].type=="radio") {
					for (var i=0; i<element.length; i++) {
						if (element[i].checked) {
							return element[i].value;
						}
					}

				// wtf?
				} else {
					var ret = new Array();
					for (var i=0; i<element.length; i++) {
						var val = getFieldValue(form,element[i]);
						if (val!="") {
							ret[ret.length] = val;
						}
					}
					return ret;
				}

			// don't know what this is.. lets die
			} else {
				alert("Type could not be determined for "+element+", dumping object");
				dumpObject(element);
				return null;
			}
		}
	}
}

/**
 * Dumps the object to a new window.
 * This is usefull for debugging.
 * @param obj - the object to dump.
 */
function dumpObject(obj) {
	var win = window.open();
	win.document.write("<table border='1'>");
	for (i in obj) {
		try {
			win.document.write(
				"<tr><td align='right'>"+i+"</td>"+
				"<td align='left'>"+obj[i]+"</td></tr>"
			);
		} catch(Error) { }
	}
	win.document.write("</table>");
}

/**
 * Adds commas to a number field.
 * @param textField - the text INPUT element.
 */
function addcommasToNumberField(textField) {

	// get field value and normalize it
	var strNum  = textField.value;
	while (strNum.indexOf(",")!=-1) {
		strNum = strNum.replace(/,/,"");
	}

	// some temporary variables
	var dec	= null;
	var ret	= "";

	// get decimal and after
	if (strNum.indexOf(".")!=-1) {
		dec 	= strNum.substring(strNum.indexOf("."),strNum.length);
		strNum	= strNum.substring(0,strNum.indexOf("."));
	}

	// add the commas
	if (strNum.length>3) {
		for (var i=strNum.length; i>-1; i-=3) {
			var comma = (strNum.length==i || i==0) ? "" : ",";
			ret = strNum.substring(i-3,i)+comma+ret;
		}
	} else {
		ret = strNum;
	}

	// add decimal
	ret += (dec!=null) ? dec : "";

	// set the value
	textField.value = ret;
}

/**
 * Formats an error message
 * @param the error message.
 * @param ... arguments for the error message
 */
function formatErrorMessage() {
	var errorMessage = arguments[0];
	for (var i=1; i<arguments.length; i++) {
		errorMessage = errorMessage.replace("{"+(i-1)+"}",arguments[i]);
	}
	return errorMessage;
}

/**
 * Removes leading and trailing spaces from a string value.
 * @param strIn - the value
 */
function trimString(strIn) {
	if (/^\s/.test(strIn)) { strIn = strIn.replace(/^\s{1,}/, ""); }
	if (/\s$/.test(strIn)) { strIn = strIn.replace(/\s{1,}$/, ""); }
	return strIn;
}

/**
 * Determines wether or not a value is a
 * valid email address.
 * @param val - the value
 */
function isEmailAddress(val) {
	val = trimString(val);
	if (val.length < 5) { return false; }
	return /(^[A-z0-9\.\-_]*)@([A-z0-9\.\-_]+)\.([A-z]{2,4})$/.test(val);
}

/**
 * Determines wether or not a value is empty.
 * @param val - the value
 */
function isEmpty(val) {
	return (!val || val==null);
}

/**
 * Ensures that a value is numeric.  Commas
 * are supported.
 * @param val - the value
 */
function isNumeric(val) {
	return !(/[^0-9,.-]/.test(val));
}






