﻿// RcsWeb Global JavaScript library
// Copyright (c) 2010 RightWeb Consulting Services

// ---------------------------------------------------------------------------------
/* STRING MANIPULATION */
// ---------------------------------------------------------------------------------


// FUNCTION: rcs_String_StartsWith()
// Returns True if string starts with substr, false otherwise   
function rcs_String_StartsWith(str, substr)
{
    return (str.match("^"+substr)==substr);
}

// FUNCTION: rcs_String_Count()
// Returns the number of times substr appears in string
function rcs_String_Count(string, substr)
{
    var _c = 0;
    for (var i=0;i<string.length;i++)
    {
        if (substr == string.substr(i,substr.length))
        _c++;
    }
    return(_c);
}

function rcs_Wait(milliseconds)
{
var date = new Date();
var curDate = null;
do {curDate = new Date();}
while(curDate-date<milliseconds);
}

// ---------------------------------------------------------------------------------
/* USER DIALOGS */
// ---------------------------------------------------------------------------------

// FUNCTION: rcs_ProcessingDialog_Show()
// Displays the processing dialog
function rcs_ProcessingDialog_Show()
{
    var dialog = eo_GetObject("ProcessingDialog");
    dialog.show(true);
    document.getElementById('rcs_ProcessingDialog_LoadingImage').src = '/RcsWeb/Resources/Images/Processing.gif';
   
}

// ---------------------------------------------------------------------------------
/* SELECT ALL CHECKBOX TOGGLE */
/*
    - Required Parameters -
    rcs_SelectionToggle_SelectAllControl    The id of the select all checkbox
                                            control.
                                            
    rcs_SelectionToggle_SelectionControl    The control used to store information
                                            about the current selection. Must be
                                            ASP.Net control to be able to access
                                            during a PostBack.
                                           
    rcs_SelectionToggle_ControlPrefix       The prefix of the id of the controls
                                            used to represent the list item
                                            selection.    
*/
// ---------------------------------------------------------------------------------

// FUNCTION: rcs_SelectionToggle_ToggleSelectAll()
// Description: Selects all List Item checkboxes on a page
// Returns: Nothing
function rcs_SelectionToggle_ToggleSelectAll()
{
    document.getElementById(rcs_SelectionToggle_SelectionControl).value = '';
    for (var i=0; i < document.forms[0].elements.length; i++)
    {
        var element = document.forms[0].elements[i];
        if (element.type == 'checkbox')
        {
            if (rcs_String_StartsWith(element.id, rcs_SelectionToggle_ControlPrefix))
            {
                if (document.getElementById(rcs_SelectionToggle_SelectAllControl).checked)
                {
                    element.checked = true;
                }
                else
                {
                    element.checked = false;
                }
                rcs_SelectionToggle_SelectListItem(element.id);
            }
        }
    }
}

// FUNCTION: rcs_SelectionToggle_SelectListItem(elementId)
// Parameters:
//     elementId    The id of the element that has just been selected.
// Returns: Nothing
function rcs_SelectionToggle_SelectListItem(elementId)
{
    var assetId = elementId.substr(rcs_SelectionToggle_ControlPrefix.length);
    if (document.getElementById(elementId).checked == true)
    {
        document.getElementById(rcs_SelectionToggle_SelectionControl).value = document.getElementById(rcs_SelectionToggle_SelectionControl).value + ':' + assetId + ':';
    }
    else
    {
        document.getElementById(rcs_SelectionToggle_SelectionControl).value = document.getElementById(rcs_SelectionToggle_SelectionControl).value.replace(':' + assetId + ':','');
    }
}


function rcs_SelectionDialog_Delete(name, secondaryText)
{
    /* Check parameters or use global */
    var useName, useSecondaryText;
    // Name
    if (typeof(name)=='string' || (name instanceof String))
    {
        useName = name;
    }
    if ((useName == null) || (useName == ''))
    {
        useName = rcs_SelectionDialog_Name;
    }
    // Secondary Text
    if (typeof(secondaryText)=='string' || (secondaryText instanceof String))
    {
        useSecondaryText = secondaryText;
    }
    if ((useSecondaryText == null) || (useSecondaryText == ''))
    {
        useSecondaryText = rcs_SelectionDialog_SecondaryText;
    }
    
    //Launches confirmation dialog
    eo_MsgBox(
        'PrimaryMsgBox',		//ID of the MsgBox control
        'Delete ' + useName + ' Confirmation',		//Message box title
        'You are about to permanently delete this ' + useName.toLowerCase() + '.<br /><br />' + rcs_SelectionDialog_SecondaryText,	//Message text
        null,			//Icon
        [
            {
                Text: ' OK ',		//OK button
                ClientSideHandler: 'rcs_SelectionDialog_DeleteConfirmed'	//Client side event handler
            },
			
            {
                Text: ' Cancel ',	//Cancel button
            }
        ]);
}

function rcs_String_ReplaceNullOrEmpty(value, replacementValue)
{
    var returnValue;
    if (typeof(value)=='string' || (value instanceof String))
    {
        returnValue = value;
    }
    if ((returnValue == null) || (returnValue == ''))
    {
        returnValue = replacementValue;
    }
    return returnValue;
}


function rcs_SelectionDialog_DeleteMultiple(name, namePlural, secondaryText)
{
    /* Check parameters or use global */
    var useName = rcs_String_ReplaceNullOrEmpty(name, rcs_SelectionDialog_Name);
    var useNamePlural = rcs_String_ReplaceNullOrEmpty(namePlural, rcs_SelectionDialog_NamePlural);
    var useSecondaryText = rcs_String_ReplaceNullOrEmpty(secondaryText, rcs_SelectionDialog_SecondaryText);

    // Prepare MessageBox
    if (document.getElementById(rcs_SelectionToggle_SelectionControl).value.length == 0)
    {
    eo_MsgBox(
        "PrimaryMsgBox",		//ID of the MsgBox control
        "Selection Not Made",		//Message box title
        "You must select at least one " + useName.toLowerCase() + " to be deleted before continuing.",	//Message text
        null,			//Icon
        [
	        {
		        Text: " OK ",		//OK button
	        }

        ]);
    }
    else
    {
        var itemCount = rcs_String_Count(document.getElementById(rcs_SelectionToggle_SelectionControl).value,'::') + 1;
        var itemCountMessage = itemCount;
        if (itemCount > 1) { itemCountMessage = itemCountMessage + ' ' + useNamePlural.toLowerCase() } else { itemCountMessage = itemCountMessage + ' ' + useName.toLowerCase() }
        eo_MsgBox(
            "PrimaryMsgBox",		//ID of the MsgBox control
            "Delete " + useName + " Confirmation",		//Message box title
            "You are about to delete " + itemCountMessage + ".<br /><br />" + useSecondaryText,	//Message text
            null,			//Icon
            [
	            {
		            Text: " OK ",		//OK button
		            ClientSideHandler: "rcs_SelectionDialog_DeleteConfirmed"	//Client side event handler
	            },
    			
	            {
		            Text: " Cancel ",	//Cancel button
	            }
            ]);
    }
}

function rcs_SelectionDialog_DeleteConfirmed(msgBox, buttonIndex)
{
    rcs_ProcessingDialog_Show();
    __doPostBack('rcs_SelectionDialog_Delete','');
}


function rcs_HelpSystem_ShowHelp(helpMode, topic)
{
    var helpPage = rcs_RcsWeb_RootPath + '/System/Help/Default.aspx';
    var parameters = 0;
    var parameterString = '';
    if (helpMode != null)
    {
        if (parameters == 0)
        {
            parameterString = parameterString + '?';
        }
        else
        {
            parameterString = parameterString + '&';
        }
        parameterString = parameterString + 'Mode=' + helpMode;
        parameters = parameters + 1;
    }
    if (topic != null)
    {
        if (parameters == 0)
        {
            parameterString = parameterString + '?';
        }
        else
        {
            parameterString = parameterString + '&';
        }
        parameterString = parameterString + 'Topic=' + topic;
        parameters = parameters + 1;
    }
    helpPage = helpPage + parameterString;
    window.open( helpPage
                ,'rcs_HelpSystem'
                ,'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=750,height=550'
                );           
}