

function OpenPopupWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}



function OpenSecurityWindow( url ) {

	smtools.permsWindow.setContentUrl( url );
	smtools.permsWindow.showCenter();

}

function OpenFileEditorWindow( url ) {

	smtools.fileEditorWindow.setContentUrl( url );
	smtools.fileEditorWindow.showCenter();

}


function OpenWindow( id, url )
{
	var win = Windows.getWindow( id );


	if ( win != null )
	{
		if ( url != null )
		{
			win.setContentUrl( url );
		}

		win.showCenter();
	}
	else
	{
		alert( 'Window ' + id + ' not found!');
	}
}



function conf_go(string, actionStr) {
	string3 = "Are you sure you want to " + actionStr + " this item?";
	if (confirm(string3)) {
		window.location.href=string;
	}
}


function confirmAjaxCall(string, call, args ) {
	if (confirm(string)) {
		
		if ( ! call )
		{
			alert( 'Function not found: ' + call );
			return false;
		}
		
		if ( args )
		{
			call( args, '' );
		}
		else
		{
			call();
		}
	}
}


function getForm( input )
{
	for ( var i = 0; i < document.forms.length; i++ )
	{
		f = document.forms[ i ];
		for(var x=0; x < f.elements.length; x++)
		{
			if ( f.elements[ x ].id == input )
			{
				return f;
			}
		}

	}

	return null;

}


function checkTPSelections( input, options )
{
	form = getForm( input.id );
	if ( form == null )
	{
		alert( 'input element is not in a form.');
		return false;
	}

	var good = false;
	len = form.elements.length;
	for(var i=0; i < len; i++)
	{
		if ( form.elements[i].checked )
		{
			good = true;
			break;
		}
	}

	if ( ! good )
	{
		alert( 'No selection has been made');
		return false;
	}


	if ( options ['confirm' ] != '' )
	{
		if ( ! confirm( options ['confirm' ] ) )
		{
			return false;
		}
	}


	form.btnPressed.value = input.id;

	if ( options[ 'onclick' ] == null )
	{
		form.submit();
		return true;
	}


	var cmd = options[ 'onclick'] + '( input, \'' + options[ 'onreturn'] + '\');';

	eval( cmd );

	return true;
}



function ToggleAll(formid, checked) {

	form = document.getElementById( formid );

	if ( form == null )
	{
		alert( 'no form found: ' + formid );
		return false;
	}

	len = form.elements.length;
	for(var i=0; i < len; i++)
	{
		form.elements[i].checked=checked;
	}
}


function ToggleCheckBoxes( formid, cb )
{
	ToggleAll( formid,  cb.checked );
}

function ToggleAllLinks( )
{
	elements = document.getElementsByTagName( "div" );

	for( i = 1; i < elements.length; i++ )
	{
		el = elements[ i ];

		if ( el != null && el.className == "linkok" )
		{
			if ( el.style.display == "block" )
			el.style.display = "none";
			else
			el.style.display = "block";
		}
	}

}


function EnableWindow( windowId )
{
	var el = document.getElementById( windowId );

	if( el )
	{
		el.style.display='inline';
	}

	return true;
}

function HideWindow( windowId )
{
	var el = document.getElementById( windowId );

	if( el )
	{
		el.style.display='none';
	}

	return false;
}



// JavaScript Document
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
	if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
		document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
		else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
	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=MM_findObj(n,d.layers[i].document);
		if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
	var i,p,v,obj,args=MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
	if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
	obj.visibility=v; }
}
var hide  = true;
function showhide(obj,lyr)
{
	var x = new getObj(lyr);
	hide = !hide;
	x.style.visibility = (hide) ? 'hidden' : 'visible';
	setLyr(obj,lyr);
}

function setLyr(obj,lyr)
{
	var newX = findPosX(obj);
	var newY = findPosY(obj)+20; // should be same as the height of links2 in CSS file..default 30px;
	if (lyr == 'testP') newY -= 50;
	var x = new getObj(lyr);
	x.style.top = newY + 'px';
	x.style.left = newX + 'px';
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
	curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
	curtop += obj.y;
	return curtop;
}


function getObj(name)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers)
	{
		if (document.layers[name])
		{
			this.obj = document.layers[name];
			this.style = document.layers[name];
		}
		else
		{
			this.obj = document.layers.testP.layers[name];
			this.style = document.layers.testP.layers[name];
		}
	}
}


function editModule( id, url, version )
{
	if ( url == '' || url == null )
	{
		url = 'htmleditor.php?a=edit&id=' + id;

		if ( version != null )
		url += '&version=' + version;
	}

	smtools.editorWindow.setContentUrl( url );
	smtools.editorWindow.showCenter();

}




function RenameFile( oldFile, newFile )
{
	document.fmMenuForm.a.value = 'renameFile';
	document.fmMenuForm.renameFile.value = oldFile;
	document.fmMenuForm.renameFileNewName.value = newFile;

	document.fmMenuForm.submit();

	return true;
}



function updateDD( sectionId, moduleId, prevId )
{

	if ( smtools.ajaxMoveModule != null )
	{
		var cmd = smtools.ajaxMoveModule + '( sectionId, moduleId, prevId, "" );';
		eval( cmd );
	}


	return true;
}



function ToggleModuleButtons( id )
{
	var btns = document.getElementById( 'modulemenubtn' + id );

	if ( btns.style.display != 'none' )
	{
		btns.style.display = 'none';
	}
	else
	{
		btns.style.display = 'block';
	}

}

function RefreshModule( id )
{


	//	pausecomp( 2000 );
	smtools.moduleIdToReload = id;

	title = document.getElementById( 'moduleTitle' + id );
	if ( title )
	{
		title.innerHTML = 'Reloading...';
	}

	smtools.ajaxRefreshModule( id, ResetModuleDD );

}

function AutoRefreshModule( jscron )
{
	jscron.stop();
	RefreshModule( jscron.args )
}


function UpdateModuleContent( id, skipRefresh )
{
	if ( smtools.editorWindow )
	{
		smtools.editorWindow.hide();
	}

	if ( skipRefresh == null )
	{
		smtools.refreshTimer = new JSCron( AutoRefreshModule, id, 0 );
		smtools.refreshTimer.start();
	}
}



function ResetModuleDD( args )
{
	if ( smtools.moduleIdToReload == null )
	{
		alert( 'No module found.');
		return;
	}

	var div = document.getElementById( 'module' + smtools.moduleIdToReload );

	if ( div == null )
	{
		alert( 'No module found.');
		return;
	}

	div.innerHTML = args;

	dndMgr.registerDraggable( new Rico.Draggable('test-rico-dnd', 'module' + smtools.moduleIdToReload , 'moduleMenu' + smtools.moduleIdToReload ) );


	smtools.moduleIdToReload = null;
}

function ResetModuleContent( id, content )
{
	if ( id == null )
	{
		alert( 'No module found.');
		return;
	}

	var div = document.getElementById( 'module' + id );

	if ( div == null )
	{
		alert( 'No module found.');
		return;
	}

	div.innerHTML = content;

	dndMgr.registerDraggable( new Rico.Draggable('test-rico-dnd', 'module' + id , 'moduleMenu' + id ) );
}


var SM = {
	Version: 0.1,
	prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1])
}

if((typeof Prototype=='undefined') || SM.prototypeVersion < 1.3)
{
	throw("SiteManager requires the Prototype JavaScript framework >= 1.3");
}




SM.Tools = Class.create();

SM.Tools.prototype = {

	initialize: function()
	{
		this.ajaxMoveModule = null;
		this.ajaxRemoveModule = null;

		this.moduleToRemove = null;

		this.ajaxToolbarUpdate = null;

		this.sectionToAdd = null;

		this.ajaxAddModule = null;

		this.ajaxRefreshModule = null;

		this.moduleIdToReload = null;

		this.editorwindow = null;

		this.refreshTimer = null;

		this.permsWindow = null;

		this.fileEditorWindow = null;

		this.ajaxFileEditorSave = null;
		
		this.configWindow = null;
		
		this.editMenuWindow = null;


	},


	delModule: function ( moduleId )
	{
		if ( ! confirm( 'Are you sure you want to remove this module?') )
		{
			return false;
		}

		this.moduleToRemove = moduleId;

		if ( this.ajaxRemoveModule != null )
		{
			var cmd = this.ajaxRemoveModule + '( moduleId, this.removeModuleDeleted );';
			eval( cmd );
		}

		return true;
	},



	removeModuleDeleted: function( args )
	{
		if (  smtools.moduleToRemove == null )
		{
			return;
		}

		var moduleDiv = document.getElementById( 'module' + smtools.moduleToRemove );

		if ( moduleDiv == null )
		{
			alert( 'Module not found.' );
			return;
		}

		var section = moduleDiv.parentNode;

		if ( section == null )
		{
			alert( 'Invalid section' );
			return;
		}

		section.removeChild( moduleDiv );

		this.moduleToRemove = null;
	},



	addModuleToSection: function( args )
	{
		if ( smtools.sectionToAdd != null )
		{
			if ( args.js != '' )
			{
				eval( args.js );
			}
			
			smtools.sectionToAdd.innerHTML += args.content;
		}

		dndMgr.registerDraggable( new Rico.Draggable('test-rico-dnd', args.id, args.handle ) );
	},


	addModule: function( selBox, pageId, sectionName )
	{
		//	alert( selBox.value + pageId + sectionName );

		this.sectionToAdd = document.getElementById( 'smsection_' + sectionName );

		if ( this.ajaxAddModule != null )
		{
			var cmd = this.ajaxAddModule + '( selBox.value, pageId, sectionName, this.addModuleToSection );';
			eval( cmd );
		}

		selBox.value = null;
	},
	
	EditMenuLink: function( menuId, linkId )
	{
		this.editMenuWindow.showCenter();
	}

}



var JSCron = Class.create();
JSCron.prototype = {
	initialize: function(callback, args, frequency ) {
		this.on = false;
		this.callback = callback;
		this.args = args;
		this.frequency = frequency;
		this.currentlyExecuting = false;


		this.registerCallback();
	},

	registerCallback: function() {
		setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
	},

	stop: function()
	{
		this.on = false;
	},

	start: function()
	{
		this.on = true;
	},

	onTimerEvent: function() {
		if ( this.on && !this.currentlyExecuting) {
			try {
				this.currentlyExecuting = true;
				this.callback( this );
			} catch( e ) {
				this.on = false;
				throw e;

			} finally {
				this.currentlyExecuting = false;
			}
		}
	}
}



function ToggleToolbar()
{

	var btn = document.getElementById( 'togglebtn' );
	var toolbar = document.getElementById( 'adminToolbar' );

	if ( btn.className == 'bon' )
	{
		btn.className = 'boff';
		toolbar.style.display = 'none';
		//		toolbar.style.visibility = 'hidden';
	}
	else
	{
		btn.className = 'bon';

		toolbar.style.display = 'block';
		//		toolbar.style.visibility = 'visible';
	}

	if ( smtools.ajaxToolbarUpdate != null )
	{
		var cmd = smtools.ajaxToolbarUpdate + '( btn.className, "" );';
		eval( cmd );
	}

}


function pausecomp(millis)
{
	date = new Date();
	var curDate = null;

	do { var curDate = new Date(); }
	while(curDate-date < millis);
}


function handleEnterSubmission (field, evt) {
	var keyCode = evt.which ? evt.which : evt.keyCode;
	if (keyCode == 13) {
		field.form.submit();
		return false;
	}
	else
	return true;
}


function renameBtnOnKeyDownHandler( evt, filename, field, nameDiv, fieldDiv  ) {
	var keyCode = evt.which ? evt.which : evt.keyCode;
	if (keyCode == 13) {

		RenameFile( filename, field.value );

		//    field.form.submit();
		return false;
	}
	else if ( keyCode == 27 )
	{
		CancelRenameFile( filename, field, nameDiv, fieldDiv );
	}
	else
	return true;
}


function CancelRenameFile( filename, field, nameDiv, fieldDiv )
{
	field.value = filename;

	HideWindow( fieldDiv );
	EnableWindow( nameDiv );
}


function SaveFileEditorContent( filename, field )
{
	if ( smtools.ajaxFileEditorSave != null )
	{
		sajax_request_type  = "POST";

		sajax_target_id = 'editorMsg';

		document.getElementById( sajax_target_id ).innerHTML = 'Saving...';

		var cmd = smtools.ajaxFileEditorSave + '( filename, field.value, "" );';
		eval( cmd );
	}
}

function FileEditorContentChanged( status )
{
	if ( status )
	{
		document.getElementById( 'editorMsg' ).innerHTML = '';

		document.getElementById( 'btnSaveFileEditor' ).disabled = false;
	}
	else
	{
		document.getElementById( 'btnSaveFileEditor' ).disabled = true;
	}
}



function SubmitForm( form, action )
{
	if ( action != null )
	{
		form.a.value = action;
	}

	form.submit();
}


function SubmitAjaxForm( form, ajaxCall )
{
	sajax_request_type = 'POST';

	for ( i = 0; i < form.elements.length; i++ )
	{
		var field = form.elements[i];
		
		switch( field.type )
		{
			case  'radio':
			if ( field.checked )
			{
				sajax_send_post_vars.push( form.elements[i].name+ '=' + escape( form.elements[i].value ) );
			}
			
			break;
			
			case 'checkbox':
			if ( field.checked )
			{
				sajax_send_post_vars.push( form.elements[i].name+ '=' + escape( form.elements[i].value ) );
			}
			
			break;
			
			default:
				sajax_send_post_vars.push( form.elements[i].name+ '=' + escape( form.elements[i].value ) );
				break;
		}
	}

	if ( ajaxCall )
	{
		ajaxCall();
	}

	return false;
}




var smtools = new SM.Tools();



