// ajax request handler
function ajax_request_handler(update,data) {
    var url = '/ajax.php';
    jQuery.ajax({
       type: 'POST',
       url: url,
       dataType: 'json',
       data: {'dataType':'json', 'update':update, 'data':data},
       success: ajax_response_handler
    });
}

// ajax response handler
function ajax_response_handler(response) {
    jQuery.each(response, function(response_key, response_val) {

        // replaceWith
        // Replace each element in the set of matched elements with the provided new content.
        // http://api.jquery.com/replaceWith/
        if (response_key == 'remote_replaceWith') {
            jQuery.each(response.remote_replaceWith, function(remote_replaceWith_key, remote_replaceWith_val) {
                if ($(remote_replaceWith_key).length) $(remote_replaceWith_key).replaceWith(remote_replaceWith_val);
            });
        }
    
        // empty
        // Remove all child nodes of the set of matched elements from the DOM.
        // http://api.jquery.com/empty/
        if (response_key == 'remote_empty') {
            jQuery.each(response.remote_empty, function(remote_empty_key, remote_empty_val) {
                if ($(remote_empty_val).length) $(remote_empty_val).empty();
            });
        }
    
        // remove
        // Remove the set of matched elements from the DOM.
        // http://api.jquery.com/remove/
        if (response_key == 'remote_remove') {
            jQuery.each(response.remote_remove, function(remote_remove_key, remote_remove_val) {
                if ($(remote_remove_val).length) $(remote_remove_val).remove();
            });
        }

        // prependTo
        // Insert every element in the set of matched elements to the beginning of the target.
        // http://api.jquery.com/prependTo/
        if (response_key == 'remote_prependTo') {
            jQuery.each(response.remote_prependTo, function(remote_prependTo_key, remote_prependTo_val) {
                if ($(remote_prependTo_key).length) $(remote_prependTo_val).prependTo(remote_prependTo_key);
            });
        }

        // appendTo
        // Insert every element in the set of matched elements to the end of the target.
        // http://api.jquery.com/appendTo/
        if (response_key == 'remote_appendTo') {
            jQuery.each(response.remote_appendTo, function(remote_appendTo_key, remote_appendTo_val) {
                if ($(remote_appendTo_key).length) $(remote_appendTo_val).appendTo(remote_appendTo_key);
            });
        }

        // before
        // Insert content, specified by the parameter, before each element in the set of matched elements.
        // http://api.jquery.com/before/
        if (response_key == 'remote_before') {
            jQuery.each(response.remote_before, function(remote_before_key, remote_before_val) {
                if ($(remote_before_key).length) $(remote_before_key).before(remote_before_val);
            });
        }

        // after
        // Insert content, specified by the parameter, after each element in the set of matched elements.
        // http://api.jquery.com/after/
        if (response_key == 'remote_after') {
            jQuery.each(response.remote_after, function(remote_after_key, remote_after_val) {
                if ($(remote_after_key).length) $(remote_after_key).after(remote_after_val);
            });
        }
    
        // callback
        if (response_key == 'remote_callback') {
            jQuery.each(response.remote_callback, function(remote_callback_key, remote_callback_val) {
                jQuery.globalEval(remote_callback_val);
            });
        }
        
    });
    
};

