﻿
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((navigator.userAgent.toLowerCase().indexOf("msie 6.") > -1) || 
        (navigator.userAgent.toLowerCase().indexOf("msie 7.") > -1))
        {
            link.setAttribute("className", className + "Selected");
        }
        else
        {
            link.setAttribute("class", 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((navigator.userAgent.toLowerCase().indexOf("msie 6.") > -1) || 
        (navigator.userAgent.toLowerCase().indexOf("msie 7.") > -1))
        {
            link.setAttribute("className", className.substring(0, className.length - 8));
        }
        else
        {
            link.setAttribute("class", 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;
                    
            PopulateBalances(appPath);
        }
        else
        {
            container.style.display = "none";
            link.innerHTML = showText;
        }
    }
}

function PopulateBalances(appPath)
{
    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);               
        }
    }      
}

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'));
            }
        }
    }
}

function ApplyPNG(imageId, pngUrl, appPath) 
{ 
    var imageObj = document.getElementById(imageId);
    if(imageObj != null)
    {              
	    imageObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + pngUrl + "', sizingMethod='scale')";
	    imageObj.src = appPath + '/Images/x.gif';
    }
}

function CorrectPNG(imageId, appPath) 
{ 
    var imageObj = document.getElementById(imageId);
    if(imageObj != null)
    {              
        //backgrounds        
		if (imageObj.currentStyle.backgroundImage.match(/\.png/i) !== null) 
		{
			bg_fnFixPng(imageObj, appPath);
		}
		// image elements
		if (imageObj.tagName=='IMG' && imageObj.src.match(/\.png$/i) !== null)
		{
			el_fnFixPng(imageObj, appPath);
		}
    }
}

var bg_fnFixPng = function(obj, appPath) 
{
	var shim = 'Images/x.gif';
	var mode = 'scale';
	var bg	= obj.currentStyle.backgroundImage;
	var src = bg.substring(5,bg.length-2);
	if (obj.currentStyle.backgroundRepeat == 'no-repeat') 
	{
		mode = 'crop';
	}
	obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
	obj.style.backgroundImage = 'url(' + appPath + '/' + shim + ')';
	obj.style.visibility = 'visible';
}

var el_fnFixPng = function(img, appPath) 
{
	var shim = 'Images/x.gif';	
	var src = img.src;
	    img.style.visibility = 'visible';	    
	    img.style.display = 'block';
	img.style.width = img.width + "px";
	img.style.height = img.height + "px";
	img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
	img.src = appPath + '/' + shim;
}

