﻿
$(document).ready(function() {

$('#northamerica').click(function() {
        var link = $(this).attr("href")
        //Set the cookie according to the text in the link
        createCookie('location', link, 30);
    });

    $('#europe').click(function() {

        var link = $(this).attr("href")
        //Set the cookie according to the text in the link
        createCookie('location', link, 30);
    });

});

function redirectPage() {
    //for debugging purpose, so that you can see what is in the menu cookie   
   // $('#debug').html('Cookie Content : ' + readCookie('location'));

    //if cookie menu exists   
    if (readCookie('location')) {

        window.location = readCookie('location');
    }
}

function createCookie(name, value, days) {
    /*if (document.cookie != document.cookie) {
        index = document.cookie.indexOf(name);
    }
    else {
        index = -1;
    }

    if (index = -1) {
        document.cookie = name + "=" + value + "; path=/";
    }*/

    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else {
        var expires = ""
    };
    
    document.cookie = name + "=" + value + expires + "; path=/";

}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(";");
    for (var i = 0; i < ca.length; i++) 
    {
        var c = ca[i];
        while (c.charAt(0) == ' ') 
        {
            c = c.substring(1, c.length);
        }

        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}
