// Author:  Javier Reartes - Santex Group - javier.reartes@santexgroup.com
// Created: Aug 2005
//Last Updated:  Feb 2, 2008

valid = new Array();
//array definitions
valid['VARCHAR'] = Array();
valid['INT'] = Array();
valid['FLOAT'] = Array();
valid['EMAIL'] = Array();
valid['URL'] = Array();
valid['PHONE'] = Array();
valid['DATE'] = Array();
valid['USZIP'] = Array();

//Add here the error messages to display, add only the part of the message that goes after the field name.
valid['VARCHAR']['MSG']	= "must be a valid character";
valid['INT']['MSG'] 	= "only accepts numbers e.g: 54679";
valid['FLOAT']['MSG'] 	= "must be numeric e.g: 54.679";
valid['EMAIL']['MSG'] 	= "must be a valid e-mail.";
valid['URL']['MSG'] 	= "must be a valid url.";
valid['PHONE']['MSG']	= "must be a correct phone number.";
valid['DATE']['MSG']	= "must be a valid date (mm/dd/yyyy).";
valid['USZIP']['MSG'] = "must be a valid format for US ZIP codes"
//regex
valid['VARCHAR']['REGEX'] 	= /[A-Za-z]/;
valid['INT']['REGEX'] 		= /(^-?\d\d*$)/;
valid['FLOAT']['REGEX'] 	= /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
valid['EMAIL']['REGEX'] 	= /^[_a-zA-Z0-9-](\.{0,1}[_a-zA-Z0-9-])*@([a-zA-Z0-9-]{2,}\.){0,}[a-zA-Z0-9-]{3,}(\.[a-zA-Z]{2,4}){1,2}$/;
valid['URL']['REGEX'] 		= /(((f|ht)tps?:\/\/)|(www\.))([a-z0-9:%_\.~\?&\-]+)((.)+)$/;
valid['PHONE']['REGEX']		= /[0-9\(\)\-]/;
valid['DATE']['REGEX'] 		= /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
valid['USZIP']['REGEX']   = /^\d{5}([\-]\d{4})?$/;

// globals var and messages, change them as you need or like
extArray = new Array(".pdf", ".doc", ".xls", ".jpg");

// funcion used to validate file types
function LimitAttach(file) {
	var a = file;
	allowSubmit = false;
	if (!a)
	{
		return;
	}
	while (a.indexOf("\\") != -1)
	{
		a = a.slice(a.indexOf("\\") + 1);
	}
	ext = a.slice(a.indexOf(".")).toLowerCase();
	for (var i = 0; i < extArray.length; i++)
	{
		if (extArray[i] == ext)
		{
			allowSubmit = true; break;
		}
	}
	if (allowSubmit)
	{
		return null;
	}
	else
	{
	return("\r\n-- Please choose file of the following types:  "
	+ (extArray.join("  ")) + "\nchoose "
	+ "another file.");
	}
}
//function used for matching regex
function match(regex, text){
	var result = text.match(regex);
	if (result == null && text != "")
	{
		return false;
	}
	else{
		return true;
	}
}
//main function called from the form
function verify (f)
{
	var msg;
	var empty = "";
    var errors = "";
	
	for(var i = 0; i < f.length; i++)
	{
		var e = f.elements[i];

		if (((e.type == "text") || (e.type == "textarea") || (e.type == "password") || (e.type == "hidden") || (e.type == "file")|| (e.type == "select-one"))  && (e.required || e.allow || e.compare))
		{
			if(e.type=="select-one" && e.value == "0"){
				errors += "-- Please select " + e.title ;
				errors += "\n";
			}
			//if required and empty, return an error
			if ((e.value == null) || (e.value == "") && (e.required || e.compare))
			{
				empty += "-- Must complete " + e.title + "\n";
            	continue;
			}
			// validate
			var check = false;
			if(e.required && e.required!='true')
			{
				check = e.required;
			}
			if(e.allow && e.allow!='true')
			{
				check = e.allow;
			}

			if(check && check != true)
			{
				if (!match(valid[check]['REGEX'], e.value))
				{
					fieldName = e.title?e.title:e.name;
					errors += "-- The field " + fieldName + " " + valid[check]['MSG'];
					errors += "\n";
				}
			}
			
			if(e.compare)
			{
				if(e.value != e.compare)
				{
					errors += "-- Please verify your password";
					errors += "\n";
				}
			}
			if(e.allow == 'FILE')
			{
				if (LimitAttach(e.value) != null)
				{
					errors += LimitAttach(e.value);
					errors += "\n";
				}
			}
		}
	}
	if (errors || empty)
	{
		msg = empty + "\r\n" + errors;
		alert(msg);
		return false;
	}
	else
	{
		return true;
	}

}