function BrowseMediaManager (objButton)
{
/*	OpenServerBrowser(
		'',
		FCKConfig.ImageBrowserURL,
		FCKConfig.ImageBrowserWindowWidth,
		FCKConfig.ImageBrowserWindowHeight);*/

		var objTextBox = objButton;
		do {
			objTextBox = objTextBox.previousSibling;
		} while (objTextBox.getAttribute('custom_type') != 'SERVERFILE');
		var sTextBoxID = objTextBox.id;

		OpenFileBrowser( '/javascript/fckeditor/editor/filemanager/browser/default/browser.html?Connector=../../../../../../../admin/fck/browser.php&FieldID='+escape(sTextBoxID), screen.width * 0.7, screen.height * 0.7, sTextBoxID ) ;
}

function OpenFileBrowser( url, width, height )
{
	// oEditor must be defined.

	var iLeft = ( screen.width  - width ) / 2 ;
	var iTop  = ( screen.height - height ) / 2 ;

	var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes,scrollbars=yes" ;
	sOptions += ",width=" + width ;
	sOptions += ",height=" + height ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;

	// The "PreserveSessionOnFileBrowser" because the above code could be
	// blocked by popup blockers.
	if ( true )
	{
		// The following change has been made otherwise IE will open the file
		// browser on a different server session (on some cases):
		// http://support.microsoft.com/default.aspx?scid=kb;en-us;831678
		// by Simone Chiaretta.
		var oWindow = window.open( url, 'ServerBrowserWindow', sOptions ) ;

		if ( oWindow )
		{
			// Detect Yahoo popup blocker.
			try
			{
				var sTest = oWindow.name ; // Yahoo returns "something", but we can't access it, so detect that and avoid strange errors for the user.
				oWindow.opener = window ;
			}
			catch(e)
			{
				alert('Some error here: ' + e);
//				alert( oEditor.FCKLang.BrowseServerBlocked ) ;
			}
		}
//		else
//			alert( oEditor.FCKLang.BrowseServerBlocked ) ;
    }
    else
		window.open( url, 'ServerBrowserWindow', sOptions ) ;
}

function SetUrl(sURL, sTextBoxID)
{
	sTextBoxID = sTextBoxID || null;
	if (sTextBoxID != null) {
		document.getElementById(sTextBoxID).value = sURL;
	}
}

function addFieldSet(objFieldSet)
{
	var max = objFieldSet.getAttribute('max') != null ? objFieldSet.getAttribute('max') : -1;
	var currentCount = 0;

	for (key in objFieldSet.childNodes) {
		if (objFieldSet.childNodes[key] != undefined && objFieldSet.childNodes[key].tagName == 'FIELDSET' && objFieldSet.childNodes[key].style.display != 'none')
			currentCount++;
	}

	if (max != -1 && currentCount+1 > max) {
		alert("You can add no more field sets to this group.\nYou should first delete some the field sets to add new ones.");
		return;
	}

	var newFS;
	for (key in objFieldSet.childNodes)
		if (objFieldSet.childNodes[key] != undefined && objFieldSet.childNodes[key].tagName == 'FIELDSET' && objFieldSet.childNodes[key].style.display == 'none')
			newFS = objFieldSet.childNodes[key].cloneNode(true);

	var counter;
	if ((counter = objFieldSet.getAttribute('counter')) != null) {
		counter++;
		objFieldSet.setAttribute('counter', counter);
	}
	else {
		objFieldSet.setAttribute('counter', 1);
		counter = 1;
	}

	objFieldSet.appendChild(newFS);
	newFS.style.display = 'block';

	enableFields(objFieldSet.getAttribute('name'), counter, newFS);
}

function enableFields(prefix, counter, objParent)
{
	var key;
	for (key in objParent.childNodes) {
		if (objParent.childNodes[key].nodeType == 3)
			continue;
		if (objParent.childNodes[key] != null) {
			switch (objParent.childNodes[key].tagName) {
				case 'FIELDSET':
				case 'SELECT':
				case 'TEXTAREA':
					objParent.childNodes[key].setAttribute('name', prefix +'{'+ counter +'}/'+ objParent.childNodes[key].getAttribute('name'));
				break;

				case 'INPUT':
					objParent.childNodes[key].setAttribute('name', prefix +'{'+ counter +'}/'+ objParent.childNodes[key].getAttribute('name'));
					if (objParent.childNodes[key].getAttribute('custom_type') == 'SERVERFILE')
						objParent.childNodes[key].setAttribute('id', 'SERVERFILE_' + Math.round(Math.random()*1000));
				break;
			}
			if (objParent.childNodes[key].tagName != 'FIELDSET') {
				objParent.childNodes[key].disabled = false;
				enableFields(prefix, counter, objParent.childNodes[key]);
			}
		}
	}
}

function removeFieldSet(objFieldSet)
{
	var objParent = objFieldSet;
	do {
		objParent = objParent.parentNode;
	} while (objParent.tagName != 'FIELDSET');

	var min = objParent.getAttribute('min') != null ? objParent.getAttribute('min') : 0;
	var currentCount = 0;

	for (key in objParent.childNodes)
		if (objParent.childNodes[key].tagName == 'FIELDSET' && objParent.childNodes[key].style.display != 'none')
			currentCount++;

	if (currentCount-1 < min) {
		alert("You can't remove this field set because its group reached it's minimum");
		return;
	}

	if (window.confirm('Are you sure?'))
		objFieldSet.parentNode.removeChild(objFieldSet);
}
