
function initSelectBox(){
       document.onkeydown = keyHandler;
       document.onkeyup = keyHandler;
       document.onmouseup = mouseupHandler;
	   document.onmousemove = mousemoveHandler;
	   selectBoxInputHiddenNameInit();
}
 
 
//returns an array of elements that match full or partial (indexOf) classname
function ElementsByClass(rootElement,ElementClass,ElementType,partialMatch){        

  
            var elementsArray=new Array();
            if (rootElement != "")
            {
                var theElements=rootElement.getElementsByTagName(ElementType);                  

                for (var x=0;x<theElements.length;x++){
				          if(partialMatch){
					           if (theElements[x].className.indexOf(ElementClass)>-1){ 
							        elementsArray[elementsArray.length]=theElements[x];
						        }
					      }else{
	                            if (theElements[x].className==ElementClass){ 
							        elementsArray[elementsArray.length]=theElements[x];
						        }
					      }
				    }                          
			}

	            return elementsArray;
	}

 

// these functions deal with capturing the CTRL key and the mouseup

var pressedCTRL=false;

function keyHandler(e)
{
            if (!e) e = window.event;  
            pressedCTRL=e['ctrlKey'];
}

 

function mouseupHandler(e)
{

            if (!e) var e = window.event;         
            var selectboxes=ElementsByClass(document,'selectBoxItems','div',true);

            if (!pressedCTRL)

                        for (var x=0;x<selectboxes.length;x++){                                                               
                               if (!ElementsByClass(selectboxes[x].parentNode,'selectBoxLink','div')[0].isMouseOverd && !ElementsByClass(selectboxes[x].parentNode,'selectBoxItems','div',true)[0].isMouseOverd){
                               	// make sure we're not over the scroll
								if (!overScrollbar){					
								     selectboxes[x].style.display="none";
	                                 selectboxes[x].isOn=false;
								 }
                               }                                   
                        }                                                           

}

 

 

// Selectbox functions!

 
// this function toggles the selectbox

function openSelectBox(which){   

            var theSelectBox= $get("selectBoxItems"); //ElementsByClass(which.parentNode,'selectBoxItems','div',true)[0];                 
            //alert(theSelectBox);

            /*if (theSelectBox.isOn){
                        theSelectBox.style.display="none"
                        theSelectBox.isOn=false;
            }           

            else*/ {
                        theSelectBox.style.display="block";
                        theSelectBox.isOn=true;
            }                                                                       
}

 

/* the purpose of this function is to tell the browser that the mouse is
 currently over the select box button, so the the main click event
 catcher knows to ignore its normal operation so we could to open or 
 close the select box */ 

function selectBoxMousing(which,e){
            if(e.type=="mouseover"){                         
                        which.isMouseOverd=true;                      
            }

            else if(e.type=="mouseout")
                 which.isMouseOverd=false;                                 

}

 
 

 

// this function selects an item

function selectBoxItem(which){

            if (document.all){
	            var r = document.body.createTextRange();
	            r.moveToElementText(selectme);
	            r.select();          
			}

            if (pressedCTRL){                       
               which.className=which.className=="selectBoxItemBlank"?"selectBoxItemSelected":"selectBoxItemBlank";
             } else{			 				 					
	                which.parentNode.parentNode.style.display="none";
	                var items=which.parentNode.getElementsByTagName('div');
	                for (var x=0;x<items.length;x++){
	                            items[x].className="selectBoxItemBlank";
								}                                                                                             
	
	                which.className="selectBoxItemSelected";
	                $get("selectBoxCaption").innerHTML = which.innerHTML;	
	                //ElementsByClass(which.parentNode.parentNode.parentNode,'selectBoxCaption','span')[0].innerHTML=which.innerHTML;					
            }
						
			//give the select box hidden input a name
			//ElementsByClass(document.getElementById(which.parentNode.parentNode.parentNode.id),'hiddenValue','input')[0].name= "hidden" + which.parentNode.parentNode.parentNode.id;
			
			//put the select box selected values in his hidden input
			
			if (arguments.length>1)
				$get(arguments[1]).value = selectBoxReturnValuesMedia(which.parentNode.parentNode.parentNode);
			//alert($get(arguments[1]).value);
			//alert($get("searchBox_Value").value);
			//ElementsByClass(document.getElementById(which.parentNode.parentNode.parentNode.id),'hiddenValue','input')[0].value = selectBoxReturnValues(which.parentNode.parentNode.parentNode);
			
		
			
}

 

// this functions returns the select box selected values
function selectBoxReturnValues(which){

            var selectedItems=new Array();

            var items=which.getElementsByTagName('div');                       

            for (var x=1;x<items.length;x++)

                        if(items[x].className=="selectBoxItemSelected")

                                    selectedItems[selectedItems.length]=items[x].innerHTML;
									
						return selectedItems;
}



// this functions returns the select box selected values
function selectBoxReturnValuesMedia(which){

            var selectedItems=new Array();

            var items=which.getElementsByTagName('div');                       

            for (var x=0;x<items.length;x++)

                        if(items[x].className=="selectBoxItemSelected")

                                    selectedItems[selectedItems.length]=items[x].ValueText;
									
						return selectedItems;
}


//give a name to the hidden input of every selectBox
function selectBoxInputHiddenNameInit(){
    
    var selectBoxes = new Array();
	var selectedItems = new Array();
	
	selectBoxes = ElementsByClass(document.body,'selectBoxContainer','div');
	
	 for (var x=0;x<selectBoxes.length;x++){
	    ElementsByClass(selectBoxes[x],'hiddenValue','input')[0].name= "hidden" + selectBoxes[x].id;
		
		selectedItems = selectBoxReturnValues(selectBoxes[x]);
		
	    ElementsByClass(selectBoxes[x],'hiddenValue','input')[0].value = selectedItems;
		
		/*for (var y=1;y<selectedItems.length;y++){
		    ElementsByClass(selectBoxes[x],'hiddenValue','input')[0].value += selectedItems[y].innerHTML;
		}*/
		
	 }
}


// this functions handles the submit part

function selectBoxSubmit(which){
 var allSelectBoxHiddens = new Array();
  for (var x=0;x<document.forms["selectBoxForm"].length;x++){
    allSelectBoxHiddens[allSelectBoxHiddens.length]="name= " + document.forms["selectBoxForm"][x].name + " value= " +document.forms["selectBoxForm"][x].value;
  }
  alert(allSelectBoxHiddens)
}

 

function clearTextBox(which,phrase){

            if (which.value==phrase)

                        which.value="";  

}

// returns the absolute position of an element
  function getAbsolutePosition(element) {
    var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.offsetParent) {
      var tmp = getAbsolutePosition(element.offsetParent);
      r.x += tmp.x;
      r.y += tmp.y;
    }
    return r;
  };
  
 
 
 function mousemoveHandler(e) {
	posx = 0;
	posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information	
}
 
// make sure that no clicks are captured when mouse is over the scrollbar
var overScrollbar=false;
function isOverScrollbar(whichObj){
	//if over scrollbar
	if (posx-getAbsolutePosition(whichObj).x>whichObj.offsetWidth-17){
		overScrollbar=true;					
	} else {
		overScrollbar=false;
	}
	
}
