/**
*
* @author John Hummel
*
* tbd-mail.js is the javascript file of the mail component used at TBD
* 
* It contains the JSON configuration object and js function to handled the configured events
* 
*/

(function ($, window, document, undefined) {

    debug = false;
    if (typeof console != "undefined") { debug = true; }

    var tbdAjaxHost = "php/tbd-ajax-functions.php";
    var tbdMailInterface = "mail/tbd-mail.php";

    var form = [
    {
        "name": "tbdFrmContactUs",
        "exclude": ["send"],
        "validation": {
            "company": "required",
            "name": "required",
            "email": "required",
            "phone": "required",
            "website": "required",
            "message": "required"
        },
        "triggers": [
            {
                "object": "send",
                "event": "click"
            }
        ],
        "mail": {
            "to": "support@the-best-designs.com",
            "from": "charlotte@hjarl.dk",
            "subject": "TBD Contact Request",
            "body": "The following data submitted from TBD"
        },
        "successURL": "",
        "errorURL": ""
    }
];

    $(function () {
        $.each(form, function (frmIdx, frmObj) {
            if (debug) { console.log(frmObj.name); }
            var frmObjSel = "#" + frmObj.name;
            if ($(frmObjSel).length > 0) {
                if (debug) { console.log(frmObjSel + " found."); }
                $.each(frmObj.validation, function (validationObj, validationRules) {
                    if (debug) { console.log(validationObj + ": " + validationRules); }
                    var currClass = "";
                    if (debug) { console.log(typeof $("#" + validationObj).attr("class")); }
                    if (typeof $("#" + validationObj).attr("class") != "undefined") { currClass = $("#" + validationObj).attr("class") };
                    $("#" + validationObj).attr("class", "validate[" + validationRules + "] " + currClass);
                });
                $.each(frmObj.triggers, function (trigIdx, trigObj) {
                    var frmTriggerObj = $("#" + trigObj.object);
                    $(frmTriggerObj).bind(trigObj.event, function (e) {
                        e.preventDefault();
                        $(frmObjSel).validationEngine();
                        if ($(frmObjSel).validationEngine('validate')) {
                            var frmData = "{";
                            $(frmObjSel + " li > *:input").each(function () {
                                if (debug) { console.log($(this).attr("name") + ": " + $(this).val()); }
                                if ($.inArray($(this).attr("name"), frmObj.exclude) == -1) {
                                    frmData += '"' + $(this).attr("name") + '"' + ": " + '"' + $(this).val() + '", ';
                                }
                            });
                            if (frmData.length > 1) {
                                frmData += '"tbdMailTo": "' + frmObj.mail.to + '", ';
                                frmData += '"tbdMailFrom": "' + frmObj.mail.from + '", ';
                                frmData += '"tbdMailSubject": "' + frmObj.mail.subject + '", ';
                                frmData += '"tbdMailBody": "' + frmObj.mail.body + '", ';
                                $.post(tbdAjaxHost, { "action": "startSession" }, function (sessionId) {
                                    if (debug) { console.log(sessionId); }
                                    frmData += '"tbdMailSessionId": "' + sessionId + '", ';
                                    $.post(tbdAjaxHost, { "action": "getToken", "sessionId": sessionId }, function (token) {
                                        if (debug) { console.log(token); }
                                        frmData += '"tbdMailToken": "' + token + '"';
                                        frmData += "}";
                                        $.post(tbdMailInterface, { "frmData": frmData }, function (mailSendResponse) {
                                            if (debug) { console.log(mailSendResponse); }
                                            if (mailSendResponse == "success") {
                                                $(frmObjSel).hide("explode", { pieces: 72 }, 3000, function() {
                                                   $("#contact p").empty()
                                                                  .append('<p>Thank you for your inquiry. Someone will contact a you shortly.</p>');
                                                });
                                            }
                                        });
                                    });
                                });
                            }
                            if (debug) { console.log(frmData); }
                        }
                    });
                });
            }
        });
    });

})(jQuery, window, document);

