USEFRAMES = 1

String.prototype.trim = function () {
	var ret = this.replace( /^[\s\t\x0a\0d\n]+/, '' );
	ret = ret.replace( /[\s\t\x0a\0d\n]+$/, '' );
	return ret;
}

/** =================================================================
 * COption::COption()
 * Creates an COption object.
 */

function COption( name, value, defaultSelected, selected )
{
    var m_Name;
    var m_Value;
    var m_DefaultSelected = false;
    var m_Selected = false;

    this.setName( name );
    this.setValue( value );
    this.setDefaultSelected( defaultSelected );
    this.setSelected( selected );
}

    /**
     * COption::setName()
     * Set name of this option.
     */
    COption.prototype.setName = function ( name )
    {
        if( typeof( name ) == "string" )
        {
            this.m_Name = name;
        }
    }

    /**
     * COption::getName()
     * Returns with name of this option.
     */
    COption.prototype.getName = function ()
    {
        var ret = "";
        if( typeof( this.m_Name ) == "string" )
        {
            ret = this.m_Name;
        }
        return ret;
    }

    /**
     * COption::setValue()
     * Sets the value of option.
     */
    COption.prototype.setValue = function ( value )
    {
        if( typeof( value ) == "string" )
        {
            this.m_Value = value;
        }
    }

    /**
     * COption::getValue()
     * Returns with value of this option.
     */
    COption.prototype.getValue = function ()
    {
        return typeof( this.m_Value ) == "string" ? this.m_Value : "";
    }

    COption.prototype.setDefaultSelected = function ( selected )
    {
        this.m_DefaultSelected = selected;
    }

    COption.prototype.isDefaultSelected = function ()
    {
        return this.m_DefaultSelected;
    }

    COption.prototype.setSelected = function ( selected )
    {
        this.m_Selected = selected;
    }

    COption.prototype.isSelected = function ()
    {
        return this.m_Selected;
    }

    COption.prototype.compareTo = function ( obj )
    {
        if( typeof( obj ) == "undefined" ) return 1;
        if( ( typeof( obj ) == "object" ) && ( obj.toString() == this.toString() ) )
        {
            value1 = this.getValue();
            value2 = obj.getValue();
            if( value1 < value2 ) return -1;
            if( value2 < value1 ) return 1;
            return 0;
        }
        return 1;
    }

    /**
     * COption::toString()
     * Returns string representation of this COption object.
     */
    COption.prototype.toString = function ()
    {
        return "COption";
    }

function selectRefresh( selectRef, options )
{
    for( var i = 0; i < selectRef.options.length; i ++ )
    {
        selectRef.options[ i ] = null;
    }

    for( var i = 0; i < options.length; i ++ )
    {
        o = selectRef.m_DataSource[ i ];
        if( ( typeof( o ) == "object" ) && ( o.toString() == "COption" ) )
        {
            option = new Option( o.getValue(), o.getName(), o.isDefaultSelected(), o.isSelected() );
        }
    }
}

function selectAdd( selectRef, o )
{
    if( ( typeof( selectRef ) == "object" ) && ( typeof( o ) == "object" ) &&
        ( o.toString() == "COption" ) )
    {
        selectRef.options[ selectRef.options.length ] =
            new Option( o.getValue(), o.getName(), o.isDefaultSelected(), o.isSelected() );
    }
}

function selectAddOption( selectRef, o )
{
    if( ( typeof( selectRef ) == "object" ) && ( typeof( o ) == "object" ) )
    {
        selectRef.options[ selectRef.options.length ] = o;
    }
}

function selectSetSelected( selectRef, idx )
{
    if( typeof( selectRef ) == "object" )
    {
        if( idx < selectRef.options.length )
        {
            selectRef.options[ idx ].selected = true;
        }
    }
}

function optionComparator( o1, o2 )
{
    if( ( typeof( o1 ) == "object" ) && ( typeof( o2 ) == "undefined" ) ) return 1;
    if( ( typeof( o2 ) == "object" ) && ( typeof( o1 ) == "undefined" ) ) return -1;
    if( ( typeof( o1 ) == "undefined" ) && ( typeof( o2 ) == "undefined" ) ) return 0;
    if( ( typeof( o1 ) == "object" ) &&
        ( typeof( o2 ) == "object" ) )
    {
        if( o1.text < o2.text ) return -1;
        if( o1.text > o2.text ) return 1;
        return 0;
    }
}

function selectSort( selectRef )
{
    if( typeof( selectRef ) == "object" )
    {
        options = new Array();
        for( var i = 0; i < selectRef.options.length; i ++ )
        {
            options[ options.length ] = selectRef.options[ i ];
        }
        options.sort( optionComparator );
        for( var i = 0; i < options.length; i ++ )
        {
            selectRef.options[ i ] =
                new Option(
                    options[ i ].text,
                    options[ i ].value,
                    options[ i ].defaultSelected,
                    options[ i ].selected
                );
        }
    }
}

function selectAddAndSort( selectRef, o )
{
    selectAdd( selectRef, o );
    selectSort( selectRef );
}

function selectRemoveAll( selectRef )
{
    if( typeof( selectRef ) == "object" )
    {
        for( var i = 0; i < selectRef.options.length; i++ )
        {
            selectRef.options[ i ] = null;
        }
        selectRef.options.length = 0;
    }
}

function selectRemoveFrom( selectRef, idx )
{
    if( typeof( selectRef ) == "object" )
    {
        if( idx < selectRef.options.length )
        {
            selectRef.options[ idx ] = null;
        }
    }
}

function selectRemoveSelected( selectRef ) {
	if( typeof( selectRef ) == "object" ) {
		var k = 0;
		for( var i = 0; i < selectRef.options.length; i++ )
		{
			if( selectRef.options[ i ].selected ) {
				selectRef.options[ i ] = null;
				k++;
			}
        }
        // selectRef.options.length -= k;
	}
}

function selectMoveOption( selectFrom, selectTo, idx )
{
    if( ( typeof( selectFrom ) == "object" ) &&
        ( typeof( selectTo ) == "object" ) )
    {
        if( idx < selectFrom.options.length )
        {
            o = new Option(
                selectFrom.options[ idx ].text,
                selectFrom.options[ idx ].value,
                selectFrom.options[ idx ].defaultSelected,
                selectFrom.options[ idx ].selected
            );
            selectTo.options[ selectTo.options.length ] = o;
			selectRemoveFrom( selectFrom, idx );
        }
    }
}

function selectCopyOption( selectFrom, selectTo, idx )
{
    if( ( typeof( selectFrom ) == "object" ) &&
        ( typeof( selectTo ) == "object" ) )
    {
        if( idx < selectFrom.options.length )
        {
            o = new Option(
                selectFrom.options[ idx ].text,
                selectFrom.options[ idx ].value,
                selectFrom.options[ idx ].defaultSelected,
                selectFrom.options[ idx ].selected
            );
            selectTo.options[ selectTo.options.length ] = o;
        }
    }
}

function selectMoveOptionAndSort( selectFrom, selectTo, idx )
{
    selectMoveOption( selectFrom, selectTo, idx );
    selectSort( selectTo );
}

function selectCopyOptionAndSort( selectFrom, selectTo, idx )
{
    selectCopyOption( selectFrom, selectTo, idx );
    selectSort( selectTo );
}

function selectMoveSelectedOptions( selectFrom, selectTo )
{
    var idx = 0;
    if( ( typeof( selectFrom ) == "object" ) &&
        ( typeof( selectTo ) == "object" ) )
    {
        len = selectFrom.options.length;
        for( var i = 0; i < len; i ++ )
        {
            o = selectFrom.options[ idx ];
            if( o.selected )
            {
                selectMoveOption( selectFrom, selectTo, idx );
            }

            else
            {
                idx ++;
            }
        }
    }
}

function selectCopySelectedOptions( selectFrom, selectTo )
{
    var idx = 0;
    if( ( typeof( selectFrom ) == "object" ) &&
        ( typeof( selectTo ) == "object" ) )
    {
        len = selectFrom.options.length;
        for( var i = 0; i < len; i ++ )
        {
            o = selectFrom.options[ idx ];
            if( o.selected )
            {
                selectCopyOption( selectFrom, selectTo, idx );
            }

            else
            {
                idx ++;
            }
        }
    }
}

function selectMoveFirstSelectedOption( selectFrom, selectTo )
{
    var idx = 0;
    if( ( typeof( selectFrom ) == "object" ) &&
        ( typeof( selectTo ) == "object" ) )
    {
        len = selectFrom.options.length;
        for( var i = 0; i < len; i ++ )
        {
            o = selectFrom.options[ idx ];
            if( o.selected )
            {
                selectMoveOption( selectFrom, selectTo, idx );
                return;
            }

            else
            {
                idx ++;
            }
        }
    }
}

function selectCopyFirstSelectedOption( selectFrom, selectTo )
{
    var idx = 0;
    if( ( typeof( selectFrom ) == "object" ) &&
        ( typeof( selectTo ) == "object" ) )
    {
        len = selectFrom.options.length;
        for( var i = 0; i < len; i ++ )
        {
            o = selectFrom.options[ idx ];
            if( o.selected )
            {
                selectCopyOption( selectFrom, selectTo, idx );
                return;
            }

            else
            {
                idx ++;
            }
        }
    }
}

function selectMoveAllOptions( selectFrom, selectTo )
{
    var idx = 0;
    if( ( typeof( selectFrom ) == "object" ) &&
        ( typeof( selectTo ) == "object" ) )
    {
        len = selectFrom.options.length;
        for( var i = 0; i < len; i ++ )
        {
            selectMoveOption( selectFrom, selectTo, 0 );
        }
    }
}

function selectCopyAllOptions( selectFrom, selectTo )
{
    var idx = 0;
    if( ( typeof( selectFrom ) == "object" ) &&
        ( typeof( selectTo ) == "object" ) )
    {
        len = selectFrom.options.length;
        for( var i = 0; i < len; i ++ )
        {
            selectCopyOption( selectFrom, selectTo, 0 );
        }
    }
}


function selectMoveSelectedOptionsAndSort( selectFrom, selectTo )
{
    selectMoveSelectedOptions( selectFrom, selectTo );
    selectSort( selectTo );
}

function selectCopySelectedOptionsAndSort( selectFrom, selectTo )
{
    selectCopySelectedOptions( selectFrom, selectTo );
    selectSort( selectTo );
}

function selectMoveAllOptionsAndSort( selectFrom, selectTo )
{
    selectMoveAllOptions( selectFrom, selectTo );
    selectSort( selectTo );
}

function selectCopyAllOptionsAndSort( selectFrom, selectTo )
{
    selectCopyAllOptions( selectFrom, selectTo );
    selectSort( selectTo );
}

function selectSelectAll( selectRef )
{
    if( typeof( selectRef ) == "object" )
    {
        if( typeof( selectRef.options ) != "undefined" )
        {
            for( var i = 0; i < selectRef.options.length; i++ )
            {
                selectRef.options[ i ].selected = true;
            }
        }
    }
}

function selectDeselectAll( selectRef )
{
    if( typeof( selectRef ) == "object" )
    {
        if( typeof( selectRef.options ) != "undefined" )
        {
            for( var i = 0; i < selectRef.options.length; i++ )
            {
                selectRef.options[ i ].selected = false;
            }
        }
    }
}


function selectInversSelection( selectRef )
{
    if( typeof( selectRef ) == "object" )
    {
        if( typeof( selectRef.options ) != "undefined" )
        {
            for( var i = 0; i < selectRef.options.length; i++ )
            {
                selectRef.options[ i ].selected = ! selectRef.options[ i ].selected;
            }
        }
    }
}

function selectMoveFirst( selectRef )
{
	while(0<selectMoveUp(selectRef));
}

function selectMoveUp( selectRef )
{
	if( typeof( selectRef ) == "object" )
    {
		src_idx = selectRef.selectedIndex;
		if( src_idx <= 0 ) return;
		target_idx = src_idx - 1;
		target_o = new Option( 		
			selectRef.options[ target_idx ].text,
            selectRef.options[ target_idx ].value,
            selectRef.options[ target_idx ].defaultSelected,
            selectRef.options[ target_idx ].selected
        );
		src_o = new Option( 		
			selectRef.options[ src_idx ].text,
            selectRef.options[ src_idx ].value,
            selectRef.options[ src_idx ].defaultSelected,
            selectRef.options[ src_idx ].selected
        );
		selectRef.options[ src_idx ] = target_o;
		selectRef.options[ target_idx ] = src_o;
	}
	return target_idx;
}

function selectMoveDown( selectRef )
{
	if( typeof( selectRef ) == "object" )
    {
		src_idx = selectRef.selectedIndex;
		if( src_idx >= selectRef.options.length - 1 ) return;
		target_idx = src_idx + 1;
		target_o = new Option( 		
			selectRef.options[ target_idx ].text,
            selectRef.options[ target_idx ].value,
            selectRef.options[ target_idx ].defaultSelected,
            selectRef.options[ target_idx ].selected
        );
		src_o = new Option( 		
			selectRef.options[ src_idx ].text,
            selectRef.options[ src_idx ].value,
            selectRef.options[ src_idx ].defaultSelected,
            selectRef.options[ src_idx ].selected
        );
		selectRef.options[ src_idx ] = target_o;
		selectRef.options[ target_idx ] = src_o;
	}
}

function selectJoinValues( selectRef, separator )
{
    if( typeof( separator ) == "undefined" )
    {
        separator=",";
    }
	value_list = new Array();
	if( typeof( selectRef ) == "object" )
	{
		for( var i = 0; i < selectRef.options.length; i++ )
		{
			value_list[ i ] = selectRef.options[ i ].value;
		}
	}
	return value_list.join( separator );
}

/** =================================================================
 * CAttribute::CAttribute()
 */
function CAttribute( name, value )
{
    var m_Name;
    var m_Value;

    this.setName( name );
    this.setValue( value );
}

    /**
     * CAttribute::setName()
     */
    CAttribute.prototype.setName = function ( name )
    {
        if( typeof( name ) == "string" )
        {
            this.m_Name = name;
        }

        else
        {
            this.m_Name = "";
        }
    }

    /**
     * CAttribute::getName()
     */
    CAttribute.prototype.getName = function ()
    {
        return typeof( this.m_Name ) == "string" ? this.m_Name : "";
    }

    /**
     * CAttribute::setValue()
     */
    CAttribute.prototype.setValue = function ( value )
    {
        if( typeof( value ) == "string" )
        {
            this.m_Value = value;
        }

        else
        {
            this.m_Value = "";
        }
    }

    /**
     * CAttribute::getValue()
     */
    CAttribute.prototype.getValue = function ()
    {
        return typeof( this.m_Value ) == "string" ? this.m_Value : "";
    }

    /**
     * CAttribute::toString()
     */
    CAttribute.prototype.toString = function ()
    {
        return "CAttribute";
    }

/** =================================================================
 * CHasAttributes::CHasAttributes()
 */
function CHasAttributes()
{
    var m_Attributes;

    this.m_Attributes = new Array();
}

    /**
     * CHasAttributes::add()
     */
    CHasAttributes.prototype.add = function ( attr )
    {
        if( typeof( this.m_Attributes ) == "undefined" )
        {
            this.m_Attributes = new Array();
        }
        if( ( typeof( attr ) == "object" ) && ( attr.toString() == "CAttribute" ) )
        {
            this.m_Attributes[ attr.getName() ] = attr;
        }
    }

    /**
     * CHasAttributes::get()
     */
    CHasAttributes.prototype.get = function ( name )
    {
        ret = new CAttribute();
        o = this.m_Attributes[ name ];
        if( ( typeof( o ) == "object" ) && ( o.toString() == "CAttribute" ) )
        {
            ret = o;
        }
        return ret;
    }

    /**
     * CHasAttributes::keys()
     */
    CHasAttributes.prototype.getKeys = function ()
    {
        var i = 0;
        ret = new Array();
        for ( var k in this.m_Attributes )
        {
            ret[ ret.length ] = k;
        }
        return ret;
    }

    /**
     * CHasAttributes::toString()
     */
    CHasAttributes.prototype.toString = function()
    {
        return "CHasAttributes";
    }

function _Dialog( url, param, action ) {
    if( param == null ) {
        param = this;
    }
    if (document.all) {	// here we hope that Mozilla will never support document.all
		var value =
			showModalDialog( url, param,
					"resizable: no; help: no; status: no; scroll: no" );
		if ( action ) {
			action(value);
		}
	}
    else {
        window.open();
    }
}

/* -----------------------------------------------------------------
 * css utilities
 * ----------------------------------------------------------------- */
function _removeClass( el, className ) {
        if( ! ( el || el.className ) )
                return;
        var classes = el.className.split( " " );
        var newClasses = new Array();
        for( var i = classes.length; 0 < i; ) {
                if( classes[ --i ] != className ) {
                        newClasses[ newClasses.length ] = classes[ i ];
                }
        }
        el.className = newClasses.join( " " );
}

function _addClass( el, className ) {
        _removeClass( el, className );
        el.className += " " + className;
}

function _hasClass( el, className ) {
        if ( ! ( el && el.className ) ) {
                return false;
        }
        var classes = el.className.split(" ");
        for (var i = classes.length; 0 < i; ) {
                if ( classes[--i] == className ) {
                        return true;
                }
        }
        return false;
}

function removeCssClass( el, className ) {
        if( ! ( el || el.className ) )
                return;
        var classes = el.className.split( " " );
        var newClasses = new Array();
        for( var i = classes.length; 0 < i; ) {
                if( classes[ --i ] != className ) {
                        newClasses[ newClasses.length ] = classes[ i ];
                }
        }
        el.className = newClasses.join( " " );
}

function addCssClass( el, className ) {
        removeCssClass( el, className );
        el.className += " " + className;
}

function hasCssClass( el, className ) {
        if ( ! ( el && el.className ) ) {
                return false;
        }
        var classes = el.className.split(" ");
        for (var i = classes.length; 0 < i; ) {
                if ( classes[--i] == className ) {
                        return true;
                }
        }
        return false;
}

// ------------------------------------------------------------------------

var __g_EditorContent;
var __g_CurrentGalleryName;
var __g_CurrentGalleryId;
var __g_CurrentImageId;

function OnShowEditorPopup( text ) {
    __g_EditorContent = text;
    _Dialog( "index.php?<?=SID?>&id=editor_popup&menu_grp=admin", this );
}

function setCurrentImageId( _id ) {
    if( __g_CurrentImageId ) {
        __g_CurrentImageId.value = _id;
    }
}

function getCurrentImageId() {
    if( __g_CurrentImageId ) {
        return __g_CurrentImageId.value;
    }
    return 0;
}

function setCurrentGalleryName( _name ) {
    if( __g_CurrentGalleryName ) {
        __g_CurrentGalleryName.value = _name;
    }
}

function getCurrentGalleryName() {
    if( __g_CurrentGalleryName ) {
        return __g_CurrentGalleryName.value;
    }
    return "";
}

function setCurrentGalleryId( _id ) {
    if( __g_CurrentGalleryId ) {
        __g_CurrentGalleryId.value = _id;
    }
}

function getCurrentGalleryId() {
    if( __g_CurrentGalleryId ) {
        return __g_CurrentGalleryId.value;
    }
    return "";
}

function OnShowImagePopup( f_gallery_name, f_gallery_id ) {
    __g_CurrentGalleryName = f_gallery_name;
    __g_CurrentGalleryId = f_gallery_id;
    _Dialog( "index.php?<?=SID?>&id=image_popup&menu_grp=admin", this );
    __g_CurrentGalleryName = null;
    __g_CurrentGalleryId = null;
}

function OnShowSelectImagePopup( f_map_gallery_name, f_map_gallery_id, f_map_id ) {
    __g_CurrentGalleryName = f_map_gallery_name;
    __g_CurrentGalleryId = f_map_gallery_id;
    __g_CurrentImageId = f_map_id;
    _Dialog( "index.php?<?=SID?>&id=image_popup&menu_grp=admin", this );
    __g_CurrentGalleryName = null;
    __g_CurrentGalleryId = null;
    __g_CurrentImageId = null;
}

function setEditorContent( c ) {
    if( __g_EditorContent )
      __g_EditorContent.value = c;
}

function getEditorContent() {
  return __g_EditorContent ? __g_EditorContent.value : "";
}

