// JavaScript Document

//Check entire form
function checkRegisterForm() {
    var why = "";
    why += isEmpty('firstName');
	why += isEmpty('lastName');
	why += checkUsername('desiredUsername');
	why += checkEmail('emailAddress');
    why += checkDropDown('remcName');
    why += checkPassword('password1');
	why += checkPassword('password2');
	why += matchPasswords('password1', 'password2');
    why += isEmpty('invitationCode');
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkLoginForm() {
    var why = "";
    why += isEmpty('userName');
	why += isEmpty('password');
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}

function checkResetForm() {
    var why = "";
    why += isEmpty('userName');
    why += checkPassword('password1');
    why += checkPassword('password2');
    why += matchPasswords('password1', 'password2');
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}

//check drop-down selection
function checkDropDown(eid) {
var error='';
var x = document.getElementById(eid);
var xs = x.selectedIndex;

    if (x.options[xs].value=='') {
        error = 'Please select a Cooperative.\n';
    }
return error;
}

// email
function checkEmail (eid) {
var error="";
var x = document.getElementById(eid);
var emailFilter=/^.+@.+\..{2,4}$/;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;

	if (x.value.length == 0) {
		error = "Please enter an email address.\n";
		x.className="invalid";
	} else if (!(emailFilter.test(x.value))) {
		error = "Please enter a valid email address.\n";
		x.className="invalid";
	} else if (x.value.match(illegalChars)) {
		error = "The email address contains illegal characters.\n";
		x.className="invalid";
    } else {
		x.className="valid";
	}
	
return error;    
}

// Zip
function checkZip (eid) {
var error="";
var x = document.getElementById(eid);
var zipFilter=/^\d{5}$/;

	if (x.value.length == 0) {
		  error = "Please enter a zip code.\n";
		  x.className="invalid";
	  } else {
		x.className="valid";
	}

	if (!(zipFilter.test(x.value))) {
		error = "Please enter a valid zip code.\n";
		x.className="invalid";
	  } else {
		x.className="valid";
	}
return error;    
}

// password - 
function checkPassword (eid) {
var error = "";
var x = document.getElementById(eid);
//var illegalChars = /[\W_]/;

	if (x.value.length == 0) {
		error = "Please enter a password.\n";
		x.className="invalid";
	} else if ((x.value.length < 5) || (x.value.length > 15)) {
       error = "Passwords should have 5 to 15 characters.\n";
		x.className="invalid";
    //} else if (illegalChars.test(x.value)) {
    //  error = "The password contains illegal characters.\n";
	//	x.className="invalid";
    //} 
    } else if (!((x.value.search(/(a-z)+/)) && (x.value.search(/(A-Z)+/)) && (x.value.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
		x.className="invalid";
    }  else {
		x.className="valid";
	}
return error;    
}

// phone number - strip out delimiters and check for 10 digits

function checkPhone (eid) {
var error = "";
var x = document.getElementById(eid);
if (x.value == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = x.value.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  	   x.className="invalid";
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
	x.className="invalid";
    } 
return error;
}

function matchPasswords (eid, eid2) {
var error = "";
var pw1 = document.getElementById(eid);
var pw2 = document.getElementById(eid2);
	if (pw1.value != pw2.value) {
		error="Passwords do not match.\n";
		pw1.className="invalid"
		pw2.className="invalid"		
	} else {
		pw1.className="valid"
		pw2.className="valid"
	}		
return error;
}

// username - 4-10 chars, uc, lc, and underscore only.
function checkUsername (eid) {
var error = "";
var un = document.getElementById(eid);
if (un.value == "") {
	error = "Please enter a username.\n";
	return error;
}
    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((un.value.length < 4) || (un.value.length > 25)) {error = "Usernames need to be 4 to 25 characters.\n";}
    else if (illegalChars.test(un.value)) {error = "The username contains illegal characters.\n";} 
return error;
}       

// non-empty textbox
function isEmpty (eid) {
var error = "";
var x = document.getElementById(eid);
var xn = x.name;
  if (x.value.length == 0) {
	  error = "The "+xn+" field is required.\n";
	  x.className="invalid";
  } else {x.className="valid";}
return error;	  
}

// was textbox altered
function isDifferent (strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {error = "You altered the inviolate text area.\n";}
return error;
}

//required checkbox
function isChx (eid) {
var error = "";
var x = document.getElementById(eid);
var xn = x.name;
	if (!x.checked) {error = "The "+xn+" box must be checked to continue.\n";}
return error;	
}
