function Module(moduleid){

    this.moduleid     = moduleid || 0;
    this.permission   = 0;
    this.modulename   = '';
    this.list         = null; // object collection
    this.columns      = null; // columns coll to display
    this.from         = 0;
    this.search       = '';
    this.count        = -1; // total # of rows
    this.order        = 'asc';
    this.sort         = '';
    this.deleted      = 0; // 0 - not deleted, 1 - deleted, -1 - all
    this.maxrows      = 0; // max # of rows per page
    this.link         = ''; // base query link to call
    this.imagepath    = '';
    this.templatepath = '';
    this.librarypath  = '';
    this.editedobject = null;
    this.updated      = false;
    
    // Abstract methods

    this.displayList = function(){
        alert("Method must be implemented in inherited class");
    }

    this.getList = function(){
        alert("Method must be implemented in inherited class");
    }

    this.setOrder = function(column) {    
        if (this.sort == column || this.sort == "") {
            this.order = this.order == 'asc' ? 'desc' : 'asc';
        }
        else{
            this.order = 'asc';
        }

        this.sort = column;
    }

    this.setTemplatePath = function(path){
        this.templatepath = path;
        this.imagepath = path + '/images';
    }

    this.setFrom = function(from){
        this.from = from;
    }

    this.find = function(search){
        this.from = 0;
        this.search = search;
    }

    this.setSearch = function(search){
        this.search = search;
    }

    this.setModuleActions = function() {
        alert("Method must be implemented in inherited class");        
    }

    this.showSearchFields = function() {
        alert("Method must be implemented in inherited class");        
    }
    
    this.setPageInfo = function(navAction) {

        var html = "";

        var to = this.from + this.maxrows; // pageinfo 0 - from, 1 - total, 2 - perpage
        if (to >= this.count) to = this.count;

        var fr = this.from;

        if (this.from > this.count) fr = this.count;
        var fl = this.count > 0 ? fr + 1 : 0; 
        if (this.count < fl) fl = this.count;

        if (fr > 0) {
            html+= '<a href="#" onclick="'+navAction+'(0)">First</a> ';
        }
        if (fr > this.maxrows) {
            var fr2 = fr - this.maxrows;
            if (fr2 < 0) fr2 = 0;
            html+= '<a href="#" onclick="'+navAction+'('+ fr2 +')">Prev</a> ';
        }

        if (this.maxrows > 1) {
            html+= '<b>'+ fl +" - "+ to +'</b> of <b>'+ this.count +'</b></span> ';
        }
        else{
            html+= '<b>'+ fl +'</b> of <b>'+ this.count +'</b></span> ';
        }

        if (this.count > fr + 2 * this.maxrows) {
            html+= '<a href="#" onclick="'+navAction+'('+ (fr + this.maxrows) +')">Next</a> ';
        }

        if (this.count > fr + this.maxrows) {
            html+= '<a href="#" onclick="'+navAction+'('+ (Math.floor((this.count - 1) / this.maxrows) * this.maxrows) +')">Last</a>';
        }

        document.getElementById("PageInfo").innerHTML = html;
        //return html;
    }

    this.setView = function(){
        h = '<span class="menutext">'+ this.modulename + ' </span>';

        document.getElementById("menuselected").innerHTML = h;
    }

    this.switchTab = function(tab) {       

        var o = this.editedobject;

        for (i in o) {
            try{
                o[i] = document.getElementById(i).value;
            }
            catch(e){}        
        }

        this.selectedTab = tab;
        this.displayItem();
    }

    this.switchTab2 = function(tab) {       

        var o = this.editedobject2;

        for (i in o) {
            try{
                o[i] = document.getElementById(i+'2').value;
            }
            catch(e){}        
        }

        this.selectedTab2 = tab;
        this.displayItem2();
    }
}

Module.closeEditor = function(){    
    var div =  arguments.length > 0 ? arguments[0] : "editdiv";            
    var ed = document.getElementById(div);
    ed.style.visibility = "hidden";        
}

Module.closeEditor2 = function(){
    var ed = document.getElementById("editdiv2");
    ed.style.visibility = "hidden";        
}

Module.closeEditor3 = function(){
    var ed = document.getElementById("editdiv3");
    ed.style.visibility = "hidden";        
}

Module.closeUpload = function(){
    var ed = document.getElementById("uploaddiv");
    ed.style.visibility = "hidden";        
}

Module.showHideSearch = function(show){
    try{
        document.getElementById("searchdiv").style.visibility = show ? 'visible' : 'hidden';
    }
    catch(e){}
}

Module.viewImage = function (url){

    var width = 400, height = 300;
    var left  = (screen.width/2) - width/2;
    var top   = (screen.height/2) - height/2;
    var displ = 'resizable=yes,';
    displ+= 'width='+width+',height='+height+',';
    displ+= 'left='+left+',top='+top+',';
    displ+= 'screenX='+left+',screenY='+top;

    viewWin = window.open(url,'View',displ);
    viewWin.focus();
}

Module.viewMogulus = function (url){

    var width = 400, height = 300;
    var left  = (screen.width/2) - width/2;
    var top   = (screen.height/2) - height/2;
    var displ = 'resizable=yes,';
    displ+= 'width='+width+',height='+height+',';
    displ+= 'left='+left+',top='+top+',';
    displ+= 'screenX='+left+',screenY='+top;

    viewWin = window.open("",'View',displ);
    viewWin.focus();

    //viewWin.document.write('<script src="'+url+'" type="text/javascript"></script>');
    //viewWin.document.close();
    //viewWin.document.body.innerHTML = '<script src="'+url+'" type="text/javascript"></script>';
            
    var script = viewWin.document.createElement('script');
    script.id = 'mogulus';
    script.type = 'text/javascript';
    script.src = url;
    viewWin.body.appendChild(script);

}

Module.upload = function(id, module, field, callback){

    var ed = document.getElementById("uploaddiv");

    ed.innerHTML = '<iframe id="uploadfr" name="uploadfr" src="upload.php" scrolling="no" unselectable="on" style="width:400px;height:100px;" frameborder="0"></iframe>';
    ed.style.visibility = "visible";
    setDivSize(ed, 400, 100, 'cc');  

    var url = 'upload.php';
    url+= '?module=' + module;
    url+= '&field=' + field;
    url+= '&id=' + id;
    url+= '&callback=' + callback;

    if (document.all) {
        var fr = frames["uploadfr"];
        fr.location.href = url;
    }
    else{
        var fr = document.getElementById("uploadfr");        
        fr.src = url;
    }   
}

Module.download = function(dir, file) {

    var width = 400, height = 300;
    var left  = (screen.width/2) - width/2;
    var top   = (screen.height/2) - height/2;
    var displ = 'resizable=yes,';
    displ+= 'width='+width+',height='+height+',';
    displ+= 'left='+left+',top='+top+',';
    displ+= 'screenX='+left+',screenY='+top;

    viewWin = window.open("download.php?dir="+dir+"&file="+file,'View',displ);
    viewWin.focus();

}

Module.print = function(html){

    var printw = window.open('','debug','scrollbars=yes,resizable=yes,width=600,height=400');
    
    printw.document.write(html);
    printw.document.close();
    printw.focus();
    printw.print();
}

