/***

ncBPNetcat CMS 1.0.0

(c) 2007 Netcat Pty. Ltd.  All rights Reserved.

***/

if (typeof(ncNetcatCOM) == 'undefined') {
    ncNetcatCOM = {};
}

if (typeof(ncNetcatCOM.ncNetcatCOM) == 'undefined') {
    ncNetcatCOM.ncNetcatCOM = {};
}

ncNetcatCOM.ncNetcatCOM.NAME = "ncNetcatCOM.ncNetcatCOM";
ncNetcatCOM.ncNetcatCOM.VERSION = "1.0.0";
ncNetcatCOM.ncNetcatCOM.__repr__ = function () {
    return "[" + this.NAME + " " + this.VERSION + "]";
};

ncNetcatCOM.ncNetcatCOM.toString = function () {
    return this.__repr__();
};


/* Windows object - creates and destroys windows and keeps track of window states */
ncNetcatCOM.ncWindows = function () {
        this.Container="";
        this.mostRecentWindow=null;
        this.callBackFunction = null;
    this.Add=function(obj)
    {
        this.push(obj);
    }
        
        this.get=function ( v_id ) {
                 var theObj = null;
                 for(i=0;i<this.length;i++) {
            if ( this[i] && v_id == this[i].id) {
                                theObj = this[i];
                        }
                 }
                 return theObj;
        }
        
        this.reFocus = function() {
                        /* If a window is opened from a IFRAME using parent.o_ncNetcat then the following  */
                        if ( this.length == 0 && parent.o_ncNetcat ) { try { parent.o_ncNetcat.windows.reFocus();} catch(e) {} }
                        var o_ncBPFramework = ncBPFramework.DOM;
                        for(i=0;i<this.length;i++) {
                                if ( this[i] && this[i].displayed ) {
                                        
                                        this[i].win.focus();
                                        if (this[i].win.o_ncNetcat) {
                                                this[i].win.o_ncNetcat.windows.reFocus();
                                        } else {        
                                                try { this[i].win.page.o_ncNetcat.windows.reFocus(); } catch (e) { }
                                        }
                                } else {
                                        // Garbage collection - remove any old windows
                                        this[i] = null;
                                        this.splice(i, 1)
                                }
                        }
        }
        
        this.closeAll = function () {
                        /* if ( this.length == 0 && parent.o_ncNetcat ) { try { parent.o_ncNetcat.windows.closeAll();} catch(e) {} }
                        var o_ncBPFramework = ncBPFramework.DOM;
                        for(i=0;i<this.length;i++) {
                                        if ( this[i] && this[i].displayed ) {
                                                        if (this[i].win.o_ncNetcat) {
                                                                        this[i].win.o_ncNetcat.windows.closeAll();
                                                        } else {        
                                                                        try { this[i].win.page.o_ncNetcat.windows.closeAll(); } catch (e) { }
                                                        }
                                        }
                                        this[i].win.close();
                        }
                        */
                        windowArray = top.o_ncWindows;
                        for(i=0;i<windowArray.length;i++) {
                                if( windowArray[i].displayed ) {
                                                windowArray[i].win.top.o_ncWindows.closeAll();
                                                windowArray[i].win.close();
                                }
                        }
        }
        };

/* Netcat window constructor - displays new windows */
ncNetcatCOM.ncWindows.ncWindow = function (v_id, v_type, v_title, v_url, v_width, v_height, v_callback) {
        this.id=v_id;
    this.type = v_type;
        this.title = v_title;
        this.url = v_url;
        this.callback = v_callback;
        this.chrome = null;
        this.win = null;
        this.width = v_width;
        this.height = v_height;
        this.blocker = null;
        this.displayed = false;
        this.result = null;
        this.callBackFunction = null;
        
        this.draw = function () {
                /* Draw a particular type of window */
                switch(this.type)
                        {
                        case 'dialog':
                                var theParameters = 'toolbar=no,border=thin,directories=no, status=no, statusbar=no,linemenubar=no,scrollbars=no,resizable=yes,status=no,width='+this.width+',height='+this.height;
                                var theFrameSet = '<HTML><HEAD><TITLE>..:: Netcat Publisher ::..</TITLE>';
                                theFrameSet += '<SCRIPT>myParent = window.opener; function init(){}';
                                theFrameSet += 'function unloadWindow() { ';
                                theFrameSet += 'try { myParent.o_ncNetcat.windows.get(id).saveWindowProperties(); } catch(e) {}';
                                theFrameSet += 'try { myParent.o_ncNetcat.windows.get(id).remove(); } catch(e) {}';
                                theFrameSet += 'try { window.opener.focus(); } catch(e) {}';
                                theFrameSet += 'try { parentWindow.o_ncNetcat.windows.get(id).remove(); } catch(e) {}';
                                theFrameSet += 'try { window.opener.top.page.o_ncNetcat.windows.get(id).remove(); } catch (e) {}';
                                theFrameSet += '} window.statusbar.visible=false;';
                                theFrameSet += 'function callBack( result ) {';
                                theFrameSet += 'try { parentWindow.o_ncNetcat.windows.get(id).callBackFunction( result ); } catch(e) { /*window.alert("callBackNotDefined- "+e);*/ }';
                                theFrameSet += '}';
                                theFrameSet += '</SCRIPT></HEAD>';
                                theFrameSet += '<FRAMESET rows="1,*" onunload="unloadWindow()" border="0" >';
                                theFrameSet += '<FRAME NAME="variables" src="'+blankURL+'"><FRAME SRC="'+this.url+Math.random()+'" NAME="page"></FRAMESET></HTML>';

                                /* Blocking DIV -- STYLES */
                                this.blocker = document.createElement("div");
                                this.blocker.style.backgroundColor="#CCCCCC";
                                this.blocker.style.position="absolute";
                                this.blocker.style.left="0px";
                                this.blocker.style.top="0px";
                                                                blockerHeight = document.height;
                                                                blockerWidth = document.width;
                                                                if ( window.innerWidth > blockerWidth ) blockerWidth = window.innerWidth;
                                                                if ( window.innerHeight > blockerHeight ) blockerHeight = window.innerHeight;
                                                                this.blocker.style.width=blockerWidth+"px";
                                                                this.blocker.style.height=blockerHeight+"px";
                                this.blocker.style.display="block";
                                this.blocker.style.opacity=".2";
                                this.blocker.style.filter="alpha(opacity=20)";
                                /* Blocking DIV -- behaviours */
                                this.blocker.id = 'ncBPBlockit';
                                this.blocker.onclick = function ()  { o_ncNetcat.windows.reFocus();o_ncNetcat.windows.reFocus();o_ncNetcat.windows.reFocus(); }
                                this.blocker.mousedown = function ()  { o_ncNetcat.windows.reFocus(); }
                                this.blocker.mouseover = function ()  { o_ncNetcat.windows.reFocus(); }
                                                                this.blocker.style.zIndex = 9999;
                                /* Blocking div -- display      */
                                                                try {
                                document.body.appendChild(this.blocker);
                                                                } catch (e) { try { document.appendChild(this.blocker); } catch(e) {} }
                                                                
                                                                this.win = window.open(blankURL, this.title, theParameters);
                                this.win.document.write( theFrameSet );
                                this.win.document.close();
                                                                this.win.focus();
                                this.win.parentWindow = window;
                                ncBPModalLastWindow = this.win;
                                                                
                                                                this.win.id = this.id;
                                this.win.top.parentDocument = document;
                                this.win.top.parentWin = self;
                                this.displayed = true;
                                this.win.top.dialogData = this.callback;
                                o_ncNetcat.windows.mostRecentWindow = this.win;
                          break    
                        case 'alert':
                          window.alert(this.title);
                          break
                        case 'question':
                          /*execute code block 2*/
                          break
                        default:
                          /*code to be executed if n is
                          different from case 1 and 2*/
                        }
        }
        
        
        
        this.saveWindowProperties = function () {
                var o_ncBPFramework = ncBPFramework.DOM;
                var winDimensions = o_ncBPFramework.getWindowDimensions(this.win);
                this.width = winDimensions.w;
                this.height = winDimensions.h;
        }
        
        this.remove = function () {
                if ( this.blocker) {
                        document.body.removeChild(this.blocker);
                        this.displayed = false;
                }
                this.blocker= null;
                window.focus();
        }
        
};

/* THE NETCAT CLASS */
function ncNetcatObject () {
        this.version = 6.1
        this.windows =  ncBPCreateCollection("ncNetcatCOM.ncWindows");
}

/* THE ALL IMPORTANT netcat object instance */
var o_ncNetcat = new ncNetcatObject();
top.ncBPModalLastWindow = null;

var ncSelectParentActionCommand = null; /* Used to determine which page called this command */

function ncSelectParentAction ( lib, name, id, updateField, updateContainer ) {
        /* Default action of SelectParent - This is normally superceeded by a function on the originating popup window */
//        window.alert('@@ - Not currently handled');
}

o_ncWindows = ncBPCreateCollection("ncNetcatCOM.ncWindows");
if ( !(top.o_ncWindows) ) {
        top.o_ncWindows = ncBPCreateCollection("ncNetcatCOM.ncWindows");
}


/*** UTILITIES ***/
/*This Function is used to create the collection object.
/Copy this function to create your own collection objects. */
function ncBPCreateCollection (ClassName)    
{
    var obj=new Array();
    eval("var t=new "+ClassName+"()");
    for(_item in t)
        {
            eval("obj."+_item+"=t."+_item);
        }
    return obj;
}

function ncBPClearDialog() {
        null;   
}

function ncBPcloseWindows () {
        o_ncNetcat.windows.closeAll();
}
