﻿///////////////////////////////////////////////////////////////////////////////
// Put here ONLY common functions for ALL THE WEBSITES
// Only functions that are reused in MyAccount, Bingo, Casino, Poker
// If javascript specific MyAccount functions, please put them in 'main.js'
///////////////////////////////////////////////////////////////////////////////

function RefreshMe() {
    try {
        document.forms[0].submit();
    }
    catch (err) {
    }
}

//Reloads the browser window with the url specified. If url is string empty reloads the same window
function WindowReload(url) {
    try {
        if (url == '') {
            window.location.reload();
        }
        else {
            window.location = url;
        }
    }
    catch (err) {
    }
}


function RefreshVPoints() {
    try {
        document.forms[0].submit();
    }
    catch (err) {
    }
}

function ShowPanelMessage(panelId, messageId, message) {
    document.getElementById(panelId).style.display = "block";
    var msgHolder = document.getElementById(messageId).innerHTML = message;
}


/* Applies the 'inputOnFocus' style to elements (the style as defined in CSS file is only for input, textbox...) */
function OnFocus(id) {
    if (!Sys.UI.DomElement.containsCssClass($get(id), "inputOnFocus")) {
        Sys.UI.DomElement.addCssClass($get(id), "inputOnFocus");
    }
}

function OnBlur(id) {
    if (Sys.UI.DomElement.containsCssClass($get(id), "inputOnFocus")) {
        Sys.UI.DomElement.removeCssClass($get(id), "inputOnFocus");
    }
}

function SetFocus(id) {
    $find(id)._onFocus();
}


/// <summary>
/// Stops multiple submissions, verifying first that the group of elements are valid (example in /Casino/BuyInWindow.aspx)
/// </summary>
/// <param name="globalObjectCanSubmit">global variable indicating if page has already been submitted</param>
/// <param name="group">validation group (all the elements in the group need to be valid, if 'null' full page needs to be valid)</param>
/// <param name="onSubmitFunction">function to be executed on submitting</param>
function CanSubmitValidGroup(globalObjectCanSubmit, group, onSubmitFunction) {

    if (globalObjectCanSubmit.canSubmit) {
        //Checks if ASP.NET javascript validators on the page are successful
        if (typeof (Page_ClientValidate) != 'function' || Page_ClientValidate(group)) {                       
            globalObjectCanSubmit.canSubmit = false;
            onSubmitFunction();
            return true;
        }
    }
    return false;
}

/// <summary>
/// Stops multiple submissions when click on html anchors (or asp:LinkButton) passed as parameter (example in Casino/BuyInWindow.aspx)
/// </summary>
/// <param name="thisAnchor">anchor that needs to be disabled</param>
function CanSubmitAnchor(thisAnchor) {
    
    var cssClass = 'anchorDisabled';    
    //If possible to submit checks if already submited if not adds the attribute
    if (thisAnchor.className.indexOf(cssClass) == -1) {
        thisAnchor.className += (thisAnchor.className.length != 0 ? " " : "") + cssClass;
        return true;
    }
    return false;
}


//* resize login iframe on login - start

function ReSizeLoginIframe() {
    AutosizeIFrame('ctl00_iLogin', 280); //270
}

function CalcIFrameHeight(iFrameId) {

    try {
        //iframe internal page height
        var iframeHeight = document.getElementById(iFrameId).contentWindow.document.body.scrollHeight;

        document.getElementById(iFrameId).height = iframeHeight;
    }
    catch (e) { }
}

function AutosizeIFrame(iFrameId, minHeight) {
    var iFrameObject = document.getElementById(iFrameId);
    if (iFrameObject != null) {
        //var contentHeight= GetContentHeight(iFrameObject);
        //iFrameObject.height = (contentHeight < minHeight ? minHeight : contentHeight);
        iFrameObject.height = minHeight;
        //alert("AutosizeIFrame: " + iFrameObject.height);
    }
}

function GetContentHeight(iFrameObject) {
    var contentHeight = 1;

    if (navigator.appName != "Microsoft Internet Explorer" && !window.opera && !document.mimeType && document.all && document.getElementById)
        contentHeight = iFrameObject.contentWindow.document.body.offsetHeight;
    else if (document.getElementById)
        contentHeight = iFrameObject.contentWindow.document.body.scrollHeight;

    return contentHeight;
}

//Checks if the key pressed is Enter key
function IsEnterKey(e) {
    var keynum = 0;
    if (e.keyCode) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    return keynum == 13;
}

//* resize login iframe on login - end

//Generates the code to display a Flash passing the absolute path
function DisplayFlashAbsolute(path, width, height, backgroundColor, flashVars, areaId) {
    var so = new VgmSWFObject(path, "flash" + areaId, width, height, "9", backgroundColor);
    so.addParam("salign", "lt");
    so.addParam("scale", "noscale");
    so.addParam("wmode", "transparent");
    if (flashVars != "") {
        so.addParam("FlashVars", flashVars);
    }
    so.write(areaId);
}

//Generates the code to display a Flash file contained in /MyAccount/swf/
function DisplayFlash(path, width, height, backgroundColor, flashVars, areaId) {
    DisplayFlashAbsolute("/MyAccount/swf/" + path, width, height, backgroundColor, flashVars, areaId);
}


////////
// Functions to open popup windows
////////

//Global reference to the last window open (check this to know if already some other popup)
var openWindowRef;

//Opens a resizable window
// If target is not specified opens Url in a new window (_blank)
function OpenWindow(theURL, theWidth, theHeight, target) {

    var settings = 'toolbar=no,location=no,scrollbars=yes,resizable=yes,titlebar=no';

    openWindowRef = OpenWindowBase(theURL, theWidth, theHeight, target, settings);

    return openWindowRef;
}

//Opens a fixed size window
// If target is not specified opens Url in a new window (_blank)
function OpenFixedSizeWindow(theURL, theWidth, theHeight, target) {
    var settings = 'toolbar=no,location=no,scrollbars=no,resizable=no,titlebar=no,directories=no';

    openWindowRef = OpenWindowBase(theURL, theWidth, theHeight, target, settings);
}

//Common function to open a window used in OpenWindow and OpenFixedSizeWindow
function OpenWindowBase(theURL, theWidth, theHeight, target, settings) {

    var availHeight = screen.availHeight - 50; //discounting Windows taskbar
    var winWidth = (screen.availWidth >= theWidth) ? theWidth : screen.availWidth;
    var winHeight = (availHeight >= theHeight) ? theHeight : availHeight;
    var winLeft = (screen.availWidth - winWidth) / 2;

    var winSettings = 'height=' + winHeight + ',width=' + winWidth + ',top=30,left=' + winLeft + ',' + settings + '\'';

    openWindowRef = window.open(theURL, (typeof (target) === 'undefined' ? '_blank' : target), winSettings);

    if (openWindowRef) {
        openWindowRef.focus();
    }
    else {
        alert('We have detected that your popup blocker prevented us from launching Virgin Games window. Please enable your browser to allow this site to launch popups');
    }

    return openWindowRef;
}

// If this function does not work for cms elements in PopUpDefault.master, working version exists in portal/js/misc.js 
function ToggleLayer(layerId)
{
   var layer = document.getElementById(layerId);
   
   if (layer.style.display == 'block')
   {
       //hideLyr(layerId)
       layer.style.display = "none";
   }
   else
   {
       //showLyr(layerId)#
       layer.style.display = "block";
   }
}

//Alternate Show Hide
function showLyr(lyrId) {
    document.getElementById(lyrId).style.display = "block";
}

function hideLyr(lyrId) {
    document.getElementById(lyrId).style.display = "none";
}

/* When required to show/hide table rows works different than 'div' IE and FF have different behaviour
showText: message to display to indicate show
hideText: message to display to indicate hide
linkShowHideId: text will be replaced for this link
rowId: Row we whant 
*/
function ToggleTableRow(showText, hideText, linkShowHideId, rowId) {
    var element = document.getElementById(rowId);
    var link = document.getElementById(linkShowHideId);

    if (element.style.display == 'block' || element.style.display == 'table-row') {
        element.style.display = 'none';
        link.innerHTML = showText;
    }
    else {
        try {
            element.style.display = 'table-row';
        }
        catch (e) {
            element.style.display = 'block';
        }
        link.innerHTML = hideText;
    }
}
