// PROTOFORM v1.0 (21/05/2008)- F.BURATTI - WWW.CSSREVOLT.COM

REGEX_AUTO_FIELD  = /^[^_]+(_Req)?(_(Tel|Email|Url|Date))?$/;
REGEX_BLANK       = /^\s*$/;
REGEX_EMAIL       = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
REGEX_TEL         = /^([0-9]*\-?\ ?\/?[0-9]*)$/;
REGEX_URL         = /^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/;
REGEX_DAY         = /^(0?[1-9]|[1-2][0-9]|3[01])$/;
REGEX_MONTH       = /^(0?[1-9]|1[0-2])$/;
REGEX_YEAR        = /^[0-9]{2,4}$/;
REGEX_TYPED_FIELD = /_(Tel|Email|Url|Date)$/;

function sendData (idform) {
	var url = idform + '.php';
	var pars = Form.serialize(idform);
		var myAjax = new Ajax.Request( url, {
								  		method: 'post', 
										parameters: pars, 
										onLoading: showLoad, 
										onComplete: function (transport) {												
													    var newData = transport.responseText;
														$('working').hide();
														$('box').insert({top:newData});
														if ($('error')) { $('error').hide(); }
														Form.reset($(idform));									
														}										
										    }); //myAjax request
}//sendData

function showLoad () {
	$('box').insert({top:'<p id="working">loading...</p>'});
}

function addFormChecks() {	
	$$('form.validate').each(function(form) {
    	Event.observe(form,'submit',checkForm);	
	});
} // addFormChecks

function checkForm(e) {
    var form = Event.element(e);
	Event.stop(e);
    var errors = '';
    var faulty = null;
		if ($('response')) {
			$('response').hide();		
			}
	for (var index = 0; index < form.elements.length; ++index) {
		
        var field = form.elements.item(index);
		
        	if (!field.id.match(REGEX_AUTO_FIELD)) {
            	continue;
			}
        		
        var value = $F(field);
			
       		if (field.id.match(/_Req/) && value.match(REGEX_BLANK)) {
            	errors += '<li>' + field.title + '</li>';
            	faulty = faulty || field;
            	continue;
        	}
		
		var typedfield = field.id.match(REGEX_TYPED_FIELD);
		
			if (typedfield  && !value.match(REGEX_BLANK)) {
	    		var type = typedfield[1];
	    		var error = checkTypedField(value, type);
	    		if (error) {
					errors += '<li>' + field.title + '</li>';
					faulty = faulty || field;
	    		}
			}
	}
		
    if (errors==0) {
		sendData(form.id); // validation passed, send ajax request
		return;
	}
		if (!$('error')) {	
			$('box').insert({top:'<ul id=\"error\">' + errors + '</ul>'});
		}
		else {
			$('error').replace('<ul id=\"error\">' + errors + '</ul>');	
		}
	
    faulty.focus();
	
} //checkForm

function checkTypedField(value, type) {

    if ('Tel' == type) {
		var phone= value;
		if (!phone.match(REGEX_TEL)) {
        	return true;
		}     
    }
	
	if ('Email' == type) {
		var address= value;
		if (!address.match(REGEX_EMAIL)) {
        	return true;
		}     
    }
	
	if ('Url' == type) {
		var resource= value;
		if (!resource.match(REGEX_URL)) {
        	return true;
		}     
    }
  
      if ('Date' == type) {
        var comps = value.split('/');
        if (3 != comps.length || !comps[0].match(REGEX_DAY) ||
            !comps[1].match(REGEX_MONTH) ||
            !comps[2].match(REGEX_YEAR))
            return true;
    }
	
    return null;
} //checkTypedField

function hoverFocus() { 
 	$$('form.validate input','form.validate textarea').each(function(item) {
    	Event.observe(item,'focus',function() {
      		Element.addClassName(this,'hoverfocus');
    	}.bind(item));
    	Event.observe(item,'blur',function() {
      		Element.removeClassName(this,'hoverfocus');
    	}.bind(item));
	});
}//hoverFocus

function validationInit() {	
	hoverFocus();
	addFormChecks();
}

document.observe ('dom:loaded', validationInit, false); 
