function getShoppingCartSummary() {
    var mainElement;
    var summaryElement;
    mainElement = document.getElementById('display_cart_summary');
    if(mainElement == null) {
        return null;
    }
    return mainElement.innerHTML;   
}
function isShoppingCartEmpty() {
    var contents;
    contents = getShoppingCartSummary();
    if(contents == null) {
        return null;
    }
    if(contents.search('(Your shopping cart is empty)') != -1) {
        return true;
    }
    return false;
}
function getShoppingCartItems() {
    var summary;
    var result;
    summary = getShoppingCartSummary();
    if(summary == null) {
        return null;
    }
    result = summary.match(/\d+/);
    if(result == null) {
        return null;
    }
    return result[0];
}

function getShoppingCartTotal() {
    var summary;
    var result1;
    var result2;
    var output;
    summary = getShoppingCartSummary();
    if(summary == null) {
        return null;
    }
    result1 = summary.match(/total\scost\sof\s.*\)/);
    result2 = summary.match(/priced\sat\s.*\)/);
    if(result1 == null && result2 == null) {
        return null;
    }
    if(result1 != null) {
        if(result1[0] == null || result1[0].length <= 15) {
            return null;
        }
        output = result1[0].substr(14, result1[0].length-15);
    }
    else {
        if(result2[0] == null || result2[0].length <= 11) {
            return null;
        }   
        output = result2[0].substr(10, result2[0].length-11);
    }
    return output;
}
function updateHeaderCartSummary() {
    var element;
    var items;
    var output;
    
    element = document.getElementById('view_cart_text_right');
    
    if(element == null) {
        return;
    } 
    items = getShoppingCartItems();
    total = getShoppingCartTotal();
    if(isShoppingCartEmpty()) {
        output = 'Cart is empty';
    }
    else if (items == null || total == null) {
        return;
    }
    else if(items == 0) {
        output = 'Cart is empty';
    }
    else {
        output = items + ' item';
        if (items > 1) {
            output = output + 's';
        }
        output = output;
    }
    element.innerHTML = output;
}
function continueShopping() {
    var referrer = null;
    var referrerPage = null;
    var defaultPage = '/';
    var currentPage = window.location.pathname;
    var currentHost = window.location.host;
    var productURL = null;
    if(document.referrer) {
        referrer = document.referrer;
    }
    try {
        productURL = getProductHistoryURL();                
    } catch(e) {
        productURL = null;
    }
    defaultPage = (productURL == null ? defaultPage : productURL);
    if(referrer == null || referrer == '') {
        window.location.href = defaultPage;
        return;
    }
    try {
        hostPosition = referrer.indexOf(currentHost)
        if(hostPosition == -1) {
            window.location.href = defaultPage;
            return; 
        }
        referrerPage = referrer.substring( (hostPosition + currentHost.length));
        if(referrerPage.toLowerCase() == currentPage.toLowerCase()) {
            window.location.href = defaultPage;
            return;         
        }
        window.location.href = referrerPage;
        return;
    }
    catch(e) {
        window.location.href = defaultPage;
        return;             
    }
}
function getProductHistoryURL() {
    var cookieName = '%2FHistory';
    var cookies = null;
    var cookiePackage = null;
    var productCode = null;
    var productCodes = null;
    
    if(!document.cookie || document.cookie == null) {
        return null;
    }
    cookies = document.cookie.split(';');
    for(index = 0; index < cookies.length; index++) {
        cookiePackage = cookies[index];
        while(cookiePackage.charAt(0) == ' ') {
            cookiePackage = cookiePackage.substring(1, cookiePackage.length);
        }            
        if(cookiePackage.indexOf(cookieName + '=') == 0) {
            productCode = cookiePackage.substring(cookieName.length + 1);
            if(productCode == null || productCode == '') {
                return null;
            }
            productCodes = productCode.split('%2C');
            return '/ProductDetails.asp?ProductCode=' + productCodes[0];
        }
    }
    return null;
}
