/* 
Requires 3 controls:
    1. lbxAvailCourses - Listbox
    2. lbxSelectedCourses - ListBox
    3. txtSaveCourses - html hidden input control
Provides 2 functions:
    1. OnClick="javascript:AddCourse();"
    2. OnClick="javascript:RemoveCourse();"
Note: Must use txtSaveCourses to get selected courses since this is client-side JavaScript
*/    
			function AddCourse() {
				lstFrom = document.getElementById('lbxAvailCourses');
				lstTo = document.getElementById('lbxSelectedCourses'); 
				MoveCourse(lstFrom, lstTo);	
				lstFrom.value=-3
				CopyCourses()
			}
			
			function RemoveCourse() {
				lstTo = document.getElementById('lbxSelectedCourses'); 
				if(lstTo.options[0].value != "") {
					for(i=0;i<lstTo.length;i++) {
                        if(lstTo.options[i].selected) {
                                lstTo.options[i] = null;
                        }
					}
					if(lstTo.length == 0) {
                        oOption = new Option("", "", false, true);
                        lstTo.options[lstTo.length] = oOption;
					}
				}
				CopyCourses()
			}
			
			function CopyCourses() {
				lstTo = document.getElementById('lbxSelectedCourses'); 
                txtTo = document.getElementById('txtSaveCourses');
  				txtTo.value='';
				for(i=0;i<lstTo.length;i++) {
  				     txtTo.value=txtTo.value + lstTo.options[i].value + ',' + lstTo.options[i].text + ',';
				}
			}

			function MoveCourse(SelectFrom, SelectTo) {
		        var value, text, oOption
				if(SelectFrom.options[0].value != "") {
				    if (SelectTo.length > 0) {
					   if(SelectTo.options[0].value == "") {
                           SelectTo.options[0] = null;
                       }
					}
					for(i=0;i<SelectFrom.length;i++) {
					    //arrCourse = SelectFrom.options[i].text.split(" $")
					    //strCourse = arrCourse[0];
					    s = SelectFrom.options[i].text
					    if (s.charAt(0) == "$") {
					        index=s.indexOf("-")
					        strCourse=s.slice(index+1)
					    } else {
					        strCourse=s
					    }
                        if(SelectFrom.options[i].selected && strCourse != "") {
								FoundIndex  = findOptionByText(SelectTo, strCourse)
								if (FoundIndex < 0) {
                                   oOption = new Option(strCourse, SelectFrom.options[i].value, false, true);
                                   SelectTo.options[SelectTo.length] = oOption;
                                } 
                        }
					}
					if(SelectFrom.length == 0) {
                        oOption = new Option("", "", false, true);
                        SelectFrom.options[SelectFrom.length] = oOption;
					}
				}
			}

    function findOptionByText($objSelect, $strText) { 
        // Straight pass-through 
        return findOption($objSelect, $strText, 'text' ); 
    }; 
			
	function findOption($objSelect, $strNeedle, $strSearch)  { 
        $_found = -1
 
        if ( $objSelect == '' ) 
            $objSelect = null; 
        else if ( ( typeof ( $objSelect ) ) == 'string' ) 
            $objSelect = document.getElementById($objSelect); 
 
        // If we have nothing to deal with, just bale... 
        if ( ( $objSelect ) && ( $strNeedle ) &&  
             ( ( $strSearch == 'text' ) || ( $strSearch == 
'value' ) ) ) 
        { 
 
            for (var $i = 0; $i < $objSelect.options.length; $i++) 
            { 
                if ( $strSearch == 'value' ) 
                    $objHayStack = $objSelect[$i].value; 
 
                else if ( $strSearch == 'text' ) 
                    $objHayStack = $objSelect[$i].text; 
 
                if ( $objHayStack == $strNeedle ) 
                { 
                    $_found = parseInt ( $i ); // We found a match 
                    break; 
                } 
            } 
        } 
 
        // Send back what we have 
        return $_found; 
    }
            