

var valmsg = [];

function validateForm(formName)
{	
	valmsg = [];
	var f = document[formName];
	var pass = true;
	
	for(var o in f.elements)
	{
		
		var el = f.elements[o];		
		try
		{				
			if(el && el.type && el.getAttribute("vsimval"))
			{			
				switch(el.type)
				{
					case "text":
					case "textarea":
					case "select":
					case "select-one":					
						doValidate(el, el.type, el.value, el.getAttribute("vsimval"), el.getAttribute("vsimvalmsg")); 
						break;
				}
			}
		
		}catch(e){}
	}	
	if(valmsg.length > 0)
	{		
		pass = false;
		showValidateMsg();
	}
	
	return pass;
}

function doValidate(el, type, value, val, msg)
{
	var pass = true;
	val = val.split("|");
	
	switch(val[0])
	{
		case "text": 
			var r = new RegExp("^[ 0-9a-zA-Z\-\,\.]{" + val[1]+ "," + val[2]+ "}$");
			pass = r.test(value);
			break;
		
		case "email":					
			var r = /^([\w\.]+)@([\w]+)(.[\w]{2,3}){1,2}$/;
			pass = r.test(value);
			break;

		case "post":					
			var r = /^[\w\s]{6,10}$/
			pass = r.test(value);
			break;

		case "phone":					
			var r = /^[ 0-9\(\)\+]{6,}$/
			pass = r.test(value);
			break;			
	}
	
	if(!pass)
	{
		valmsg[valmsg.length] = msg;
	}
}

function showValidateMsg()
{
	var s = '<h3 class="text_error">Enquiry submission could not be completed</h3><p class="text_error">' ;
	for(var i = 0 ; i < valmsg.length ; i++)
	{
		s += valmsg[i] + '<br />';		
	}
	s += '</p><p class="text_error"><i style="font-size:90%;">Text should be limited to letters, numbers, dashes, commas, spaces and periods.</i></p>';
	document.getElementById("errormsg").innerHTML = s;
}
