﻿function GroupToggle(blockId) {
    var blockDiv = document.getElementById(blockId + '_products');
    var groupHeaderDiv = document.getElementById(blockId + '_showhide');

    if (blockDiv.style.display == "none") { blockDiv.style.display = "block"; groupHeaderDiv.innerHTML = '<img alt="-" src="graphics/minus.gif" />&nbsp;&nbsp;'; }
    else { blockDiv.style.display = "none"; groupHeaderDiv.innerHTML = '<img alt="+" src="graphics/plus.gif" />&nbsp;&nbsp;'; }
}



function BlockToggle(blockId) {
    var blockDiv = document.getElementById(blockId + '_products');
    if (blockDiv.style.display == "none") { blockDiv.style.display = "block"; }
    else { blockDiv.style.display = "none"; }
}

//---| Flyttar upp en div ett steg i en föräldradiv med andra divar |-----------------
//blockID = ID till diven
//parentId = ID till förälderdiven
//array = den array som håller datat
function BlockMove_Up(blockId, parentId, array) {
    var blockDiv = document.getElementById(blockId);
    var parentDiv = document.getElementById(parentId);
    if (blockDiv !== null && parentDiv !== null) {
        var sibling = previousSiblingElement(blockDiv);
        if (sibling !== null && sibling.id.length > 0) {
            blockDiv.parentNode.removeChild(blockDiv);
            parentDiv.insertBefore(blockDiv, sibling);
            ArraySwap(array, blockDiv.id, sibling.id);
        }
    }
}

function ArraySwap(array, id1, id2) {
    var element1, element2, temp;

    for (i in array) {
        if (array[i][0] == id1) element1 = i;
        if (array[i][0] == id2) element2 = i;
    }

    temp = array[element1];
    array[element1] = array[element2];
    array[element2] = temp;
}

function ArrayDelete(array, id) {
    for (i in array) {
        if (array[i][0] == id) {
            array.splice(i, 1);
            break;
        }
    }
}


function BlockMove_Down(blockId, parentId, array) {
    var blockDiv = document.getElementById(blockId);
    var parentDiv = document.getElementById(parentId);
    if (blockDiv !== null && parentDiv !== null) {
        var firstsibling = nextSiblingElement(blockDiv);
        if (firstsibling !== null && firstsibling.id.length > 0) {
            var secondsibling = nextSiblingElement(firstsibling);
            blockDiv.parentNode.removeChild(blockDiv);
            if (secondsibling !== null) {
                parentDiv.insertBefore(blockDiv, secondsibling);
                ArraySwap(array, blockDiv.id, firstsibling.id);
            }
            else {
                parentDiv.appendChild(blockDiv);
                ArraySwap(array, blockDiv.id, firstsibling.id);
            }
        }
    }
}

function BlockDelete(blockId, parentId, array, joinedProducts, confirmText) {
    try {
        if (confirmText != null) {
            if (confirm(confirmText)) {
                //okidoki
            }
            else
                return false;
        }

        if (joinedProducts != null) {
            //if it is a product, then check for joined product-functionality
            var id = parseInt(blockId.replace(/product/, ""));
            if (id != "NaN") {
                for (i in array) {
                    //find product
                    if (array[i][2] != id)
                        continue;

                    //is product joined with something?
                    if (array[i][17] < 0)
                        continue;

                    //find joinedProduct
                    for (j in joinedProducts) {

                        if (joinedProducts[j].id != array[i][17])
                            continue;

                        joinedProducts[j].remove(document, joinedProducts, array);
                    }

                    break;
                }
            }
        }
    }
    catch (err) {
    }

    $("#" + String(blockId)).remove();
    ArrayDelete(array, blockId);

    try {
        if (parentId.length > 0)
            UpdateTrolley(array.length, "");
    }
    catch (ee) { }
}

function AddGroup() {
    var originalGroup = parent.document.getElementById('groupDivToCopy');
    var newGroup = originalGroup.cloneNode(true);
    var l = groups.length;

    newGroup.setAttribute('class', 'groups');
    newGroup.setAttribute('id', 'group' + l);

    newGroup.innerHTML = newGroup.innerHTML.replace(/groupDivToCopy/g, 'group' + l);

    document.getElementById('productSort').appendChild(newGroup);

    groups[l] = new Array('group' + l, l, 'Ny grupp', '0', 'false');

    //Måste initiera thickbox igen för att få med den nyss tillagda gruppen
    tb_init('a.thickbox, area.thickbox, input.thickbox');
}

function nextSiblingElement(node) {
    if (node !== null) {
        var sibling = node.nextSibling;
        if (sibling !== null && sibling.nodeType == 1) {
            return sibling;
        } else if (sibling !== null) {
            while ((sibling = sibling.nextSibling) !== null) {
                if (sibling.nodeType == 1) {
                    return sibling;
                }
            }
        }
    }

    return null;
}

function previousSiblingElement(node) {
    if (node !== null) {
        var sibling = node.previousSibling;
        if (sibling !== null && sibling.nodeType == 1) {
            return sibling;
        } else if (sibling !== null) {
            while ((sibling = sibling.previousSibling) !== null) {
                if (sibling.nodeType == 1) {
                    return sibling;
                }
            }
        }
    }

    return null;
}


function getxmlhttpobject() {
    var xmlhttp = false;
    /*@cc_on@*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    @end@*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

function filterData(indata) {
    if ((parseInt(indata) != "NaN") && (parseInt(indata) == indata))
        return indata;

    indata = replaceAll(indata, '&', 'oOo');
    indata = replaceAll(indata, '>', 'gGg');
    indata = replaceAll(indata, '<', 'lLl');
    indata = replaceAll(indata, '\"', 'qQq');
    indata = replaceAll(indata, '\'', 'aAa');

    return indata;
}


function unfilterData(indata) {
    if ((parseInt(indata) != "NaN") && (parseInt(indata) == indata))
        return indata;

    indata = replaceAll(indata, 'oOo', '&');
    indata = replaceAll(indata, 'gGg', '>');
    indata = replaceAll(indata, 'lLl', '<');
    indata = replaceAll(indata, 'qQq', '\"');
    indata = replaceAll(indata, 'aAa', '\'');

    return indata;
}


function replaceAll(data, replace, replacewith) {
    try {
        return data.replace(new RegExp(replace, "g"), replacewith);
    }
    catch (err) {
        alert(data + "\n" + err);
        return data;
    }
}

//requires products-array
function getNextCountInArray(products) {
    var id = -1;
    var tmpId = 0;

    for (i in products) {

        tmpId = parseInt(products[i][2]);

        if (id < tmpId)
            id = tmpId;
    }

    return id + 1;
}

