
/*  $Id: //depot/princeorg/main/www/_js/register.js#7 $  */

//checking that user does not input any bad characters for username or password
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
function dodacheck(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  if(lchar.search(mikExp) != -1) {
    var tst = val.value.substring(0, (strLength) - 1);
    val.value = tst;
  }
}
//checking to make sure user has completed all required fields
var whitespace = " \t\n\r";
var defaultEmptyOK = false;
var digits = "0123456789";

function isEmpty(s) {
  return ((s == null) || (s.length == 0));
}

function isWhitespace (s) {
  var i;
  // Is s empty?
  if (isEmpty(s)) return true;
  // Search through string's characters one by one
  // until we find a non-whitespace character.
  // When we do, return false; if we don't, return true.
  for (i = 0; i < s.length; i++) {   
    // Check that current character isn't whitespace.
    var c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) return false;
  }
  // All characters are whitespace.
  return true;
}

function stripCharsInBag(s, bag) {   
  var i;
  var returnString = "";
  for (i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

function stripCharsNotInBag(s, bag) {
  var i;
  var returnString = "";
  for (i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if (bag.indexOf(c) != -1) returnString += c;
  }
  return returnString;
}

function stripWhitespace(s) {   
  return stripCharsInBag(s, whitespace);
}

function isLetter (c) {
  return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}

function isDigit (c) {
  return ((c >= "0") && (c <= "9"));
}

function isInteger (s) {
  var i;
  if (isEmpty(s)) 
    if (isInteger.arguments.length == 1) return defaultEmptyOK;
    else return (isInteger.arguments[1] == true);
  
  // Search through string's characters one by one
  // until we find a non-numeric character.
  // When we do, return false; if we don't, return true.
  
  for (i = 0; i < s.length; i++) {
    // Check that current character is number.
    var c = s.charAt(i);
    if (!isDigit(c)) return false;
  }
  
  // All characters are numbers.
  return true;
}

function isEmail (s) {
  if (isEmpty(s)) 
    if (isEmail.arguments.length == 1) return defaultEmptyOK;
    else return (isEmail.arguments[1] == true);
  // is s whitespace?
  if (isWhitespace(s)) return false;
  // there must be >= 1 character before @, so we
  // start looking at character position 1 
  // (i.e. second character)
  var i = 1;
  var sLength = s.length;
  // look for @
  while ((i < sLength) && (s.charAt(i) != "@")) {
    i++;
  }
  if ((i >= sLength) || (s.charAt(i) != "@")) return false;
  else i += 2;
  // look for .
  while ((i < sLength) && (s.charAt(i) != ".")) {
    i++;
  }
  // there must be at least one character after the .
  if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
  else return true;
}

function checkEmail(email) {
  return isEmail(email);
}

function checkZip(zip) {
  if (isEmpty(zip)) return false;
  var tempphone = stripCharsNotInBag(zip, digits);
  return (tempphone.length==9||tempphone.length==5);
}

function OnSubmitFunc() {
  document.reg.uname.value = stripWhitespace(document.reg.uname.value);
  document.reg.email.value = stripWhitespace(document.reg.email.value);
  document.reg.zip.value = stripWhitespace(document.reg.zip.value);
  
  var strFields = "";
  var blnFocus = false;
  
  //checking user name
  if (document.reg.uname.value == "" || document.reg.uname.value.length < 3) {
    strFields += "Username (empty or too short)\n"
      if (!blnFocus) {
	document.reg.uname.focus();
	blnFocus = true;
      }
  }

  //checking password 1
  if (document.reg.pword1.value == "" || document.reg.pword1.value.length < 6) {
    strFields += "Password (empty or too short)\n";
    if (!blnFocus) {
      document.reg.pword1.focus();
      blnFocus = true;
    }
  }

  //checking password 2
  if (document.reg.pword2.value == "") {
    strFields += "Re-type password\n";
    if (!blnFocus) {
      document.reg.pword2.focus();
      blnFocus = true;
    }
  }
  
  //check password question
  var sel_pass = document.reg.pw_question.options.selectedIndex;
  var pass = document.reg.pw_question.options[sel_pass].value; 
  
  if (pass =="") {
    strFields += "Question we'll ask\n";
    if (!blnFocus) {
      document.reg.pw_question.focus();
      blnFocus = true;
    }
  }
  
  //checking password answer
  document.reg.pw_answer.value = trim(document.reg.pw_answer.value);
  if (document.reg.pw_answer.value == "") {
    strFields += "Your password answer\n";
    if (!blnFocus) {
      document.reg.pw_answer.focus();
      blnFocus = true;
    }
  }

  //checking first name
  document.reg.fname.value = trim(document.reg.fname.value);
  if (document.reg.fname.value == "") {
    strFields += "First Name\n";
    if (!blnFocus) {
      document.reg.fname.focus();
      blnFocus = true;
    }
  }

  //checking last name
  document.reg.lname.value = trim(document.reg.lname.value);
  if (document.reg.lname.value == "") {
    strFields += "Last Name\n";
    if (!blnFocus) {
      document.reg.lname.focus();
      blnFocus = true;
    }
  }

  //checking city
  document.reg.city.value = trim(document.reg.city.value);
  if (document.reg.city.value == "") {
    strFields += "City\n";
    if (!blnFocus) {
      document.reg.city.focus();
      blnFocus = true;
    }
  }
  
  if (document.reg.region.value < 1) {
    strFields += "Region\n";
    if (!blnFocus) {
      document.reg.region.focus();
      blnFocus = true;
    }
  }

  var sel_st = document.reg.state.options.selectedIndex;
  var st = document.reg.state.options[sel_st].value; 
  
  var sel_country = document.reg.country.options.selectedIndex;
  var coun = document.reg.country.options[sel_country].value; 
  
  //check zip & state if country is USA
  if (coun=="USA") {
    if (!(checkZip(document.reg.zip.value))) {
      strFields += "Zip/Postal Code\n";
      if (!blnFocus) {
      	document.reg.zip.focus();
        blnFocus = true;
      }
    }
  
    if (st == "" || "--" == st) {
      strFields += "State\n";
      if (!blnFocus) {
      	document.reg.state.focus();
        blnFocus = true;
      }
    }
  }
  
  // check country				    
  if ("" == coun || "--" == coun) {
    strFields += "Country\n";
    if (!blnFocus) {
      document.reg.country.focus();
      blnFocus = true;
    }
  }
  
  // checking email
  if (!(checkEmail(document.reg.email.value))) {
    strFields += "Email\n";
    if (!blnFocus) {
      document.reg.email.focus();
      blnFocus = true;
    }
  }
  
  if (strFields != "") {
    alert("The following fields had problems:\n\n" + strFields);
    return false;
  }
  
  if (document.reg.uname.value.length<3 ||
      document.reg.uname.value.length>25) {
    alert ("Username must be 3 and 25 characters");
    document.reg.uname.select();
    return false;
  }

  if (document.reg.pword1.value.length<6) {
    alert ("Password must be at least 6 characters");
    document.reg.pword1.select();
    return false;
  }
  
  if (document.reg.pword1.value == document.reg.uname.value) {
    alert ("The password you selected is the same as your username. This is not secure; please select a different password.");
    document.reg.pword1.select();
    return false;
  }
  
  if (document.reg.pword1.value!=document.reg.pword2.value) {
    alert("The password and re-type password fields must match!");
    document.reg.pword2.select();
    return false;
  }

  if (document.reg.agree.checked==false) {
    alert ("You must agree to the user agreement.");
    return false;
  }
  
  return true;
}

function doSubmitForm() {
  if (OnSubmitFunc()) {
    document.reg.submit();
  }
}
