﻿
$.ajaxSetup({
    global: false,
    type: "POST",
    cache: false,
    beforeSend: preprocess,
    complete: postprocess,
    error: errorprocess
});

$.ajaxSetup({
    global: false,
    type: "GET",
    cache:false,
    beforeSend: preprocess,
    complete: postprocess,
    error: errorprocess
});

function errorprocess(XMLHttpRequest, textStatus, errorThrown) {
    $(".error").html("Error: " + textStatus + " " + errorThrown);
    $(".error").css("display", "block");
}

function preprocess(data) {
    $(".loading").css("display", "inline");
}

function postprocess() {
    $(".loading").css("display", "none");
}

function processJson(data) {
    
    // clear old messages
    $(".success,.warning,.error,.info").css("display", "none");
    
    if (data.Success) {
        $(".success").html(data.Success);
        $(".success").css("display", "block");
    }
    if (data.Info) {
        $(".info").html(data.Info);
        $(".info").css("display", "block");
    }
    if (data.Warning) {
        $(".warning").html(data.Warning);
        $(".warning").css("display", "block");
    }
    if (data.Error) {
        $(".error").html(data.Error);
        $(".error").css("display", "block");
    }

    if (window.postproc) {
        postproc(data);
    }
}

$(document).ready(function() {
    setupValidate();

    // add loading icons after buttons
    $("input.button").after(" <span class=\"loading\" style=\"display: none;\"><img alt=\"Loading... icon\" src=\"/images/loading-small.gif\"/></span>");

    // add loading icons after any load class
    $(".load").after(" <span class=\"loading\" style=\"display: none;\"><img alt=\"Loading... icon\" src=\"/images/loading-small.gif\"/></span>");
    
    //$("input.but").after(" <span class=\"loading\" style=\"display: none;\"><img alt=\"Loading... icon\" src=\"/images/loading-small.gif\"/></span>");
});

function setupValidate() {
    
    // use "each" to handle more than one form
    $(".jsonForm").each(function() {
        $(this).validate({
            debug: true,
            ignore: ".ignore,[id$=___Config]",
            errorClass: "invalid",
            success: "valid",
            wrapper: "div",
            invalidHandler: function() {
                //alert('hi657');
            },
            submitHandler: function(form) {
                $(form).ajaxSubmit(
                {
                    dataType: 'json',
                    success: processJson,
                    resetForm: false
                });
            }
        })
    });

    $("#normalform").each(function() {
        $(this).validate({
            debug: true,
            ignore: ".ignore",
            errorClass: "invalid",
            success: "valid",
            wrapper: "div",
            invalidHandler: function() {
                alert('hi');
            },
            submitHandler: function(form) {
                /*$(form).ajaxSubmit(
                {
                dataType: 'json',
                success: processJson,
                resetForm: false
                });*/
                form.submit();
            }
        });
    });
}