/* Google analytics code */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22842131-1']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

/* Function that displays status bar messages. */
function DisplayStatusMsg(msgStr) {
	window.status = msgStr;
	document.ReturnValue = true;
}

function openWin(windowURL, windowName, windowFeatures){
	return window.open(windowURL, windowName, windowFeatures ); 
}

/* Function that resets the default value of a form element. */
function resetFmElement(e) {
	if(e.type == 'hidden' || e.type == 'text' || e.type == 'textarea' || e.type == 'password') {
		e.value = e.defaultValue;
	} else if(e.type == 'select-one' || e.type == 'select-multiple') {
		for(var i = 0; i < e.options.length; i++) {
			e.options[i].selected = e.options[i].defaultSelected;
		}
	} else if(e.type == 'checkbox' || e.type == 'radio') {
		if(e[0]) {
			for(var i = 0; i < e.length; i++) {
				e[i].checked = e[i].defaultChecked;
			}
		} else {
			e.checked = e.defaultChecked;
		}
	}
}
// a utility function that trims and returns the input string 
function fnTrim(s) {
	return s.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
// A utility function that returns true if a string contains only 
// whitespace characters or is zero length.
function fnIsBlank(s)
{
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}
/* converts a string representation of a number to a float.  Default = 0. */
function fnStr2Float(s) {
	s = s + '';						// make sure s is type string 
	s = s.replace(/[^0-9\.]/g, "");	// remove any non-numeric characters such as comma, $, etc. 
	if(!s.length) s = '0';			// if no numbers in s then default to 0 
	return parseFloat(s);
}
/* formats a string representation of a number to the proper decimal places, with/without commas. */
function toStrFormat(s, d, c) {	// s = (string) the number to be formated, d = (integer) decimal places, c = (true|false) add commas for thousands etc. 
	var rv = '', sv = '', v;
	s = s + '';
	v = s.replace(/[^0-9\.]/g, "");
	if(!v.length) v = '0';
	v = parseFloat(v);
	if(!isNaN(v)) {
		sv = (d > 0) ?  (Math.round(v * Math.pow(10, d)) / Math.pow(10, d)) + '' : Math.round(v) + '';
		if(d > 0) {
			if(sv.indexOf('.') == -1) sv = sv + '.';
			while(sv.indexOf('.') >= sv.length - (d)) sv = sv + '0';
			if(c) {
				while(sv.search(/\d{4}[^\d]/) > -1) {
					sv = sv.replace(/(\d)(\d{3}[^\d])/, "$1,$2");
				}
			}
		} else {
			if(c && sv.length > 3) {
				sv = sv.substring(0, sv.length - 3) + "," + sv.substring(sv.length - 3, sv.length);
				while(sv.search(/\d{4}[^\d]/) > -1) {
					sv = sv.replace(/(\d)(\d{3}[^\d])/, "$1,$2");
				}
			}
		}
		rv = sv;
	}
	return rv;
}
function fnFormValidate(f) {
	var sMessage = '';
	var bReturnValue = true;
	var aEle = f.elements;
	for(i = 0; i < aEle.length; i++) {
		if(aEle[i].type == 'hidden' && aEle[i].name.match(/_required\b/)) {
			var sName = aEle[i].name.slice(0, aEle[i].name.length - 9);
			if(aEle[sName]) {
				var e = aEle[sName];
				var bNotFound = true;
				switch(e.type) {
					case 'hidden':
					case 'text':
					case 'textarea':
					case 'password':
						if(!fnIsBlank(e.value)) bNotFound = false;
						break;
					case 'select-one':
						for(var j = 0; j < e.options.length; j++) {
							if((j == 0 && e.options[j].selected && e.options[j].value.length > 0) || (j > 0 && e.options[j].selected)) bNotFound = false;
						}
						break;
					case 'select-multiple':
						for(var j = 0; j < e.options.length; j++) {
							if(e.options[j].selected) bNotFound = false;
						}
						break;
					case 'checkbox':
					case 'radio':
					default:
						if(e[0]) {
							for(var j = 0; j < e.length; j++) {
								if(e[j].checked) bNotFound = false;
							}
						} else {
							if(e.checked) bNotFound = false;
						}
						break;
				}
				if(bNotFound) {
					sMessage += aEle[i].value + '\n';
					bReturnValue = false;
				}
			}
		}
	}
	if(sMessage.length) alert(sMessage + '\nPlease correct the above and re-try.');
	return bReturnValue;
}
