
// $Id: common.js,v 1.10 2006/11/02 21:12:08 blester Exp $

/// IE 5 for Mac compatibility
Array.prototype.pop = function()
{
    out = this[this.length-1];
    this.length--;
    return out;
}
function get_attribute(el,attr_name)
{
    var i;
    
    if(!el.attributes) return false;
    
    for(i=0; i < el.attributes.length; i++)
    {
        if(el.attributes[i].nodeName.toLowerCase() == attr_name.toLowerCase())
        {
            return el.attributes[i].nodeValue;
        }
    }
    return false;
}
// use this instead of Element.setAttribute (it doesn't work properly in IE)
function set_attribute(el,attr_name,value)
{
    var i,attr;
    
    if(!el) return false;
    
    if(el.getAttributeNode(attr_name))
    {
        for(i=0; i < el.attributes.length; i++)
        {
            attr = el.attributes[i];
            if(attr.name == attr_name)
            {
                attr.value = value;
                if(attr.value != value)
                    el.setAttribute(attr_name,value);
                return;
            }
        }
    }else
    {
        el.setAttribute(attr_name,value);
        return;
    }
}
function popup( url, wt, ht, allow_scrolling, name )
{
    if(!name) name = 'popup';
	
	if(!allow_scrolling){
	    allow_scrolling = 0;
	}else{
	    allow_scrolling = 1;
	}
	
	var new_window = window.open(url,name,"location=0,scrollbars="+allow_scrolling+",width="+wt+",height="+ht+",status=0,resizable=1");
	new_window.focus();
	
	return false;
}

// {{{ Trims whitespace from right/left of string. Got source from 
// http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3621&lngWId=2
function Trim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	}else{
		return TRIM_VALUE;
	}
}
function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;
	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	}
	return strTemp;
}
function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);

	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;
	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
}
// }}}

function _get(id)
{
    return document.getElementById(id);
}
// {{{
// returns an array of elements with the given class_name
// parent is optional (defaults to 'body' if not defined), 
// out is private & should not be set by the programmer
function _getByClassName(class_name,parent_node,out)
{
    var i,node,node_list;
    
    node_list = out || [];
    parent_node = parent_node || document.getElementsByTagName('body')[0];
    
    for(i=0; i < parent_node.childNodes.length; i++)
    {
        node = parent_node.childNodes[i];
        
        if(get_attribute(node,'class') == class_name)
        {
            node_list.push(node);
        }
        _getByClassName(class_name,node,node_list);
    }
    return node_list;
}
// }}}

// {{{ calls function, 'fnc' for each child element of 'el', passing each child as a parameter to 'fnc'
function walk(el,fnc)
{
    var i,children,child;
    
    children = el.childNodes;
    
    for(i=0; i < children.length; i++)
    {
        child = children[i];
        //alert(child.nodeName);
        result = fnc(child);
        
        if(child.childNodes.length) walk(child,fnc);
    }
}
// }}}

// {{{ determines whether 'crnt' is descendent of 'el'
function isDescendentOf(el,crnt) 
{
    crnt = crnt.parentNode;
    //alert(crnt.nodeName);
    if(crnt == el) {
        return true;
    }else if(crnt.nodeName.toLowerCase() == 'html') {
        return false;
    }else {
        return isDescendentOf(el,crnt);
    }
}
// }}}