var validate_msg;

function validate_form(theForm)
{
		for (var i = 0; i < theForm.length; i++)
		{
				var oElem = theForm.elements[i];
				if (v_getType(oElem) == 'HIDDEN')
				{
						validate_msg = "";
						var cname = oElem.name;
						if (cname.length > 1)
						{
								if (cname.charAt(0) == '@')
								{
										var params = cname.split(';');
										var con = null;
										for (var j = 0; j < params.length; j++)
										{
												if (v_trim(params[j].split(':')[0].toUpperCase()) == 'MSG')
												{
														var details = params[j].split(':');
														if (details[1]) validate_msg = v_trim(details[1]);
												}
										}
										for (var j = 0; j < params.length; j++)
										{
												var details = params[j].split(':');
												if (details[0])
												{
														if (v_trim(details[0].toUpperCase()) == 'CONTROL')
														{
																if (details[1]) con = v_getCon(theForm, v_trim(details[1]));
														}
														if (con)
														{
																v_clearError(con);
																if (v_trim(details[0].toUpperCase()) == 'REQUIRED')
																{
																		if (!validate_required(con)) return false;
																		if (details[1])
																		{
																				if (!validate_length(con, v_trim(details[1]))) return false;
																		}
																}
																if (v_trim(details[0].toUpperCase()) == 'MAX')
																{
																		if (details[1])
																		{
																				if (!validate_max(con, v_trim(details[1]))) return false;
																		}
																}
																else if (v_trim(details[0].toUpperCase()) == 'TYPE')
																{
																		if (details[1])
																		{
																				if (v_trim(details[1].toUpperCase()) == 'DECIMAL')
																				{
																						if (!validate_decimal(con)) return false;
																				}
																				else if (v_trim(details[1].toUpperCase()) == 'INTEGER')
																				{
																						if (!validate_int(con)) return false;
																				}
																				else if (v_trim(details[1].toUpperCase()) == 'EMAIL')
																				{
																						if (!validate_email(con)) return false;
																				}
																				else if (v_trim(details[1].toUpperCase()) == 'DATE')
																				{
																						if (!validate_date(con)) return false;
																				}
																				else if (v_trim(details[1].toUpperCase()) == 'ALPHA')
																				{
																						if (!validate_alpha(con)) return false;
																				}
																				else if (v_trim(details[1].toUpperCase()) == 'PHONE')
																				{
																						if (!validate_phone(con)) return false;
																				}
																		}
																}
																else if (v_trim(details[0].toUpperCase()) == 'CONTENTS')
																{
																		if (details[1])
																		{
																				if (!validate_contents(con, v_trim(details[1]))) return false;
																		}
																}
																else if (v_trim(details[0].toUpperCase()) == 'NOINDEX')
																{
																		if (details[1])
																		{
																				if (!validate_index(con, v_trim(details[1]))) return false;
																		}
																}
																else if (v_trim(details[0].toUpperCase()) == 'CHECKED')
																{
																		if (!validate_checked(con)) return false;
																}
																else if (v_trim(details[0].toUpperCase()) == 'CHOSEN')
																{
																		if (!validate_chosen(con)) return false;
																}
																else if (v_trim(details[0].toUpperCase()) == 'EXTENSIONS')
																{
																		if (details[1])
																		{
																				if (!validate_extensions(con, v_trim(details[1]))) return false;
																		}
																}
														}
												}												
										}
								}
						}
				}
		}
		return true;
}

function v_setError(oCon)
{
		oCon.style.backgroundColor = '#FEE2E2';
		oCon.style.border = '1px solid #880000';
		oCon.focus();
}

function v_clearError(oCon)
{
		oCon.style.backgroundColor = '#E1F1CB';
		oCon.style.border = '1px solid #008800';
}

function v_getCon(theForm, cName)
{
		for (var i = 0; i < theForm.length; i++)
		{
			if (theForm.elements[i].name)
			{
				if (theForm.elements[i].name.toUpperCase() == cName.toUpperCase()) return theForm.elements[i];
			}
		}
		return null;
}

function v_getType(oCon)
{
		var ret = "";
		if (oCon.tagName.toUpperCase() == 'INPUT') ret = oCon.type.toUpperCase();
		else ret = oCon.tagName.toUpperCase();
		return ret;
}

function validate_required(oCon)
{
		if (oCon.value == "")
		{
				v_alert("Please enter a value for the \"" + v_getName(oCon) + "\" field.");
				v_setError(oCon);
				return false;
		}
		return true;
}

function validate_length(oCon, iLen)
{
		if (oCon.value.length < iLen)
		{
				v_alert("Please enter at least " + iLen + " characters in the \"" + v_getName(oCon) + "\" field.");
				v_setError(oCon);
				return false;
		}
		return true;
}

function validate_max(oCon, iLen)
{
		if (oCon.value.length > iLen)
		{
				v_alert("Please enter at most " + iLen + " characters in the \"" + v_getName(oCon) + "\" field.");
				v_setError(oCon);
				return false;
		}
		return true;
}

function validate_contents(oCon, sOk)
{
		for (i = 0; i < oCon.value.length; i++)
		{
				ch = oCon.value.charAt(i);
				if (sOk.indexOf(ch) < 0)
				{
						v_alert("Invalid character \"" + ch + "\" in the \"" + v_getName(oCon) + "\" field.");
						v_setError(oCon);
						return false;
				}
		}
		return true;
}

function validate_count(oCon, sChar, iCount)
{
		var j = 0;
		for (var i = 0; i < oCon.value.length; i++)
		{
				ch = oCon.value.charAt(i);
				if (ch == sChar) j++;
				if (j > iCount)
				{
						v_alert("There may only be " + iCount + " occurances of character \"" + ch + "\" in the \"" + v_getName(oCon) + "\" field.");
						v_setError(oCon);
						return false;
				}
		}
		return true;
}

function validate_email(oCon)
{
	if (/^[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,4}$/.test(oCon.value))
    {
        return true;
    }
	v_alert("Please enter a valid email address in the \"" + v_getName(oCon) + "\" field.");
	v_setError(oCon);
	return false;
}

function validate_date(oCon)
{
	if (/^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/.test(oCon.value) || oCon.value == '')
	{
		return true;
	}
	v_alert("Please enter a valid date in the \"" + v_getName(oCon) + "\" field.");
	v_setError(oCon);
	return false;
}

function validate_decimal(oCon)
{
		var checkOK = "0123456789.";
		if (!validate_contents(oCon, checkOK)) return false;
		if (!validate_count(oCon, '.', 1)) return false;
		return true;
}

function validate_alpha(oCon)
{
		var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
		if (!validate_contents(oCon, checkOK)) return false;
		return true;
}

function validate_phone(oCon)
{
		var checkOK = "0123456789-";
		if (!validate_contents(oCon, checkOK)) return false;
		return true;
}

function validate_int(oCon)
{
		var checkOK = "0123456789";
		if (!validate_contents(oCon, checkOK)) return false;
		return true;
}

function validate_index(oCon, iSel)
{
		if (oCon.selectedIndex == iSel)
		{
				v_alert("Invalid selection in the \"" + v_getName(oCon) + "\" field.");
				v_setError(oCon);
				return false;
		}
		return true;
}

function validate_checked(oCon)
{
		if (oCon.checked == false)
		{
				v_alert("Field \"" + v_getName(oCon) + "\" must be checked to continue.");
				v_setError(oCon);
				return false;
		}
		return true;
}

function validate_chosen(oCon)
{
	var radioSelected = false;
	var arr = document.getElementsByName(oCon.name);	
	
	for (var i = 0; i < arr.length; i++)
	{
		if (arr[i].checked) radioSelected = true;
	}
	if (!radioSelected)
	{
		v_alert("Please select one of the \"" + v_getName(oCon) + "\" options.");
		v_setError(oCon);
		return false;
	}
	
	return true;
}

function validate_extensions(oCon, sExtensions)
{	
		var extensions = "(" + sExtensions.replace(/ /g, "").replace(/,/g, ")|(") + ")";
		var rx = new RegExp("^.+\.(" + extensions + ")$", "i");
		if (!rx.test(oCon.value) && oCon.value != "")
		{
				v_alert("You may only upload files with the extension (" + sExtensions.replace(/,/g, ", ") + ") in the \"" + v_getName(oCon) + "\" field.");
				v_setError(oCon);
				return false;
		}
		return true;
}

function v_trim(sIn)
{
		return sIn.replace(/^\s+|\s+$/g,"");
}

function v_getName(oCon)
{
		var ret = oCon.title;
		if (ret == "" || ret == "undefined") ret = oCon.name;
		return ret;
}

function v_alert(str)
{
		if (validate_msg != "")
		{
				alert(validate_msg);
		}
		else
		{
				alert(str);
		}
}