/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var ValidForm = Class.create({
	initialize : function(){
//		$$('form').invoke('observe', 'submit', this.valider.bindAsEventListener(this));
		this.creatTinyMceElement();
	},
	valider : function(e){
	  try{
		e.stop();
		var form = e.element();
		var els = form.getElements();
		var messageError = '';
/*		for(i=0; i<els.length; i++){
			if (els[i].hasClassName('valid'))
				messageError += Object.isFunction(this[els[i].id]) ? this[els[i].id](els[i].id) : this.text(els[i].id);
		}
*/		console.log(messageError);
		if(messageError.length <= 1){
			form.request({parameters:{ajax:1}, onComplete:function(xhr){
				form.ancestors()[0].innerHTML = xhr.responseText;
			}});
		} else
			alert(messageError);
	  }catch(e){console.log(e);}
	},
	text : function(id){
		return this.valid(id, [$F(id).gsub(/\//, '').gsub(/\\/, '').gsub(/\'/, '').gsub(/\"/, '').gsub(/\</, '').gsub(/\>/, ''), /^[\_\w\-]+([\s\.\_\-]?[\w]+)*$/, 5, 255]);
	},
	mail : function(id){
		return this.valid(id, [$F(id).gsub(/\//, '').gsub(/\\/, '').gsub(/\'/, '').gsub(/\"/, '').gsub(/\</, '').gsub(/\>/, ''), /^[\_\w\-]+(\.[\_\w\-]+)*@[\_\w\-]+(\.[a-zA-Z]{2,})+$/, 5, 255]);
	},
	valid : function(id, ar){
		var ret = '';
		if($F(id) != ar[0])
			ret += ', Caracteres invalides';
		if ($F(id).search(ar[1]) == -1)
			ret += ', Format invalide';
		if ($F(id).length <ar[2])
			ret += ', trop peu de caractères (mini : '+ar[2]+')';
		if ($F(id).length > ar[3])
			ret += ', trop de caractères (maxi : '+ar[3]+')';
		if (ret.length>=1)
			ret = 'Erreur sur le champ "'+$(id).previousSibling.innerHtml+'" :\n'+ret.sub(1)+"\n";
		return ret;
	},
	creatTinyMceElement : function(){
			tinyMCE.init({
				theme : "advanced",
				mode : "textareas",
				language : "fr",
				skin : "o2k7",

				theme_advanced_buttons1 : "justifyleft,justifycenter,justifyright,justifyfull,|,bold,italic,underline,strikethrough,|,undo,redo,|,styleselect,formatselect,fontselect,fontsizeselect,|,link,unlink",
				theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,blockquote,|,insertdate,inserttime,|,forecolor,backcolor,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,advhr,|,cite,abbr,acronym,del,ins,attribs,|,visualchars",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resizing : true,
				plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
				content_css : "http://"+window.location.hostname+"/mail.css",
				// Drop lists for link/image/media/template dialogs
/*				template_external_list_url : "http://"+window.location.hostname+"/tiny_mce/aux/template_list.js",
				external_link_list_url : "http://"+window.location.hostname+"/tiny_mce/aux/link_list.js",
				external_image_list_url : "http://"+window.location.hostname+"/tiny_mce/aux/image_list.js",
				media_external_list_url : "http://"+window.location.hostname+"/tiny_mce/aux/media_list.js",
*/				// Replace values for the template plugin
				template_replace_values : { username : "Some User", staffid : "991234" }
			});
	}
});