
Array.prototype.sortNum = function() 
{
   return this.sort( function (a,b) { return a-b; } );
}

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

function fast_trim (str) 
{
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) 
	{
		if (/\S/.test(str.charAt(i))) 
		{
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}
function setCookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else 
	{
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function removeCookie(name) 
{
	setCookie(name,"",-1);
}

//**********************************************************************************************************
//*
//*  ajaxGet
//*
//*  Sends an ajax GET request to the given URL
//*
//**********************************************************************************************************
function ajaxGet(url, data, callback, bustcache)
{
	if (bustcache != false)
	{
		data['cachebust']=Math.random();
	}
	$.ajax({cache:true,
			data:data,
			dataType:"json",
			error:internalAjaxErrorHandler,
			processData:true,
			success:internalAjaxSuccessHandler,
			type:"GET",
			url:url,
			x_user_callback:callback});
}

//**********************************************************************************************************
//*
//*  ajaxPost
//*
//*  Sends an ajax POST request to the given URL
//*
//**********************************************************************************************************
function ajaxPost(url, data, callback, bustcache)
{
	if (bustcache != false)
	{
		data['cachebust']=Math.random();
	}
	$.ajax({cache:true,
			data:data,
			dataType:"json",
			error:internalAjaxErrorHandler,
			processData:true,
			success:internalAjaxSuccessHandler,
			type:"POST",
			url:url,
			x_user_callback:callback});
}

//**********************************************************************************************************
//*
//*  internalAjaxErrorHandler
//*
//*  Takes care of notifying the correct callback of the error condition
//*
//**********************************************************************************************************
function internalAjaxErrorHandler(XMLHttpRequest, textStatus, errorThrown)
{
  // typically only one of textStatus or errorThrown
  // will have info
  this; // the options for this ajax request
  
  if (this.x_user_callback)
  {
	this.x_user_callback(false, null, textStatus == null ? errorThrown : textStatus);
  }
}

//**********************************************************************************************************
//*
//*  internalAjaxSuccessHandler
//*
//*  Takes care of notifying the correct callback of the successful result
//*
//**********************************************************************************************************
function internalAjaxSuccessHandler(data, textStatus)
{
  // data could be xmlDoc, jsonObj, html, text, etc...
  this; // the options for this ajax request
  
  if (this.x_user_callback)
  {
	  this.x_user_callback(true, data, textStatus);
  }
}

//**********************************************************************************************************
//*
//*  verifyEmail
//*
//*  Basic attempt at verifying an email address
//*
//**********************************************************************************************************
function verifyEmail(email)
{
	var e = fast_trim(email);
	if (e == "") return false;
	
	var at = e.indexOf("@", 0);
	var dot = -1;
	if (at >= 0)
	{
		dot = e.indexOf(".", at);
	}
	if (at > 0 && dot > at + 1 && dot < e.length - 1)
	{
		return true;
	}
	return false;
}


