function WebValidation()
{
	/* properties */

	/* methods */
	this.validateEmail 					= Web_validateEmail;
	this.validateTextField				= Web_validateTextField;
	this.validateSelectField			= Web_validateSelectField;
	this.validateFormTextField			= Web_validateFormTextField;
	this.validateDateFormat				= Web_validateDateFormat;
    this.validateCreditCard         	= Web_validateCreditCard;
    this.validateXOrSelect				= Web_validateXOrSelect;
	this.validateCheckbox				= Web_validateCheckbox;
	this.validateAgreement				= Web_validateAgreement;
	this.validateInteger				= Web_validateInteger;
    this.validatePhoneNum               = Web_validatePhoneNum;
}

function Web_validatePhoneNum(field, fieldname) {
    var str = field.value;
    var re1 = /[(][0-9]{3}[)][ ]*[0-9]{3}-[0-9]{4}/;
    var re2 = /[0-9]{3}[ -.][0-9]{3}[ -.][0-9]{4}/;
    var re3 = /[0-9]{10}/;
    if ( str.match(re1) || str.match(re2) || str.match(re3) ) {
        return true;
    }
    else {
        // alert the user of the error
        alert(fieldname + " has incorrect format.");

        // put focus on the form field
        field.focus();
        return false;
    }
}
function Web_validateFormTextField( field, min, max, fieldname )
{
	var str = field.value;
	// replace all spaces with nothing and store in a temp value
	str = str.replace( /(\s+)$/g, "" );

	// verify that the length of the field is not greater than the max or less than the min length	
	if ((field.value.length < min) || (field.value.length > max))
	{
		// alert the user of the error
		alert(fieldname + " must be between " + min + " and " + max + " characters long.");

		// put focus on the form field
		field.focus();
		return false;
		
	}
	// verify that the field does not just contain spaces
	else if ((field.value.length != 0) && ( str == ""))
	{
		// alert the user of the problem
		alert( fieldname + " contains only spaces. Please fill in correctly."  );

		// put focus on the form field
		field.focus();
		return false;
	}
	// no problems. return success
	else return true;
}

function Web_validateTextField(field, fieldname)
{
	var str = field.value;
	if ((str == null) || (str == ""))
	{
		// alert the user of the error
		alert("Please enter a(n) " + fieldname + ".");

		// put focus on the form field
		field.focus();
		return false;
		
	}
	// no problems. return success
	else return true;
}

function Web_validateSelectField(fieldOne, fieldNameOne)
{
	var strValueOne		= fieldOne.options[fieldOne.selectedIndex].value;
	
	if ((0 == strValueOne.length))
	{
		alert("Please select a " + fieldNameOne + ".");
		fieldOne.focus();
		return false;
	} else return true;
}

function Web_validateEmail(field)
{
  var fullEmail = field.value;
  if (fullEmail == "") {
    alert("\nPlease enter your Internet e-mail address.")
    field.focus();
    return false;
  }
  if (fullEmail.indexOf (" ",0) == -1) {}
  else {
    alert("\nYour e-mail address cannot contain spaces.\n\nPlease re-enter your e-mail address.")
    field.select();
    field.focus();
    return false;
  }
  if (fullEmail.indexOf ("@",0) == -1 || fullEmail.indexOf (".",0) == -1) {
    alert("\nYour e-mail address requires that a \"@\" and a \".\" be used.\n\nPlease re-enter your e-mail address.")
    field.select();
    field.focus();
    return false;
  }
  else
    if (fullEmail.length < 8 || fullEmail.substring((fullEmail.length - 1),(fullEmail.length)) == "." ||  fullEmail.substring(0,1) == "@")
    {
    alert("\nYour e-mail address is invalid.\n\nPlease re-enter your e-mail address.")
    field.select();
    field.focus();
    return false;
    }
  else
    {
    return true;
    }

}
function Web_validateDateFormat(field, fieldname)
{
	var str = field.value;
	var strDateNum = "1" + field.value + "";
	var strDateInt = parseInt(strDateNum,10) + "";
	
	str = str.replace( /\//g, "" );

	if(field.value.length != 10 || isNaN(strDateInt)) {
		// alert the user of the error
		alert("Please enter " + fieldname + " in MM/DD/YYYY format.");

		// put focus on the form field
		field.focus();
		return false;
	
	}

	// no problems. return success
	else return true;
}
function Web_validateCreditCard(field,fieldname)
{
	var str = field.value;
	if ((str == null) || (str == ""))
	{
		// alert the user of the error
		alert("Please enter a credit card number.");
		// put focus on the form field
		field.focus();
		return false;
	}
	else if(str.indexOf(" ",0) >= 0) {
    	alert("\nYour credit card cannot contain spaces.\n\nPlease re-enter your credit card number.")
    	field.select();
    	field.focus();
    	return false;	
	}	
	// If okay
	else return true;
}
function Web_validateXOrSelect(fieldOne, fieldTwo, fieldNameOne, fieldNameTwo)
{
	var strValueOne		= fieldOne.options[fieldOne.selectedIndex].value;
	var strValueTwo		= fieldTwo.options[fieldTwo.selectedIndex].value;
	
	if ((0 == strValueOne.length) && (0 == strValueTwo.length))
	{
		alert("You must select either " + fieldNameOne + " or " + fieldNameTwo + ".");
		fieldOne.focus();
		return false;
	} else if ((0 != strValueOne.length) && (0 != strValueTwo.length))
	{
		alert("You must select either " + fieldNameOne + " or " + fieldNameTwo + ", but not both.");
		fieldOne.focus();
		return false;
	} else return true;
}
function Web_validateCheckbox(fieldOne, fieldNameOne)
{
	var count = fieldOne.length;
	var checked = false;
	for(var i = 0; i < count; i++) {
		if(fieldOne[i].checked) {
			checked = true;
			break;
		}
	}
	if(!checked) {
		alert("Please select " + fieldNameOne + ".");
		//fieldOne.focus();
		return false;
	} else return true;
}
function Web_validateAgreement(fieldOne, message)
{
	if(!fieldOne.checked) {
		alert(message);
		//fieldOne.focus();
		return false;
	} else return true;
}
function Web_validateInteger(elem, message)
{
	var str = elem.value;
	var re = /^[-]?\d*\.?\d*$/;
	str = str.toString();
	if (!str.match(re)) {
		alert("You must enter a number for the  " + message);
		elem.focus();
		return false;
	}
	else {
		return true;
	}
}
