//Author:Jonathan Van Schoick
//Title:validate.js
//Purpose:JavaScript library of functions to validate against
//        a form to ensure user has properly filled in all fields
//Modified from: Practical JavaScript for the Usable Web
//		 available from Glashaus Publishers (2002)

function Validate() {}

Validate.prototype.containsValue = function(theElement, theDocument)
{
var isValid = true;
	if (theElement.value == 0)
		{
		isValid = false;
		this.showError(theElement.name, theDocument);
		theElement.focus();
		}
	else
		{
		isValid = true;
		this.hideError(theElement.name, theDocument);
		}
return isValid;
}

Validate.prototype.radioTrue = function(theElement, theDocument)
{
var isValid = false;
var radioCounter;

for (radioCounter = theElement.length - 1; radioCounter >= 0; radioCounter--)
	{
	isValid = theElement[radioCounter].checked;
	if (isValid)
		{
		 this.hideError(theElement[0].name, theDocument);
		 break;
		 }
	else
		{
		this.showError(theElement[0].name, theDocument);
		}
	}
return isValid;
}

Validate.prototype.isNumeric = function(theElement, theDocument)
{

var myRegExExpresion = /^\d+$/g;
var isValid;
if (myRegExExpresion.test(theElement.value))
	{
	isValid = true;
	this.hideError(theElement.name, theDocument);
	}
else
	{
	isValid = false;
	this.showError(theElement.name, theDocument);
	theElement.focus();
	}
return isValid;
}

Validate.prototype.isEmailAddress = function(string)
{
var validEMailRegExp = /^\w(\.?\w)*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;

	if (validEMailRegExp.test(string))
		{
		return true;
		}
	else
		{
		return false;
		}
}


Validate.prototype.showError = function(theElement, theDocument)
{
var theElementError = theElement;
theElementError = theElementError + "Error";

if (document.layers)
	{
	theDocument[theElementError].visibility = "visible";
	}
else if (document.all)
	{
	theDocument.all[theElementError].style.visibility = "visible";
	}
else
	{
	theDocument.getElementById(theElementError).style.visibility = "visible";
	}

}

Validate.prototype.hideError = function(theElement, theDocument)
{
var theElementError = theElement;
theElementError = theElementError + "Error";

if (document.layers)
	{
	theDocument[theElementError].visibility = "hidden";
	}
else if (document.all)
	{
	theDocument.all[theElementError].style.visibility = "hidden";	
	}
else
	{
	theDocument.getElementById(theElementError).style.visibility = "hidden";
	}

}

Validate.prototype.hideErrorHeader = function (theDocument)
{
if (document.layers)
	{
	theDocument.errorHeader.visibility = "hidden";
	}
else if (document.all)
	{
	theDocument.all.errorHeader.style.visibility = "hidden";	
	}
else
	{
	theDocument.getElementById(errorHeader).style.visibility = "hidden";
	}
}

Validate.prototype.hideAll = function (theArray, theDocument)
{
var elementName = "hello";

for (var i = 0;  i < theArray.length ; i++)
	{
	elementName = theArray[i];
	this.hideError(elementName, theDocument);
	}	
}


function validateZIP(s) {

     // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
 
     if (!reZip.test(s)) {
          alert("Please enter a 5 digit zip code");
          return false;
     }
 

}
