function gebi(el_id) { 
	return document.getElementById(el_id); 
}

function crel(type, attribs) { 
	var el = document.createElement(type);
	for(var i in attribs) el[i] = attribs[i];
	return el;
}

function addLoadEvent(func) { 
	var oldonload = window.onload; 
		if (typeof window.onload != 'function') { 
			window.onload = func; 
		} else { 
			window.onload = function() { 
			if (oldonload) { 
				oldonload(); 
			} 
			func(); 
		} 
	} 
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function number_format( number, decimals, dec_point, thousands_sep ) 
{
	var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
	var d = dec_point == undefined ? "." : dec_point;
	var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
	var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;

	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function FormValidator()
{
	this.is_valid = true;
	this.errors = Array();

	this.alpha_regex =  /^[a-zA-Z]+$/;
	this.alpha_numeric_regex = /^[a-zA-Z0-9]+$/;
	this.email_regex = /(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b)/i;
}

FormValidator.prototype.raiseError = function(msg)
{
	this.is_valid = false;
	this.errors[this.errors.length] = msg;
}

FormValidator.prototype.isValid = function() { return this.is_valid; }
FormValidator.prototype.countErrors = function() { return this.errors.length; }
FormValidator.prototype.getErrors = function() { return this.errors; }

FormValidator.prototype.alertErrors = function()
{
	var msg = 'Please correct the following errors:\n';
	for(var i=0;i<this.errors.length;i++) {
		msg += this.errors[i] + '\n';	
	}
	alert(msg);
}

FormValidator.prototype.notEmpty = function(value, msg)
{
	if(value.length == 0) {
		this.raiseError(msg);
		return false;
	}

	return true;
}

FormValidator.prototype.notEquals = function(value, not_value, msg)
{
	if(value == not_value) {
		this.raiseError(msg);
		return false;
	}
	return true;
}

FormValidator.prototype.isNumeric = function(value, msg)
{
	if(isNaN(value)) {
		this.raiseError(msg);
		return false;
	}
	return true;
}

FormValidator.prototype.isWithinNumericRange = function(value, min, max, msg)
{
	if(value < min || value > max) {
		this.raiseError(msg);
		return false;
	}
	return true;
}

FormValidator.prototype.isAlpha = function(value, msg)
{
	return this.matchRegex(value, this.alpha_regex, msg);
}

FormValidator.prototype.isAlphaNumeric = function(value, msg)
{
	return this.matchRegex(value, this.alpha_numeric_regex, msg);
}

FormValidator.prototype.isEmail = function(value, msg)
{
	return this.matchRegex(value, this.email_regex, msg);
}

FormValidator.prototype.matchRegex = function(value, regex, msg)
{
	if(typeof regex != 'Object') {
		regex = new RegExp(regex);
	}
	
	if(value.search(regex) == -1) {
		this.raiseError(msg);
		return false;
	}
	return true;
}

FormValidator.prototype.isChecked = function(field, msg)
{
	for(var i=field.length-1;i>-1;i--) {
		if(field[i].checked) {
			return true;
		}
	}

	this.raiseError(msg);
	return false;
}

function recordOutboundLink(link, category, action) {
    _gat._getTrackerByName()._trackEvent(category, action);
    setTimeout('document.location = "' + link.href + '"', 100);
}

