var sendEmailCommunique = function (name, firstname, firm, role, phone_number, email_address){
    this.name  = name || undefined;
    this.firstname = firstname || undefined;
    this.firm =  firm || '';
    this.role = role || undefined;
    this.phone_number = phone_number || '';
    this.email_address = email_address || undefined;
    this.validate();
}

sendEmailCommunique.prototype = {
    validate:function (){
        this.collectRens = new Array();
        for (i in this){
            if (typeof this[i] != 'function' && typeof this[i] != 'object'){
                if (this[i] == undefined){
                    this.emptyField(i);
                    return;
                }else{
                    if (this[i]!=''){
                        this.collectRens[this[i]]= i;
                    }
                }
            }
        }
        if (this.checkEmail()){
            this.httpQueryBuild();
            this.send();
        }else{
            this.msgShow('errorEmailSyntax');
        }
    },
    
    emptyField:function (champ){
        switch (champ) {
            case 'name':
                this.msgShow('errorName');
            break;
            case 'firstname':
                this.msgShow('errorFirstname');
            break;
            case 'role':
                this.msgShow('errorRole');
            break;
            case 'email_address':
                this.msgShow('errorEmail_address');
            break;     
        }
    },
    
    send:function (){
        var referer = this;
        $.ajax({
            type: "POST",
            data : referer.dataToPost,
            url: "home/subscribe",
            error:function (){
                referer.msgShow('error500');
            },
            success: function(msg){
                referer.msgShow(msg);
            }
        });
    },
    
    httpQueryBuild:function(){
        this.dataToPost = new String();
        for (i in this.collectRens){
            this.dataToPost+= this.collectRens[i]+"="+i+"&";
        }
    },
    
    msgShow:function (msg){
        if (msgText[msg]){
            alert(msgText[msg]);
        }else{
            alert(msg)
        }
    },
    
    checkEmail: function (){
        re = /^([a-zA-Z0-9]+(([\.\-\_]?[a-zA-Z0-9]+)+)?)\@(([a-zA-Z0-9]+[\.\-\_])+[a-zA-Z]{2,4})$/gi;
        result = this.email_address.match(re) ;
        if (result == null) {
            return false;
        } else {
            return true;
        }
    }
}

var sendUnsubscribe = function (email_address){
    this.email_address = email_address || '';
    re = /^([a-zA-Z0-9]+(([\.\-\_]?[a-zA-Z0-9]+)+)?)\@(([a-zA-Z0-9]+[\.\-\_])+[a-zA-Z]{2,4})$/gi;
    result = this.email_address.match(re) ;
    if (result == null) {
        alert(msgText['errorEmailSyntax'])
    }else{
        this.send();
    }
}

sendUnsubscribe.prototype={
    send:function(){
        var referer = this;
        $.ajax({
            type: "POST",
            data : "unsubscribe="+referer.email_address,
            url: "home/unsubscribe",
            error: function(ss){
                alert(msgText['error500']);
            },
            success: function(msg){
                referer.msgShow(msg);
            }
        });
    },
    msgShow:function (msg){
        if (msgText[msg]){
            alert(msgText[msg]);
        }else{
            alert(msg)
        }
    }

}