// ADDED TO SUPPORT DHTML

String.prototype.trim = function()
{
	return this.replace( /(^\s*)|(\s*$)/g, '' ) ;
}

var FCKeditorAPI=new Object();

function FCKeditorAPI_GetInstance(instanceName)
  {
  return this.__Instances[instanceName];
  };

FCKeditorAPI.__Instances=new Object();
FCKeditorAPI.Version='2.2';
FCKeditorAPI.GetInstance=FCKeditorAPI_GetInstance;

var __FCKeditorNS = new Object();

__FCKeditorNS.FCKBrowserInfo=new Object();
var sAgent=navigator.userAgent.toLowerCase();
__FCKeditorNS.FCKBrowserInfo.IsIE=(sAgent.indexOf("msie")!=-1);
__FCKeditorNS.FCKBrowserInfo.IsGecko=!__FCKeditorNS.FCKBrowserInfo.IsIE;
__FCKeditorNS.FCKBrowserInfo.IsSafari=(sAgent.indexOf("safari")!=-1);
__FCKeditorNS.FCKBrowserInfo.IsNetscape=(sAgent.indexOf("netscape")!=-1);

var FCKEvents;

if ( !( FCKEvents = __FCKeditorNS.FCKEvents ) )
{
	FCKEvents = __FCKeditorNS.FCKEvents = function( eventsOwner )
	{
		this.Owner = eventsOwner ;
		this.RegisteredEvents = new Object() ;
	}

	FCKEvents.prototype.AttachEvent = function( eventName, functionPointer )
	{
		if ( ! this.RegisteredEvents[ eventName ] ) this.RegisteredEvents[ eventName ] = new Array() ;

		this.RegisteredEvents[ eventName ][ this.RegisteredEvents[ eventName ].length ] = functionPointer ;
	}

	FCKEvents.prototype.FireEvent = function( eventName, params )
	{
		var bReturnValue = true ;

		var oCalls = this.RegisteredEvents[ eventName ] ;
		if ( oCalls )
		{
			for ( var i = 0 ; i < oCalls.length ; i++ )
				bReturnValue = ( oCalls[ i ]( this.Owner, params ) && bReturnValue ) ;
		}

		return bReturnValue ;
	}
}

var FCKCodeFormatter;

if ( !( FCKCodeFormatter = __FCKeditorNS.FCKCodeFormatter ) )
{
	FCKCodeFormatter = __FCKeditorNS.FCKCodeFormatter = new Object() ;

	FCKCodeFormatter.Regex = new Object() ;

	// Regex for line breaks.
	FCKCodeFormatter.Regex.BlocksOpener = /\<(P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|OL|UL|LI|TITLE|META|LINK|BASE|SCRIPT|LINK|TD|TH|AREA|OPTION)[^\>]*\>/gi ;
	FCKCodeFormatter.Regex.BlocksCloser = /\<\/(P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|OL|UL|LI|TITLE|META|LINK|BASE|SCRIPT|LINK|TD|TH|AREA|OPTION)[^\>]*\>/gi ;

	FCKCodeFormatter.Regex.NewLineTags	= /\<(BR|HR)[^\>]\>/gi ;

	FCKCodeFormatter.Regex.MainTags = /\<\/?(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR)[^\>]*\>/gi ;

	FCKCodeFormatter.Regex.LineSplitter = /\s*\n+\s*/g ;

	// Regex for indentation.
	FCKCodeFormatter.Regex.IncreaseIndent = /^\<(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL)[ \/\>]/i ;
	FCKCodeFormatter.Regex.DecreaseIndent = /^\<\/(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL)[ \>]/i ;
	FCKCodeFormatter.Regex.FormatIndentatorRemove = new RegExp( '' ) ;

	FCKCodeFormatter.Regex.ProtectedTags = /(<PRE[^>]*>)([\s\S]*?)(<\/PRE>)/gi ;

	FCKCodeFormatter._ProtectData = function( outer, opener, data, closer )
	{
		return opener + '___FCKpd___' + FCKCodeFormatter.ProtectedData.addItem( data ) + closer ;
	}

	FCKCodeFormatter.Format = function( html )
	{
		// Protected content that remain untouched during the
		// process go in the following array.
		FCKCodeFormatter.ProtectedData = new Array() ;
		
		var sFormatted = html.replace( this.Regex.ProtectedTags, FCKCodeFormatter._ProtectData ) ;
	
		// Line breaks.
		 sFormatted		= sFormatted.replace( this.Regex.BlocksOpener, '\n$&' ) ; ;
		sFormatted		= sFormatted.replace( this.Regex.BlocksCloser, '$&\n' ) ;
		sFormatted		= sFormatted.replace( this.Regex.NewLineTags, '$&\n' ) ;
		sFormatted		= sFormatted.replace( this.Regex.MainTags, '\n$&\n' ) ;
		
		// Indentation.
		var sIndentation = '' ;
		
		var asLines = sFormatted.split( this.Regex.LineSplitter ) ;
		sFormatted = '' ;
		
		for ( var i = 0 ; i < asLines.length ; i++ )
		{
			var sLine = asLines[i] ;
			
			if ( sLine.length == 0 )
				continue ;
			
			if ( this.Regex.DecreaseIndent.test( sLine ) )
				sIndentation = sIndentation.replace( this.Regex.FormatIndentatorRemove, '' ) ;

			sFormatted += sIndentation + sLine + '\n' ;
			
			if ( this.Regex.IncreaseIndent.test( sLine ) )
				sIndentation += '' ;
		}
		
		// Now we put back the protected data.
		for ( var i = 0 ; i < FCKCodeFormatter.ProtectedData.length ; i++ )
		{
			var oRegex = new RegExp( '___FCKpd___' + i ) ;
			sFormatted = sFormatted.replace( oRegex, FCKCodeFormatter.ProtectedData[i] ) ;
		}

		return sFormatted.trim() ;
	}
}

var FCKXml;

if( __FCKeditorNS.FCKBrowserInfo.IsIE )
  {
  __FCKeditorNS.CreateXmlObject = function( object )
  {
  	var aObjs ;
  	
  	switch ( object )
  	{
  		case 'XmlHttp' :
  			aObjs = [ 'MSXML2.XmlHttp', 'Microsoft.XmlHttp' ] ;
  			break ;
  				
  		case 'DOMDocument' :
  			aObjs = [ 'MSXML2.DOMDocument', 'Microsoft.XmlDom' ] ;
  			break ;
  	}
  
  	for ( var i = 0 ; i < 2 ; i++ )
  	{
  		try { return new ActiveXObject( aObjs[i] ) ; }
  		catch (e) 
  		{}
  	}
  	
  	if ( FCKLang.NoActiveX )
  	{
  		alert( FCKLang.NoActiveX ) ;
  		FCKLang.NoActiveX = null ;
  	}
  }
  
  if ( !( FCKXml = __FCKeditorNS.FCKXml ) )
  {
  	FCKXml = __FCKeditorNS.FCKXml = function()
  	{
  		this.Error = false ;
  	}
  
  	FCKXml.prototype.LoadUrl = function( urlToCall )
  	{
  		this.Error = false ;
  
  		var oXmlHttp = __FCKeditorNS.CreateXmlObject( 'XmlHttp' ) ;
  
  		if ( !oXmlHttp )
  		{
  			this.Error = true ;
  			return ;
  		}
  
  		oXmlHttp.open( "GET", urlToCall, false ) ;
  		
  		oXmlHttp.send( null ) ;
  		
  		if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 )
  			this.DOMDocument = oXmlHttp.responseXML ;
  		else if ( oXmlHttp.status == 0 && oXmlHttp.readyState == 4 )
  		{
  			this.DOMDocument = __FCKeditorNS.CreateXmlObject( 'DOMDocument' ) ;
  			this.DOMDocument.async = false ;
  			this.DOMDocument.resolveExternals = false ;
  			this.DOMDocument.loadXML( oXmlHttp.responseText ) ;
  		}
  		else
  		{
  			this.Error = true ;
  			alert( 'Error loading "' + urlToCall + '"' ) ;
  		}
  	}
  
  	FCKXml.prototype.SelectNodes = function( xpath, contextNode )
  	{
  		if ( this.Error )
  			return new Array() ;
  
  		if ( contextNode )
  			return contextNode.selectNodes( xpath ) ;
  		else
  			return this.DOMDocument.selectNodes( xpath ) ;
  	}
  
  	FCKXml.prototype.SelectSingleNode = function( xpath, contextNode ) 
  	{
  		if ( this.Error )
  			return ;
  			
  		if ( contextNode )
  			return contextNode.selectSingleNode( xpath ) ;
  		else
  			return this.DOMDocument.selectSingleNode( xpath ) ;
  	}
  }
  }
else
  {
  __FCKeditorNS.CreateXmlObject = function( object )
  {
  	switch ( object )
  	{
  		case 'XmlHttp' :
  			return new XMLHttpRequest() ;
  		case 'DOMDocument' :
  			return document.implementation.createDocument( '', '', null ) ;
  	}
  	return null ;
  }
  
  if ( !( FCKXml = __FCKeditorNS.FCKXml ) )
  {
  	FCKXml = __FCKeditorNS.FCKXml = function()
  	{}
  
  	FCKXml.prototype.LoadUrl = function( urlToCall )
  	{
  		var oFCKXml = this ;
  
  		var oXmlHttp = __FCKeditorNS.CreateXmlObject( 'XmlHttp' ) ;
  		oXmlHttp.open( "GET", urlToCall, false ) ;
  		oXmlHttp.send( null ) ;
  		
  		if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 )
  			this.DOMDocument = oXmlHttp.responseXML ;
  		else if ( oXmlHttp.status == 0 && oXmlHttp.readyState == 4 )
  			this.DOMDocument = oXmlHttp.responseXML ;
  		else
  			alert( 'Error loading "' + urlToCall + '"' ) ;
  	}
  
  	FCKXml.prototype.SelectNodes = function( xpath, contextNode )
  	{
  		var aNodeArray = new Array();
  
  		var xPathResult = this.DOMDocument.evaluate( xpath, contextNode ? contextNode : this.DOMDocument, 
  				this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
  		if ( xPathResult ) 
  		{
  			var oNode = xPathResult.iterateNext() ;
   			while( oNode )
   			{
   				aNodeArray[aNodeArray.length] = oNode ;
   				oNode = xPathResult.iterateNext();
   			}
  		} 
  		return aNodeArray ;
  	}
  
  	FCKXml.prototype.SelectSingleNode = function( xpath, contextNode ) 
  	{
  		var xPathResult = this.DOMDocument.evaluate( xpath, contextNode ? contextNode : this.DOMDocument,
  				this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), 9, null);
  
  		if ( xPathResult && xPathResult.singleNodeValue )
  			return xPathResult.singleNodeValue ;
  		else	
  			return null ;
  	}
  }
  }

__FCKeditorNS.prepareEditor = function( editor, fckTools )
  {
  //alert( "preparing " + editor + " with func=" + fckTools );
  //editor.StartEditor = FCKStartEditor;
  //editor.UpdateLinkedField = noopFunction;
  fckTools.AttachToLinkedFieldFormSubmit = fck_noopFunction;
  editor.Events.AttachEvent( "OnBlur", function( o, param ) { fck_textareaChanged( o ); } );
  }

function fck_noopFunction( ignore )
  {
  }

function fck_textareaChanged( editor )
  {
  editor.UpdateLinkedField();
  
  if( editor.LinkedField && editor.LinkedField.onchange )
    editor.LinkedField.onchange();
  }
