﻿//
// Code required by main site Default.aspx and also Games/Default.aspx
//

function SavePrefsToBroker(prefs, isSavedToBroker, functionCalledAfterSave)
{
    var updatePunterPreferencesImageContainer;
    if (top.isGamesSite)
        updatePunterPreferencesImageContainer = top.document.getElementById('updatePunterPreferencesImageContainer');
    else
        updatePunterPreferencesImageContainer = top.GBE_header.document.getElementById('updatePunterPreferencesImageContainer');

    if (updatePunterPreferencesImageContainer != null)
    {
        updatePunterPreferencesImageContainer.innerHTML = ''; /*clear existing image*/

        var newImageElement;
        if (top.isGamesSite)
            newImageElement = top.document.createElement('IMG');
        else
            newImageElement = top.GBE_header.document.createElement('IMG');

        var timePostFix = new Date().getTime();
        timePostFix += '&currentPreferences=' + top.GetPreferencesURLPostfix();
        newImageElement.src = encodeURI(GBE_Web_UI_Util_Root + "/Secure/Account/UpdatePunterPrefs.aspx?x=" + timePostFix + "&isSavedToBroker=" + isSavedToBroker);
        newImageElement.width = 1;
        newImageElement.id = 'updatePunterPreferencesImage';
        newImageElement.height = 1;
        updatePunterPreferencesImageContainer.appendChild(newImageElement);

        if (functionCalledAfterSave)
        {
            newImageElement.calledFunction = functionCalledAfterSave;
            callFunctionAfterPreferencesUpdated();

        }

    }

}  		

function getNewTopWindow(method)
{
    try
    {
        return eval("opener.top." + method);
    }
    catch (e)
    {
        /*alert('HERE CAN BE ALERT THAT POPUP HAS TO BE CLOSED');*/
        /*window.close();*/
        if (method.indexOf("loadHelp") > -1)
        {
            try
            {
                return eval("top." + method);
            }
            catch (e)
            { }
        }
        if (method.indexOf("displayPopup") > -1)
        {
            try
            {
                return alert("Your session has expired. Please close your browser and log in again")
            }
            catch (e) { }
        }

        /*alert("error");*/
    }
}

function OpenPopupWindow(url, isModal, warningTextIfNotOpened, properties, name)
{
    var newWindow = null;
    if (isModal == true)
    {
        newWindow = xShowModalDialog(url, null, warningTextIfNotOpened);
    }
    else
    {
        if (properties == null)
            properties = 'status=no,width=960, top=25, left=20, height=650,scrollbars=no,menubar=no,titlebar=no,toolbar=no,resizable=yes';
        newWindow = window.open(url, name, properties);

        if (newWindow == null)
        {
            if (warningTextIfNotOpened != null)
            {
                alert(warningTextIfNotOpened);
            }
            else
            {
                alert(GBE_Web_UI_Common_Popup_DefaultErrorMessageWhenOpening);
            }
        }
    }
    return newWindow;
}

modalWin = "null";

function xShowModalDialog(sURL, vArguments, warningTextIfNotOpened)
{
    if (sURL == null || sURL == '')
    {
        /*alert ("Invalid URL input.");*/
        return false;
    }
    if (vArguments == null || vArguments == '')
    {
        vArguments = '';
    }

    if (window.navigator.appVersion.indexOf("MSIE") != -1)
    {
        var newWin = null;
        try
        {
            newWin = window.showModalDialog(sURL, vArguments, "dialogWidth:400px;dialogHeight:210px;scroll:no;");
        }
        catch (e)
        {
            newWin = null;
        }
        newWin = true; /*temporary fix */
        if (newWin == null)
        {
            if (warningTextIfNotOpened != null)
            {
                alert(warningTextIfNotOpened);
            }
            else
            {
                alert(GBE_Web_UI_Common_Popup_DefaultErrorMessageWhenOpening);
            }
        }
        return newWin;

    }
    modalWin = window.open(String(sURL), "", "scrollbars=no,directories=0,menubar=0,titlebar=0,toolbar=0,modal=yes,height=1010,width=1000");
    if (modalWin == null)
    {
        if (warningTextIfNotOpened != null)
        {
            alert(warningTextIfNotOpened);
        }
        else
        {
            alert(GBE_Web_UI_Common_Popup_DefaultErrorMessageWhenOpening);
        }
    }
    else
    {
        window.onfocus = function (e)
        {
            if (e == null) e = window.event;
            StopEvent(e);
            window.blur();
            modalWin.focus();
        };

        if (vArguments != null && vArguments != '')
        {
            modalWin.dialogArguments = vArguments;
        }
    }

    return modalWin;
}

function StopEvent(evt)
{
    if (evt.preventDefault)
    {
        evt.preventDefault();
    }
    if (evt.stopPropagation)
    {
        evt.stopPropagation();
    }
    evt.returnValue = false;


    if (window.event != null) window.event.cancelBubble = true;
    evt.cancelBubble = true;
}



function submitFormOnKeyPress(e, obj, submitButtonElement, avoidSubmit)
{
    try
    {
        if (obj != null
            && top.GBE_main != null
            && top.GBE_main.GBE_rightNav != null
            && top.GBE_main.GBE_rightNav.GBE_rightBetSlip != null
            && top.GBE_main.GBE_rightNav.GBE_rightBetSlip.focusId != null
            && top.GBE_main.GBE_rightNav.GBE_rightBetSlip.focusId != '')
        {
            var focusedElement = top.GBE_main.GBE_rightNav.GBE_rightBetSlip.document.getElementById(top.GBE_main.GBE_rightNav.GBE_rightBetSlip.focusId);
            if (focusedElement != null && focusedElement.value != '' && focusedElement.onchange)
            {
                focusedElement.onchange();
            }
        }
    }
    catch (e)
    {
    }

    var key;
    var result = false;

    if (obj.document != null && obj.document.parentWindow != null && obj.document.parentWindow.event != null)
        key = obj.document.parentWindow.event.keyCode;     /*IE*/
    else key = e.which;     /*firefox*/

    if (key == 13)
        result = true;
    else result = false;

    if (result == true && avoidSubmit == true)
    {
        result = !result;
    }

    if (result == true)
    {
        if (submitButtonElement != null)
            submitButtonElement.focus();

        if (submitButtonElement != null && submitButtonElement.onclick != null)
        {
            e.returnValue = submitButtonElement.onclick();
            return e.returnValue;
        }
        else
        {
            e.returnValue = top.getTopWindow().ForceSubmitForm(obj);
            return e.returnValue;
        }
    }

    return true;
}

function setCookie(name, value, days, setDomainToTopLevel)
{
    if (value != null)
    {
        var today = new Date();
        var expiry = new Date(today.getTime() + days * 24 * 60 * 60 * 1000);

        document.cookie = name + "=" + escape(value)
            + (setDomainToTopLevel == true ? "; domain=." + TOPLEVELDOMAIN : "")
            + "; path=/; "
            + (days != null ? "expires=" + expiry.toGMTString() : "");
    }
}

var oddsSystemTypes = {
    DECIMAL: 0,
    FRACTIONAL: 1,
    AMERICAN: 2
};
/*
odds sytem id's -- passed in on url's 
0 - decimal
1 - fractional
2 - american
*/

