/**
 * Une Classe qui permet de créer des formulaires javaScript prototype And jQuery complient.
 * @package Formulaire
 * @author Steven-Mickaël COUR 01/07/2009
 * @copyright  Copyright (c) 2008-2009 Eolia
 * @version 1.0.0
 */

function Formulaire_Prototype(Name, Css, RenderTo, Lang)
{
		this.Css = Css;
		this.Lang = Lang;
		this.Name = Name;
		this.RenderTo = RenderTo;
		this.Inputs = new Array();
		this.OnError = false;
		this.Req = 0;
		this.Max = 0;
		this.MessageError = new Array();
		this.MessageError['NoEmpty'] = new Array();
		this.MessageError['NoEmpty']['FR'] = 'Doit être renseigné.';
		this.MessageError['Email'] = new Array();
		this.MessageError['Email']['FR'] = 'Doit être une adresse email valide.';
		this.MessageError['UNIX'] = new Array();
		this.MessageError['UNIX']['FR'] = 'ne doit pas contenir d\'espace ni de caraxtère accentué.';
		this.MessageError['Integer'] = new Array();
		this.MessageError['Integer']['FR'] = 'ne doit contenir uniquement des chiffres.';
		this.MessageError['Date'] = new Array();
		this.MessageError['Date']['FR'] = 'Doit être une date au format jj/mm/aaaa.';
		this.MessageError['DateSQL'] = new Array();
		this.MessageError['DateSQL']['FR'] = 'Doit être une date au format aaaa/mm/jj.';
		this.MessageError['Antispam'] = new Array();
		this.MessageError['Antispam']['FR'] = 'Le code de sécurité n\'est pas valide.';
		
		$(RenderTo).insert({bottom: "\n"+''+"\n"+'<form id="'+Name+'" target="Iframe'+Name+'" method="post" action="/includes/FormContact/SendMail.php" enctype="multipart/form-data" ><input type="hidden" name="RenderTo" id="RenderTo" value="'+this.RenderTo+'" /><input type="hidden" name="language" id="language" value="'+this.Lang+'" /><input type="hidden" name="BaseCss" id="BaseCss" value="'+this.Css+'" /><input type="hidden" name="ListFile" id="ListFile" value="" /><br /><input type="hidden" name="FormInfo" id="FormInfo" value="" /><input type="hidden" name="renderTo" id="renderTo" value="'+RenderTo+'" /></form>'}); 
		this.AddAntiSpam = function( To, Id, Legend, Size, Validator )
		{
			$(To+'_Table').insert({bottom: "\n"+'<tr class="'+this.Css+'TrInput" ><td class="'+this.Css+'TdInputCaption" >'+Legend+' : <span id="'+Id+'_Error" ></span></td></tr><tr><td id="'+Id+'_td_Antispam" class="'+this.Css+'TdInputAntispam" ><img id="'+Id+'_Antispam" src="/includes/FormContact/antispam.php?name='+Id+'&strlen='+Size+'" alt="anti-flood" style="display: inline;" align="absmiddle" />&nbsp;&nbsp;<input id="'+Id+'" name="'+Id+'" class="'+this.Css+'InputBoxAntiSpam" type="text" style="width: '+( (Size * 17) +22)+'px" /></td></tr>'});
						
			tmp = new Array();
			tmp.id = Id;
			tmp.Validator = Validator;
			tmp.Size = Size;
			tmp.Legend = Legend;
			tmp.Error = "";
			this.Inputs.push(tmp);

		}//fin this.AddFieldSet = function(Id, Width, Float, Close)
	
		this.AddFieldSet = function(To, Id, Legend, Width, Float, Close)
		{
			$(To).insert({bottom: "\n"+'<div id="'+Id+'" class="'+this.Css+'FieldSet" style="float: '+Float+'; width:'+Width+';" >'+( Legend != "" ? '<div id="'+Id+'_Legend" class="'+this.Css+'FieldSet_Legend" >'+Legend+'</div>' : '')+'<table class="'+this.Css+'TableInput" cellspacing="0" cellpadding="0" width="100%" ><tbody id="'+Id+'_Table" ></tbody</table></div>'});
			if( Close == true )
			{
				$(To).insert({bottom: "\n"+'<br clear="all" style="height: 1px;" />'});
			}//fin this.AddFieldSet = function(Id, Legend, Width, Float, Close)
		}//fin this.AddFieldSet = function(Id, Width, Float, Close)
		
		this.AddInputSelect = function( To, Id, Legend, Default, Datas )
		{
			$(To+'_Table').insert({bottom: "\n"+'<tr class="'+this.Css+'TrInput" ><td class="'+this.Css+'TdInputCaption" >'+Legend+' : <span id="'+Id+'_Error" ></span></td></tr><tr><td class="'+this.Css+'TdInputField" ><select id="'+Id+'" name="'+Id+'" class="'+this.Css+'TdInputSelect" ></select></td></tr>'});
			
			data = Datas.split('|');
			for( i=0 ; i<data.length ; i++ )
			{
				$(Id).insert({bottom: "\n"+'<option value="'+data[i]+'" '+(data[i] == Default ? 'selected="selected"' : '')+' >'+data[i]+'</option>'});
			}//fin for( Val in data )
			
			tmp = new Array();
			tmp.id = Id;
			tmp.Validator = "";
			tmp.Legend = Legend;
			tmp.Error = "";
			this.Inputs.push(tmp);
			$('ListFile').value = $('ListFile').value+' '+Id;
			$('FormInfo').value += Id+"="+tmp.Legend+";";
		}//fin this.AddInputText = function( To, Id, Legend, Width )
		
		this.AddInputText = function( To, Id, Legend, Suffixe, Default, Width, Validator )
		{
			$(To+'_Table').insert({bottom: "\n"+'<tr class="'+this.Css+'TrInput" ><td class="'+this.Css+'TdInputCaption" >'+Legend+' : <span id="'+Id+'_Error" ></span></td></tr><tr><td class="'+this.Css+'TdInputField" ><input id="'+Id+'" name="'+Id+'" class="'+this.Css+'InputBox" type="text" value="'+Default+'" style="width: '+Width+'" /><span class="'+this.Css+'InputSuffixe" >'+Suffixe+'</span></td></tr>'});
			
			tmp = new Array();
			tmp.id = Id;
			tmp.Validator = Validator;
			tmp.Legend = Legend;
			tmp.Error = "";
			this.Inputs.push(tmp);
			$('ListFile').value = $('ListFile').value+' '+Id;
			$('FormInfo').value += Id+"="+tmp.Legend+";";
		}//fin this.AddInputText = function( To, Id, Legend, Width )
		
		this.AddInputTextarea = function( To, Id, Legend, Suffixe, Default, Row, Col, Validator )
		{
			$(To+'_Table').insert({bottom: "\n"+'<tr class="'+this.Css+'TrInput" ><td class="'+this.Css+'TdInputCaption" >'+Legend+' : <span id="'+Id+'_Error" ></span></td></tr><tr><td class="'+this.Css+'TdInputField" ><textarea id="'+Id+'" name="'+Id+'" class="'+this.Css+'InputBox" cols="'+Col+'" rows="'+Row+'" >'+Default+'</textarea><span class="'+this.Css+'InputSuffixe" >'+Suffixe+'</span></td></tr>'});
			
			tmp = new Array();
			tmp.id = Id;
			tmp.Validator = Validator;
			tmp.Legend = Legend;
			tmp.Error = "";
			this.Inputs.push(tmp);
			$('ListFile').value = $('ListFile').value+' '+Id;
			$('FormInfo').value += Id+"="+tmp.Legend+";";
		}//fin this.AddInputTextarea = function( To, Id, Legend, Suffixe, Default, Row, Col, Validator )
	
		this.AddFile = function( To, Id, Legend, MaxLength, Accept )
		{
			$(To+'_Table').insert({bottom: "\n"+'<tr class="'+this.Css+'TrInput" ><td class="'+this.Css+'TdInputCaption" >'+Legend+' : <span id="'+Id+'_Error" ></span></td></tr><tr id="Tr_'+Id+'" ><td class="'+this.Css+'TdInputField" ><input type="hidden" name="'+Id+'_Accept" value="'+Accept+'"><input type="hidden" name="MAX FILE SIZE" value="'+( MaxLength * 1024 )+'"><input id="'+Id+'" name="'+Id+'" class="'+this.Css+'InputBox" type="file" accept="'+Accept+'" /></td></tr>'});
			
			tmp = new Array();
			tmp.id = Id;
			tmp.Validator = "";
			tmp.Error = "";
			this.Inputs.push(tmp);
			$('ListFile').value = $('ListFile').value+' F_'+Id;
		}//fin this.AddInputText = function( To, Id, Legend, Width )
	
		this.AddSubmit = function( Id, Text )
		{
			$(this.Name).insert({bottom: "\n"+'<div class="'+this.Css+'ButtonContent" ><button type="button" id="'+Id+'" name="'+Id+'" class="'+this.Css+'Button" >'+Text+'</button></div>'});
			Event.observe(Id, 'mouseover', function (){ $(Id).className = this.Css+'Button Over'; }.bindAsEventListener(this) );
			Event.observe(Id, 'mouseout',  function (){ $(Id).className = this.Css+'Button'; }.bindAsEventListener(this) );
			Event.observe(Id, 'click',     function (){ this.CheckDatas() }.bindAsEventListener(this) );
		}//fin this.AddSubmit = function( Id, Text )
		
		this.CheckDatas = function()
		{	
			this.OnError = false;
			this.Inputs.each( function(index){ this.Validate(index) }.bindAsEventListener(this));
			if( !this.OnError )
			{
				$(this.Name).submit();
			}
		}//fin this.CheckDatas = function()
		
		this.Validate = function(Index)
		{
			t=0;
			var ErrorText = "";
			$w(Index['Validator']).each(function(ValidatorName){ 
				switch(ValidatorName)
				{
					case 'NoEmpty':
						if( $(Index.id).value == "" )
						{
							this.OnError = true;
							ErrorText = this.MessageError['NoEmpty'][this.Lang];
						}//fin if( $(Index['id']).value == "" )
						break;
					case 'UNIX':
						var reg = /^[a-zA-Z0-9_#]*$/;
						if( reg.exec($(Index['id']).value) == null )
						{
							this.OnError = true;
							ErrorText = this.MessageError['UNIX'][this.Lang];
						}//fin if( $(Index['id']).value == "" )
								break;
					case 'Email':
						var reg = /^([^<>\n@;]+@[^<>\n@;]+\.[^<>\n@;]+)?$/;
						if( reg.exec($(Index['id']).value) == null )
						{
							this.OnError = true;
							ErrorText = this.MessageError['Email'][this.Lang];
						}//fin if( $(Index['id']).value == "" )
						break;
					case 'Integer':
						var reg = /^[0-9]*$/;
						if( reg.exec($(Index['id']).value) == null )
						{
							this.OnError = true;
							ErrorText = this.MessageError['Integer'][this.Lang];
						}//fin if( $(Index['id']).value == "" )
						break;
					case 'DateSQL':
						var reg = /^([0-9]{4}.[0-9]{2}.[0-9]{2})?$/;
						if( reg.exec($(Index['id']).value) == null )
						{
							this.OnError = true;
							ErrorText = this.MessageError['DataSQL'][this.Lang];
						}//fin if( $(Index['id']).value == "" )
						break;
					case 'Date':
						var reg = /^([0-9]{2}.[0-9]{2}.[0-9]{4})?$/;
						if( reg.exec($(Index['id']).value) == null )
						{
							this.OnError = true;
							$(Index.id+'_Error').innerHTML = this.MessageError['Date'][this.Lang];
							ErrorText = this.Css+'InputBoxError';
						}//fin if( $(Index['id']).value == "" )
						break;
					case 'Antispam':
						this.Req = Math.floor((new Date()).getTime() / 1000);
						new Ajax.Request('/includes/FormContact/antispamCtrl.php',{
							method: "post",
							parameters: 'source='+$(Index.id+'_Antispam').src+'&id='+Index.id+'&Code='+$(Index.id).value,
							onSuccess: function(transport) { 	
								t = 1;
								if( transport.responseText != "")
								{
									this.OnError = true;
									ErrorText = this.MessageError['Antispam'][this.Lang];
									$(Index.id+'_Antispam').remove();
									$(Index.id+'_td_Antispam').insert({top: transport.responseText })
									$(Index.id).value = "";
								}
							}.bindAsEventListener(this)
						 });
						break;
						
					default:
						break;
				}
			}.bindAsEventListener(this));
			
			
			if( this.Req != 0 )
			{
				
				while( ( Math.floor((new Date()).getTime() / 1000)) - this.Req < 5 ){}
				this.Req = 0;
				alert(t);
			}
			
			if( ErrorText != "" && this.OnError )
			{
				$(Index.id+'_Error').innerHTML = ErrorText;
				$(Index.id).className = this.Css+'InputBoxError';
			}
			else
			{
				$(Index.id+'_Error').innerHTML = '';
				$(Index.id).className = this.Css+'InputBox';
			}
			
		}//fin this.Validate = function(Index)
}//fin Formulaire.Formulaire_Prototype = function(){
