﻿// Author:David Leghorn     16/7/07

// This script contains Ajax/HttpRequest error and timeout callback functions
// This script must be imported in the <head> section of any pages utilsing asp.net Ajax request e.g.
// <head>
//    <script type="text/javascript" src="../../Scripts/Ajax/RequestErrorHandling.js"></script> 
//  </head><body> .... etc .. 


//  UpdateRequestError and UpdateRequestTimedOut functions are utilised by Add, Update or Delete
//  related Pages/Http Requests

// ParseError function is a generic error handler called by all RequestError functions. ParseError 
// identifies common errors like 'Authentication failed' and presents more user friendly messages than
// microsoft ajax frameworks default messages


// GLOBAL VARIABLES

// variables hold details of last httprequest error. By storing error information relating to the last
// request in these global variables, other scripts on the page can access error info if required.

var RequestErrorMessage = "";
var RequestExceptionType;
var RequestErrorNumber;


     // -------------------- UPDATE REQUEST FAILED CALLBACK FUNCTION ---------------------------
     
     // context param defines the name of the entity we are attempting to update,delete or add,
     // e.g. context = 'Job Card'
     
    function UpdateRequestError(errorObj,context)    // OnError(arg)  
    {
        ParseError(errorObj, context);
    }
    
    
     // -------------------- SEARCH REQUEST FAILED CALLBACK FUNCTION ---------------------------
     
    function SearchRequestError(errorObj,context)    // OnError(arg)  
    {
         ParseError(errorObj, context);
        //var errMsg = "<b>Jobcard Search request failed:</b><br />" + errorObj.get_message() + " exception type : " + errorObj.get_exceptionType(); // + " number = " + errorObj.number;
        //alert("error = " + errMsg);
    }


    // -------------------- DATA REQUEST FAILED CALLBACK FUNCTION ---------------------------
     
    function DataRequestError(errorObj,context)    // OnError(arg)  
    {
         ParseError(errorObj, context);
    }
    
        
     function PopupDataRequestError(errorObj,context)
     {
         PopupDataRequestInProgress = false; 
         ParseError(errorObj, context); 
     }

 
    // ---------- GENERIC ERROR HANDLING/REPORTING FUNCTION -----------------
    
    // ParseError (1) stores error details in global variables (2) identifies generic ms ajax framework
    // errors e.g. 'Authentication failed' and presents alternative more friendly user messages in
    // the context of GoPlan, (3) This error handler could also attempt to fire off an Ajax HttpRequest
    // to log error details in database??
    
    function ParseError(errorObject, requestContext)
    {
        RequestErrorMessage = errorObject.get_message();
        RequestExceptionType = errorObject.get_exceptionType();
        
        var displayErrorMessage = "";
        
        // NOTE: FOR PRODUCTION APP, CAN CONSIDER indexOf('failed') TO RTEPRESENT AN AUTHENTICATION / LOGIN EXPIRED ERROR
        
        if(RequestErrorMessage.indexOf('Authentication failed') != -1)  // Authentication Cookie has Expired
        {
           // displayErrorMessage = requestContext + " Failed! - Your user Authentication Cookie (your login) has expired! Please Logout of system and Re-Login!";
           displayErrorMessage = "<b>Request Failed! </b><br />Your Login has expired! Please <a href='http://ifa-voice.com/Logout.aspx'>Logout</a> of system and Re-Login!";

        }
        else if(RequestErrorMessage.indexOf('failed') != -1)
        {
             displayErrorMessage = "<b>Request Failed:!</b><br /> Your Login has expired - please <a href='http://ifa-voice.com/Logout.aspx'>Logout</a> of system and Re-Login!";
        }
        else  // generic error message
        {
           // displayErrorMessage = "<b>Error Occurred!</b><br />Error Message = " + RequestErrorMessage + " <br />Exception Type = " +  RequestExceptionType;
           displayErrorMessage = "<b>Sorry, we could not complete your request.</b><br />This problem may have occurred because;<br /><br />* Your login has expired. Please <a href='https://ifa-voice.com/Login.aspx'>Re-Login</a>.<br /><br />* There is a problem with your internet connection e.g. very slow internet connection or loss of internet connection, or <br /><br />* We are temporarily experiencing a problem with our server.";
        }
       
        // else //IF ERROR NOT IDENTIFIED ATTEMPT AJAX WRITE TO DATABASE (or email??)
        // show error message details to user
        
        ShowHideAnimatedMsgBox("hidden","");
        ShowHideOkMsgBox("visible",displayErrorMessage );
    
    }
    
 
 
    // --------- UPDATE REQUEST TIMED OUT CALLBACK ----------------------------
    
    function UpdateRequestTimedOut(result)
    {
       ShowRequestTimedOutMessage();
       // debug/testing
       //alert(result);
    }
 
 
    // --------- SEARCH REQUEST TIMED OUT CALLBACK ----------------------------
    
    function SearchRequestTimedOut(result)
    {
        ShowRequestTimedOutMessage();
       // debug/testing
       //alert(result);
    }


    // --------- DATA REQUEST TIMED OUT CALLBACK ----------------------------
    
    function DataRequestTimedOut(result)
    {
        ShowRequestTimedOutMessage();
       //alert(result);
    }
    
    
    // ---------- POPUP DATA REQUEST TIMED OUT --------------------------

    function PopupRequestTimedOut(result,context)
    {
        ShowRequestTimedOutMessage();
        PopupDataRequestInProgress = false; 
    }
    
    // --------- GENERIC REQUEST TIMED OUT HANDLING / MESSAGE -------------------
    
    function ShowRequestTimedOutMessage()
    {
        var timedoutMsg = "<b>Operation Timed Out!</b><br />This problem is typically related to an internet connection problem e.g. very slow internet connection or loss of internet connection, or we are temporarily experiencing a problem with our server. If your internet connection is functioning as expected, please try again.";
        ShowHideAnimatedMsgBox("hidden","");
        ShowHideOkMsgBox("visible",timedoutMsg );
    }
 
 
       
