﻿
function replaceQueryString(url, param, value) {
    if (param == "catid") {
        if (value == "17" || value == "18") {
            url = replaceQueryString(url, 'sortindex', '20');
        }
        else {
            url = replaceQueryString(url, 'sortindex', '0');
        }
        url = replaceQueryString(url, 'subcatid', '0');
        url = replaceQueryString(url, 'page', '1');
    }

    if (param == "subcatid") {
        url = replaceQueryString(url, 'page', '1');
    }

    if (url.indexOf("style=") == -1) {
        var tmpCat = getQueryStringVar("catid", "0");
        var tmpSubCat = getQueryStringVar("subcatid", "0");
        url = 'view-styles.aspx?style=&catid=0&subcatid=0&pricelow=0&pricehigh=0&decoid=0&colorid=0&fabid=0&useid=0&desid=0&page=1&pagesize=10&sortindex=0'
        if (tmpCat != "0")
            url = replaceQueryString(url, 'catid', tmpCat);
        if (tmpSubCat != "0")
            url = replaceQueryString(url, 'subcatid', tmpSubCat);
    }

    var re = new RegExp("([?|&])" + param + "=.*?(&|$)", "i");
    var theStr = "";
    if (url.match(re))
        theStr = url.replace(re, '$1' + param + "=" + value + '$2');
    else if (url.indexOf("?") == -1)
        theStr = url + '?' + param + "=" + value;
    else
        theStr = url + '&' + param + "=" + value;

    return theStr;
}

function getQueryStringVar(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
}

function replacePricing(url, fLow, fHigh) {

    url = replaceQueryString(url, 'pricelow', fLow);
    url = replaceQueryString(url, 'pricehigh', fHigh);

    return url;

}


// The Cookie Jar
////// Standard functions for Setting, Getting, and Deleting Cookies
// To set a cookie pass in the name, value, and persist type
// SetCookie("name", "value", 1) set the persist type to 1 to store your cookie
// or to 0 to last for this browser session only. By default the storage time is for one year.
// You can add another argument to hold the length of time you want to store the cookie:
// SetCookie("name", "value",1,15768000000) would store the cookie for 6 months.
// the time is calcutaled in miliseconds so for one month of milliseconds you calculate:
// 31 * 24 * 60 * 60 * 1000=2678400000 1 month
// 1 * 24 * 60 * 60 * 1000=86400000    1 day
// 60 * 60 * 1000 = 1 hr
// 1 day = 1 (day) * 24 (hours per day) * 60 (minutes per hour) * 60 (seconds per minute) * 1000 (milliseconds per second) = 86400000 milliseconds
// To get or delete a cookie simply pass in the name of the cookie you want to effect
// GetCookie("name") DeleteCookie("name")
// if you ask for a cookie that does not exist it will return null.

function getCookieVal(offset) {
    endstr = document.cookie.indexOf(";", offset)
    if (endstr == -1) endstr = document.cookie.length
    return unescape(document.cookie.substring(offset, endstr))
}

function GetCookie(name) {
    arg = name + "="
    alen = arg.length
    clen = document.cookie.length
    var i = 0
    while (i < clen) {
        j = i + alen
        if (document.cookie.substring(i, j) == arg) return getCookieVal(j)
        i = document.cookie.indexOf(" ", i) + 1
        if (i == 0) break
    }
    return null
}

function SetCookie(name, value, per, exp) {
    cstr = name + "=" + escape(value) + ";"
    if (per) {
        addtime = (exp > 0) ? exp : 31536000000
        expdate = new Date()
        expdate.setTime(expdate.getTime() + addtime)
        expdate = expdate.toGMTString()
        cstr += " expires=" + expdate
        cstr += '; path=/';

    }
    document.cookie = cstr
}

function DeleteCookie(name) {
    exp = new Date()
    exp.setTime(exp.getTime() - 1)
    cval = GetCookie(name)
    if (cval != null)
        document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + '; path=/';
}

function checkSupportForCookies() {
    
    SetCookie('testcookie', '1', 1);
    if (GetCookie('testcookie')) {
        DeleteCookie('checkCookie');
        return true;
    }
    else {
        return false;
    }
    
}

function updateProductPortfolio(cookie, value) {
    if (GetCookie(cookie) == null) {
        SetCookie(cookie, value, 1);   // no cookie so add it
    }
    else {
        var theCookie = GetCookie(cookie);  // found cookie so parse it
        var idx = theCookie.indexOf(value);
        if (idx == -1) { //id not in cookie
            var theCookieArray = theCookie.split(',');
            var theCookieArrayLen = theCookieArray.length;
            if (theCookieArrayLen > 10) {
                theCookieArray.shift();
                theCookieArray.push(value);
            }
            else {
                theCookieArray.push(value);
            }
            SetCookie(cookie, theCookieArray.toString(), 1)
        }
    }
}

function removeProductPortfolio(cookie) 
{
    var cnt = 0;
    var txt = "";
    removeItem = document.getElementsByName("chkRemoveProduct");
    for (i = 0; i < removeItem.length; ++i) 
    {
        if (removeItem[i].checked) 
        {
            if (GetCookie(cookie) == null) 
            {
                // skip remove
            }
            else 
            {
                    var theCookie = GetCookie(cookie);  // found cookie so parse it
                    var theCookieArray = theCookie.split(',');
                    var idx = theCookieArray.indexOf(removeItem[i].value);
                    if (idx == -1) 
                    { //id not in cookie

                    }
                    else {

                        theCookieArray.splice(idx, 1)
                        SetCookie(cookie, theCookieArray.toString(), 1)
                        window.location.reload();
                        
                    }
            }
         
        }
    }

}


