function getRadioValue(group){
  for(var i = 0; i < group.length; i++){
    var radio = group[i];
    if (radio.checked) return radio.value;
  }
  return null;
}

/**
 * would prefer to do this as a Node.prototype but IE sucks.
 **/
function childOf(childNode, testNode){
    var parentNode = childNode;
    while(parentNode
	  && parentNode.parentNode){
	parentNode = parentNode.parentNode;
	if (parentNode == testNode) return true;
    }
    return false;
}

/**
 * rgb2hex
 **/

function rgb2hex(rgb){
    var regEx = new RegExp("rgb\\(([0-9]*)\\s*,\\s*([0-9]*)\\s*,\\s*([0-9]*)", "i");
    var matches = regEx.exec(rgb);
    
    if (matches){
	var hex = '#' + padNum(parseInt(matches[1]).toString(16), 2)
	    + padNum(parseInt(matches[2]).toString(16), 2)
	    + padNum(parseInt(matches[3]).toString(16), 2);
	return hex.toUpperCase();
    }
    return rgb;
}

function padNum(value, digits){
    for(var i = 0; i < digits - value.length; i++){
	value = '0' + value;
    }
    return value;
}

function clearTable(table, skip){
    if (!skip || (typeof skip) == 'undefined') skip = 0;
    while(table.rows.length > skip){
	table.deleteRow(-1);
    }
}

/**
 *
 **/
function getRandString(length){
    var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    
    var randString = '';
    for(var i = 0; i < length; i++){
	randString += chars.charAt([Math.floor(Math.random() * chars.length)]);
    }
    return randString;
}

function getElementRow(el){
    while(el.nodeName != 'TR'){
        if (el.parentNode){
            el = el.parentNode;
        }
        else {
            alert("Couldn't find Element Row.");
            return false;
        }
    }
    return el;
}

function getRowTable(row){
    var element = row;
    while(element.nodeName != 'TABLE'){
        if (element.parentNode){
            element = element.parentNode;
        }
        else {
            alert("Couldn't find Row Table.");
            return false;
        }
    }
    return element;
}

function getRowIndex(row){
    var table = getRowTable(row);
    if (table){
	for(var i = 0; i < table.rows.length; i++){
	    if (table.rows[i] == row) {
		return i;
	    }
	}
    }
    return -1;
}

function lpad(value, length){
    while(value.length < length){
	value = '0' + value;
    }
    return value;

}

function openWindow(url, target, param) {
    if (Browser.Engine.trident){
        var newWindow = window.open('',target,param);
        if (newWindow)
            newWindow.location.href = url;
    }
    else {
        var newWindow = window.open(url,target,param);
    }
    
    if (newWindow){
        newWindow.focus();
    }
    else {
        alert("There was a problem opening the new window.  You may need to disable popup blocking for this site.");
    }
}

function catchReturn(e){
    var ev = new Event(e);
    
    if (ev.key == 'enter'){
        ev.stop();
        return false;
    }
    return true;
}

function setSelectValue(select, value){
    $A(select.options).each(function(option, index){
                                if (option.value == value){
                                    select.selectedIndex = index;
                                }
                            });


    
}

function setOptionsArray(select, options, selectVals, skip){
    if (!select){
        //        consoleErr("In setOptionsArray: select is null");
        return false;
    }
    
    if (skip){
        select.options.length = skip;
    }
    else {
        select.options.length = 0;
    }
    options.each(function(option){
                     select.options[select.options.length] = new Option(option, option);
                     if (selectVals && selectVals.contains(option)){
                         select.options[select.options.length - 1].selected = true;
                     }
                 });
}



/**
 * el needs to have height/width set in pixels
 * and position should be fixed
 **/
function center(el, doMorph){
    winSize = window.getSize();
    var height = el.getStyle('height');
    height = height.substring(0, height.length - 2);

    var width = el.getStyle('width');
    width = width.substring(0, width.length - 2);

    var x = Math.max(((winSize.x - width) / 2));
    var y = Math.max(0, ((winSize.y - height) / 2));

    if (doMorph){
        el.morph({left: x,
                  top: y
                 });
    }
    else {
        el.setStyles({left: x,
                      top: y
                     });
    }
    return el;
}
