/*
© 2005, 2006 MSH Medien System Haus
*/

String.prototype.trim = function() {
    return(this.replace("/^\\s+/",'').replace("/\\s+$/",''));
};


String.prototype.wrap = function(wrapAt, wrapWith) {
if(!wrapWith) wrapWith = '\n';
var wrap = '';
var count = 0;
do
{
count += wrapAt;
wrap += this.substr(count-wrapAt, wrapAt) + wrapWith;
} while(count < this.length)

return wrap;
};


String.prototype.replaceStr = function (fromString, toString) {
   
   var temp = this;
   if (fromString == "") {
      return this;
   }
   if (toString.indexOf(fromString) == -1) { 
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { 
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      
      
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } 
      
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } 
   return temp; 
};


String.prototype.endsWith = function(str) {
    return (this.length-str.length)==this.lastIndexOf(str);
};


String.prototype.reverse = function() {
    var s = "";
    var i = this.length;
    while (i>0) {
        s += this.substring(i-1,i);
        i--;
    }
    return s;
};


String.prototype.toInt = function() {
    var a = new Array();
    for (var i = 0; i < this.length; i++) {
        a[i] = this.charCodeAt(i);
    }
    return a;
};


String.prototype.toXHTML = function()
{
  return this.replace(/[< ]+([^= ]+)/gi, function($1){return $1}).
  replace(/\s*=\s*(['"])?(([^>" ]| (?=[^"=]+['"]))+)\1?/gi, '="$2"').
  
  replace(/(checked|compact|declare|defer|disabled|ismap|multiple|no(href|resize|shade|wr  ap)|readonly|selected)/gi, '$1="$1"').
  replace(/_moz[^=]*=\s*\S*/g, '').
  replace(/(="[^']*)'([^'"]*")/, '$1$2').
  
  replace(/\s{2,}/g, ' ');
};