
function TRequestResult () {
    this.Status = 0;
    this.Result = false;
    this.Message = '';
}

function TRequest( url, params, allowJS ) {
    var result = new TRequestResult();

//    var oRESPONSE = $(response_id);
//        oRESPONSE.update("Loading...");

    if (allowJS == undefined) {
        allowJS = false;
    }

    var oLoader = $("rio_loader");
    if (oLoader)
    oLoader.style.visibility = "visible"

    var oUpdater = new Ajax.Request(
        url, {
            method: "post",
            parameters: params,
            //encoding: "ISO-8859-1",
            asynchronous: false,
            evalScripts: allowJS,
            onSuccess: function(transport) {
                result.Status = transport.status;
                result.Result = true;
                result.Message = transport.responseText;
                result.Result = (transport.getHeader("Rio-Result")==0) ? false:true;
            },
            onFailure: function(transport) {
                result.Status = transport.status;
                result.Result = false;
                result.Message = transport.responseText;
                result.Result = (transport.getHeader("Rio-Result")==0) ? false:true;

                if (result.Status = 401) {
                    alert("Your session has expired.");
                    document.location.href = '/Admin';
                }
    	    }
        }
    );

    if (oLoader)
    oLoader.style.visibility = "hidden"


//    var oUpdater = new Ajax.Updater(
//        oRESPONSE, url, {
//            method: "post",
//            parameters: params,
//            //encoding: "ISO-8859-1",
//            asynchronous: false,
//            onSuccess: function(transport) {
//                result.Status = transport.status;
//                result.Result = true;
//                result.Message = transport.responseText;
//            },
//            onFailure: function(transport) {
//                result.Status = transport.status;
//                result.Result = false;
//                result.Message = transport.responseText;
//
//                if (transport.status = 401) {
//                    document.location.reload();
//                    alert("Session expired")
//                }
//    	    }
//        }
//    );
    return result;
}

function TRio() {

    this.ApplicationId = 0;
    this.PageId = 0;
    this.ModuleId = 0;
    this.ObjectId = 0;
    this.ErrorId = "";
    this.StatusId = "";

    /****************************************
    *** interface
    */
    this.Dump = Dump;
    this.Status = Status;
    this.Error = Error;
    this.ProcessRequest = ProcessRequest;

    this.ObjectSelected = ObjectSelected;

    this.ApplicationAdd = ApplicationAdd;
    this.ApplicationDelete = ApplicationDelete;
    this.ApplicationSelected = ApplicationSelected;

    this.PageAdd = PageAdd;
    this.PageDelete = PageDelete;
    this.PageSelected = PageSelected;

    this.ModuleAdd = ModuleAdd;
    this.ModuleDelete = ModuleDelete;
    this.ModuleSelected = ModuleSelected;
    this.ModuleInstall = ModuleInstall;
    this.ModuleUpdate = ModuleUpdate;

    this.FolderAdd = FolderAdd;
    this.FolderDelete = FolderDelete;

    this.PropertyAdd = PropertyAdd;
    this.PropertyDelete = PropertyDelete;

    /****************************************
    *** implementation
    */
    function ObjectSelected( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        } else {
            this.ObjectId = id;
        }
        new TRequest( "AdminAjax", "command=get_object&id=" +id )
    }


    function ApplicationAdd( id  ) {
        alert('Depricated rio.ApplicationAdd');return;
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        open_dialog( "Add application", "AdminAjax", "command=set_application", 400 );
    }


    function ApplicationSelected( aid ) {
        if (aid==-1) {
            return this.Error( "Invalid aid");
        } else {
            this.ApplicationId = aid;
        }
        this.ProcessRequest( new TRequest( "AdminAjax", "command=get_object&aid=" +aid + "&id=" +aid ), "object_properties" );
    }


    function ApplicationDelete( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        alert("ApplicationDelete")
    }


    function PageAdd( aid, id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        //open_dialog( "Add page", "AdminAjax", "command=set_page&parent_id=" +id, 400 );
        open_dialog( "Add page", "AdminAjax", "command=set_page&aid=" + aid + "&parent_id=" +id, 400 );
    }


    function PageDelete( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        if (!confirm("Delete page")) {
            return;
        }
        var Request = new TRequest( "AdminAjax", "command=del_page&page_id=" +id )
        if (Request.Result) {
            refresh.Navigation();
        }
    }


    function PageSelected( aid, id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        } else {
            this.PageId = id;
        }
        this.ProcessRequest( new TRequest( "AdminAjax", "command=get_object&aid=" +aid + "&id=" +id) , "object_properties" )
    }


    function ModuleAdd( pid ) {

        /*get module_id*/
        var oSELECT_MODULES = $("module_objects_id");
        var mid = oSELECT_MODULES.options[oSELECT_MODULES.selectedIndex].value;
        if (pid==-1) {
            return this.Error( "Invalid pid" );
        }
        if (mid==-1) {
            return this.Error( "Invalid mid" );
        }
        var Request = new TRequest( "AdminAjax", "command=add_module&pid=" +pid +"&mid=" +mid )
        if (Request.Result) {
            refresh.Navigation();
        }
    }


    function ModuleDelete( pid, mid ) {
        if (pid==-1) {
            return this.Error( "Invalid pid");
        }
        if (mid==-1) {
            return this.Error( "Invalid mid");
        }
        if (!confirm("Delete module")) {
            return;
        }
        new TRequest( "AdminAjax", "command=del_module&pid=" +pid+"&mid=" +mid )
    }


    function ModuleSelected( aid, id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        } else {
            this.ModuleId = id;
        }
        this.ProcessRequest( new TRequest( "AdminAjax", "command=get_object&aid=" +aid + "&id=" +id) , "object_properties" )
    }


    function ModuleInstall( name ) {
        if (name == "") {
            return this.Error( "Invalid module name");
        }
        this.ProcessRequest( new TRequest( "AdminAjax", "command=install_module&module_name=" +name ) );
    }


    function ModuleUpdate( aid, mid ) {
        var Result = new TRequest( "AdminAjax", "command=update_module&aid=" +aid + "&mid=" +mid);
    }


    function PropertyAdd( pid, id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        open_dialog( "Add property", "AdminAjax", "command=add_property&pid=" +pid +"&id=" +id );
    }


    function PropertyDelete( aid, id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        open_dialog( "Delete property", "AdminAjax", "command=get_property&aid=" +aid +"&id=" +id );
    }


    function FolderAdd() {

    }

    function FolderDelete() {

    }


    function ProcessRequest( Request, target ) {
        if (Request.Result) {
            if (target) {
                oTARGET = $(target);
                oTARGET.update( Request.Message )
            } else {
                this.Status( Request.Message )
            }
        } else {
            this.Error( Request.Message )
        }
    }

    function Status( txt ) {
       var oRESPONSE = $(this.StatusId);
       if (oRESPONSE) {
            oRESPONSE.update( txt );
       } else {
           alert( 'Status: ' + txt )
       }

    }

    function Error( txt ) {
       var oRESPONSE = $(this.ErrorId);
       if (oRESPONSE) {
            oRESPONSE.update( txt );
       } else {
           alert( 'Error: ' + txt )
       }
    }

    function Dump() {
        var oRESPONSE = $("rio_notify");
        s  = "<br>ObjectId:"+this.ObjectId;
        s += "<br>ApplicationId:"+this.ApplicationId;
        s += "<br>PageId:"+this.PageId;
        s += "<br>ModuleId:"+this.ModuleId;
        this.Status(s)
    }

}


function TRio2() {

    var self = this;
    this.ApplicationId = 0;
    this.PageId = 0;
    this.ModuleId = 0;
    this.ObjectId = 0;
    this.NavigationId = 0;
    this.ErrorId = "";
    this.StatusId = "";
    this.AllowIE  = true;
    this.Data = new Array();

    this.IS_IE = document.all;
    this.IS_MOZ = !this.IS_IE;
    this.GET_MOZ = '/files/Firefox-Setup-3.6.3.exe';

    this.Dialog = null;


    /****************************************
    *** interface
    */
    this.Dump = Dump;
    this.Status = Status;
    this.Error = Error;
    this.ProcessRequest = ProcessRequest;
    this.ProcessForm = ProcessForm;
    this.SendForm = SendForm;

    this.SelectObject = SelectObject;
    this.SelectObjectTabbed = SelectObjectTabbed;
    this.DeleteObject = DeleteObject;

    this.ApplicationAdd = ApplicationAdd;
    this.ApplicationDelete = ApplicationDelete;

    this.PageAdd = PageAdd;
    this.PageDelete = PageDelete;

    this.ModuleAdd = ModuleAdd;
    this.ModuleDelete = ModuleDelete;
    this.ModuleInstall = ModuleInstall;
    this.ModuleUpdate = ModuleUpdate;
    this.ModuleExecute = ModuleExecute;

    this.FolderAdd = FolderAdd;
    this.FolderDelete = FolderDelete;

    this.CollectionAdd = CollectionAdd;
    this.CollectionDelete = CollectionDelete;
    this.CollectionItemAdd = CollectionItemAdd;
    this.CollectionItemDelete = CollectionItemDelete;

    this.UserCollectionDelete = UserCollectionDelete;

    this.PropertyAdd = PropertyAdd;
    this.PropertyDelete = PropertyDelete;
    this.PropertyGet = PropertyGet;
    this.PropertySet = PropertySet;

    this.ImageDelete = ImageDelete;
    this.ImageRemove = ImageRemove;

    this.Toggle      = Toggle;
    this.DojoInstalled = DojoInstalled;

    /****************************************
    *** implementation
    */
    function DojoInstalled() {
        return (typeof dojo != "undefined") ? true : false;
    }


    function SelectObject( id ) {
        return this.SelectObjectTabbed( id );

        if (id==-1) {
            return this.Error( "Invalid id");
        }
        var Request = new TRequest( "AdminAjax", "command=get_object&id=" +id );
        this.ProcessRequest( Request, "admin_module_properties" );
    }


    function SelectObjectTabbed( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }

        //dojo
        if (this.DojoInstalled()) {
        }

        //$('admin_module_properties').setStyle({display:'none'});
        //$('admin_module_properties').setStyle({visibility:'hidden'});

        var Request = new TRequest( "AdminAjax", "command=get_object_tabbed&id=" +id );
        this.ProcessRequest( Request, "admin_module_properties" );

        //$('admin_module_properties').setStyle({display:'block'});
        //$('admin_module_properties').setStyle({visibility:'visible'});

        return true;
    }


    function DeleteObject( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        if (!confirm("Delete object")) {
            return;
        }
        var Request = new TRequest( "AdminAjax", "command=del_object&id=" +id )
        this.ProcessRequest( Request );
        if (Request.Result) {
            refresh.Navigation();
        }
    }


    function ApplicationAdd( id  ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        dialog.Open( "Add application", "AdminAjax", "command=set_application", 400 );
        //dialog.Open( "Delete property", "AdminAjax", "command=get_property&id=" +id );

    }


    function PageAdd( pid ) {
        if (pid==-1) {
            return this.Error( "Invalid pid");
        }
        dialog.Open( "Add page", "AdminAjax", "command=set_page&pid=" +pid, 400 );
        //open_dialog( "Add page", "AdminAjax", "command=set_page&pid=" +pid, 400 );
    }


    function CollectionSelect( id ) {
        return this.SelectObjectTabbed( id );

        if (id==-1) {
            return this.Error( "Invalid id");
        }
        var Request = new TRequest( "AdminAjax", "command=get_object&id=" +id );
        this.ProcessRequest( Request, "admin_module_properties" );
    }

    function CollectionAdd( pid ) {
        if (pid==-1) {
            return this.Error( "Invalid pid");
        }
        dialog.Open( "Add collection", "AdminAjax", "command=set_collection&pid=" +pid, 400 );
//        open_dialog( "Add collection", "AdminAjax", "command=set_collection&pid=" +pid, 400 );
    }

    function CollectionDelete( id, label ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        if (!confirm("Delete collection '"+label+"' ?")) {
            return;
        }
//        var Request = new TRequest( "AdminAjax", "command=del_collection&id=" +id )
        var Request = new TRequest( "AdminAjax", "command=del_object2&id=" +id )
        this.ProcessRequest( Request );
        if (Request.Result) {
            refresh.Collection();
        }
    }


    function UserCollectionDelete( id, label ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        if (!confirm("Delete user '" + label + "' ?")) {
            return;
        }
//        var Request = new TRequest( "AdminAjax", "command=del_collection&id=" +id )
        var Request = new TRequest( "AdminAjax", "command=del_object2&id=" +id )
        this.ProcessRequest( Request );
        if (Request.Result) {
            refresh.UserCollection();
        }
    }

    function CollectionItemAdd( pid ) {
        if (pid==-1) {
            return this.Error( "Invalid pid");
        }
        dialog.Open( "Add collectionItem", "AdminAjax", "command=set_collectionitem&pid=" +pid, 200 );
//        open_dialog( "Add collectionItem", "AdminAjax", "command=set_collectionitem&pid=" +pid, 200 );
    }

    function CollectionItemDelete( id, param, label ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        if (!confirm("Delete item '"+label+"' ?")) {
            return;
        }
        if (param == undefined) {
            param = false;
        }
        param = (param) ? 1:0;
        var Request = new TRequest( "AdminAjax", "command=del_object2&id=" +id + "&param=" +param )
        this.ProcessRequest( Request );
        if (Request.Result) {
            //refresh.UserCollection();
            refresh.Collection();
        }
    }

    function ModuleAdd( pid ) {

        if (this.DojoInstalled()) {
        } else {
            /*get module_id*/
            var oSELECT_MODULES = $("module_objects_id");
            var mid = oSELECT_MODULES.options[oSELECT_MODULES.selectedIndex].value;
        }

        if (pid==-1) {
            return this.Error( "Invalid pid");
        }
        if (mid==-1) {
            return this.Error( "Invalid mid");
        }
        var Request = new TRequest( "AdminAjax", "command=add_module&pid=" +pid +"&mid=" +mid )
        this.ProcessRequest( Request );
        if (Request.Result) {
            refresh.Navigation( this.NavigationId, 'options[m]=1&options[p]=0');
        }
    }


    function ApplicationDelete( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        if (!confirm("Delete application")) {
            return;
        }
        var Request = new TRequest( "AdminAjax", "command=del_application&id=" +id )
        this.ProcessRequest( Request );
        if (Request.Result) {
            refresh.Navigation();
        }
    }


    function PageDelete( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        if (!confirm("Delete page")) {
            return;
        }
        var Request = new TRequest( "AdminAjax", "command=del_page&id=" +id )
        this.ProcessRequest( Request );
        if (Request.Result) {
            refresh.Navigation();
        }
    }


    function ModuleDelete( pid, mid ) {
        if (pid==-1) {
            return this.Error( "Invalid pid");
        }
        if (mid==-1) {
            return this.Error( "Invalid mid");
        }
        if (!confirm("Delete module")) {
            return;
        }
        var Request = new TRequest( "AdminAjax", "command=del_module&pid=" +pid+"&mid=" +mid )
        this.ProcessRequest( Request );
        if (Request.Result) {
            refresh.Navigation( this.NavigationId, 'options[m]=1&options[p]=0');
        }
    }


    function ModuleExecute( oSender, form, request ) {
//        if (!confirm("Execute module")) {
//            return;
//        }

        var params = ''
        for (p in request.params) {
            params += p + '=' + request.params[p] + '&'
        }

        if (form) {
            var oFORM = $(form);
            var value = null;
            if (oFORM) {
                for(var i=0;i<oFORM.elements.length;i++) {
                    var element = oFORM.elements[i];
                    switch(element.type) {
                        case 'checkbox':
                            value = (element.checked) ? 'true':'false';
                            break;
                        default:
                            value = element.value;
                    }
                    params += element.name + '=' + value + '&'
                }
            }
        }

        var Request = new TRequest( request.url, "command=" + request.command + "&" + params, request.allowJS );
        this.ProcessRequest( Request, request.output );

        //remove submit button
        if (Request.Result) {
            if (this.DojoInstalled()) {
                //Element.remove(oSender.parentElement);
            } else {
                Element.remove(oSender);
            }
        }
    }




    function ImageDelete( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        if (!confirm("Delete image")) {
            return;
        }
        var Request = new TRequest( "AdminAjax", "command=del_object&id=" +id )
        this.ProcessRequest( Request );
        if (Request.Result) {
            this.ImageRemove(id);
            //refresh.Navigation();
        }
    }

    function ImageRemove( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        var oIMAGE = $("gallery_image_"+id);
        if (oIMAGE) {
            oIMAGE.remove();
        }
    }



    function ModuleInstall( params ) {
        if (this.DojoInstalled()) {
        } else {
            var oSOURCE = $(params.source)
            if (oSOURCE) {
                var Request = new TRequest( "AdminAjax", "command=install_module&module_name=" +oSOURCE.value );
                this.ProcessRequest( Request );
                if (Request.Result) {
                    //refresh.Navigation();
                }
            }
        }



        /*
        //function ModuleInstall( name ) {
        if (name == "") {
            return this.Error( "Invalid module name");
        }
        var Request = new TRequest( "AdminAjax", "command=install_module&module_name=" +name );
        this.ProcessRequest( Request );
        if (Request.Result) {
            //refresh.Navigation();
        }
        */
    }


    function ModuleUpdate( aid, mid ) {
        var Result = new TRequest( "AdminAjax", "command=update_module&aid=" +aid + "&mid=" +mid);
    }


    function PropertyAdd( pid ) {
        if (pid==-1) {
            return this.Error( "Invalid pid");
        }
        dialog.Open( "Add property", "AdminAjax", "command=add_property&pid=" +pid );
    }


    function PropertyDelete( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        dialog.Open( "Delete property", "AdminAjax", "command=get_property&id=" +id );
    }


    function PropertyGet( oSENDER, target ) {
        if (this.DojoInstalled()) {
        } else {
            var oSELECT_PROPS = oSENDER;
            var prop = oSELECT_PROPS.options[oSELECT_PROPS.selectedIndex];
            var value = prop.getAttribute("current");
            var oPROP_VALUE = $(target);
            if (oPROP_VALUE) {
                oPROP_VALUE.update(value)
            }
        }
    }


    function PropertySet( oSENDER, target ) {
        var oTARGET = $(target);
        if (oTARGET) {
            if (this.DojoInstalled()) {
            } else {
                oTARGET.update(oSENDER.value)
            }
        }
    }


    function FolderAdd( pid ) {
        if (pid==-1) {
            return this.Error( "Invalid pid");
        }
        dialog.Open( "Add folder", "AdminAjax", "command=set_folder&pid=" +pid, 400 );
        //open_dialog( "Add folder", "AdminAjax", "command=set_folder&pid=" +pid, 400 );
    }


    function FolderDelete( id ) {
        if (id==-1) {
            return this.Error( "Invalid id");
        }
        if (!confirm("Delete folder")) {
            return;
        }
        var Request = new TRequest( "AdminAjax", "command=del_folder&id=" +id )
        this.ProcessRequest( Request );
        if (Request.Result) {
            refresh.Navigation( this.NavigationId, 'options[m]=0&options[p]=0');
        }
    }


    function Toggle( oSender, id ) {
        var tree_open  = '<img class="admin tree" src="/phtml/img/tree_open.png" />';
        var tree_close = '<img class="admin tree" src="/phtml/img/tree_close.png" />';
        var c = $(id);
        if (c.getStyle('display') == 'block') {
            c.setStyle({display: 'none'});
//            oSender.firstChild.replace(tree_close);
            oSender.firstChild.src="/phtml/img/tree_close.png";
        } else{
            c.setStyle({display: 'block'});
//            oSender.firstChild.replace(tree_open);
            oSender.firstChild.src="/phtml/img/tree_open.png";
        }
    }


    function ProcessForm( id ) {
        var Request = this.SendForm( id );
        this.ProcessRequest( Request );
        if (Request.Result) {
            //alert('ok')
        }
    }

    function SendForm( id ) {
        var oRequest = new TRequestResult();

        if (!$(id)) {
            return "Invalid form id";
        }

        var oLoader = $(this.ProgressId);
        if (oLoader)
        oLoader.style.visibility = "visible"

        //Update fck formfields
        for ( i = 0; i < parent.frames.length; ++i ) {
            if ( parent.frames[i].FCK ) {
                parent.frames[i].FCK.UpdateLinkedField();
            }
        }

        $(id).request({
            asynchronous: false,
            onSuccess: function(transport) {
                oRequest.Status = transport.status;
                oRequest.Result = true;
                oRequest.Message = transport.responseText;
                oRequest.Result = (transport.getHeader("Rio-Result")==0) ? false:true;
            },
            onFailure: function(transport) {
                oRequest.Status = transport.status;
                oRequest.Result = false;
                oRequest.Message = transport.responseText;
                oRequest.Result = (transport.getHeader("Rio-Result")==0) ? false:true;

                if (oRequest.Status = 401) {
                    alert("Session expired")
                }
            }
        })

        if (oLoader)
        oLoader.style.visibility = "hidden";

        return oRequest;
    }


    function ProcessRequest( oRequest, target ) {
       var oRESPONSE = $(this.StatusId);
           oRESPONSE.update( '' );
       var oRESPONSE = $(this.ErrorId);
           oRESPONSE.update( '' );

        if (oRequest.Result) {
            if (target) {
                oTARGET = $(target);
                oTARGET.update( oRequest.Message );
                if (this.DojoInstalled()) {
                }
            } else {
                this.Status( oRequest.Message )
            }
        } else {
            this.Error( oRequest.Message )
        }
    }

    function Status( txt ) {
        return;
       var oRESPONSE = $(this.StatusId);
       if (oRESPONSE) {
            oRESPONSE.update( txt );
       } else {
           alert( 'Status: ' + txt )
       }

    }

    function Error( txt ) {
       var oRESPONSE = $(this.ErrorId);
       if (oRESPONSE) {
            oRESPONSE.update( txt );
       } else {
           alert( 'Error: ' + txt )
       }
    }

    function Dump() {
        var oRESPONSE = $("rio_notify");
        s  = "<br>ObjectId:"+this.ObjectId;
        s += "<br>ApplicationId:"+this.ApplicationId;
        s += "<br>PageId:"+this.PageId;
        s += "<br>ModuleId:"+this.ModuleId;
        this.Status(s)
    }

}


function TRefresh() {
    this.ErrorId = "";
    this.StatusId = "";

    /****************************************
    *** interface
    */
    this.Status = Status;
    this.Error = Error;
    this.ProcessRequest = ProcessRequest;

    this.Navigation = Navigation;
    this.Collection = Collection;
    this.UserCollection = UserCollection;
    this.Files      = Files;
    this.Images     = Images;
    this.AvailableModules = AvailableModules;

    /****************************************
    *** implementation
    */
    function Navigation( target, options ) {
        if (!target) {
            target = 'admin_module_navigation';
        }
        this.ProcessRequest( new TRequest( "RefreshAjax", "command=refresh_navigation" + "&options[]=0&" + options), target );
    }


    function Collection( target, options ) {
        if (!target) {
            target = 'admin_module_collection';
        }
        this.ProcessRequest( new TRequest( "RefreshAjax", "command=refresh_collection"), target );
    }


    function UserCollection( target, options ) {
        if (!target) {
            target = 'admin_module_usercollection';
        }
        this.ProcessRequest( new TRequest( "RefreshAjax", "command=refresh_usercollection"), target );
    }


    function AvailableModules( target ) {
        this.ProcessRequest( new TRequest( "RefreshAjax", "command=refresh_available_modules"), target );
    }


    function Files( id, target ) {
        this.ProcessRequest( new TRequest( "RefreshAjax", "command=refresh_files&id="+id), target );
    }

    function Images( id, target ) {
        this.ProcessRequest( new TRequest( "RefreshAjax", "command=refresh_images&id="+id), target );
    }

    function ProcessRequest( Request, target ) {
        if (Request.Result) {
            if (target) {
                oTARGET = $(target);
                oTARGET.update( Request.Message )
            } else {
                this.Status( Request.Message )
            }
        } else {
            this.Error( Request.Message )
        }
    }


    function Status( txt ) {
       var oRESPONSE = $(this.StatusId);
       if (oRESPONSE) {
            oRESPONSE.update( txt );
       } else {
           alert( 'Status: ' + txt )
       }

    }


    function Error( txt ) {
       var oRESPONSE = $(this.ErrorId);
       if (oRESPONSE) {
            oRESPONSE.update( txt );
       } else {
           alert( 'Error: ' + txt )
       }
    }

}


function TUser() {

    this.ErrorId = "";

    /****************************************
    *** interface
    */
    this.Match = Match;
    this.Error = Error;

    /****************************************
    *** implementation
    */
    function Match( oFORM ) {
        var result = false;
        if (oFORM) {
            var p1 = oFORM.p1;
            var p2 = oFORM.p2;

            //length
            if (p1.value.length <= 6) {
                this.Error("Password must have a length of at least 7 characters.")
            } else
            if (p2.value.length <= 6) {
                this.Error("Password must have a length of at least 7 characters.")
            } else
            //match
            if (p1.value==p2.value) {
                result = true;
            } else {
                this.Error("Passwords are not similar.")
            }
        }
        return result;
    }

    function Error( txt ) {
       var oRESPONSE = $(this.ErrorId);
       if (oRESPONSE) {
            oRESPONSE.update( txt );
       } else {
           alert( 'Error: ' + txt )
       }
    }

}

function TTracker() {

    this.Queue          = new Array();
    //this.Output         = document.open('/virtec.html','virtec', 'toolbar=no,menubar=no,status=no,titlebar=no,resizable=yes,scrollbars=yes,width=500,height=600', false);

    /****************************************
    *** interface
    */

    this.Install            = Install;
    this.Track              = Track;
    this.Flush              = Flush;
    this.Log                = Log;
    this.GoogleInstalled    = GoogleInstalled;

    /****************************************
    *** implementation
    */
    function GoogleInstalled() {
	return false;
        var result = false;
        if (_gaq) {
            result = true;
        }
        return result;
    }

    function Install() {
        document.observe('click', (function(event){
//            var target = event.findElement('a[rel^=tracker]') || event.findElement('img[rel^=tracker]');
            var target = event.findElement('a') || event.findElement('button') || event.findElement('input[type^=button]');
            if (target) {
                var item = {sender: target.tagName, label:target.innerHTML};
                this.Queue.push( item )
                this.Log( item )
            }
        }).bind(this));
    }

    function Track( oSENDER, params ) {
        if (this.GoogleInstalled()) {
            _gaq.push([params.ca, params.ac, params.ol, params.ov]);
        } else {
        }
    }

    function Log( trackitem ) {
        var text  = 'Sender: ' +trackitem.sender;
            text += '  Label: ' +trackitem.label;
        this.Output.log(1, 'trackitem', text);
    }

    function Flush() {

    }
}


function TDialog() {

    this.oDialog          = null;

    /****************************************
    *** interface
    */

    this.DojoxInstalled  = DojoxInstalled;
    this.Open            = Open;
    this.Close           = Close;

    /****************************************
    *** implementation
    */
    function DojoxInstalled() {
        return (typeof dojo != "undefined") ? true : false;
    }

    function Open( title, url, params, height, width ) {
        var height = (height) ? height : 350;
        var width = (width) ? width : 450;

        if (this.DojoxInstalled()) {
            var oRequest = new TRequest( url, params );
            if (oRequest.Result) {
        		var node = document.createElement("div");
        		dojo.body().appendChild(node);
        		this.oDialog = new dijit.Dialog({ title: title, width: width, height: height }, node);

                this.oDialog.containerNode.update(oRequest.Message);
		        //this.oDialog.containerNode.innerHTML = oRequest.Message;
                dojo.parser.parse(this.oDialog.containerNode);

//        		var btn = new dijit.form.Button({ label: "Close" });
//        		this.oDialog.containerNode.appendChild(btn.domNode);
//        		dojo.connect(btn, "onClick", function(){
//        			dialog.oDialog.destroy();
//
//        		});
        		this.oDialog.show();
            }
        } else {
            this.oDialog = new UI.Window({
                theme: "mac_os_x",
                shadow: true,
                draggable: true,
                draggable: false,
                resizable: false,
                //resizable: true,
                wired: false,
                width:  width,
                height: height}).center();

            this.oDialog.setAjaxContent( url, {
                method: "post",
                parameters: params,
                asynchronous: true
                } );

            this.oDialog.show(true);
            this.oDialog.title( title );
            this.oDialog.activate();
        }
    }


    function Close() {
        if (this.DojoxInstalled()) {
            this.oDialog.destroy();
        } else {
            this.oDialog.close();
        }
    }


}


var rio = new TRio();
rio.StatusId = "status_info";
rio.ErrorId  = "error_info";

var rio2 = new TRio2();
rio2.AllowIE  = true;
rio2.ProgressId = "rio_loader";
rio2.NavigationId = "admin_module_navigation";
rio2.StatusId = "status_info";
rio2.ErrorId  = "error_info";

var refresh = new TRefresh();
refresh.StatusId = "status_info";
refresh.ErrorId  = "error_info";

var user = new TUser();
user.ErrorId = "error_info";

var tracker = new TTracker();
var dialog = new TDialog();

