// 2011.01.30 Mirek Cymer / Custom Creations
// Form Validation functions

function exists(inputValue)
{
   	var aCharExists = false;

   	// Step through th inputValue, using the charAt()
   	// method to detect non-space characters.

   	for(var i=0;i<=inputValue.length;i++)
   	{
   		if(inputValue.charAt(i) != " " && inputValue.charAt(i) != "")
   		{
   			aCharExists = true;
   			break;
   		}
   	}
   	return aCharExists;
}

function isValidEmail(inputValue)
{
   	var foundAt = false;
	var foundDot = false;

	// Step through the inputValue looking for
	// "@" and "."

	for (var i=0;i<=inputValue.length;i++)
	{
		if(inputValue.charAt(i) == "@") { foundAt = true; }
		else { if(inputValue.charAt(i) == ".") { foundDot = true; }}
	}

	if(foundAt && foundDot) { return true; }
	else { return false; }

	return false;
}

function getRadVal(btnGroup)
{
	for (var i=0; i<btnGroup.length; i++) { if(btnGroup[i].checked) { return btnGroup[i].value; }}
}

/*
Given its ID, validates form based on presence of following attributes in its elements:
	"reqd" (is required; equals message to display when failed)
	"email" (is valid email address; equals message to display when failed)
	"either" (equals ID of other element) and "eitherMsg" (equals message to display when failed)
	"both" (equals ID of other element) and "bothMsg"(equals message to display when failed)
*/
function validateForm(form)
{
	var f = document.getElementById(form);

	for(var i=0; i<f.elements.length; i++)
	{
		var el = f.elements[i];

		if(el.getAttribute("reqd") && !exists(el.value))
		{
			el.focus();
			alert(el.getAttribute("reqd"));
			return false;
		}

		if(el.getAttribute("email") && !isValidEmail(el.value))
		{
			el.focus();
			alert(el.getAttribute("email"));
			return false;
		}

		if(el.getAttribute("either"))
		{
			var el2 = document.getElementById(el.getAttribute("either"));

			if(!el.checked && !el2.checked)
			{
				el.focus();
				alert(el.getAttribute("eitherMsg"));
				return false;
			}
		}
	}

	return true;
}
