1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2009 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 * Scripts for the fck_select.html page.
22 */
23
24function Select( combo )
25{
26	var iIndex = combo.selectedIndex ;
27
28	oListText.selectedIndex		= iIndex ;
29	oListValue.selectedIndex	= iIndex ;
30
31	var oTxtText	= document.getElementById( "txtText" ) ;
32	var oTxtValue	= document.getElementById( "txtValue" ) ;
33
34	oTxtText.value	= oListText.value ;
35	oTxtValue.value	= oListValue.value ;
36}
37
38function Add()
39{
40	var oTxtText	= document.getElementById( "txtText" ) ;
41	var oTxtValue	= document.getElementById( "txtValue" ) ;
42
43	AddComboOption( oListText, oTxtText.value, oTxtText.value ) ;
44	AddComboOption( oListValue, oTxtValue.value, oTxtValue.value ) ;
45
46	oListText.selectedIndex = oListText.options.length - 1 ;
47	oListValue.selectedIndex = oListValue.options.length - 1 ;
48
49	oTxtText.value	= '' ;
50	oTxtValue.value	= '' ;
51
52	oTxtText.focus() ;
53}
54
55function Modify()
56{
57	var iIndex = oListText.selectedIndex ;
58
59	if ( iIndex < 0 ) return ;
60
61	var oTxtText	= document.getElementById( "txtText" ) ;
62	var oTxtValue	= document.getElementById( "txtValue" ) ;
63
64	oListText.options[ iIndex ].innerHTML	= HTMLEncode( oTxtText.value ) ;
65	oListText.options[ iIndex ].value		= oTxtText.value ;
66
67	oListValue.options[ iIndex ].innerHTML	= HTMLEncode( oTxtValue.value ) ;
68	oListValue.options[ iIndex ].value		= oTxtValue.value ;
69
70	oTxtText.value	= '' ;
71	oTxtValue.value	= '' ;
72
73	oTxtText.focus() ;
74}
75
76function Move( steps )
77{
78	ChangeOptionPosition( oListText, steps ) ;
79	ChangeOptionPosition( oListValue, steps ) ;
80}
81
82function Delete()
83{
84	RemoveSelectedOptions( oListText ) ;
85	RemoveSelectedOptions( oListValue ) ;
86}
87
88function SetSelectedValue()
89{
90	var iIndex = oListValue.selectedIndex ;
91	if ( iIndex < 0 ) return ;
92
93	var oTxtValue = document.getElementById( "txtSelValue" ) ;
94
95	oTxtValue.value = oListValue.options[ iIndex ].value ;
96}
97
98// Moves the selected option by a number of steps (also negative)
99function ChangeOptionPosition( combo, steps )
100{
101	var iActualIndex = combo.selectedIndex ;
102
103	if ( iActualIndex < 0 )
104		return ;
105
106	var iFinalIndex = iActualIndex + steps ;
107
108	if ( iFinalIndex < 0 )
109		iFinalIndex = 0 ;
110
111	if ( iFinalIndex > ( combo.options.length - 1 ) )
112		iFinalIndex = combo.options.length - 1 ;
113
114	if ( iActualIndex == iFinalIndex )
115		return ;
116
117	var oOption = combo.options[ iActualIndex ] ;
118	var sText	= HTMLDecode( oOption.innerHTML ) ;
119	var sValue	= oOption.value ;
120
121	combo.remove( iActualIndex ) ;
122
123	oOption = AddComboOption( combo, sText, sValue, null, iFinalIndex ) ;
124
125	oOption.selected = true ;
126}
127
128// Remove all selected options from a SELECT object
129function RemoveSelectedOptions(combo)
130{
131	// Save the selected index
132	var iSelectedIndex = combo.selectedIndex ;
133
134	var oOptions = combo.options ;
135
136	// Remove all selected options
137	for ( var i = oOptions.length - 1 ; i >= 0 ; i-- )
138	{
139		if (oOptions[i].selected) combo.remove(i) ;
140	}
141
142	// Reset the selection based on the original selected index
143	if ( combo.options.length > 0 )
144	{
145		if ( iSelectedIndex >= combo.options.length ) iSelectedIndex = combo.options.length - 1 ;
146		combo.selectedIndex = iSelectedIndex ;
147	}
148}
149
150// Add a new option to a SELECT object (combo or list)
151function AddComboOption( combo, optionText, optionValue, documentObject, index )
152{
153	var oOption ;
154
155	if ( documentObject )
156		oOption = documentObject.createElement("OPTION") ;
157	else
158		oOption = document.createElement("OPTION") ;
159
160	if ( index != null )
161		combo.options.add( oOption, index ) ;
162	else
163		combo.options.add( oOption ) ;
164
165	oOption.innerHTML = optionText.length > 0 ? HTMLEncode( optionText ) : '&nbsp;' ;
166	oOption.value     = optionValue ;
167
168	return oOption ;
169}
170
171function HTMLEncode( text )
172{
173	if ( !text )
174		return '' ;
175
176	text = text.replace( /&/g, '&amp;' ) ;
177	text = text.replace( /</g, '&lt;' ) ;
178	text = text.replace( />/g, '&gt;' ) ;
179
180	return text ;
181}
182
183
184function HTMLDecode( text )
185{
186	if ( !text )
187		return '' ;
188
189	text = text.replace( /&gt;/g, '>' ) ;
190	text = text.replace( /&lt;/g, '<' ) ;
191	text = text.replace( /&amp;/g, '&' ) ;
192
193	return text ;
194}
195