// (c) 2011 mpmcash.com, all rights reserved

/* --------------------------------------------------------------------------------------------- */

function submitForm(form, url) {
    $.ajax({
        url: url,
        type: "POST",
        data: $(":input", form),
        dataType: "json",
        success: function(result) {
            if (result.form)
                $(form).html(result.form);
            else if (result.popupHtml)
                $("#popup_content").html(result.popupHtml);
            
            if (typeof submitFormDone == "function")
                submitFormDone(result, form);
        },
        statusCode: ajaxErrorHandlers
    });
}

/* --------------------------------------------------------------------------------------------- */
	
function adTrafficGraph(elem, adId) {
    $.ajax({
        url: "/mpmcash/graphs/adTraffic/" + adId.toString(),
        dataType: "json",
        success: function(html) {
            $("#" + elem).html(html);
        },
        statusCode: ajaxErrorHandlers
    });
}

/* --------------------------------------------------------------------------------------------- */

var refreshHeader = "";
var refreshUrl = null;
var refreshData = null;

function ajaxRefresh(data) {
    
    if (!refreshUrl)
        alert("error: no refresh url");
    
    $("#content").html('<h1>' + refreshHeader +
            '</h1><br/><img src="/mpmcash/st/images/ajax-loader.gif"/>');
        
    if (typeof data != "undefined")
        refreshData = data;
    
    $.ajax({
        url: refreshUrl,
        data: refreshData,
        dataType: "json",
        success: function(html) {
            $("#content").html(html);
            if (typeof ajaxRefreshDone == "function")
                ajaxRefreshDone();
        },
        statusCode: ajaxErrorHandlers
    });
    
}

/* --------------------------------------------------------------------------------------------- */

var ajaxErrorHandlers = {
    400: function() {
        alert("your webbrowser sent an invalid request");
    },
    404: function() {
        alert("page not found");
    },
    461: function() {
        alert("timeout. login again");
    },
    500: function() {
        alert("an error occured");
    }
};

/* --------------------------------------------------------------------------------------------- */

