	var cm=false;
	var iFrameOb=false;
	var iFrameOpen=false;
	var iFrameIX=false;
	var iFrameWidth=600;
	var iFrameHeight=600;

	function launchClassFieldDetails(ix, field) {
		if(field) {
			field = "&f=" + field;
		}
		else field = '';

		setupIFrame(ix);
		iFrameOb.src="/admin/dev_tools/blank.php?ix="+ix+"&r="+Math.random() + field;
	}

	function showFormSubmission(ix){
		setupIFrame(ix);
		iFrameOb.src="/admin/dev_tools/formDetails.php?ix="+ix+"&r="+Math.random();
	}

	function setupIFrame(ix){
		var w,h,sX,sY;
		if(!iFrameOb)
			iFrameOb=document.getElementById('shopOverlayIFrame');

		if(iFrameOpen && ix==iFrameIX){
			closeSpecialWindow();
			return;
		}

		greyOutWindow();

		iFrameIX=ix;
		iFrameOb.style.left=(getScrollX()+((getWindowWidth()-(iFrameWidth+4))/2))+'px';
		iFrameOb.style.top=(getScrollY()+((getWindowHeight()-(iFrameHeight+4))/2))+'px';
		iFrameOb.style.display="";
		iFrameOpen=true;
	}

	function closeSpecialWindow(){
		iFrameOb.style.left="-1000px";
		iFrameOb.style.top=0;
		iFrameOb.style.display="none";
		iFrameOpen=false;
	}

	function createUEData(e){
		var f=document.forms[0];
		var n=e.name.toString().substring(0,e.name.toString().length-5);
		var newType=e[e.selectedIndex].value;
		var tv=f[n].value.toString().split('~');
		f[n].value=newType+'~'+(tv.length>1?tv[1]:'');
	}

	/**
	 * Used in Widget picker popup
	 */
	function openFolder(x){
		alert(x);
	}

	/**
	 * Originally in admin.js
	 */
	function getWindowHeight(){
		var h=0;
		if(document.all){
			if(document.documentElement && document.documentElement.clientHeight)
				h=document.documentElement.clientHeight;
			else
				h=document.body.clientHeight;
		}else{
			h=window.innerHeight;
		}
		return h;
	}
	
	function getWindowWidth(){
		var w=0;
		if(document.all){
			if(document.documentElement && document.documentElement.clientWidth)
				w=document.documentElement.clientWidth;
			else
				w=document.body.clientWidth;
		}else{
			w=window.innerWidth;
		}
		return w;
	}
	
	function getScrollY(){
		return(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop;
	}
	
	function getScrollX(){
		return (document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft;
	}

	function getPageWidth(){
		return(document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth; 
	}

	function getPageHeight(){
		return(document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
	}
	
	// URL tree picker launcher...
	// t can be either 'single' or 'multiple'
	// ob is the form object name that will store the result
	// st is the start object (can be null/not specified)
	function launchURLPicker(t,ob,st){
		openSpecialWindow();
		currentOb=ob;
		iFrameOb.src="/media/site/helpers/urlPicker.php?callBack=parent.urlPicker&pickType="+t+(st?'&startOb='+st:'')+"&r="+Math.random();
	}

	var greyDiv=false;
	var greyOpacity=0;
	var greyMaxOpacity=0.7;
	var greyChangeDir=0;
	var greyDivShowing=false;
	var greyTmr=false;

	function greyOutWindowFast(){
		greyOutWindow(true);
	}

	function greyOutWindow(fast){
		if(greyDivShowing)
			return;
		if(!greyDiv)
			greyDiv=document.getElementById('greyer');
		if(!greyDiv)
			return;
		greyDivShowing=true;
		setElementOpacity(greyDiv,(fast?greyMaxOpacity:0.2));
		greyDivOpacity=(fast?greyMaxOpacity:0.2);
		greyDiv.style.display='';
		greyDiv.style.position='absolute';
		greyDiv.style.backgroundColor='#ffffff';
		greyDiv.style.padding=0;
		greyDiv.style.width="100%"; //getPageWidth()+'px';
		greyDiv.style.height=getPageHeight()+'px';
		greyDiv.style.top=0;
		greyDiv.style.left=0;
		greyChangeDir=0.16;
		if(fast)
			return;
		greyTmr=setTimeout('greyChange()',50);
	}

	function greyChange(){
		greyOpacity+=greyChangeDir;
		if(greyOpacity>greyMaxOpacity){
			greyOpacity=greyMaxOpacity;
			greyTmr=false;
		}else if(greyOpacity<0){
			greyOpacity=0;
			greyTmr=false;
			greyDiv.style.display='none';
			greyDivShowing=false;
		}else{
			greyTmr=setTimeout('greyChange()',40);
		}
		setElementOpacity(greyDiv,greyOpacity);
	}

	function clearGreyOutWindow(){
		if(!greyDivShowing)
			return;
		greyChangeDir=-0.2;
		if(!greyTmr)
			greyTmr=setTimeout('greyChange()',40);
	}

	/**
	 * Set opacity of an element (range 0 to 1)
	 */
	function setElementOpacity(ob,v){
		if(v<0)
			v=0;
		else if(v>1)
			v=1;
		if(!ob || !ob.style)
			return;
		if(document.all){
			ob.style.filter='Alpha(opacity='+Math.round(v*100)+')';
		}else{
			ob.style.opacity=v;
		}
	}

	/**
	 * Launch a widget picker window
	 */
	function launchWidgetPicker(ix,title,group,currentWidget){
		var url="/media/site/helpers/widgetPicker.php?group="+group+"&ix="+ix+"&currentWidget="+currentWidget+"&callBack=pickWidget&folderIcon=widget_folder.png&objectIcon=widget.png&title="+title+"&currentVirtualPrefix="+currentVirtualPrefix+"&r="+Math.random();
		openSpecialWindow(url);
	}


	/**
	 * Launch a widget editor window
	 */
	function launchWidgetEditor(ix,title,group,currentWidget){
		var url="/media/site/helpers/widgetEditor.php?object="+obId+"&ix="+ix+"&currentWidget="+currentWidget+"&callBack=editWidget&title="+title+"&currentVirtualPrefix="+currentVirtualPrefix+"&r="+Math.random();
		openSpecialWindow(url,760,540);
	}

	var iFrameOpacity=0;
	var iFrameMaxOpacity=0.9;
	var iFrameChangeDir=0.2;
	var iFrameTmr=false;

	function openSpecialWindow(src,w,h){

		if(!iFrameOb )
		{
			iFrameOb=document.getElementById('shopOverlayIFrame');
		}
		if(iFrameOpen){
			closeSpecialWindow();
			return;
		}
		if(cm)
			cm.hideHelpBox();
		greyOutWindow();

		if(!w)
			w=600;
		if(!h)
			h=400;
		setElementOpacity(iFrameOb,0)
		iFrameWidth=w;
		iFrameHeight=h;

		
		iFrameOb.style.width=w+'px';
		iFrameOb.style.height=h+'px';
		iFrameOb.style.left=( ( ( getWindowWidth() + (getScrollX()*2) ) - (iFrameWidth+4) ) /2 )+'px';
		iFrameOb.style.top=( ( ( getWindowHeight() + (getScrollY()*2) ) - (iFrameHeight+4) ) /2 )+'px';
		iFrameOb.style.display="";
		iFrameOpen=true;
		if(src)
			iFrameOb.src=src;
		iFrameTmr=setTimeout('fadeUpIFrame()',50);
	}

	function fadeUpIFrame(){
		iFrameOpacity+=iFrameChangeDir;
		if(iFrameOpacity>iFrameMaxOpacity){
			iFrameOpacity=iFrameMaxOpacity;
			iFrameTmr=false;
		}else{
			iFrameTmr=setTimeout('fadeUpIFrame()',50);
		}
		setElementOpacity(iFrameOb,iFrameOpacity);
	}

	function closeSpecialWindow(){
		clearGreyOutWindow();
		iFrameOb.style.left="-1000px";
		iFrameOb.style.top=0;
		iFrameOb.style.display="none";
		iFrameOb.src='/media/site/helpers/blank.html';
		iFrameOpen=false;
	}

	function createMediaItem(cDir){
		openSpecialWindow('/media/site/helpers/editMediaItem.php?item='+cDir+"&cmd=newItem",450,300);
	}

	function viewMediaItem(url,w,h){
		if(!w || w>180){
			if(!w){
				w=800;
				h=600;
			}
		}
		var win=window.open('/media/site/helpers/showMediaItem.php?item='+url,'mediaItemPopup',"status=0,toolbar=0,scrollbars=1,resizable=1,width="+w+",height="+h);
		win.focus();
	}

	function deleteMediaItem(url){
		if(!confirm("Please confirm you wish to delete this item"))
			return false;
		var pth=url.split('/');
		var file=pth[pth.length-1];
		var dir='';
		for(var n=0;n<pth.length-1;n++){
			if(pth[n]!='')
				dir+='/'+pth[n];
		}
		top.location.href="/admin/MediaCentre?dir="+dir+'&deleteFile='+file;
	}

	function editMediaItem(url){
		openSpecialWindow('/media/site/helpers/editMediaItem.php?item='+url,450,300);
	}

	currentFld='';

	/**
	 * Open the media centre picker
	 */
	function openMediaCentrePicker(filterType,fieldName){
		currentField=fieldName;
		openSpecialWindow('/media/site/helpers/pickMediaItem.php?filter='+filterType,750,500);
	}

	/**
	 * Callback from the media centre picker
	 */
	function chooseMediaItem(url){
		document.forms[0][currentField].value=url;
		var e=document.getElementById('hider_'+currentField);
		if(e)
			e.innerHTML='<span style="color:#ff0000">&lt;Media Centre Item Selected&gt;</span>&nbsp;';
		closeSpecialWindow();
	}

	/**
	 * Trim the space from the beginning and end of a
	 * string, also convert double spaces to single
	 */
	function trim(s){
		s=s.replace(/[^a-zA-Z0-9\-_ ]/g,'');
		s=s.replace(/\s{2,}/g,' ');
		s=s.replace(/^\s+/g,'');
		return s.replace(/\s+$/g,'');
	}

	/**
	 * Generic attach event function. Used all over the place
	 */
	function attachDomEvent(ob,eType,f,cap){
        if(ob.addEventListener)
                ob.addEventListener(eType,f,cap);
        else if(ob.attachEvent)
                ob.attachEvent("on"+eType,f);
        else if(navigator.userAgent.toLowerCase().indexOf('mac')!=-1||navigator.userAgent.toLowerCase().indexOf('apple')!=-1)
                eval('ob.on'+eType+'='+f);
        else
                alert("Handler could not be added");
	}

	/**
	 * returns the dimensions of a block element (x,y,width,height) as an array
	 */
	function findObDimensions(ob){
		var width=ob.offsetWidth;
		var height=ob.offsetHeight;
		var x=y=0;
		if (ob.offsetParent){
			x=ob.offsetLeft
			y=ob.offsetTop
			while (ob=ob.offsetParent){
				x+=ob.offsetLeft
				y+=ob.offsetTop
			}
		}
		return [x,y,width,height];
	}

// form builder - disable button for certain things
function enableDisableDetailsButton(select, id) {
	var elm = document.getElementById(id);

	if(!elm) return;

	var allowable = ['richtext', 'select', 'radio', 'checkboxes']; // fields that need extra data
	var selected  = select.options[select.selectedIndex].value; // what has just been selected
	var hidden    = select.form.elements[select.name.replace('[type]', '[special]')];

	// it's now richtext it wasn't before
	if(selected == 'richtext' && select.getAttribute("defaultValue") != 'richtext') {
		hidden.value = '';
	}
	else if(select.getAttribute("defaultValue") == 'richtext' && selected != 'richtext') {
		hidden.value = '';
	}
	else hidden.value = hidden.defaultValue;

	for(var i = 0; i < allowable.length; i++) {
		if(allowable[i] == selected) {
			elm.disabled = false;
			return;
		}
	}

	elm.disabled = true;
}


