// Copyright (c)2010 Graphic Vision Neuroinformatics, Inc. - All rights reserved
// Licensed to Western Business Systems, Inc.

Masada_250 = {}
Masada_250.InsertScript = function(Id, Text)
{
    if (document.getElementById(Id)) return;
    var script = document.createElement("script");
    script.setAttribute('id', Id);
    script.setAttribute('type', 'text/javascript');

    if (script.canHaveChildren)
    {
        script.appendChild(d.createTextNode(Text));
    }
    else
    {
        script.text = Text;
    }
    document.body.appendChild(script);
}


Masada_250.Panel = function(path, name, callback)
{
    var This = this;
    var Container = this.Element = $$('div');
    var Path = path;
    var Name = name;
    this.Callback = callback;
        
    this.BindTo = function(parent) { parent.appendChild(Container); return This; }

    var Flags = {}
    Flags.ReadOnly = 1;
    Flags.WriteOnly = 2;
    Flags.ReadWrite = 4;
    Flags.UseInner = 8;
    Flags.Label = 16;
    
    var Control = function() {}
    Control.prototype.Flags = Flags;
    Control.prototype.Value = function(value)
    {
        if (arguments.length > 0)
        {
            switch(this.Element.type)
            {
                case 'button':
                {
                }  
                case 'checkbox':
                case 'file':
                case 'hidden':
                case 'image':
                case 'password':
                case 'radio':
                case 'reset':
                break;
                case 'select-one':
                case 'select-multiple':
                {
                    var Ctrl = this.Element;
                    //if (value.length > 0) SetDdlOption(this.Element,value);
                    if (!(value == null || typeof(value) == 'undefined'))
                    { 
                        for(var i=0;i<Ctrl.options.length;i++)
                        {
                            if(Ctrl.options[i].value.trim() == value)
                            {
                                Ctrl.selectedIndex = i;
                                if (this.OnUpdate != 'undefined' && this.OnUpdate) this.OnUpdate();
                                return;
                            }
                        }
                        var option = document.createElement("OPTION");
                        option.text = value;
                        option.value = value;
                        if (value.length > 0)
                        {
                            //option.text += '(not in list)';
                            Ctrl.options.add(option);
                            Ctrl.selectedIndex = i;
                        }
                        else
                        {
                            Ctrl.options.add(option,0);
                            Ctrl.selectedIndex = 0;
                        }
                    }
                    break;
                }
                case 'submit':
                    break;
                case 'text':
                case 'textarea':
                    this.Element.value = value;
                    break;
                default:
                {   // span
                alert("default:" + this.Element.type);
                    this.Element.innerHTML = value;
                }
            }
            if (this.OnUpdate != 'undefined' && this.OnUpdate) this.OnUpdate();
        }
        else
        {
            switch(this.Element.type)
            {
                case 'button':
                {
                }  
                case 'checkbox':
                case 'file':
                case 'hidden':
                case 'image':
                case 'password':
                case 'radio':
                case 'reset':
                break;
                case 'select-one':
                case 'select-multiple':
                    var Ctrl = this.Element;
                    if (Ctrl.selectedIndex < 0) return "";
                    if (this.IoFlags == this.Flags.UseInner)
                        return Ctrl[Ctrl.selectedIndex].innerHTML;
                    else
                        return Ctrl[Ctrl.selectedIndex].value;
                    break;
                case 'submit':
                    break;
                case 'text':
                case 'textarea':
                    return this.Element.value;
                default:
                {   // span
                    return this.Element.innerHTML;
                }
            }
        }
    }
    
    this.InsertElement = function(element)
    {
        var Id = element.id;
        if (Id && Id != 'undefined')
        {
            var Ctrl = new Control();
            Ctrl.IsDirty = false;
            Ctrl.Element = element;
            Ctrl.IoFlag = Ctrl.Flags.ReadWrite;
            This[Id] = Ctrl;
            element.removeAttribute(element.id);                   
        }
    }
    
    this.Clone = function()
    {
        return new Masada_250.Panel(Path, Name, null);
    }
    
    this.Clear = function()
    {
        var ioType = 3;
        for (var property in This)
        {
            var Obj = This[property];
            if (Obj.IoFlag && Obj.IoFlag != 0)
            {
                var Len = property.length;
                var Ext = Obj.Ext;
                if (Ext == 'Lbl' || Ext == 'Dte') continue;
                Obj.Value('');
            }
        }
        return This;
    }
    
    this.Load = function(rec)
    {
        var ioType = 3;
        for (var property in This)
        {
            var Obj = This[property];
            if (Obj.IoFlag == ioType)
            {
                var Ext = Obj.Ext;
                if (Ext == 'Lbl' || Ext == 'Dte') continue;
                Obj.Value(rec.GetValue(Obj.Name));
            }
        }
        return This;
    }
    
    this.List = function()
    {
        var List = [];
        for (var property in This)
        {
            var Obj = This[property];
            if(Obj && typeof(Obj.SourceName)== 'string')
            {
            //alert(Obj.SourceName);
             List.push(Obj);
            }
        }
        return List;
    }
    
    this.Store = function(rec)
    {
        for (var property in This)
        {
            var Obj = This[property];
            if (Obj.IoFlag == 3 || Obj.IoFlag == 2)
            {
                var Ext = Obj.Ext;
                if (Ext == 'Lbl' || Ext == 'Dte') continue;
                rec.SetValue(Obj.Name, Obj.Value());
            }
        }
        return This;
    }
    
    this.Disabled = function(state)
    {
        for (var property in This)
        {
            if (property.substr(0,8) == 'function') continue;
            var obj = This[property];
            var Id = obj.id;
            if (Id && Id.length > 0)
            {
                obj.disabled = state;
            }
        }
    }

    
    var Build = function(html)
    {
        Container.innerHTML = html;

        var descend = function(node)
        {
            //if (node.nodeType == 3) return;    // node.TEXT_NODE
            var Elements = node.childNodes;
            for(var i=0; i<Elements.length; i++)
            {
                descend(Elements[i]);
                This.InsertElement(Elements[i]);
            }
        }
        descend(Container);
        
        var Scripts = Container.getElementsByTagName('script');
        for (var i=0;i<Scripts.length;i++)
        {
            var Panel = Scripts[i];
            Panel.parentNode.removeChild(Panel);
            Masada_250.InsertScript(Panel.id, Panel.innerHTML);
        }
        if (Name.length) eval(Name + '.call(This);');
        else if(This.Callback) This.Callback(This);
    }
  
    var OnLoadComplete = function(rtn)
    {
        if (typeof(This.Resources[Name]) == 'undefined')
        {
            This.Resources[Name] = rtn;
        }
        Build(rtn)
        if (This.Callback) This.Callback(This);
    }
    
    if (typeof(This.Resources[Name]) == 'undefined')
    {
        gvLib.WebServices.LoadAsciiFile(Path , OnLoadComplete); 
    }
    else
    {
        Build(This.Resources[Name])
        if (This.Callback) This.Callback(This);
    }
}

Masada_250.Panel.prototype.Resources = {}
Masada_250.RecordSet = function(tableName, key)
{
    var ThisControl = this;
    var d = document;
    var RecordKey = "Id";
    var ReturnDoc = null;
    var RecordDoc = null;
    var EventHandler = null;
    var Fields = null;
    var Rows = null;
    var HeaderNode = null;
    
    this.Closed = false;
    this.Status = null;
    this.StatusText = null;
    this.RowCount = 0;
    this.QueryValue = 0;
    
    var TableName = tableName;
    if (arguments.length == 2) RecordKey = key;
    
    
    var Callback = function(rtn)
    {
        ReturnDoc = new ActiveXObject("MSXML2.DOMDocument")
        ReturnDoc.loadXML(rtn);
        var root = ReturnDoc.documentElement;
        HeaderNode = root.selectSingleNode("Header");
        var Node = HeaderNode.selectSingleNode("RowCount");
        if (Node != null) ThisControl.RowCount = parseInt(Node.text);
        var StatusNode = HeaderNode.selectSingleNode("ReturnStatus");
        ThisControl.Status = StatusNode.attributes.getNamedItem("Condition").text;
        ThisControl.StatusText = StatusNode.text;

        switch(ThisControl.Status)
        {
            case 'Error':
                alert(ThisControl.StatusText);
                break;
            case 'New':
                RecordDoc = ReturnDoc;
                Rows = root.selectSingleNode("Rows");
                Fields = Rows.selectSingleNode("Row[@RowNbr='" + '0' + "']/Fields");
                ClearDirtyFlag();
                if (RecordKey != 'Id') HeaderNode.selectSingleNode("RecordKey").text = RecordKey;
                SetValue(RecordKey,'0');
                break;
            case 'Open':
                if (ThisControl.RowCount == 0) break;
                RecordDoc = ReturnDoc;
                Rows = root.selectSingleNode("Rows");
                Fields = Rows.selectSingleNode("Row[@RowNbr='" + '0' + "']/Fields");
                if (RecordKey != 'Id') HeaderNode.selectSingleNode("RecordKey").text = RecordKey;
                ClearDirtyFlag();
                break;
            case 'Insert':
                SetValue(RecordKey,ThisControl.StatusText);
                break;
            case 'Update':
                ClearDirtyFlag();
                break;
            case 'AdRequest':
                RecordDoc = ReturnDoc;
                Rows = RecordDoc.documentElement.selectSingleNode("Rows");
                break;
            case 'NonQuery':
                ThisControl.QueryValue = parseInt(HeaderNode.selectSingleNode("QueryValue").text);
                break;
        }
        if (EventHandler != null) EventHandler(ThisControl);
    }
    
    var Errorback = function(err)
    {
        alert("(RecordSet():error during ajax call");
    }
    
    var Load = function(Query,eventHandler)
    {
        if (arguments.length == 2)
            EventHandler = eventHandler;
        else
            EventHandler = null;
        
        this.Closed = false;
        if (TableName == 'Users')
        {
            RecordDoc = Main.Ad.GetUsersTblDoc();
            var root = RecordDoc.documentElement;
            Rows = root.selectSingleNode("Rows");
            Fields = Rows.selectSingleNode("Row[@RowNbr='" + '0' + "']/Fields");
            var Count = root.selectSingleNode("Header/RowCount").text;
            ThisControl.RowCount = parseInt(Count);
            //ClearDirtyFlag();
            if (EventHandler != null) EventHandler(ThisControl);
        }
        else
            gvLib.WebServices.GetTableXml(TableName,Query, Callback,Errorback);
    }

    var Store = function(eventHandler)
    {
        if (arguments.length == 1)
            EventHandler = eventHandler;
        else
            EventHandler = null;
            
        gvLib.WebServices.StoreTableXml(RecordDoc.xml,Callback);
    }
    
       
    var Close = function()
    {
        tis.Closed = true;
        Store();
    }
    
    var ClearDirtyFlag = function()
    {
        var nodes = Fields.selectNodes("Field");
        for (var i=0;i< nodes.length;i++)
           nodes[i].attributes.getNamedItem('IsDirty').value = "False";
    }
    
    var SetAllDirtyFlags = function(value)
    {
        var Value = 'False';
        if (value) Value = 'True';
        var nodes = Fields.selectNodes("Field");
        for (var i=0;i< nodes.length;i++)
           nodes[i].attributes.getNamedItem('IsDirty').value = Value;
    }
    
    var DirtyCount = function()
    {
        var count = 0;
        var nodes = Fields.selectNodes("Field");
        for (var i=0;i< nodes.length;i++)
           if (nodes[i].attributes.getNamedItem('IsDirty').value == "True") count++;
        return count;
    }
    
    var SetValue = function(Name,Value)
    {
        var FieldNode = Fields.selectSingleNode("Field[@Id='" + Name + "']");
        if (FieldNode.text != Value)
        {            
            FieldNode.text = Value;
            FieldNode.attributes.getNamedItem('IsDirty').value = "True";
        }
    }
    
    var GetValue = function(arg1, arg2)
    {
        var RowNbr = '0';
        var Name = null;
        if (arguments.length == 1)
        {
            Name = arg1;
        }
        else
        {
            RowNbr = arg1;
            Name = arg2;
        }
        if (ThisControl.RowCount < parseInt(RowNbr)) alert("rowcount error");
        Fields = Rows.selectSingleNode("Row[@RowNbr='" + RowNbr + "']/Fields");
        return Fields.selectSingleNode("Field[@Id='" + Name + "']").text;
    }
    
    var ExtractForm = function(List)
    {
        var UseUpperCase = false;
        for (var i=0;i<List.length;i++)
        {
            var Obj = List[i];
            if(Obj.SourceName != null)
            {
                SetValue(Obj.SourceName,Obj.Value());
            }
            else
            {
                var Path = Obj.split(':');
                var Control = Path[1];
                var Ext = Control.substr(Control.length-3);
                if (Ext == 'Img') continue; // don't write signature
                var FieldName = Control.substring(0,Control.length-3);
                if (UseUpperCase) FieldName = FieldName.toUpperCase();
                SetValue(FieldName,GetFormValue(List[i]));
            }
        }
    }
    
    var ExtractXmlold = function(List)
    {
        var UseUpperCase = false;
        for (var i=0;i<List.length;i++)
        {
            var Path = List[i].split(':');
            var NameSpace = Path[0];
            var Control = Path[1];
            
            var FieldName = Control.substring(0,Control.length-3);
            if (UseUpperCase) FieldName = FieldName.toUpperCase();
            var CurrentNode = Fields.selectSingleNode("Field[@Id='" + FieldName + "']");
            if (CurrentNode == null)
            {
                alert("Field: " + FieldName + " not found");
            }
            else
            {
                SetFormValue(List[i], CurrentNode.text)
            }
        }
    }  
    
    var ExtractXml = function(List)
    {
        //alert('doing extract');
        var UseUpperCase = false;
        var FieldName;
        for (var i=0;i<List.length;i++)
        {
            var Obj = List[i];
            if (typeof(Obj) == 'string') // original
            {
                var Path = Obj.split(':');
                var NameSpace = Path[0];
                var Control = Path[1];
                
                FieldName = Control.substring(0,Control.length-3);
                if (UseUpperCase) FieldName = FieldName.toUpperCase();
                var CurrentNode = Fields.selectSingleNode("Field[@Id='" + FieldName + "']");
                if (CurrentNode == null)
                {
                    alert("Field: " + FieldName + " not found");
                }
                else
                {
                    SetFormValue(List[i], CurrentNode.text)
                }
            }
            else if(Obj.SourceName != 'undefined' && Obj.SourceName != null)
            {
                FieldName = Obj.SourceName;
                var CurrentNode = Fields.selectSingleNode("Field[@Id='" + FieldName + "']");
                if (CurrentNode == null)
                {
                    alert("Field: " + FieldName + " not found");
                }
                else
                {
//                alert(CurrentNode.text);
                    Obj.Value(CurrentNode.text);
                }
            }
            else
                alert(FieldName);
        }
    }  

    var AddRow = function()
    {
        var Rows = RecordDoc.documentElement.selectSingleNode("Rows");
        Rows.setAttribute("Count", '1');
        var Row = RecordDoc.createNode(1, 'Row', '');
        Rows.appendChild(Row);
        Row.setAttribute('RowNbr', '0');
        Fields = RecordDoc.createNode(1,"Fields", "");
        Row.appendChild(Fields);

        var SchemaFields = RecordDoc.documentElement.selectNodes("Schema/Fields/Field");
        for (var i=0;i<SchemaFields.length;i++)
        {
            var Field = RecordDoc.createNode(1, 'Field', '');
            Field.setAttribute('Id', SchemaFields[i].attributes.getNamedItem('Id').value);
            Field.setAttribute('IsDirty', 'False');
            Fields.appendChild(Field);
        }
    }
    
    var AddCol = function()
    {
        var SchemaFields = RecordDoc.documentElement.selectNodes("Schema/Fields/Field");
    }
    
    
    this.RecordDoc = RecordDoc;
    this.AddRow = AddRow;
    this.AddCol = AddCol;
    this.Load = Load;
    this.Store = Store;
    this.Close = Close;
    this.DirtyCount = DirtyCount;
    this.SetValue = SetValue;
    this.GetValue = GetValue;
    this.SetAllDirtyFlags = SetAllDirtyFlags;
    this.ExtractXml = ExtractXml;
    this.ExtractForm = ExtractForm;
}

Masada_250.Select = function()
{
    var This = this;
    var Element = null;
    var TextName = null;
    var ValueName = null;
    var Handler = null;
    var Arg = 0;
    var Length = arguments.length;

    if (typeof (arguments[0]) == 'object')
    {
        Element = arguments[0];
    }
    else
    {
        Element = $$('select');
        Element.style.width = arguments[Arg++] + 'px';
        if (--Length)
            Element.style.height = arguments[Arg] + 'px'
    }

    this.OnComplete = function(handler)
    {
        Handler = handler;
        return This;
    }

    var Add = function(text, value) {
        var o = $$("OPTION");
        o.text = text;
        o.value = value;
        Element.options.add(o);
        return This;
    }

    this.Load = function(query)
    {
        var ValueName = arguments[2];
        var TextName = arguments[3];
        var ListData = new Masada_250.RecordSet(arguments[1]);
        var OnComplete = function(rtn)
        {
            for (var i = 0; i < rtn.RowCount; i++)
            {
                Add(rtn.GetValue(i, TextName), rtn.GetValue(i, ValueName));
            }
            if (Handler != null) Handler(This);
        }
        ListData.Load(query, OnComplete);
        return This;
    }

    this.Index = function(idx)
    {
        Element.selectedIndex = idx;
    }

    this.Style = function(name, value)
    {
        Lbl.style[name] = value;
        return This;
    }

    this.Add = Add;
    this.Element = Element;
}

Masada_250.GetDdlValue = function(ddl)
{
    if (ddl && ddl.selectedIndex > -1)
    {
        return ddl[ddl.selectedIndex].value;
    }
    return '';
}

Masada_250.SetDdlOption = function(control,value)
{
    var Options = control.options;
    var Index = control.selectedIndex;
    for(var i=0;i<Options.length;i++)
    {
        var Option = Options[i];
        var Value = Option.value.trim();
        if (Value.length == 0) Value = Option.text.trim();
        if(Value == value)
        {
            control.selectedIndex = i;
            return;
        }
    }
    control.selectedIndex = 0;
}

Masada_250.Table = function(width)
{
    var d = document;
    var ThisTable = this;
    var Tbl = this.Element = $$('table');
    if (arguments == 1) Tbl.width = width;
    Tbl.cellSpacing = '0';
    Tbl.cellPadding = '0';
    Tbl.border = '0';
    var DomTable = Tbl.appendChild($$('tbody'));
    DomTable.style.display = 'none';
    
    var Visible = function(state)
    {
        if (state) DomTable.style.display = 'block';
        else DomTable.style.display = 'none';
        return ThisTable;
    }
    var Position = function(mode,left,top)
    {
        Tbl.style.position = mode;
        Tbl.style.left = left + 'px';
        Tbl.style.top = top + 'px';
    }
    
    var BindTo = function(parent)
    {
        parent.appendChild(Tbl)
        return ThisTable;
    }
    
    var Row = function()
    {
        var ThisRow = this;
        this.Table = ThisTable;
        this.DomRow = DomTable.appendChild($$('tr'));
        var Cell = function(width,addDiv)
        {
            var ThisCell = this;
            this.Row = ThisRow;
            var td = $$('td');
            ThisRow.DomRow.appendChild(td);
            if (width) td.style.width = width;
            if (addDiv)
            {
                this.Element = $$('div');
                td.appendChild(this.Element);
                this.Element.style.height = ThisRow.DomRow.style.height;
            }
            else
                this.Element = td;
            
            var GetPosition = function()
            {
                return GetElementPosition(ThisCell.Element);
            }
            this.GetPosition = GetPosition;
        }
        
        var AddCell = function(width,addDiv)
        {
            var Width = null;
            var AddDiv = false;
            if (arguments.length > 0) Width = SetStyle(width);
            if (arguments.length > 1) AddDiv = addDiv;
            var cell = new Cell(Width,AddDiv);
            cell.Element.style.height = ThisRow.DomRow.style.height;
            return cell;
        }
        this.AddCell = AddCell;
    }
    
    var AddRow = function(height)
    {
        var row = new Row();
        if (arguments.length > 0) row.DomRow.style.height = SetStyle(height);
        return row;
    }
    this.BindTo = BindTo;
    this.AddRow = AddRow;    
    this.Position = Position;
    this.Visible = Visible;
}

Masada_250.XmlDom = function(Xml)
{
    var xmlDoc = null;
    
    var OuterXML = function(node)
    { //  with Opera
        if (node.ownerDocument && node.ownerDocument.implementation &&
            document.implementation.createLSSerializer &&
            (serializer = node.ownerDocument.implementation.createLSSerializer()))
        {
            return serializer.writeToString(node);
        }
    }
    
    try //Internet Explorer
    {
        xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
        xmlDoc.async = "false";
        xmlDoc.loadXML(Xml);
    }
    catch(e)
    {
        try //Firefox, Mozilla, Opera, etc.
        {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(Xml,"text/xml");
        }
        catch(e) {alert(e.message); return null;}
    }
    
    this.OuterXML = OuterXML;
    this.DocumentElement = xmlDoc.documentElement;
    return xmlDoc;
}

Masada_250.VerticalTab = function(tabRoot)
{
    var This = this;
    var ContentDiv = $$('div');
    var Styles = tabRoot.lastChild;
    var OnClickHander = null;
    var PanelWidth = tabRoot.getAttribute('width');
    var PanelHeight = tabRoot.getAttribute('height');
    
    // Menu area
    var tbl = ContentDiv.appendChild($$("table"));
    var TabBody = tbl.appendChild($$("tbody"));
    var TabRow = TabBody.appendChild($$("tr"));
    var cell = TabRow.insertCell();
    cell.style.verticalAlign = 'top';
    
    var MenuHdr = $$('div');
    cell.appendChild(MenuHdr);
    
    var MenuDiv = $$('div');
    cell.appendChild(MenuDiv);

    // content area
    var MainContentCell = TabRow.insertCell();
    MainContentCell.style.verticalAlign = 'top';

    var ContentMgrDiv = new Masada_250.ContentManager(PanelWidth,PanelHeight,0,0);
    //ContentMgrDiv.style.borderStyle = 'inset'; 
    ContentMgrDiv.style.color = 'black';
    ContentMgrDiv.style.backgroundColor = 'white';
    ContentMgrDiv.style.font = 'normal 9pt arial';
    MainContentCell.appendChild(ContentMgrDiv);
    var ContentMgr = ContentMgrDiv.Manager;
    
    var mainMenu = new Masada_250.Menu(Styles);
    MenuDiv.appendChild(mainMenu.Container);

    this.AddHandler = function(cbFunction)
    {
        OnClickHander = cbFunction;
    }

    var Add = function(menuItem,div)
    {
        div.id = menuItem;
        ContentMgr.InsertExisting(div,'auto');
        mainMenu.Add(menuItem,menuItem,ItemClick);
    }

    var ItemClick = function(obj)
    {
        var PnlDiv = ContentMgr.Find(obj.innerHTML);
        if (PnlDiv)
        {
            ContentMgr.CurrentPanel(PnlDiv);
            if (OnClickHander) OnClickHander(PnlDiv);
        }
    }
    this.Add = Add;
    this.BindTo = function(parent) { parent.appendChild(ContentDiv); return This; }
}

Masada_250.Menu = function(styles)
{
    var MenuDefaultStyle = new Masada_250.Style(styles.childNodes[1]);
    var MenuHoverOverStyle = new Masada_250.Style(styles.childNodes[2]);
    var MenuMouseOutStyle = new Masada_250.Style(styles.childNodes[3]);
 
    var This = this;
    d = document;
    this.Container = $$('Div');
    this.Container.style.backgroundColor = 'Gainsboro';
            
    var MenuItem = function(container, name, label, className, onclick)
    {
        var Item = $$('a');
        MenuDefaultStyle.SetStyle(Item);

        Item.onmouseover = function()
        {
            MenuHoverOverStyle.SetStyle(this);
        }
            
        Item.onmouseout = function()
        {
            MenuMouseOutStyle.SetStyle(this);
        }

        Item.onclick = function(){onclick(this);}
        Item.name = name;
        Item.className = className;
        Item.innerHTML = label;
        container.appendChild(Item);
        this.div = document.createElement('div');
        this.div.id = name;
        
        Item.Div = this.div;
        container.appendChild(this.div);
        
        var Add = function(name, label, clickEvent)
        {
            return new MenuItem(this.div, name, label,'submenu',clickEvent);
        }
        this.Add = Add;
    }
    
    var Add = function(name, label, clickEvent)
    {
        var ClickEvent = null;
        if (arguments.length == 3) ClickEvent = clickEvent;
        return new MenuItem(this.Container, name, label, 'menu1',ClickEvent);
    }
    this.Add = Add;
}

Masada_250.Style = function(style)
{
    var This = this;
    var Properties = style.childNodes;
    for (var i=0;i<Properties.length;i++)
    {
        var Property = Properties[i].attributes[0];
        this[Property.nodeName] = Property.value;
    }

    var SetStyle = function(cell)
    {
        for (var property in This)
        {
            if (property.substr(0,8) == 'function') continue;
            cell.style[property] = This[property];
        }
    }   
    this.SetStyle = SetStyle;
}

// Copyright (c) 2008 Graphic Vision NeuroInformatics, Inc.
// Version 2.01

 Masada_250.ContentManager = function(width, height, left, top)
 {
    var ThisControl = this;
    
    var Width = width + 'px';
    var Height = height + 'px';
    var Left = left + 'px';
    var Top = top + 'px';
    
    var d = document;
    var CurrentDiv = null;
    var count = 0;
    var Divs = [];
    Divs.length = 0;
    
    var Properties = function()
    {
        this.Name = null;
        this.IsDisabled = false;
        this.Overflow = 'hidden';
        this.Visible = function(state) {  }
        this.Clear = function() { ClearDiv(this);  }
    }
    
    var Container = d.createElement('Div');
    Container.Manager = this;
    Container.style.cssText = "";
    Container.style.width = Width;
    Container.style.height = Height;
    Container.style.top = Top;
    Container.style.left = Left;
    Container.style.position = 'relative';
    Container.style.visibility = 'visible';
    Container.style.overflow = 'hidden';
    Container.style.margin = '0px';
    Container.style.padding = '0px';
    Container.style.zIndex = 1000;
      
    var InsertExisting = function(div,overflow)
    {
        var divNode = div.parentNode.removeChild(div);
        Insert(divNode,overflow);
        Container.appendChild(divNode);
        return divNode;
    }
    
    var InsertNew = function(id,overflow)
    {
        var div = d.createElement('div');
        div.id = id;
        Insert(div,overflow);
        Container.appendChild(div);
        return div;
    }
    
    var Insert = function(div,overflow)
    {
        div.Properties = new Properties();
        if (arguments.length == 2) div.Properties.Overflow = overflow;
        if (Divs.length == 0)
        {
            CurrentDiv = div;
        }
        InitializeDiv(div);
        Divs.push(div);
        return div;
    }
    
         
    var Clear = function()
    {
        DeleteChildren(Container);
    }
    
    var Find = function(id)
    {
        for (var i=0;i<Divs.length;i++)
        {
            if (Divs[i].id == id) return Divs[i];
        }
        return null;
    }
    
    var InitializeDiv = function(div)
    {
        div.style.position = 'absolute';
        div.style.display = 'none';
        div.style.overflow = div.Properties.Overflow;
        div.style.borderStyle = 'none'; 
        div.style.zIndex = 1;
        div.style.left = 0;
        div.style.top = 0;
        div.style.width = Width;
        div.style.height = Height;
        div.style.padding = '0px';
    }
    
    var CurrentPanel = function(div)
    {
        if (arguments.length == 0 && CurrentDiv) return CurrentDiv;
        if (div.Properties.IsDisabled) return;
        div.style.display = 'block';
        div.style.zIndex = 10000;
        
        if (CurrentDiv && div != CurrentDiv)
        {
            CurrentDiv.style.display = 'none';
            CurrentDiv.style.zIndex = 1;
            CurrentDiv = div;
        }
    }
    
    Container.style.display = 'block';

    this.Find = Find;
    this.InsertNew = InsertNew;
    this.InsertExisting = InsertExisting;
    this.CurrentPanel = CurrentPanel;
    return Container;
}

Masada_250.CssManager = function()
{
        thecss = document.createStyleSheet();      
//    //var thecss = new Array();
//    if (document.styleSheets[0].cssRules)  // Standards Compliant
//    {
//        thecss = document.styleSheets[0].cssRules;
//    }
//    else
//    {  
//        //thecss = document.styleSheets[0].rules;  // IE 
//    }
//    alert(thecss.length);
//    for (i=0;i<thecss.length;i++) alert(thecss[i].selectorText);
    
    var Insert = function(selector, declaration)
    {
        var Sheet = document.styleSheets[0]
        var NbrOfRules = Sheet.cssRules ? Sheet.cssRules.length : Sheet.rules.length

        if (Sheet.deleteRule) // firefox
        {
            Sheet.insertRule(selector + '{' + declaration + '}', NbrOfRules-1);
        }
        else if (Sheet.removeRule) // IE
        {
            Sheet.addRule(selector, declaration);
        }
    }
    
    var Delete = function()
    {
        var Sheet = document.styleSheets[0]
        var NbrOfRules = Sheet.cssRules ? Sheet.cssRules.length : Sheet.rules.length

        if (Sheet.deleteRule) // firefox
        {
            Sheet.deleteRule(NbrOfRules-1);
        }
        else if (Sheet.removeRule) // IE
        {
            Sheet.removeRule(NbrOfRules-1);
        }
    }
    this.Insert = Insert;
}


Masada_250.VerticalMenu = function()
{
    
    var This = this;
    d = document;
    this.Container = d.createElement('Div');
    this.Container.style.backgroundColor = 'Gainsboro';
    
    var CssMgr = new Masada_250.CssManager();
    
    CssMgr.Insert('body',"margin:0;padding:0;border:none;");
    
    
    CssMgr.Insert('.menu1',"height:20px;");
    CssMgr.Insert('.menu1',"display:block;");
    //CssMgr.Insert('.menu1',"background-image:url(/images/sidebar_header.png);");
    CssMgr.Insert('.menu1',"padding-left:10px;");
    CssMgr.Insert('.menu1',"color:#204070;");
    CssMgr.Insert('.menu1',"background-color:Gainsboro;");
    CssMgr.Insert('.menu1',"border-color:gray;");
    CssMgr.Insert('.menu1',"border:solid 1px;");
    
    
    CssMgr.Insert('.submenu',"height:20px;");
    CssMgr.Insert('.submenu',"display:block;");
    //CssMgr.Insert('.submenu',"background-image:url(/images/sidebar_header.png);");
    CssMgr.Insert('.submenu',"padding-left:20px;");
    CssMgr.Insert('.submenu',"color:#204070;");
    CssMgr.Insert('.submenu',"background-color:Gainsboro;");
    //CssMgr.Insert('.submenu',"border:solid 1px;");

    CssMgr.Insert('.hide','display: none;');
    CssMgr.Insert('.show','display: block;');
    
    var ShowHide = function(obj)
    {
        var div = obj.Div;
        if(div.className != 'show')
           div.className = 'show';
        else
           div.className = 'hide';
    }
        
    var MenuItem = function(container, name, label, className, onclick)
    {
        var Item = document.createElement('a');
        Item.onmouseover = function() {this.style.backgroundColor='gray'; this.style.color='#ffffff'; document.body.style.cursor = 'hand';}
        Item.onmouseout = function() {this.style.backgroundColor='Gainsboro'; this.style.color='#204070'; document.body.style.cursor = 'default';}
        Item.onclick = function(){onclick(this);}
        Item.name = name;
        Item.className = className;
        Item.innerHTML = label;
        container.appendChild(Item);
        this.div = document.createElement('div');
        this.div.id = name;
        this.div.className = 'hide';
        Item.Div = this.div;
        container.appendChild(this.div);
        
        var Add = function(name, label, clickEvent)
        {
            return new MenuItem(this.div, name, label,'submenu',clickEvent);
        }
        this.Add = Add;
    }
    
    var Add = function(name, label, clickEvent)
    {
        var ClickEvent = ShowHide;
        if (arguments.length == 3) ClickEvent = clickEvent;
        return new MenuItem(this.Container, name, label, 'menu1',ClickEvent);
    }
    this.Add = Add;
}

Masada_250.TabPanel = function(panelNode)
{
    var This = this;
    var PanelNode = panelNode;
    var Container = $$('div');

    var Id = PanelNode.getAttribute('Id');
    var height = parseInt(PanelNode.getAttribute('Height'));
    var width = parseInt(PanelNode.getAttribute('Width'));
    var BackgroundColor = PanelNode.getAttribute('Background');

    var Width = width + 'px';
    var Height = height + 'px';

    var CurrentTab = null;
    var count = 0;

    var Tab = function()
    {
        var This = this;
        this.id = null;
        this.IsDisabled = false;
        this.Width = '75px';
        this.Height = '24px';
        this.SelectImage = null;
        this.UnSelectImage = null;
        this.DisabledImage = null;
        this.CurrentImage = null;
        this.Header = null;
        this.OnSelect = null;
        this.Visible = function(state) { }
        this.Focus = function()
        {
            This.Disabled(false);
            SelectTab(This);
            return This;
        }
        this.Disabled = function(state)
        {
            This.IsDisabled = state;
            This.Element.style.display = 'none';
            if (state)
            {
                if (This == CurrentTab) CurrentTab = null; 
                This.Header.style.background = "url(" + This.DisabledImage + ")";
            }
            else
            {
                This.Header.style.background = "url(" + This.UnSelectImage + ")";
            }

            return This;
        }
    }

    Container.style.cssText = "";
    Container.style.width = Width;
    Container.style.height = Height;
    Container.style.overflow = 'hidden';
    Container.style.margin = '0px';
    Container.style.padding = '0px';
    Container.style.color = 'black';
    Container.style.font = 'normal 9pt arial';

    var tbl = Container.appendChild($$("table"));
    tbl.cellPadding = '0px';
    tbl.cellSpacing = '0px';
    var TabBody = tbl.appendChild($$("tbody"));
    var TabRow = TabBody.appendChild($$("tr"));

    var TabDiv = $$('Div');
    Container.appendChild(TabDiv);
    TabDiv.style.position = 'relative';
    TabDiv.style.padding = '0px';
    TabDiv.style.color = 'black';
    TabDiv.style.font = 'normal 9pt arial';
    TabDiv.style.zIndex = 400;
    TabDiv.style.height = Height;
    TabDiv.style.backgroundColor = BackgroundColor;


    var InsertDiv = function(Tab)
    {
        Tab.Element = $$('Div');
        TabDiv.appendChild(Tab.Element);
        ResetDiv(Tab.Element);
        Tab.Disabled(true);
        return Tab;
    }

    var ResetDiv = function(div)
    {
        div.style.cssText = "";
        div.style.display = 'none';
        div.style.zIndex = 1;
        div.style.left = 0;
        div.style.top = 0;
        //div.style.width = Width;
        div.style.height = Height;
        div.style.position = 'absolute';
        div.style.overflow = 'auto';
        div.style.padding = '5px';
        return This;
    }

    var SelectTab = function(NewTab)
    {
        if (NewTab.IsDisabled) return;
        //if (CurrentTab && CurrentTab.Id == NewTab.Id) return;
        if (CurrentTab != null)
        {
            CurrentTab.Header.style.background = "url(" + CurrentTab.UnSelectImage + ")";
            CurrentTab.Element.style.display = 'none';
        }
        NewTab.Header.style.background = "url(" + NewTab.SelectImage + ")";
        NewTab.Element.style.display = 'block';
        CurrentTab = NewTab;
        if (NewTab.OnSelect) NewTab.OnSelect();
        return This;
    }

    var TabsNode = PanelNode.selectSingleNode("Tabs");
    var TabNodes = TabsNode.selectNodes("Tab");
    for (var i = 0; i < TabNodes.length; i++)
    {
        var Id = TabNodes[i].attributes.getNamedItem('Id').value;
        var TabWidth = TabNodes[i].attributes.getNamedItem('Width').value;
        var TabHeight = TabNodes[i].attributes.getNamedItem('Height').value;
        var ImagesNodes = TabNodes[i].selectNodes("Image");

        var NewTab = new Tab();
        NewTab.Id = Id;
        NewTab.SelectImage = ImagesNodes[0].attributes.getNamedItem('Path').value;
        NewTab.UnSelectImage = ImagesNodes[1].attributes.getNamedItem('Path').value;
        NewTab.DisabledImage = ImagesNodes[2].attributes.getNamedItem('Path').value;

        var Header = NewTab.Header = TabRow.appendChild($$("td"));
        Header.id = Id;
        Header.style.background = "url(" + NewTab.DisabledImage + ")";
        Header.style.width = TabWidth + 'px';
        Header.style.height = TabHeight + 'px';
        Header.onclick = function()
        {
            SelectTab(This[this.id]);
        }
        Header.onmouseover = function()
        {
            this.style.cursor = 'hand';
        }
        Header.onmouseout = function()
        {
            this.style.cursor = 'pointer';
        }
        var space = TabRow.appendChild($$("td"));
        space.style.width = '4';
        This[Id] = NewTab;
        InsertDiv(NewTab);
    }

    var BindTo = function(parent)
    {
        parent.appendChild(Container);
        Container.style.backgroundColor = parent.style.backgroundColor;
        return This;
    }

    this.BindTo = BindTo;
    this.Tab = Tab;
}

    Masada_250.TabPanelOld = function(root)
    {
        var This = this;
        var PanelNode = root;
        var Container = $$('div');

        var Id = PanelNode.getAttribute('Id');
        var height = parseInt(PanelNode.getAttribute('Height'));
        var width = parseInt(PanelNode.getAttribute('Width'));
        var BackgroundColor = PanelNode.getAttribute('Background');

        var Width = width + 'px';
        var Height = height + 'px';

        var CurrentTab = -1;
        var count = 0;
        var Tabs = [];
        Tabs.length = 0;

        var TabElement = function()
        {
            this.Nbr = null;
            this.Name = null;
            this.IsDisabled = false;
            this.Width = '75px';
            this.Height = '24px';
            this.SelectImage = null;
            this.UnSelectImage = null;
            this.DisabledImage = null;
            this.CurrentImage = null;
            this.ContentPath = null;
            this.Cell = null;
            this.Div = null;
            this.OnSelect = null;
            this.Focus = function() { DisableTab(this, false); SelectTab(this); }
            this.Visible = function(state) { }
            this.Disabled = function(state) { DisableTab(this, state); return this; }
        }

        Container.style.cssText = "";
        Container.style.width = Width;
        Container.style.height = Height;
        Container.style.position = 'absolute';
        Container.style.overflow = 'hidden';
        //Container.style.borderStyle = 'inset'; 
        Container.style.margin = '0px';
        Container.style.padding = '0px';
        Container.style.zIndex = 1000;
        Container.style.color = 'black';
        Container.style.font = 'normal 9pt arial';

        var tbl = Container.appendChild($$("table"));
        tbl.cellPadding = '0px';
        tbl.cellSpacing = '0px';
        var TabBody = tbl.appendChild($$("tbody"));
        var TabRow = TabBody.appendChild($$("tr"));

        var TabDiv = $$('Div');
        Container.appendChild(TabDiv);
        TabDiv.style.position = 'relative';
        TabDiv.style.padding = '0px';
        TabDiv.style.color = 'black';
        TabDiv.style.font = 'normal 9pt arial';
        TabDiv.style.zIndex = 400;
        TabDiv.style.height = Height;
        TabDiv.style.width = Width;
        TabDiv.style.backgroundColor = BackgroundColor;
        var TabPanelRes = null;

        var Tab = function(TabNo)
        {
            return Tabs[TabNo];
        }

        var InsertDiv = function(Tab)
        {
            Tab.Div = $$('Div');
            TabDiv.appendChild(Tab.Div);
            ResetDiv(Tab.Div);
            DisableTab(Tab, true);
            return Tab;
        }

        var ResetDiv = function(div)
        {
            div.style.cssText = "";
            div.style.display = 'none';
            div.style.zIndex = 1;
            div.style.left = 0;
            div.style.top = 0;
            div.style.width = Width;
            div.style.height = Height;
            div.style.position = 'absolute';
            div.style.overflow = 'auto';
            div.style.padding = '5px';
        }

        var RefreshTab = function(tab)
        {
            if (tab.IsDisabled)
            {
                tab.CurrentImage.src = tab.DisabledImage.src;
                tab.Div.style.display = 'none';
                tab.Div.style.zIndex = 1;
            }
            else
            {
                if (tab.Nbr == CurrentTab)
                {
                    tab.CurrentImage.src = tab.SelectImage.src;
                    tab.Div.style.display = 'block';
                    tab.Div.style.zIndex = 400;
                }
                else
                {
                    tab.CurrentImage.src = tab.UnSelectImage.src;
                    tab.Div.style.display = 'none';
                    tab.Div.style.zIndex = 1;
                }
            }
        }

        var DisableTab = function(tab, state)
        {
            tab.IsDisabled = state;
            RefreshTab(tab);
        }

        var SelectTab = function(NewTab)
        {
            var TabNo = NewTab.Nbr;
            if (NewTab.IsDisabled || CurrentTab == TabNo) return;
            var LastCurrentTab = null;
            if (CurrentTab > -1) LastCurrentTab = Tabs[CurrentTab];
            CurrentTab = TabNo;
            RefreshTab(NewTab);
            if (LastCurrentTab) RefreshTab(LastCurrentTab);
            if (NewTab.OnSelect) NewTab.OnSelect();
        }

        var TabsNode = PanelNode.selectSingleNode("Tabs");
        var TabNodes = TabsNode.selectNodes("Tab");
        for (var i = 0; i < TabNodes.length; i++)
        {
            var ImagesNodes = TabNodes[i].selectNodes("Image");
            var NewTab = new TabElement();
            NewTab.SelectImage = new Image();
            NewTab.SelectImage.src = ImagesNodes[0].attributes.getNamedItem('Path').value;
            NewTab.UnSelectImage = new Image();
            NewTab.UnSelectImage.src = ImagesNodes[1].attributes.getNamedItem('Path').value;
            NewTab.DisabledImage = new Image();
            NewTab.DisabledImage.src = ImagesNodes[2].attributes.getNamedItem('Path').value;
            NewTab.CurrentImage = new Image();
            //NewTab.CurrentImage.src = NewTab.DisabledImage.src;
            NewTab.Name = TabNodes[i].selectSingleNode("Module").attributes.getNamedItem('Name').value;
            NewTab.Cell = $$("td");
            NewTab.Nbr = count;
            NewTab.Cell.Parent = NewTab;
            NewTab.Cell.onmousedown = function() { SelectTab(this.Parent); }
            NewTab.Cell.appendChild(NewTab.CurrentImage);

            TabRow.appendChild(NewTab.Cell);
            var space = $$("td");
            space.style.width = '4';
            TabRow.appendChild(space);
            Tabs.push(NewTab);
            InsertDiv(NewTab);
            ContentPath = "";
            var ContentNode = TabNodes[i].selectSingleNode("Module").attributes.getNamedItem('Path');
            if (ContentNode) NewTab.ContentPath = ContentNode.value;
            count++;
        }
alert('working');

        this.BindTo = function(parent)
        {
            parent.appendChild(Container);
            return This;
        }
        this.Tab = Tab;
    }

