/**
* Tommy Brière
* Productions Vic Pelletier
* Tous droits réservées
* 2008-05-14
* Script de génération de formulaire automatique
* NÉCESSITE 
* formhelper
* alert
* utils
**/
RessourcesTextes.add([["all.frm.type_invalide","Type de champ invalide"],["all.trop_caract","Vous avez dépassé la limite maximale de {1} caractères pour le champ {0}."],["all.liste_vide","Aucun élément"],["formulaire.pref_doublon","Vous ne pouvez pas créer deux formulaires avec le même préfixe..."],["formulaire.def_msg","Votre formulaire a bien été envoyé. Nous ferons suite à votre demande dans les meilleurs délais possibles."]]);

var FORM_GRANDEUR_CH=35;
var _formulaires=[];

function ifrSucces(prefixe){
_formulaires[prefixe].ifrSucces();}
function Formulaire(postUrl, postData, prefixe, apresPost, firstTabIndex){
var me= this;
var champs=[];
this.postData= postData;
this.postUrl= postUrl;
this.iframeMode= false;
this.insource=false;
this.iajouterApres=-1;
this.formHelper= null;
this.apresPost= apresPost;
this.urlApres= null;
this.msgApres= null;
if((prefixe)&&(prefixe!="")){
this.prefixe= prefixe;}else{
this.prefixe="w_fo_c";}
if(firstTabIndex){} else{
firstTabIndex= 1;}
if(_formulaires[this.prefixe]){
alert(_TR("formulaire.pref_doublon","You cannot create two forms with the same prefix..." ));} else{
_formulaires[this.prefixe]=this;}
this.init= function(){
me.generateIds();}

this.ajouterChamps= function(type, obligatoire, legende, msg_erreur, msg_aide, valeur, choix){
if(champsTypeMap[type]){
if(type==6){
this.iframeMode=true;}
var c= new champsTypeMap[type];
c.obligatoire= obligatoire;
c.legende=legende;
c.msg_erreur=msg_erreur;
c.msg_aide=msg_aide;
c.valeur= valeur;
if(choix){
c.choix= choix;}
this.ajouterCustomChamps(c);
return c;} else{
alert(_TR("all.frm.type_invalide","Field type invalid." ));}
return null;}

this.ajouterCustomChamps= function(c){
c.id=this.prefixe+champs.length;
champs[champs.length]=c;}

this.update= function(){
var html="";
for(var i= this.iajouterApres; i< champs.length; i++){
var c= champs[i];
html+= c.getHtml();}
var table= document.createElement("table");
table.innerHTML= html;
var form= $w(this.formId);
form.insertBefore(table, form.childNodes[1]);}

this.resetDynaChamps= function(){
for(var i= this.iajouterApres; i< champs.length; i++){
var c= champs[i];
if(c.clean){
c.clean();}}
var form= $w(this.formId);
if(form.childNodes[1].tagName=="TABLE"){
form.removeChild(form.childNodes[1]);}
champs.splice(this.iajouterApres, champs.length-this.iajouterApres);}

this.ecrire= function(){
document.write(me.getHtml());
me.initFormHelper();
$w(this.prefixe+"_btnSoumettre").onclick=me.soumettre;
this.iajouterApres=champs.length;
me.apresEcrire();
return false;}

this.initFormHelper= function(){
var data=[];
for(var i=0; i< champs.length; i++){
var c= champs[i];
data= data.concat(c.getFrmHelperData());}
me.formHelper= new FormulaireHelper(data, me.formId, me.postData, null, this.frmHelperAutoSubmit, true, firstTabIndex);
me.formHelper.formulaire= me;
if((me.postUrl!=null)&&(me.postUrl!="")){
me.formHelper.url= me.postUrl;}
if(me.iframeMode){
me.formHelper.addParams(this.postData);
me.formHelper.addData("SubmitFromIFrame","true" );
me.formHelper.addData("prefix", this.prefixe);}
me.formHelper.afterSuccessSave= me.afterSuccessSave;}

this.afterSuccessSave= function(){
if(me.apresPost){
me.apresPost();} else{
if((me.urlApres!=null)&&(me.urlApres!="") ){
if((me.msgApres!=null)&&(me.msgApres!="") ){
alert(me.msgApres);}
window.location.href=me.urlApres;} else{
var msg= me.msgApres;
if((msg==null)||(msg=="")){
msg= _TR("formulaire.def_msg","The form was sent. We will answer to your request as soon as possible." );}
var f= $w(me.formId);
f.innerHTML="<p>"+msg+"</p>";}}}

this.frmHelperAutoSubmit= function(){
me.soumettre();}

this.inserer= function(id){
if(!me.formHelper){
$w(id).innerHTML= me.getHtml();
me.initFormHelper();
$w(this.prefixe+"_btnSoumettre").onclick=me.soumettre;
this.iajouterApres=champs.length;
me.apresEcrire();}}

this.generateIds= function(){
Formulaire.g_counter++;
me.formId="f_id_"+ Formulaire.g_counter;}

this.getHtml= function(){
var html="";
if(this.iframeMode){
html+='<iframe name="'+me.formId+'_iframe" id="'+me.formId+'_iframe" src="javascript:;" class="formiframe"></iframe>';}
html+='<form id="'+me.formId+'" method="POST" enctype="multipart/form-data" target="'+me.formId+'_iframe" action="'+this.postUrl+'">';
html+='<table>';
for(var i=0; i< champs.length; i++){
var c= champs[i];
html+= c.getHtml();}
html+="</table>";
html+='<p><a class="form-button enregistrer" href="#" id="'+this.prefixe+'_btnSoumettre" >'+ _TR("all.soumettre","Submit" )+'</a></p>';
html+="</form>";
return html;}
this.apresEcrire= function(){
for(var i=0; i< champs.length; i++){
champs[i].apresEcrire();}}

this.soumettre= function(){
for(var i=0; i< champs.length; i++){
var c= champs[i];
if(!c.valider()){
return false;}}
if(me.iframeMode){
$w(me.formId).submit();} else{
me.formHelper.valider();}
return false;}

this.ifrSucces= function(){
me.afterSuccessSave();}
this.getChamps=function(i){
return champs[i];}
this.getNbChamps=function(){
return champs.length;}
this.clean= function(){
for(var i=0; i< champs.length; i++){
var c= champs[i];
if(c.clean){
c.clean();}}
_formulaires[this.prefixe]=null;}
this.init();}

function Champs(){
this.getHtml= function(){
var html="";
html+="<tr>";
html+='<td><label for="'+this.id+'">';
if(this.obligatoire){
html+="* ";}
html+= this.legende;
html+=' :</label>';
html+='</td><td>';
html+= this.getFHtml();
html+='</td>';
if(Formulaire.AIDE_INTERROGATION){
html+='<td><a class="interrogation" id="'+this.id+'_i" href="#">'+ _TR("all.interrogation" ,"?")+'</a></td>';}
html+="</tr>";
return html;}
this.getFrmHelperData= function(){
return[[this.id, this.msg_erreur, false, this.msg_aide]];}

this.valider= function(){
var valeur= $w(this.id).value;
var ok=((!this.obligatoire) ||(valeur !="" ));
if(!ok){
$w(this.id).focus();
alert(this.msg_erreur);}
return ok;}
this.apresEcrire= function(){}}

function ChampsTexte(){
Champs.call(this);
this.carmax= 200;
}

function ChampsTexteSimple(){
ChampsTexte.call(this);
this.getFHtml= function(){
var size;
if(this.carmax> FORM_GRANDEUR_CH){
size= FORM_GRANDEUR_CH;} else{
size= this.carmax;}
var type="text";
if(this.choix){
type="password";}
var html='<input id="'+this.id+'" name="'+this.id+'" maxlength="'+this.carmax+'" size="'+size+'" type="'+type+'" value="'+escapeHTML(this.valeur)+'" />';
return html;}}
function ChampsEntier(){
Champs.call(this);
this.carmax= 10;
this.getFHtml= function(){
var html='<input id="'+this.id+'" name="'+this.id+'" size="'+FORM_GRANDEUR_CH+'" maxlength="'+this.carmax+'" type="text" value="'+escapeHTML(this.valeur)+'" />';
return html;}}
function estEmailValide(valeur){
apos=valeur.indexOf("@");
dotpos=valeur.lastIndexOf(".");
lastpos=valeur.length-1;
if(apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2){
return false;} else{
return true;}}

function ChampsCourriel(){
ChampsTexte.call(this);
this.getFHtml= function(){
var html='<input id="'+this.id+'" name="'+this.id+'" size="'+FORM_GRANDEUR_CH+'" maxlength="'+this.carmax+'" type="text" value="'+escapeHTML(this.valeur)+'" />';
return html;}

this.valider= function(){
var valeur= $w(this.id).value;
var ok;
if(valeur !="" ){
ok= estEmailValide(valeur);
if(!ok){
$w(this.id).focus();
alert(_TR("all.courriel_invalide","«{0}» is not a valid email address. Please enter another one in the field {1}." , valeur, this.legende));}} else{
ok=(!this.obligatoire);
if(!ok){
$w(this.id).focus();
alert(this.msg_erreur);}}
return ok;}}

function ChampsTexteMultiligne(){
Champs.call(this);
this.carmax= 600;
this.getFHtml= function(){
var html='';
html+='<textarea id="'+this.id+'" name="'+this.id+'" rows="4" cols="'+Math.floor(FORM_GRANDEUR_CH*3/4)+'" onkeypress="return onKeyPressLimit(this, event, '+this.carmax+')" onkeyup="onKeyUpLimit(this, '+this.carmax+');" >'+escapeHTML(this.valeur)+'</textarea>';
return html;}

this.valider= function(){
var valeur= $w(this.id).value;
var ok;
if(valeur !="" ){
ok= valeur.length< this.carmax;
if(!ok){
$w(this.id).focus();
alert(_TR("all.trop_caract","You surpassed the maximum limit of {1} characters for the field {0}." , this.carmax, this.legende));}} else{
ok=(!this.obligatoire);
if(!ok){
$w(this.id).focus();
alert(this.msg_erreur);}}
return ok;}}

function ChampsCocher(){
Champs.call(this);
this.getFHtml= function(){
var checked="";
if(this.valeur &&(this.valeur !="0" )){
checked=' checked="checked" ';}
var html='<input id="'+this.id+'" name="'+this.id+'" type="checkbox" '+checked+' />';
return html;}

this.valider= function(){
var valeur= $w(this.id).checked;
var ok=((!this.obligatoire) || valeur);
if(!ok){
$w(this.id).focus();
alert(this.msg_erreur);}
return ok;}}

function ChampsChoix(){
Champs.call(this);
this.choix=[];
this.getFrmHelperData= function(){
var data=[];
for(var i=0; i< this.choix.length; i++){
data[i]=[this.id+"_"+i, this.msg_erreur, this.obligatoire, this.msg_aide];}
return data;}
this.getFHtml= function(){
var html='';
for(var i=0; i< this.choix.length; i++){
var c= this.choix[i];
if(c[0]==this.valeur){
sel=' checked="checked" ';} else{
var sel="";}
html+='<input id="'+this.id+'_'+i+'" name="'+this.id+'" '+sel+' type="radio" value="'+c[0]+'" />';
html+=' '+ c[1];}
return html;}

this.getValeur= function(){
for(var i=0; i< this.choix.length; i++){
var c= this.choix[i];
var o= $w(this.id+"_"+i);
if(o && o.checked){
return c[0];}}
return 0;}

this.valider= function(){
var valeur= this.getValeur();
var ok=((!this.obligatoire) ||(valeur!=0));
if(!ok){
$w(this.id+"_0").focus();
alert(this.msg_erreur);}
return ok;}}

function ChampsFichier(){
Champs.call(this);
this.getFHtml= function(){
var size= FORM_GRANDEUR_CH-12;
var html='<input id="'+this.id+'" size="'+size+'" name="'+this.id+'" type="file" />';
return html;}}

function ChampsListe(){
Champs.call(this);
this.valeur=[];
this.enr= objectRepository.add(this);
this.todel="";
this.getFHtml= function(){
var html='<input id="'+this.id+'" name="'+this.id+'" type="hidden" />';
html+='<div id="'+this.id+'_ls">';
html+='<table>';
html+=this.getListeHtml();
html+='</table>';
html+='</div>';
html+='<div id="'+this.id+'_afrm">'+this.getAddFrmHtml()+'</div>';
return html;}

this.valider= function(){
var ok=((!this.obligatoire) ||(this.valeur.length>0));
if(!ok){
$w(this.id).focus();
alert(this.msg_erreur);}
return ok;}

this.getListeHtml=function(){
var html="";
if(this.valeur.length>0){
for(var i=0; i< this.valeur.length; i++){
html+="<tr>";
html+="<td>"+this.valeur[i].nom+"</td>";
html+='<td><a href="#" onclick="objectRepository.get('+this.enr+').retirer('+i+'); return false;">'+_TR("all.retirer","Remove" )+"</a></td>";
html+="</tr>";}} else{
html+= _TR("all.liste_vide","No element" );}
return html;}

this.getAddFrmHtml=function(){
var html="";
html+='<p><input id="'+this.id+'_af" type="text" value="" onkeypress="objectRepository.get('+this.enr+').checkenter(event);"> <a href="#" onclick="objectRepository.get('+this.enr+').ajouter();return false;">'+_TR("all.ajouter","Add" )+'</a></p>';
return html;}

this.checkenter=function(e){
if(enterPressed(e)){
this.ajouter();}}

this.retirer= function(i){
this.todel+="del:"+ this.valeur[i].id+";" 
this.valeur.splice(i, 1);
this.update();}

this.ajouter=function(){
var i=$w(this.id+"_af");
if(i.value !="" ){
this.ajouterEl(i.value);
i.value="";}}
this.ajouterEl=function(el){
if(!(el instanceof Array)){
el={id:0, nom:el};}
this.valeur[this.valeur.length]=el;
this.update();}

this.update=function(){
$w(this.id+'_ls').innerHTML="<table>"+this.getListeHtml()+"</table>";
var ec= this.todel;
for(var i=0; i< this.valeur.length; i++){
var v= this.valeur[i];
if(v.id==0){
ec+="add:"+ encodeURIComponent(v.nom)+";" }}
$w(this.id).value=ec;}

this.clean=function(){
objectRepository.remove(this.enr);
this.enr=0;
this.valeur=null;}}

function ChampsVide(){
Champs.call(this);
this.getFHtml= function(){
var html='<input id="'+this.id+'" name="'+this.id+'" type="hidden" />';
html+='<div id="'+this.id+'_c"></div>';
return html;}
this.valider= function(){
return true;}}

function ChampsChoixCb(){
Champs.call(this);
this.choix=[];
this.getFHtml= function(){
var html='<select id="'+this.id+'" name="'+this.id+'" >';
html+='<option></option>';
for(var i=0; i< this.choix.length; i++){
var c= this.choix[i];
if(c.id){
var id= c.id;
var nom= c.nom;} else{
var id= c[0];
var nom= c[1]}
if(id==this.valeur){
sel=' selected="selected" ';} else{
var sel="";}
html+='<option value="'+id+'" '+sel+' >'+ nom+'</option>';}
html+="</select>"
return html;}

this.getValeur= function(){
var sel= $w(this.id);
return sel.value;}

this.valider= function(){
var valeur= this.getValeur();
var ok=((!this.obligatoire) ||(valeur> 0));
if(!ok){
$w(this.id).focus();
alert(this.msg_erreur);}
return ok;}}
Formulaire.g_counter= 0;
Formulaire.AIDE_INTERROGATION= true;
var champsTypeMap=[
null,
ChampsTexteSimple,
ChampsCourriel,
ChampsTexteMultiligne,
ChampsCocher,
ChampsChoix,
ChampsFichier,
ChampsVide,
ChampsChoixCb,
null,
null,
ChampsEntier];