/***************************************************************/
/******** These are the basic validator "helper" methods *******/
/***************************************************************/


///////////////////////////////////////////
function isAllLetters(checkStr)
///////////////////////////////////////////
{
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ";
	return validateStr(checkStr, checkOK);
}

///////////////////////////////////////////
function isAllNumbers(checkStr)
///////////////////////////////////////////
{
	var checkOK = "1234567890";
	return validateStr(checkStr, checkOK);
}
///////////////////////////////////////////
function validateStr(checkStr, checkOK)
///////////////////////////////////////////
{
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
	  	ch = checkStr.charAt(i);
	  	for (j = 0;  j < checkOK.length;  j++)
	  	{
	    		if (ch == checkOK.charAt(j))
	      			break;
	  	}
	  	if (j == checkOK.length)
	  	{
	    		allValid = false;
	    		break;
	  	}
	}
	return allValid;
}
////////////////////////////////////////
function isFieldNull(theField)
////////////////////////////////////////
{
	if(theField == null) return true;
	return false;
}
////////////////////////////////////////
function isEmptyString(value)
////////////////////////////////////////
{
	if(value == "" || value.length < 1) return true;
	return false;
}
////////////////////////////////////////
function showErrorMsg(theField, theMsg)
////////////////////////////////////////
{
	alert(theMsg);
	theField.focus();
 	return false;
}
////////////////////////////////
function validateFieldInput(theField, fieldName, minLength, maxLength)
////////////////////////////////
{
	var theMsg = "Please fill in the in the \"" + fieldName + "\" field.";
	
	
	if(isFieldNull(theField))
	{
		alert(theMsg);
		return false;
	}
	else
	{
		if(isEmptyString(theField))
		{
			return showErrorMsg(theField, theMsg);
		}

		var fLength = theField.value.length;
				
		if(minLength == maxLength && fLength < maxLength)
		{
	 		return showErrorMsg(theField, "Please enter " + minLength + " character(s) in the \"" + fieldName + "\" field.");
		}
		if(minLength != 0 && fLength < minLength)
		{
	 		return showErrorMsg(theField, "Please enter at least " + minLength + " character(s) in the \"" + fieldName + "\" field.");
		}
		if(fLength > maxLength)
		{
	 		return showErrorMsg(theField, "Please enter no more than " + maxLength + " characters in the \"" + fieldName + "\" field.");
		}
  		return true;
	}
}

/*************************************************************/
/******** These are methods for common validator tasks *******/
/*************************************************************/

////////////////////////////////
function validateFirstName(theForm)
////////////////////////////////
{
	if(validateFieldInput(theForm.FirstName, "FirstName", 1, 15))
	{
		if(!isAllLetters(theForm.FirstName.value))
			return showErrorMsg(theForm.FirstName, "Please enter only letter characters in the \"FirstName\" field.");
		return true;
	}
	return false;
}
////////////////////////////////
function validateLastName(theForm)
////////////////////////////////
{

	if(validateFieldInput(theForm.LastName, "LastName", 1, 15))
	{
		if(!isAllLetters(theForm.LastName.value))
			return showErrorMsg(theForm.LastName, "Please enter only letter characters in the \"LastName\" field.");
		return true;
	}
	return false;
}
////////////////////////////////
function validatePhoneNumber(theForm)
////////////////////////////////
{

	if(validateFieldInput(theForm.Phone, "Phone Number", 10, 10))
	{
		if(!isAllNumbers(theForm.Phone.value))
			return showErrorMsg(theForm.LastName, "Please enter only numbers in the \"Phone Number\" field.");
		return true;
	}
	return false;
}
////////////////////////////////
function ValidateSectionNumber(theForm)
////////////////////////////////
{
	var courseLength = theForm.Courses.value.length; 
  	var courseValue  = theForm.Courses.value;
  	
  	if(validateFieldInput(theForm.Courses, "Courses", 0, 75))
  	{
  		if(courseValue.toUpperCase() != "NONE" && courseLength < 5) 
  			return showErrorMsg(theForm.Courses, "Please enter at least a 5 digit section number in the \"Courses\" field, or enter \"NONE\"");
		return true;
	}
	return false;
}

//////////////////////////////////////////////////
function isEmailValid(theForm){
//////////////////////////////////////////////////
	var email1   = theForm.Email.value;
	var email2   = theForm.Email2.value;
	var AtSym    = email1.indexOf('@');
	var Period   = email1.lastIndexOf('.');
	var Space    = email1.indexOf(' ');
	var Length   = email1.length - 1;

	if 	((AtSym < 1) || (AtSym == Length) || (Period <= AtSym+1) 
		|| (Period == Length ) || (Space  != -1)) 	{  
	   alert('Please enter a valid e-mail address!');
   		theForm.Email.focus();
	   return false;
	}
	else if(email1 != email2)	{
		alert('Please enter the same e-mail address in both boxes.');
		theForm.Email2.focus();
		return false;
	}
	return true;
}

//////////////////////////////////////////////////
function isWebCTIDValid(theForm){
//////////////////////////////////////////////////
	if(!validateFieldInput(theForm.WebCT_ID, "WebCT ID", 9, 9)) return false;

	if(!isAllLetters(theForm.WebCT_ID.value.substring(0,2)))
	{
		return showErrorMsg(theForm.WebCT_ID, "The first two characters of your WebCT ID must be letters.");
	}
	if(!isAllNumbers(theForm.WebCT_ID.value.substring(2,10)))
	{
		return showErrorMsg(theForm.WebCT_ID, "The last seven characters of your WebCT ID must be numbers.");
	}
	if(theForm.WebCT_ID.value != theForm.WebCT_ID2.value)
	{
		return showErrorMsg(theForm.WebCT_ID2, "Please enter the same WebCT ID in both boxes.");
	}
	return true;
}

//////////////////////////////////////////////////
function isStudentIDValid(theForm){
//////////////////////////////////////////////////
	if(!validateFieldInput(theForm.sid, "Student ID", 7, 7)) return false;
	if(!isAllNumbers(theForm.sid.value))
	{
		return showErrorMsg(theForm.WebCT_ID, "Your student ID is a 7 digit number (not your Social Security Number).");
	}
	if(theForm.sid.value != theForm.sid2.value)
	{
		return showErrorMsg(theForm.WebCT_ID2, "Please enter the same Student ID in both boxes.");
	}
	return true;
}

