﻿/*
mailFrom : Expediteur du mail
mailTo : Destinataire du mail
mailSubject : sujet du mail
templatePath : chemin de la template à utiliser
IncompleteFormMessage : message pour l'alerte si tous les champs obligatoires ne sont pas renseigne
ConfirmUrl : url à afficher en cas de succes
*/
function SendForm(mailFrom, mailTo, mailSubject, templatePath, IncompleteFormMessage, ConfirmUrl) {
    if (!validForm()) { alert(IncompleteFormMessage); return null; }
    var list = new Array();
    $(":radio[checked], :text, select, textarea, :hidden.get").each(function(arg) {
        list.push({ Key: $(this).attr("name"), Value: $(this).val() });
    });
    $(":checkbox").each(function(arg) {
        list.push({ Key: $(this).attr("name"), Value: $(this).is("[checked]") });
    });
    var participant = "";
    $(":text[name^=TxtParticipanteNome]").each(function() {
        var n = $(this).attr("name").replace("TxtParticipanteNome", "");
        if (n > 0) participant += "<hr />";
        participant += "Nome: <strong>" + $(this).val() + "</strong><br />";
        participant += "Cargo: <strong>" + $(":text[name=TxtParticipanteCargo" + n + "]").val() + "</strong><br />";
        participant += "Email: " + $(":text[name=TxtParticpanteEmail" + n + "]").val();
    });
    list.push({ Key: "Participantes", Value: participant });
    var GE_Informacao2 = "";
    $(":checkbox[name^=GE_Informacao2][checked]").each(function() {
        GE_Informacao2 += "<br />" + $("label[for=" + $(this).context.id + "]").text()
    });
    list.push({ Key: "GE_Informacao2", Value: GE_Informacao2 });
    var GE_Areas2 = "";
    $(":checkbox[name^=GE_Areas2][checked]").each(function() {
        GE_Areas2 += "<br />" + $("label[for=" + $(this).context.id + "]").text()
    });
    list.push({ Key: "GE_Areas2", Value: GE_Areas2 });
    list.push({ Key: "EmailSubject", Value: mailSubject });
    list.push({ Key: "EmailToAddress", Value: mailTo });
    list.push({ Key: "EmailFromAddress", Value: mailFrom });
    list.push({ Key: "EmailTemplatePath", Value: templatePath });
    var json = JSON.stringify(list);
    $.ajax({
        url: '/_vti_bin/AjaxMailer/AjaxMailer.svc/GetForm',
        data: json,
        type: 'POST',
        contentType: 'application/json;charset=utf-8',
        success: function(response) {
            if (ConfirmUrl)
                window.location = ConfirmUrl;
            else
                alert(response);
        },
        complete: function(msg) { if (msg.status != 200) alert(msg.status + " - " + msg.statusText); }
    });
}
function validForm() {
    var isOK = true;
    var cssValid = { 'border': '#A8111C solid 2px' };
    var cssInvalid = { 'border': '' };

    $(":text.needed").each(function() { if ($(this).val().length == 0) { isOK = false; $(this).css(cssValid); } else { $(this).css(cssInvalid); } });
    $("select.needed").each(function() { if ($(this)[0].selectedIndex == -1 || $(this).val() == -1) { isOK = false; $(this).css(cssValid); } else { $(this).css(cssInvalid); } });
    $(":radio.needed").each(function() { var nom = $(this).attr("name"); if ($(":radio[name=" + nom + "][checked]").length == 0) { isOK = false; $(this).css(cssValid); } else { $(this).css(cssInvalid); } });

    $(".ToolTip").remove();
    ex_id = 0;
    $(":text.needed.email").each(function() {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
        if (!filter.test($(this).val())) {
            isOK = false;
            $(this).css(cssValid);
            $('<div id="ToolTip' + ex_id++ + '" class="ToolTip"></div>').appendTo("body")
				.append('<label>ex: cursos@globalestrategias.pt</label>')
				.css('top', $(this).offset().top + "px").css('left', ($(this).offset().left + $(this).width() + 12) + "px")
				.hide()
				.fadeIn(2000)
				.wait(3000)
				.fadeOut(2000);
        }
        else {
            $(this).css(cssInvalid);
        }
    });
    return isOK;
}

$.fn.wait = function(time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $(self).dequeue();
        }, time);
    });
};



