/*
  Add several methods to String.
  Steaven Woyan
*/

//add a trim() function to String
//removes white space from the beginning and end
String.prototype.trim = function() {  
  return this.replace(/^\s+|\s+$/g, '');
};

//add a ltrim() function to String
//removes white space from the beginning
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,'');
}

//add a rtrim() function to String
//removes white space from the end
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,'');
}

//add a xmlFixUp() function to String
//does xml 'safe' replacements for char data
String.prototype.xmlFixUp = function() {
  return this.replace(/&/g, "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("'", "&apos;").replace("\"", "&quot;");
  //return this.replace("&", "test").replace("<", "test").replace(">", "test").replace("'", "test").replace("\"", "test");
};

//add a isEmail() function to String
//does the string conform to a 'normal' email address
String.prototype.isEmail = function() {  
  return (null != this.match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/));
};

//add a isNumeric() function to String
//does the string conform to a 'normal' number
String.prototype.isNumeric = function() {  
  return (null != this.match(/^[-+]?\d*\.?\d+$/));
};

//add a isInteger() function to String
//does the string conform to a 'normal' integer
String.prototype.isInteger = function() {  
  return (null != this.match(/^[-+]?\d+$/));
};

//add a isUSADate() function to String
//does the string conform to a 'normal' USA date mm/dd/yyyy
String.prototype.isUSADate = function() {  
  var r;
  //are we formatted correctly?  
  r = (null != this.match(/^((?:0[1-9])|(?:1[012]))\/((?:0[1-9])|(?:[12][0-9])|(?:3[01]))\/(\d{4})$/));
  if(!r) {
    return false;
  }
  //we are formatted correctly, now is it valid with the given date?
  var m = parseInt(RegExp.$1,10);
  var d = parseInt(RegExp.$2,10);
  var y = parseInt(RegExp.$3,10);
  
  if(y <= 0) {
    return false;
  }  
  if(m==2) {
    if(d > 29) {return false;}
    if(d==29 && !isLeap(y)) {return false;}
  }
  if(m==4 || m==6 || m==9 || m==11) {
    if(d > 30) {return false;}
  }
  return true;
};

//add a isISODate() function to String
//does the string conform to a 'normal' ISO date yyyy-mm-dd
String.prototype.isISODate = function() {  
  var r;
  //are we formatted correctly?
  r = (null != this.match(/^(\d{4})-((?:0[1-9])|(?:1[012]))-((?:0[1-9])|(?:[12][0-9])|(?:3[01]))$/));
  if(!r) {
    return false;
  }
  //we are formatted correctly, now is it valid with the given date?
  var m = parseInt(RegExp.$2,10);
  var d = parseInt(RegExp.$3,10);
  var y = parseInt(RegExp.$1,10);
  
  if(y <= 0) {
    return false;
  }  
  if(m==2) {
    if(d > 29) {return false;}
    if(d==29 && !isLeap(y)) {return false;}
  }
  if(m==4 || m==6 || m==9 || m==11) {
    if(d > 30) {return false;}
  }
  return true;
};
