﻿/*
Page Author				: Stefan Kruger
Page Created Date		: 16-Jul-2010
Page Modification Log	:
Person:							Date:		Description:
------------------------------------------------------------------------------------------------------
Stefan Kruger					16-Jul-2010	Created file

To Do:
------
*/

var loadingDisplay = "<div class=\"default_text ajax_message\">Loading content...</div>";
var loadFailure = "<div class=\"default_text ajax_message\">Load failed.</div>";

function loadFunction(data,onComplete,loadIn,postLoad,extendedData,historyObject){
    try {
        //Execute onComplete function if needed
        if (onComplete) {
            //alert('Complete')
            onComplete(data,loadIn,postLoad,extendedData,historyObject);
        }
    }
    catch (ex){
        handleException(ex,"loadFunction","Main");
    }
}

function errorFunction(xhr,error,ex,loadIn){
    try {
        //Reset loading display
        if (loadIn) {
            document.getElementById(loadIn).innerHTML = loadFailure;
        }
        //Alert status response
        alert("AJAX load error occured. Status : " + xhr.status + "\nResponse : " + xhr.responseText);
    }
    catch (ex){
        handleException(ex,"errorFunction","Main");
    }
}

function ajaxRequest(page,data,preLoad,postLoad,onComplete,loadIn,extendedData,historyObject,postFlag) {
    //Preload
    try {
        eval(preLoad);
    }
    catch (ex){
        handleException(ex,"loadContent","Preload");
    }
    //Set loading display
    try {
        if (loadIn) {
            document.getElementById(loadIn).innerHTML = loadingDisplay;
        }
    }
    catch (ex){
        handleException(ex,"loadContent","Loading display");
    }
    //Flag any ajax request
    var sendData = "ajaxrequest=true";
    if (data) {
        sendData += "&" + data;
    }
    var requestType = "get";
    if (postFlag) {
        requestType = "post";
    }
    //alert(page + "?" + sendData);
    //Load ajax
    $.ajax({
        type: requestType,
        timeout: 30000,
        url: page,
        data: sendData,
        success: function(receivedData){
            loadFunction(receivedData,onComplete,loadIn,postLoad,extendedData,historyObject);
        },
        error : function(xhr,error,ex){
            errorFunction(xhr,error,ex,loadIn);
        }
    });
}
