﻿/**********************************************************
*
*   Dictionary object definition.
*   
*   Pages where used : Reserve.aspx (price.js)
*
**********************************************************/

// ====
// ==== The KeyValue pair object
function DicoElement(key, element) {
    this.Key = key;
    this.Element = element;
}

// ====
// ==== The Dictionnary itself 
function Dictionnary() {

    this.Data = new Array();

    // ===== Add Method
    this.AddOrUpdate = function(key, elmt) {

        var dicoElmt = new DicoElement(key, elmt);

        for (i in this.Data) {
        if (this.Data[i].Key == key) {
        // Update
        this.Data[i].Element = elmt;
        return;
        }
        }

        // If here, elmt was not found -> add it
        this.Data.push(dicoElmt);
    };

    // ==== Remove method
    this.Remove = function(key) {

        var dataTemp = new Array();

        for (i in this.Data) {
        if (this.Data[i].Key != key) {
        dataTemp.push(this.Data[i]);
        }
        }

        this.Data = dataTemp;
    };

    this.GetValues = function() {
        var values = new Array();

        for (i in this.Data)
        values.push(this.Data[i].Element);

        return values;
    };

    this.Get = function(key) {
        for (i in this.Data) {
        if (this.Data[i].Key == key) {
        return this.Data[i].Element;
        }
        }

        return null;
    };

    this.ContainKey = function(key) {

        var test = this.Get(key);

        return test != null;
    };
}

/*****
*       Dictionnary End
*********************************************************/

/*********************************************************
*   Block UI Until releaseUI is called
**********************************************************/
function wait() {

    if (ENABLE_DISPLAYING_PROGRESS_BAR_FOR_AJAX_CALL) {
        $.blockUI
        ({
            message: '<br/><h1>' + GeneralStrings_PleaseWait + '</h1><img src="/Content/Images/ajax-loader.gif" /><br/><br/>',
            overlayCSS: { opacity: 0 }
        });
    }
}

// Release an UI previously blocked
function stopWait() 
{
    try 
    {
        if (ENABLE_DISPLAYING_PROGRESS_BAR_FOR_AJAX_CALL) 
        {
            $.unblockUI();
        }
    }catch(e){}
}

/**********************************************************
* Block the UI and show an error message with error look and feel
*
* errorMessage :    The main message to display
* errorDetails :    optional, some details on error
* errorTitle :      optional, a specific title for the error (a default one will be display if not supply)
* redirectOkUrl :   optional, if supply the user will be redirect to the given url when wlick on Ok
*
**********************************************************/
function showError(errorMessage, errorDetails, errorTitle, redirectonOkUrl) {
    
    $("#errorMessage", "#errorDiv").html(errorMessage);
    
    // Set an url on Ok button if requested
    if (redirectonOkUrl != null) {
        $("#okButton", "#errorDiv").attr("href", redirectonOkUrl);
        $("#okButton", "#errorDiv").click(
            function() {
                $.unblockUI();                
            });
    }
    else {
        $("#okButton", "#errorDiv").click(
            function() {
                $.unblockUI();
                location.reload(true);
            });
    }

    // Set error details if requested
    if (errorDetails != null) {
        $("#errorDetail", "#errorDiv").show();
        $("#errorDetailsText", "#errorDiv").html(errorDetails);
    } else {
        $("#errorDetail", "#errorDiv").hide();
        $("#errorDetailsText", "#errorDiv").html("");
    }

    // Set error title if requested
    if (errorTitle != null)
        $("#errorTitle", "#errorDiv").html(errorTitle);
    
    // Show modal error
    $.blockUI({ message: $("#errorDiv") });
}

/**********************************************************
* Block the UI and show a message with message look and feel
*
* message :    The main message to display
* errorTitle :      optional, a specific title for the error (a default one will be display if not supply)
*
**********************************************************/
function showMessage(message, errorTitle) {    

    $("#message", "#messageDiv").html(message);

    // Set error title if requested
    if (errorTitle != null)
        $("#messageTitle", "#messageDiv").html(errorTitle);
    
    // Show modal error
    $.blockUI({
        message: $("#messageDiv"),
        overlayCSS: { opacity: 0.4 }
    });    
}

/**********************************************************
* Enum StatusViewModel utilisé par l'object JsonResultViewModel
* pour les retours ajax
*
**********************************************************/

var JsonStatus = { SUCCESS: 0, FAILURE: 1, WARNING: 2 }

function setBottomImage(className) {
    $("#bottom_img_container").removeClass();
    $("#bottom_img_container").addClass(className);
}


// startwth function
String.prototype.startsWith = function (str) {
    return (this.match("^" + str) == str);
}

//endwith function
String.prototype.endsWith = function (str)
{ return (this.match(str + "$") == str) }

// trim function
String.prototype.trim = function () {
    return
    (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))
}
