var fV = {

  validationGroup: new Array(),
  elementGroup: new Array(),  
  radioGroup: new Array(),

  addEvent: function(elm, evType, fn, useCapture) 
  {
    // cross-browser event handling for IE5+, NS6 and Mozilla By Scott Andrew
    if (elm.addEventListener)
    {
      elm.addEventListener(evType, fn, useCapture);
      return true;
    }
    else if (elm.attachEvent)
    { 
      var r = elm.attachEvent('on' + evType, fn);
      return r;
    }
    else
    {
      elm['on' + evType] = fn;
    }
  },

  setValidationGroup: function(validArray)
  {
  	fV.validationGroup = validArray;
  },
  
  setElementGroup: function(elementArray)
  {
  	fV.elementGroup = elementArray;
  },   
  
  setRadioGroup: function(radioArray)
  {
  	fV.radioGroup = radioArray;
  },  

  init: function() 
  {
    for(var x in fV.radioGroup)
    {
  	var buttonName = document.getElementById(fV.radioGroup[x][0]).getAttribute("name");
  	var radiobuttons = document.getElementsByName(buttonName);
  	for(var y = 0; y < radiobuttons.length; y++)
  	{
  		fV.addEvent(radiobuttons[y], 'blur', fV.checkBlurValid, false);
  	}
    }
    
    for(var y in fV.elementGroup)
    {
  	fV.addEvent(document.getElementById(fV.elementGroup[y][0]), 'blur', fV.checkGroupBlurValid, false);
    }    
    
    for (var i in fV.validationGroup) 
    {
      	fV.addEvent(document.getElementById(fV.validationGroup[i][0]), 'blur', fV.checkBlurValid, false);
    }
    
    document.getElementById(fV.validationGroup[0][0]).form.onsubmit = fV.checkValidSubmit; // Safari
  },

  checkValidSubmit: function()
  {
	var validForm = true;
	var validError = '';
	
	for (var i in fV.validationGroup) 
	{
		//need a try incase error message is not there, therefore an exception would be thrown
		try
		{
			//if the field is empty or invalid
			if(!fV.checkValid(document.getElementById(fV.validationGroup[i][0])))
			{
				//would be best if there was a line break
				if(validForm)
				{
					validForm = false;
				}
				validError += fV.validationGroup[i][1];
				fV.setErrorMessage(document.getElementById(fV.validationGroup[i][0]));				
			}
		}catch(e){}
    	}
    	
	for(var j in fV.radioGroup)
	{
		if(!fV.checkValid(document.getElementById(fV.radioGroup[j][0])))
		{
			validForm = false;
			validError += fV.radioGroup[j][1];
			fV.setButtonErrorMessage(document.getElementById(fV.radioGroup[j][0]));	
		}
    	}
    	
    	if(!fV.checkValidGroup())
    	{
    		validForm = false;
    		validError += fV.elementGroup[0][1];
    		fV.setGroupErrorMessage(document.getElementById(fV.elementGroup[0][0]));
    	}

    	if(!validForm)
    	{
		try
		{
			//if error div is there, set the value of it
			var errorDiv = document.getElementById("errordiv");
			try
			{
				errorDiv.removeChild(errorDiv.firstChild);
			}
			catch(e)
			{
			}
			var errormessage = validError;
			var errordata = document.createTextNode(errormessage);
			errorDiv.appendChild(errordata);
		}
		catch(e)
		{
		
		}
	}
	return validForm;
  },
  
  checkBlurValid: function(e)
  {
	if(window.event)
	{
		var target = window.event.srcElement;
	}
	else
	{
		if(e)
		{
			var target = e.target;
		}
		else
		{
			var target = null;
		}
	}
	
	if(fV.checkValid(target))
	{
		fV.clearErrorMessage(target);
	}
	else
	{
		fV.setErrorMessage(target);
	}
  },
  
  checkGroupBlurValid: function()
  {
	if(fV.checkValidGroup())
	{
		fV.clearErrorMessage(document.getElementById(fV.elementGroup[0][0]));
	}
	else
	{
		fV.setGroupErrorMessage(document.getElementById(fV.elementGroup[0][0]));
	}
  },  
  
  checkValid: function(target)
  {
	//if its an input element, you can get the type: checkbox, file, hidden, image, password, radio, reset, submit, text
	//otherwise it will be a: TextArea, Select
	switch(target.type.toLowerCase())
	{
		case 'text':
			return fV.checkValidText(target);
			break;
		case 'textarea':
			return fV.checkValidText(target);
			break;
		case 'checkbox':
			return fV.checkValidCheckBox(target);
			break;
		case 'radio':
			return fV.checkValidRadioButton(target);
			break;
		case 'select-one':
			return fV.checkValidText(target);
			break;
		case 'select-multiple':
			return fV.checkValidText(target);
			break;		
	}
  },
  
  checkValidText: function(target)
  {
	//if there is no value
	if(!target.value)
	{
		return false;
	}
	else
	{	
		for (var i = 0; i < fV.validationGroup.length; i++) 
		{
		      	//if this is the element that is clicked on
		      	if(target.getAttribute("id") == fV.validationGroup[i][0])
		      	{
		      		if(fV.validationGroup[i][2] == undefined)
		      		{
		      			return true;
		      		}
		      		else
		      		{
		      			//if there is a value, does it have a regular expression?
		      			return fV.handleValidity(target, fV.validationGroup[i][2]);
		      		}
		      	}
    		}
	}
  },
  
  checkValidGroup: function()
  {
	if(fV.elementGroup.length < 1 || fV.elementGroup == null)
	{
		return true;
	}
	else
	{
		for(var i in fV.elementGroup)
		{
			var target = document.getElementById(fV.elementGroup[i][0]);

			//if there is no value
			if(!target.value)
			{
				continue;
			}
			else
			{	
				if(fV.elementGroup[i][2] == undefined)
				{
					return true;
				}
				else
				{
					//if there is a value, does it have a regular expression?
					if(fV.handleValidity(target, fV.elementGroup[i][2]))
					{
						return true;
					}
				}
			}
		}
		return false;
	}
  }, 
  
  checkValidCheckBox: function(target)
  {
  	if(target.checked)
  	{
    		return true;
  	}
  	else
  	{
  		return false;
  	}
  },
  
  checkValidRadioButton: function(target)
  {
  	var buttonName = target.getAttribute("name");
  	var radiobuttons = document.getElementsByName(buttonName);

  	for(var i = 0; i < radiobuttons.length; i++)
  	{
  		if(radiobuttons[i].checked)
  		{
  			return true;
  		}
  	}
  	return false;
  },
  
  handleValidity: function(target, regex)//if there is any regular expression
  {  	
  	var re = regex;
	if (!target.value.match(re))
	{
		return false;
    	}
    	else
    	{
    		return true;
    	}
  },  
  
  setErrorMessage: function(target)
  {
	var errormessage = "This is a required field.";
	for (var i = 0; i < fV.validationGroup.length; i++) 
	{
	      	if(target.getAttribute("id") == fV.validationGroup[i][0])
	      	{
	      		if(fV.validationGroup[i][1] != undefined)
	      		{
	      			errormessage = fV.validationGroup[i][1];
	      		}
	      		break;
	      	}
    	}
    	fV.setMessage(target, errormessage);			
  },
  
  setButtonErrorMessage: function(target)
  {
	var errormessage = "This is a required field.";
	for (var i = 0; i < fV.radioGroup.length; i++) 
	{
	      	if(target.getAttribute("id") == fV.radioGroup[i][0])
	      	{
	      		if(fV.radioGroup[i][1] != undefined)
	      		{
	      			errormessage = fV.radioGroup[i][1];
	      		}
	      		break;
	      	}
    	}
    	fV.setMessage(target, errormessage);		
  },
  
  setGroupErrorMessage: function(target)
  {
	var errormessage = "This is a required field.";
	if(fV.radioGroup[0][1] != undefined)
	{
		errormessage = fV.elementGroup[0][1];
	}
	fV.setMessage(target, errormessage);
  },
  
  setMessage: function(target, errormsg)
  {
  	//if there is an error div, remove it. if there is not a div, it throws an exception
  	fV.clearErrorMessage(target);
  	var errorIndicator = document.createElement('div');
	errorIndicator.setAttribute("class", "error");
	errorIndicator.setAttribute("className", "error");
	
	var errormessage = errormsg;
    	var errordata = document.createTextNode(errormessage);
	errorIndicator.appendChild(errordata);
	target.parentNode.insertBefore(errorIndicator,target.parentNode.firstChild);
  },
  
  clearErrorMessage: function(target)
  {
  	try
  	{
  		if(target.parentNode.firstChild.getAttribute("class") == "error")
  		{
  			target.parentNode.removeChild(target.parentNode.firstChild);
  		}
  	}
  	catch(e)
  	{
  		//alert(e);
  	} 
  }
  
}

