var layerPopupRegistry= new Array();

function hideDropdownMenus() {
	if (layerPopupRegistry.length > 0) {
		for ( i=0; i < layerPopupRegistry.length; i++) {
			hide_menu(layerPopupRegistry[i]);
		}
	}
}	

// This code is run every time a page is reloaded
// It hides your dropdown menus if they are visible
// when you click anywhere on the page
document.onmouseup = hideDropdownMenus;

var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));


function dropdownUserMenu( menu_parent_id , menu_child_id) {

	menu_parent_obj = get_element( menu_parent_id );
	menu_child_obj  = get_element( menu_child_id  );
	
	var parentX = menu_parent_obj.offsetWidth;
	var parentY = menu_parent_obj.offsetHeight;

	menu_child_obj.style.left        = get_object_posleft( menu_parent_obj, -25);
	menu_child_obj.style.top         = get_object_postop( menu_parent_obj, -130) + parentY; 
	//menu_child_obj.style.top         = 30;
	menu_child_obj.style.visibility  = 'visible';

	var childX  = menu_child_obj.offsetWidth;
	var objLeft = eval( menu_child_obj.style.left.substring( 0, menu_child_obj.style.left.indexOf("px")) );
	if ( objLeft+childX > document.body.clientWidth ) {
		menu_child_obj.style.left = (document.body.clientWidth - childX-2);
	}
	layerPopupRegistry[layerPopupRegistry.length] = menu_child_id;
}

function displayFloatingDiv( div_id) {
	divObj  = get_element( div_id  );	
	divObj.style.visibility  = 'visible';	
}

function get_element(idname) {
	switch ( get_browser() ) {
		case "std":	{ return document.getElementById(idname); } break;
		case "IE4": { return document.all[idname];            } break;
		case "NS4": { return document.layers[idname];         } break;
	}
}

function get_browser() {
	if (document.getElementById) {
		return "std";
	}
	else if (document.all) {
		return "IE4";
	}
	else if (document.layers) {
		return "NS4";
	}
}



browser = get_browser();

function get_object_posleft(obj, offset) {

	var left = obj.offsetLeft;
	while((obj = obj.offsetParent) != null) {
		left += obj.offsetLeft;
	}
	return left+offset+ 'px'; // Since content div is at this offset (25)
}

function get_object_postop(obj, offset) {
	var top = obj.offsetTop;
	while((obj = obj.offsetParent) != null) {
		top += obj.offsetTop;
	}
	return top+offset+ 'px'; // Since content div is at this offset (130)
}



function hide_menu( menuid) {
	var menuobj = get_element(menuid);
	menuobj.style.visibility='hidden';
}


function submitForm(theFormId, uri, button) {

	var theForm = get_element(theFormId);

	if(theForm) {
    if (typeof button != 'undefined' && typeof button.disabled != 'undefined' ) { 
      button.disabled=true; 
    }
    if(uri != '') {
		  theForm.action = uri;
    }
		theForm.submit();
	} else {
		alert("Bug! Cant find form "+theFormId);
	}
}

function setFirstFocus(theFormId, objId) {

   var formObj = document.getElementById(theFormId);
   formObj.objId.focus();
   //fieldObj.focus();
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/**
 * resizeTree is used for the function to expand the tree horisontally.
 */
function resizeTree() {
	if ( !treeResized ) {
		document.getElementById('tree').style.overflowX='visible';
		document.getElementById('treeExpander').src = themeRoot + '/images/design/collapseTree.gif';
		treeResized = true;
	}
	else {
		document.getElementById('tree').style.overflowX='auto';
		document.getElementById('treeExpander').src = themeRoot +'/images/design/expandTree.gif';
		treeResized = false;
	}
}

function getSizeInfo() {
	val = 100;
	if ( !filterActive ) {
		val = val - 28;
	}
	return val;
}
 


function displaySelectBox() {
  var v,t,args=displaySelectBox.arguments;
  if (args.length!=1) return 0;
  v = args[0];
  for(i=0;i<document.forms.length;i++) {
    for(j=0;j<document.forms[i].elements.length;j++) {
      var formObj=document.forms[i].elements[j];
      if (formObj.type.toLowerCase() == "select-one") {
        t=(v=='show')?'visible':(v='hide')?'hidden':v;
        formObj.style.visibility=t;
      }
    }
  }
}


function deleteItems(theFormId,dispatch) {
    var elem = document.getElementById(theFormId);
	document.getElementById("dispatch").value=dispatch;
	var deleteList = "";
    if (elem) {
		var elems = document.getElementsByTagName("input");
		for (i=0; i<elems.length;i++) {
			obj = elems[i];
			if (obj.type == "checkbox") {
				if (obj.checked) {
					deleteList += ( deleteList.length > 0 ? "," : "" ) + obj.name;
				}
			}
		}
		document.getElementById("selection").value=deleteList;
	}
	elem.submit();

}


function expandDiv( obj , imgPath ) {
	var objId = obj;
	if ( document.getElementById( objId ) ) {
		if ( document.getElementById(objId).style.display == "inline" ) {
			document.getElementById(objId).style.display = "none";
			document.getElementById(obj + "Expander").src = imgPath + "plus.gif";

		}
		else {
			document.getElementById( objId ).style.display='inline';
			document.getElementById(obj + "Expander").src = imgPath + "minus.gif";
		}
	}
}


//usage <html:form action='<action>' onsubmit="return validateSubmit()">
//prevents several submits
var submitted = false;

function validateSubmit() {
    if (submitted)
        return false;
    else {
        submitted = true;
        return true;
    }
}

function selectOrUnselect(theFormId, obj) {
  var elem = obj;
  if(elem.checked == true) {
    selectItems(theFormId, true);
  } else {
    selectItems(theFormId, false);
  }
}

function selectItems(theFormId,seltype) {
    var elem = document.getElementById(theFormId);
    if (elem) {
		var elems = document.getElementsByTagName("input");
		for (i=0; i<elems.length;i++) {
			if (elems[i].type=="checkbox") {
				elems[i].checked = seltype;
			}
		}
	}
}

function submitItems(theFormId,dispatch) {
    var elem = document.getElementById(theFormId);
	document.getElementById("dispatch").value=dispatch;
	var submitList = "";
    if (elem) {
		var elems = document.getElementsByTagName("input");
		for (i=0; i<elems.length;i++) {
			obj = elems[i];
			if (obj.type == "checkbox") {
				if (obj.checked) {
					submitList += ( submitList.length > 0 ? "," : "" ) + obj.name;
				}
			}
		}
		document.getElementById("selection").value=submitList;
	}
	elem.submit();
}

function addtolist( objId , buttonId ) {

	var nrofchecked=0;
	var textField = document.getElementById( objId );
	var buttonObj = document.getElementById( buttonId );
	if ( textField.value.length > 0 ) {
		buttonObj.disabled=false;
	}
	else {
		buttonObj.disabled=true;
	}
}

function manageList( listprefix , buttonId ) {

	var nrofchecked=0;
	var clist = document.getElementsByTagName( "input" );
	for ( i=0; i< clist.length; i++) {
		if ( !clist[i].id.indexOf(listprefix) ) {
			if ( clist[i].checked )
				nrofchecked++;
		}
	}
	if ( nrofchecked > 0 ) {
		document.getElementById(buttonId).disabled=false;
	}
	else {
		document.getElementById(buttonId).disabled=true;
	}
}

function displayExtPopup(xSize,ySize,url,opts){
	var newWindow;
	newWindow = window.open(url,'Help','width='+xSize+',height='+ySize+','+opts);
	newWindow.focus();
}

function changeTitle(theString) {
	document.title = theString;
}

function popupMessage( fid, oid, msg) {
	var oFrame = getFrame(fid);

	var oDiv = document.getElementById(oid);
	oDiv.style.display='inline';
	oFrame.document.open("text/html","replace");
	oFrame.document.writeln('<html><head>');
	scriptHeader = '<link rel="stylesheet" href="'+themePath+'/css/tms.css" type="text/css">';
	oFrame.document.writeln('</head><body style="border:solid 0px #888888; margin:0px;"><table cellpadding=2 cellspacing=0 border=0>');
	oFrame.document.writeln(scriptHeader);
	oFrame.document.writeln('<tr><td style="background-color:#ffffff">' + msg + '</td></tr>');
	oFrame.document.writeln('</table></body></html>');
	oFrame.document.close();
	displaySelectBox('hide');
	oDiv.style.visibility='visible';
}

function closeMessagePopup( oid ){
	var oDiv = document.getElementById(oid);
	displaySelectBox('show');
	oDiv.style.visibility='hidden';
	oDiv.style.display='none';
}

function displaySelectBox() {
  var v,t,args=displaySelectBox.arguments;
  if (args.length!=1) return 0;
  v = args[0];
  for(i=0;i<document.forms.length;i++) {
    for(j=0;j<document.forms[i].elements.length;j++) {
      var formObj=document.forms[i].elements[j];
      if (formObj.type.toLowerCase() == "select-one") {
        t=(v=='show')?'visible':(v='hide')?'hidden':v;
        formObj.style.visibility=t;
      }
    }
  }
}

function getFrame(theId) {
	var oFrame;
	if (!document.all) {
		oFrame = document.getElementById(theId).contentWindow;
	}
	else {
		oFrame = document.frames(theId);
	}
	return oFrame;	
}

function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function displayPopup() {
    var obj,args=displayPopup.arguments;
    for (i=0; i<(args.length-1); i+=2) {
        if (( obj = findObj(args[i]) ) != null ) {
            obj.style.height = document.body.clientHeight;
            obj.style.width  = document.body.clientWidth;
        }
    }
  for (i=0; i<(args.length-1); i+=2) 
    {
      if ((obj=findObj(args[i]))!=null)
      { 
        v=args[i+1]; d=v;
        if (obj.style) 
        { 
            v=(v=='show')?'visible':(v='hide')?'hidden':v;
            d=(d=='show')?'inline':(d='hide')?'none':d;
            obj.style.visibility=v;
            obj.style.display=d;
        }
      }
    }
}


function isDecendant(decendant,ancestor) {
	return ((decendant.parentNode==ancestor) ||
        	(decendant.parentNode!=document) &&
		isDecendant(decendant.parentNode,ancestor));
}


function setFirstFocus(theFieldId) {

	var theField = get_element(theFieldId);
	if(theField) {
		theField.focus();
	}
}


function validEmail(theEmail) {
	var x = theEmail;
	var filter  = /^([a-zA-Z0-9_\.\-%\+])+\@(([a-zA-Z0-9\.\-])+\.)+([a-zA-Z]{2,4})+$/;
	if (filter.test(x)) {
		return true;
	}	
	return false;
}


function setDisplayTimeout(element, option, timeout) {

  if(timeout != null) {
    setTimeout("document.getElementById(\"" + element + "\").style.display = \"" + option + "\"", timeout);
  } else {
    document.getElementById(element).style.display = option;
  }
}


function FindPos(ctrl) {

  var pos = {x:0, y:0};
 
  if (ctrl.offsetParent) {
    while(ctrl) {
       pos.x += ctrl.offsetLeft;
       pos.y += ctrl.offsetTop;
       ctrl = ctrl.offsetParent;
    }
  }
  else if (ctrl.x && ctrl.y) {
    pos.x += ctrl.x;
    pos.y += ctrl.y;
  }
 
  return pos;
}


