function toggleFilters (el) { 
	var req = getXMLHttpRequest();
	if(req) {
		//window.alert(el.checked);
		
		req.open("GET", "../admin/toggleFilters.form?ignoreFilters=" + el.checked, true);
		req.send(null);
	}
}

function getXMLHttpRequest() {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
	    return new XMLHttpRequest();
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
	   return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function editList(domain, listName) {
	if(!domain || domain.length == 0) {
		domain = "EMDT";
	}
	if(listName) {
		if (domain == "DATABASE")
			window.open("../admin/editDBList.do?domain=" + domain + "&name=" + listName+ "&method=list",'editList','scrollbars=1,toolbar=1,status=1,menubar=1,resizable=1,location=0,directories=0,width=700,height=400,left=0,top=0');
		else
			window.open("../admin/editList.jsp?domain=" + domain + "&name=" + listName, 'editList','scrollbars=1,toolbar=1,status=1,menubar=1,resizable=1,location=0,directories=0,width=700,height=400,left=0,top=0');
	} else {
		window.alert("List domain/name missing");
	}
}

var cursorPointer = "pointer";
if(document.all) { 
	//Define IE specific vars in here..
	cursorPointer = "hand"; //For IE versions less then 6
}

function popup(url, id, width, height) {
	return window.open(url, id,'scrollbars=1,toolbar=1,status=1,menubar=1,resizable=1,location=1,directories=1,width=' + width + ',height=' + height + ',left=0,top=0');
}

function dataGuard() {
	var saveButton = document.getElementById('saveDataButton');
	if(saveButton && unsavedData) {
		return window.confirm("You have unsaved data. Are you sure?");
	}
	return true;
}

function buttonOver(button) {
	button.className = "buttonOver";
}

function buttonOut(button) {
	button.className = "button";
}

function confirmAndDispatch(methodName, message) {
	if(window.confirm(message)) {
		return dispatch(methodName);
	}
	return false;
}

function dispatch(methodName) {
	if(methodName) {
		var target = document.getElementById("dispatchMethod");
		if(target) {
			target.value = methodName;
			return true;
		} else {
			window.alert("Cannot find dispatchMethod Element");
		}
	} else {
		window.alert("No method name to dispatch to!");
	}
	return false;
}

function moveSelection(sourceId, targetId) {
	var source = document.getElementById(sourceId);
	var target = document.getElementById(targetId);
	if(!source) {
		window.alert("Could not find source selection field with id '" + sourceId + "'");
	}
	if(!target) {
		window.alert("Could not find target selection field with id '" + targetId + "'");
	}
	//@todo check source and target are selection fields
	for(var i = 0; i < source.options.length;) {
		if(source.options[i].selected) {
			var option = source.options[i];
			if(source.options[i].value != "emptyPlaceHolder") {
				target.insertBefore(option, target.options[0]);
				continue;
			}
		}
		++i;
	}
}

function updateParent() {
	
	if(window.opener) {
		if(window.opener.update) {
			window.opener.update();
		}
	}
}

function select(mode) {
	var els = document.getElementsByTagName("input");
	for(var i = 0; i < els.length; i++) {
		if(els.item(i).type == "checkbox" && hasClassName(els.item(i), "allNonInvert")) {
			if(mode == "all") {
				els.item(i).checked = true;
			} else if(mode == "none") {
				els.item(i).checked = false;
			} else if(mode == "invert") {
				els.item(i).checked = !els.item(i).checked;
			}		
		}
	}
}

function getClassNames(el) {
	return el.className.split(/\s+/);
}

function hasClassName(el, className) {
	var classNames = getClassNames(el);
	for(var i = 0; i < classNames.length; i++) {
		if(classNames[i] == className) return true;
	}
	return false;
}

function save(formId, actionId) {
	if(setVal(actionId, "save")) {
		document.getElementById(formId).submit();
	}
}

function cancel(formId, actionId) {
	if(setVal(actionId, "cancel")) {
		document.getElementById(formId).submit();
	}
}

function setVal(elId, val) {
	var target = document.getElementById(elId);
	if(target) {
		target.value=val;
		return true;
	} else {
		alert("Could not set element value. Element id = " + elId + ", value = " + val);
		return false;
	}
}

function changePage(formId, actionId, pageNum) {
	var formEl = document.getElementById(formId);
	if(!formEl) {
		alert("Could not find form element with " + formId + " id");
		return false;
	}
	
	if(setVal(actionId, "changePage") && setVal("_page", pageNum)) {
		formEl.submit();
		return true;
	}
	return false;
}

//send browser to a url and display progress indicator
function dispatchAndShowPleaseWait (url)	{
	window.location = url;
	showPleaseWait();
}
//show 'please wait' message in an iFrame.
//Note that you need to include the relevant iFrame code in your jsp.
function showPleaseWait()	{
	document.getElementById("waitframe").style.visibility = "visible";
	document.getElementById("waitframe").src = document.getElementById("waitframe").src; 
}


function isChecked(aCheckbox){
		if(aCheckbox){	
			if(aCheckbox.length){
				var checkboxLength = aCheckbox.length;		
				for(var i = 0; i<checkboxLength ; i=i+1){
				 	if(aCheckbox[i].checked == true ){
				 		return true;
				 	}
				}
				return false;
			}else{
				if(aCheckbox.checked == true ){
				 		return true;
				}else{
						return false;
				}
			}
		}else{
			return false;
		}
	}


function deselectCheckBox(aCheckbox){
		if(aCheckbox){	
			if(aCheckbox.length){
				var checkboxLength = aCheckbox.length;		
				for(var i = 0; i<checkboxLength ; i=i+1){
				 	aCheckbox[i].checked = false;				 		
				}
				return false;
			}else{
				aCheckbox.checked = false;
			}
		}
	}
function fnMoveItems(lstbxFrom,lstbxTo)
{
	 var varFromBox = document.all(lstbxFrom);
	 var varToBox = document.all(lstbxTo); 
	 if ((varFromBox != null) && (varToBox != null)) 
	 { 
		  if(varFromBox.length < 1) 
		  {
			   alert('Please select atleast one user.');
			   return false;
		  }
		  if(varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1		
		  {
			   alert('Please select a user to Add/Remove');
			   return false;
		  }
		  while ( varFromBox.options.selectedIndex >= 0 ) 
		  { 			  
			   var newOption = new Option(); // Create a new instance of ListItem 
			
			   newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text; 
			   newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value; 
			   varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
			
			   varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox 
		
		  } 
	 }
	 sortOptions(varFromBox);
	 sortOptions(varToBox);
	 
	 return false; 
}


function compareOptionText(a,b) {
  return a.text!=b.text ? a.text<b.text ? -1 : 1 : 0;
}
function sortOptions(list) {
	var items = list.options.length;
	// create array and make copies of options in list
	var tmpArray = new Array(items);
	for ( i=0; i<items; i++ ){
		tmpArray[i] = new Option(list.options[i].text,list.options[i].value);
	}
	// sort options using given function
	tmpArray.sort(compareOptionText);
	// make copies of sorted options back to list
	for ( i=0; i<items; i++ ){
		list.options[i] = new Option(tmpArray[i].text,tmpArray[i].value);
	}
  }

