// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
var ie = (document.all) ? true : false;
function setStyleByClass(t,c,p,v){
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}

/**
*  formatCurrency - returns the passed string formatted as a currency value.
*  returns '0' if string can not be converted into a number.
*/
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));

    num = (((sign)?'':'-') + '$' + num + '.' + cents);

    return  num;
}


/**
*  getElementsByClass
*/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}



/**
* removeTableRows
*/
function removeChildren(obj){
  if(typeof(obj)=='string'){
    obj = document.getElementById(obj);
  }
    while(obj.firstChild!=null){
        obj.removeChild(obj.firstChild);
  }
  return false;
}

//  REMOVE A LINE FROM THE SKILLS TABLE
function removeLineFromParent(tablefield,parentTag) {
    while (true)  {
        tablefield = tablefield.parentNode;
        if (tablefield.tagName == parentTag) break;
    }
    var parpar = tablefield.parentNode;
    parpar.removeChild(tablefield);
}    
    

/** 
*  POST an http request
*/
function PostHttp(httpstring) {
    var xmlDoc;

    if (window.XMLHttpRequest) {
        xmlDoc = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
    }   

    xmlDoc.open("GET", httpstring, false);
    xmlDoc.send(null);
}

/**
*
*/
function setTabs(showTab, hiddenTab) {
    var divs = document.getElementsByTagName('DIV');
    for (ii = 0; ii < divs.length; ii++) {
        if (divs[ii].className == hiddenTab)
            divs[ii].style.visibility = 'hidden';
    }
    if (showTab != '' )
        document.getElementById(showTab).style.visibility = 'visible';
}


/** 
*  build a post string from a set of field names
*/
function makePostParameters() {
    var retval ='';
    appendVal = "&";
    for (iloop =0; iloop < makePostParameters.arguments.length; iloop++) {
        tmpval = document.getElementById(makePostParameters.arguments[iloop]).value;
        retval = retval + appendVal + makePostParameters.arguments[iloop];
        retval = retval + "=" + tmpval;
    }
    return retval;
}


/**
*  Retrieve text of an XML document element, including
*   elements using namespaces
*  Parameters : Prefix (name space -- but I dont know what this means so its usually blank
*               local (name of element)
*               parentElem is the parent of what we are trying to get
*               index (property in element -- I think, but i use 0)
*/
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

/**
*  Retrieve text of an XML document element, including
*   elements using namespaces
*  Parameters : Prefix (name space -- but I dont know what this means so its usually blank
*               local (name of element)
*               parentElem is the parent of what we are trying to get
*               index (property in element -- I think, but i use 0)
*/
function getElementText(local, parentElem) {
    var retval = "";
    var result = "";

    result = parentElem.getElementsByTagName(local)[0];
    if (result) {

        if (result.childNodes.length > 1) {
            retval = result.childNodes[1].nodeValue;
        } else if (result.childNodes.length == 1) {
            retval = result.firstChild.nodeValue;    		
        } else {
            retval = "";
        }

    } else {
        retval = "n/a";
    }

    if (retval == "null") retval = "";

    return retval;
}



// **********************************************************************
// xml to table
// **********************************************************************

/** 
*  build dynamic table on returned data 
*/
function createXMLtable(target_div, tag_name, xmlDoc)
{
    var xmltable = null;
 	var x = xmlDoc.responseXML.getElementsByTagName(tag_name);

	if ( x.length == 0 ) return;
	
	xmltable = document.createElement('TABLE');
	xmltable.setAttribute('id','xmltable');
	xmltable.setAttribute('cellPadding',5);
	xmltable.setAttribute('border',1);
	
	var tmp = document.createElement('TBODY');
	xmltable.appendChild(tmp);
	
        var row = document.createElement('TR')	
        for (j=0;j<x[0].childNodes.length;j++)
	{
		if (x[0].childNodes[j].nodeType != 1) continue;
		var container = document.createElement('TH');
		var theData = document.createTextNode(x[0].childNodes[j].nodeName);
		container.appendChild(theData);
		row.appendChild(container);
	}
	tmp.appendChild(row);	
	
	for (i=0;i<x.length;i++)
	{
		var row = document.createElement('TR');
                row.setAttribute('className', 'unselected');
                row.setAttribute('onMouseOver', 'javascript:mouseoverresponse(this)');
                row.setAttribute('onMouseOut', 'javascript:mouseoffresponse(this)');
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;
			var container = document.createElement('TD');

                        if ( x[i].childNodes[j].firstChild )
                            var theData = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);

			container.appendChild(theData);
			row.appendChild(container);
		}
		tmp.appendChild(row);
	}
	
        document.getElementById(target_div).appendChild(xmltable);

        return xmltable;
}


/**
*
*/
function selectTableItem(tableitem, selrow, selcol) {
    var tbodies = tableitem.getElementsByTagName('TBODY');
    var tbody = tbodies[0];
    var trow = tbody.rows[selrow];
    var retval = trow.cells[selcol].innerHTML;

    return retval;
}


/**
*
*/
function mouseoverresponse(val) {
    val.setAttribute('class','selected');
}

/**
*
*/
function mouseoffresponse(val) {
    val.setAttribute('class','unselected');
}

