// JavaScript Document
function validateForm(theForm) {
  var reason = "";
  switch (theForm.name) {
    case "register":
    reason += comparePasswords(theForm);
    reason += validatePassword(theForm.password);
    reason += validatePassword(theForm.verifyPassword);
    reason += validateEmail(theForm.emailAddress);
    break;
    case "newUser":
    reason += comparePasswords(theForm);
    reason += validatePassword(theForm.password);
    reason += validatePassword(theForm.verifyPassword);
    reason += validateEmail(theForm.emailAddress);
				reason += validateUserName(theForm.username);
    break;
    case "editUserPassword":
    reason += comparePasswords(theForm);
    reason += validatePassword(theForm.oldPassword);
    reason += validatePassword(theForm.password);
    reason += validatePassword(theForm.verifyPassword);
    break;
default:
	break;
  }
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function checkSubscribeEmail() {
	document.getElementById('submittedEmailAddress').className='black';
	document.getElementById('submittedEmailAddress').value='';
}

function checkPassword(val) {
var proscribedChars = /[\W_]/;
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(proscribedChars) != -1) {
  var tst = val.value.substring(0, (strLength) - 1);
  val.value = tst;
  }
}

function checkUserName(val) {
var proscribedChars = /[\W_]/;
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(proscribedChars) != -1) {
  var tst = val.value.substring(0, (strLength) - 1);
  val.value = tst;
  }
}

function checkEmail(val) {
var proscribedChars = /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);

if(lchar.match(proscribedChars)) {
  var tst = val.value.substring(0, (strLength) - 1);
  val.value = tst;
  }
}


function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(val) {
    var error="";
    var tVal = val.value;
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (val.value == "") {
        val.style.background = '#f00';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tVal)) {
        val.style.background = '#f00';
        error = "Please enter a valid email address.\n";
    } else if (val.value.match(illegalChars)) {
        val.style.background = '#f00';
        error = "The email address contains illegal characters.\n";
    } else {
        val.style.background = '#fff';
    }
    return error;
}

function validatePassword(val) {
    var error = "";
    var illegalChars = /[\W_]/;
    var fieldName = "password";
	if (val.name == "verifyPassword") {
	  fieldName = "verified password"; 	
	}
    if (val.value == "") {
        val.style.background = '#f00';
        error = "You didn't enter a " + fieldName + ".\n";
    } else if ((val.value.length < 5 ) || (val.value.length > 15)) {
        error = "The  " + fieldName + " is the wrong length. \n";
        val.style.background = '#f00';
    } else if (illegalChars.test(val.value)) { // shouldn't be able to get any illegal chars in, anyway
        error = "The " + fieldName + " contains illegal characters.\n";
        val.style.background = '#f00';
	} else if (val.value.search(/[a-zA-Z]/) == -1) {
        error = "The " + fieldName + " must contain at least one alphabetic character.\n";
        val.style.background = '#f00';
	} else if (val.value.search(/\d/) == -1) {
        error = "The " + fieldName + " must contain at least one numeric character.\n";
        val.style.background = '#f00';		
    } else {
        val.style.background = '#fff';
    }
   return error;
}

function validateUserName(val) {
   	var passwordType;
    var error = "";
    var illegalChars = /[\W_]/;
    var fieldName = "username";
   	if (val.name == "username") {
   	  fieldName = "valid username"; 	
   	}
    if (val.value == "") {
        val.style.background = '#f00';
        error = "You didn't enter a " + fieldName + ".\n";
    } else if ((val.value.length < 3 ) || (val.value.length > 15)) {
        error = "The  " + fieldName + " is the wrong length. \n";
        val.style.background = '#f00';
    } else if (illegalChars.test(val.value)) { // shouldn't be able to get any illegal chars in, anyway
        error = "The " + fieldName + " contains illegal characters.\n";
        val.style.background = '#f00';
				} else {
        val.style.background = '#fff';
    }
   return error;
}

function comparePasswords(theForm) {
	var passwordType;
	if (theForm.name == "editUserPassword") {
		 passwordType = "new ";
	}
	var error = "";
	if (theForm.password.value != theForm.verifyPassword.value) {
		error = "The " + passwordType + "passwords do not match.\n";
		theForm.password.style.background = "#f00";
		theForm.verifyPassword.style.background = "#f00";
	}
    return error;
   
}

function checkLength(theForm) {
	 if (theForm.submittedEmailAddress.value.length == 0 || theForm.submittedEmailAddress.value == 'enter email address') {
			 return false;
		}

}