var http;

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}

function formSubmit(f,url,module)
{
	var formData='module='+module;
	for(var i=0;i<f.elements.length;i++)
	{
		var obj=f.elements[i];
		switch (obj.type)
		{
			case 'submit':
				break;
			default:
				formData+='&'+obj.name+'='+encodeURIComponent(obj.value);
		}
	}
	if (getQueryVariable('debug')=='1')
		alert(formData);
	sendRequest(url,formData);
	return false;
}

function urlSubmit(url)
{
	sendURLRequest(url);
	return false;
}

function formBack(url,module)
{
	var formData='module='+module;
	sendRequest(url,formData);
	return false;
}

function createRequestObject() 
{
	var ro;
	try
	{
		ro=new XMLHttpRequest();
	}
	catch (error)
	{
		try
		{
			ro=new ActiveXObject("Microsoft.XMLHTTP");
	 	}
		catch (error)
		{
			return false;
		}
	}
	return ro;
}

function handleHttpResponse() 
{
  var loader=document.getElementById('loader');
  if (http.readyState == 4) 
  {
	loader.style.display='none';
	if (http.status==200)
    {
	    var responseText=http.responseText;
	    if (responseText.indexOf("DEBUG")==0)
	    	alert(responseText);
	    var modules = http.responseXML.getElementsByTagName('module');
	    for(var i=0;i<modules.length;i++)
	    {
	    	var module=modules[i];
	    	var targetID=module.getAttribute('id');
			var targetNode=document.getElementById(targetID);
			var modStartSearch='<module id="'+targetID+'">';
			var modStart=responseText.indexOf(modStartSearch);
			var modEnd=responseText.indexOf('</module>',modStart);
			var modText=responseText.substring(modStart+modStartSearch.length,modEnd);		
			targetNode.innerHTML=modText;
		}
	}
  	else
    {
    	alert("Problem retrieving XML data.")
    }
  }
  else
  {
	  loader.style.display='block';
  }
}

function sendURLRequest(url)
{
	http=createRequestObject();
	http.onreadystatechange=handleHttpResponse;
	http.open("GET",url,true);
	http.send(null);
}

function sendRequest(url,vars)
{
	http=createRequestObject();
	http.onreadystatechange=handleHttpResponse;
	http.open("POST",url,true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	http.send(vars);
}

function navigateTo(url)
{
	document.location.href=url;
}

function core_copyFileValue(field)
{
	if (field.value=='')
		return;
	var slashPos=field.value.lastIndexOf('/');
	if (slashPos==-1)
		slashPos=field.value.lastIndexOf('\\');
	var shortName=field.value.substr(slashPos+1);
	var targetField=field.form.elements['display_'+field.name];
	targetField.value=shortName;
}

var gPopUp=null;
var gPopUpMask=null;
var gPopUpFrame=null;
var gReturnFunc=null;
var gReturnContext=null;

function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}
function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return window.undefined; 
}

function popDialog(url,width,height,returnFunc,context)
{
	if (gPopUp==null)
	{
		gPopUp=document.getElementById('popup');
		gPopUpMask=document.getElementById('popupMask');
		gPopUpMask.style.opacity='.2';
		gPopUpMask.style.filter='alpha(opacity=20)';
		gPopUpFrame=document.getElementById('popupFrame');
	}
	gPopUp.style.display='block';
	gPopUpMask.style.display='block';
	gPopUp.style.width=width+"px";
	gPopUp.style.height=height+"px";
	gPopUpFrame.src=url;
	gReturnFunc=returnFunc;
	gReturnContext=context;
	
	centrePopWin();
	window.onresize=centrePopWin;
	window.onscroll=centrePopWin;
	return false;
}

function closeDialog(ok,retval)
{
	gPopUp.style.display='none';
	gPopUpMask.style.display='none';	
	gPopUpFrame.src='';
	window.onresize=null;
	window.onscroll=null;
	if (ok)
		gReturnFunc(gReturnContext,retval);
}

function centrePopWin()
{
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();	
	var scTop = parseInt(document.body.scrollTop,10);
	var scLeft = parseInt(document.body.scrollLeft,10);
		
	gPopUpMask.style.height=fullHeight+"px";
	gPopUpMask.style.width=fullWidth+"px";
	gPopUpMask.style.top=scTop+"px";
	gPopUpMask.style.left=scLeft+"px";
			
	gPopUp.style.top=(scTop+((fullHeight-parseInt(gPopUp.style.height))/2))+"px";
	gPopUp.style.left=(scLeft+((fullWidth-parseInt(gPopUp.style.width))/2))+"px";
}

function insertText(textarea,text)
{
	if (document.selection) 
	{
		textarea.focus();
		sel=document.selection.createRange();
		sel.text=text;
	}
	else if (textarea.selectionStart||textarea.selectionStart==0) 
	{
		var startPos = textarea.selectionStart;
		var endPos = textarea.selectionEnd;
		textarea.value = textarea.value.substring(0,startPos)+text+textarea.value.substring(endPos,textarea.value.length);
	} 
}

function page_cbkMove(form,path)
{
	var path=path.split("|")[0].substr(1);
	delete form.onsubmit;
	form.elements['newparent'].value=path;
	form.submit();
}

function page_move(form,popUrl)
{
	popDialog(popUrl+'paths',200,200,page_cbkMove,form);
	return false;
}

function inflateHeight(element)
{
	if (element==null)
		return;
		
	var par=element.parentNode;
	var parentHeight=par.offsetHeight;
	var siblingHeight=0;
	var sibling=par.firstChild;
	while(sibling!=null)
	{
		if (sibling!=element)
			siblingHeight+=sibling.offsetHeight;
		sibling=sibling.nextSibling;
	}
	element.style.height=(parentHeight-siblingHeight)+"px";
	element.style.minHeight='0';
}

function pop_window(url,width,height)
{
	fullscreen=false;
	if (width==null)
	{
		width=screen.width;
		height=screen.height;
		fullscreen=true;
	}
	x=(screen.width-width)/2;
	y=(screen.height-height)/2;
	day = new Date();
	id = day.getTime();
	eval('page' + id + " = window.open('"+url+"','"+id+"', 'toolbar=0,scrollbars=0,location=0,fullscreen="+((fullscreen)?'1':'0')+",statusbar=0,menubar=0,resizable=1,width=" +width+",height="+height+",screenX="+x+",screenY="+y+"');");
}