﻿
function ToggleDropDown(linkId, containerId, defaultClassName)
{
    var className = GetElementClassName(linkId);
    if(className.indexOf("Selected") == -1)
    {
        ShowDropDown(linkId, containerId, defaultClassName);
    }
    else
    {
        HideDropDown(linkId, containerId, defaultClassName);
    }
}

function ShowDropDown(linkId, containerId, defaultClassName)
{
    var browser = GetBrowser();
    var link = document.getElementById(linkId);
    var container = document.getElementById(containerId);    
    var className = GetElementClassName(linkId);
    if(className.indexOf("Selected") == -1)
    {
        container.style.display = "block";
        if(browser != "IE")
        {
            link.setAttribute("class", className + "Selected");
        }
        else
        {
            link.setAttribute("className", className + "Selected");
        }
    }
}

function HideDropDown(linkId, containerId, defaultClassName)
{
    var browser = GetBrowser();
    var link = document.getElementById(linkId);
    var container = document.getElementById(containerId);    
    var className = GetElementClassName(linkId);
    if(className.indexOf("Selected") > -1)
    {
        container.style.display = "none";
        if(browser != "IE")
        {
            link.setAttribute("class", className.substring(0, className.length - 8));
        }
        else
        {
            link.setAttribute("className", className.substring(0, className.length - 8));
        }
    }
}

function MatchDropDownSize(dropDownContainerId,
    offsetContainerId,
    dropDownListId,
    buttonId,
    elements)
{
    var dropDownContainer = document.getElementById(dropDownContainerId);
    var offsetContainer = document.getElementById(offsetContainerId);
    var dropDownList = document.getElementById(dropDownListId);
    var button = document.getElementById(buttonId);
    
    if((dropDownContainer != null) && 
    (dropDownList != null) &&
    (button != null) && 
    (offsetContainer != null) &&
    (elements != null))
    {
    
        var newWidth = button.offsetWidth;
        dropDownList.style.width = (newWidth - 3) + "px";
        dropDownContainer.style.right = (offsetContainer.offsetWidth + 2) + "px"
        
        if(elements.length > 0)
        {
            for(var x = 0; x < elements.length; x++)
            {
                var innerElement = document.getElementById(elements[x]);
                if(innerElement != null)
                {
                    innerElement.style.width = (newWidth - 23) + "px";
                }
            }
        }
        
    }
}

function ajaxRequest()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    { 
        // code for IE6, IE5
        return  new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        return null;
    }
}

function ToggleBalances(linkId, containerId, showText, hideText, appPath)
{

    var link = document.getElementById(linkId);
    var container = document.getElementById(containerId);    
    
    if((link != null) && (container != null))
    {
        if(link.innerHTML != hideText)
        {
            container.style.display = "block";
            link.innerHTML = hideText;
                    
            var bonusValue = document.getElementById("BonusValue");
            var withdrawableValue = document.getElementById("WithdrawableValue");
            var totalValue = document.getElementById("TotalValue");
            
            if((bonusValue != null) && (withdrawableValue != null) && (totalValue != null))
            {               
                var xmlhttp = new ajaxRequest();
                if(xmlhttp != null)
                {
                    var getBonusURL = appPath + "/Services/GetBalance.ashx?DetailedResponse=1"
                    xmlhttp.open("GET", getBonusURL, true); 
                    xmlhttp.onreadystatechange=function() 
                    {                        
                        if (xmlhttp.readyState==4) 
                        {
                            if((xmlhttp.responseText != null) && (xmlhttp.responseText.length > 0))
                            {
                                if(xmlhttp.responseText.indexOf("|") > -1)
                                {
                                    var bonusValues = xmlhttp.responseText.split("|");
                                    if((bonusValues != null) && (bonusValues.length == 3))
                                    {
                                        bonusValue.innerHTML = bonusValues[1];
                                        withdrawableValue.innerHTML = bonusValues[0];
                                        totalValue.innerHTML = bonusValues[2];
                                    }
                                }
                            }
                        }
                    }    
                    xmlhttp.send(null);               
                }
            }            
        }
        else
        {
            container.style.display = "none";
            link.innerHTML = showText;
        }
    }
}

function UsernameBehaviour(eventId, usernameId, defaultText)
{
    var postField = document.getElementById("txtUserName");
    var username = document.getElementById(usernameId);
    
    if((postField != null) && (username != null))
    {
        postField.value = username.value;
       
        if(((eventId == "click") || (eventId == "focus")) && 
        (username.value == defaultText))
        {
            username.value = "";
        }
        else if((eventId == "blur") && (username.value == ""))
        {
            username.value = defaultText;
        }
    }
}

function PasswordBehaviour(passwordId, plainPasswordId, direction)
{
    var postField = document.getElementById("txtPassword");
    var password = document.getElementById(passwordId);
    var plainPassword = document.getElementById(plainPasswordId);
        
    if((postField != null) && (password != null) && (plainPassword != null))
    {
        postField.value = password.value;
        
        if(direction == 1)
        {
            if(password.value.length == 0)
            {
                password.style.display = "none";
                plainPassword.style.display = "block";
            }
        }
        else if(direction == 2)
        {
            password.style.display = "block";
            plainPassword.style.display = "none";
            password.focus();
        }
    }
}

function SetHiddenPassword(passwordId)
{
    var postField = document.getElementById("txtPassword");
    var password = document.getElementById(passwordId);
    
    if((postField != null) && (password != null))
    {
        postField.value = password.value;        
    }
}

function AddLinkButtonClickFunction(id) 
{
    var linkButton = document.getElementById(id);

    if ((linkButton) && (typeof(linkButton.click) == 'undefined')) 
    {
        linkButton.click = function() 
        {
            var result = true;

            if (linkButton.onclick) 
            {
                result = linkButton.onclick();
            }     
            if ((typeof(result) == 'undefined') || (result))
            {
                eval(linkButton.getAttribute('href'));
            }
        }
    }
}

