function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}

function popupImage(image, title, alt)
{
	img = new Image();
	img.src = image;

	lPos = (screen.width) ? (screen.width-img.width+50)/2 : 0;
	tPos = (screen.height) ? (screen.height-img.height+100)/2 : 0;
	title = title ? title:'Image Popup';
	alt = alt ? alt:'';

	popup = this.open('', '_blank', "toolbar=no,width="+(img.width+50)+",height="+(img.height+100)+",left="+lPos+",top="+tPos+",directories=no,status=no,scrollbars=no,resize=no,menubar=no");

	popup.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1-transitional.dtd">\n');
	popup.document.write('<html xmlns="http://www.w3.org/1999/xhtml">\n');
	popup.document.write('<head>\n');
	popup.document.write('<title>'+title+'</title>\n');
	popup.document.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\n');
	popup.document.write('<style type="text/css">\n');
	popup.document.write('body { margin: 0px; background-color: #aaa; }\n');
	popup.document.write('a { text-decoration: none; font: normal 10px Arial, Helvetica, sans-serif}\n');
	popup.document.write('img { border: 1px solid #000; }\n');
	popup.document.write('.description { font: normal 12px Arial, Helvetica, sans-serif; color: #000000; line-height: 20px; text-align:center; }\n');
	popup.document.write('.title { font: bold 12px Arial, Helvetica, sans-serif; color: #000000; text-align: center; }\n');
	popup.document.write('.navigation { text-align:center; }\n');
	popup.document.write('</style>\n');
	popup.document.write('</head>\n');
	popup.document.write('<body>\n');
	popup.document.write('<table cellspacing="0" cellpadding="10" style="text-align:center; height:100%; width:100%; border: 0">\n');
	popup.document.write('<tr><td align="center" class="title">'+title+'</td></tr>\n');
	popup.document.write('<tr><td align="center" class="description"><img src="'+image+'" alt="'+alt+'" /></td></tr>\n');
	popup.document.write('<tr><td align="center" class="navigation"><a href="javascript:window.close();">Close Window</a></td></tr>\n');
	popup.document.write('</table>\n');
	popup.document.write('</body>\n');
	popup.document.write('</html>');

	popup.document.close();
	popup.focus();
}

function popupWindow(url, width, height, name)
{
	lPos = (screen.width) ? (screen.width-width)/2 : 0;
	tPos = (screen.height) ? (screen.height-height)/2 : 0;
	name = (name) ? name : '_blank';

	newWin = this.open(url, name, "toolbar=no,width="+width+",height="+height+",left="+lPos+",top="+tPos+",directories=no,status=no,scrollbars=no,resize=no,menubar=no");
	newWin.focus();
}

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 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;
		}
	}
}

var ConvertDirectoryField = function(field_id)
{
	this.dir_field = document.getElementById(field_id);
}//end ConvertDirectoryField

ConvertDirectoryField.prototype.update = function(value, append)
{
	this.dir_field.value = this.convertString(value);
	if(typeof append != 'undefined') {
		this.dir_field.value = this.dir_field.value + append;
	}
}

ConvertDirectoryField.prototype.convertString = function(value)
{
	value = value.toLowerCase();
	value = value.replace(/&/g, 'and');
	value = value.replace(/[^A-Za-z0-9]/g, '-');
	value = value.replace(/-{2,}/g, '-');

	return value;
}

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(val)) {
		this.raiseError(msg);
		return false;
	}
	return true;
}

FormValidator.prototype.isWithinNumericRange = function(value, min, max, msg)
{
	if(val < min || val > 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;
}

/**
 * usage:
 *
 * var limiter = new CharacterLimit(150, document.getElementById('some-input-field'), document.getElementById('some-element'));
 * limiter.update();
 */
CharacterLimit = function(limit, field, track)
{
	this.limit = limit;
	this.field = field;
	this.track = track || false;

	var tmp = this;
	this.field.onkeyup = function() { tmp.update(); }

	this.update = function() {
		if(this.field.value.length > this.limit) {
			alert('Character limit has been reached.');
			this.field.value = this.field.value.substring(0, this.limit);
		}

		if(this.track) {
			this.track.innerHTML = '' + this.limit - this.field.value.length;
		}
	}
}
