
var lblColorSetting = "";
var fldColorSetting = "";
var BorderSettings = "";
var rbxBorderSettings = "";
var activatedtxt = false;
var activatedrbx = false;
var valid = true;


function FormValidation(submit)
{
    var ServerSideValidate = false;
	strValidate = "";	
	if(submit=='reset')
	{
		document.forms[0].reset();
	}

	formulier = document.forms[0];
	valfields = 0;
	for(i=0;i<formulier.length;i++)
	{
		validationtype = document.forms[0][i].attributes["validationtype"];
		field = document.forms[0][i];
		
		if(validationtype != null)
		{
			label = document.getElementById("label" + field.name);
			
			if(validationtype.value == 5)
			{
		        ServerSideValidate = true;
		    }

			if(!activatedtxt && field.currentStyle && field.type != "checkbox" && field.type != "radio")// get form border styling and color
			{
				if(field.currentStyle.borderStyle)
					BorderSettings += field.currentStyle.borderStyle;
				if(field.currentStyle.borderWidth)
					BorderSettings += " " + field.currentStyle.borderWidth;
				if(field.currentStyle.borderColor)
					BorderSettings += " " + field.currentStyle.borderColor;				
				if(field.currentStyle.color)
					fldColorSetting += field.currentStyle.color;				
				if(label.currentStyle.color)
					lblColorSetting += label.currentStyle.color;
				if(BorderSettings=="inset 2px #000000")
				{
					BorderSettings = "inset 2px";
				}
				else if(field.currentStyle.borderStyle=='none' && field.currentStyle.borderWidth!=null)
				{
					BorderSettings = "solid " + field.currentStyle.borderWidth + ' ' + field.currentStyle.borderColor;
				}
				activatedtxt = true;
			}
			else if(!activatedrbx && field.currentStyle && (field.type == "checkbox" || field.type == "radio"))
			{
				if(field.currentStyle.borderStyle)
					rbxBorderSettings += field.currentStyle.borderStyle;
				if(rbxBorderSettings != "none")
				{
					if(field.currentStyle.borderWidth)
						rbxBorderSettings += " " + field.currentStyle.borderWidth;
					if(field.currentStyle.borderColor)
						rbxBorderSettings += " " + field.currentStyle.borderColor;				
					if(rbxBorderSettings=="inset 2px #000000")
						rbxBorderSettings = "inset 2px";
				}
				activatedrbx = true;
			}


			label.style.color = lblColorSetting;
			if(field.type != "checkbox" && field.type != "radio")
			{
				field.style.border = BorderSettings;
			}
			field.style.color = fldColorSetting;			
						
			if(field.type == "checkbox" || field.type == "radio")
			{
				if(validationtype.value > 0)
				{
					field.style.border = rbxBorderSettings;
					cbox = formulier[field.name];
					oneChecked = false;
					if(cbox.length > 0)
					{
						for(c=0;c<cbox.length;c++)
						{
							if(cbox[c].checked)
							{
								oneChecked = true;
								for(c=0;c<cbox.length;c++)
								{
									cbox[c].style.border = rbxBorderSettings;
								}								
							}
						}
						i = i + cbox.length -1;
					}
					else
					{
						if(field.checked)
						{
							oneChecked = true;	
						}
					}					
					if(!oneChecked)
					{
						if(cbox.length > 0)
						{
							for(c=0;c<cbox.length;c++)
							{
								cbox[c].style.border = "dashed 1px red";
								cbox[c].onblur = function()
								{
									FormValidation();
								}
							}
						}
						else
						{
							field.style.border = "dashed 1px red";
							field.onblur = function()
							{
								FormValidation();
							}
						}					
						strValidate += "<br />&nbsp;&nbsp;-&nbsp;" + label.innerHTML;
						label.style.color = "red";
					}
				}
			}
			else
			{
				valfields++;
				Validate(field,validationtype.value);
			}
		}
	}
	
	if(strValidate != "")
	{
		document.getElementById('errormsg').innerHTML = strValidationText + '<br />' + strValidate;
		document.getElementById('errormsg').style.display = "block";
		submit = "";
		return false;
	}
	else
	{
		document.getElementById('errormsg').innerHTML = "";
		document.getElementById('errormsg').style.display = "none";
		if(ServerSideValidate)
		{
		    document.getElementById('postback').value = "validate";
		}
		else
		{
		    document.getElementById('postback').value = "true";
		}
		if(submit != null && submit=='submit')
		{
            document.forms[0].submit();
		}
		submit = "";
		
	}
}
function Validate(field,validationtype)
{
	label = document.getElementById("label" + field.name);
	ipField = document.getElementById(field.id);
	field.value = field.value.toString().replace(/^\s+|\s+$/, '')

	if(validationtype == 1 && field.value == "") // Required
	{
		valid = false;
	}
	else if(validationtype == 2) // E-mail
	{
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if(!filter.test(field.value))
		{
			valid = false;
		}
	}
	else if(validationtype == 3) // Text
	{
		var filter = /^[a-zA-Z]{1,255}$/;
		if(!filter.test(field.value))
		{
			valid = false;
		}
	}
	else if(validationtype == 4) // Numbers
	{
		var filter = /^[0-9]{1,255}$/;
		if(!filter.test(field.value))
		{
			valid = false; 
		}
	}
	else if(validationtype == 5 && field.value == "") // Secret Code
	{
	    // real validation after postback. First just a required field...
	    valid = false;
	}
	if(valid==false)
	{		
		if(validationtype == 2)
		{
			strValidate += "<br />&nbsp;&nbsp;-&nbsp;" + label.innerHTML.replace(/:/gi,"").replace(/\*/gi,"") + "&nbsp;&nbsp;(AAA@BBB.CC) ";
		}
		else
		{
			strValidate += "<br />&nbsp;&nbsp;-&nbsp;" + label.innerHTML.replace(/:/gi,"").replace(/\*/gi,"");
		}
		label.style.color = "red";
		ipField.style.border = "dashed 1px red";
		ipField.style.color = "red";
		ipField.onblur = function()
		{
			FormValidation();
		}
	}
	valid = true;
}
