var whitespace = " \t\n\r";


function DataForm() {
}

DataForm.prototype.Get = function(controlId) {
    return this[controlId];
}

DataForm.prototype.Add = function(controlId, name, obligatoire, type) {
    this[controlId] = new Object();
    this[controlId].name = name;
    this[controlId].obligatoire = obligatoire;
    this[controlId].type = type;
}

function EnvoiFormulaire(id, bValid, type, lang, data) {
    var form = new Formulaire(id, bValid, type!="mail", lang, data);
    form.Envoi();
}

function EstVide(s) {
    var i;
    if ((s == null) || (s.length == 0)) return true;
    for(var i = 0; i < s.length; i++) {
	var c = s.charAt(i);
	if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function CheckEmail(v) {
    if (document.all) {
       var regExp = /^[^ \t]+@[^ \t]+\.[A-Za-z]+[ \t]*(<[^<>]*>)?$/;
       return regExp.exec(v) != null;
    }	
    return v.indexOf("@") != -1;
}

function Formulaire(id, bValid, bmail, lang, data) {
    this.id = id;
    this.name = "formulairepages"+id;
    this.bmail = bmail;
    this.data = data; // data[0].type,data[0].name,data[0].obligatoire
    this.bValid = bValid;
    this.locale = new Object();
    this.locale.fr = new Object();
    this.locale.en = new Object();
    this.locale.es = new Object();
    this.locale.lang = lang;
    this.locale.fr["erreur_envoi"] = "Le formulaire ne peut-être envoyé !\nVeuillez contacter le webmaster du site.";
    this.locale.es["erreur_envoi"] = "No se ha podido enviar el formulario.\nPor favor, contacte el webmaster del sitio.";
    this.locale.en["erreur_envoi"] = "The form can't be sent.\nPlease, contact the webmaster of the site.";
    this.locale.fr["alerte1"] = "Adresse email invalide !";
    this.locale.es["alerte1"] = "¡ Dirección de correo electrónico no válida !";
    this.locale.en["alerte1"] = "Wrong registration !";
    this.locale.fr["alerte2"] = "Attention, tous les champs obligatoires du formulaire doivent être saisies !";
    this.locale.es["alerte2"] = "¡ Cuidado, debe rellenar todos los campos obligatorios del formulario !";
    this.locale.en["alerte2"] = "Denotes required fields !";

    this.locale.fr["mesg_preview"] = "Voici le contenu du mail qui sera envoyé à la validation du formulaire.<br> L'envoi ne fonctionne que sur le site diffusé.";
    this.locale.fr["oui"] = "oui";
    this.locale.fr["Nom"] = "Nom";
    this.locale.fr["Valeur"] = "Valeur";
    this.locale.fr["Titre_Formulaire"] = "Retour de formulaire";	
    this.locale.fr["non"] = "non";
    this.locale.Message = function(id) { 
	var lang = this.lang;
	if (typeof(this[lang]) != "object") {
	    lang = "fr";
	}
	if (typeof(this[lang][id]) != "string") {
	    lang = "fr";
	}
	return this[lang][id];
    }
}

Formulaire.prototype.Validation = function() {
    var forms = document.forms[this.name];
    if (!this.bValid) {
	alert(this.locale.Message("erreur_envoi"));
	return false;
    }
    if (forms.tagName == "FORM") {
      var form = forms;
      forms = new Array();
      forms[0] = form;	
    }
    for(var i=0; i < forms.length ; i++) { 
	if (forms[i].length == 0) continue;
	var form = forms[i];
	var control = form[0];
	var controlId = control.name;
	var data = this.data[controlId];
	if (data == null) continue;
        if (data.type == "email") {
	   var value = control.value;
	   if (!EstVide(value) && !CheckEmail(value)) {
	      alert(this.locale.Message("alerte1"));
	      control.focus();
	      return false;
	   }
        }
	if (data.obligatoire == "true") {
	    if (data.type == "radio") {
		var trouve = false;
	        var name = controlId;
		for(var j=0; j < form[name].length ; j++) {
		   if (form[name][j].checked) trouve = true;
		}
		if (!trouve) {
		   alert(this.locale.Message("alerte2"));
		   control.focus();
		   return false;
		}
	    }
	    else {
		var value = control.value;
		if (EstVide(value)) {
		    alert(this.locale.Message("alerte2"));
		    control.focus();
		    return false;
		}
	    }
	}
    }
    return true;
}

Formulaire.prototype.Envoi = function() {
    if (!this.Validation()) return false;
    var output = "";
    var forms = document.forms[this.name];
    var form_envoi = document.forms["Realform"+this.id];
    if (!this.bmail) {
	var PCouleurTexte = "black";
	var PCouleurPage = "#FFFFFF";
	var PFont = "Verdana";
	var PSize = "11px";
        var sStyle = "color:"+PCouleurTexte+";font-family:"+PFont+";font-size:"+PSize+";";
	output += "<html><head><title>"+this.locale.Message("Titre_Formulaire")+"</title></head><body ";
	output += "color='"+PCouleurTexte+"' bgcolor='"+PCouleurPage+"'>";
	if (!isDiffusion) {
   	   output += "<p><EM>"+this.locale.Message("mesg_preview")+"</EM></p>";
        }
	output += "<H2 style='color:navy'>"+this.locale.Message("Titre_Formulaire")+"</H2>";
	output += "<table border=0 cellspacing=0 cellpadding=0><tr><td bgcolor='#C0C0C0'>";
	output += "<table border=0 cellspacing=2 cellpadding=4 style='"+sStyle+"'>";
	output += "<td style='background-color:#C0C0C0;'>"+this.locale.Message("Nom")+"</td>";
	output += "<td style='background-color:#C0C0C0;'>"+this.locale.Message("Valeur")+"</td>";
	output += "</tr>";
    }
    if (forms.tagName == "FORM") {
	  forms = new Array();
	  var j = 0;
	  for (var i=0; i<document.forms.length ; i++)
		{
			
			if (document.forms[i].name == this.name)
			{
				forms[j] = document.forms[i];	
				j++;
			}
		}
    }
    for(var i=0; i < forms.length ; i++) {
	var value = "";
	if (forms[i].length == 0) continue;
	var form = forms[i];
	var control = form[0];
	var controlId = control.name;
	var data = this.data[controlId];
	if (data == null) continue;
	if (data.type == "email") {
	    value = control.value;
	    if (!this.bmail) {
		//form_envoi["From"].value = value;
	    }
	}
	else if (data.type == "checkbox") {
	    value = this.locale.Message(forms[i][0].checked ? "oui" : "non");
	}
	else if (data.type == "liste") {
	    var options = control.options;
	    for(var j=0; j < options.length ; j++) {
		if (options[j].selected) {
		    if (value != "")
			value += ", ";
		    value += options[j].value;
		}
	    }
	}
	else if (data.type == "radio") {
		var name = control.name;
		for(var j=0; j < form[name].length ; j++) {
		    if (form[name][j].checked) {
			value = form[name][j].value;
		    }
		}
	}
	else if (data.type == "bouton") {
	    continue;
	}
	else {
	    value = control.value;
	}
	if (!this.bmail) {
	    // envoi à un script php
	    output += "<tr><td valign='top' nowrap style='background-color:lightgrey;'>"+data.name+"&nbsp;:&nbsp;</td>";
	    output += "<td valign='top' width='400' style='background-color:lightgrey;'>"+value+"</td></tr>";
	}
	else {
	    // envoi par le client de messagerie
	    output += data.name + ": " + value + "\n";
	}
    }
    if (!this.bmail) {
	output += "</table></body></html>";
	form_envoi["Message"].value = output;
    }
    else {
	form_envoi.action += "&body="+escape(output)+"&vide=";
    }
    if (!this.bmail) {
	if (!isDiffusion) {
 	   context.createFile(context.processor.outputPath+"\\form.html",output);
  	   window.open("form.html","form","width=550,height=500")
 	   return false;
        }
    }
    form_envoi.submit();
}


