﻿function SetFocusField() 
{
    setFocusToField = true;
    FieldToFocusId = tbPasswordId;
}

function AccountActivationKeyPress(e) 
{
    //e is event object passed from function invocation        
    if (e && e.which) 
    {
        //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; //character code is contained in NN4's which property
    }
    else 
    {
        e = event;
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }
 
    if (characterCode == 13) 
    { 
        if (accountActivationValidateButtonId) 
        {
            var accountActivationValidateButton = document.getElementById(accountActivationValidateButtonId);
            if (accountActivationValidateButton)
                accountActivationValidateButton.click();  
        }

        return false;
    }
    else
        return true;
}

function LostPasswordKeyPress(e) 
{
    // e is event object passed from function invocation        
    /*if (e && e.which) 
    {
        // if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; // character code is contained in NN4's which property
    }
    else 
    {
        e = event;
        characterCode = e.keyCode; // character code is contained in IE's keyCode property
    }


    if (characterCode == 13) 
        return true;
    else
        return true;*/
}

// AO, à déplacer, utilisé par overlay d'ajout de service
function initScrollBar(blocScrollId, blocScrollConteneurId, barreScrollId, btScrollId, minWidthValueRequiredForScrollbar)
{	        	            
    //ScrollBar standalone
    var _bloc_scroll = document.getElementById(blocScrollId);
    var _barre_scroll = document.getElementById(barreScrollId);        	
    var _bloc_scroll_conteneur = document.getElementById(blocScrollConteneurId);
	                    	
    if(_bloc_scroll && _barre_scroll)
        if (_bloc_scroll.clientWidth>minWidthValueRequiredForScrollbar)
            processInitScrollBar(blocScrollId, blocScrollConteneurId, barreScrollId, btScrollId);
	
    // AO : masquage du scrollbar horizontal si il n'est pas nécessaire
    if (_bloc_scroll.clientWidth<=_bloc_scroll_conteneur.clientWidth)
        _barre_scroll.style.display = "none";            	 
    else
        _barre_scroll.style.display = "block";
}

// AO, à déplacer, utilisé par overlay d'ajout de service
function processInitScrollBar(blocScrollId, blocScrollConteneurId, barreScrollId, btScrollId)
{
    var el = document.getElementById(blocScrollId);
    
    if (el)
    {
	    var arraychildren = getArrayChildren(el);
	    
	    // AO, 17/06/2009, sécurité
	    if (!arraychildren[0])
	        return;
	    
	    var quotient = arraychildren.length;
	    var widthChildren = eval(arraychildren[0].offsetWidth + getStyleValue(arraychildren[0],'margin-left') + getStyleValue(arraychildren[0],'margin-right'));
	    var el_width = eval(eval(quotient)*eval(widthChildren));
	    el.style.width = el_width+'px';
	    			    
	    if(arraychildren[0].offsetWidth >0)
		    createScrollBar(blocScrollId, blocScrollConteneurId, barreScrollId, btScrollId);
    }
}      

// Fonction qui masque le contenu des RADWindow, but : empêcher les animations flash de passer au dessus des popins
function HideAllWidgetsContent() 
{
    if (document.getElementById(iFrameDependantId)) 
        var iFrameWidgContentWindow = document.getElementById(iFrameDependantId).contentWindow;
}

// Updates "TableOnglets" vertical position only for Firefox when a postback is done
function UpdateTabsTopFireFox() 
{
    if (browserIsFirefox) 
    {
        var tableOnglet = document.getElementById(tableOngletId);

        if (tableOnglet)
            tableOnglet.style.top = "7px";
    }
}

// Updates "TableOnglets" vertical position only for Internet Explorer when a postback is done
function UpdateTabsTopIE() 
{
    if (navigator.appName == "Microsoft Internet Explorer") {
        var tableOnglet = document.getElementById(tableOngletId);

        if (tableOnglet)
            tableOnglet.style.top = "20px";
    }
}

function PopinIsDisplayed()
{
    var retVal = false;
    var obj;
    
    if (typeof(popinTemplateId)!="undefined")
        obj=this;
    else if (typeof(parent.popinTemplateId)!="undefined")
        obj=parent;    
    
    if (obj.document.getElementById(obj.popinTemplateId).className=="popin" || obj.document.getElementById(obj.popinTemplateSpecialId).className=="popin" || obj.document.getElementById(obj.popinIFrameId).className=="popin")
        retVal=true;
        
    return retVal;
}

function AddWidget(IdWidgetModel) 
{    
    var iFrameWidgContentWindow = document.getElementById(iFrameDependantId).contentWindow;
    iFrameWidgContentWindow.widgetType = IdWidgetModel;
    
    if (IdWidgetModel != "" && IdWidgetModel != undefined) 
        iFrameWidgContentWindow.RadWindowClientCreate(IdWidgetModel);
            
    IncreaseNbWidgetsCurrentTab();
                    
    ClosePopinTemplateSpecial();
}

function AddNewWidgets(arg)
{
    if (arg)
    {
        var iFrameWidgContentWindow = document.getElementById(iFrameDependantId).contentWindow;
        if (iFrameWidgContentWindow)
        {
            var nbWidgetAdded =  eval(iFrameWidgContentWindow.document.getElementById(iFrameWidgContentWindow.hdNbWidgetsInCurrentTabId).value);

            for(var i=0; i < arg.length; i++)
            {
               if (iFrameWidgContentWindow.GuidEmpty != arg[i])
                   nbWidgetAdded++;
            }
            
            iFrameWidgContentWindow.DisplayNewWidget(arg);
            iFrameWidgContentWindow.document.getElementById(iFrameWidgContentWindow.hdNbWidgetsInCurrentTabId).value =nbWidgetAdded;            
        }
     }
           
    SetSophusTag_JS('WidgetPlaceCheck');
    ClosePopinTemplateSpecial(true);    
}

function DecreaseNbWidgetsCurrentTab()
{
    var iFrameWidgContentWindow = document.getElementById(iFrameDependantId).contentWindow;
    var nbWidgetsInCurrentTab = eval(iFrameWidgContentWindow.document.getElementById(iFrameWidgContentWindow.hdNbWidgetsInCurrentTabId).value);            
    nbWidgetsInCurrentTab--;            
    iFrameWidgContentWindow.document.getElementById(iFrameWidgContentWindow.hdNbWidgetsInCurrentTabId).value=nbWidgetsInCurrentTab;
}

function IncreaseNbWidgetsCurrentTab()
{
    var iFrameWidgContentWindow = document.getElementById(iFrameDependantId).contentWindow;
    var nbWidgetsInCurrentTab = eval(iFrameWidgContentWindow.document.getElementById(iFrameWidgContentWindow.hdNbWidgetsInCurrentTabId).value);            
    nbWidgetsInCurrentTab++;            
    iFrameWidgContentWindow.document.getElementById(iFrameWidgContentWindow.hdNbWidgetsInCurrentTabId).value=nbWidgetsInCurrentTab;  
}

function ShowPopinTemplateWithDelay(delay) 
{
    setTimeout("ShowPopinTemplate()", delay)
}

function ShowPopinTemplateSpecialWithDelay(delay) 
{
    setTimeout("ShowPopinTemplateSpecial()", delay)
}

// Fonction appellée lorsque le chargement de la page est terminé
function PageReady_Default() 
{
    WindowResize();
    
    if (loadDefaultTab == true) 
    {
        loadDefaultTab = false;

        // Chargement de l'onglet par défaut
        LoadDefaultTab2(_tabId, _tabName, _tabIndex, "", _rnd);
    }
}

function FocusField() 
{
    var FieldToFocus = document.getElementById(FieldToFocusId);
    if (FieldToFocus)
        FieldToFocus.focus();

    setFocusToField = false;
    FieldToFocusId = '';
}

function LogFlashHeader() 
{
    var el = document.getElementById("content_header_flash");

    if (el && el.setLoged) {
        el.setLoged();
        clearInterval(intervLogFlashHeader);
    }
}

function LoadDefaultTab(tabId, tabName, tabIndex, rnd) 
{
    _tabId = tabId;
    _tabName = tabName;
    _tabIndex = tabIndex;
    _rnd = rnd;
    defaultTabLoaded = false;
    loadDefaultTab = true;
}

function SetActiveTabStyle(tabId) 
{
    var desktop = document.getElementById(tableOngletId);
    
    for (var i = 0; i < desktop.getElementsByTagName("TD").length; i++) 
    {
        var td = desktop.getElementsByTagName("TD")[i];
        if (td != null && td != undefined && td.attributes.length > 0 && td.attributes["iactivetabid"] != undefined) 
        {
            if (td.attributes["iactivetabid"].value == tabId) 
            {                
                td.className = "on add_onglet";
                break;
            }
        }
    }
}

// Affichage du RADWindow modal de sélection du fond d'écran à appliquer
function ShowBackGroundSelection() 
{
    var lnFrames = parent.frames.length;
    var lbBackGround;

    for (i = 0; i < lnFrames; i++) 
    {
        lbBackGround = parent.frames[i].document.getElementById("lbBackGround");

        if (lbBackGround)
            lbBackGround.click();
    }
}

function ChangeTabBackGroundSelection(tabId) 
{
    var parentRow = document.getElementById("ctl00_userControlTemplateSpecial_" + tbTabCellId).parentNode.parentNode;
    var hRefs = parentRow.getElementsByTagName("A");

    document.getElementById(hdShowPopinTemplateSpecialArgumentId).value = tabId;
}

function ChangeTabAddService(tabId) 
{
    var parentRow = document.getElementById("ctl00_userControlTemplateSpecial_" + tbTabCellId).parentNode.parentNode;
    var hRefs = parentRow.getElementsByTagName("A");

    document.getElementById(hdShowPopinTemplateSpecialArgumentId).value = tabId;
}

function ValidateInviteFriend() 
{
    document.getElementById(hdShowPopinTemplateArgumentId).value = "OK";
}

function BackGroundClientApply(BackGroundId, closePopinTemplateSpecial) {
    // Enregistrement du nouveau fond d'écran pour l'onglet de l'utilisateur
    //CallAjaxRecord("type=BackGroundChange&BackGroundId=" + BackGroundId + "&tabId=" + iActiveTabId, "changeBackGround");
    var data = "type=BackGroundChange&BackGroundId=" + BackGroundId + "&Lan=" + languageId + "&tabId=" + iActiveTabId;
    var url = AppBasePath + "ESP/AjaxAsyncDesktop.aspx?" + data + "&rnd=" + randomString("abcdecfhijklmnopqrstuvwxyz", 64);
    var xhr = createXHR();
    xhr.open("GET", url, true);
    xhr.send(null);
    
    // Recherche du l'URL de l'image du nouveau fond d'écran
    var data = "type=backgroundGetURL&BackGroundId=" + BackGroundId;
    var url = AppBasePath + "ESP/AjaxAsyncDesktop.aspx?" + data + "&Lan=" + languageId + "&rnd=" + randomString("abcdecfhijklmnopqrstuvwxyz", 64);
    var xhr = createXHR();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            SetSophusTag_JS('BackGroundChange');

            if (xhr.responseText) {
                response = eval('(' + xhr.responseText + ')');

                if (response != null) {
                    if (response.BackgroundURL != "") {
                        var iFrameWidg = document.getElementById(iFrameDependantId);
                        iFrameWidg.contentWindow.document.body.style.background = "url(" + response.BackgroundURL + ") center 3px no-repeat fixed";
                    }
                }
            }
        }
    };

    xhr.send(null);

    if (closePopinTemplateSpecial)
        ClosePopinTemplateSpecial(true);
}

function LoadDefaultTab2(tabId, tabName, tabIndex, widgetIdToFocus, rnd) 
{
    var iframeWidgets = document.getElementById(iFrameDependantId);

    if (iframeWidgets && (defaultTabLoaded == false)) 
    {
        iActiveTabId = tabId;
        defaultTabLoaded = true;
        clearInterval(inter);
        iframeWidgets.src = AppBasePath + "ESP/Widget.aspx?tabID=" + tabId + "&tabIndex=" + tabIndex + "&rnd=" + rnd + "&widgetIdToFocus=" + widgetIdToFocus + "&Lan=" + languageId + "&countryId=" + countryId + "&linguisticVersionId=" + linguisticVersionId;
    }
}
function RegisterWidgetsPositions(positions) 
{
    var hdPositions = document.getElementById(lstPositionsWidgetsId);
    if (hdPositions)
        hdPositions.value = positions;
}

function ChangeWidgetTab() 
{
    try 
    {        
        if (hOveredTab != "" && widgetDraggedId != "" && widgetDraggedId!=undefined && widgetDraggedId!= GuidEmpty && iActiveTabId != hOveredTab) 
        {
            var iFrameWidget = document.getElementById(iFrameDependantId);

            if (iFrameWidget && widgetDraggedId) 
            {
                var oWnd = iFrameWidget.contentWindow.$find(widgetDraggedId);

                if (oWnd) 
                {
                    var oManager = oWnd.get_windowManager();
                    
                    iFrameWidget.contentWindow.document.getElementById(oManager.get_id()).setAttribute("isDropped_"+oWnd.get_id(), "true");
                    
                    // Enregistrement du changement d'onglet du widget
                    var data = "type=WidgetTabChange&WidgetId=" + widgetDraggedId + "&Lan=" + iFrameWidget.contentWindow.document.getElementById(iFrameWidget.contentWindow.hdLanguageIdId).value + "&TabSourceId=" + iActiveTabId + "&TabDestinationId=" + hOveredTab;
                    var url = AppBasePath + "ESP/AjaxAsyncDesktop.aspx?" + data + "&rnd=" + randomString("abcdecfhijklmnopqrstuvwxyz", 64);
                    var xhr = createXHR();
                    xhr.open("GET", url, true);
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState == 4 && xhr.status == 200) 
                        {
                            if (xhr.responseText) 
                            {
                                response = eval('(' + xhr.responseText + ')');
                                if (response != null && response.ok == '1') 
                                {
                                    DecreaseNbWidgetsCurrentTab();

                                    // Sécurité pour ne pas supprimer le widget en base
                                    enableWidgetDeletion = false;

                                    if (oManager) 
                                    {
                                        var objManager = iFrameWidget.contentWindow.document.getElementById(oManager.get_id());

                                        if (objManager) 
                                        {
                                            objManager.setAttribute("enableWidgetDeletion_" + widgetDraggedId, "false");
                                            oWnd.close();
                                            iFrameWidget.contentWindow.widgetDraggedId = GuidEmpty;
                                            objManager.setAttribute("enableWidgetDeletion_" + widgetDraggedId, "true");
                                        }
                                    }

                                    iFrameWidget.contentWindow.ReloadRADWindowsIframes(oManager, false);

                                    // Ré-activation de la suppression des widgets
                                    enableWidgetDeletion = true;
                                }
                                else 
                                {
                                    document.getElementById(popinControlHiddenFieldId).value = "ucMessage";
                                    document.getElementById(codeMessageHiddenFieldId).value = maxNbWidgetsForTabReachedMessage;
                                    ShowPopinTemplate();                                    
                                    iFrameWidget.contentWindow.ReloadRADWindowsIframes(oManager, true);
                                }
                            }
                        }
                    };
                    
                    xhr.send(null);
                }
                widgetDraggedId = GuidEmpty;
            }
        }
    }
    catch (e) 
    {    
    }
}
