function trim(s) {

    if((s==null)||(typeof(s)!='string')||!s.length) {
        return '';
    }
    return s.replace(/^\s+/,'').replace(/\s+$/,'');

}

function check_blank(field) {

    if (field == '') {
        return false;
    }
    if (trim(field) == '') {
        return false;
    }
    return true;
}

function echeck(str) {

      var at="@";
      var dot=".";
      var lat=str.indexOf(at);
      var lstr=str.length;
      var ldot=str.indexOf(dot);

      if (str.indexOf(at)==-1){
           return false;
      }

      if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
          return false;
      }

      if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
          return false;
      }

      if (str.indexOf(at,(lat+1))!=-1){
          return false;
      }

      if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
          return false;
      }

      if (str.indexOf(dot,(lat+2))==-1){
          return false;
      }

      if (str.indexOf(" ")!=-1){
          return false;
      }

      return true;
}

function validate_login() {

    var userName = document.form1.username.value;
    var password = document.form1.password.value;

    if (!check_blank(userName)) {
        alert("Username cannot be blank.");
        return false;
    }

    if (!check_blank(password)) {
        alert("Password cannot be blank.");
        return false;
    }

    return true;
}

function validate_password() {

    var p1 = document.form1.p1.value;
    var p2 = document.form1.p2.value;

    if (!check_blank(p1)) {
        alert("Password cannot be blank.");
        return false;
    }

    if (!check_blank(p2)) {
        alert("Re-enter Password cannot be blank.");
        return false;
    }

    return true;
}

function validate_login2() {

    var userName = document.form2.username.value;
    var password = document.form2.password.value;

    if (!check_blank(userName)) {
        alert("Username cannot be blank.");
        return false;
    }

    if (!check_blank(password)) {
        alert("Password cannot be blank.");
        return false;
    }

    return true;
}

function validate_subscribe() {

    var name = document.form3.name.value;
    var email = document.form3.email.value;

    if (!check_blank(name)) {
        alert("Name cannot be blank.");
        return false;
    }

    if (!check_blank(email)) {
        alert("Email cannot be blank.");
        return false;
    }

    if (!echeck(email)) {
        alert ("Email address is invalid.");
        return false;
    }

    return true;
}

