﻿function currency(selectedCurrency, numberOfItems)
{
    var Pounds = null;
    var UsDollars = null;
    var Euros = null;        
        
    for (i=1;i<=numberOfItems;i=i+1)
    {
        try
        {
            Pounds = document.getElementById('currencyPoundSterling' + i);
            UsDollars = document.getElementById('currencyUsDollars' + i);
            Euros = document.getElementById('currencyEuros' + i);
        }
        catch(err)
        {
            // Do Nothing.                
        }
        
        // Determine which price to show based on the value of selectedCurrency.                   
        if(Pounds || UsDollars || Euros)
        {    
        if (selectedCurrency != "poundSterling")
            {   
                Pounds.style.display = "none";
            }
        else
            {
                Pounds.style.display = "block";
                
            }
                

        if (selectedCurrency != "usDollars")
            {
                UsDollars.style.display = "none";
                

                
            }
        else
            {
                UsDollars.style.display = "block";
                
            }

            
        if (selectedCurrency != "euros")
            { Euros.style.display = "none"; }
        else
            { Euros.style.display = "block"; }
            

        }
        setCookie("currencyChoice",selectedCurrency,365);
    }
    
        
        // BasketCurrencyDisplay is only created when items are databound in the content list.
        // It is injected onto the page at runtime.
        try
        {
            BasketCurrencyDisplay(selectedCurrency);
        }
        catch(err)
        {
            // Do Nothing.                
        }
    
    // Handle User clicking a currency flag if there are no currency values displayed on the page.
    if (!Pounds || !UsDollars || !Euros)
    {
        setCookie("currencyChoice",selectedCurrency,365);
    }
}

// Handle cookies.
// Set cookie.
function setCookie(cookieName,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=cookieName+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+ ";path=/";
}

// Check if the cookie exists.
// If it doesn, pass the value of the cookie to the currency function,
// otherwise use the default value of poundSterling
function checkCookie(cookieName, numberOfItems)
{
    currencyChoice=getCurrencyChoice();
    currency(currencyChoice, numberOfItems);
}

// Get cookie.
function getCookie(cookieName)
{
    if (document.cookie.length>0)
    {
        var c_start=document.cookie.indexOf(cookieName + "=");
        if (c_start!=-1)
        {
            c_start=c_start + cookieName.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 "";
        
}

function getCurrencyChoice()
{
    currencyChoice=getCookie("currencyChoice");

    if (currencyChoice==null || currencyChoice=="")
    {
        currencyChoice = "poundSterling";
    }
    return currencyChoice;
}

// View All Page and Intro To Sailing currency selectors.

function currencyViewAll(selectedCurrency)
{
    var Pounds = null;
    var UsDollars = null;
    var Euros = null;     
        
        try
        {
            Pounds = document.getElementById('viewAllGBP');
            UsDollars = document.getElementById('viewAllUSD');
            Euros = document.getElementById('viewAllEur');
        }
        catch(err)
        {
            // Do Nothing.                
        }
        
        // Determine which price to show based on the value of selectedCurrency.                   
        if(Pounds || UsDollars || Euros)
        {    
        if (selectedCurrency != "poundSterling")
            { Pounds.style.display = "none"; }
        else
            { Pounds.style.display = "block"; }
                

        if (selectedCurrency != "usDollars")
            {
                UsDollars.style.display = "none";
                

                
                // Intro to sailing Page Buttons.
                try
                {
                    // Set link depending on currency selected.
                    if(selectedCurrency == "poundSterling")
                    {
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").href = "/buySailingDVD.aspx";
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").target = "_self";
                    }
                    else
                    {
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").href = "javascript:analyticsCall('http://www.amazon.co.uk/Introduction-To-Sailing/dp/B00477HIRA/ref=sr_1_7?ie=UTF8&qid=1288691867&sr=1-7');";
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").target = "_self";
                    }
                    // Show button.
                    document.getElementById("ctl00_MainContent_cmdBuyDVD").style.visibility = 'visible';
                }
                catch(err)
                {
                    // Do Nothing.
                }
                
                // View All Page Buttons.
                try
                {
                    // Set link depending on currency selected.
                    if(selectedCurrency == "poundSterling")
                    {
                        document.getElementById("ctl00_MainContent_cmdBuyDVDIntroToSailing").href = "/buySailingDVD.aspx";
                        document.getElementById("ctl00_MainContent_cmdBuyDVDIntroToSailing").target = "_self";
                        document.getElementById("ctl00_MainContent_cmdBuyDVDLearningToSailSingle").href = "/buySailingDVD.aspx";
                        document.getElementById("ctl00_MainContent_cmdBuyDVDLearningToSailSingle").target = "_self";
                        document.getElementById("ctl00_MainContent_cmdBuyDVDLearnToSailTwo").href = "/buySailingDVD.aspx";
                        document.getElementById("ctl00_MainContent_cmdBuyDVDLearnToSailTwo").target = "_self";
                    }
                    else
                    {
                        document.getElementById("ctl00_MainContent_cmdBuyDVDIntroToSailing").href = "javascript:analyticsCall('http://www.amazon.co.uk/Introduction-To-Sailing/dp/B00477HIRA/ref=sr_1_7?ie=UTF8&qid=1288691867&sr=1-7');"; 
                        document.getElementById("ctl00_MainContent_cmdBuyDVDIntroToSailing").target = "_self";              
                        document.getElementById("ctl00_MainContent_cmdBuyDVDLearningToSailSingle").href = "javascript:analyticsCall('http://www.amazon.co.uk/Learn-To-Sail-Single-Handed/dp/B00477R1BS/ref=sr_1_4?ie=UTF8&qid=1288691867&sr=1-4');";
                        document.getElementById("ctl00_MainContent_cmdBuyDVDLearningToSailSingle").target = "_self";
                        document.getElementById("ctl00_MainContent_cmdBuyDVDLearnToSailTwo").href = "javascript:analyticsCall('http://www.amazon.co.uk/Learn-to-Sail-Two-Handed/dp/B00477JPPS/ref=sr_1_3?ie=UTF8&qid=1288691867&sr=1-3');";
                        document.getElementById("ctl00_MainContent_cmdBuyDVDLearnToSailTwo").target = "_self";
                    }
                    // Show button.
                    document.getElementById("ctl00_MainContent_cmdBuyDVDIntroToSailing").style.visibility = 'visible';
                    document.getElementById("ctl00_MainContent_cmdBuyDVDLearningToSailSingle").style.visibility = 'visible';
                    document.getElementById("ctl00_MainContent_cmdBuyDVDLearnToSailTwo").style.visibility = 'visible';
                }
                catch(err)
                {
                    // Do Nothing.
                }
            }
        else
            {
                UsDollars.style.display = "block";
                
                // Intro to sailing Page Buttons.
                try
                {
                    document.getElementById("ctl00_MainContent_cmdBuyDVD").style.visibility = 'hidden';
                }
                catch(err)
                {
                    // Do Nothing.
                }
                
                // View All Page Buttons.
                try
                {
                    document.getElementById("ctl00_MainContent_cmdBuyDVDIntroToSailing").style.visibility = 'hidden';
                    document.getElementById("ctl00_MainContent_cmdBuyDVDLearningToSailSingle").style.visibility = 'hidden';
                    document.getElementById("ctl00_MainContent_cmdBuyDVDLearnToSailTwo").style.visibility = 'hidden';
                }
                catch(err)
                {
                    // Do Nothing.
                }
            }

            
        if (selectedCurrency != "euros")
            { Euros.style.display = "none"; }
        else
            {
                Euros.style.display = "block";
            }
            

        }
}


// Check if the cookie exists.
// If it doesn, pass the value of the cookie to the currency function,
// otherwise use the default value of poundSterling
function checkCookieViewAll(cookieName)
{
    currencyChoice=getCookie(cookieName);

    if (currencyChoice!=null && currencyChoice!="")
    {
        currencyViewAll(currencyChoice);
    }
    else
    {
        // Default currency if a cookie has not been set.
        currencyViewAll("poundSterling");
    }
}


// Learn to Sail Single.
function currencyViewAllSingle(selectedCurrency)
{
    var Pounds = null;
    var UsDollars = null;
    var Euros = null;        
        
        try
        {
            Pounds = document.getElementById('viewAllGBPSingle');
            UsDollars = document.getElementById('viewAllUSDSingle');
            Euros = document.getElementById('viewAllEurSingle');
        }
        catch(err)
        {
            // Do Nothing.                
        }
        
        // Determine which price to show based on the value of selectedCurrency.                   
        if(Pounds || UsDollars || Euros)
        {    
        if (selectedCurrency != "poundSterling")
            { Pounds.style.display = "none"; }
        else
            { Pounds.style.display = "block"; }
                

        if (selectedCurrency != "usDollars")
            {
                UsDollars.style.display = "none";
                
                try
                {
                    // Set link depending on currency selected.
                    if(selectedCurrency == "poundSterling")
                    {
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").href = "/buySailingDVD.aspx";
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").target = "_self";
                    }
                    else
                    {
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").href = "javascript:analyticsCall('http://www.amazon.co.uk/Learn-To-Sail-Single-Handed/dp/B00477R1BS/ref=sr_1_4?ie=UTF8&qid=1288691867&sr=1-4');";
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").target = "_self";
                    }
                    // Show button.
                    document.getElementById("ctl00_MainContent_cmdBuyDVD").style.visibility = 'visible';
                }
                catch(err)
                {
                    // Do Nothing.
                }
            }
        else
            {
                UsDollars.style.display = "block";
                
                try
                {
                    document.getElementById("ctl00_MainContent_cmdBuyDVD").style.visibility = 'hidden';
                }
                catch(err)
                {
                    // Do Nothing.
                }
            }

            
        if (selectedCurrency != "euros")
            { Euros.style.display = "none"; }
        else
            { Euros.style.display = "block"; }
            
        }
}

// Check if the cookie exists.
// If it doesn, pass the value of the cookie to the currency function,
// otherwise use the default value of poundSterling
function checkCookieViewAllSingle(cookieName)
{
    currencyChoice=getCookie(cookieName);

    if (currencyChoice!=null && currencyChoice!="")
    {
        currencyViewAllSingle(currencyChoice);
    }
    else
    {
        // Default currency if a cookie has not been set.
        currencyViewAllSingle("poundSterling");
    }
}

// Learn to Sail Two.
function currencyViewAllTwoHanded(selectedCurrency)
{
    var Pounds = null;
    var UsDollars = null;
    var Euros = null;        
        
        try
        {
            Pounds = document.getElementById('viewAllGBPTwoHanded');
            UsDollars = document.getElementById('viewAllUSDTwoHanded');
            Euros = document.getElementById('viewAllEurTwoHanded');
        }
        catch(err)
        {
            // Do Nothing.                
        }
        
        // Determine which price to show based on the value of selectedCurrency.                   
        if(Pounds || UsDollars || Euros)
        {    
        if (selectedCurrency != "poundSterling")
            { Pounds.style.display = "none"; }
        else
            { Pounds.style.display = "block"; }
                

        if (selectedCurrency != "usDollars")
            {
                UsDollars.style.display = "none";
                
                try
                {
                    // Set link depending on currency selected.
                    if(selectedCurrency == "poundSterling")
                    {
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").href = "/buySailingDVD.aspx";
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").target = "_self";
                    }
                    else
                    {                    
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").href = "javascript:analyticsCall('http://www.amazon.co.uk/Learn-to-Sail-Two-Handed/dp/B00477JPPS/ref=sr_1_3?ie=UTF8&qid=1288691867&sr=1-3');";
                        document.getElementById("ctl00_MainContent_cmdBuyDVD").target = "_self";
                    }
                    // Show button.
                    document.getElementById("ctl00_MainContent_cmdBuyDVD").style.visibility = 'visible';
                }
                catch(err)
                {
                    // Do Nothing.
                }
            }
        else
            {
                UsDollars.style.display = "block";
                
                try
                {
                    document.getElementById("ctl00_MainContent_cmdBuyDVD").style.visibility = 'hidden';
                }
                catch(err)
                {
                    // Do Nothing.
                }
            }

            
        if (selectedCurrency != "euros")
            { Euros.style.display = "none"; }
        else
            { Euros.style.display = "block"; }
        }
}

// Check if the cookie exists.
// If it doesn, pass the value of the cookie to the currency function,
// otherwise use the default value of poundSterling
function checkCookieViewAllTwoHanded(cookieName)
{
    currencyChoice=getCookie(cookieName);

    if (currencyChoice!=null && currencyChoice!="")
    {
        currencyViewAllTwoHanded(currencyChoice);
    }
    else
    {
        // Default currency if a cookie has not been set.
        currencyViewAllTwoHanded("poundSterling");
    }
}

// Homepage currency selector.

function currencyHomePage(selectedCurrency)
{
    var Pounds = null;
    var UsDollars = null;
    var Euros = null;        
        
        try
        {
            Pounds = document.getElementById('homePageCurrencyTextGBP');
            UsDollars = document.getElementById('homePageCurrencyTextUSD');
            Euros = document.getElementById('homePageCurrencyTextEUR');
        }
        catch(err)
        {
            // Do Nothing.                
        }
        
        // Determine which price to show based on the value of selectedCurrency.                   
        if(Pounds || UsDollars || Euros)
        {    
        if (selectedCurrency != "poundSterling")
            { Pounds.style.display = "none"; }
        else
            { Pounds.style.display = "inline"; }
                

        if (selectedCurrency != "usDollars")
            { UsDollars.style.display = "none"; }
        else
            { UsDollars.style.display = "inline"; }

            
        if (selectedCurrency != "euros")
            { Euros.style.display = "none"; }
        else
            { Euros.style.display = "inline"; }
            

        }
        /*setCookie("currencyChoice",selectedCurrency,365);      */
}

// Check if the cookie exists.
// If it doesn, pass the value of the cookie to the currency function,
// otherwise use the default value of poundSterling
function checkCookieHomePage(cookieName)
{
    currencyChoice=getCookie(cookieName);

    if (currencyChoice!=null && currencyChoice!="")
    {
        currencyHomePage(currencyChoice);
    }
    else
    {
        // Default currency if a cookie has not been set.
        currencyHomePage("poundSterling");
    }
}

// Google Analytics Call
// Used to register a click to google anayltics when the user clicks a 'buy dvd' button.
function analyticsCall(amazonURL)
{

    pageTracker._trackPageview("/AmazonRedirects.html");
    
    window.open(amazonURL);
  
}
