//Key utilitiy functions
function getkey(e)
{
	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;
}

//Checks the given key press against a set of valid
//characters.  If character is not in the valid set
//then return false.
function checkChars(e, validChars)
{
	var key, keychar;
	key = getkey(e);
	if (key == null) return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	checks = validChars.toLowerCase();

	if (checks.indexOf(keychar) != -1)
		return true;

	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	   return true;

	return false;
}

//Checks that a key press is numeric
function checkNumeric(e){
   return checkChars(e,'01234567890');
}

function isValue(e){
	return (e!=undefined)&&(e!=null);
}


//Cookie handling code
function Cookie(document, name, expiration, path, domain, secure)
{
	this._document = document;
	this._name = name;

	if(expiration)
		this._expiration = expiration
	else this._expiration = null;

	if(path) this._path = path;
	else this._path = null;

	if(domain) this._domain = domain;
	else this._domain = null;

	if(secure) this._secure = secure;
	else this._secure = false;
}

function _Cookie_store()
{
	this._document.cookie = this.toString();
} // _Cookie_store()

function _Cookie_load()
{
	var ok = false;

	var cookieStr = this._document.cookie;
	if (cookieStr && cookieStr != "")
	{
		var start = cookieStr.indexOf(this._name + '=');
		if(start != -1)
		{
			start += this._name.length + 1;
			var end = cookieStr.indexOf(';', start);
			if (end == -1) end = cookieStr.length;
			var cookieVal = cookieStr.substring(start, end);

			var a = cookieVal.split('&');
			for(var i = 0; i < a.length; i++)
				a[i] = a[i].split(':');

			for(var i = 0; i < a.length; i++)
				this[a[i][0]] = unescape(a[i][1]);

			ok = true;
		}
	}
	return ok;
} // _Cookie_load()

function _Cookie_remove()
{
	var cookie = this._name + '=';
	if (this._path) cookie += '; path=' + this._path;
	if (this._domain) cookie += '; domain=' + this._domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

	this._document.cookie = cookie;
}

function _Cookie_toString()
{
	var cookieval = "";
	for(var prop in this)
	{
		if ((prop.charAt(0) != '_') && (typeof this[prop] != 'function'))
		{
			if (cookieval != "") cookieval += '&';
			cookieval += prop;
			if(this[prop]&&this[prop]!='undefined'){
				cookieval+= ':' + escape(this[prop].toString());
			}
		}
	}
	var cookieStr = this._name + '=' + cookieval;

	if(this._expiration) cookieStr += '; expires=' + this._expiration.toString();
	if(this._path) cookieStr += '; path=' + this._path;
	if(this._domain) cookieStr += '; domain=' + this._domain;
	if(this._secure) cookieStr += '; secure=' + secure;

	return cookieStr;
}

Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;
Cookie.prototype.toString = _Cookie_toString;
//End Cookie Handling

//String enhancements
String.prototype.rmaLTrim = function(){
	return this.replace(/\s*((\S+\s*)*)/,"$1");
}

String.prototype.rmaRTrim = function(){
	return this.replace(/((\s*\S+)*)\s*/,"$1");
}

String.prototype.rmaTrim = function(){
	return this.replace(/^\s*|\s*$/g,"");
}

function isEmpty(value){
  return (!value)||(value.rmaTrim()=="");
}

function getQueryVar(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}


