//Start functions for Dual Checkbox List stuff :-)

function initChecklist() {
    if (document.all && document.getElementById) {
        // Get all unordered lists
	   var lists = document.getElementsByTagName("ul");
        
        for (i = 0; i < lists.length; i++) {
            var theList = lists[i];
            
		  // Only work with those having the class "checklist"
            if (theList.className.indexOf("checkList") > -1) {
                var labels = theList.getElementsByTagName("label");
                
			 // Assign event handlers to labels within
                for (var j = 0; j < labels.length; j++) {
                    var theLabel = labels[j];
                    theLabel.onmouseover = function() { this.className += " hover"; };
                    theLabel.onmouseout = function() { this.className = this.className.replace(" hover", ""); };
                }
            }
        }
    }
}

		function findAncestorOfType(node, type){
			if (null == node) 
			 	return null;

			if (null == type || 0 >= type.length  )
				return null;
				
			return findAncestorOfTypeR(node, type.toUpperCase());
		}
		
		function findAncestorOfTypeR(node, type){
			var parent = node.parentNode;
			
			if (null == parent)
				return null;
				
			if( type == parent.tagName.toUpperCase() ){
				return parent;
			} else {
				return findAncestorOfTypeR(parent, type );
			}
		}
		
		/*
		function sortCheckBoxList(listId) {
			//we take the ul.. and 
			// try and sort the li's the problem, however is getting the text inside the node.. 
			var list = document.getElementById(listId);
			quickSortList(list,0,list.childNodes.length -1 );
		}*/
		
		// 0 == same
		// -1 less than
		// 1 greater than
		function compareListNodes(a,b) {
				
			// two non li items are equal	
			if (a.tagName !="LI" && b.tagName !="LI") 
				return 0;

			//the first as an li is less than
			if (a.tagName !="LI")
				return -1;
			
			// an li.. is always greater than a non li	
			if (b.tagName !="LI")
				return 1;
							
			//otherwise compare the strings.
			//this could be slow.. not sure			
			var sA = ( a.childNodes[0].lastChild.nodeValue );  
			var sB = ( b.childNodes[0].lastChild.nodeValue );  
			
			if (sA == sB)
				return 0;
			else if (sA < sB)
				return -1;
			else
				return 1;
				
		}
				
		function selectCheckBoxListItem(node){

			if (null == node || null == node.id)
				node = this;
								
			parentLI = findAncestorOfType(node, "LI");
			if (null == parentLI)
				return;
			
			parentUL = parentLI.parentNode;
			parentUL.removeChild( parentLI );
						
			node.onclick = deselectCheckBoxListItem;
			
			var selectedId = parentUL.id.replace(/unselected/,"selected");
			var selectedUL = document.getElementById( selectedId );
			
			insertNodeInPosition(selectedUL, parentLI);
			
			node.checked = true;
			
			return true;
		}
		
		function deselectCheckBoxListItem(node){

			if (null == node || null == node.id)
				node = this;
			
			parentLI = findAncestorOfType(node, "LI");
			
			parentUL = parentLI.parentNode;
			parentUL.removeChild( parentLI );
						
			node.onclick = selectCheckBoxListItem;

			var unselectedId = parentUL.id.replace(/selected/,"unselected");
			var unselectedUL = document.getElementById( unselectedId );
			

			//unselectedUL.appendChild(parentLI);
			insertNodeInPosition( unselectedUL, parentLI);
			node.checked = false;
				
			return true;
		}
	
		function insertNodeInPosition(parentList, nodeToInsert){
			
			//empty case
			if (0 == parentList.childNodes.length) {
				parentList.appendChild(nodeToInsert);
			}
			var max = parentList.childNodes.length -1;
			var min = 0;
			var inserted = false;
			while ( min <= max ){
				var mid = Math.floor((max  + min) / 2);
				
				//var midNode = parentList.childNodes[mid];
				var i = 0;
				while ( 0 < (mid - i) && parentList.childNodes[mid - i].tagName != "LI"){
					i++;
				}
				var midNode = parentList.childNodes[mid - i ];
				if (null == midNode)
					midNode = parentList.firstChild;
					
				// we need to check and see if we are less than or equalTo
				var midCmp = compareListNodes(nodeToInsert, midNode );
				if (0 == midCmp ) {// we are equal.. so we can just insert before mid..
					parentList.insertBefore( nodeToInsert, midNode);
					return;
				} else {
					if ( 0 > midCmp){
						// we are less than.. we will look one node ahead.. and see if we are less than that
						// if we are, we can insert, otherwise fiddle the bounds and loop.
						
						var i = 1;
						while ( 0 < (mid - i) && parentList.childNodes[mid - i].tagName != "LI"){
							i++;
						}

						var nextSibling = parentList.childNodes[mid - i];
						if (null == nextSibling){
							parentList.insertBefore( nodeToInsert, parentList.childNodes[0] );
							return;
						}
												
						var downCmp = compareListNodes(nodeToInsert, nextSibling);
						if (0 <= downCmp ) {
							// we are greater then the one below mid.. so we have an insert point
							// before the mid..
							parentList.insertBefore(nodeToInsert, midNode );
							return;
						} else {
							// we are still less.. so we can move mid to be max
							max = (mid - i);	
						}
						
					} else if (0 < midCmp){
						// we are greater than, we will look one node back.. and see if we are greater than than
						// if we are we can insert.. otherwise fiddle the bounds and loop

						
						var i = 1;
						while ((parentList.childNodes.length -1 ) > (mid+i) && parentList.childNodes[mid + i].tagName != "LI"  ){
							i++;
						}

						var nextSibling = parentList.childNodes[mid + i];
						if (null == nextSibling){
							parentList.appendChild(nodeToInsert);
							return;
						}
						var upCmp = compareListNodes(nodeToInsert, nextSibling);
						if (0 >= upCmp) {
							parentList.insertBefore(nodeToInsert, nextSibling);
							return;
						} else {
							// we are still bigger than mid.. 
							min = (mid + i);
						}
					}
					
				}
			}
			alert('did we actually do an insertion if we get here?');
			return;
		}

//End functions for Dual Checkbox List stuff :-)
