﻿function createCookie(name, value, days) {
    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 + "=" + escape(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;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function nextStep(id, step, user_form_tn, catalog_name, stepVal) {
    var currentWrapper = jQuery('#step_' + step);
    jQuery(currentWrapper).removeClass('confOn');
    jQuery(currentWrapper).next().addClass('confOn');

    if (step == 1) {
        eraseCookie("tmpSelectedProperties");
        eraseCookie("selectedProperties");
        eraseCookie("confStepVal");
        createCookie("confStepVal", "", 1);
    }

    var propertyId = id.split('_')[1];

    createCookie("confStep", step, 1);

    stepVal = stepVal.replace("ž", "z");
    stepVal = stepVal.replace("š", "s");
    stepVal = stepVal.replace("č", "c");
    stepVal = stepVal.replace(/\u2019/g, "'");

    var value = new String("");

    if (readCookie("confStepVal") != null) {
        value = value + readCookie("confStepVal") + stepVal + '%3B';
    }
    else
        value = value + stepVal + '%3B';

    createCookie("confStepVal", value, 1);

    value = value.replace("'", "\'");

    if (step == 1)
        value = value + ';;;';
    else if (step == 2)
        value = value + ';;';
    else if (step == 3)
        value = value + ';';
    else if (step == 4)
        _gaq.push(['_trackEvent', 'configurator', 'configurator', catalog_name + ' | ' + unescape(value), 1]);

    if (readCookie("selectedProperties") != null) {
        // string concatenation with '+' not working in Safari
        //createCookie("selectedProperties", [readCookie("selectedProperties"), propertyId].join(","));
        createCookie("selectedProperties", [readCookie("selectedProperties"), propertyId].join(","));
    }
    else {
        //createCookie("selectedProperties", propertyId);
        createCookie("selectedProperties", propertyId);
    }
   
    var selectedValues = readCookie('selectedProperties');
    var currentCatalogId = readCookie('currentCatalogId');


    if (user_form_tn) {

        if (selectedValues != null) {
            //selectedValues = selectedValues.substr(0, selectedValues.length - 1);
            selectedValues = selectedValues.substr(0, selectedValues.length);
        }
        else
            selectedValues = '';
        window.location = user_form_tn + "?selection=" + arrayExtractNegative(selectedValues) + "&type=" + currentCatalogId;
    }
    else {
        return false;
    }
}

function nextStep2(id, step, user_form_tn, catalog_name, stepVal) {
    var currentSelection = null;
    var arrSelectedValues;
    
    if (step == 1) {
        eraseCookie("tmpSelectedProperties");
        eraseCookie("selectedProperties");
        eraseCookie("confStepVal");
        createCookie("confStepVal", "", 1);
    }
    

    var propertyId = id.split('_')[1];

    createCookie("confStep", step, 1);

    stepVal = stepVal.replace("ž", "z");
    stepVal = stepVal.replace("š", "s");
    stepVal = stepVal.replace("č", "c");
    stepVal = stepVal.replace(/\u2019/g, "'");

    var value = new String("");

    if (readCookie("confStepVal") != null) {
        value = value + readCookie("confStepVal") + stepVal + '%3B';
    }
    else
        value = value + stepVal + '%3B';

    createCookie("confStepVal", value, 1);

    value = value.replace("'", "\'");

    if (step == 1)
        value = value + ';;;';
    else if (step == 2)
        value = value + ';;';
    else if (step == 3)
        value = value + ';';
    else if (step == 4)
        _gaq.push(['_trackEvent', 'configurator', 'configurator', catalog_name + ' | ' + unescape(value), 1]);

    if (readCookie("selectedProperties") != null) {
        currentSelection = readCookie("selectedProperties");
        arrSelectedValues = currentSelection.split(",");

        if (arrSelectedValues[step - 1] != null) {
            arrSelectedValues[step - 1] = propertyId;
        }
        else {
            arrSelectedValues.push(propertyId);
        }
        createCookie("selectedProperties", arrSelectedValues.join(","));
    }
    else {
        createCookie("selectedProperties", propertyId);
    }

    var selectedValues = readCookie('selectedProperties');
    var currentCatalogId = readCookie('currentCatalogId');
	
	var refConfig = readCookie('refConfig');
    var srcConfig = readCookie('srcConfig');


    if (user_form_tn) {

        if (selectedValues != null) {
            //selectedValues = selectedValues.substr(0, selectedValues.length - 1);
            selectedValues = selectedValues.substr(0, selectedValues.length);
        }
        else
            selectedValues = '';
        window.location = user_form_tn + "?selection=" + arrayExtractNegative(selectedValues) + "&type=" + currentCatalogId+ "&referrer=" + refConfig+ "&src=" + srcConfig;
    }
    else {
        var stepResult = step + 1;
        if (propertyId == 13 || propertyId == 14) {
            stepResult = stepResult + 1;

            currentSelection = readCookie("selectedProperties");
            arrSelectedValues = currentSelection.split(",");
            if (arrSelectedValues[stepResult - 2] != null) {
                arrSelectedValues[stepResult - 2] = 16;
            }
            else {
                arrSelectedValues.push(16);
            }
            createCookie("selectedProperties", arrSelectedValues.join(","));
        }
        else if (propertyId == 25) {
            stepResult = stepResult + 1;

            currentSelection = readCookie("selectedProperties");
            arrSelectedValues = currentSelection.split(",");
            if (arrSelectedValues[stepResult - 2] != null) {
                arrSelectedValues[stepResult - 2] = 30;
            }
            else {
                arrSelectedValues.push(30);
            }
            createCookie("selectedProperties", arrSelectedValues.join(","));
        }

        window.location = "?tn=configuration" + "&step=" + stepResult + "&c=" + currentCatalogId + "&selectedValues=" + arrayExtractNegative(selectedValues);
    }
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function arrayExtractNegative(str) {
    var resultValue = new Array();
    var splitValue = str.split(",");
    for (i = 0; i < splitValue.length; i++) {
        //if (splitValue[i] == -1) {
        //    splitValue.splice(i, 1);
        //}
        if (splitValue[i] != -1) {
            resultValue.push(splitValue[i]);
        }
    }
    return resultValue.join(",");
    //return splitValue.join(",");
}

function arrayRemoveLast(str) {
    var splitValue = str.split(",");
    splitValue.splice(splitValue.length - 1, 1);
    return splitValue.join(",");
}

function prevStep(step) {
    var currentWrapper = jQuery('#step_' + step);
    jQuery(currentWrapper).removeClass('confOn');
    jQuery(currentWrapper).prev().addClass('confOn');

    createCookie("selectedProperties", arrayRemoveLast(readCookie("selectedProperties")));
    return false;
}

function prevStep2(step) {
    //var currentWrapper = jQuery('#step_' + step);
    //jQuery(currentWrapper).removeClass('confOn');
    //jQuery(currentWrapper).prev().addClass('confOn');

    //createCookie("selectedProperties", arrayRemoveLast(readCookie("selectedProperties")));
    //return false;

    var selectedValues = readCookie('selectedProperties');
    var currentCatalogId = readCookie('currentCatalogId');

    var arrSelectedValues = selectedValues.split(",");
    var poppedValue = arrSelectedValues.pop();

    selectedValues = arrSelectedValues.join(",");
    createCookie("selectedProperties", selectedValues);

    if (step == 1) {
        window.location = "?tn=configuration&c=" + currentCatalogId + "&test=oa";
    }

    else {
        if (poppedValue == 16) {
            selectedValues = readCookie('selectedProperties');
            arrSelectedValues = selectedValues.split(",");
            arrSelectedValues.pop();
            selectedValues = arrSelectedValues.join(",");
            createCookie("selectedProperties", selectedValues);
            //window.location = "?tn=configuration" + "&step=2&c=" + currentCatalogId + "&selectedValues=" + arrayExtractNegative(selectedValues) + "&test=oa"; 
            window.location = "?tn=configuration" + "&step=2&c=" + currentCatalogId + "&selectedValues=" + arrayExtractNegative(selectedValues); 
        }
        else {
            //window.location = "?tn=configuration" + "&step=" + step + "&c=" + currentCatalogId + "&selectedValues=" + arrayExtractNegative(selectedValues) + "&test=oa"; 
            window.location = "?tn=configuration" + "&step=" + step + "&c=" + currentCatalogId + "&selectedValues=" + arrayExtractNegative(selectedValues); 
        }
    }
}
