﻿// Copyright (c) 2010 Graphic Vision NeuroInformatics, Inc.
// Version 3.00

Masada_300 = {}

$ = function()
{
    return document.getElementById(arguments[0]);
}

$$ = function()
{
    return document.createElement(arguments[0]);
}

Masada_300.GetEventHandler = function(event)
{
    if (window.event) event = window.event;
    return event;
}

Masada_300.GetKeyCode = function(event)
{
    var ex = Masada_300.GetEventHandler(event);
    return ex.charCode || ex.keyCode; // e.which
//    if (e.keyCode) code = e.keyCode;
//	else if (e.which) code = e.which;
}

Masada_300.Mask = function()
{
    var This = this;
    var TestExp = null;
    var NextTab = null;
    var Tbx = null;
    var Length = 1000;

    var onkeypressSSN = function(e)
    {
        Tbx.style.color = 'red';
        var Code = Masada_300.GetKeyCode(e);
        var value = Tbx.value;
        var Pos = value.length + 1;
        if (Pos > Length)
        {
            Tbx.value += String.fromCharCode(Code);
            Tbx.onblur();
            return false;
        }
        if (Pos == 4 || Pos == 7)
        {
            if (Code == 45)
                return true;
            else
            {
                Tbx.value += '-';
                Tbx.value += String.fromCharCode(Code);
                return false;
            }
        }
        if ((Code < 48 || Code > 57)) return false;
        return true;
    }

    var onkeypressZIP = function(e)
    {
        Tbx.style.color = 'red';
        var Code = Masada_300.GetKeyCode(e);
        var value = Tbx.value;
        var Pos = value.length + 1;
        if (Pos > Length)
        {
            Tbx.value += String.fromCharCode(Code);
            Tbx.onblur();
            return false;
        }
        if (Pos == 6)
        {
            if (Code == 45)
                return true;
            else
            {
                Tbx.value += '-';
                Tbx.value += String.fromCharCode(Code);
                return false;
            }
        }
        if ((Code < 48 || Code > 57)) return false;
        return true;
    }

    var onkeypressDefault = function(e)
    {
        Tbx.style.color = 'red';
        var value = Tbx.value;
        var Pos = value.length + 1;
        if (Pos > Length) return false;
        return true;
    }

    this.Mode = function(mode)
    {
        switch (mode.toLowerCase())
        {
            case 'ssn':
                {
                    TestExp = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/;
                    Tbx.onkeypress = onkeypressSSN;
                    Length = 10;
                    // Spare = /^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$/;
                    break;
                }
            case 'zip':
                {
                    TestExp = /^(?!0{5})(\d{5})(?!-?0{4})(-?\d{4})?$/;
                    //TestExp = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/;
                    Tbx.onkeypress = onkeypressZIP;
                    Length = 9;
                    break;
                }
            case 'email':
                {
                    TestExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
                    Tbx.onkeypress = onkeypressDefault;
                    break;
                }
            case 'phone':
                {
                    //TestExp = /^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$/;
                    Tbx.onkeypress = onkeypressDefault;
                    break;
                }
            case 'dollar':
                {
                    TestExp = /^\$?\d+(\.(\d{2}))?$/;
                    Tbx.onkeypress = onkeypressDefault;
                    break;
                }
            case 'creditcard': //AMEX, VISA, MasterCard
                {
                    TestExp = /^((4\d{3})|(5[1-5]\d{2}))(-?|\040?)(\d{4}(-?|\040?)){3}|^(3[4,7]\d{2})(-?|\040?)\d{6}(-?|\040?)\d{5}/;
                    Tbx.onkeypress = onkeypressDefault;
                    break;
                }
            case 'signedfloat':
                {
                    TestExp = /^[-+]?[0-9]*\\.?[0-9]+$/;
                    Tbx.onkeypress = onkeypressDefault;
                    break;
                }
            case 'positiveinteger': //AMEX, VISA, MasterCard
                {
                    TestExp = /^[1-9]+[0-9]*$/;
                    Tbx.onkeypress = onkeypressDefault;
                    break;
                }
        }
        return This;
    }

    TestExp = null;

    this.BindTo = function(tbx)
    {
        Tbx = tbx;
        tbx.onblur = function(e)
        {
            if (Tbx.value.length == 0 || !TestExp)
            {
                Tbx.style.color = 'black';
                if (NextTab) NextTab.focus();
                return true;
            }
            if (!TestExp.test(Tbx.value)) { Tbx.focus(); Tbx.value = ''; return false; }
            Tbx.style.color = 'black';
            if (NextTab) NextTab.focus();
            return false;
        }
        Tbx.onkeypress = onkeypressDefault;
        return This;
    }

    this.MaxLength = function(length)
    {
        Length = length;
        return This;
    }
    
    this.ChainTo = function(nextTab)
    {
        NextTab = nextTab;
    }
}

Masada_300.BrowserInfo = function()
{
    this.Type = null;
    var Agent = navigator.userAgent;
    if (Agent.indexOf("MSIE") != -1) this.Type = 'IE';
    else if (Agent.indexOf("Firefox") != -1)  this.Type = 'FF';
    else if (Agent.indexOf("Navigator") != -1)  this.Type = 'NV';
    else if (Agent.indexOf("Opera") != -1 ) this.Type = 'OP';
    else this.Type = 'UK';
}

Masada_300.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.Set = SetStyle;
}

Masada_300.XmlDom = function(Xml)
{
    var xmlDoc = null;
    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;}
    }
    
    var outerXML = function(node)
    {
        if (window.ActiveXObject) return node.xml;
        else return (new XMLSerializer()).serializeToString(node);
    }

    this.Document = xmlDoc;
    this.outerXML = outerXML;
}

if (!window.ActiveXObject)
{
    Element.prototype.selectNodes = function(sXPath)
    {
        var oEvaluator = new XPathEvaluator();
        var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
        var aNodes = new Array();
        if (oResult != null)
        {
            var oElement = oResult.iterateNext();
            while(oElement)
            {
                aNodes.push(oElement);
                oElement = oResult.iterateNext();
            }
        }
        return aNodes;
    }

    Element.prototype.selectSingleNode = function(sXPath)
    {
        var oEvaluator = new XPathEvaluator();
        var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        if (oResult != null)
            return oResult.singleNodeValue;
        else
            return null;
    }
}


Masada_300.Panel = function(width, height, borderColor, backgroundColor, label, overflow)
{
    try
    {
        var Container = $$('div');
        var Content = Container;

        var Width = 0;
        var Height = 0;
        var HdrHeight = 0;
        var FtrHeight = 0;
        var BorderColor = 'transparent';
        var BackgroundColor = 'transparent';
        var Label = '';

        var Overflow = 'auto';

        var Hdr = null;
        var Ftr = null;

        var OnRenderComplete = function(rtn)
        {
            var guids = rtn.split(',');
            if (Hdr && HdrType != 'Std') Hdr.style.background = 'url(/Resource.aro?guid=' + guids[0] + ') ' + 'no-repeat';
            if (Ftr && FtrType != 'Std') Ftr.style.background = 'url(/Resource.aro?guid=' + guids[1] + ') ' + 'no-repeat';
        }

        if (arguments.length == 0)
        {
        }
        else if (arguments.length > 1)
        {
            Width = width;
            Height = height;
            Container.style.font = 'normal 9pt arial';
            Container.style.width = width;

            Hdr = Container.appendChild($$('div'));
            Hdr.style.height = '24px';
            Hdr.style.width = width + 'px';

            BorderColor = borderColor;
            BackgroundColor = backgroundColor;
            Label = label;
            Overflow = overflow;

            Content = Container.appendChild($$('div'));
            Content.style.overflow = Overflow;
            Content.style.height = height + 'px';
            Content.style.backgroundColor = BackgroundColor;
            Content.style.borderStyle = 'solid';
            Content.style.borderColor = BorderColor;
            Content.style.borderWidth = '0px 2px 0px 2px';

            Ftr = Container.appendChild($$('div'));
            Ftr.style.height = '30px';
            Ftr.style.width = width + 'px';
            Masada_224.WebServices.RenderPanelImages3(Width, Height, BorderColor, BackgroundColor, Label, OnRenderComplete, OnAjaxErrors);
        }
        else
        {
            try
            {
                var PanelNode = arguments[0];

                var Id = PanelNode.getAttribute('Id');
                Height = parseInt(PanelNode.getAttribute('Height'));
                Width = parseInt(PanelNode.getAttribute('Width'));
                var BorderWidth = parseInt(PanelNode.getAttribute('BorderWidth'));
                var Type = PanelNode.getAttribute('Type');
                Container.style.width = Width + 'px';

                var HeaderNode = PanelNode.selectSingleNode("Header");
                var HdrType = null;
                if (HeaderNode != null)
                {
                    HdrType = HeaderNode.getAttribute('Type');
                    HdrHeight = parseInt(HeaderNode.getAttribute('Height'));
                    Label = HeaderNode.getAttribute('Label');
                    Hdr = Container.appendChild($$('div'));
                    if (HdrHeight) Hdr.style.height = HdrHeight - 2 + 'px';
                    var HeaderStyleNode = HeaderNode.selectSingleNode("Style");
                    if (HeaderStyleNode) new Style(HeaderStyleNode).SetStyle(Hdr);
                }

                var FooterNode = PanelNode.selectSingleNode("Footer");

                if (HeaderNode || FooterNode)
                {
                    Content = Container.appendChild($$('div'));
                    Content.style.borderStyle = 'solid';
                    Content.style.borderWidth = '0 ' + BorderWidth + 'px ' + '0 ' + BorderWidth + 'px';
                }

                var StyleNode = PanelNode.selectSingleNode("Style");
                if (StyleNode != null)
                {
                    var PanelStyle = new Style(StyleNode);
                    PanelStyle.SetStyle(Content);
                    if (PanelStyle.backgroundColor) BackgroundColor = PanelStyle.backgroundColor;
                    if (PanelStyle.borderColor) BorderColor = PanelStyle.borderColor;
                }

                if (FooterNode)
                { // there's a problem here - when we change headernode to footernode it breaks
                    var FtrType = FooterNode.getAttribute('Type');
                    Ftr = Container.appendChild($$('div'));
                    FtrHeight = parseInt(FooterNode.getAttribute('Height'));
                    if (FtrHeight) Ftr.style.height = FtrHeight + 'px';
                    var FooterStyleNode = FooterNode.selectSingleNode("Style");
                    if (FooterStyleNode) new Style(FooterStyleNode).SetStyle(Ftr);
                }

                var height = (Height - HdrHeight - FtrHeight);
                if (height > 0) Content.style.height = height + 'px';
                else Content.style.height = '0px';
                if (!(HdrType == 'Std' && FtrType == 'Std'))
                {
                    Masada_224.WebServices.RenderPanel(SerializeXML(PanelNode), OnRenderComplete, OnAjaxErrors);
                }
            }
            catch (e)
            {
                throw new Error("unable to configure panel.\r\n" + e.description);
            }
        }

        this.GetHeader = function()
        {
            return Hdr;
        }

        this.BindTo = function(parent)
        {
            parent.appendChild(Container);
            return this;
        }


        this.Div = Container;
        this.Footer = Ftr;
        this.Header = Hdr;
        this.Container = Content;
    }
    catch (e)
    {
        alert("Error(Panel): " + e.description);
    }
}

Masada_300.DatePicker = function(initDate)
{
    var This = this;
    var OnComplete = null;
    var isDisable = false;
    var Mode = 'Calendar';
    var TextField = null;
    var Container = null;
    var DayCells = new Array(32);
    var ColumnWidth = 30;
    var WkDayLblHeight = 20;
    var DateCellHeight = 100;
    var CurrentDate = '';
    var CellTextAlign = 'center';
    var CellVerticalAlign = 'middle';

    if (arguments.length > 0) CurrentDate = initDate;

    var SetStyle = function(control, fontSize, fontWeight, color, backgroundColor)
    {
        control.style.fontFamily = FontFamily;
        control.style.fontSize = fontSize;
        if (fontWeight.length != 0) control.style.fontWeigh = fontWeight;
        control.style.color = color;
        control.style.backgroundColor = backgroundColor;
    }
    
    var MonthLabel = ['January', 'February', 'March', 'April', 'May', 'June',
        'July', 'August', 'September', 'October', 'November', 'December'];
    var DayLabel = null;
    var FontFamily = 'Tahoma, Arial, Helvetica, sans-serif';
    
    
    
    var pickerDiv = $$("div");
    pickerDiv.style.position = 'absolute';
    pickerDiv.style.zIndex = 10000;
    pickerDiv.style.display = 'none';
    pickerDiv.style.borderStyle = 'solid';
    pickerDiv.style.borderWidth = '1px';
        
    var CalTbl = pickerDiv.appendChild($$('table'));
    SetStyle(CalTbl, '12px','', '#505050', '#dddddd');
    CalTbl.cols = '7';
    CalTbl.style.textAlign = 'center';
    CalTbl.style.border = '1px solid #ababab';
              
       
    DayLabel = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
    ColumnWidth = 10;
    WkDayLblHeight = 10;
    WkDayLblFontWeight = 'normal';
    DateCellHeight = 10;
    CellTextAlign = 'center';
    CellVerticalAlign = 'middle';
    
    var CalBody = CalTbl.appendChild($$('tbody'));  
    document.body.appendChild(pickerDiv);
    
    
    var BindTo = function(parent)
    {
     //   SetMode('DatePicker');
        Container = $$("div");
        Container.style.borderStyle = 'inset';
        Container.style.backgroundColor = 'white';
        Container.style.borderWidth = '2px';
        Container.style.width = '90px';
        
        var tbl = Container.appendChild($$("table"));
        tbl.style.borderColor = '#204070';
        tbl.style.borderWidth = '0px';
        tbl.style.width = '100%';
        tbl.style.height = '100%';
        tbl.style.borderCollapse = 'collapse';
        
        var PickerTbody = tbl.appendChild($$("tbody"));
        var NewRow = $$("tr");
        
        var left = $$("td");
        left.style.width = '100%';
        TextField = $$("input");
        TextField.type = 'text';
        TextField.style.width = '68px';
        TextField.style.borderStyle = 'none';
        TextField.borderCollapse = 'collapse';
        TextField.style.fontSize = '10pt';
        TextField.style.height = '14px';
        TextField.style.lineHeight = '14px';
        TextField.value = CurrentDate;
        left.appendChild(TextField);
        NewRow.appendChild(left);
        
        var right = $$("td");
        right.style.padding = 0;
        right.style.margin = 0;
        var Image = $$('img');
        Image.src = 'Images/SelectArrow.gif';
        Image.onclick = DisplayCalendar;
        Image.style.height = '18px';
        right.appendChild(Image);
        NewRow.appendChild(right);

        PickerTbody.appendChild(NewRow);
        parent.value = Value;
        parent.appendChild(Container);
        return This;
    }
        
    var DisplayCalendar = function()
    {
        if (isDisable) return;
        if (pickerDiv.style.display == 'none')
        {
            if (Container)
            {
                var Loc = GetPosition();
                pickerDiv.style.left = Loc[0];
                pickerDiv.style.top = Loc[1];
            }
            CurrentDate = TextField.value;
            var theDate = null;
            if (checkDate(CurrentDate))
            {
                CurrentDate = TextField.value.replace(/-/g,'/'); 
                CurrentDate = CurrentDate.replace(/-/g,'.'); 
                theDate = new Date(CurrentDate);
            }
            else
                theDate = new Date();
            
            BuildCalendar(theDate.getFullYear(), theDate.getMonth());
            var day = theDate.getDate();
            DayCells[day].onmouseout = function()
            {
                this.style.backgroundColor = '#777777';
                this.style.borderColor = '#777777';
                this.style.cursor = 'pointer';
                this.style.color = 'white';
            }
            DayCells[day].onmouseout();
            pickerDiv.style.display = '';
        }
        else
        {
            if (Container)
            {
                if (CurrentDate.length > 0) TextField.value = CurrentDate;
            }
            pickerDiv.style.display = 'none';
            DeleteCalendar();
        }
    }

    var Value = function(Value)
    {
        if (arguments.length == 1){CurrentDate = Value;}
        else return CurrentDate;
    }
       
    var GetPosition = function()
    {
        var x = y = 0;
        var obj = Container;
        var Browser = new Masada_300.BrowserInfo().Type;
        while (obj.offsetParent != null)
        {
            x += obj.offsetLeft;
            y += obj.offsetTop;
            obj = obj.offsetParent;
        }
        if (Browser == 'IE' && Container.parentNode.style.position == '')
        {
            x += 10;
            y += 12;
        }
        y += Container.offsetHeight;
        return [x+'px',y+'px'];
    }
        
    var DeleteCalendar = function()
    {
        while (CalBody.hasChildNodes())
        {
            CalBody.removeChild(CalBody.firstChild);
        }            
    }
    
    var CalendarHeader = function(date)
    {
        var HdrTbl = new Table();
        HdrTbl.style.width = '100%';
        var Row = HdrTbl.AddRow();
        Row.style.backgroundColor = '#dddddd';
        Row.style.border =  'none';
        
        var LftArw = Row.AddCell();
        LftArw.style.width = '10px';
        LftArw.appendChild(getButton(date, -1, "&lt;"));

        var TitleDiv = $$('div');
        TitleDiv.style.fontSize = '11px';
        TitleDiv.style.fontWeight = 'bold';
        TitleDiv.style.color = 'black';
        TitleDiv.innerHTML = MonthLabel[ date.getMonth()] + " " + date.getFullYear();
        var TitleCell = Row.AddCell().appendChild(TitleDiv);
         
        var RgtArw = Row.AddCell();
        RgtArw.style.width = '10px';
        RgtArw.appendChild(getButton(date, 1, "&gt;"));
        
        this.BindTo = function(parent)
        {
            parent.appendChild(HdrTbl);
            return this;
        }
    }
    
    var CalendarFooter = function()
    {
        var HdrTbl = new Table();
        HdrTbl.style.width = '100%';
        var Row = HdrTbl.AddRow();
        Row.style.border =  'none';
        var Footer = Row.AddCell();
        SetStyle(Footer, '10px','bold', 'black', '#dddddd');

        Footer.innerHTML = 'Today: ' + ExtractDate(new Date());

        this.BindTo = function(parent)
        {
            parent.appendChild(HdrTbl);
            return this;
        }
    }
    
    var BuildCalendar = function(year, month)
    {
        var CalDay = new Date(year, month, 1);
        
        var HdrCell = CalBody.appendChild($$('tr')).appendChild($$('td'));
        HdrCell.colSpan = '7';
        Header = new CalendarHeader(CalDay).BindTo(HdrCell);
        
        var WeekRow = CalBody.appendChild($$('tr')); 
        var DayCell = null;
        for(i = 0; i < 7; i++)
        {
            var Cell = WeekRow.appendChild($$('td')); 
            Cell.style.backgroundColor = '#204070';
            Cell.style.width = ColumnWidth + 'px';
            Cell.style.height = WkDayLblHeight + 'px';
            Cell.style.fontWeight = WkDayLblFontWeight;
            Cell.style.color = 'white';
            Cell.innerHTML = DayLabel[i];
        }
        
        WeekRow = CalBody.appendChild($$('tr')); 
        WeekRow.style.backgroundColor = 'White';
        WeekRow.style.borderStyle = 'none';
        WeekRow.style.borderWidth = '0px';

        for (var i=0;i<CalDay.getDay();i++) // leading blanks
        {
            DayCell = WeekRow.appendChild($$('td'));
            DayCell.style.width = ColumnWidth + 'px';
            DayCell.style.height = DateCellHeight + 'px';
            DayCell.innerHTML = '&nbsp';
        }
        do
        {
            var DayOfMonth = CalDay.getDate();
            DayCells[DayOfMonth] = DayCell = WeekRow.appendChild($$('td')); 
            DayCell.style.width = ColumnWidth + 'px';
            DayCell.style.height = DateCellHeight + 'px';
            DayCell.style.border = '1px solid';
            DayCell.DateString = ExtractDate(CalDay);
            DayCell.innerHTML = DayOfMonth.toString();
            DayCell.style.textAlign = CellTextAlign;
            DayCell.style.verticalAlign = CellVerticalAlign;

            DayCell.onmouseover = function()
            {
                this.style.backgroundColor = '#ffff00';
                this.style.borderColor = '#888888';
                this.style.color = 'red';
            }
                
            DayCell.onclick = function()
            {
               CurrentDate = this.DateString;
               DisplayCalendar();
            }
            DayCell.onmouseout = function()
            {
                this.style.backgroundColor = '#fcfcfc';
                this.style.borderColor = '#fcfcfc';
                this.style.cursor = 'pointer';
                this.style.color = 'black';
            }
            DayCell.onmouseout();
            if (CalDay.getDay() == 6)
            {
                WeekRow = CalBody.appendChild($$('tr')); 
            }
            CalDay.setDate(CalDay.getDate() + 1);
        }
        while (CalDay.getDate() > 1)
        
        if (CalDay.getDay() > 0) // trailing blanks
        {
            for (i = 6; i > CalDay.getDay(); i--)
            {
                DayCell = WeekRow.appendChild($$('td')); 
                DayCell.style.width = ColumnWidth + 'px';
                DayCell.style.height = DateCellHeight + 'px';
                DayCell.innerHTML = '&nbsp';
            }
        }
        
        var FtrCell = CalBody.appendChild($$('tr')).appendChild($$('td'));
        FtrCell.colSpan = '7';
        new CalendarFooter().BindTo(FtrCell);
    }
    
    var disabled = function(state)
    {
        TextField.disabled = state;
        isDisable = state;
    }
       
    var getButton = function(date, adjust, label)
    {
        var Month = (date.getMonth () + adjust) % 12;
        var Year = date.getFullYear() + parseInt((date.getMonth() + adjust) / 12);
        if (Month < 0)
        {
            Month += 12;
            Year += -1;
        }
        var btn = $$('button');
        SetStyle(btn, '10px','bold', 'gray', '#d8e8ff');
        btn.style.padding = '0px';
        btn.onclick = function(){DeleteCalendar(); BuildCalendar(Year,Month);}
        btn.innerHTML = label;
        return btn;
    }
    
    var ExtractDate = function(theDate)
    {
        var sMonth = (theDate.getMonth()+1).toString();
        if (sMonth.length == 1) sMonth = '0' + sMonth;
        var sDay = theDate.getDate().toString();
        if (sDay.length == 1) sDay = '0' + sDay;
        return sMonth + '/' + sDay + '/' + theDate.getFullYear();
    }
    
    var checkDate = function(date)
    {
        var delim1 = date.indexOf('/');
        var delim2 = date.lastIndexOf('/');
        if (delim1 != -1 && delim2 != -1)
        {
            var month = parseInt(date.substring(0, delim1));
            var day = parseInt(date.substring((delim1 + 1), delim2));
            var year = parseInt(date.substring(delim2 + 1));
            if (year > 49 && year < 100) year += 1900;
            else if (year >= 0 && year < 50)year += 2000;
            if (!(isNaN(month) || isNaN(day) || isNaN(year))) 
                return ExtractDate(new Date(year,month-1,day));
        }
        return false;
    }

    this.BindTo = BindTo;
    this.Value = Value;
    this.disabled = disabled;
    
    var Table = function()
    {
        var This = this;
        var Table = $$('table');
        var Body = Table.appendChild($$('tbody'));
        
        var AddCell = function(row, width)
        {
            var cell = row.appendChild($$('td'));
            alert(arguments.length);
            if (arguments.length > 1){ alert(width); cell.style.width = width;}
            return cell;
        }
        
        Table.AddRow = function()
        {
            var Row = Body.appendChild($$('tr'));
            Row.AddCell = function(width)
            {
                var cell = this.appendChild($$('td'));
                if (arguments.length == 1){ cell.style.width = width;}
                return cell;
            }
            return Row;
        }
        return Table;
    }
}

Masada_300.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 Body = ThisTable.Body = Tbl.appendChild($$('tbody'));
    Body.style.display = 'none';
    
    var Visible = function(state)
    {
        if (state) Body.style.display = 'block';
        else Body.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';
    }
    
    this.Clear = function()
    {
        while (Body.hasChildNodes())
        {
            Body.removeChild(Body.firstChild);
        }
    }
    
    var BindTo = function(parent)
    {
        parent.appendChild(Tbl)
        return ThisTable;
    }
    
    var Row = function()
    {
        var ThisRow = this;
        this.Table = ThisTable;
        this.Element = Body.appendChild($$('tr'));
        var Cell = function(width,addDiv)
        {
            var ThisCell = this;
            this.Row = ThisRow;
            var td = $$('td');
            ThisRow.Element.appendChild(td);
            if (width) td.style.width = Masada_300.AppendPx(width);
            if (addDiv)
            {
                var Div = this.Element = $$('div');
                if (width) Div.style.width = Masada_300.AppendPx(width);
                td.appendChild(Div);
                Div.style.height = ThisRow.Element.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 = Masada_300.AppendPx(width);
            if (arguments.length > 1) AddDiv = addDiv;
            var cell = new Cell(Width,AddDiv);
            cell.Element.style.height = ThisRow.Element.style.height;
            return cell;
        }
        this.AddCell = AddCell;
    }
    
    var AddRow = function(height)
    {
        var row = new Row();
        if (arguments.length > 0) row.Element.style.height = Masada_300.AppendPx(height);
        return row;
    }
    this.BindTo = BindTo;
    this.AddRow = AddRow;    
    this.Position = Position;
    this.Visible = Visible;
}

Masada_300.AppendPx = function(style)
{
    if (typeof style == "number") return style + 'px';
    return style;
}

Masada_300.ExtractDate = function(theDate)
{
    var sMonth = (theDate.getMonth() + 1).toString();
    if (sMonth.length == 1) sMonth = '0' + sMonth;
    var sDay = theDate.getDate().toString();
    if (sDay.length == 1) sDay = '0' + sDay;
    if (arguments.length > 1 && arguments[1] == true) return sMonth + '/' + sDay;
    return sMonth + '/' + sDay + '/' + theDate.getFullYear();
}

Masada_300.CheckDate = function(date)
{
    var delim1 = date.indexOf('/');
    var delim2 = date.lastIndexOf('/');
    if (delim1 != -1 && delim2 != -1)
    {
        var month = parseInt(date.substring(0, delim1));
        var day = parseInt(date.substring((delim1 + 1), delim2));
        var year = parseInt(date.substring(delim2 + 1));
        if (year > 49 && year < 100) year += 1900;
        else if (year >= 0 && year < 50) year += 2000;
        if (!(isNaN(month) || isNaN(day) || isNaN(year)))
            return ExtractDate(new Date(year, month - 1, day));
    }
    return false;
}

Masada_300.Ddl = function()
{
    var This = this;
    var Element = null;
    var TextName = null;
    var ValueName = null;
    var Handler = null;
    var Arg = 0;
    var Length = arguments.length;
    var Width = arguments[0];
    var Height = arguments[1];

    Element = $$('select');
    Element.style.width = Width;
    Element.style.height = Height;

    this.Callback = function(handler)
    {
        Handler = handler;
        return This;
    }

    this.Add = function(text, value)
    {
        var Value = text;
        if (arguments.length > 1) Value = arguments[1];
        var o = $$("OPTION");
        o.text = text;
        o.value = Value;
        Element.options.add(o);
        return This;
    }

    this.Load = function(RecSet, textName)
    {
        var TextName = arguments[1];
        var ValueName = TextName;
        if (arguments.length > 2) ValueName = arguments[2];
        for (var i = 0; i < rtn.RowCount; i++)
        {
            This.Add(RecSet.GetValue(i, TextName), RecSet.GetValue(i, ValueName));
        }
        return This;
    }

    this.Index = function(idx)
    {
        if (arguments.length > 0)
            Element.selectedIndex = idx;
        else
            return Element.selectedIndex;
    }

    this.Style = function(name, value)
    {
        Lbl.style[name] = value;
        return This;
    }

    this.Selection = function(value)
    {
        if (arguments.length > 0)
        {
            var Options = Element.options;
            var Index = Element.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)
                {
                    Element.selectedIndex = i;
                    return;
                }
            }
            Element.selectedIndex = 0;
        }
        else
            return Element.options[Element.selectedIndex].value;
    }


    this.Clone = function()
    {
        return new Masada_300.Ddl(Width, Height);
    }

    this.BindTo = function(parent)
    {
        parent.appendChild(Element)
        return This;
    }
    this.Element = Element;
}

// String Manipulation prototypes
String.prototype.count = function(c)
{  
    return this.split(c).length-1;
}

String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.toBool = function()
{
    switch (this.toUpperCase())
    {
        case 'TRUE':
            return true;
        case 'FALSE':
            return false;
    }
}

String.prototype.checkDate = function()
{
    var delim1 = this.indexOf('/');
    var delim2 = this.lastIndexOf('/');
    if (delim1 != -1 && delim2 != -1)
    {
        var month = this.substring(0, delim1);
        var day = this.substring((delim1 + 1), delim2);
        var year = this.substring((delim2 + 1));
        if (!(isNaN(parseInt(month)) || isNaN(parseInt(day)) || isNaN(parseInt(year))))
        {
            return true;
        }
    }
    return false;
}

String.prototype.extractDate = function()
{
    if (this.length == 0) return "";
    var D = new Date(this);
    return (D.getMonth() + 1 )+ '/' + D.getDate() + '/' + D.getYear();
} 

// Copyright (c) 2009 Graphic Vision NeuroInformatics, Inc.
// Version 2.02

with(Masada_300)
{
    Masada_300.ImagePlayer = function(width, height)
    {
        var This = this;
        var Container = $$('div')
        This.Element = Container;
        var Fade = false;
        Container.style.overflow = 'hidden';
        Container.style.margin = '0';
        Container.style.border = '0';
        var SlideInterval = null;

        var Width = 0;
        var Height = 0;
        var Href = 'Disabled';

        if (arguments.length > 1)
        {
            Width = width;
            Height = height;
            if (arguments.length == 3 && arguments[2]) Href = 'Enabled';
        }
        else
        {
            var PlayerNode = arguments[0];
            Height = parseInt(PlayerNode.getAttribute('Height'));
            Width = parseInt(PlayerNode.getAttribute('Width'));
            Href = PlayerNode.getAttribute('Href');
            SetPlayList(PlayerNode);
        }

        var ImageDiv = Container.appendChild($$('div'));
        ImageDiv.style.overflow = 'hidden';
        ImageDiv.style.borderBottom = '0px';

        var TitleTbx = null;
        this.ShowMarquee = function()
        {
            TitleTbx = $$('input');
            TitleTbx.type = 'text';
            TitleTbx.style.width = '100%';
            TitleTbx.style.color = 'black';
            TitleTbx.style.fontSize = '8pt';
            //TitleTbx.style.fontWeight = 'bold';
            TitleTbx.style.backgroundColor = '#dddddd';
            Container.appendChild(TitleTbx);
            return This;
        }

        var ImageElement = function()
        {
            this.ImageName = null;
            this.Title = null;
            this.Image = null;
        }

        var ImagePanel = function()
        {
            var Frames = [];
            Frames.length = 0;
            var ImageNbr = 0;
            var ViewMs = 7000;

            var ImageAnchor = $$('a');
            var DisplayImage = new Image();
            DisplayImage.width = Width;
            DisplayImage.height = Height;
            DisplayImage.style.margin = '0px';
            DisplayImage.style.backgroundColor = '#aaaaaa';
            ImageAnchor.appendChild(DisplayImage);
            if (Href && Href == 'Enabled')
            {
                ImageAnchor.onmouseover = function() { this.style.cursor = 'hand'; }
                ImageAnchor.onmouseout = function() { this.style.cursor = 'pointer'; }
            }
            this.Reset = function(resource, title)
            {
                ImageNbr = 0;
                Frames.length = 0;
            }

            var Add = function(resourceId, viewSeconds, title)
            {
                var NewFrame = new ImageElement();
                NewFrame.Image = new Image(Width, Height);
                NewFrame.ViewSeconds = viewSeconds;
                if (arguments.length > 2 && title.length > 0) NewFrame.Title = ' ' + title;
                NewFrame.Image.src = resourceId;
                Frames.push(NewFrame);
                return NewFrame;
            }

            var Run = function()
            {
                if (Frames.length < 1) return;
                ImageNbr = 0;
                //SlideInterval = setInterval(Next, ViewMs);
                Show();
                return This;
            }

            var Start = function()
            {
                if (Frames.length < 1) return;
                Next();
                //SlideInterval = setInterval(Next, ViewMs);
            }

            var Stop = function()
            {
                clearInterval(SlideInterval);
            }

            var Next = function()
            {
                if (Frames.length < 1) return;
                ImageNbr++;
                if (ImageNbr >= Frames.length) ImageNbr = 0;
                Show();
            }

            var Previous = function()
            {
                if (Frames.length < 1) return;
                ImageNbr--;
                if (ImageNbr < 0) ImageNbr = Frames.length - 1;
                Show();
            }

            var OpacityTimer = null;
            var Show = function()
            {
                if (Fade)
                {
                    DisplayImage.style.opacity = 0;
                    DisplayImage.style.filter = 'alpha(opacity=0)';
                }
                clearInterval(SlideInterval);
                var Frame = Frames[ImageNbr];
                ViewMs = Frame.ViewSeconds * 1000;
                if (Frame.Title)
                {
                    DisplayImage.alt = Frame.Title;
                    if (TitleTbx) TitleTbx.value = Frame.Title;
                }
                DisplayImage.src = Frame.Image.src;
                var DoIt = function(link)
                {
                    window.open(link);
                }
                if (Frame.Link) ImageAnchor.onclick = function() { DoIt(Frame.Link) };
                SlideInterval = setInterval(Next, ViewMs);
                if (Fade) OpacityTimer = setInterval(Transition, 50);
            }
            var Counter = 0;
            var Transition = function()
            {
                Counter += 2;
                if (Counter > 100)
                {
                    clearInterval(OpacityTimer);
                    Counter = 0;
                    return;
                }
                DisplayImage.style.opacity = .01 * Counter;
                DisplayImage.style.filter = 'alpha(opacity=' + Counter + ')';
            }

            this.BindTo = function(parent)
            {
                parent.appendChild(ImageAnchor);
            }

            this.Add = Add;
            this.Run = Run;
            this.Start = Start;
            this.Stop = Stop;
            this.Next = Next;
            this.Previous = Previous;
        }

        var CurrentImage = new ImagePanel();
        CurrentImage.BindTo(ImageDiv);

        var SetPlayList = function(imagesNode)
        {
            CurrentImage.Reset();
            var ImageNodes = imagesNode.childNodes;
            for (var i = 0; i < ImageNodes.length; i++)
            {
                var Item = ImageNodes[i];
                var Id = Item.getAttribute('Id');
                var ViewSeconds = 7;
                var ViewTime = Item.getAttribute('ViewTime');
                if (ViewTime) ViewSeconds = parseInt(ViewTime);
                var TitleNode = Item.childNodes[0];
                var Frame = CurrentImage.Add(Id, ViewSeconds)
                if (TitleNode) Frame.Title = ' ' + TitleNode.nodeValue;
                var Link = Item.getAttribute('Link');
                if (Link) Frame.Link = Link;
            }
            return This;
        }


        this.ShowControls = function()
        {
            var HdrDiv = Container.appendChild($$('div'));
            HdrDiv.style.width = '120px';
            HdrDiv.style.padding = '1px 60px 0';

            var HdrTbl = new Table();
            HdrTbl.BindTo(HdrDiv);
            HdrTbl.Visible(true);
            var HdrRow = HdrTbl.AddRow()


            var BackBtn = HdrRow.AddCell().Element.appendChild(new Button('<', 22, 22).Element);
            BackBtn.style.cursor = 'hand';
            BackBtn.onclick = function()
            {
                RunBtn.value = 'Start';
                clearInterval(SlideInterval);
                CurrentImage.Previous();
            }

            var RunBtn = HdrRow.AddCell().Element.appendChild(new Button('Stop', 45, 22).Element);
            RunBtn.style.cursor = 'hand';
            RunBtn.onclick = function()
            {
                if (this.value == 'Start')
                {
                    this.value = 'Stop';
                    CurrentImage.Start();
                }
                else
                {
                    this.value = 'Start';
                    CurrentImage.Stop();
                }
            }

            var FwdBtn = HdrRow.AddCell().Element.appendChild(new Button('>', 22, 22).Element);
            FwdBtn.style.cursor = 'hand';
            FwdBtn.onclick = function()
            {
                RunBtn.value = 'Start';
                clearInterval(SlideInterval);
                CurrentImage.Next();
            }
            return This;
        }

        this.BindTo = function(parent)
        {
            parent.appendChild(Container);
            return This;
        }

        this.Run = function()
        {
            CurrentImage.Run();
            return This;
        }

        this.SetPlayList = SetPlayList;

        CurrentImage.Run();
    }
}

    Masada_300.Textbox = function(label)
    {
        var Tbx = $$('input'); 
        Tbx.type = 'text';
        if (arguments.length > 1) Tbx.style.width = arguments[1] + 'px';
        if (arguments.length > 2) Tbx.style.height = arguments[2] + 'px';

        this.Style = function(name, value)
        {
            Tbx.style[name] = value;
            return This;
        }
        this.Element = Tbx;
    }
    
    Masada_300.Button = function(label)
    {
        var Btn = $$('input'); 
        Btn.type = 'button';
        Btn.value = label;
        if (arguments.length > 1) Btn.style.width = arguments[1] + 'px';
        if (arguments.length > 2) Btn.style.height = arguments[2] + 'px';

        this.Style = function(name, value)
        {
            Lbl.style[name] = value;
            return This;
        }
        this.Element = Btn;
    }
    
    Masada_300.Label = function(label, width, height)
    {
        var This = this;
        var Lbl = $$('Label'); 
        Lbl.innerHTML = label;
        if (arguments.length > 1) Lbl.style.width = arguments[1] + 'px';
        if (arguments.length > 2) Lbl.style.height = arguments[2] + 'px';
        
        this.Style = function(name, value)
        {
            Lbl.style[name] = value;
            return This;
        }
        this.Element = Lbl;
    }

    Masada_300.ContentManager = function(width, height)
    {
        var This = this;
        var Width = width + 'px';
        var Height = height + 'px';

        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 = $$('Div');
        Container.style.cssText = "";
        Container.style.width = Width;
        Container.style.height = Height;
        Container.style.textAlign = 'left';
        Container.style.position = 'relative';
        Container.style.overflow = 'hidden';
        Container.style.margin = '0px';
        Container.style.padding = '0px';
        //Container.style.zIndex = 1000;

        var InsertNew = function(id, overflow)
        {
            var div = $$('div');
            div.id = id;
            div.style.width = Width;
            div.style.height = Height;
            div.Properties = new Properties();
            if (arguments.length == 2) div.Properties.Overflow = overflow;
            if (Divs.length == 0)
            {
                CurrentDiv = div;
            }
            InitializeDiv(div);
            Divs.push(div);
            Container.appendChild(div);
            return div;
        }

        var Insert = function(panel)
        {
            var div = panel.Div;
            div.Properties = new Properties();
            if (Divs.length == 0)
            {
                CurrentDiv = div;
            }
            InitializeDiv(div);
            Divs.push(div);
            Container.appendChild(div);
            return panel;
        }

        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.overflow = div.Properties.Overflow;
            div.style.display = 'none';
        }


        var CurrentPanel = function(panel)
        {
            var div = panel.Div;
            //if (arguments.length == 0 && CurrentDiv) return CurrentDiv;
            //if (div.Properties.IsDisabled) return;
            div.style.display = 'block';

            if (CurrentDiv && div != CurrentDiv)
            {
                CurrentDiv.style.display = 'none';
                CurrentDiv = div;
            }
        }

        Container.style.visibility = 'visible';

        var BindTo = function(parent)
        {
            parent.appendChild(Container);
            return this;
        }

        this.BindTo = BindTo;
        this.Find = Find;
        this.Insert = Insert;
        this.InsertNew = InsertNew;
        this.CurrentPanel = CurrentPanel;
        this.Container = Container;
    }


