/*
validate.js

This script provides client-side validation
for the site's contact form, as well as 
allowing fieldsets to "pop up" and fields to 
enable/disable at appropriate times.

*/


function anythingThere(field, errspanname)
{
	theerrorspan = document.getElementById(errspanname);
	if (trim(field.value) == "" || trim(field.value) == null)
	{
		theerrorspan.innerHTML = "Required!";
		field.style.borderColor = "#FF0000";
		field.style.borderWidth = "2px";
		return false;
	}
	else
	{
		theerrorspan.innerHTML = "";
		field.style.borderColor = "";
		field.style.borderWidth = "";
		return true;
	}
}


// Strip whitespace from the beginning and end of a string

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

function validateEmail(field, errspanname) 
{
	theerrorspan = document.getElementById(errspanname);
	notEmpty = anythingThere(field, errspanname);
	if (notEmpty == false)
	{
		return false;
	}
	else
	{
		// check email syntax
		isValid = isEmail(field.value);
		// alert(isValid);
		if(!isValid)
		{
			theerrorspan.innerHTML = "Invalid address";
			field.style.borderColor = "#FF0000";
			field.style.borderWidth = "2px";
			return false;
		}
		else
		{
			theerrorspan.innerHTML = "";
			field.style.borderColor = "";
			field.style.borderWidth = "";
			return true;
		}
	}
}

// Check if a string is in valid email format. Returns true if valid, false otherwise. 


function validateForm()
{
	unfield = document.getElementById("Username");
	pwfield = document.getElementById("Password");
	unver = anythingThere(unfield, "unerror");
	pwver = anythingThere(pwfield, "pwerror");
	if(unver == false || pwver == false)
	{
		window.location = "#form";
		if(unver == false)
		{
			unfield.focus();
			unfield.select();
		}
		else if(pwver == false)
		{
			pwfield.focus();
			pwfield.select();
		}
		return false;
	}
	else
	{
		return true;
	}
}
