﻿//Enumerated
var Pounds = 0;
var Dollars = 1;
var Euros = 2;

// This function can be used by redirect pages
// It is, however, very dangerous to use JavaScript for redirects,
// as Google can penalise sites very heavily.
//
//function redirectPage() {
//    if(typeof(EditorClasses) == 'undefined')
//    {
//        var redirectLocation = getMetaTagText('RedirectLocation');
//        if(redirectLocation.length>0)
//        {
//            window.location=redirectLocation;
//        }
//    }
//}

// This is a function for returning the value from a specific
// meta tag
function getMetaTagText (metaTagName) {
    var metaElements = document.all ?
        document.all.tags('META') :
        document.getElementsByTagName ?
        document.getElementsByTagName ('META') : new Array();
    var metaKeywords = '';
    var i = 0;
    for (var m = 0; m < metaElements.length; m++)
        if (metaElements[m].name == metaTagName)
        {
            metaKeywords = metaElements[m].content;
        }
    return metaKeywords;
}


//These functions are used by the currency Switcher

var nowOnload = window.onload; // Let's save the existing assignment, if any
window.onload = function () {
    // You can call as many functions as you want here;
    setCombos();

    // Now we call old function which was assigned to onLoad, thus playing nice
    if(nowOnload != null && typeof(nowOnload) == 'function') {
        nowOnload();
    }
}

function setCombos() {
    if (document.forms['frmMain']!=null && document.forms['frmMain'].currency!=null) {
        document.forms['frmMain'].currency.selectedIndex=currencyIndex(currentCurrency(document.location));
    }
}

function switchPage(selection) {
    document.location = newCurrencyPage(document.location, newCurrency(selection.selectedIndex));
}

function newCurrencyPage(currentLocation, newCurrency) {
    var urlPrefix = (currentCurrency(currentLocation)==Pounds)
        ? "../"
        : "../";
    return urlPrefix + currencyUrl(newCurrency);
}

function currencyUrl(currency) {
    switch (currency) {
        case Pounds:
            return "pounds/";
            break;
        case Dollars:
            return "dollars/";
            break;
        case 2:
            return "euros/";
            break;
        default:
            return ".";
    }
}

function currencyIndex(currency) {
    switch (currency) {
    case Pounds:
        return 0;
        break;
    case Dollars:
        return 1;
        break;
    case Euros:
        return 2;
        break;
    default:
        return 0;
    }
}

function newCurrency(comboIndex) {
    switch (comboIndex) {
    case 0:
        return Pounds;
        break;
    case 1:
        return Dollars;
        break;
    case 2:
        return Euros;
        break;
    default:
        return Pounds;
    }
}

function currentCurrency(currentLocation) {
    if (checkEndOfString(currentLocation.toString(), "/dollars/")) {
        return Dollars;
    } else if (checkEndOfString(currentLocation.toString(), "/euros/")) {
        return Euros;
    } else {
        return Pounds;
    }
}

function checkEndOfString(checkString, endToCheck) {
//Returns true if the end of _string_ matches _endToCheck_
    return (checkString.substr(checkString.length - endToCheck.length).toLowerCase()==endToCheck.toLowerCase());
}