﻿
var loadingDisplay = "Please wait while your request is processing...";
var loadFailure = "Load failed.";



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.reposonseText);
    }
    catch (ex) {
        handleException(ex, "errorFunction", "Main");
    }
}

function ajaxRequest(page, data, preLoad, postLoad, onComplete, loadIn, extendedData, historyObject, asyncdata, ajaxmethod, stimeout) {
    //Preload

    var PageCulture = $('#PageCulture').val();

    //alert(PageCulture);

    if (PageCulture == 'sv-SE') {
        loadingDisplay = 'Var god vänta medan din förfrågan behandlar...'
    }
    else if (PageCulture == 'de-DE') {
        loadingDisplay = 'Bitte warten, Ihre Anfrage wird bearbeitet...'
    }
    else if (PageCulture == 'es-ES') {
        loadingDisplay = 'Por favor, espere mientras su solicitud se está procesando...'
    }
    else if (PageCulture == 'nl-NL') {
        loadingDisplay = 'Een ogenblik geduld aub uw verzoek wordt verwerkt...'
    }
    else if (PageCulture == 'da-DK') {
        loadingDisplay = 'Vent venligst, mens din anmodning behandler ...'
    }

    try {
        eval(preLoad);
    }
    catch (ex) {
        handleException(ex, "loadContent", "Preload");
    }
    //Set loading display
    try {
        if (loadIn) {
            var loadingHtml = '';

            loadingHtml = '<table><tr><td width="33px"><img alt="loading" src="/images/loading.gif" /></td><td>' + loadingDisplay + '</td></tr></table>'

            $('#msgLoading').show();
            $('#msgLoading').html(loadingHtml);
        }
    }
    catch (ex) {
        handleException(ex, "loadContent", "Loading display");
    }
    //Flag any ajax request
    var sendData = "ajaxrequest=true";
    if (data) {
       sendData += "&" + data;
    }
    //sendData = data;
    //alert(page + "?" + sendData);
    //Load ajax
   var isSync = true;
   if (asyncdata != null) {
       isSync = asyncdata;
   }

   if (ajaxmethod == null || ajaxmethod == '') {
       ajaxmethod = 'post';
   }

   var AjaxTimeOut = 100000;

   if (stimeout != null && stimeout != '') {
       AjaxTimeOut = stimeout;
   }

   //alert(AjaxTimeOut);
   jQuery.ajax({
       type: ajaxmethod,
       timeout: AjaxTimeOut,
       url: page,
       data: sendData,
       cache: false,
       async: isSync,
       success: function (receivedData) {
           loadFunction(receivedData, onComplete, loadIn, postLoad, extendedData, historyObject);
       },
       error: function (xhr, error, ex) {
           //errorFunction(xhr, error, ex, loadIn); 
           //CleanupXHR(xhr);
       },
       complete: function (xhr, status) {
           //CleanupXHR(xhr);
           if ($('#msgLoading').length > 0) {
               $('#msgLoading').hide();
           }
       }
   });
}

function AjaxReturnRequest(page, senddata, method) {

    var ReturnVal = ''

       jQuery.ajax({
           type: method,
           timeout: 100000,
           url: page,
           data: senddata,
           cache: false,
           async: false,
           success: function (receivedData) {
               ReturnVal = receivedData
           },
           error: function (xhr, error, ex) {
               errorFunction(xhr, error, ex, null); 
           },
           complete: function (xhr, status) {
               if ($('#msgLoading').length > 0) {
                   $('#msgLoading').hide();
               }
           }
       });
    return ReturnVal
}

function CallWebServices(WebServicesURL, WebMethod, Parameters) {

    var WebPost = WebServicesURL + "/" + WebMethod;
    alert(WebPost);
    alert(Parameters)
    $.ajax({
        beforeSend: function(req) {
            req.setRequestHeader("Connection", "close");
        },
        type: "POST",
        url: WebPost,
        data: Parameters,
        contentType: "application/json; charset=utf-8",
        cache: true,
        dataType: "json",
        success: function(msg) {
            alert(msg.d);
        },
        error: function(e) {
            alert("An error occur while calling web services.");
        }
    });

}

function CleanupXHR(xhr) {
    if (xhr != null) {
        xhr.abort();
    }
}



