/*
 * Copyright (c) 2007, White Dactyl Labs
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of White Dactyl Labs nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WHITE DACTYL LABS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL WHITE DACTYL LABS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

function isblank(s)
{
	for(var i=0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if( (c!=' ') && (c!='\n') && (c!='\t'))
			return false;
	}
	
	return true;
}

function verify(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	var count_empty = 0;
	
	for(var i=0; i<f.length; i++)
	{
		var empty = 0;
		
		var e = f.elements[i];
		if(e.type == "text" || e.type == "textarea")
		{
			if(e.value == null || e.value == "" || isblank(e.value))
			{
				empty = 1;
				if(!e.optional)
				{
					var name = e.name;
					if(e.longname != null)
						name = e.longname;
					empty_fields += "\n          " + name;
					count_empty += 1;
				}
			}
		}

		if(!empty && (e.numeric || e.min != null || e.max != null))
		{
			var v = parseFloat(e.value);
			if(	isNaN(v)			||
				(e.min != null && v < e.min)	||
				(e.max != null && v > e.max))
			{
				errors += "- The field " + e.name + " must be a number"
				if(e.min != null)
					errors += " greater than " + e.min;
				if(e.max != null)
					errors += " and less than " + e.max;
				errors += ".\n";
			}
		}

	}
	
	if(!empty_fields && !errors)
		return true;
	
	
	msg = "Errors prevent form submission.  Please correct and then re-submit.\n\n";
	if(empty_fields) {
		if(count_empty == 1)
		{
			msg += "- The following field is empty:\n";
		}
		else
		{
			msg += "- The following fields are empty:\n";
		}
		msg += empty_fields + "\n";
	}
	if(errors)
		msg += errors;
	alert(msg);
	return false;
}
