﻿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)
{
      $("#" + String(blockId)).remove();         
      ArrayDelete(array,blockId);
}

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)
        {
            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)
        {
            return data.replace( new RegExp(replace,"g"), replacewith );                
        }