window.std.string = new Object();

window.std.string.strip = window.__String_strip__ = function(string)
{
 if (string)
 while (true)
 {
     string = String(string);
	 var length = string.length;
	 for (var i=1; i<arguments.length; i++)
	 {
	  if (string.substring(0,arguments[i].length)==arguments[i])
	   string = string.substring(arguments[i].length,string.length);
	  
	  if (string.substr(string.length-arguments[i].length)==arguments[i])
		string = string.substring(0,string.length-arguments[i].length);
	 }
	 
	 if (string.length == length)
		 return string;  
 }
 else return false;
};

window.std.string.stripW = window.__String_stripW__ = function(string)
{
 var n1 = n2 = String(string);

 while (true)
 {
  n2 = std.string.strip(n1,' ','\n','\r');

  if (n2.length==n1.length)
   return n2;
  n1 = n2;
 }
};

window.std.string.between = window.__String_between__ = function(string, char1, char2)
{
 if (char1 && string.indexOf(char1)==-1)
 	return '';
	
 var pos1 = char1 ? string.indexOf(char1) + char1.length : 0;
 var pos2 = char2 && string.indexOf(char2, pos1)!=-1 ? string.indexOf(char2, pos1) : string.length;
	
 if (string)
  return string.substring(pos1,pos2);
 else
  return '';	
};

window.std.string.betweenA = window.__String_betweenA__ = function(str, strArray, text)
{
 if (text && text.indexOf(str)!=-1)
 {
  if (strArray && typeof strArray == "object")
  for (var i=0; i<strArray.length; i++)
  {
   if (text.indexOf(strArray[i])!=-1)
    return text.substring(text.indexOf(str)+str.length,text.indexOf(strArray[i]));
  }
  return text.substr(text.indexOf(str)+str.length);
 }
 else
  return '';	
};

/* isNumeric thanks to CodeToad, http://www.codetoad.com/javascript/isnumeric.asp */
window.std.string.isNumeric = window.__String_isNumeric__ = function(string)
{
 var string = String(string);
 var ValidChars = "0123456789.-";
 var IsNumber = true;
 var Char;
 
 
 for (i = 0; i < string.length && IsNumber == true; i++)
 {
  Char = string.charAt(i);
  if (ValidChars.indexOf(Char) == -1)
   IsNumber = false;
 };
 
 return IsNumber;
};

window.std.string.isLogical = window.__String_isLogical__ = function(string)
{
 if (string == 'true' || string == 'false')
  return true;
 else
  return false;
};

window.std.string.isVector = window.__String__isVector__ = function(string, model)
{
 switch (model)
 {
    case '()': 
	if (string.indexOf('(') != -1 && string.indexOf(')') != -1 && string.indexOf(',') != -1)
	 return true;
	 
	case '[]':
	if (string.indexOf('[') != -1 && string.indexOf(']') != -1 && string.indexOf(',') != -1)
	 return true;
	 
	case '{}':
	if (string.indexOf('{') != -1 && string.indexOf('}') != -1 && string.indexOf(',') != -1)
	 return true;
	
	default:
	if (string.indexOf(',') != -1)
	 return true;
 }
 return false;
};

window.std.string.toNumeric = window.__String_toNumeric__ = function(string)
{
 if (std.string.isNumeric(string))
  return parseFloat(string);
 else return null;
};

window.std.string.toLogical = window.__String_toLogical__ = function(string)
{
 if (std.string.isLogical(string))
  return string=='true';
 else return null;
};


window.std.string.toVector = window.__String_toVector__ = function(string, model)
{
    var vector = new Array();
    if (std.string.isVector(string,model))
    {
        var words = string.substring(1,string.length-1).split(',');
        for (var i=0; i<words.length; i++)
            vector.push(parseFloat(words[i]));
        return vector;
    }
    else return null;
};

window.std.string.toFunction = window.__String_toFunction__ = function(string, ref)
{
 try
 {
     var fun = function()
     {
      try
      {
       eval(fun.code);
      }
      catch(e)
      {
       window.instance.exception.error("string",e," in "+string);
      }
     };

     fun.code = String(string);
     fun.ref = ref;

     return fun;
 }
 catch(e)
 {
     window.instance.exception.error("string",e," in "+string);
 }
};

window.std.string.toCamelcase = window.__String_toCamelcase__ = function(string)
{
 return string.replace(/\-(.)/g, function(m, l){return l.toUpperCase()});
};

window.std.string.toArray = window.__String_toArray__ = function(string, seps)
{	
 var ret = new Array();
 var seps = std.def(seps,[';',',',':']);
 
 if (!std.string.search(string, seps))
  return string;

 if (seps[0])
 {
  if (string.indexOf(seps[0])!=-1)
  {
   var words = new Array();
   var syllabels = string.split(seps[0]);
   var word = "";
   
   if (seps.length > 1)
   {
    for (var i=0; i<syllabels.length; i++)
	{
	 if (word != '' && syllabels[i].indexOf(seps[1]) != -1)
	 {
		 words.push(std.string.stripW(word));
		 word = "";
	 }
	 word += (word!=""?seps[0]:"") + syllabels[i];
	}
	words.push(std.string.stripW(word));
   }
   else words = syllabels;
   
   for (var i=0; i<words.length; i++)
   {
	var word = std.string.stripW(words[i]);
    if (seps.length > 1)
 	 ret.push(std.string.toArray(words[i],seps.slice(1)));	
    else
	 ret.push(std.def(std.string.toLogical(word),std.string.toNumeric(word),std.string.toVector(word,'()'),word));
   }
  }
  else return std.string.toArray(string,seps.slice(1));
 }
 
 return ret;
};

window.std.string.toObject = window.__String_toObject__ = function(string, seps)
{
	var array = std.string.toArray(string, seps);
	var fun = function(array, parent)
	{
		if (array.length == 2 && typeof array[0] == 'string')
		{
			parent[std.string.toCamelcase(array[0])] = array[1];
		}
		else
			for (var i=0; i<array.length; i++)
				if (typeof array[i] == 'object')
			 		fun(array[i], array);
	};
	
	fun(array, array);
	return array;
};

window.std.string.toXmlEntities = window.__String_toXmlEntities__ = function(string)
{
 return string.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/'/g,'&apos;').replace(/"/g,'&quot;');
};

window.std.string.count = window.__String_count__ = function(string, char)
{
 var amm = 0;
 for (var i=0; i<string.length; i++)
  if (string.charAt(i) == char)
   amm++;
	
 return amm;
};

window.std.string.search = window.__String_count__ = function(string, thing)
{
	for (var i=0; i<thing.length; i++)
		if (string.indexOf(thing[i])!=-1)
			return true;
};

window.std.string.random = window.__String_random__ = function(length)
{
	var charArray = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	
	var string = "";
	for (var i=0; i<length; i++)
		string += charArray.charAt(Math.random()*Math.pow(10,3)%charArray.length);

	return string;
};

window.std.string.first = function(string)
{
	var pos = min = string.length;
	for (var i=1; i<arguments.length; i++)
	{
		if ((pos = string.indexOf(arguments[i])) != -1 && pos < min)
			min = pos;
	}
			
	return min;
};

window.std.string.last = function(string)
{
	var pos = max = 0;
	for (var i=1; i<arguments.length; i++)
		if ((pos = string.lastIndexOf(arguments[i])) != -1 && pos > max)
			max = pos;
			
	return max;
};

window.std.string.split = function(string)
{
	var ret = new Array();
	var pos = 0;
	for (var i=1; i<arguments.length; i++)
	{
		ret.push(string.substring(pos, arguments[i]));
		pos = arguments[i];
	}
	
	ret.push(string.substr(pos));
	return ret;
};

window.std.string.format = function(string)
{
 return string;
};
