

DScript = 
{
    saveState : function(value, name)
    {
        if (theForm.getElementsByTagName('div')[0] && name)
        {
            if ($('DS__' + name))
            {
                if (value)
                    $('DS__' + name).value = value.toString();
            }
            else
            {
               statefield = new DScript.HTML.HTMLControls.TextBox();
               statefield.context.id = 'DS__' + name;
               statefield.context.visible = 0;
               statefield.create();
               theForm.getElementsByTagName('div')[0].appendChild(statefield.HTMLControl());
               
               if (value)
                    statefield.setValue(value.toString());
            }
        }
    },
    
    restoreState : function(name)
    {
        if (name && name.length > 0)
        {
            if ($('DS__' + name))
            {
                 return Json.evaluate($('DS__' + name).value);
            }
        }
     }
}

// namespace placeholder
DScript.HTML = {}
DScript.HTML.HTMLControls = {}

DScript.DScovery = 
{
    ///////////////////////////////
    //
    //  Define an overview
    //
    ///////////////////////////////
    Overview : new Class
    ({
            initialize: function(src)
            {
                this.context = src;
                this.context.searchstate = -1;
            },
            
            toString : function()
            {
                return Json.toString(this.datasource);
            }
    }),
    
    implement : function(properties)
    {
        Object.extend(DScript.DScovery,properties);
    }
}



DScript.HTML.HTMLControls = 
{
    // Base Control class
    Control : new Class
    ({ 
        initialize: function()
        {
            var context = 
            {
                'id':'',
                'classAttribute':'',
                'visible':1,
                'value':''
            }
            
            this.context = context;
        },
        
        HTMLControl : function()
        {
            if (!this.control)
            {
                if ( $(this.context.id) )
                {
                    this.control = $(this.context.id);
                }
                else if ( $(this.context.id.replace(/\$/g,'_') ) )
                {
                    this.control = $(this.context.id.replace(/\$/g,'_') );
                }
            }
            
            if (this.control)
            {
                if (this.control.style && this.control.style.display == "none") 
                {
                    this.context.visible = 0;
                }
                else
                {
                    this.context.visible = 1;
                }
                
            }
            
            return this.control;
        },
        
        Visible : function(vis)
        {
            if (!this.control) this.control = this.HTMLControl();
        
            if (this.control)
            {
                if (vis)
                {
                    this.control.style.display = "block";
                    this.context.visible = 1;
                }
                else
                {
                    this.control.style.display = "none";
                    this.context.visible = 0;
                }
            }
        },
        
        saveState : function(value)
        {
              if (value)
                DScript.saveState(value, this.context.id);
              else
                DScript.saveState(this, this.context.id);
        },
        
        restoreState : function(container)
        {
            if ($('DS__'+this.context.id))
            {
                if (!container)
                    this.context = DScript.restoreState(this.context.id);
                else
                    container.context = DScript.restoreState(this.context.id);
            }
        },
            
        toString : function()
        {
            return Json.toString(this.context);
        }
    }),
    
    
    implement : function(properties)
    {
        Object.extend(DScript.HTML.HTMLControls,properties);
    }
}

DScript.HTML.HTMLControls.Button = DScript.HTML.HTMLControls.Control.extend
({
    initialize: function()
    {
        this.parent(); //will call the previous initialize;
    },
    
    onButtonClick : function()
    {
        return this.HTMLControl().onclick;
    }
});

DScript.HTML.HTMLControls.Label = DScript.HTML.HTMLControls.Control.extend
({
    initialize: function()
    {
        this.parent(); //will call the previous initialize;
    },
        
    setValue : function(value)
    {
        this.context.value = value;
        this.HTMLControl().innerHTML = this.context.value;
    },
    
    getValue : function()
    {
        return this.context.value;
    }
});

DScript.HTML.HTMLControls.TextBox = DScript.HTML.HTMLControls.Control.extend
({
    initialize: function(id)
    {
        this.parent(); //will call the previous initialize;
        this.context.classAttribute = '';
        
        if (id)
        {
            this.context.id = id;
            
            if ($(id))
            {
                this.control = $(id);
                
                this.restoreState();
                this.HTMLControl().value = this.context.value;
            }
        }
    },
    
    create: function()
    {
        textbox = document.createElement('input');
        if (this.context.id.length > 0)
        {
            textbox.setAttribute('id',this.context.id);
            textbox.setAttribute('name',this.context.id);
        }
            
        if (this.context.classAttribute.length > 0)
            textbox.setAttribute('class',this.context.classAttribute);
            
        if (this.context.visible == 0)
            textbox.setAttribute('type','hidden');
            
        if (this.context.value)
            textbox.setAttribute('value',this.context.value);
        else
            
            textbox.setAttribute('value','');
            
        this.Control = textbox;
        this.control = textbox;
    },
    
    saveState : function()
    {
        this.context.value = this.HTMLControl().value;
        this.parent().saveState();
    },
        
    setValue : function(value)
    {
        if (this.HTMLControl())
        {
            this.context.value = value;
            this.HTMLControl().value = this.context.value;
        }
        else
        {
            console.error('No page instance found. %s' + '.create() must be called before a value can be set.',this.context.id);
        }
    },
    
    getValue : function()
    {
        return this.context.value;
    }
});


DScript.HTML.HTMLControls.HyperLink = DScript.HTML.HTMLControls.Button.extend
({
   initialize: function(id)
    {
        this.parent(); //will call the previous initialize;
        this.context.classAttribute = '';
        this.context.href = '#';
        
        if (id)
        {
            this.context.id = id;
            
            if ($(id))
            {
                this.control = $(id);
            }
        }
    },
    
    PostBackUrl:function(url)
    {
        this.context.href = url;
    },
    
    submit:function(eventTarget,eventArgument)
    {
         if (!theForm.onsubmit || (theForm.onsubmit() != false))
         {
            theForm.__EVENTTARGET.value = eventTarget;
            theForm.__EVENTARGUMENT.value = eventArgument;
            theForm.action = this.context.href;
            theForm.submit();
        }
    },
        
    create: function()
    {
        hlink = document.createElement('a');
        
        if (this.context.id.length > 0)
            hlink.setAttribute('id',this.context.id);
            
        if (this.context.classAttribute.length > 0)
            hlink.setAttribute('class',this.context.classAttribute);
        
        hlink.setAttribute('href',this.context.href);
        
        hlink.appendChild(document.createTextNode(this.context.value));
        
        this.control = hlink;
    }
});

DScript.DScovery.Navigation = {}

DScript.DScovery.Navigation.First = DScript.HTML.HTMLControls.HyperLink.extend
({
   initialize: function()
   {
        this.parent(); //will call the previous initialize;
        this.context.classAttribute = '';
        this.context.value = 'First';
        this.context.classAttribute = 'first';
        this.create();
   }
});

DScript.DScovery.Navigation.Next = DScript.HTML.HTMLControls.HyperLink.extend
({
   initialize: function()
   {
        this.parent(); //will call the previous initialize;
        this.context.classAttribute = '';
        this.context.value = 'Next';
        this.context.classAttribute = 'next';
        this.create();
   }
});

DScript.DScovery.Navigation.Last = DScript.HTML.HTMLControls.HyperLink.extend
({
   initialize: function()
   {
        this.parent(); //will call the previous initialize;
        this.context.classAttribute = '';
        this.context.value = 'Last';
        this.context.classAttribute = 'last';
        this.create();
   }
});

DScript.DScovery.Navigation.End = DScript.HTML.HTMLControls.HyperLink.extend
({
   initialize: function()
   {
        this.parent(); //will call the previous initialize;
        this.context.classAttribute = '';
        this.context.value = 'End';
        this.context.classAttribute = 'end';
        this.create();
   }
});


DScript.HTML.HTMLControls.DataControl = DScript.HTML.HTMLControls.Control.extend
({
    initialize: function()
    {
        this.parent(); //will call the previous initialize;
        
        this.context.DataSourceID = '';
        this.context.DataSource = '';
    },
        
    BindToXmlDataSource : function(datasource)
    {
        this.context.DataSourceID = datasource.context.id;
        this.context.DataSource = datasource.context.value;
    }
});

DScript.HTML.HTMLControls.Xml = DScript.HTML.HTMLControls.Control.extend
({
    initialize: function()
    {
        this.parent(); //will call the previous initialize;
        
        this.context.DataSource = '';
        this.context.TransformSource = '';
        this.context.DocumentSource = '';
    },
    
    Request: function(querystring)
    {
        this.myXHR = new XHR({method: 'get',async: true,onSuccess: this.onSuccess.bind(this), onFailure: this.onFailure.bindAsEventListener(this)});
        this.myXHR.send(this.context.DocumentSource, querystring);
    },
        
    onSuccess: function()
    {
        if (this.myXHR.transport.statusText == 'OK')
        {
            this.context.DataSource = this.myXHR.transport.responseText;
        }
    },
    
    onFailure: function()
    {
        if (DScript.DScovery.DScoveryManager.logging == 1)
            console.log(this.myXHR.transport.statusText);
    },
        
    BindToXmlDataSource : function(datasource)
    {
        this.context.DataSource = datasource;
    }
});

DScript.DScovery.Message =
{
     MessageBox : new Class
     ({
        initialize: function(control)
        {            
            if (control.HTMLControl() != null)
            {
                this.control = new DScript.HTML.HTMLControls.Control(); 
                this.control.control = new Element('label', {
                        'class' : 'message'
                });
            
                control.HTMLControl().adopt(this.control.control);
            }
        },
        
        write : function(message)
        {
            this.control.HTMLControl().innerHTML = message;
        }              
     }),
     
     Console : function(message)
     {
        if (DScript.DScovery.DScoveryManager.logging > 0)
        {
            try
            {
                console.log(message);
            }
            catch(err)
            {
                return;
            }
        }
     }
}

DScript.DScovery.DScoveryManager = 
{
    'id':'',
    'ayncmode':1,
    'datasource':'',
    'logging':0,
   
    DScoveryServer : new Class
    ({
        initialize: function()
        {
            var context = 
            {
                'id':'',
                'hidden':1,
                'address':''
            }
            
            this.context = context;
            
            if (typeof (WebForm_CallbackComplete) == "function") 
            {
                // set the original version with fixed version
                WebForm_CallbackComplete = DScript.DScovery.DScoveryManager.WebForm_CallbackComplete_SyncFixed;
            }
        },
        
        toString : function()
        {
            return Json.toString(this.context);
        }
    }),
    
    saveState : function()
    {
        if ($('__DSCOVERYDATA'))
            $('__DSCOVERYDATA').value = Json.toString(DScript.DScovery.DataGroups.context);
        
        DScript.saveState(DScript.DScovery.DScoveryManager,'SystemStateBag')
    },
    
    WebForm_CallbackComplete_SyncFixed : function() 
    {
        // SyncFix: the original version uses "i" as global thereby resulting in javascript errors when "i" is used elsewhere in consuming pages
        for (var i = 0; i < __pendingCallbacks.length; i++) 
        {
            callbackObject = __pendingCallbacks[ i ];
            if (callbackObject && callbackObject.xmlRequest && (callbackObject.xmlRequest.readyState == 4)) 
            {
                // the callback should be executed after releasing all resources
                // associated with this request.
                // Originally if the callback gets executed here and the callback
                // routine makes another ASP.NET ajax request then the pending slots and
                // pending callbacks array gets messed up since the slot is not released
                // before the next ASP.NET request comes.
                // FIX: This statement has been moved below
                // WebForm_ExecuteCallback(callbackObject);
                if (!__pendingCallbacks[ i ].async) 
                {
                    __synchronousCallBackIndex = -1;
                }
                __pendingCallbacks[i] = null;

                var callbackFrameID = "__CALLBACKFRAME" + i;
                var xmlRequestFrame = document.getElementById(callbackFrameID);
                
                if (xmlRequestFrame) 
                {
                    xmlRequestFrame.parentNode.removeChild(xmlRequestFrame);
                }

                // SyncFix: the following statement has been moved down from above;
                WebForm_ExecuteCallback(callbackObject);
           }
       }
   },
   
   toString : function()
   {
        context = "";
        context.id = DScript.DScovery.DScoveryManager.id;
        context.asynch = DScript.DScovery.DScoveryManager.ayncmode;
        context.logging = DScript.DScovery.DScoveryManager.logging;
        
        return Json.toString(context);
   },
    
    implement : function(properties)
    {
        Object.extend(DScript.DScovery.DScoveryManager,properties);
    }
}

DScript.DScovery.DScoverySearch =
{
    
}
