var ELEMENT_NODE = 1;

var flag = true;
var checkFormResult = "";

function checkForm(form)
{
	/*
	form = document.getElementById(formId);
	if(!form)
	{
		alert("Не могу проверить данные на корректность ввода!");
		return false;
	}
	*/
	flag = true;
	check(form);
	if (!flag)
	{
		alert("Допущены ошибки при заполнении полей ввода");
	}
	return(flag);
}

function check($node)
{
	if($node == null)
	{
		return;
	}

	while($node != null/* && flag*/)
	{
		if($node.nodeType == ELEMENT_NODE)
		{
			var fieldType = $node.getAttribute("mandatory");
			if( fieldType != null)
			{

				switch(fieldType)
				{
					case "int":
					case "float":
					case "date":
					case "varchar":
						//localFlag = ($node.getAttribute("value") != "")
						localFlag = ($node.value != "")
						break;
					case "text":
						//localFlag = ($node.firstChild != null)
						localFlag = ($node.value != null)
						break;
					case "email":
						email1 = $node.getAttribute("value");
						rExp = /^[a-z0-9_-]{1,20}@(([a-z0-9-]+\.)+(com|net|org|mil|edu|gov|arpa|info|biz|inc|name|[a-z]{2})|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$/gi;
						localFlag = (!email1.search(rExp));
						break;
				    case "checkboxgroup":
				        localFlag = testCheckBoxGroup($node);
				        break;
				    default:
				        alert(fieldType);
				        break;
				}

				$node.style.backgroundColor = localFlag ? "white" : "#FFCCCC";
				flag = flag && localFlag;

			}
			var a = $node.firstChild;
			check(a);
		}
		$node = $node.nextSibling;
	}
}

function testCheckBoxGroup($node)
{
    var flag = false;

    while($node != null)
	{
		if($node.nodeType == ELEMENT_NODE)
		{
			var fieldType = $node.getAttribute("type");
			if( fieldType != null && fieldType == "checkbox")
			{
				flag = flag || $node.checked;
			}
			var a = $node.firstChild;
			flag = flag || testCheckBoxGroup(a);
		}
		$node = $node.nextSibling;
	}

    return flag;
}
