var selectBlank;
var messageOverride = new Array();
function validateEmail(src) {
     var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
}

var x = 1;
var invalid = 0;

function needsValid(element, message)
{
	
	//console.debug(message);
	if(message == 'required')
	{
		if(messageOverride['required'])
		{
			message = messageOverride['required'];
		} else {
			message = "Please enter a value in this field";
		}
	}
	
	if(message == 'email')
	{
		if(messageOverride['email'])
		{
			message = messageOverride['email'];
		} else {
			message = "Please enter a valid email address";
		}
	}
	
	//console.debug(element);
	
	var fixMe = document.createTextNode(message);
	var fixCon = document.createElement('span');
	fixCon.setAttribute('class', 'validationWarn');
	fixCon.setAttribute('className', 'validationWarn');
	fixCon.setAttribute('id', 'validation[' + x + ']');
	x++;
	fixCon.appendChild(fixMe);

	element.parentNode.appendChild(fixCon);

	element.setAttribute('class', 'validationWarnInput');
	element.setAttribute('className', 'validationWarnInput');
	
	invalid++;
}



function clearValidations()
{
	var c = 0;
	while(c <= x)
	{
		if(document.getElementById('validation[' + c + ']'))
		{
			var de = document.getElementById('validation[' + c + ']');
			de.parentNode.removeChild(de);
		}
		c++;
	}
	x = 1;
	c = 0;
	invalid = 0;
}

function validateForm(eventObject)
{
	clearValidations();
	invalid = 0;
	var i = 0;
	var sels = document.getElementsByTagName('select');
	var ins = document.getElementsByTagName('input');
	
	//console.debug(ins);
	
	var v = 0;
	
	if(sels.length > 0)
	{
		while(i < sels.length)
		{
			if(Validate[sels[i].id] && sels[i].id && sels[i].scrollWidth > 0)
			{
				sels[i].removeAttribute('class');
				if((sels[i].value == 'Select State') || sels[i].value == selectBlank || sels[i].value == '')
				{
					needsValid(sels[i], 'required');
					v++;
				}
			
			}
			i++;
		}
	}
	i = 0;
	var fObj;
	if(ins.length > 0)
	{
		while(i < ins.length)
		{
			if(Validate[ins[i].id] && ins[i].id)
			{
				ins[i].removeAttribute('class');
				if(Validate[ins[i].id] == 'email' && ins[i].scrollWidth > 0)
				{
					if(!validateEmail(ins[i].value))
					{
						needsValid(ins[i], 'email');
						v++;
					}
				} 
				if(Validate[ins[i].id] == 'required' && ins[i].scrollWidth > 0){
					if(ins[i].value == '' || !ins[i].value)
					{
						needsValid(ins[i], 'required');
						v++;
					}
				}
				fObj = ins[i];
			}
			
			i++;
		}
	}
		
	if(invalid > 0)
	{
		if (eventObject.preventDefault) {
			eventObject.preventDefault();
		} else if (window.event) /* for ie */ {
			window.event.returnValue = false;
		}
		
		return false;
	} else { 
		var brk;
		var s = 0;
		while(s < 6 || brk == true)
		{
			if(fObj.tagName == "FORM")
			{
				s = 99;
				fObj.submit();
				break;
				brk = true;
			} else {
				fObj = fObj.parentNode;
			}
			s++
		}
		return true; 
	}

}

function inputSubAdd()
{
	var iS = document.getElementsByTagName('input');
	var s = 0;
	
	if(iS.length > 0)
	{
		while(s < iS.length)
		{
			if(iS[s].type == 'submit')
			{
				if (iS[s].addEventListener) {
					iS[s].addEventListener("click", validateForm, false);
				} else if (iS[s].attachEvent) {
					iS[s].attachEvent("onclick", validateForm);
				}
			}
			s++;
		}
	}
}

function validation()
{
	window.setTimeout(inputSubAdd, 100);
}
