DTech.Rules = {
	
	'form:reset' : function(e, event) {
		if (!confirm('Are you sure you want to reset this form?')) {
			Event.stop(event);
		}
	},
	
	'input#pdf:click' : function(e) 
	{
		// Retrieve the row with an id of email2.
		var email = $('email2');
		
		// If the row is visible hide it otherwise show it.
		// Also If the row is visible this means that they want a pdf version emailed to them
		// which does not require their physical address so only show the name and email address
		// as required.  If the row is hidden then they want the brochure emailed to them
		// so show the required spans for all the physical address info.
		if (email.visible()) 
		{ 
			email.hide();
			$('address_required').show();
			$('city_required').show();
			$('state_required').show();
			$('zip_required').show();

		}
		else 
		{
			email.show();
			$('address_required').hide();
			$('city_required').hide();
			$('state_required').hide();
			$('zip_required').hide();
		}
	},
	
	'input#SendMail:click' : function(e, event) {
		var name = $('name');
		var email = $('email3');
		var address = $('address');
		var city = $('city');
		var state = $('state');
		var zip = $('zip');
		var box = $('pdf');
		var msg = "";
		var verification = 0;

			
		if ($F(name) == "") {
		msg += "Please fill out the *Name* field!\n";
		verification ++;
		}
		
		if ($F(box) == "Yes" && $F(email) == "") {
		msg += "Please fill out the *Email* field!\n";
		verification ++;
		}
		
		if ($F(box) == null && $F(address) == "") {
		msg += "Please fill out the *Address* field!\n";
		verification ++;
		}
		
		if ($F(box) == null && $F(city) == "") {
		msg += "Please fill out the *City* field!\n";
		verification ++;
		}
		
		if ($F(box) == null && $F(state) == "") {
		msg += "Please fill out the *State* field!\n";
		verification ++;
		}
		
		if ($F(box) == null && $F(zip) == "") {
		msg += "Please fill out the *Zip Code* field!\n";
		verification ++;
		}
		
		if (verification != 0) {
		alert(msg);
		Event.stop(event);
		}
	}
	
};


DTech.EventSelectors = {
	
	version: '1.0_pre',
	
	cache: [],
	
	start : function(rules) {
		this.rules = rules || {};
	    this.timer = new Array();
		this._extendRules();
		this.assign(this.rules);
	},
	
	assign : function(rules) {
		var observer = null;
		
		this._unloadCache();
		
		rules._each(function(rule) {
			$A(rule.key.split(',')).each(function(selector) { 
				var pair = selector.split(':');
				var event = pair[1];
				
				$$(pair[0]).each(function(element) {
					if (pair[1] == '' || pair.length == 1) {
						return rule.value(element);
					}
					
					if (event.toLowerCase() == 'loaded') {
						this.timer[pair[0]] = setInterval(this._checkLoaded.bind(this, element, pair[0], rule), 15);
					}
					else {
						observer = function(event) {
							var element = Event.element(event);
							if (element.nodeType == 3) {
								element = element.parentNode;
							}
							
							rule.value($(element), event);
						};
						
						this.cache.push([element, event, observer]);
						
						Event.observe(element, event, observer);
					}
				}.bind(this));
			}.bind(this));
		}.bind(this));
	},
  
	_unloadCache : function() {
		if (!this.cache) {
			return;
		}
		
		for (var i = 0; i < this.cache.length; i++) {
			Event.stopObserving.apply(this, this.cache[i]);
			
			this.cache[i][0] = null;
		}
		
		this.cache = [];
	},
	
	_checkLoaded : function(element, timer, rule) {
		if (element.tagName != 'undefined') {
			clearInterval(this.timer[timer]);
			
			rule.value($(element));
		}
	},
	
	_extendRules : function() {
		Object.extend(this.rules, {
			_each: function(iterator) {
				for (key in this) {
					if(key == '_each') {
						continue;
					}
					
					var value = this[key];
					var pair = [key, value];
					pair.key = key;
					pair.value = value;
					
					iterator(pair);
				}
			}  
		});
	}
	
};


Event.observe(window, 'load',
	function() {
		DTech.EventSelectors.start(DTech.Rules);
	}
);


Ajax.Responders.register({
	onComplete: function() {
		DTech.EventSelectors.assign(DTech.Rules);
	}
});