var activeOptionRef;
var focusElm;
var dropElm;

function activateSelect(objSelect,fakeSelect,dropDown) {
	
	// setting focus element
	focusElm = objSelect;
	
	// display dropdown
	dropDown.style.display = "block";
	
	// resetting class
	dropDown.className = dropDown.className.replace(" transparent","");	
	
	// building dropdown if empty
	if(dropDown.childNodes.length == 0) {
	
		for(var x=0; x<objSelect.options.length;x++) {
			
			// creating element
			var option = document.createElement('div');
			
			// setting text
			option.innerHTML = objSelect.options[x].text;
			
			// setting value
			option.value = x;			
			
			// setting onclick
			option.onmousedown = function () {
				
				// setting selected index
				objSelect.selectedIndex = this.value;
				
				// setting selected
				objSelect.selected = 'true';
				
				// setting innerhtml
				fakeSelect.innerHTML = this.innerHTML;
				
				// setting dropdown className
				dropDown.className = dropDown.className.replace(" transparent","");
				
				// setting invisible
				dropDown.style.display = "none";
				
				// reset old option
				activeOptionRef.className = activeOptionRef.className.substring(0,activeOptionRef.className.length-7);
				
				// saving activeref
				activeOptionRef = this;
				
				// appending active to class
				this.className += ' active';
			}
			
			
			/*
			// setting right class
			if(x == 0 && objSelect.options.length == 1) {
				option.className = 'optionTopBottom';
			}*/
			
			if(x == 0 && objSelect.options.length > 1) {
				option.className = 'optionTop';
			}
			
			/*
			if(x != 0 && objSelect.options.length-1 == x) {
				option.className = 'optionBottom';
			}
			*/
		
			// active?
			if(x == objSelect.selectedIndex) {
				activeOptionRef = option;
				option.className += ' active';
			}
			
			
			
			
			if(option.className.indexOf('active') < 0){
				
				option.onmouseout = function(){
					
					var cssClass = this.className;
					
					cssClass = cssClass.replace('itemHover', '');
					this.className = cssClass;
				}
									
				option.onmouseover = function() {
						
					this.className += ' itemHover';
	
				}
			}
				
			dropDown.appendChild(option);
		}
	}	
}

function deActivateSelect(objSelect,fakeSelect,dropDown) {
	
	// setting focus element
	focusElm = null;
	
	dropDown.className += " transparent";
	
	dropElm = dropDown;
	
	// setting invisible
	setTimeout('dropElm.style.display = "none"',100);
}

function changeOption(objRef) {
	
	// getting dropdown
	var dropDown = objRef.parentNode.nextSibling;
	
	var layer = objRef.previousSibling;
	
	// getting node to select
	var option = dropDown.childNodes[objRef.selectedIndex];
	
	// setting layer text
	layer.innerHTML = option.innerHTML;
	
	// reset old option
	activeOptionRef.className = activeOptionRef.className.substring(0,activeOptionRef.className.length-7);
	
	option.className += " active";
		
	activeOptionRef = option;
}

function activateField(objRef) {

	// lets hide layer
	objRef.style.display = 'none';
	
	// set focus on fiels
	objRef.nextSibling.focus();
}

function deActivateField(objRef) {

	// lets show layer if field is empty
	if(objRef.value == "") {
		objRef.previousSibling.style.display = 'inline';
	}
}

function toggleCheckbox(objRef) {

	// lets show layer if field is empty
	if(objRef.className == "checkboxWrapper") {
	
		objRef.className = 	"checkboxWrapperActive";
		
		// checking child
		objRef.childNodes[0].checked = true;
		
	} else {
	
		objRef.className = 	"checkboxWrapper";
		
		// checking child
		objRef.childNodes[0].checked = false;
	}
}














/**
*	checkboxId - The id of the real checkbox that changes
*	checkedImage - The image to use when the checkbox is checked
*	uncheckedImage - The image to use when the checkbox is not checked
*/
function toggleCustomCheckbox(checkboxId, checkedImage, uncheckedImage) {

	// Make sure that the checkbox exists
	if(document.getElementById(checkboxId)) {
		
		// Assume the checkbox will become checked since that will be the case most of the times
		var newImgSrc = checkedImage;
		
		// If the checkbox is already checked
		if(!document.getElementById(checkboxId).checked) {
		
			newImgSrc = uncheckedImage;
			document.getElementById(checkboxId).checked = false;
				
		}
		else {
			
			document.getElementById(checkboxId).checked = true;
		}

		// Set the src of the image to the new src		
		document.getElementById(checkboxId + 'Custom').src = newImgSrc;
	
	}
	
	// Stop the normal action of the checkbox
	return true;

}


function checkCustomCheckbox(checkboxId, checkedImage){

	// Make sure that the checkbox exists
	if(document.getElementById(checkboxId)) {
		
		if(document.getElementById(checkboxId + 'Custom')){
			// Set the src of the image to the new src		
			document.getElementById(checkboxId + 'Custom').src = checkedImage;
		}
		
		document.getElementById(checkboxId).checked = true;
	}
	
	return true;
}


function ieFixClickCheckbox(checkboxId) {

	// Make sure we are running a browser that needs this
	if(document.all && navigator.appVersion.indexOf("MSIE")>-1) {
	
		document.getElementById(checkboxId).click()
	}

}

/*
* 	Function to make sure that images works as intended when inside a label
*	Original code from http://snook.ca/ with modifications by Matter
*/
function fixIeImgLabels() {
	
	// If we are running IE on Windows
	if(document.all && navigator.appVersion.indexOf("MSIE")>-1 && navigator.appVersion.indexOf("Windows")>-1)
	{
	
		// Gel all labels
		var a = document.getElementsByTagName("label");
		
		// Loop the labels
		for(var i=0,j=a.length;i<j;i++){
		
			// If the label has kids and the first one is an image
			if(a[i].hasChildNodes && a[i].childNodes.item(0).tagName == "IMG") {
			
				// Get what the label is for
				a[i].childNodes.item(0).forid = a[i].htmlFor;
				
				// Apply a function to onclick for the imge
				a[i].childNodes.item(0).onclick = function() {
				
					var e = document.getElementById(this.forid);
				 
					switch(e.type){
					
						case "radio":
							//e.checked |= 1;
							e.click(); // Added by Matter
							break;
							
						case "checkbox":
							//e.checked = !e.checked;
							e.click(); // Added by matter
							break;
							
						case "text":
						case "password":
						case "textarea":
							e.focus();
							break;
					}
				}
			}
		}
	}	

}





function savePage(formAction, hiddenId, hiddenName) {
		
	// checking for htmleditors
	for(var x=0;x<frames.length;x++) {
	
		// copy content to hidden field
		var tmpName = frames[x].name;
		
		// saving html
		if(tmpName.substring(tmpName.length-6,tmpName.length) == "_frame") {
			var frameRef = new element(frames[x].name);
			
			//save dataq to form
			var tmpFormObj = eval("document.eventDataForm." + tmpName.substring(0,tmpName.length-6));
			tmpFormObj.value = frameRef.obj.contentWindow.document.body.innerHTML;
		}
	}
	
	document.eventDataForm.action = formAction;
	
	document.getElementById(hiddenId).name = hiddenName;
	
	
	// submitting form
	document.eventDataForm.submit();		
}



