/** String Functions **/
function chomp(str, delim)
{
	return str.substr(0, str.indexOf(delim));
}

function getChomp(str, delim)
{
	return str.substr(str.indexOf(delim) + 1); 
}

function padl(str, len, c)
{
  var result = new String(str);
  while (result.length < len)
    result = c.concat(result);
  return result;
}

function trim(str)
{
  var sIdx = 0;
  var eIdx 
  for (var i = 0; i < str.length; i++)
  {
    if ((" \n\r\t").indexOf(str.charAt(i)) == -1)
    {
    	sIdx = i;
      break;
    }
  }
  for (var i = str.length - 1; i >= 0; i--)
    if ((" \n\r\t").indexOf(str.charAt(i)) == -1)
    {
    	eIdx = i;
      break;
    }
  return str.substr(sIdx, (eIdx - sIdx) + 1);
}

/** Objects **/

/*
 * AjaxRequest Object
 *   - string getResponse()
 *   - boolean isReady()
 *   - void send(url, method, catchData)
 */
function AjaxRequest()
{
	var created = false;
	this.busy = false;
	this.returnFunction = function() {};
	try 
	{ 
		this.xhReq = new ActiveXObject("Msxml2.XMLHTTP"); 
		created = true; 
	} 
	catch (e) {}
  try 
  { 
  	this.xhReq = new ActiveXObject("Microsoft.XMLHTTP"); 
  	created = true; 
  } 
  catch (e) {}
  try 
  { 
  	this.xhReq = new XMLHttpRequest(); 
  	created = true; 
  } 
  catch (e) {}
  if (!created)
  	alert("XMLHttpRequest not supported");
}

AjaxRequest.prototype.getResponse = function()
{
	try
	{	
		return this.xhReq.responseText;
	}
	catch (e) {}
}

AjaxRequest.prototype.send = function(url, method, returnFunction)
{
	this.xhReq.open(method, url, true);
  this.xhReq.onreadystatechange = returnFunction;
  this.xhReq.send(null);
}

AjaxRequest.prototype.post = function(url)
{
	this.xhReq.open("POST", url, true);
  this.xhReq.send(null);
}

AjaxRequest.prototype.isReady = function()
{
	return this.xhReq.readyState == 4;
}

/*
 * List Object
 *   - void add(array)
 *   - void clear()
 *   - array get(index)
 	 - array getById(id)
 *   - int length()
 *   - void remove(index)
 *   - void sort(col)
 */
function List()
{
	this.array = new Array();
}

List.prototype.add = function(array)
{
	var len = this.array.length;
	if (array.length == 1)
		this.array[len] = array[0];
	else
		this.array[len] = array;
}

List.prototype.clear = function()
{
	this.array.length = 0;
}

List.prototype.get = function(index)
{
	return this.array[index];
}

List.prototype.getById = function(id)
{
	for (i = 0; i < this.length(); i++)
	{
		var e = this.get(i);
		if (e.id == id)
			return e;
	}
}

List.prototype.length = function()
{
	return this.array.length;
}

List.prototype.remove = function(index)
{
	var result = Array();
	for (i = 0; i < this.array[index].length; i++)
		result[i] = this.array[index][i];
		
	for (j = index + 1; j < this.array.length; j++)
		this.array[j-1] = this.array[j];
		
	this.array.length = this.array.length - 1;
}

List.prototype.sort = function(col)
{
	for (var i = 0; i < this.array.length; i++)
	{
		for (var j = this.array.length - 1; j > i; j--)
		{
			if ((this.array[j-1].length < col && this.array[j].length >= col) ||
					this.array[j-1].length >= col && this.array[j].length >= col &&	this.array[j-1][col].toUpperCase() > this.array[j][col].toUpperCase())
			{
				var temp = this.array[j];
				this.array[j] = this.array[j-1];
				this.array[j-1] = temp;
			}
		}
	}
}

/*
 * StringTokenizer Object
 *   - boolean hasTokens()
 *   - string nextToken()
 */
function StringTokenizer(str, delims)
{
  this.delims = delims;
  this.str = str;
  this.idx = 0;
}

StringTokenizer.prototype.hasTokens = function()
{
  return (this.idx != this.str.length);
}

StringTokenizer.prototype.nextToken = function()
{
  var rem = this.str.substr(this.idx, this.str.length);
  var eIdx = -1;
  for (var i = 0; i < rem.length; i++)
  {
    if (this.delims.indexOf(rem.charAt(i)) != -1)
    {
      eIdx = i;
      break;
    }
  }
  if (eIdx != -1)
  {
    this.idx += eIdx + 1;
    return rem.substr(0, eIdx);
  }
  else if (this.hasTokens())
  {
    this.idx = this.str.length; 
    return rem;
  }
  else
    return null;
}

//Remove html tags
String.prototype.removeHtml = function()
{
	var tmpString = this.replace(/&(lt|gt);/g, 
	
	function (strMatch, p1)
	{
 		return (p1 == "lt")? "<" : ">";
	});

	return tmpString.replace(/<\/?[^>]+(>|$)/g, "");
}

String.prototype.replaceAll = function(findString, newString)
{
	var result = this;
	while(result.indexOf(findString) != -1)
		result = result.replace(findString, newString)
	
	return result;
}
