﻿// JScript File
// Manage listboxes between differents pages (self.parent.opener...)

    function SetDescription(pDestinationListId, pDescriptionValue)
    {
        if (window.dialogArguments) 
        {
            window.opener = window.dialogArguments;
            // Load the items from the previous list : sport, competition...
            if (self.parent.opener != null && self.parent.opener.document.forms[0] != null)
            {
                self.parent.opener.frames[pDestinationListId].document.body.innerHTML =
                    document.getElementById(pDescriptionValue).value;
                window.close();
            }
        }
    }

    function SetCheckBoxValue(pDestinationId, pCheckBoxValue)
    {
        if (window.dialogArguments) 
        {
            window.opener = window.dialogArguments;
            // Load the items from the previous list : sport, competition...
            if (self.parent.opener != null && self.parent.opener.document.forms[0] != null)
            {
                self.parent.opener.document.getElementById(pDestinationId).checked =
                    pCheckBoxValue;
                window.close();
            }
        }
    }
    
    function SetTextBoxValue(pDestinationId, pTextBoxValue)
    {
        if (window.dialogArguments) 
        {
            window.opener = window.dialogArguments;
            // Load the items from the previous list : sport, competition...
            if (self.parent.opener != null && self.parent.opener.document.forms[0] != null)
            {
                self.parent.opener.document.getElementById(pDestinationId).value =
                    pTextBoxValue;
                self.parent.opener.document.getElementById(pDestinationId).readOnly = false;    
                window.close();
            }
        }
    }
    
    function SetSelectedValueListSingle(pDestinationListId, pTypeClassificationElementListId)
    {
       // Load the items from the previous list : sport, competition...
        if (self.parent.opener != null && self.parent.opener.document.forms[0] != null)
        {
            var vLength = self.parent.opener.document.getElementById(pTypeClassificationElementListId).options.length;
            var i = 0;
            for (i = 0; i < vLength; i++)
            {
                // Add the selected item
                document.getElementById(pDestinationListId).options[i].selected = self.parent.opener.document.getElementById(pTypeClassificationElementListId).options[i].selected;
            }
        } 
    }
    
    //Returns the list of values contained (not only selected) by the list in one string
    //separeted by comas.
    function GetListContentValueAsString (pListId){
        var vList = document.getElementById (pListId);
        if (vList != null){
            var vResult = '';
            
            var vLength = vList.options.length;
            var i = 0;
            for (i = 0; i < vLength; i++)
            {
                var vValue = vList.options[i].value;
                
                if (vResult != '') {
                    vResult = vResult + ',';
                }
                
                vResult = vResult + vValue;
            }            
            
            return vResult;
        }
        else {
            return '?list not found?';
        }
    }
    
    function InitListSingle(pDestinationListId, pTypeClassificationElementListId)
    {
        // Load the items from the previous list : sport, competition...
        if (self.parent.opener != null && self.parent.opener.document.forms[0] != null)
        {
            var vLength = self.parent.opener.document.getElementById(pTypeClassificationElementListId).options.length;
            var i = 0;
            for (i = 0; i < vLength; i++)
            {
                var vSelectedId = self.parent.opener.document.getElementById(pTypeClassificationElementListId).options[i].value;
                var vSelectedValue =  self.parent.opener.document.getElementById(pTypeClassificationElementListId).options[i].text;
        	    var vSelectedListLength = 0;
	            if(document.getElementById(pDestinationListId).options != null)
	            {
		            vSelectedListLength = document.getElementById(pDestinationListId).options.length;
	            }
	            else
	            {
		            vSelectedListLength = 0;
	            }
                // Add the selected item
                document.getElementById(pDestinationListId).options[vSelectedListLength] = new Option(vSelectedValue, vSelectedId);
            }
        }
    }
    
    function InitListSingleModal(pDestinationListId, pTypeClassificationElementListId)
    {
        // Load the items from the previous list : sport, competition...
        // IE only - other browsers will be fine
        if (window.dialogArguments) 
        {
            window.opener = window.dialogArguments;
        
        if (window.opener != null && window.opener.document.forms[0] != null)
        {
            var vLength = window.opener.document.getElementById(pTypeClassificationElementListId).options.length;
            var i = 0;
            for (i = 0; i < vLength; i++)
            {
                var vSelectedId = window.opener.document.getElementById(pTypeClassificationElementListId).options[i].value;
                var vSelectedValue =  window.opener.document.getElementById(pTypeClassificationElementListId).options[i].text;
        	    var vSelectedListLength = 0;
	            if(document.getElementById(pDestinationListId).options != null)
	            {
		            vSelectedListLength = document.getElementById(pDestinationListId).options.length;
	            }
	            else
	            {
		            vSelectedListLength = 0;
	            }
                // Add the selected item
                document.getElementById(pDestinationListId).options[vSelectedListLength] = new Option(vSelectedValue, vSelectedId);
            }
        }
        }
    }
    
    function InitList(pOriginListId, pDestinationListId, pTypeClassificationElementListId)
    {
        // Load the items from the previous list : sport, competition...
        if (self.parent.opener != null && self.parent.opener.document.forms[0] != null)
        {
            var vLength = self.parent.opener.document.getElementById(pTypeClassificationElementListId).options.length;
            var i = 0;
            for (i = 0; i < vLength; i++)
            {
                var vSelectedId = self.parent.opener.document.getElementById(pTypeClassificationElementListId).options[i].value;
                var vSelectedValue =  self.parent.opener.document.getElementById(pTypeClassificationElementListId).options[i].text;
                
	        	var vSelectedListLength = 0;
		        if(document.getElementById(pDestinationListId).options != null)
		        {
			        vSelectedListLength = document.getElementById(pDestinationListId).options.length;
		        }
		        else
		        {
			        vSelectedListLength = 0;
		        }
                // Add the selected item
                document.getElementById(pDestinationListId).options[vSelectedListLength] = new Option(vSelectedValue, vSelectedId);
            }
        }
        
        // Load only the non selected items in the source list
        // : synchronize the source and the destination lists
        var vLength = document.getElementById(pDestinationListId).options.length;
        var l = 0;
        for (l = 0; l < vLength; l++)
        {
            var vSelectedId0 = document.getElementById(pDestinationListId).options[l].value;
            var vLengthSource0 = document.getElementById(pOriginListId).options.length;
            
           
            var j = 0;
            for (j = 0; j < vLengthSource0; j++)
            {
                if(document.getElementById(pOriginListId).options[j] != null && document.getElementById(pOriginListId).options[j].value == vSelectedId0)
                {
                    //si l'élément doit être en rouge
                    var vSelectedColor0 = document.getElementById(pOriginListId).options[j].style.color;
                    if (vSelectedColor0 != null)
                    {
                        document.getElementById(pDestinationListId).options[l].style.color = vSelectedColor0;
                    }
                    // Remove the item from the source list
                    document.getElementById(pOriginListId).options[j] = null;
                }
            }
        }

        return false;
    }
    
    
    function RefreshParent(pDestinationListId, pTypeClassificationElementListId)
    {
        self.parent.opener.ValidateForm(document.getElementById(pDestinationListId), pTypeClassificationElementListId);
        window.close();
    }
    
    function RefreshParentAndSelect(pDestinationListId, pTypeClassificationElementListId)
    {
        self.parent.opener.ValidateForm(document.getElementById(pDestinationListId), pTypeClassificationElementListId);
        self.parent.opener.SetAllSelectedValue(pTypeClassificationElementListId);
        window.close();
    }
    
    function SetSelectedValue(pOriginListId, pDestinationListId, pMessageForNoSelection)
    {
        if(document.getElementById(pOriginListId).selectedIndex > -1)
        {
            var vIndex = document.getElementById(pOriginListId).selectedIndex;
            var vSelectedId = document.getElementById(pOriginListId).options[vIndex].value;
            var vSelectedValue =  document.getElementById(pOriginListId).options[vIndex].text;
            var vSelectedColor = document.getElementById(pOriginListId).options[vIndex].style.color;
            
            var vSelectedListLength = document.getElementById(pDestinationListId).options.length;
            // Add the selected item
            document.getElementById(pDestinationListId).options[vSelectedListLength] = new Option(vSelectedValue, vSelectedId);
            if (vSelectedColor != null)
            {
                 document.getElementById(pDestinationListId).options[vSelectedListLength].style.color = vSelectedColor;
            }
            
            // Remove the item from the origin list
            document.getElementById(pOriginListId).options[vIndex] = null;
        }
        else
        {
            alert(pMessageForNoSelection);
        }
        
        //sort destination list
        ListSort(pDestinationListId);
        //alert('DEBUG 04 APRES');                

        return false;
    }
    
    function SetAllSelectedItems(pOriginListId, pDestinationListId)
    {
        var vLength = document.getElementById(pOriginListId).options.length;
        var i = 0;
        for (i = 0; i < vLength; i++)
        {
            var vSelectedId = document.getElementById(pOriginListId).options[i].value;
            var vSelectedValue =  document.getElementById(pOriginListId).options[i].text;
            var vSelectedColor = document.getElementById(pOriginListId).options[i].style.color;
            var vSelectedListLength = document.getElementById(pDestinationListId).options.length;
            // Add the selected item
            document.getElementById(pDestinationListId).options[vSelectedListLength] = new Option(vSelectedValue, vSelectedId);
            if (vSelectedColor != null)
            {
                 document.getElementById(pDestinationListId).options[vSelectedListLength].style.color = vSelectedColor;
            }
        }
        // Remove the item from the origin list
        document.getElementById(pOriginListId).options.length = 0;
        
        //alert('DEBUG 02');
        //sort destination list
        ListSort(pDestinationListId);

        return false;
    }
    
    function RemoveAllSelectedItems(pOriginListId, pDestinationListId)
    {
        var vLength = document.getElementById(pDestinationListId).options.length;
        var i = 0;
        for (i = 0; i < vLength; i++)
        {
            var vSelectedId = document.getElementById(pDestinationListId).options[i].value;
            var vSelectedValue =  document.getElementById(pDestinationListId).options[i].text;
            var vSelectedColor = document.getElementById(pDestinationListId).options[i].style.color;
            var vSourceListLength = document.getElementById(pOriginListId).options.length;
            // Add the selected item to the origin list
            document.getElementById(pOriginListId).options[vSourceListLength] = new Option(vSelectedValue, vSelectedId);
            if (vSelectedColor != null)
            {
                 document.getElementById(pOriginListId).options[vSourceListLength].style.color = vSelectedColor;
            }
        }
        // Remove the item from the origin list
        document.getElementById(pDestinationListId).options.length = 0;
        
       // alert('DEBUG 01');
        //sort origin list
        ListSort(pOriginListId);

        return false;
    }
    
    function RemoveSelectedValue(pOriginListId, pDestinationListId, pMessageForNoSelection)
    {
        if(document.getElementById(pDestinationListId).selectedIndex > -1)
        {
            var vIndex = document.getElementById(pDestinationListId).selectedIndex;
            var vSelectedId = document.getElementById(pDestinationListId).options[vIndex].value;
            var vSelectedValue =  document.getElementById(pDestinationListId).options[vIndex].text;
            var vSelectedColor = document.getElementById(pDestinationListId).options[vIndex].style.color;
            var vSourceListLength = document.getElementById(pOriginListId).options.length;
            // Add the selected item to the origin list
            document.getElementById(pOriginListId).options[vSourceListLength] = new Option(vSelectedValue, vSelectedId);
            if (vSelectedColor != null)
            {
                 document.getElementById(pOriginListId).options[vSourceListLength].style.color = vSelectedColor;
            }
            // Remove the item from the origin list
            document.getElementById(pDestinationListId).options[vIndex] = null;
        }
        else
        {
            alert(pMessageForNoSelection);
        }
        
        //alert('DEBUG 03');
        //sort origin list
        ListSort(pOriginListId);
        
        return false;
    }
    
    function ValidateForm(pDestinationListId, pTypeClassificationElementListId)
    {
        // Load the items from the previous list : sport, competition...
        var vLength = pDestinationListId.options.length;
        document.getElementById(pTypeClassificationElementListId).options.length = 0;
        var i = 0;
        for (i = 0; i < vLength; i++)
        {
            var vSelectedId = pDestinationListId.options[i].value;
            var vSelectedValue =  pDestinationListId.options[i].text;
        	var vSelectedListLength = 0;
	        if(document.getElementById(pTypeClassificationElementListId).options != null)
	        {
	        
		        vSelectedListLength = document.getElementById(pTypeClassificationElementListId).options.length;
	        }
	        else
	        {
		        vSelectedListLength = 0;
	        }
            // Add the selected item
            document.getElementById(pTypeClassificationElementListId).options[vSelectedListLength] = new Option(vSelectedValue, vSelectedId);
        }
        return false;
    }
    
    function CheckSingleMode(pDestinationList, pTypeClassificationElementListId)
    {
        var vSelectedListLength = document.getElementById(pDestinationList).options.length;
		if(vSelectedListLength >= 2)
		{
		    alert('Only one item is allowed.');
		}
		else
		{
		    RefreshParent(pDestinationList, pTypeClassificationElementListId);
		}
		return false;
    }




// Compare two options within a list by TEXT
function compareOptionText(a, b) 
{ 
    var s1 = a.text;
    var s2 = b.text;
    
    if (s1 == s2) {
      return 0;
    }
    if (s1 > s2) {
      return 1;
    }
    return -1;
}

//quick sort array
function qsort(a) {
    if (a.length == 0) return new Array();

    var left = new Array();
    var right = new Array();
    var pivot = a[0];
    for (var i = 1; i < a.length; i++) {
        if (a[i].text < pivot.text)
            left.push(a[i]);
        else
            right.push(a[i]);
    }

    return qsort(left).concat(pivot, qsort(right));
}

	function Quicksort(vec, loBound, hiBound)
	/**************************************************************
		This function adapted from the algorithm given in:
			Data Abstractions & Structures Using C++, by
			Mark Headington and David Riley, pg. 586.

		Quicksort is the fastest array sorting routine for
		unordered arrays.  Its big O is n log n.
	 **************************************************************/
	{

		var pivot, loSwap, hiSwap, temp;

		// Two items to sort
		if (hiBound - loBound == 1)
		{
			if (vec[loBound].text > vec[hiBound].text)
			{
				temp = vec[loBound];
				vec[loBound] = vec[hiBound];
				vec[hiBound] = temp;
			}
			return;
		}

		// Three or more items to sort
		pivot = vec[parseInt((loBound + hiBound) / 2)];
		vec[parseInt((loBound + hiBound) / 2)] = vec[loBound];
		vec[loBound] = pivot;
		loSwap = loBound + 1;
		hiSwap = hiBound;

		do {
			// Find the right loSwap
			while (loSwap <= hiSwap && vec[loSwap].text <= pivot.text)
				loSwap++;

			// Find the right hiSwap
			while (vec[hiSwap].text > pivot.text)
				hiSwap--;

			// Swap values if loSwap is less than hiSwap
			if (loSwap < hiSwap)
			{
				temp = vec[loSwap];
				vec[loSwap] = vec[hiSwap];
				vec[hiSwap] = temp;
			}
		} while (loSwap < hiSwap);

		vec[loBound] = vec[hiSwap];
		vec[hiSwap] = pivot;


		// Recursively call function...  the beauty of quicksort

		// 2 or more items in first section		
		if (loBound < hiSwap - 1)
			Quicksort(vec, loBound, hiSwap - 1);


		// 2 or more items in second section
		if (hiSwap + 1 < hiBound)
			Quicksort(vec, hiSwap + 1, hiBound);
	}
	
//Sorts any list by TEXT 
function ListSort (pListToSortId){
    var vList = document.getElementById (pListToSortId);
    
    if (vList != null && vList.options.length < 500){
        //temp array to sort items
        var vListValues = new Array (vList.options.length);
        
        //fills array with options
        var len = 0;
        for( len = 0; len < vList.options.length; len++ ) 
        {
            if ( vList.options[ len ] != null )
            {
                vListValues[ len ] = new Option( vList.options[ len ].text, vList.options[ len ].value, vList.options[ len ].defaultSelected, vList.options[ len ].selected );
                vListValues[ len ].style.color=vList.options[ len ].style.color;
            }
        }
        //sort list values
        Quicksort (vListValues, 0, vListValues.length-1);
        
        // Populate the destination with the items from the new array
        //VERY LONG !!
        for ( var j = 0; j < vListValues.length; j++ ) 
        {
            if ( vListValues[ j ] != null  && vList.options[ j ] != vListValues[ j ])
            {
                vList.options[ j ] = vListValues[ j ];
            }
        }
    }
}



