// Copyright 1999-2006, e-vita as.
// Revision Date: 2006-01-13

function listAddElement(toList, txt, val)
{
  toList.options[toList.length] = new Option (txt, val, false, true);
}

function checkIfElementInList(toList, txt, val)
{
	for (var i=0; i<toList.length; i++)
	{
		if (toList.options[i].value == val)
			return true;
	}
	return false;
	
}

function listSwapElements(e1, e2)
{
  var ttext = e1.text;
  var tvalue = e1.value;
  e1.text = e2.text;
  e1.value = e2.value;
  e2.text = ttext;
  e2.value = tvalue;
}
 
function listMoveElementUp(toList)
{
	var i;
    // go through the list and get all selected items -- except the top one
    for (i = 1; i <= toList.length-1; i++) 
    {
        // if the item is selected then swap it
        if (toList.options[i].selected)                     
        {
            listSwapElements(toList.options[i], toList.options[i - 1]);
            toList.options[i - 1].selected = true;
            toList.options[i].selected = false;
        }
    }
}

function listMoveElementDown(toList)
{
	var i;
    // go through the list and get all selected items
    for ( i = toList.length-1; i >= 0; i--) 
    {
        // if the item is selected then swap it
        if (toList.options[i].selected)                     
        {
            // check if it is not the first item
            if (i != toList.length-1)
            {
                listSwapElements(toList.options[i], toList.options[i + 1]);
                toList.options[i + 1].selected = true;
                toList.options[i].selected = false;
            }                        
        }
    }
}
function listMoveElementTop(toList)
{
    // get the first item selected which needs to move to top
    var iSelected = toList.selectedIndex;
    if (iSelected == 0)
        return;
    
    var iMoveTop;  
    // now run the moveup loop   
    for ( iMoveTop = 1; iMoveTop <= iSelected; iMoveTop++)
        listMoveElementUp(toList);
}

function listMoveElementBottom(toList)
{
	var i;
	var iSelected;
	var iMoveDown;
    // get the last item selected which needs to move to bottom
    for ( i = 0; i <= toList.length-1; i++) 
    {
        // if the item is selected then swap it
        if (toList.options[i].selected)                     
            iSelected = i;
    }
    
    if (iSelected == toList.length-1)  
        return;
                 
    iSelected = toList.length - 1 - iSelected;

    // now run the movedown loop   
    for ( iMoveDown = 1; iMoveDown <= iSelected; iMoveDown++)
        listMoveElementDown(toList);
}

function listRemoveElement (toList)
{
	var i;
    var count = 0;
	
    for (i=0; i<toList.length; i++) 
    {
        if (toList.options[i].selected)
        {
            ++count;
            continue;
        }

        if (count > 0)
        {
            toList.options[i-count].text  = toList.options[i].text;
            toList.options[i-count].value = toList.options[i].value;
        }
    }
    
    toList.length = toList.length - count;        
}

function listSelectAll(fromList)
{
	var i;
    for ( i = 0; i <= fromList.length-1; i++ )
        fromList.options[i].selected = true;
    return true;
}

function listUnSelectAll(fromList)
{
	var i;
    for ( i = 0; i <= fromList.length-1; i++ )
        fromList.options[i].selected = false;
    return true;
}

function listClearList(fromList)
{
    fromList.length = 0;
}

function listCopyToList(fromList, toList, direction)
{
	var i;
	var val;
    for ( i = 0; i <= fromList.length-1;)
    {
        if (fromList.options[i].selected)
        {
            val = fromList.options[i].value;
            if ( val == "0:0:0" ) { return;}
            if ( val != "" )
            {
                // check if value is a spacer
                if ( val != "spacer" )
                {
                    if ( direction == "left" )
                    {
                        // remove from right and do not add on left
                        fromList.options[i]= null;
                    }   
                    else
                    {
                        // add to right but do not remove from left
                        fromList.options[i].selected = false;
                    }
                }
                else
                { 
                    //only increment when not moving and deleting
                    // create a new row
                    // added these lines
                    // removes from fromList and unselects item in toList
                    fromList.options[i]= null;
                }  //only increment when not moving and deleting
            }
        }
        else
            i++;  //only increment when not moving and deleting
    }
    listDeleteBlankRowIfNotEmpty(fromList);
}

function listCopyAll(fromList, toList, direction)
{
    var indexofspacer = -1;
    var spacerval = "";
    var spacertxt = "";
    var indexofitem = toList.length;
    var i;
    var txt;
    var val;
    for ( i = 0; i <= fromList.length-1; i++ )
    {
        txt = fromList.options[i].text;
        txt = document.addportletdlg.p_providerid[document.addportletdlg.p_providerid.selectedIndex].text+":"+txt;
        val = fromList.options[i].value;
        if ( val != "" )
        { 
            if(direction != "left")
            {
                // check if we need to copy the spacer too
                if ( val != "0:0:0")
                {
                    toList.options[indexofitem] = new Option( txt, val, false, true );
                    toList.options[indexofitem].selected = false;
                    indexofitem++;
                }
            }
        }
    }
    
    if (indexofspacer != -1 && direction == "right" ) // let the spacer be on the from list
        fromList.length = 1;
    else
        if(direction == "left")
            listClearList(fromList);

    listDeleteBlankRowIfNotEmpty(toList);
    listUnSelectAll(toList);
}

function listDeleteBlankRowIfNotEmpty(toList)
{
    var idx = -1;
    var val = "";
    var i;
    
    // find a blank row in table 
    for (i = 0; i < toList.length; i++)
    { 
        val = toList.options[i].value;
        if (val == "")
        {
            idx = i;
            break;
        }
    }
    
    if (idx >= 0 && (toList.length > 1))
        toList.options[idx] = null;
}

// iKnowBase SelectList functions
function AddElementsToiKBSelectList(iframeID, maxCount, maxCountStr)
{
	if (iframeID == "")
		var file = evitaGetElementById("ViewerlinkFileName");
	else
		var file = evitaGetElementById(iframeID).contentWindow.evitaGetElementById("ViewerlinkFileName");

    if (file == undefined)
        AddMultipleToiKBSelectList(iframeID, maxCount, maxCountStr);
    else
    {
	var id = evitaGetElementById(iframeID).contentWindow.evitaGetElementById("ViewerlinkId").value;
	var title = evitaGetElementById(iframeID).contentWindow.evitaGetElementById("ViewerlinkTitle").value;
	AddOneToiKBSelectList(id, title, maxCount, maxCountStr);
    }
}

function AddOneToiKBSelectList(id, title, maxCount, maxCountStr)
{
    var numInList = evitaGetElementById ("selectlist").length;
   if (numInList < maxCount)
    {
        var check = false;
        for (var i=0; i<evitaGetElementById ("selectlist").length; i++)
        {
            if (evitaGetElementById ("selectlist").options[i].value == id)
                check = true;
        }
	
        if (check == false)
        {
            listAddElement(evitaGetElementById ("selectlist"), title, id);
        }
        else
        {
            //str = "<bean:message key="dimensionNavigator.msg.exists" locale='userLocale' />";
            //alert(str);
        }
    }
    else
    { 
	  str = maxCountStr + " " + maxCount + "."
	  alert(str); 
    }
}

function AddMultipleToiKBSelectList(iframeID, maxCount, maxCountStr)
{	
	var userAgent = navigator.userAgent.toLowerCase();
	if ((userAgent.indexOf("msie") != -1) || (userAgent.indexOf("opera") != -1)) 
      {
		// IE
		if (iframeID == "")
		{
			var cbs = document.getElementsByName("p_picklist_id");
    			var txt = document.getElementsByName("p_picklist_title");
		}
		else
		{
			var cbs = evitaGetElementById(iframeID).contentWindow.document.getElementsByName("p_picklist_id");
    			var txt = evitaGetElementById(iframeID).contentWindow.document.getElementsByName("p_picklist_title");
		}
		for (i=0 ; i<cbs.length; i++)
    		{
        		if (cbs.item(i).checked)
        		{
            		var cbs1 = cbs.item(i).value.split("##");
		            var txt1 = txt.item(i).value.split("##");
            		for (j=0;j<cbs1.length;j++)
            		{
		                AddOneToiKBSelectList(cbs1[j], txt1[j], maxCount, maxCountStr);
            		}
        		}
    		}
	}
     	else
	{
		// Firefox
		var docLen = document.getElementById(iframeID).contentWindow.document.all.length;
		cbsArray = new Array();
		txtArray = new Array();
		for (k=0; k<docLen; k++)
		{
			if (document.getElementById(iframeID).contentWindow.document.all[k].id == "p_picklist_id")
			{
				cbsArray.push(document.getElementById(iframeID).contentWindow.document.all[k]);
			}
			else if (document.getElementById(iframeID).contentWindow.document.all[k].id == "p_picklist_title")
			{
				txtArray.push(document.getElementById(iframeID).contentWindow.document.all[k]);

			}
		}
		for (k=0; k<cbsArray.length; k++)
		{
			if (cbsArray[k].checked)
			{
				AddOneToiKBSelectList(cbsArray[k].value, txtArray[k].value, maxCount, maxCountStr);
			}
		}
	}
    
}

function listCallback (cbFunction, cbClosure)
{
    var list = evitaGetElementById('selectlist');

	var list_ids = new Array();
	var list_labels = new Array();
	var list_subattribs = new Array();

	// TODO: This mechanism of passing subAttribute-info MUST change!
	// We pass an array with one element for each set of subattributes
	// Each element is a TEXTUAL description of the subattributes for a single item
	// Thus, three items {a,b,c} where a and c have subattributes, will give a 
	// subattr-list with two items, containing subattrs for a and c respectiveliy

	var itemCount = 0;
	var subCount = 0;

	for (i=0; i<list.length; i++)
	{
		if (list[i].value.length == 0)
			continue;

		list_ids[itemCount] = list[i].value;
		list_labels[itemCount] = list[i].text;
		++itemCount;
		/*
		if (subAttributes[list[i].value])
		{
			list_subattribs[subCount] = subAttributes[list[i].value];
			++subCount;
		}*/
	}
		
	var runThis = "parent.opener." + cbFunction+"(parent, cbClosure, list_ids, list_labels, list_subattribs);";
	eval(runThis);
}