﻿// JScript File

// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  This function moves options between select boxes. Works best with
//  multi-select boxes to create the common Windows control effect.
//  Passes all selected values from the first object to the second
//  object and re-sorts each box.
//  If a third argument of 'false' is passed, then the lists are not
//  sorted after the move.
//  If a fourth string argument is passed, this will function as a
//  Regular Expression to match against the TEXT or the options. If 
//  the text of an option matches the pattern, it will NOT be moved.
//  It will be treated as an unmoveable option.
//  You can also put this into the <SELECT> object as follows:
//    onDblClick="moveSelectedOptions(this,this.form.target)
//  This way, when the user double-clicks on a value in one box, it
//  will be transferred to the other (in browsers that support the 
//  onDblClick() event handler).
// -------------------------------------------------------------------
function moveSelectedOptions(from,to,autoSort) {


	// Unselect matching options, if required
	/*
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
		*/
	// Move them over
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) 
	{
	    sortSelect(to);
    }
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	
	//disable and then re-enable the <SELECT> element, to force a screen redraw
	//[fixes FogBugz Case #776] (Cristos L-C on 2006-06-06)
	from.disabled = true;
	from.disabled = false;
	}
	
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {

	var o = new Array();

	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value) ;
		}
	if (o.length==0) { return; }
	
	o = o.sort(sortOptions);
    
	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value);
		}
	}


// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}

function sortOptions(a,b)
{
    if ((a.text+"") < (b.text+"")) { return -1; }
	if ((a.text+"") > (b.text+"")) { return 1; }
	return 0;
}			
		
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a 
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before 
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions(obj) {
	if (!hasOptions(obj)) 
	{ 
	    return; 
	}
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
		}
	}	
	
function getAllLists(obj1, obj2, obj3, benchCount) {
    if(obj1.options.length > benchCount)
    {
        alert("Up to " + benchCount + " Benchmarks can be selected.");
        return false;
    }
    if(obj1 != null)
        selectAllOptions(obj1);
    if(obj2 != null)
        selectAllOptions(obj2);
    if(obj3 != null)
        selectAllOptions(obj3);
}	

function getAllLists2(obj1, obj2, obj3) {
    if(obj1 != null)
        selectAllOptions2(obj1);
    if(obj2 != null)
        selectAllOptions2(obj2);
    if(obj3 != null)
        selectAllOptions2(obj3);
}	

function selectAllOptions2(obj) 
{
    var element = null;
    element = document.getElementById(obj);
	if (element != null)
	{    
	    for (var i=0; i<element.options.length; i++) 
	    {
		    element.options[i].selected = true;
		}
	}
}	

function VerifySelectionCount(control , maxCount, text)
{
    if(control != null)
    {
        var count = 0;
        for(var i = 0; i< control.options.length; i++)
        {
            if(control.options[i].selected == true)
            {
                count = count + 1;
            }
        }
        if(count > maxCount)
        {
            alert("Up to " + maxCount + " " + text + " can be selected.");
            return false;
        }
    }
}

