var CBWeb = CBWeb ? CBWeb : function() {
    // Shortcut function for document.getElementById (cross-browser);
    function createGet() {
        if (document.getElementById)
          return function(id) {return document.getElementById(id);}
        else if (document.all)
          return function(id) {return document.all[id];}
        else if (document.layers)
          return function(id) {return document.layers[id];}
        else
          return function() {return null;}
    }

    return {
        getElementById : createGet()
    };
}();

// String prototype additions
String.prototype.trim = function()
{
  return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}
String.prototype.trimEnd = function()
{
  return this.replace(/(\b.*\b|)\s*$/, "$1");
}
String.prototype.trimStart = function()
{
  return this.replace(/^\s*(\b.*\b|)/, "$1");
}
String.prototype.isDate = function()
{
  var re=/^((0?\d)|(1[0-2]))(\\|\/|-)((0?\d)|([1-2]\d)|(3[0-1]))(\\|\/|-)(\d{2}|\d{4})$/;
  return re.test(this);
}
String.prototype.isTime = function()
{
  var re=/^(((0?[1-9]|1[0-2])(:|\.)[0-5]\d((:|\.)[0-5]\d)?( )?(([aA]|[pP])[mM]))|((0?\d|1\d|2[0-3])(:|\.)[0-5]\d((:|\.)[0-5]\d)?))$/;
  return re.test(this);
}
String.prototype.isDateTime = function()
{
  var re=/^((0?\d)|(1[0-2]))(\\|\/|-)((0?\d)|([1-2]\d)|(3[0-1]))(\\|\/|-)(\d{2}|\d{4})( )(((0?[1-9]|1[0-2])(:|\.)[0-5]\d((:|\.)[0-5]\d)?( )?(([aA]|[pP])[mM]))|((0?\d|1\d|2[0-3])(:|\.)[0-5]\d((:|\.)[0-5]\d)?))$/;

  if (this.isDate()) return true;
  if (this.isTime()) return true;
  if (re.test(this)) return true;
  return false;
}
String.prototype.isInteger = function()
{
  var re=/^-?\d+$/;
  return re.test(this);
}
String.prototype.isPositiveInteger = function()
{
  var re=/^\d+$/;
  return re.test(this);
}
String.prototype.isFloat = function()
{
  var re=/^-?(\d+(\.\d*)?|\.\d+)$/;
  return re.test(this);
}
String.prototype.isPositiveFloat = function()
{
  var re=/^(\d+(\.\d+)?|\.\d+)$/;
  return re.test(this);
}
String.prototype.toDateTime = function(dateOrder)
{
  if (!this.isDateTime())
    return null;

  var y, m, d;

  var yearFirst = new RegExp('^\\s*(\\d{4}|\\d{2})([-/]|\\. ?)(\\d{1,2})\\2(\\d{1,2})\\s*$');
  var match = this.match(yearFirst);
  if (match != null && (dateOrder == 'ymd' || dateOrder == 'ydm'))
  {
    y = match[1];
    if (dateOrder == 'ymd')
    {
      m = match[3];
      d = match[4];
    }
    else
    {
      m = match[4];
      d = match[3];
    }
  }
  if (dateOrder == 'ymd' || dateOrder == 'ydm')
    return null;
  var yearLast = new RegExp('^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2(\\d{4}|\\d{2})\\s*$');
  match = this.match(yearLast);
  if (match == null)
    return null;

  if (dateOrder == 'mdy')
  {
      y = match[4];
      m = match[1];
      d = match[3];
  }
  else if (dateOrder == 'dmy')
  {
      y = match[4];
      m = match[3];
      d = match[1];
  }

  var date = new Date(y, m - 1, d);
  if (y < 100)
      date.setFullYear(y);

  return date;
}
String.prototype.toInteger = function()
{
    if (!this.isInteger())
        return null;

    return parseInt(this);
}
String.prototype.toFloat = function()
{
    if (!this.isFloat())
        return null;

    return parseFloat(this);
}
String.prototype.toBoolean = function()
{
    if (this.toUpperCase() == 'TRUE')
        return true;
    else if (this.toUpperCase() == 'FALSE')
        return false;
    else
        throw 'Not a boolean';
}
String.prototype.getWidth = function()
{
  var span = $('msSpan');
  if (span == null)
  {
    span = document.createElement('span');
    span.id = 'msSpan';
    span.style.visibility = 'hidden';
    document.body.appendChild(span);
  }

  span.innerHTML = this;
  return span.offsetWidth;
}

// Array prototype additions
Array.prototype.indexOf = function(val)
{
	for (var i=0; i<this.length; i++)
		if (this[i]==val)
			return i;
	return -1;
}
