var aRequiredFields = new Array();

// Pass a string value to test it for emptyness or all whitespace
function isEmpty(value)
{
	if (value == null || value == "")
		return true;
	else
	{
		// If any non-whitespace characters are found value is not empty
		var sValue = String(value);
		if (sValue.search(/\S/) == -1)
			return true;
		else
			return false;
	}
}

// field is the browser object to be validated
// sFieldName is the user friendly field name of the field object
function RequiredField(field, sFieldName)
{
	this.field = field;
	this.sFieldName = sFieldName;
	
	aRequiredFields[aRequiredFields.length] = this;
}

// aRequiredFields must be an array of type RequiredField
function validateRequiredFields()
{
	var bError = false;
	var sErrorMessage = "The following fields are required but were not completed:\n";
	// Loop through aRequiredFields checking whether each field is empty or not
	for (var i = 0; i < aRequiredFields.length; i++)
	{
		var field = aRequiredFields[i].field;
		if (field.type == "text" || field.type == "password" || field.type == "hidden" || field.type == "textarea")
		{
			if (isEmpty(field.value))
			{
				bError = true;
				sErrorMessage += "\t\t" + aRequiredFields[i].sFieldName + "\n";
			}
		}
		else if (field.type == "select-one" || field.type == "select-multiple")
		{
			if (field.selectedIndex == -1)
			{
				bError = true;
				sErrorMessage += "\t\t" + aRequiredFields[i].sFieldName + "\n";
			}
		}
		else if (typeof(field) == "object" && typeof(field.length) == "number")
		{
			// Radio buttons and checkboxes
			var bFound = false;
			for (var j = 0; j < field.length; j++)
			{
				if (field[j].checked == true)
					bFound = true;
			}
			if (bFound == false)
			{
				bError = true;
				sErrorMessage += "\t\t" + aRequiredFields[i].sFieldName + "\n";
			}
		}
		else
		{
			bError = true;
			sErrorMessage = "A " + field.type + " field cannot be made required";
		}
	}
	if (bError == true)
		return sErrorMessage;
	else
		return "";
}

//***********************************************************
//A function to check if checkString is a valid Email address
//***********************************************************

function checkEmail(checkString)
{
    var newstr = "";
    var at = false;
    var dot = false;

    // DO SOME PRELIMINARY CHECKS ON THE DATA

    // IF EMAIL ADDRESS HAS A '@' CHARACTER
    if (checkString.indexOf("@") != -1) {
      at = true;

    // IF EMAIL ADDRESS HAS A '.' CHARACTER
    } else if (checkString.indexOf(".") != -1) {
      dot = true;
    }
    // PARSE REMAINDER OF STRING
    for (var i = 0; i < checkString.length; i++) {
        ch = checkString.substring(i, i + 1)
        if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
                || (ch == "@") || (ch == ".") || (ch == "_")
                || (ch == "-") || (ch >= "0" && ch <= "9")) {
                newstr += ch;
                if (ch == "@") {
                    at=true;
                }
                if (ch == ".") {
                    dot=true;
                }
        }
		else if (s.charCodeAt(i) >= 128 && s.charCodeAt(i) <= 165)
			newstr += ch;
    }
    if ((at == true) && (dot == true)) {
 //       return newstr;
 		return true;	
    }
    else {
//      // DISPLAY ERROR MESSAGE
//      alert ("Sorry, the email address you\nentered is not in the correct\nformat.");
//      return checkString;
		return false;
    }
}
