﻿


function ShowAjaxMsg(title, msg, cont) {
        $('#ajaxwaiting').remove();
        if (!cont)
            $('#' + SendMailInfo.replaceId).html("<div id='message'></div>");
        else {
            // show a validation error message
            if ($('#message').length == 0)
                $('#' + SendMailInfo.replaceId).prepend("<div id='message' class='validation'></div>");
                // reset form
                
                
             }
        $('#message').html(title).hide();
        $('#message').append(msg).fadeIn('slow');
}


var options = {
    /*
    target: '#' + SendMailInfo.replaceId,
    */
    url: SendMailInfo.sendMailRoot + 'sendMail.ashx',
    success: function(msg) {
        var result = msg.split(" - ");
        var status, detail;
        status = result[0];
        if (result.length > 1)
           detail = result[1];
        
        switch (status) {
            case "OK":
                ShowAjaxMsg(SendMailInfo.okMsg[0],SendMailInfo.okMsg[1], false);
                break;
            case "FormSetup":
                document.location.href = SendMailInfo.sendMailRoot + "FormSetup.aspx";                    
                break;
            case "Validation failed":
                ShowAjaxMsg(SendMailInfo.validationMsg[0],detail, true);
                break;               
            default:
                ShowAjaxMsg(SendMailInfo.errMsg[0],SendMailInfo.failMsg[1], false);
                break;
        }
    }, 
    error: function(msg) {
           ShowAjaxMsg(SendMailInfo.failMsg[0],SendMailInfo.failMsg[1], false);
    }
};

/* creates a layer on top of the form when ajax is called */

function CreateWaitingLayer() {
    var el = $('#' + SendMailInfo.replaceId);
    if (el.length != 0) {
        el.prepend('<div id="ajaxwaiting">&nbsp;</div>');
        var target = $('#ajaxwaiting');
        var offset = el.offset();
        target.css('position', 'absolute');
        target.css("width", el.width()).css("height", el.height());
        target.css('left', offset.left);
        target.css('top', offset.top);
        //target.css("width", el.width()).css("height", el.height()).css("left", 0).css("top", 0)
        target.show().css('z-index', 1);
        target.prepend('<div style="height: 40%; width: 1px" /><img src="' + SendMailInfo.sendMailRoot + 'images/ajax-loader.gif" />');
    }
}

$(document).ready(function() {
    var setup = {
        submitHandler: function(form) {
            CreateWaitingLayer();
            $(form).ajaxSubmit(options);
        },
        errorPlacement: function(error, element) {
            var offset = element.offset();
            error.insertBefore(element)
            error.addClass('errormessage');
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
            error.css('top', offset.top);

        }
    };
    // preload waiting image
    var img = new Image();
    img.src = SendMailInfo.sendMailRoot + 'images/ajax-loader.gif';
    
    $('#' + SendMailInfo.formId).validate(setup);
});



