﻿function replaceCurrentAnchor(withAnchor) {
    // strip existing anchor with link
    var currentUrl = window.location.href;
    
    var anchorIndex = currentUrl.indexOf('#');
   
    var hrefWithoutAnchor;
    if (anchorIndex > -1) {
        hrefWithoutAnchor = currentUrl.substr(0, anchorIndex);
    } else {
        hrefWithoutAnchor = currentUrl;
    }

    location.href = hrefWithoutAnchor + "#" + withAnchor;   
}
function addCurrencyToUrl(currencyCode) {
    var url = location.href;
    
    url = url.replace("cur=GBP", "");
    url = url.replace("cur=USD", "");
    url = url.replace("cur=EUR", "");
    
    if (url.indexOf("?") > -1) {
        url = url.replace("?", "?cur=" + currencyCode + "&");
    } else {
        url = url + "/?cur=" + currencyCode;
    }
    location.href = url;

}
function redirectAnchorUrl() {
    var anchorIndex = location.href.indexOf('#');
    if(anchorIndex == -1)
        return;

    var anchor = location.href.substr(anchorIndex);
    var url = anchor.substr(1);

    var urlValidator = new RegExp("\/[\w\.?=%&=\-@/$,]*"); 
    if (url.match(urlValidator)) {
        location.href = url;
    }
}


String.prototype.reverse = function() {
    splitext = this.split("");
    revertext = splitext.reverse();
    reversed = revertext.join("");
    return reversed;
}

redirectAnchorUrl();
