function wfCloseWindow($id)
{
   if(wfElement($id))
   {
	   document.body.removeChild(wfElement($id));
	   document.body.removeChild(wfElement($id+'layer'));
   }
   else
   {
      alert('Window '+$id+' do not exist!');
   }
}
function wfOpenWindow($id, $url, $width, $height, $title)
{
   if(!wfElement($id))
   {
	   document.body.appendChild(wfCreateDiv('layer', $id+'layer'));
	   document.body.appendChild(wfCreateDiv('window', $id, $width, $height, $title));
   	   wfCenterDiv($id);
	   wfLoadDiv($id+'_content', $url);
       Drag.init(wfElement($id+'_top'), wfElement($id));
   }
   else
   {
      alert('Window '+$id+' already exist!');
   }
}

function wfElement($id){return document.getElementById($id);}

function wfCreateDiv($type, $id, $width, $height, $title)
{
	if(!$width)$width=200;
	if(!$height)$height=200;
	if(!$title)$title='';
    var newdiv = document.createElement('div');
    newdiv.setAttribute('id', $id);
	if($type=='layer')
	{
	   //newdiv.style.width = '100%';
	   //newdiv.style.height = '100%';
	   newdiv.style.position = "absolute";
	   newdiv.style.left = 0;
	   newdiv.style.top = 0;
	   newdiv.style.background = "silver";
	   newdiv.style.filter = "alpha(opacity=30)"; //ie
	   newdiv.style.opacity = 0.3; //ff
	   newdiv.style.width = wfGetPageWidth();
	   newdiv.style.height = wfGetPageHeight();
	  // alert(wfGetPageHeight());
	   
	}
	else if($type=='window')
	{
	   newdiv.style.width = $width;
	   newdiv.style.height = $height;
	   newdiv.style.position = "absolute";
	   newdiv.style.left = 200;
	   newdiv.style.top = 200;
	   newdiv.style.background = "white";
	   newdiv.style.border = "1px solid black";
	   newdiv.innerHTML = "<div id='"+$id+"_top' style='background:gray;height:17px;'><div style='float:left;padding-left:2px;color:white;font-weight:bold;font-family:verdana;font-size:14px;'>"+$title+"</div><div style='text-align:right;'><a href='#' onclick=\"wfCloseWindow(\'"+$id+"\')\"><img src='../images/icons/cancel.png' border='0'></a></div></div><div id='"+$id+"_content' style='background:white'></div>";
	}
	return newdiv;
}

function wfGetPageHeight()
{
	return document.body.scrollHeight;
}
function wfGetPageWidth()
{
    return document.body.scrollWidth;
}


function wfCenterDiv($id)
{
	div_obj = wfElement($id);
	toptop = 0;
	leftleft = 0;
	//pageheight = 100;
	if(navigator.appName == 'Netscape')
	{
	    toptop = ((innerHeight - div_obj.style.height.replace('px',''))/2);
	    leftleft = ((innerWidth - div_obj.style.width.replace('px',''))/2);
	    //pageheight = outerHeight;
	}
	else if(true)
	{
	    toptop = ((document.body.offsetHeight - div_obj.style.height.replace('px',''))/2);
	    leftleft = ((document.body.offsetWidth - div_obj.style.width.replace('px',''))/2);
		//alert(document.body.scrollTop);
		
		//toptop = 0;
		
	    //alert(document.body.offsetHeight);
	    //alert(document.body.clientHeight);
	    //alert(document.body.scrollHeight);
	    //pageheight = document.body.offsetHeight;
	}
	if(toptop < 0) toptop=10;
	toptop = toptop + document.body.scrollTop;
	leftleft = leftleft + document.body.scrollLeft;
	div_obj.style.top = toptop+'px';
	div_obj.style.left = leftleft+'px';
	//document.getElementById(prefix+'WindowLayer').style.height = pageheight+'px';
}

var stored_div_url = new Array();

function wfLoadDiv(id, url)
{
    //document.getElementById(id).innerHTML='<img style="margin:10px;" src="images/loading.gif">';
    stored_div_url[id] = url;
    //updateAlert(url+'('+id+')');
    new Ajax.Updater(id, url, {asynchronous:true, evalScripts:true, method: 'get'});
}
function wfUpdateDiv(id)
{
    if(stored_div_url[id])
    {
        wfLoadDiv(id, stored_div_url[id]);
    }
}


function wfPostForm($form, $url)
{
	new Ajax.Updater('WorkDiv', $url, {asynchronous:true, evalScripts:true, parameters:Form.serialize($form)});
}

