    function set_mode (x) {
        if (document.main) {
            document.main.mode.value = x;
        }
    }
    function set_action (x) {
        document.main.action.value = x;
    }
    function set_device (x) {
        document.main.device.value = x;
    }

    function set_page (x) {
        document.main.page.value = x;
    }
    function set_closeme () {
        document.main.closeme.value = 1;
    }

    function set_popup () {
        document.main.popup.value = 1;
    }
    function set_returnme() {
        document.main.returnme.value = 1;
    }
    function set_return_param(x) {
        document.main.return_param.value = x;
    }
    function set_return_url(x) {
        document.main.return_url.value = x;
    }
    function set_return_handler(x) {
        document.main.return_handler.value = x;
    }
    function set_clear_my_url(x) {
        document.main.clear_my_url.value = x;
    }
    function set_objpath(x) {
        document.main.objpath.value = x;
    }
    
    function not_null(x) {
        if (document.main.elements[x].value != '' ) {
            return true;
        }
        else {
           return false;
        }
    }

/*
    // this function has been replaced by the sticky parameters mechanism in html_wrapper
    // good design work sandeep!
    function set_cycle_params(x,y) {
        //the parameter to be sent back in formpost return 
        var node = document.createElement("input");
        node.setAttribute("name", "cycle_params");
        node.setAttribute("id", "cycle_params");
        node.setAttribute("value", x );
        node.setAttribute("type", "hidden");
        document.main.appendChild(node);
       
        // and it's value as hidden fields which unless overwritten by server, will be returned 

        var vnode = document.createElement("input");
        node.setAttribute("name", x);
        node.setAttribute("id", x);
        node.setAttribute("value", y );
        node.setAttribute("type", "hidden");
        document.main.appendChild(vnode);

    }

*/

    t = null;
    function closeme(x){
        t = setTimeout("self.close()",x);
    }

    function resizeme(x) {
        t = setTimeout("resize_window()", x);
    }

    function reloadme(x) {
        t = setTimeout("window.location.reload()", x);
    }

    function close_n_reload() {
        closeme(10);
        window.opener.location.reload();
    }

    function resize_window() {
        height=screen.availHeight - (screen.availHeight)/4;
        width =screen.availWidth - (screen.availWidth)/4;
        window.resizeTo(width,height);
        window.moveTo((screen.availWidth/2)-(width/2),(screen.availHeight/2)-(height/2));
        self.focus();
    }


    function popwindow (url,name,tab,wd,ht)
    {
        var params="menubar=no, location=no, status=no, directories=no, toolbar=no, scrollbars=yes, resizable=yes, alwaysraised=yes, left=10, top=10";
         if (wd) {
               params = params + ', width=' + wd;
         }
         if (ht) {
               params = params + ', height=' + ht;
         }
         var w = window.open(url, name, params);
    }

// this should be in the pagetools file or something similar, not a windowing issue, but here as a placeholder

     function copy_name_from_path(x,y) {
        var s = x.value;
        var i = s.lastIndexOf("\\");
        if (!(i > 0)) {
            i = s.lastIndexOf("index.html"); 
        }
        if (i > 0) {
            y.value = s.substr(i+1);
        }
    }

function clear_url(url) {
    
     var x = url;
     var pattern = new RegExp("[?].*");
     x = x.replace(pattern, "");
     return x;
}

function set_url_param(url, param, value) {

     var x = url;
     var value_array = value.split(","); //allow csv lists

     var pattern1 = new RegExp(param+"=[^&]*&", "gi");
     var pattern2 = new RegExp("&"+param+"=[^&]*", "gi");
     var pattern3 = new RegExp(param+"=[^&]*$");
     x = x.replace(pattern1, "");
     x = x.replace(pattern2, "");
     x = x.replace(pattern3, "");
     pattern = new RegExp ("[?]");
     var result = x.match(pattern);
     if (result != null) {
         x = x + '&' + param + '=' + value_array[0];
     }
     else
     {
         x = x + '?' + param + '=' + value_array[0];
     }
     value_array.shift(); //we are done with first value
     for (var n = 0; n < value_array.length; n++) {
         x = x + '&' + param + '=' + value_array[n];
     }
     return x;
}


function quietly_invoke_url(x) {
    var newScript=document.createElement('script');
    newScript.src=x;
    newScript.type="text/javascript";
    document.body.appendChild(newScript);
    newScript.parentNode.removeChild(newScript);
}

/* this function is used in List forms to handle selection state
   the items selected are remembered in a well known hidden input field
   named 'set_selection', as a CSV which is maintained by this function
   Additionally, the rows in the list form are hilighted to indicate selection
     
       multiple: whether to do multiple selection (true), or not (false)
       v: value (id) of selected object
       row: 
       count:
*/

function update_selection (task,value,display_value,count,row,table) {
    var color_object = function (x, color) {
                        x.bgColor = color;
                    };
    if (!task) { 
        task = 'select';
    }
    var sel_field = document.getElementById('set_selection');
    var display_field = document.getElementById('set_display_selection');
    if (!sel_field) {
        sel_field = document.createElement("input");
        sel_field.setAttribute("type", "hidden");
        sel_field.setAttribute("name", "set_selection");
        sel_field.setAttribute("id", "set_selection");
        document.forms[0].appendChild(sel_field);
    }
    if (!display_field) {
        display_field = document.createElement("input");
        display_field.setAttribute("type", "hidden");
        display_field.setAttribute("name", "set_display_selection");
        display_field.setAttribute("id", "set_display_selection");
        document.forms[0].appendChild(display_field);
    }

    /* remove all selection feedback if multiple is false */
    /* important assumption, supplied row is a TR and it's parent is the TABLE */
    if (task != 'multiselect') {
        var re = new RegExp("^list_row");
        var local_toggle = 1;
        for ( var r=0; r<table.rows.length; r++ ) {
             if (!table.rows[r].id || !table.rows[r].id.match(re)) {
                 continue;
             }

             if (local_toggle == 0) {
                table.rows[r].bgColor="#f1f1f1";
                table.rows[r].onmouseout=color_object(table.rows[r], "#f1f1f1");
                local_toggle = 1;
             } else {
                table.rows[r].bgColor="#fdfdfd";
                table.rows[r].onmouseout=color_object(table.rows[r], "#fdfdfd");
                local_toggle = 0;
             }
        }
    }

    /* handle the selection feedback on this row */
    str=","+sel_field.value + ",";
    display_str = ","+display_field.value+",";

    var re=new RegExp(","+value+",");
    var display_re = new RegExp("," + display_value + ",");

/*
    if (str.match(re)) {
            str=str.replace(re, ",");
            display_str = display_str.replace(display_re, ",");

            if (count == 0) {
                row.bgColor="#f1f1f1";
                row.onmouseout=color_object(row, "#f1f1f1");
            } else {
               row.bgColor="#fdfdfd";
                row.onmouseout=color_object(row, "#fdfdfd");
            }
    } else {
*/
           str+=value+",";
           display_str+=display_value+",";
           row.bgColor="yellow";
           row.onmouseout=function() {row.bgColor="yellow"};
/*
    }
*/

    str=str.replace(/,*$/,"");
    str=str.replace(/^,*/,"");
    display_str=display_str.replace(/,*$/,"");
    display_str=display_str.replace(/^,*/,"");

    sel_field.value=str;
    display_field.value=display_str;
}


// some useful and commonly used return handlers

/*
function fck_image_dialog_return(x) {
    window.opener.SetUrl( 'cache/'+x ) ;
    window.close();
}
*/


/* this is the standard form of the select handler used by list forms when acting as object pickers
   this generic handler simply updates the innerHTML and value of field f in the calling window, 
   using values of the fields named x and y in the list form
   if you wish to construct a custom select handler, please be sure to use this function signature 
   and overall semantic

       function my_select_handler (f, x, y )
       { f : fieldname to receive returned value
         x : valuecolumn
         y : namecolumn
       }

*/
   
function object_select_return(field) {
        var sel_field = document.getElementById('set_selection');
        var display_field = document.getElementById('set_display_selection');
        window.opener.document.getElementById(field).value = sel_field.value;

        window.opener.document.getElementById('display_'+field).innerHTML = display_field.value;

        window.opener.document.getElementById(field).onchange();
        //window.close();
}


/* this function merely applies the last selection to the given field, 
   useful for edit scenarios where one field is being edited and can hold one value only
*/

function image_select_return(field) {
    var sel_field = document.getElementById('set_selection');
    var display_field = document.getElementById('set_display_selection');

    var w = window.parent.dojo.widget.manager.getWidgetById(field);
    
    var image_list = display_field.value.split(",");
    for (var i=0; i<image_list.length; i++) {
        /* TODO check the existing filename and figure out what prefix to use (thumbnail, picker ..) */
        w.update_editable('cache/picker_small_'+image_list[i]);
    }
    var value_list = sel_field.value.split(",");
    for (var i=0; i<value_list.length; i++) {
        w.value = value_list[i];
    }
    w.saveEdit();
}

function image_add_return(cname) {
    
    var layout = window.parent.dojo.widget.manager.getWidgetById(cname);
    var field = layout.edit_widget.widgetId;
    image_select_return(field);
    layout.add_to_layout();
}

function qm_marquee(id, height, width, delta, interval, direction) {
    height = height || 200;
    width = width || 100;
    delta = delta || 3; //pixels
    interval = interval || 100; //milliseconds
    direction = direction || 'up';
    var node = document.getElementById(id);
    var content = node.innerHTML;
    node.parentNode.removeChild(node);
    var marquee = new xbMarquee('marquee', height, width, delta, interval, direction, 'scroll', content);
    marquee.onmouseover = function() { marquee.stop();};
    marquee.onmouseout = function() { marquee.start(); };
    startmarquee = function() {marquee.start();};
    document.addOnLoadFunction(startmarquee);
}

