﻿var cookieHasNotYetBeenChecked = true;
var cookieHasExisted = false;

function setCookie(expiryMins)
{
    var exdate=new Date();

    if((expiryMins != null) && 
    (expiryMins > 0))
    {
        exdate.setTime(exdate.getTime() + (expiryMins * 60 * 1000));
    }
    else
    {
        var currenthours = exdate.getHours();      
        currenthours += 3;            
        if(currenthours > 23)
        {
            currenthours = currenthours - 24;     
            exdate.setDate(exdate.getDate() + 1);  
        }                      
        exdate.setHours(currenthours);
    }
    
    document.cookie= "bet365_OpenAccount_Session=True;expires="+exdate.toGMTString() + ";path=/";
    
};

function checkCookie()
{
    
    var openAccountCookie=getCookie('bet365_OpenAccount_Session')
    var recall = true;

    if ((openAccountCookie!=null && openAccountCookie!="") || cookieHasNotYetBeenChecked)
    {
        if(openAccountCookie!=null && openAccountCookie!="" && !cookieHasExisted)
        {
            cookieHasNotYetBeenChecked = false;
            cookieHasExisted = true;
        }
    }
    else 
    {       
        recall = false;
        var exdate=new Date();
	    exdate.setDate(exdate.getDate() - 1);	
	    document.cookie = "bet365_OpenAccount_Session=;expires="+exdate.toGMTString() + ";path=/";
        window.location = window.location;
    }
      
    if(recall)
    {
     setTimeout("checkCookie();", 1000);
    }

}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}

var timerID = null;
var clock = null;
var clockSpan;

function Start(yy,mo,da,hh,mm,ss)
{    
   
    var oDoc = document;
    if(IsDocLoaded(oDoc))
    {
        clock=new Date(yy,mo,da,hh,mm,ss);
	    clockSpan=document.getElementById('clock');
	    if(typeof clockSpan!='undefined'){UpdateTimer(1000);}
    }
    else
    {
        setTimeout("Start(" + yy + "," + mo + "," + da + "," + hh + "," + mm + "," + ss + ");", 1000);
    }

};

function UpdateTimer(increase)
{
    var h;
    var m;
    var s;

	if(timerID)
	{
		clearTimeout(timerID);
	};

	clock.setTime(clock.getTime()+increase);
	
	if(clock.getHours()<10)
	{
		h='0'+clock.getHours();
	}
	else
	{
		h=clock.getHours();
	};
	
	if(clock.getMinutes()<10)
	{
		m='0'+clock.getMinutes();
	}
	else
	{
		m=clock.getMinutes();
	};
	
	if(clock.getSeconds()<10)
	{
		s='0'+clock.getSeconds();
	}
	else
	{
		s=clock.getSeconds();
	};
	
	clockSpan.innerHTML=""+h+":"+m+":"+s;
	
	timerID=setTimeout("UpdateTimer(1000)",1000);
	
}

function GetBrowser()
{
	var sBrowser=navigator.userAgent.toLowerCase();

	if(sBrowser.indexOf("chrome") != -1)
	{
		return 'CHROME';
	}
	else if(sBrowser.indexOf("opera") != -1)
	{
		return 'OPERA';
	}
	else if(sBrowser.indexOf("netscape") != -1)
	{
		if(sBrowser.indexOf("msie") != -1){return 'IE';}
		else{return 'NETSCAPE';}
	}
	else if(sBrowser.indexOf("firefox") != -1)
	{
		return 'FIREFOX';
	}
	else if(sBrowser.indexOf("chrome") != -1)
	{
		return 'CHROME';
	}
	else if(sBrowser.indexOf("safari") != -1)
	{
		return 'SAFARI';
	}
	else if(sBrowser.indexOf("msie 8.") != -1)
	{
		return 'IE8';
	}
	else if(sBrowser.indexOf("msie") != -1)
	{
		return 'IE';
	}
	else
	{
		return 'IE*';
	}
}

function GetScreenRes()
{
	var sHeight = screen.height;

	switch (GetBrowser())
	{
		case 'OPERA' :
			sHeight=(sHeight * 0.70);
			return sHeight;
		case 'NETSCAPE' :
			sHeight=sHeight-200;
			return sHeight;
		case 'IE' :
			sHeight=sHeight-250;
			return sHeight;
		case 'FIREFOX' :
			sHeight=sHeight-250;
			return sHeight;
		case 'IE8' :
			sHeight=sHeight-250;
			return sHeight;
	}
}

function GetScreenWidth()
{
	var sWidth = screen.width;
	sWidth=sWidth - (sWidth * 0.90);
	return Math.round(sWidth);
}

function GetScreenTop()
{
	var sTop = screen.height;
	sTop=sTop - (sTop * 0.88);
	return Math.round(sTop);
}

function IsDocLoaded(oDoc)
{
    var sBsr = GetBrowser();
    var sState = false;

    if (sBsr =='FIREFOX')
    {
          sState = true;
    }
    else if (sBsr =='SAFARI')
    {
          if (/loaded|complete/.test(document.readyState))
          {
                sState = true;
          }
    }
    else
    {
          if(oDoc.readyState=="complete" || oDoc.readyState=="interactive")
          {
                sState = true;
          }
    }

    return sState;

}

function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}

function fixOnMouseOut(element, event, JavaScript_code) {
	var current_mouse_target = null;
	if( event.toElement ) {				
		current_mouse_target 			 = event.toElement;
	} else if( event.relatedTarget ) {				
		current_mouse_target 			 = event.relatedTarget;
	}
	if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
		eval(JavaScript_code);
	}
}

function fixOnMouseOutWithAdditionalElement(mainElement, extraElementClientId, event, JavaScript_code) {
	
	var current_mouse_target = null;
	var extraElement = document.getElementById(extraElementClientId);
	
	if( event.toElement ) 
	{				
		current_mouse_target = event.toElement;
	} 
	else if( event.relatedTarget ) 
	{				
		current_mouse_target = event.relatedTarget;
	}
	
	if( !is_child_of(mainElement, current_mouse_target) && mainElement != current_mouse_target ) 
	{	    
	    if( !is_child_of(extraElement,current_mouse_target) && extraElement != current_mouse_target ) 
	    {		    	       
	        eval(JavaScript_code);
		}
	}
}

function GetElementClassName(elementId, defaultClassName)
{    
    var browser = GetBrowser();
    var element = document.getElementById(elementId);
    var className = defaultClassName;    
    if(((browser.indexOf("IE") == -1) || 
    (navigator.userAgent.toLowerCase().indexOf("msie 8.") > -1)) && 
    (element.getAttribute("class") != null))
    {
        className = element.getAttribute("class");
    }  
    else if((browser.indexOf("IE") > -1) && (element.getAttribute("className") != null))
    {
        className = element.getAttribute("className");
    }
    return className;
}

function Slider_Load(siderButtonIdPrefix,
    selectedItemId, 
    targetValueId,
    inProgressId,
    portalPanelId,
    sliderPanelId,
    newIndex,
    maxIndex,
    extraScroll)
{    

    var selectedItem = document.getElementById(selectedItemId);
    var targetValue = document.getElementById(targetValueId);
    var inProgress = document.getElementById(inProgressId);
    var portalPanel = document.getElementById(portalPanelId);
    var sliderPanel = document.getElementById(sliderPanelId);

    if((selectedItem != null) && 
    (targetValue != null) &&
    (portalPanel != null) &&
    (sliderPanel != null) &&
    (inProgress != null))
    {
      
        if(extraScroll == null)
        {
            extraScroll = 0; 
        }
        
        var itemWidth = portalPanel.offsetWidth;
        
        var targetPosition = (itemWidth * newIndex) * -1
        
        if(extraScroll > 0)
        {
            if(targetPosition < 0)
            {
                targetPosition = targetPosition - (newIndex * extraScroll);
            }
            else
            {
                targetPosition = targetPosition + (newIndex * extraScroll);
            }
        }
        
        sliderPanel.style.left = targetPosition + "px";
        
        var newSelectedButton = document.getElementById(siderButtonIdPrefix + "_Button" + newIndex);
        
        if(newSelectedButton != null)
        {            
            var browser = GetBrowser();
            
            for(var x=0;x<=maxIndex;x=x+1)
            {
                if(x != newIndex)
                {                
                    var noneSelectedButton = document.getElementById(siderButtonIdPrefix + "_Button" + x);
                    if(noneSelectedButton != null)
                    {
                    
                        var noneSelectedClass = "btnsMiddle";
                        if(x == 0)
                        {
                            noneSelectedClass = "btnsLeft";
                        }
                        else if(x == maxIndex)
                        {
                            noneSelectedClass = "btnsRight";
                        }
                        
                        if(browser != "IE")
                        {
                            noneSelectedButton.setAttribute("class", noneSelectedClass);
                        }
                        else
                        {
                            noneSelectedButton.setAttribute("className", noneSelectedClass);
                        }   
                        
                    }
                }
            }
            
            var newSelectedClass = "btnsMiddleSelected";
            if(newIndex == 0)
            {
                newSelectedClass = "btnsLeftSelected";
            }
            else if(newIndex == maxIndex)
            {
                newSelectedClass = "btnsRightSelected";
            }
                                    
            if(browser != "IE")
            {
                newSelectedButton.setAttribute("class", newSelectedClass);
            }
            else
            {
                newSelectedButton.setAttribute("className", newSelectedClass);
            }      
        }
    }
}

function Slider_Click(siderButtonIdPrefix,
    selectedItemId, 
    targetValueId,
    inProgressId,
    portalPanelId,
    sliderPanelId,
    newIndex,
    maxIndex,
    extraScroll)
{    

    var selectedItem = document.getElementById(selectedItemId);
    var targetValue = document.getElementById(targetValueId);
    var inProgress = document.getElementById(inProgressId);
    var portalPanel = document.getElementById(portalPanelId);
    var sliderPanel = document.getElementById(sliderPanelId);

    if((selectedItem != null) && 
    (targetValue != null) &&
    (portalPanel != null) &&
    (sliderPanel != null) &&
    (inProgress != null))
    {
        
        var selectedIndex = selectedItem.value;       
       
        if(inProgress.value == "0")
        {                    
            var itemWidth = portalPanel.offsetWidth;
                            
            inProgress.value = "1";
            targetValue.value = newIndex;   
                         
            PerformScrolling(sliderPanelId, 
                inProgressId,
                targetValueId,
                selectedItemId,
                itemWidth,
                extraScroll);  
                
        }
        else if(inProgress.value == "1")
        {        
            targetValue.value = newIndex;
        }
        
        var newSelectedButton = document.getElementById(siderButtonIdPrefix + "_Button" + newIndex);
        
        if(newSelectedButton != null)
        {            
            var browser = GetBrowser();
            
            for(var x=0;x<=maxIndex;x=x+1)
            {
                if(x != newIndex)
                {                
                    var noneSelectedButton = document.getElementById(siderButtonIdPrefix + "_Button" + x);
                    if(noneSelectedButton != null)
                    {
                    
                        var noneSelectedClass = "btnsMiddle";
                        if(x == 0)
                        {
                            noneSelectedClass = "btnsLeft";
                        }
                        else if(x == maxIndex)
                        {
                            noneSelectedClass = "btnsRight";
                        }
                        
                        if(browser != "IE")
                        {
                            noneSelectedButton.setAttribute("class", noneSelectedClass);
                        }
                        else
                        {
                            noneSelectedButton.setAttribute("className", noneSelectedClass);
                        }   
                        
                    }
                }
            }
            
            var newSelectedClass = "btnsMiddleSelected";
            if(newIndex == 0)
            {
                newSelectedClass = "btnsLeftSelected";
            }
            else if(newIndex == maxIndex)
            {
                newSelectedClass = "btnsRightSelected";
            }
                                    
            if(browser != "IE")
            {
                newSelectedButton.setAttribute("class", newSelectedClass);
            }
            else
            {
                newSelectedButton.setAttribute("className", newSelectedClass);
            }      
        }        
    }
}

function PerformScrolling(sliderPanelId,
    inProgressId,
    targetValueId,
    selectedItemId,
    itemWidth,
    extraScroll)
{    
    var sliderPanel = document.getElementById(sliderPanelId);
    var inProgress = document.getElementById(inProgressId);
    var selectedItem = document.getElementById(selectedItemId);
    var targetValue = document.getElementById(targetValueId);
    
    if(extraScroll == null)
    {
        extraScroll = 0; 
    }
           
    if((sliderPanel != null) &&
    (targetValue != null) &&
    (inProgress != null) &&
    (selectedItem != null))
    {      
            
        var targetPosition = (itemWidth * (targetValue.value)) * -1
        
        if(extraScroll > 0)
        {
            if(targetPosition < 0)
            {
                targetPosition = targetPosition - ((targetValue.value) * extraScroll);
            }
            else
            {
                targetPosition = targetPosition + ((targetValue.value) * extraScroll);
            }
        }
     
        var selectedIndex = selectedItem.value;
        var currentPosition = (itemWidth * selectedIndex) * -1;
                
        if((sliderPanel.style.left != null) && 
        (sliderPanel.style.left.length > 0))
        {
        
            var position = sliderPanel.style.left;
            if(position.indexOf("px") > -1)
            {
                position = position.substring(0, position.length - 2)
            }
            
            currentPosition = parseInt(position);
        }
        
        if(currentPosition != targetPosition)
        {      
        
            var change = itemWidth / 30;
            var pause = 10;
            
            if(change < 10)
            {
                change = 10;
            }
            if(currentPosition > targetPosition)            
            {
                change = change * -1;
            }              
            
            var decelerationZone = (itemWidth / 150);
            
            if(change < 0)
            {     
                if(targetPosition > currentPosition - decelerationZone)
                {                    
                    change = change / 15;
                    pause = 50;
                }  
                if(targetPosition > currentPosition - (decelerationZone * 5))
                {                    
                    change = change / 10;
                    pause = 40;
                }                  
                else if(targetPosition > currentPosition - (decelerationZone * 10))
                {
                    change = change / 8;
                    pause = 30;
                }
                else if(targetPosition > currentPosition - (decelerationZone * 15))
                {
                    change = change / 6; 
                    pause = 20;
                }
                else if(targetPosition > currentPosition - (decelerationZone * 20))
                {
                    change = change / 4;
                    pause = 10;
                } 
                else if(targetPosition > currentPosition - (decelerationZone * 30))
                {              
                    change = change / 2;
                }       
                                
                if(targetPosition - currentPosition > change)
                {
                    change = targetPosition - currentPosition;
                } 
                
                if(change > -1)
                {
                    change = -1;
                }
                              
            }
            else
            {
            
                if(targetPosition < currentPosition + decelerationZone)
                {                    
                    change = change / 15;
                    pause = 50;
                }  
                if(targetPosition < currentPosition + (decelerationZone * 5))
                {                    
                    change = change / 10;
                    pause = 40;
                }                  
                else if(targetPosition < currentPosition + (decelerationZone * 10))
                {
                    change = change / 8;
                    pause = 30;
                }
                else if(targetPosition < currentPosition + (decelerationZone * 15))
                {
                    change = change / 6;
                    pause = 20;
                }
                else if(targetPosition < currentPosition + (decelerationZone * 20))
                {
                    change = change / 4;
                    pause = 10;
                } 
                else if(targetPosition < currentPosition + (decelerationZone * 30))
                {              
                    change = change / 2;
                }    
                 
                if(targetPosition - currentPosition < change)
                {
                    change = targetPosition - currentPosition;
                }
                
                if(change < 1)
                {
                    change = 1;
                }
                
            }    
           
            sliderPanel.style.left = (currentPosition + change) + "px";
            
            setTimeout("PerformScrolling('" + sliderPanelId + "', '" + inProgressId + "', '" + targetValueId + "', '" + selectedItemId + "', " + itemWidth + ", " + extraScroll + ");", pause);
            
        }
        else
        {            
            inProgress.value = "0";
            selectedItem.value = targetValue.value;
        }
    }
}

function SelectAdditionalElement(elementId, defaultClassName)
{
    var element = document.getElementById(elementId);
    if(element != null)
    {
        var className = GetElementClassName(elementId);
        if(className.indexOf("Selected") == -1)
        {
            var browser = GetBrowser();
            if(browser != "IE")
            {
                element.setAttribute("class", className + "Selected");
            }
            else
            {
                element.setAttribute("className", className + "Selected");
            }
        }    
    }
}

function SelectAdditionalElements(elements, defaultClassName)
{
    if((elements != null) && (elements.length > 0))
    {
        for(var x = 0; x < elements.length; x++)
        {
            SelectAdditionalElement(elements[x], defaultClassName);
        }
    }
}

function DeSelectAdditionalElement(elementId, defaultClassName)
{
    var element = document.getElementById(elementId);
    if(element != null)
    {
        var className = GetElementClassName(elementId);
        if(className.indexOf("Selected") != -1)
        {
            var browser = GetBrowser();
            if(browser != "IE")
            {
                element.setAttribute("class", className.substring(0, className.length - 8));
            }
            else
            {
                element.setAttribute("className", className.substring(0, className.length - 8));
            }
        }    
    }
}

function DeSelectAdditionalElements(elements, defaultClassName)
{
    if((elements != null) && (elements.length > 0))
    {
        for(var x = 0; x < elements.length; x++)
        {
            DeSelectAdditionalElement(elements[x], defaultClassName);
        }
    }
}

function verticalCentreModalPopup(modalPopupBehaviour, panelDivClientId, safariWidth)
{
    var modalPopupObj = $find(modalPopupBehaviour);
    modalPopupObj.show();
    
    var browser = GetBrowser();
    if(browser == 'OPERA')
    {         
        var sheight = window.innerHeight / 2;
        var panelDiv = document.getElementById(panelDivClientId);
        sheight = sheight - (panelDiv.offsetHeight / 2);        
        panelDiv.style.top = sheight + 'px';
    }  
    if(browser == 'SAFARI')
    {  
        var panelDiv = document.getElementById(panelDivClientId);
        panelDiv.style.width = safariWidth + 'px';
        
        var sWidth = window.innerWidth / 2;
        sWidth = sWidth - (panelDiv.offsetWidth / 2);        
        panelDiv.style.left = sWidth + 'px';             
    }    
}

function ApplyBackgroundToElement(elementId, imageUrl)
{    
    var element = document.getElementById(elementId);
    if(element != null)
    {        
        element.style.backgroundImage = "url('" + imageUrl + "')";
    }
}

function getIFObj(obj)
{
      var objRef; 
      
//      objRef = $find(obj);

      if(document.all)
      {
            objRef = eval("document.all." + obj);
      }
      
      if(document.getElementById)
      {
            objRef = document.getElementById(obj);
      }

      return objRef;
}
