 
// general utility for browsing a named array or object
function xshow(o) {
    s = '';
    
    for(var e in o) 
    { 
    
        
        if ( o.hasOwnProperty(e) ) {
           s += e+'='+o[e]+'\n'; 
        }
    
    }
    
    alert( s );
}

/**
* Writes a dynamically generated list
* @param string The parameters to insert into the <select> tag
* @param array A javascript array of list options in the form [key,value,text]
* @param string The key to display for the initial state of the list
* @param string The original key that was selected
* @param string The original item value that was selected
*/ 
function writeDynaList( selectParams, source, key, orig_key, orig_val ) {
    var html = '\n    <select ' + selectParams + '>';
    var i = 0;
    for ( var x in source) {
        
      if ( source.hasOwnProperty(x) ) {
      
        if (source[x][0] == key) {
            var selected = '';
            if ((orig_key == key && orig_val == source[x][1]) || (i == 0 && orig_key != key)) {
                selected = 'selected="selected"';
            }
            html += '\n        <option value="'+source[x][1]+'" '+selected+'>'+source[x][2]+'</option>';
        } 
      
        i++;
       }
    }
    html += '\n    </select>';

    document.writeln( html );
}

/**
* Changes a dynamically generated list
* @param string The name of the list to change
* @param array A javascript array of list options in the form [key,value,text]
* @param string The key to display
* @param string The original key that was selected
* @param string The original item value that was selected
*/
function changeDynaList( listname, source, key, orig_key, orig_val ) {
    var list = eval( 'document.adminForm.' + listname );

    // empty the list
    for (var i in list.options.length) {
        list.options[i] = null;
    }
    i = 0;
    for (var x in source) {
        if (source[x][0] == key) {
            opt = new Option();
            opt.value = source[x][1];
            opt.text = source[x][2];

            if ((orig_key == key && orig_val == opt.value) || i == 0) {
                opt.selected = true;
            }
            list.options[i++] = opt;
        }
    }
    list.length = i;
}

/**
* Adds a select item(s) from one list to another
*/
function addSelectedToList( frmName, srcListName, tgtListName ) {
    var form = eval( 'document.' + frmName );
    var srcList = eval( 'form.' + srcListName );
    var tgtList = eval( 'form.' + tgtListName );

    var srcLen = srcList.length;
    var tgtLen = tgtList.length;
    var tgt = "x";

    //build array of target items
    for (var i=tgtLen-1; i > -1; i--) {
        tgt += "," + tgtList.options[i].value + ",";
    }

    //Pull selected resources and add them to list
    for (var ii=srcLen-1; ii > -1; ii--) {
        if (srcList.options[ii].selected && tgt.indexOf( "," + srcList.options[ii].value + "," ) == -1) {
            opt = new Option( srcList.options[ii].text, srcList.options[ii].value );
            tgtList.options[tgtList.length] = opt;
        }
    }
}

function delSelectedFromList( frmName, srcListName ) {
    var form = eval( 'document.' + frmName );
    var srcList = eval( 'form.' + srcListName );

    var srcLen = srcList.length;

    for (var i=srcLen-1; i > -1; i--) {
        if (srcList.options[i].selected) {
            srcList.options[i] = null;
        }
    }
}

function moveInList( frmName, srcListName, index, to) {
    var form = eval( 'document.' + frmName );
    var srcList = eval( 'form.' + srcListName );
    var total = srcList.options.length-1;

    if (index == -1) {
        return false;
    }
    if (to == +1 && index == total) {
        return false;
    }
    if (to == -1 && index == 0) {
        return false;
    }

    var items = []; // new Array;
    var values = []; // new Array;

    for (i=total; i >= 0; i--) {
        items[i] = srcList.options[i].text;
        values[i] = srcList.options[i].value;
    }
    for (i = total; i >= 0; i--) {
        if (index == i) {
            srcList.options[i + to] = new Option(items[i],values[i], 0, 1);
            srcList.options[i] = new Option(items[i+to], values[i+to]);
            i--;
        } else {
            srcList.options[i] = new Option(items[i], values[i]);
       }
    }
    srcList.focus();
}

function setSelectedValue( frmName, srcListName, value ) {
    var form = eval( 'document.' + frmName );
    var srcList = eval( 'form.' + srcListName );

    var srcLen = srcList.length;

    for (var i=0; i < srcLen; i++) {
        srcList.options[i].selected = false;
        if (srcList.options[i].value == value) {
            srcList.options[i].selected = true;
        }
    }
}

function getSelectedValue( frmName, srcListName ) {
    var form = eval( 'document.' + frmName );
    var srcList = eval( 'form.' + srcListName );

    i = srcList.selectedIndex;
    if (i != null && i > -1) {
        return srcList.options[i].value;
    } else {
        return null;
    }
}

function getSelectedText( frmName, srcListName ) {
    var form = eval( 'document.' + frmName );
    var srcList = eval( 'form.' + srcListName );

    i = srcList.selectedIndex;
    if (i != null && i > -1) {
        return srcList.options[i].text;
    } else {
        return null;
    }
}

function chgSelectedValue( frmName, srcListName, value ) {
    var form = eval( 'document.' + frmName );
    var srcList = eval( 'form.' + srcListName );

    i = srcList.selectedIndex;
    if (i != null && i > -1) {
        srcList.options[i].value = value;
        return true;
    } else {
        return false;
    }
}

/**
* Toggles the check state of a group of boxes
*
* Checkboxes must have an id attribute in the form cb0, cb1...
* @param The number of box to 'check'
*/
function checkAllzbo ( n ) {
    var f = document.adminForm;
    var c = f.toggle.checked;
    var n2 = 0;
    for (i=0; i < n; i++) {
        cb = eval( 'f.cb' + i );
        if (cb) {
            cb.checked = c;
            n2++;
        }
    }
    if (c) {
        document.adminForm.boxchecked.value = n2;
    } else {
        document.adminForm.boxchecked.value = 0;
    }
}

/**
*/
function listItemTaskClear( id, task, n ) {
    var f = document.adminForm;
    cbx = eval( 'f.' + id );
    if (cbx) {
        checkAll(n);
        cbx.checked = true;
        submitbutton(task);
    }
    return false;
}

function listItemTask( id, task ) {
    var f = document.adminForm;
    cb = eval( 'f.' + id );
    if (cb) {
        cb.checked = true;
        submitbutton(task);
    }
    return false;
}

function listItemArtTask( id, task ) {
    var f = document.adminForm;
    f.elements['artcheck'].value = id;
    submitbutton(task);
    return false;
}

function isChecked(isitchecked){
    if (isitchecked == true){
        document.adminForm.boxchecked.value++;
    }
    else {
        document.adminForm.boxchecked.value--;
    }
}
/**
* Submit the admin form
*/
function submitform(pressbutton,formname){
    document.forms[formname].action.value=pressbutton;
    try {
        document.forms[formname].onsubmit();
        }
    catch(e){}
    document.forms[formname].submit();
}

/**
* Default function.  Usually would be overriden by the component
*/
function submitbutton(pressbutton,formname) {
    submitform(pressbutton,formname);
}



/**
* Getting radio button that is selected.
*/
function getSelected(allbuttons){
    for (i=0;i<allbuttons.length;i++) {
        if (allbuttons[i].checked) {
            return allbuttons[i].value;
        }
    }
}

/**
* Pops up a new window in the middle of the screen
*/
function popupWindow(mypage, myname, w, h, scroll) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
    win = window.open(mypage, myname, winprops);
    if ( parseInt(navigator.appVersion) >= 4 ) 
    { 
       win.window.focus(); 
    }
}

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

function selPic(path, name)
{
    document.dataform.basepic.src = path+name;
    document.dataform.d_img.value = name;
    hideRow('imgthumb');
    hideRow('noimg');
    showRow('imgset');
}
function delPic(path)
{
    document.dataform.d_img.value='';
    hideRow('imgthumb');
    hideRow('imgset');
    showRow('noimg');
}

 