    
    function element (id)
    {
        return document.getElementById(id);
    }

    function confirm_redirect (message, url)
    {
        if (confirm(message)) {document.location=url};
    };

    function confirm_redirect_multi (message, form)
    {
        if (confirm(message))
        {
            form_add_hidden (form,'multi_delete','1');
            document.forms[form].submit();
        };
    };

    function keyboard_driver (keyboard_form)
    {
        var keyboard_value = '';
        eval ('keyboard_value = document.' + keyboard_form + '.keyboard_select.value');
        if (keyboard_value == 1)
        {
            //document.write (event.keycode);
            if (event.keyCode==97)  event.keyCode=4304;
            if (event.keyCode==98)  event.keyCode=4305;
            if (event.keyCode==103) event.keyCode=4306;
            if (event.keyCode==100) event.keyCode=4307;
            if (event.keyCode==101) event.keyCode=4308;
            if (event.keyCode==118) event.keyCode=4309;
            if (event.keyCode==122) event.keyCode=4310;
            if (event.keyCode==84)  event.keyCode=4311;
            if (event.keyCode==105) event.keyCode=4312;
            if (event.keyCode==107) event.keyCode=4313;
            if (event.keyCode==108) event.keyCode=4314;
            if (event.keyCode==109) event.keyCode=4315;
            if (event.keyCode==110) event.keyCode=4316;
            if (event.keyCode==111) event.keyCode=4317;
            if (event.keyCode==112) event.keyCode=4318;
            if (event.keyCode==74)  event.keyCode=4319;
            if (event.keyCode==114) event.keyCode=4320;
            if (event.keyCode==115) event.keyCode=4321;
            if (event.keyCode==116) event.keyCode=4322;
            if (event.keyCode==117) event.keyCode=4323;
            if (event.keyCode==102) event.keyCode=4324;
            if (event.keyCode==113) event.keyCode=4325;
            if (event.keyCode==82)  event.keyCode=4326;
            if (event.keyCode==121) event.keyCode=4327;
            if (event.keyCode==83)  event.keyCode=4328;
            if (event.keyCode==67)  event.keyCode=4329;
            if (event.keyCode==99)  event.keyCode=4330;
            if (event.keyCode==90)  event.keyCode=4331;
            if (event.keyCode==119) event.keyCode=4332;
            if (event.keyCode==87)  event.keyCode=4333;
            if (event.keyCode==120) event.keyCode=4334;
            if (event.keyCode==106) event.keyCode=4335;
            if (event.keyCode==104) event.keyCode=4336;
        }
    }

    //FOR OLD VERSION SUPPORT
    function keyboard_driver_geo (keyboard_control_form, keyboard_control_name, keyboard_control_geo)
    {
        var keyboard_control_value = '';
        eval ('keyboard_control_value = document.' + keyboard_control_form + '.' + keyboard_control_name + '.value');
        if (keyboard_control_value == keyboard_control_geo)
        {
            //document.write (event.keycode);
            if (event.keyCode==97)  event.keyCode=4304;
            if (event.keyCode==98)  event.keyCode=4305;
            if (event.keyCode==103) event.keyCode=4306;
            if (event.keyCode==100) event.keyCode=4307;
            if (event.keyCode==101) event.keyCode=4308;
            if (event.keyCode==118) event.keyCode=4309;
            if (event.keyCode==122) event.keyCode=4310;
            if (event.keyCode==84)  event.keyCode=4311;
            if (event.keyCode==105) event.keyCode=4312;
            if (event.keyCode==107) event.keyCode=4313;
            if (event.keyCode==108) event.keyCode=4314;
            if (event.keyCode==109) event.keyCode=4315;
            if (event.keyCode==110) event.keyCode=4316;
            if (event.keyCode==111) event.keyCode=4317;
            if (event.keyCode==112) event.keyCode=4318;
            if (event.keyCode==74)  event.keyCode=4319;
            if (event.keyCode==114) event.keyCode=4320;
            if (event.keyCode==115) event.keyCode=4321;
            if (event.keyCode==116) event.keyCode=4322;
            if (event.keyCode==117) event.keyCode=4323;
            if (event.keyCode==102) event.keyCode=4324;
            if (event.keyCode==113) event.keyCode=4325;
            if (event.keyCode==82)  event.keyCode=4326;
            if (event.keyCode==121) event.keyCode=4327;
            if (event.keyCode==83)  event.keyCode=4328;
            if (event.keyCode==67)  event.keyCode=4329;
            if (event.keyCode==99)  event.keyCode=4330;
            if (event.keyCode==90)  event.keyCode=4331;
            if (event.keyCode==119) event.keyCode=4332;
            if (event.keyCode==87)  event.keyCode=4333;
            if (event.keyCode==120) event.keyCode=4334;
            if (event.keyCode==106) event.keyCode=4335;
            if (event.keyCode==104) event.keyCode=4336;
        }
    }

    function form_select_relation (select_form_name, select_parent_name, select_child_name, select_child_options)
    {
        eval ('var selected_id = document.' + select_form_name + '[\"' +select_parent_name + '\"].options.value');

        if (!selected_id) return;
        eval ('var select_child_options_current = ' + select_child_options + '[selected_id]');
        eval ('document.' + select_form_name + '[\"' + select_child_name + '\"].options.length = 0');
        for (i=0; i<select_child_options_current.length; i+=2)
        {
            eval ('document.' + select_form_name + '[\"' + select_child_name + '\"].options[i/2] = new Option (select_child_options_current[i], select_child_options_current[i+1])');
        }
    };

    function seconds (hours, minutes, seconds)
    {
        result = 0;
        if (hours) result = parseInt(hours) * 3600;
        if (minutes) result = parseInt(minutes) * 60 + result;
        if (seconds) result = parseInt(seconds) + result;
        if (!result) return 0;
        return result;
    }

    function text_to_seconds (text)
    {
        if (between(':',':',text))
        {
            return seconds (before(':',text),between(':',':',text),after_last(':',text));
        }
        return seconds (before(':',text),after_last(':',text));
    }

    function birthday_to_input (birthday, input)
    {
        if (birthday!='')
        {
            var day = parseInt(after_last('-',birthday));
            var month = parseInt(between('-','-',birthday));
            var year = parseInt(before('-',birthday));

            document.getElementById(input+'_day').value = day;
            document.getElementById(input+'_month').value = month;
            document.getElementById(input+'_year').value = year;
        }
    }
    
    function seconds_to_input (seconds, input)
    {
        text = seconds_to_text (seconds);
        var hours = before (':', text);
        var minutes = after (':', text);
        if (hours.length<2) hours = '0'+hours;
        if (minutes.length<2) minutes = '0'+minutes;
        document.getElementById(input).value = seconds;
        document.getElementById(input+'_hours').value = hours;
        document.getElementById(input+'_minutes').value = minutes;
    }

    function seconds_to_text (seconds)
    {
        if (!seconds) return '00:00';
        seconds = parseInt(seconds);
        if (!seconds) return '00:00';
        hours = parseInt (seconds/3600);
        minutes = parseInt((seconds - (hours*3600))/60);
        return hours + ':' + minutes;
    }

    function numbers (myfield, e, dec)
    {
        var key;
        var keychar;

        if (window.event)
           key = window.event.keyCode;
        else if (e)
           key = e.which;
        else
           return true;
        keychar = String.fromCharCode(key);

        // control keys
        if ((key==null) || (key==0) || (key==8) || 
            (key==9) || (key==13) || (key==27) )
           return true;

        // numbers
        else if ((("0123456789").indexOf(keychar) > -1))
           return true;

        // decimal point jump
        else if (dec && (keychar == "."))
           {
           //myfield.form.elements[dec].focus();
           return true;
           }
        else
           return false;
    }


//    menu_init = function()
//    {
//        try
//        {
//            if (document.all&&document.getElementById)
//            {
//                menu_root = document.getElementById('menu');
//                for (i=0; i<menu_root.childNodes.length; i++)
//                {
//                    node = menu_root.childNodes[i];
//                    if (node.nodeName=='LI')
//                    {
//                        node.onmouseover=function()
//                        {
//                            this.className+=' over';
//                        }
//                        node.onmouseout=function()
//                        {
//                            this.className=this.className.replace(' over', '');
//                        }
//                    }
//                }
//            }
//        }
//        catch (e)
//        {
//        }
//    }
//
//    window.onload = menu_init;
//
//    function selects_hide ()
//    {
//        for (j=0; j<document.forms.length; j++)
//        {
//            var theForm = document.forms[j]
//            for(i=0; i<theForm.elements.length; i++)
//            {
//                var alertText = ''
//                if(theForm.elements[i].type == 'select-one')
//                {
//                    theForm.elements[i].style.visibility = 'hidden';
//                }
//            }
//        }
//    }
//
//    function selects_unhide ()
//    {
//        for (j=0; j<document.forms.length; j++)
//        {
//            var theForm = document.forms[j]
//            for(i=0; i<theForm.elements.length; i++)
//            {
//                var alertText = ''
//                if(theForm.elements[i].type == 'select-one')
//                {
//                    theForm.elements[i].style.visibility = 'visible';
//                }
//            }
//        }
//    };

    function decimal_to_hex (number)
    {
        if (number < 0)
        {
            number = 0xFFFFFFFF + number + 1;
        }

        return number.toString(16).toLowerCase();
    }

    function rgb_to_hex (color)
    {
        if (color.substr(0, 1) === '#')
        {
            return color;
        }
        var red = parseInt(between('(',',',color));
        var green = parseInt(between(',',',',color));
        var blue = parseInt(between(',',')',color));
        return '#' + decimal_to_hex(red) + decimal_to_hex(green) + decimal_to_hex(blue);
    }

    function table_row_color (row, action, c_out, c_over, c_click)
    {
        var current = rgb_to_hex (row.style.backgroundColor);
        if (action == 'click')
        {
            if (current == c_click)
            {
                row.style.backgroundColor = c_out;

            }
            else
            {
                row.style.backgroundColor = c_click;
            }
        }
        else if (action == 'over' && current!=c_click)
        {
            row.style.backgroundColor = c_over;
        }
        else if (action == 'out' && current!=c_click)
        {
            row.style.backgroundColor = c_out;
        }
    }

//    var ajax_object;
//    //var ajax_div;
//    //var ajax_after;
//    try
//    {
//        ajax = new ActiveXObject("Msxml2.XMLHTTP");
//    }
//    catch (e)
//    {
//        try
//        {
//            ajax_object = new ActiveXObject("Microsoft.XMLHTTP");
//        }
//        catch(oc)
//        {
//            ajax_object = null;
//        }
//    }
//    if (!ajax_object && typeof XMLHttpRequest!="undefined")
//    {
//        ajax_object = new XMLHttpRequest();
//    };
//
//    function ajax_fetch ()
//    {
//        if (ajax_object.readyState==2)
//        {
//
//        }
//        else if (ajax_object.readyState==4)
//        {
//           //alert('state: ' + ajax.readyState + ' result: ' + ajax.responseText);
//            document.getElementById(ajax_div).innerHTML = ajax_object.responseText;
//            try
//            {
//                eval (ajax_div+'_after(ajax.responseText);');
//            }
//            catch (exception)
//            {
//
//            }
//        };
//
//        //alert ("div: " + ajax_div + "\nstate: " + ajax.readyState + "\nresult: " + ajax.responseText);
//    };

    function ajax_query (ajax_url, ajax_dest)
    {
        window[ajax_dest+'_ajax'] = new ajax (ajax_dest+'_ajax', ajax_url);
        window[ajax_dest+'_ajax'].element = ajax_dest;
        window[ajax_dest+'_ajax'].after = function ()
        {
            try
            {
                eval (ajax_div+'_after(window[\''+this.name+'\'].client.responseText);');
            }
            catch (exception)
            {

            }
        }
        window[ajax_dest+'_ajax'].fetch ();
//
//        ajax_div = ajax_dest;
//        if (ajax_object)
//        {
//          //alert (ajax_url);
//          ajax_url = encodeURI (ajax_url);
//          ajax_object.open ('GET', ajax_url, true);
//          ajax_object.onreadystatechange = ajax_fetch;
//          ajax_object.send (null);
//        }
    };

    function checkbox_checkall (sender)
    {
        for (var i = 0; i < sender.form.elements.length; i++)
        {
            if (sender.form.elements[i].type == 'checkbox')
            {
                sender.form.elements[i].checked = sender.form.cb_check_all.checked;
            }
        };
    };

    function form_add_hidden (form, name, value)
    {
        var element;
        element = document.createElement('input');
        element.setAttribute('type', 'hidden');
        element.setAttribute('name', name);
        element.setAttribute('id', document.forms[form].elements.length);
        element.setAttribute('value', value);
        document.forms[form].appendChild(element);
    };

    function div_show (div)
    {
       if (document.layers) document.layers[div].visibility = "show";
       else document.getElementById(div).style.visibility = "visible";
    };

    function div_hide (div)
    {
       if (document.layers) document.layers[div].visibility="hide";
       else document.getElementById(div).style.visibility="hidden";
    };

    ////////////////////////////////////////////////
    function move_div(id)
    {
        var div=document.getElementById(id);
        var newpos=parseInt(div.style.top)+2+'px';
        if (newpos<600)
        {  // Here you put the value it should move to
            setTimeout('moveDiv('+id+')',100) // Tweak the time to get a smooth movement.
        }
        else
        {
            //setTimeout('functionToHideDiv()',1000)
        }
    };

    function selected_index (sender)
    {
        return sender.options[sender.selectedIndex].value;
    };

    ////////////////////////////////////////////////////

    var hdms_browser_field;
    var hdms_browser_win;

    function _hdms_browser_window (field, url, type, sender)
    {
        hdms_browser_field = field;
        hdms_browser_win = sender;
        tinyMCE.activeEditor.windowManager.open
        (
            {
               url : 'file.htm',
               width : 320,
               height : 240
            },
            {
               custom_param : 1
            }
        );
        _return ("hm");
    };

    function _return (value)
    {
        tinyMCEPopup.getWindowArg('window').document.getElementById(tinyMCEPopup.getWindowArg('input')).value = value;
        tinyMCEPopup.close();
    };

    function hdms_browser_window (field_name, url, type, win)
    {

        // alert("Field_Name: " + field_name + "\nURL: " + url + "\nType: " + type + "\nWin: " + win); // debug/testing

        /* If you work with sessions in PHP and your client doesn't accept cookies you might need to carry
        the session name and session ID in the request string (can look like this: "?PHPSESSID=88p0n70s9dsknra96qhuk6etm5").
        These lines of code extract the necessary parameters and add them back to the filebrowser URL again. */

        var cmsURL = before_last('/',window.location.toString())+'/actions/browser.php';    // script URL - use an absolute path!
        if (cmsURL.indexOf("?") < 0)
        {
            //add the type as the only query parameter
            cmsURL = cmsURL + "?type=" + type;
        }
        else
        {
            //add the type as an additional query parameter
            // (PHP session ID is now included if there is one at all)
            cmsURL = cmsURL + "&type=" + type;
        }

        tinyMCE.activeEditor.windowManager.open
        (
            {
                file : cmsURL,
                title : 'File Browser',
                width : 640,  // Your dimensions may differ - toy around with them!
                height : 480,
                resizable : "yes",
                inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
                close_previous : "no"
            },
            {
                window : win,
                input : field_name
            }
        );

        return false;
  };

    function elements_disabled (div, state)
    {
        div = document.getElementById(div);
        for (var i = 0; i < div.childNodes.length; i++)
        {
            div.childNodes[i].disabled = state;
        };
    };

    function clock_run ()
    {
        theTime = window.setTimeout('clock_run()', 1000);
        //var clock_date = new Date();
        clock_date.setSeconds(clock_date.getSeconds()+1);
        var hrNow = clock_date.getHours();
        var mnNow = clock_date.getMinutes();
        var scNow = clock_date.getSeconds();
        var miNow = clock_date.getTime();
        var mon = clock_date.getMonth() + 1;
        var date = clock_date.getDate();
        var year = clock_date.getYear();
        var year2 = clock_date.getYear() - 2000;
        if(year2<=9)
        {
            year2 = '0' + year2;
        }
        var day = clock_date.getDay();

        if(hrNow == 0) {hour = 12;var ap = ' AM';}
        else if(hrNow <= 11) {ap = ' AM';hour = hrNow;}
        else if(hrNow == 12) {ap = ' PM';hour = 12;}
        else if (hrNow >= 13) {hour = (hrNow - 12);ap = ' PM';}
        if(hrNow >= 13) {hour = hrNow - 12;}
        if(mnNow <= 9) {min = '0' + mnNow;}
        else{min = mnNow}
        if (scNow <= 9) {secs = '0' + scNow;}
        else {secs = scNow;}

        var zday=new Array(7)
        if (day==0) {zday='Sunday'};
        if (day==1) {zday='Monday'};
        if (day==2) {zday='Tuesday'};
        if (day==3) {zday='Wednesday'};
        if (day==4) {zday='Thursday'};
        if (day==5) {zday='Friday'};
        if (day==6) {zday='Saturday'};

        var zmon = new Array(12)
        if (mon==1) {zmon='January'};
        if (mon==2) {zmon='February'};
        if (mon==3) {zmon='March'};
        if (mon==4) {zmon='April'};
        if (mon==5) {zmon='May'};
        if (mon==6) {zmon='June'};
        if (mon==7) {zmon='July'};
        if (mon==8) {zmon='August'};
        if (mon==9) {zmon='September'};
        if (mon==10) {zmon='October'};
        if (mon==11) {zmon='November'};
        if (mon==12) {zmon='December'};

        clock_div = document.getElementById('clock_div');
        if (clock_div)
        {
            clock_div.innerHTML = ''+zmon+' '+date+' '+hour+':'+min+':'+secs+ap+'';
        };
    }

    function factorial(n)
    {
      if (n==0) return 0;
      if (n==1) return 1;
      //console.log ('---------------');
      //console.log ('n:'+n);
      var result = 1;
      for (i=1; i<=n; i=i+1)
      {
          result = result * i;
          //console.log ('i:'+i+ ' result:' + result);
      };
      return result;
    }


//    function options_query (options_url, options_select)
//    {
//        var options_source = null;
//        try
//        {
//            options_source = new XMLHttpRequest();
//        }
//        catch (ex)
//        {
//           try {
//                options_source = new ActiveXObject('Msxml2.XMLHTTP');
//            }
//            catch (ex) {
//                options_source = new ActiveXObject('Microsoft.XMLHTTP');
//            }
//        }
//        if (options_source == null) {
//            window.alert('AJAX is not available in your browser');
//            return;
//        }
//        options_source.open('GET', options_url, false);
//        options_source.send();
//        var options_plain = options_source.responseText;
//        return options_plain;
//    }

    var block_display = 'table-row';

    function options_toxml (options_plain)
    {
        var options_xml = null;
        try
        {
            options_xml = new ActiveXObject('Microsoft.XMLDOM');
            options_xml.async = 'false';
            options_xml.loadXML(options_plain);
        }
        catch (ex)
        {
            try
            {
                var domParser = new DOMParser();
                options_xml = domParser.parseFromString(options_plain, 'text/xml');
            }
            catch (ex)
            {
                alert(ex.message);
            }
        }
        return options_xml;
    }

    function options_bind (options_url, options_select)
    {
        window[options_select+'_ajax'] = new ajax (options_select+'_ajax', options_url);
        window[options_select+'_ajax'].after = function ()
        {
            options_apply (before_last ('_',this.name));
        };
        if (window[options_select+'_ajax_final'] && typeof window[options_select+'_ajax_final']=="function")
        {
            window[options_select+'_ajax'].final = window[options_select+'_ajax_final'];
        }
        window[options_select+'_ajax'].fetch ();
    }

    function options_apply (options_select)
    {
        var options_select_block = options_select+'_block';
        if (options_select!='' || block_display==null)
        {
            var options_xml = options_toxml(window[options_select+'_ajax'].client.responseText);
            var options_items = options_xml.getElementsByTagName('options');
            if (options_items.length <= 0)
            {
                //if no records fetched we are there
                if (document.getElementById(options_select_block)!=null)
                {
                    //if element exists with id of value options_select_block
                    //we hide it. this element with that id must contain this select input
                    //the value of options_select_block is subject of hiding contentless selectbox
                    document.getElementById(options_select_block).style.display = 'none';
                    document.getElementById(options_select).options.length = 0;
                }
                return;
            }
            options_select = document.getElementById(options_select);
            options_select.options.length = 0;
            for (var i = 0; i < options_items[0].childNodes.length; i++)
            {
                var options_caption = '';
                var options_name = '';
                var x;
                var y;
                // GET THE DISPLAY TEXT
                x = options_xml.getElementsByTagName('caption')[i]
                if (x)
                {
                    y = x.childNodes[0];
                    options_caption = y.nodeValue;
                    // GET THE VALUE
                    x = options_xml.getElementsByTagName('name')[i]
                    y = x.childNodes[0];
                    options_name = y.nodeValue;
                    // CREATE THE OPTION TO ADD TO THE SELECT BOX
                    var option_new = window.document.createElement('OPTION');
                    option_new.text = options_caption;
                    option_new.value = options_name;
                    options_select.options.add(option_new);
                 }
            }
            if (block_display!=null && document.getElementById(options_select_block)!=null)
            {
                //block display stores the method for displayng
                //hidden element which contains hidden select
                //where i modify it i have no idea already
                document.getElementById(options_select_block).style.display = block_display;
            }
         }
    }

    function options_count (options_select)
    {
        return document.getElementById(options_select).options.length;
    }

    function options_empty (options_select, hide)
    {
        if (hide && document.getElementById(options_select+'_block')!=null) document.getElementById(options_select+'_block').style.display = 'none';
        document.getElementById(options_select).options.length = 0;
        options_add (options_select, '0', '...');
    }

    function options_add (options_select, options_name, options_caption)
    {
        options_select = document.getElementById(options_select);
        var options_new = window.document.createElement('OPTION');
        options_new.text = options_caption;
        options_new.value = options_name;
        options_select.options.add(options_new);
    }

    function scrollto (id)
    {
        if ((obj = document.getElementById(id)) && obj != null)
        {
          window.scrollTo (0, obj.offsetTop);
        }
    }

    function collapse (div_id, img_id, img_src_show, img_src_hide)
    {
        if(document.getElementById(div_id).style.display=='none')
        {
            document.getElementById(div_id).style.display='block';
            document.getElementById(img_id).src=img_src_hide;
        }
        else
        {
            document.getElementById(div_id).style.display='none';
            document.getElementById(img_id).src=img_src_show;
        }
    }

    function ajax (name, url, element)
    {
        if (!name)
        {
            console.debug ('Object name required');
            return;
        }
        this.name = name;
        this.url = url;
        this.element = element;
        this.client = null;
        this.asynch = true;
        this.query = '';
        window[this.name] = this;
        this.object = function ()
        {
            try
            {
                this.client = new XMLHttpRequest();
            }
            catch (ex)
            {
               try
               {
                   this.client = new ActiveXObject('Msxml2.XMLHTTP');
               }
               catch (ex)
               {
                   this.client = new ActiveXObject('Microsoft.XMLHTTP');
               }
            }
            if (this.client == null)
            {
                console.error ('XMLHTTP object cant be created');
                return;
            }
            this.client.parent = this.name;
        }

        this.attach = function ()
        {
            this.client.onreadystatechange = function ()
            {
                if (this.readyState==3)
                {
                    window[this.parent].loading ();
                }
                else if (this.readyState==4)
                {
                    window[this.parent].ready ();
                    window[this.parent].after ();
                    window[this.parent].final ();
                }
            };
        }

        this.ready = function ()
        {
            if (this.element && document.getElementById(this.element))
            {
                document.getElementById(this.element).innerHTML = this.client.responseText;
            }
        }

        this.after = function ()
        {

        }

        this.before = function ()
        {
        }

        this.final = function ()
        {

        }

        this.loading = function ()
        {
        }

        this.fetch = function ()
        {
            if (this.client && this.url)
            {
                this.before ();
                this.client.open ('GET', this.url+this.query, this.asynch);
                this.client.send ();
            }
        }

        this.timer = function (seconds, preload)
        {
            if (preload)
            {
                this.fetch ();
            }
            setInterval ('window[\''+this.name+'\'].fetch()',seconds*1000)
        }

        this.object ();
        this.attach ();

        if (this.url && this.element)
        {
            this.fetch ();
        }

    }

    config = new Object();
    config.widgets = '';
    config.page = '';

    if (!Array.indexOf)
    {
        Array.prototype.indexOf = function (obj)
      {
        for (var i = 0; i < this.length; i++)
        {
          if (this[i] == obj) {
            return i;
          };
        };
        return -1;
      };
    }

    function param (name, value)
    {
        if (!value)
        {
            value = name;
        }
        this.name = name;
        this.value = value;
        this.query = function ()
        {
            return '&'+this.name+'='+encodeURI(this.get());
        }
        this.get = function ()
        {
            if (typeof this.value=='object')
            {
                return this.value.value;
            }
            if (typeof this.value=='string' && document.getElementById(this.value)!=null)
            {
                return document.getElementById (this.value).value;
            }
            if (typeof this.value=='string' && document.getElementsByName(this.value).length>0)
            {
                return document.getElementsByName(this.value)[0].value;
            }
            else
            {
                return this.value;
            }
        }
    }

    function widget (name, id, element, form, load)
    {
        if (!name)
        {
            console.debug ('Object initialization requires name');
        }
        if (!id)
        {
            console.debug ('Object initialization requires id');
        }
        this.name = name;
        window[this.name + '_widget'] = this;
        this.id = id;
        this.client = new ajax (this.name + '_client', config.widgets);
        this.element = element;
        this.client.element = element;
        this.form = form;
        this.params = new Array;
        this.process = function (interval, preload)
        {
            if (typeof interval=='number')
            {
               if (preload==null)
               {
                   preload = true;
               }
               this.client.timer (interval, preload);
            }
            else
            {
                this.client.fetch ();
            }
        }
        this.prepare = function ()
        {
            this.client.query = '?apage='+config.page+'&awidget='+this.id;
            this.client.element = this.element;
            for (var i = 0; i < this.params.length; i++)
            {
                this.client.query += this.params[i].query();
            }
            if (this.form)
            {
                container = document.getElementById(this.form);
                if (!container)
                {
                    container = document.getElementsByName(this.form);
                    if (container && container.length>0)
                    {
                        container = container[0];
                    }
                }
                if (container)
                {
                    elements = container.elements;
                }
                if (elements)
                {
                    for (var i = 0; i < form.length; i++)
                    {
                        if (elements[i].name)
                        {
                            this.client.query = '&' + elements[i].name + '=' + encodeURI(elements[i].value);
                        }
                    }
                }
            }
        }
        this.before = function ()
        {
        }
        this.after = function ()
        {
        }
        this.final = function ()
        {
        }
        this.values = function ()
        {
        }
        this.define = function (name, value)
        {
            this.params.push (new param(name,value));
        }
        this.assign = function (name, value)
        {
            if (!value)
            {
                value = name;
            };
            for (var i = 0; i < this.params.length; i++)
            {
                if (this.params[i].name==name)
                {
                    this.params[i].value = value;
                    return;
                }
            }
            this.define (name, value);
        }
        this.client.before = function ()
        {
            window[before_last('_client',this.name)+'_widget'].values ();
            window[before_last('_client',this.name)+'_widget'].prepare ();
            window[before_last('_client',this.name)+'_widget'].before ();
        }
        this.client.after = function ()
        {
            window[before_last('_client',this.name)+'_widget'].after ();
        }
        this.client.final = function ()
        {
            window[before_last('_client',this.name)+'_widget'].final ();
        }
        if (load)
        {
            this.process (load ,true);
        }
    }





