function validateForm2(thisForm) {
var reason = "";
var un = thisForm.username;
var em = thisForm.email;
var pw = thisForm.password;
var cf = thisForm.confirm;

	reason += validateUsername(un);
	reason += validateEmail(em);
	reason += validatePassword(pw);
	reason += validateConfirm(cf);

	if (reason != "") {
		return false;
	}
	//hashPassword(theForm.pwd);
	return true;
}

function validateEmpty(fld) {
	var error="";
	var tfld = trim(fld.value); // value of field with whitespace trimmed off
	var alertDiv = document.getElementById(fld.name + "alert");
	var errcolor = '#E60D0D';

	function alertInput(error,color) {
		alertDiv.style.color = color;
	}

	if (tfld == "") {
		error = "Please enter a message to send in your email.\n\n"; //'
		alertInput(error,errcolor);
	} else {
		alertInput("Accepted",'#00B200');
	}
	return error;
}

function checkUserName(un,type) {

	xmlHttp = ajaxObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	if (xmlHttp) {
		var url = "checksn.php";
		url = url +"?"+type+"="+un.value;
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		return xmlHttp.responseText;
	} else {
		return false;
	}
}

// username - 8-20 chars, uc, lc, and underscore only.
function validateUsername(fld) {
	var error = "";
	var illegalChars = /\W/; // allow letters, numbers, and underscores
	var alertDiv = document.getElementById(fld.name + "alert");
	var errcolor = '#E60D0D';

	function alertInput(error,color) {
		//fld.style.borderColor = color;
		alertDiv.innerHTML = error;
		alertDiv.style.color = color;
	}

	if (fld.value == "") {
		error = "Please enter a username.\n";
		alertInput(error,errcolor);
	} else if (fld.value.length < 6) {
		error = "The username is "+(6-fld.value.length)+" character(s) too short.\n";
		alertInput(error,errcolor);
	} else if (fld.value.length > 20) {
		error = "The username is "+(fld.value.length-20)+" character(s) too long.\n";
		alertInput(error,errcolor);
	} else if (illegalChars.test(fld.value)) {
		error = "The username contains illegal characters.\n";
		alertInput(error,errcolor);
	} else if (checkUserName(fld,'sn') == 0) {
		error = "The username "+ fld.value +" is not available";
		alertInput(error,errcolor);
	} else if (checkUserName(fld,'sn') == 1) {
		alertInput("The username "+ fld.value +" is available",'#00B200');
	}
	return error;
}

function validatePassword(fld) {
	var error = "";
	var illegalChars = /\W/; // allow letters, numbers, and underscores
	var alertDiv = document.getElementById(fld.name + "alert");
	var errcolor = '#E60D0D';

	function alertInput(error,color) {
		//fld.style.borderColor = color;
		alertDiv.innerHTML = error;
		alertDiv.style.color = color;
	}

	if (fld.value == "") {
		error = "Please enter a password.\n";
		alertInput(error,errcolor);
	} else if (fld.value.length < 8) {
		error = "The password is "+(password.length-fld.value.length)+" character(s) too short.\n";
		alertInput(error,errcolor);
	} else if (fld.value.length > 12) {
		error = "The password is "+(fld.value.length-12)+" character(s) too long.\n";
		alertInput(error,errcolor);
	} else if (illegalChars.test(fld.value)) {
		error = "The password contains illegal characters.\n";
		alertInput(error,errcolor);
	} else {
		alertInput('Accepted','#00B200');
	}
	return error;
}

function validateConfirm(fld) {
	var error = "";
	var password = document.getElementById('password').value;
	var alertDiv = document.getElementById(fld.name + "alert");
	var errcolor = '#E60D0D';

	function alertInput(error,color) {
		//fld.style.borderColor = color;
		alertDiv.innerHTML = error;
		alertDiv.style.color = color;
	}

	if (fld.value == "") {
		error = "Please re-enter your password.\n";
		alertInput(error,errcolor);
	} else if (fld.value.length < password.length) {
		error = "Just "+(8-fld.value.length)+" character(s) to go!\n";
		alertInput(error,errcolor);
	} else if (fld.value.length > password.length) {
		error = "The password you entered is "+(fld.value.length-password.length)+" character(s) longer than your original password!\n";
		alertInput(error,errcolor);
	} else if (fld.value != password) {
		error = "Passwords do not match!\n";
		alertInput(error,errcolor);
	} else {
		alertInput('Accepted','#00B200');
	}
	return error;
}

function trim(s) {
	return s.replace(/^\s+|\s+$/, '');
}


function validateEmail(fld) {
	var error="";
	var tfld = trim(fld.value); // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/ ; //"
	var alertDiv = document.getElementById(fld.name + "alert");
	var errcolor = '#E60D0D';

	function alertInput(error,color) {
		alertDiv.style.color = color;
	}

	if (fld.value == "") {
		error = "Please enter an email address.\n\n"; //'
		alertInput(error,errcolor);
	} else if (!emailFilter.test(tfld)) { //test email for illegal characters
		error = "Please check that your email address is correct.\n(Ex: username@host.com)\n\n";
		alertInput(error,errcolor);
	} else if (fld.value.match(illegalChars)) {
		error = "The email address you have entered contains illegal characters.\nIllegal characters are characters which email addresses should not contain.\n\nCommon allowed characters include letters, numbers, and periods.\nEx: John.Smith23@Yahoo.com\n\n";
		alertInput(error,errcolor);
	} else {
		alertInput("You entered the email address: "+ fld.value,'#00B200');
	}
	return error;
}

function validatePhone(fld) {
	var error = "";
	var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');
	var alertDiv = document.getElementById(fld.name + "alert");

	function alertInput(error,color) {
		//fld.style.borderColor = color;
		alertDiv.innerHTML = error;
		alertDiv.style.color = color;
	}

	if (fld.value == "") {
		error = "Please enter a phone number.\n";
		alertInput(error,'red');
	} else if (isNaN(parseInt(stripped))) {
		error = "The phone number contains illegal characters.\n";
		alertInput(error,'red');
	} else if (!(stripped.length == 10)) {
		error = "The phone number is the wrong length. Make sure you included an area code.\n";
		alertInput(error,'red');
	} else {
		alertInput('Accepted','');
	}
	return error;
}

function hashPassword(password) {
	// Hash password
	var securePassword = hex_sha1(password.value);

	// Save hashed password
	document.getElementById('securePassword').value = securePassword;

	// Remove plain password
	password.value = '';
}

