/* 
 * Input field validation class library
 *
 * $Id: validate.js 37149 2009-07-08 17:53:41Z release $>
 */

var message;

var formFields = new Array;

/*
 * Add a field to the control array
 */
function addValidator(field, desc, vtype, required)
{
	formFields[field] = new Validator(desc, vtype, required);
}

/*
 * Validator object
 *
 * desc			description of field contents
 * vtype		validation type
 * required		whether field is required
 */
function Validator(desc, vtype, required) 
{
	this.desc = desc;
	this.type = vtype;
	this.required = required;

	// set the validation function
	switch ( vtype ) 
	{
		case "name":		this.fn = validateName;
							break;
		case "int":			this.fn = validateInt;
							break;
		case "phone":		this.fn = validatePhone;
							break;
		case "email":		this.fn = validateEmail;
							break;
		case "url":			this.fn = validateURL;
							break;
		case "hour":		this.fn = validateHour;
							break;
		case "min":			this.fn = validateMin;
							break;
		case "mon":			this.fn = validateMon;
							break;
		case "mday":		this.fn = validateMday;
							break;
		case "year":		this.fn = validateYear;
							break;
		case "text2":		this.fn = validateText2;
							break;
		case "checkbox":	this.fn = validateText;
							break;
		default		:		this.fn = validateText;
							break;
	}
	
	return this;
}

function errorMsg(desc, s) 
{
	message += desc + ": " + s + "\n";
}

function isEmpty(text) 
{
	if ( text == null || text =="" ) 
	{
		return true;
	}

	var ok = /[^\s]+/.test(text);
	
	if ( ok == false )
		return true;

	return false;
}

function validateText(text, desc) 
{
	// note: here a match is bad
    var ok = /[\\\^`]+/.test(text);
	
	if ( ok == true )
	{
		errorMsg(desc, "contains unacceptable character(s).");
		return false;
	}

	return true;
}

function checkSize(e) 
{
	alert("size = " + e.value.length + "\nmax is 65535");
}

function confirmDelete() 
{
	return confirm("Really delete? This is not reversable.");
}

function validateText2(text, desc) 
{
	// note: here a match is bad
    var ok = /[\\\^`]+/.test(text);
	
	if ( ok == true )
	{
		errorMsg(desc, "contains unacceptable character(s).");
		return false;
	}
	
	if ( text.length > 65535 )
	{
		errorMsg(desc, "length is " + text.length + ", max is 65535.");
		return false;
	}

	return true;
}

function validateInt(n, desc) 
{
	if ( 1 * n != n )
	{
		errorMsg(desc, "not an integer.");
		return false;
	}

	return true;
}

function validateName(name, desc) 
{
	// note: here a match is bad
    var ok = /[^\s\w&,._-]+/.test(name);

	if ( ok == true ) 
	{
		errorMsg(desc, 'contains unacceptable character(s).');
		return false;
	}
	
	return true;

}

function validatePhone(phone, desc) 
{
	// valid forms:
	// ddd.ddd.dddd
	// ddd-ddd-dddd
	// ddd ddd dddd
	
	var ok = /^\d{3,3}[-.\s]+\d{3,3}[-.\s]+\d{4,4}$/.test(phone);

	if ( ok == false ) 
	{
		var s = "required format: 'nnn nnn nnnn'";
		s += " or 'nnn-nnn-nnnn'";
		s += " or 'nnn.nnn.nnnn'.";
        errorMsg(desc, s);
		return false;
	}
	
	return true;

}

function validateEmail(email, desc) 
{
	var ok = /^[\w.-]+\@[a-zA-Z0-9.-]+\.[a-zA-Z][a-zA-Z0-9]+$/.test(email);

	if ( ok == false ) 
	{
		errorMsg(desc, "invalid format.");
		return false;
	}
	
	return true;
}

function validateURL(url, desc) 
{
	var ok = /^http:\/\/[a-zA-Z0-9.-]+[\/a-zA-Z0-9.-]+$/.test(url);

	if ( ok == false ) 
	{
		errorMsg(desc, "invalid format.");
		return false;
	}
	
	return true;
}

function validateHour(hour, desc) 
{
	var ok = /^[\d.-]{2,2}$/.test(hour);

	if ( ok == false ) 
	{
		errorMsg(desc, "invalid format.");
		return false;
	}
	
	if ( hour < 0 || hour > 23 )
	{	
		errorMsg(desc, "range is 0..23.");
		return false;
	
	}
	
	return true;
}

function validateMin(min, desc) 
{
	var ok = /^[\d.-]{2,2}$/.test(min);

	if ( ok == false ) 
	{
		errorMsg(desc, "invalid format.");
		return false;
	}
	
	if ( min < 0 || min > 59 )
	{	
		errorMsg(desc, "range is 0..59.");
		return false;
	
	}
	
	return true;
}

function validateMon(mon, desc) 
{
	var ok = /^[\d.-]{1,2}$/.test(mon);

	if ( ok == false ) 
	{
		errorMsg(desc, "invalid format [" + mon + "].");
		return false;
	}
	
	if ( mon < 1 || mon > 12 )
	{	
		errorMsg(desc, "range is 1..12.");
		return false;
	
	}
	
	return true;
}

function validateMday(mday, desc) 
{
	var ok = /^[\d.-]{1,2}$/.test(mday);

	if ( ok == false ) 
	{
		errorMsg(desc, "invalid format.");
		return false;
	}
	
	if ( mday < 1 || mday > 31 )
	{	
		errorMsg(desc, "range is 1..31.");
		return false;
	
	}
	
	return true;
}

function validateYear(year, desc) 
{
	var ok = /^[\d.-]{4,4}$/.test(year);

	if ( ok == false ) 
	{
		errorMsg(desc, "invalid format.");
		return false;
	}
	
	if ( year < 2004 || year > 2005 )
	{	
		errorMsg(desc, "range is 2004..2005.");
		return false;
	
	}
	
	return true;
}

function validateForm(form) 
{
	message = "We have detected the following errors:\n\n";
	
	var okay = true;
	
	for ( var name in formFields )
	{
		if(typeof form.elements[name] == 'undefined')
			continue;
			
		if ( isEmpty(form.elements[name].value) )
		{
			if ( formFields[name].required ) 	
			{
				if ( formFields[name].type == 'checkbox' )
				{
					errorMsg(formFields[name].desc, "box must be checked.");
					okay = false;
				}
				else
				{
					errorMsg(formFields[name].desc, "field is required.");
					okay = false;
				}
			}
		}
		else if ( formFields[name].fn(
					form.elements[name].value, 
					formFields[name].desc) == false )
		{
			okay = false;
		}
	}
	
	if ( okay == false )
	{
		message += "\n\nPlease correct these errors and submit the form again";
		alert(message);
		return false;
	}
	
	return true;
}
