// JavaScript Document


// Ajax Call function - Generic Page Content Editor
function setDiv(x) 
{
	var sendValue;
	sendValue= document.getElementById(x).value;
	
	//alert('X : '+x+' val : '+sendValue+' ');
	if(timer !='running')
	{
		switch(x)
		{
			case 'editor' 	:  ajaxTalk( 'ajax_editor.php?snd='+sendValue+'&m='+ Math.floor(Math.random()*10000)+'',  'editor' );  			break;
		}
	
	}
	//alert('End AJAX');	pausecomp(200);
	
runTimer();
	//CreateEditor();
}	

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

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


function Toggle()
{
	//alert('To Save your Changes \n\nClick on Save Changes at bottom of Page ');
	
	// Try to get the FCKeditor instance, if available.
	var oEditor ;
	if ( typeof( FCKeditorAPI ) != 'undefined' )
		oEditor = FCKeditorAPI.GetInstance( 'DataFCKeditor' ) ;

	// Get the _Textarea and _FCKeditor DIVs.
	var eTextareaDiv	= document.getElementById( 'Textarea' ) ;
	var eFCKeditorDiv	= document.getElementById( 'FCKeditor' ) ;
	
	//alert('Textarea DIV : '+ eTextareaDiv.style.display);

	// If the _Textarea DIV is visible, switch to FCKeditor.
	if ( eTextareaDiv.style.display == 'none' )
	{
			timer = 'running';
		// If it is the first time, create the editor.
		if ( !oEditor )
		{
			CreateEditor() ;
		}
		else
		{
			// Set the current text in the textarea to the editor.
			oEditor.SetData( document.getElementById('DataTextarea').value ) ;
			//oEditor.SetData( document.getElementById('initialContent').value ) ;
		}

		// Switch the DIVs display.
		eTextareaDiv.style.display = 'none' ;
		eFCKeditorDiv.style.display = '' ;

		// This is a hack for Gecko 1.0.x ... it stops editing when the editor is hidden.
		if ( oEditor && !document.all )
		{
			if ( oEditor.EditMode == FCK_EDITMODE_WYSIWYG )
				oEditor.MakeEditable() ;
		}
	}
	else
	{
		// Set the textarea value to the editor value.
		document.getElementById('DataTextarea').value = oEditor.GetXHTML() ;

		// Switch the DIVs display.
		eTextareaDiv.style.display = '' ;
		eFCKeditorDiv.style.display = 'none' ;
	}
	eTextareaDiv.style.display = 'none' ;
}

function CreateEditor()
{
	
	// Copy the value of the current textarea, to the textarea that will be used by the editor.
	//document.getElementById('DataFCKeditor').value = document.getElementById('DataTextarea').value ;

	// Automatically calculates the editor base path based on the _samples directory.
	// This is usefull only for these samples. A real application should use something like this:
	// oFCKeditor.BasePath = '/fckeditor/' ;	// '/fckeditor/' is the default value.
	var sBasePath = document.location.href.substring(0,document.location.href.lastIndexOf('_samples')) ;

	// Create an instance of FCKeditor (using the target textarea as the name).
	var oFCKeditor = new FCKeditor( 'DataFCKeditor' ) ;
	//oFCKeditor.Config['ToolbarStartExpanded'] = false ;
	oFCKeditor.BasePath = sBasePath ;
	oFCKeditor.Width = '100%' ;
	oFCKeditor.Height = '750' ;
	//oFCKeditor.ToolbarSet	= 'Basic' ;
	oFCKeditor.Config['SkinPath'] = '/editor/skins/silver/' ;
	oFCKeditor.ReplaceTextarea() ;
	//oFCKeditor.Value = document.getElementById('initialContent').value;
	//oFCKeditor.Create() ;
}

// The FCKeditor_OnComplete function is a special function called everytime an
// editor instance is completely loaded and available for API interactions.
function FCKeditor_OnComplete( editorInstance )
{
	// Enable the switch button. It is disabled at startup, waiting the editor to be loaded.
	//document.getElementById('BtnSwitchTextarea').disabled = false ;
}

function PrepareSave()
{
	// If the textarea isn't visible update the content from the editor.
	if ( document.getElementById( 'Textarea' ).style.display == 'none' )
	{
		var oEditor = FCKeditorAPI.GetInstance( 'DataFCKeditor' ) ;
		document.getElementById( 'DataTextarea' ).value = oEditor.GetXHTML() ;
	}
}