/**
 * @author jollymor
 */

var aok;
var stuff = new Array();

function displayDesc() {
    var list = document.getElementById("evalId");
    var txt = document.getElementById("descrip");
    txt.innerHTML = stuff[list.selectedIndex];
}
function getFirstDesc() {
    var txt = document.getElementById("descrip");
    txt.innerHTML = stuff[0];
}
function addDescription(desc) {
    var len = stuff.push(desc);
}


function enforceMaximumLength(fld,len) {
    var sp = document.getElementById("chars");
    var left = (len - fld.value.length);
    sp.innerHTML = "(" + left + " left)";
    if (left <= 0) {
        sp.style.color = "Red";
    } else {
        sp.style.color = "Black";
    }
}


  function validateId(formName, stdid) {
  	var ok = true;
  	  if (stdid.value == "") {
	  	document.getElementById("messages").innerHTML = "You must enter your Sheridan student ID number.";
		document.getElementById("stdidErr").innerHTML = "*";
        document.getElementById(formName).stdid.focus();
        ok = false;
	  } else if (stdid.value.length !== 9) {
	  	document.getElementById("messages").innerHTML = "Your student number is 9 digits long.  You must enter exactly 9 digits.";
		document.getElementById("stdidErr").innerHTML = "*";
        document.getElementById(formName).stdid.focus();
        ok = false;
	  } 
	 return ok;
  }
  function validateLogin(formName, login) {
  	var ok = true;
  	if (login.value == ""){
		  document.getElementById("messages").innerHTML = "You must enter your Sheridan user name or login name.";
	      document.getElementById("loginErr").innerHTML = "*";
	      document.getElementById(formName).login.focus();
	      ok = false;
	 }
	 return ok;
  }
  function validatePass(formName, pass) {
  	 var ok = true;
  	 if (pass.value == ""){
 		  document.getElementById("messages").innerHTML = "You must enter your password in order to successfully activate.";
          document.getElementById("passErr").innerHTML = "*";
          document.getElementById(formName).pass.focus();
          ok = false;
       }
	  return ok;
  }
  function validateConfirm(formName, confirm, pass) {
  	var errFlag = 0;
	var ok = true;
    if (confirm.value == ""){
		  	document.getElementById("messages").innerHTML = "You must confirm the password you wish to use.";
	  	errFlag = 1;
	} else if (confirm.value != pass.value) {
		document.getElementById("messages").innerHTML = "The Password and Confirm Password fields don't match.";
		document.getElementById("passErr").innerHTML = "*";
	  	errFlag = 1;
    }
	if (errFlag == 1) {
		document.getElementById("confErr").innerHTML = "*";
      	document.getElementById(formName).confirm.focus();
      	ok = false;
	}
	return ok;
  }
  
  function validateEmail(formName, email) {
  	var ok = true;
  	if (email.value == ""){
 		  document.getElementById("messages").innerHTML = "You must enter a new email address.";
          document.getElementById("emailErr").innerHTML = "*";
          document.getElementById(formName).email.focus();
          ok = false;
		  
    } else {
		invalidemail = 0;
		var at="@"
		var dot="."
		var lat=email.value.indexOf(at)
		var lstr=email.value.length
		var ldot=email.value.indexOf(dot)
		if ((lat <= 0) || (lat >= lstr)){
		   invalidemail = 1;
		} else if ((ldot <= 0) || (ldot >= lstr)){
		   invalidemail = 1;
		} else if (email.value.indexOf(at,(lat+1)) > -1){
		   invalidemail = 1;
		} else if ((email.value.substring(lat-1,lat)==dot) || (email.value.substring(lat+1,lat+2)==dot)){
		   invalidemail = 1;
		} else if (email.value.indexOf(dot,(lat+2))==-1){
		   invalidemail = 1;
		} else if (email.value.indexOf(" ") >= 0){
		   invalidemail = 1;
		}
		if (invalidemail == 1) {
			document.getElementById("messages").innerHTML = "Your email address is invalid.";
			document.getElementById("emailErr").innerHTML = "*";
			document.getElementById(formName).email.focus();
			ok = false;
		}
	}
	return ok;
  }
  
  function validateConfEmail(formName, confEmail, email) {
  	var ok = true;
	if (confEmail.value == "") {
		document.getElementById("messages").innerHTML = "You must confirm your new email address.";
        document.getElementById("confEmailErr").innerHTML = "*";
        document.getElementById(formName).confEmail.focus();
        ok = false;
	} else if (confEmail.value != email.value) {
		document.getElementById("messages").innerHTML = "Your emails don't match - please check your email in both fields.";
        document.getElementById("confEmailErr").innerHTML = "*";
		document.getElementById("emailErr").innerHTML = "*";
        document.getElementById(formName).confEmail.focus();
        ok = false;
	} else {
		invalidemail = 0;
		var at="@"
		var dot="."
		var lat=confEmail.value.indexOf(at)
		var lstr=confEmail.value.length
		var ldot=confEmail.value.indexOf(dot)
		if ((lat <= 0) || (lat >= lstr)) {
		   invalidemail = 1;
		} else if (ldot <= 0 || ldot >= lstr) {
		   invalidemail = 1;
		} else if (confEmail.value.indexOf(at,(lat+1)) > -1) {
		   invalidemail = 1;
		} else if ((confEmail.value.substring(lat-1,lat)==dot) || (confEmail.value.substring(lat+1,lat+2)==dot)) {
		   invalidemail = 1;
		} else if (confEmail.value.indexOf(dot,(lat+2))==-1) {
		   invalidemail = 1;
		} else if (confEmail.value.indexOf(" ") >= 0) {
		   invalidemail = 1;
		}
		if (invalidemail == 1) {
			document.getElementById("messages").innerHTML = "Your confirm email address is invalid.";
			document.getElementById("confEmailErr").innerHTML = "*";
			document.getElementById(formName).confEmail.focus();
			ok = false;
		}		
	}
	return ok;
  }
  function validateActData (){
	clearActiveErrs();
	
    var nowcont = true;

	var stdid = document.getElementById("stdid");
	var login = document.getElementById("login");
	var pass = document.getElementById("pass");
	var confirm = document.getElementById("confirm");
	
	nowcont = validateId("actForm", stdid);     
	
    if (nowcont == true){
		nowcont = validateLogin("actForm", login);
    }
	
    if (nowcont == true){
       nowcont = validatePass("actForm", pass);
    }
	if (nowcont == true){
	  	nowcont = validateConfirm("actForm", confirm, pass);
    }
    return nowcont;
}
function validateLoginData (){
	clearLoginErrs();
    var nowcont = true;
	var stdid = document.getElementById("stdid");
	var login = document.getElementById("login");
	var pass = document.getElementById("pass");
	   
	nowcont = validateId("loginForm", stdid);
	
	if (nowcont == true){
		nowcont = validateLogin("loginForm", login);
    }
	
    if (nowcont == true){
       nowcont = validatePass("loginForm", pass);
    }

    return nowcont;
  }
function validatePwData (){
	clearPwErrs();
    var nowcont = true;
	var pass = document.getElementById("pass");
	var confirm = document.getElementById("confirm");
	var oldpw = document.getElementById("oldpw");
    if (nowcont == true){
       nowcont = validatePass("changepwForm", pass);
    }
	if (nowcont == true){
	  	nowcont = validateConfirm("changepwForm", confirm, pass);
    }
	
	if (nowcont == true){
        if (oldpw.value == ""){
 		  document.getElementById("messages").innerHTML = "You must enter your old password.";
          document.getElementById("oldpwErr").innerHTML = "*";
          document.getElementById("changepwForm").oldpw.focus();
          nowcont = false;
        }
    }
    return nowcont;
  }
  
  function validateEmailData (){
	clearEmailErrs();
    var nowcont = true;
	var pass = document.getElementById("pass");
	var email = document.getElementById("email");
	var confEmail = document.getElementById("confEmail");
	
	if (nowcont == true){
      nowcont = validateEmail("emailForm", email);
    }
	if (nowcont == true) {
		nowcont = validateConfEmail("emailForm", confEmail, email);
	}
    if (nowcont == true){
	  nowcont = validatePass("emailForm", pass);
    }
    
    return nowcont;
  }
  
function clearLoginErrs() {

	document.getElementById("messages").innerHTML = "";
	document.getElementById("stdidErr").innerHTML="";
	document.getElementById("loginErr").innerHTML = "";
	document.getElementById("passErr").innerHTML = "";
}
function clearActiveErrs() {
	clearLoginErrs();
	document.getElementById("confErr").innerHTML="";
}

function clearPwErrs() {
	document.getElementById("messages").innerHTML = "";
	document.getElementById("passErr").innerHTML = "";
	document.getElementById("confErr").innerHTML="";
	document.getElementById("oldpwErr").innerHTML = "";
}
function clearEmailErrs() {
	document.getElementById("messages").innerHTML = "";
	document.getElementById("passErr").innerHTML = "";
	document.getElementById("emailErr").innerHTML="";	
	document.getElementById("confEmailErr").innerHTML="";
}

 