


String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

/**
 * returns string with all leading and trailing characters 
 * eliminated.
 */
function trim(str){
  var s = new String(str);
  //trailing spaces
  while (s.length>0 && isSpaceCharacter(""+s.charAt(s.length-1))){
    s = s.substring(0,s.length-1);
  }
  //leading spaces
  while (s.length>0 && isSpaceCharacter(""+s.charAt(0))){
    s = s.substring(1);
  }
  return s
}

var spaces = " \t\r\n"+String.fromCharCode(160);

function isSpaceCharacter(ch){
  return spaces.indexOf(ch) >-1;
}
/**
 * Validates that input's value is correct email address
 */
function validateEmail(elem) {
    var str = "";
    if (elem.value) {
        str = new String(elem.value);
    } else {
        str = new String(elem);
    }

    if (window.RegExp) {
        var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
        var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$";
        var reg3str = "\"([^\"]+)\"\\s+<{1}([A-z]{1}[A-z0-9\\x2d\\.]*[A-z0-9]{1}@[A-z0-9\\x2d\\.]*[A-z-9]{1}\\.[A-z]{2,5})>{1}";
        var reg1 = new RegExp(reg1str);
        var reg2 = new RegExp(reg2str);
        var reg3 = new RegExp(reg3str);
        if (!reg1.test(str) && (reg2.test(str) || reg3.test(str))) return true;
        return false;
    } else {
        if (str.indexOf("@") >= 0) return true;
        return false;
    }
}

function isEmpty(elem){
  return trim(elem.value).length==0;
}

function validateForm(f){
  if (isEmpty(f.sendto)) {
    alert("Please enter \"Send to\" address");
    f.sendto.focus();
    return false;
  }

  if(!validateEmail(f.sendto)){
      alert("Please enter valid \"Send to\" address");
      f.sendto.focus();
      return false;
  }

  if (isEmpty(f.receipientname)) {
    alert("Please enter receipient name");
    f.receipientname.focus();
    return false;
  }

  if (isEmpty(f.sendfrom)) {
    alert("Please enter \"Send from\" address");
    f.sendfrom.focus();
    return false;
  }

  if(!validateEmail(f.sendfrom)){
      alert("Please enter valid \"Send from\" address");
      f.sendfrom.focus();
      return false;
  }

  if (isEmpty(f.sendername)) {
    alert("Please enter sender name");
    f.sendername.focus();
    return false;
  }

  if (isEmpty(f.subject)) {
    alert("Please enter subject");
    f.subject.focus();
    return false;
  }

  return true;
}

