var formCheckBoxes;
var idPrefix = "checkboxDiv-";

function getallcheckboxes() {

	var returnArray = new Array();

	if (document.forms && document.forms.length > 0)

 	for (f=0;f<document.forms.length;f++) {
		var form = document.forms[f];

		if (form && form.elements) {
			for (i=0;i<form.elements.length;i++) {
				var box = form.elements[i];
				if (box.type && box.type == "checkbox" && box.style) {
				box.className = 'heartCheckBoxHidden';
				returnArray.push(box);
				}
			}
		}
	}

	return returnArray;
}

function checkconsistency() {

	if (! formCheckBoxes) formCheckBoxes = getallcheckboxes();

 	for (i=0;i<formCheckBoxes.length;i++) {
		
		var box = formCheckBoxes[i];
		var id = box.id;
		var isChecked = box.checked;
		var heartDivLayer = document.getElementById(idPrefix + id);
					
		if (heartDivLayer) {
			if (isChecked && heartDivLayer.className != 'heartCheckBoxDivSelected') heartDivLayer.className = 'heartCheckBoxDivSelected';
			else if (! isChecked && heartDivLayer.className != 'heartCheckBoxDivEmpty') heartDivLayer.className = 'heartCheckBoxDivEmpty';
		} else {
			box.className = 'heartCheckBoxVisible';
		}
	}
}

function checkbox(checkboxId) {

	var checkBox = document.getElementById(checkboxId);
	if (checkBox) checkBox.checked = (! checkBox.checked);
	checkconsistency();

}

checkconsistency();

function ValidateForm()
{
	var retval = true;//may not have any validations, in that case we must be able to submit the form, therefore "true" as default..
					//if an exception occurs we still return true (if nothing has validated as false), then the server may do the validation instead.
	try
	{
		var collectedErrors = "";
		if (document.forms && document.forms.length > 0)
		{
			var form = document.forms["SchemaGenerated"];
			if (form && form.elements) 
			{
				for (i = 0; i < form.elements.length; i++) 
				{
					var element = form.elements[i];
					if(element.type && element.type == "hidden")
					{
						//check if this is a validation field: 
						if(element.name.indexOf("Validation_") == 0)
						{
							var regularString = element.value;
							var splittedName;
							var validateFieldName = "";
							var validateFieldElement;
							var errorTextFieldName;
							var validateFieldValue;
							var errorText;
							errorTextFieldName = element.name.replace("Validation_", "ErrorMessage_");
							errorText = form.elements[errorTextFieldName].value;
							//get name for the field that is to be validated. This info is part of the 
							//validation and errormessage-names (gets it from the validationfield):
							splittedName = element.name.split("_");
							for(j = 2; j < splittedName.length; j++)
							{
								if(validateFieldName == "")
								{
									validateFieldName = splittedName[j];
								}
								else
								{
									validateFieldName += "_" + splittedName[j];
								}
							}
							validateFieldElement = form.elements[validateFieldName];
							if(!validateFieldElement.type && validateFieldElement.length)
							{
								//only a list of radiobuttons matches this scenario, so we know we have
								//several available choices:
								if(validateFieldElement.length > 0)
								{
									validateFieldValue = "";
									 for (k=0; k < validateFieldElement.length; k++)
									 {
								        if (validateFieldElement[k].checked)
										{
											validateFieldValue = validateFieldElement[k].value;
											break;
										}
									} 
								}
							}
							else
							{
								if(validateFieldElement.type == "radio" || validateFieldElement.type == "checkbox")
								{
									//this may happen if there is only one radio that is not in a group:
									if(validateFieldElement.checked)
									{
										validateFieldValue = validateFieldElement.value;
									}
									else
									{
										validateFieldValue = "" //i.e. it is not selected
									}
								}
								else
								{
									validateFieldValue = validateFieldElement.value;
								}
							}
							//do the validation:
							var regularObject = new RegExp(regularString, "i");
							if(!validateFieldValue.match(regularObject))
							{
								retval = false;
								collectedErrors += errorText + "\n"
							}
						}
					}
				}
			}
		}
		if(!retval)
		{
			alert(collectedErrors);
		}
	}
	catch(e)	
	{
		//alert(e.description);
	}
	return retval;
}