/* ------------------------------------------------------------------------------------ *
| GLOBALS Javascript FILE :: For QUERIDODESIGN Projects and Contact Page 				|
| js/globals.js																			|
| 																						|
| PROJECT :: www.queridodesign.net v3.0													|
| Copyright (C) 2008, QUERIDODESIGN, Switzerland										|
| 																						|
+ ------------------------------------------------------------------------------------- +
| Authors :: Christoph Pojer															|
* ------------------------------------------------------------------------------------- */

// CONTACT FORM CLASS :: zilenCe
var Form = new Class({

	Implements: [Options],

	options: {
		classNormal: 'normal',
		classFocus: 'focus',
		classError: 'error'
	},

	elements: {},
	voptions: {},

	Regex: {
		email: /^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i
	},

	initialize: function(el, options){
		this.setOptions(options);

		this.el = $(el).addEvent('submit', this.submit.bindWithEvent(this));

		this.els = this.el.getElements('input[type!=submit][type!=reset], textarea').filter(function(el){
			var title = el.get('title');

			el.addEvents({
				focus: this.focus.bind(this, el),
				blur: this.blur.bind(this, el)
			});

			if(!title) return false;

			this.elements[el.id] = el;
			this.voptions[el.id] = title.split(',').map(String.trim).map(String.toLowerCase);

			el.erase('title');

			return true;
		}, this);
	},

	submit: function(e){
		this.error = false;
		this.els.each(function(el){
			this.check(el);
		}, this);

		if(this.error){
			e.stop();
			return;	
		}

		var obj = {};

		this.el.getElements('input[type!=submit][type!=reset], textarea').each(function(el){
			obj[el.name] = el.get('value');
		});

		new Request.JSON({
			url: this.el.get('action'),
			onComplete: (function(j){
				if(!j || !j.msg) return;

				this.el.getElements('input[type=submit], input[type=reset], li.required').set('tween', {duration: 200}).fade(0);
				//this.el.set('tween', {duration: 200}).fade(0);

				// $('message_sent').getElement('div.contact_msg_'+j.msg).setStyle('display', 'block');

				$('message_sent').setStyles({
					opacity: 0,
					display: 'block'
				}).set('tween', {duration: 800}).fade(1);
			}).bind(this)
		}).post(obj);
	},

	focus: function(el){
		el.morph(el.get('tag')+'.'+this.options.classFocus);
	},

	blur: function(el){
		el.morph(el.get('tag')+'.'+this.options.classNormal);
	},

	check: function(el){
		var error = false,
			value = el.get('value');
		if(this.voptions[el.id].contains('required') && (!value || value.test(/^\s+$/))){
			error = true;
		}

		Hash.each(this.Regex, function(v, i){
			if(!this.voptions[el.id].contains(i)) return;

			if(!value.test(v)) error = true;
		}, this);

		el.set('value', value.trim());

		if(error){
			this.error = error;
			el.morph(el.get('tag')+'.'+this.options.classError);
		}
	},

	reset: function(){
		this.els.fireEvent('keydown');
		this.error = false;
		this.el.getElements('input[type!=submit][type!=reset], textarea').set('value', '');
	}
});

window.addEvent('domready', function() {

	// REMOVE DOTTED LINES FROM IE 'a' TAGS and FORM BUTTONS :: nfq
	$$('#resetForm, #sendForm').addEvent('focus', function() {
		this.blur();
	});
});

