1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
4 *
5 * == BEGIN LICENSE ==
6 *
7 * Licensed under the terms of any of the following licenses at your
8 * choice:
9 *
10 *  - GNU General Public License Version 2 or later (the "GPL")
11 *    http://www.gnu.org/licenses/gpl.html
12 *
13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14 *    http://www.gnu.org/licenses/lgpl.html
15 *
16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
17 *    http://www.mozilla.org/MPL/MPL-1.1.html
18 *
19 * == END LICENSE ==
20 *
21 * Useful functions used by almost all dialog window pages.
22 */
23
24// Gets a element by its Id. Used for shorter coding.
25function GetE( elementId )
26{
27	return document.getElementById( elementId )  ;
28}
29
30function ShowE( element, isVisible )
31{
32	if ( typeof( element ) == 'string' )
33		element = GetE( element ) ;
34	element.style.display = isVisible ? '' : 'none' ;
35}
36
37function SetAttribute( element, attName, attValue )
38{
39	if ( attValue == null || attValue.length == 0 )
40		element.removeAttribute( attName, 0 ) ;			// 0 : Case Insensitive
41	else
42		element.setAttribute( attName, attValue, 0 ) ;	// 0 : Case Insensitive
43}
44
45function GetAttribute( element, attName, valueIfNull )
46{
47	var oAtt = element.attributes[attName] ;
48
49	if ( oAtt == null || !oAtt.specified )
50		return valueIfNull ? valueIfNull : '' ;
51
52	var oValue = element.getAttribute( attName, 2 ) ;
53
54	if ( oValue == null )
55		oValue = oAtt.nodeValue ;
56
57	return ( oValue == null ? valueIfNull : oValue ) ;
58}
59
60var KeyIdentifierMap =
61{
62	End		: 35,
63	Home	: 36,
64	Left	: 37,
65	Right	: 39,
66	'U+00007F' : 46		// Delete
67}
68
69// Functions used by text fields to accept numbers only.
70function IsDigit( e )
71{
72	if ( !e )
73		e = event ;
74
75	var iCode = ( e.keyCode || e.charCode ) ;
76
77	if ( !iCode && e.keyIdentifier && ( e.keyIdentifier in KeyIdentifierMap ) )
78			iCode = KeyIdentifierMap[ e.keyIdentifier ] ;
79
80	return (
81			( iCode >= 48 && iCode <= 57 )		// Numbers
82			|| (iCode >= 35 && iCode <= 40)		// Arrows, Home, End
83			|| iCode == 8						// Backspace
84			|| iCode == 46						// Delete
85			|| iCode == 9						// Tab
86	) ;
87}
88
89String.prototype.Trim = function()
90{
91	return this.replace( /(^\s*)|(\s*$)/g, '' ) ;
92}
93
94String.prototype.StartsWith = function( value )
95{
96	return ( this.substr( 0, value.length ) == value ) ;
97}
98
99String.prototype.Remove = function( start, length )
100{
101	var s = '' ;
102
103	if ( start > 0 )
104		s = this.substring( 0, start ) ;
105
106	if ( start + length < this.length )
107		s += this.substring( start + length , this.length ) ;
108
109	return s ;
110}
111
112String.prototype.ReplaceAll = function( searchArray, replaceArray )
113{
114	var replaced = this ;
115
116	for ( var i = 0 ; i < searchArray.length ; i++ )
117	{
118		replaced = replaced.replace( searchArray[i], replaceArray[i] ) ;
119	}
120
121	return replaced ;
122}
123
124function OpenFileBrowser( url, width, height )
125{
126	// oEditor must be defined.
127
128	var iLeft = ( oEditor.FCKConfig.ScreenWidth  - width ) / 2 ;
129	var iTop  = ( oEditor.FCKConfig.ScreenHeight - height ) / 2 ;
130
131	var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes,scrollbars=yes" ;
132	sOptions += ",width=" + width ;
133	sOptions += ",height=" + height ;
134	sOptions += ",left=" + iLeft ;
135	sOptions += ",top=" + iTop ;
136
137	// The "PreserveSessionOnFileBrowser" because the above code could be
138	// blocked by popup blockers.
139	if ( oEditor.FCKConfig.PreserveSessionOnFileBrowser && oEditor.FCKBrowserInfo.IsIE )
140	{
141		// The following change has been made otherwise IE will open the file
142		// browser on a different server session (on some cases):
143		// http://support.microsoft.com/default.aspx?scid=kb;en-us;831678
144		// by Simone Chiaretta.
145		var oWindow = oEditor.window.open( url, 'FCKBrowseWindow', sOptions ) ;
146
147		if ( oWindow )
148		{
149			// Detect Yahoo popup blocker.
150			try
151			{
152				var sTest = oWindow.name ; // Yahoo returns "something", but we can't access it, so detect that and avoid strange errors for the user.
153				oWindow.opener = window ;
154			}
155			catch(e)
156			{
157				alert( oEditor.FCKLang.BrowseServerBlocked ) ;
158			}
159		}
160		else
161			alert( oEditor.FCKLang.BrowseServerBlocked ) ;
162    }
163    else
164		window.open( url, 'FCKBrowseWindow', sOptions ) ;
165}
166