function showAlert() {
    alert('test');
}

//if checks adds the value to cookie, if unchecked removes the value from cooki
function manageCheckboxvalues(elem, cookieName) {
    var checkbox = elem;
    var cookieValue = readCookie(cookieName);
    if (checkbox.checked) {
        if (cookieValue != null) {
            createCookie(cookieName, cookieValue + checkbox.value);
        }
        else {
            createCookie(cookieName, checkbox.value);
        }
    }
    else {
        if (cookieValue.indexOf(checkbox.value) >= 0) {
            var checkBoxValueRemovedCookieString = ReplaceAll(cookieValue, checkbox.value, ' ');
            createCookie(cookieName, checkBoxValueRemovedCookieString);
        }
    }
}

//Remove value from cookie
function removeValuefromCookie(cookieName, valueToBeRemoved) {
    valueToBeRemoved = ReplaceAll(valueToBeRemoved, "+", " ");
    var cookieValue = readCookie(cookieName);
    var checkBoxValueRemovedCookieString = ReplaceAll(cookieValue, valueToBeRemoved, ' ');
    createCookie(cookieName, checkBoxValueRemovedCookieString);
}

//redirect to product comparison page
function redirectToProductComparison(elem, cookieName, errLabelId, comparisonUrl) {
    var errLabel = document.getElementById(errLabelId);
    var productsToCompare = 0;
    if (readCookie(cookieName) != null) {
        productsToCompare += readCookie(cookieName).toString().split(',').length - 1;
    }

    var link = elem;
    if (productsToCompare < 2 && errLabel != null) {
        document.getElementById(errLabelId).innerHTML = "Less than 2 items have been selected for comparison";
        link.href = "#";
    }
    else {
        link.href = comparisonUrl;
    }
}

//creates the cookie
function createCookie(name, value) {
    var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

//reads the cookie value
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;
}

//removes the cookie with specified name
function eraseCookie(name) {
    createCookie(name, "");
}

//function that goes through the cookie and check the values
function parseCookieAndCheckComparisonBoxes(name) {
    var cookieValue = readCookie(name);
    for (var index = 0; index < document.forms[0].comparison.length; index++) {
        var currentcheckbox = document.forms[0].comparison[index];
        if (cookieValue != null) {
            if (cookieValue.indexOf(currentcheckbox.value) >= 0) {
                document.forms[0].comparison[index].checked = true;
            } 
        }
    }
}

//replace all funcction
function ReplaceAll(Source, stringToFind, stringToReplace) {
    var temp = Source;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }

    return temp;

}
 
