// X,Y, H,W of elements
// Returns an array containing data
// Requires an element name to be passed
// Cross DOM compatible
function getPos(el) {
	var x,w,y,h; 
	if (document.getBoxObjectFor) { 
		var bo = document.getBoxObjectFor(el); 
		x = bo.x; 
		w = bo.width;
		y = bo.y;
		h = bo.height;
		} 
	else if (el.getBoundingClientRect) { 
		var rect = el.getBoundingClientRect(); 
		x = rect.left; 
		w = rect.right - rect.left;
		y = rect.top;
		h = rect.bottom - rect.top;
	} 
	if(document.all) { 
		el.x = x;
		el.y=y;
		el.w= w;
		el.h = h;
	}
	
	var pos_a =	new Array(el.x, el.y, el.w, el.h);	
	// alert("Left: " + x + "\rTop: " + y + "\rWidth: " + w + "\rHeight: " + h);
	return pos_a;
}

// Sets the position of an element relative to another element.
// Required element to position against and element to move.
function setPos(elp, eld)	{
	// alert("Here "+ elp.x+"\n Width "+ elp.y);
	var schdiv = eld;
	if(document.all) {
		ps	=	getPos(elp);
		schdiv.style.position = 'absolute';
		schdiv.style.top 	= ps[1] + 7;
		schdiv.style.left	= ps[0] + 1;
	} else {
		  // alert("Here "+ elp.x+"\n Width "+ elp.y+ "\n\nHere "+ eld.x+"\n Width "+ eld.y);
		  eld.style.top = elp.y + 10 +"px";
		  eld.style.left = elp.x + 5 + "px";
	}
}

function showForm(formname, focuselement, hideelement) {
	var form_div 		= document.getElementById(formname);
	var focus_element 	= document.getElementById(focuselement);
	var hide_element	= document.getElementById(hideelement);
	grayOut(true, {'bgcolor':'#CCCCCC','opacity':'80'});
	form_div.style.visibility = 'visible';
	hide_element.style.visibility = 'hidden';
	// focus_element.focus();
}

function closeForm(formname, showdiv) {
	var form_div = document.getElementById(formname);
	var show_div = document.getElementById(showdiv);
	form_div.style.visibility = 'hidden';
	show_div.style.visibility = 'visible';
	grayOut(false);
}

function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';				 
  } else {
     dark.style.display='none';
  }
}

function popWindow(winURL, winName, winWidth, winHeight, winScroll, resizeable, toolbar, directories, status, menubar) { 

		if( menubar == '' ) {
			menubar = '0'
		}
		if( status == '' ) {
			status = '0'
		}
		if( directories == '' ) {
			directories = '0'
		}
		if( toolbar == '' ) {
			toolbar = '0'
		}
		if( resizeable == '' ) {
			resizeable = 'no'
		}
		if(winScroll == '') {
			winScroll = 'no'
		}
		if(winWidth == '') {
			winWidth = 800;
		}
		if(winHeight == '') {
			winHeight = 600;
		}
		if(winName == '') {
			winName == 'NewWindow';
		}
		var winLeft = (screen.width - winWidth) / 2;
		var winTop = (screen.height - winHeight) / 2;
		var winProps = 'height='+winHeight+',width='+ winWidth+',top='+winTop+',left='+winLeft+',scrollbars='+winScroll+',resizeable=no,toolbar='+toolbar+',directories='+directories+',status='+status+',menubar='+menubar;
		var NewWindow = window.open(winURL,winName,winProps);
}