/**
 *	FormValidation
 *
 *	Author: Warren Krewenki <warren@vdhinc.com>
 *	Last Modified : August 14, 2006
 *
 *	Unobtrusive JSON-based approach to form validation.  Simply declare required
 *	fields as having the class "required" or whatever class you choose.
 *
 *	The javascript runs on page load, and attaches itself to any forms. When a form is submitted,
 *	it is first validated against this script before normally submitting.  If the onsubmit event
 *	is already spoken for, the code simply doesn't run.
 *
 */
var FormValidation = {
	effects : false,
	originalBorder : null,
	errorBorder : '2px dashed #f00',
	requiredClass : 'required',
	init : function(){
		if(typeof Scriptaculous != 'undefined'){
			this.effects = true;
		}
		forms = document.getElementsByTagName('form');
		if(forms.length > 0){
			for(i=0;i<forms.length;i++){
				if(forms[i].onsubmit){
					// onsubmit already exists, time to hacky hacky
				//} else {
					//forms[i].setAttribute('onsubmit','return FormValidation.Validate(this);');
				}
			}
		}
	},
	Validate : function(formObj){
		els = formObj.getElementsByTagName('*');
		i=0;
		while(i < els.length){
			thisNode = els[i];
			if(typeof thisNode != 'undefined'){
				if(thisNode.className == this.requiredClass){
					switch(thisNode.tagName){
						case 'INPUT':
							if(thisNode.value <= 0 || ((thisNode.type == 'CHECKBOX' || thisNode.type == 'checkbox') && thisNode.checked == false) || ((thisNode.name == 'email' || thisNode.id == 'email') && !this.ValidateEmail(thisNode.value))){
								thisNode.focus();
								thisNode.select();
								this.DisplayError(thisNode,i);
								return false;
							} else {
								if(!this.effects){
									thisNode.style.border = this.originalBorder;
								}
							}
							break;
						case 'SELECT':
							if(thisNode.selectedIndex <= 0){
								thisNode.focus();
								this.DisplayError(thisNode,i);
								return false;
							} else {
								if(!this.effects){
									thisNode.style.border = this.originalBorder;
								}
							}
							break;
						case 'TEXTAREA':
							if(thisNode.value <= 0){
								thisNode.focus();
								thisNode.select();
								this.DisplayError(thisNode,i);
								return false;
							} else {
								if(!this.effects){
									thisNode.style.border = this.originalBorder;
								}
							}
							break;
						default:
					
							break;
					}
				}
			i++;
			}
		}
		return true;
	},
	Test : function(node){
		
	},
	DisplayError : function(obj,id){
		if(!obj.id){
			newId = 'validate'+id;
			obj.setAttribute('id',newId);
		}
		if(this.effects == true){
			new Effect.Highlight(obj.id,{startcolor:'#ff9999', endcolor: '#ffffff'});
			new Effect.Shake(obj.id);
			new Effect.Pulsate(obj.id);
			return false;
		} else {
			document.getElementById(obj.id).style.border = this.errorBorder;
			return false;
		}
	},
	ValidateEmail : function(strEmail){
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
		if (strEmail.search(validRegExp) == -1){
			return false;
		} else {
			return true;
		}
	}
};

var oldLoad = window.onload;
window.onload = function(){
	if(oldLoad){
		oldLoad();
	}
	FormValidation.init();
	return true;
}