


function l_node(html, cols, type, dir_up, hier, html2, type2, dir_up2, linenum)
{
	this.html = html
	this.name = html.indexOf('>') == -1 ? html : html.substring(html.indexOf('>')+1,html.indexOf('</'));
	this.name = trim(this.name);
	this.row = cols;
	this.dir_up = dir_up;
	this.hier = hier ? hier : '';
	this.type = type;
	if (html2 != null && html2.length > 0)
	{
		this.html2 = html2
		this.name2 = html2.indexOf('>') == -1 ? html2 : html2.substring(html2.indexOf('>')+1,html2.indexOf('</'));
		this.name2 = trim(this.name2);
		this.type2 = type2;
		this.dir_up2 = dir_up2;
	}
	this.linenum = linenum;
}

function l_isBlank(str)
{
	return (str=='&nbsp;' || str=='');
}


function l_sortnodes(node1, node2)
{
    var result = l_sortnodes2(node1.name, node2.name, node1.type) * (node1.dir_up ? 1 : -1);
    if (result == 0 && node1.type2 != null)
		return l_sortnodes2(node1.name2, node2.name2, node1.type2) * (node1.dir_up2 ? 1 : -1);
	else
		return result;
}

function l_sortnodes2(node1name, node2name, coltype)
{
    if (coltype.indexOf('CURRENCY')!=-1 || coltype == 'INTEGER' || coltype == 'FLOAT' || coltype == 'PERCENT')
    	return l_numnodes(node1name, node2name, coltype);
    else if( coltype == 'PRIORITY')
        return l_prioritynodes(node1name, node2name);
    else if( coltype == 'DATE' || coltype == 'MMYYDATE' || coltype == 'DATETIME' )
        return l_datenodes(node1name, node2name, coltype == 'DATETIME');
    else if( coltype == 'TIMEOFDAY' )
        return l_timenodes(node1name, node2name);
    else
    	return l_charnodes(node1name,node2name);
}

function l_charnodes(str1, str2)
{
	if (l_isBlank(str1) || l_isBlank(str2))
	{
		var empty1 = l_isBlank(str1);
		var empty2 = l_isBlank(str2);
		return (empty1 && !empty2) ? 1 : ((empty2 && !empty1) ? -1 : 0);
	}
	else
		return str1.toLowerCase() < str2.toLowerCase() ? -1 : (str1 == str2 ? 0 : 1);
}

function l_numnodes(node1name, node2name, type)
{
	var num1 = l_num(node1name, type);
	var num2 = l_num(node2name, type);
	return  num1 < num2 ? -1 : (num1 == num2 ? 0 : 1);
}

function l_getpriority(nodename)
{
	var node = nodename.toLowerCase();
  	if (node.charAt(0) == 'l')
   		return 30;
  	else if (node.charAt(0) == 'm')
    	return 20;
  	else if (node.charAt(0) == 'h')
    	return 10;
}

function l_prioritynodes(node1name, node2name)
{
	var num1 = l_getpriority(node1name);
	var num2 = l_getpriority(node2name);
	return num1 < num2 ? -1 : (num1 == num2 ? 0 : 1);
}

function l_datenodes(node1name, node2name, bDateTime)
{
  var date1;
  var date2;
  if ( bDateTime )
  {
	date1 = stringtotime(node1name, node1name).getTime();
	date2 = stringtotime(node2name, node2name).getTime();
  }
  else
  {
	date1 = stringtodate(node1name).getTime();
	date2 = stringtodate(node2name).getTime();
  }

  if (isNaN(date1)) return 1;
  else if (isNaN(date2)) return -1;
  else return (date1 < date2) ? -1 : (date1 == date2 ? 0 : 1);
}

function l_timenodes(node1name, node2name)
{
  var date1 = new Date();
  var date2 = date1 ;

  date1 = l_time(node1name, date1);
  date2 = l_time(node2name, date2);
  return date1 < date2 ? -1 : (date1 == date2 ? 0 : 1);
}

function l_time(time, date)
{
	var re = /([0-9][0-9]?)(:[0-5][0-9])\s?([AaPp])?[Mm]?/;
	var result = re.exec(time);

	var hours = parseInt(RegExp.$1);
	var minutes = parseInt(RegExp.$2.substr(1));
	var ampm = (RegExp.$3.length == 0 || RegExp.$3.toUpperCase() == 'A') ? 'am' : 'pm';
	hours = (ampm == 'pm') ? (hours%12)+12 : hours%12;
	return hours*60+minutes;
}

function l_num(num, type)
{
	num = num.replace(/[,$%]/g,'');
	if (num.charAt(0)=='(')
		num = '-'+num.substring(1,num.length-1);
	if (isNaN(num))
		num = (type=='INTEGER') ? '0' : '0.0';
	return (type=='INTEGER') ? parseInt(num) : parseFloat(num);
}

function l_id(id)
{
	return document.all[id];
}

function l_elem(id)
{
	return document.forms[0].elements[id];
}

function l_sort(col,coltype,hier,hiercol,mach,colname,sAlternateSortType)
{
    // this is used in cases where a column needs to be sorted by something different than it's type/value
	if(sAlternateSortType != null)
		coltype = sAlternateSortType;
	var i = 0, node,node2, numRows = 0, rnode, col2 = -1, col2type;
	var elems = new Array(), elem, cols;
	var dir_up = true, dir_up2;
	var ordered_mach = false;
	if (mach == '' || mach == null) mach = '';
	else if (document.forms[0].elements[mach+'sortidx'] != null)
	{
		col2 = document.forms[0].elements[mach+'sortidx'].value;
		if (col2 == col)
			col2 = -1;
		else
		{
			col2type = document.forms[0].elements[mach+'sorttype'].value;
			dir_up2 = document.forms[0].elements[mach+'sortdir'].value == "UP";
		}
		ordered_mach = hasEncodedField(mach,mach+"seqnum");
	}

	node = l_id(mach+'dir'+col);
	if (node.src.indexOf('nlup.gif') != -1)
		dir_up = false;
	node.src = dir_up ? '/images/nav/nlup.gif' : '/images/nav/nldown.gif';
	node2 = l_id(mach+'header');
	for (idx=0; idx < node2.cells.length; idx++)
	{
		if (idx == col)
			continue;
		node = l_id(mach+'dir'+idx);
		if (typeof node != 'undefined')
			node.src = '/images/nav/nlempty.gif';
	}

    while (true)
    {
        node = l_id(mach+'row'+i);
        if (typeof node != 'undefined')
        {
            // when we are sorting by a different type/value than the cell contiains, that value will
            // exist in a hidden span - which will always be the first child within the <TD>
	        if(sAlternateSortType != null)
	            elem = node.cells[col].firstChild.innerHTML;
	        else
	            elem = node.cells[col].innerHTML;
        	cols = new Array();

			for (idx=0; idx < node.cells.length; idx++)
			{
				cols[idx] = node.cells[idx].innerHTML;
				
			}


			if (hier)
			{
				hierfld = l_elem('_hier'+i);
				if (hiercol)
					elem = hierfld.value;
				elems[i] = new l_node(elem.toLowerCase(),cols,coltype,dir_up,hierfld.value);
			}
			else if (col2 != -1)
				elems[i] = new l_node(elem.toLowerCase(),cols,coltype,dir_up,null,node.cells[col2].innerHTML,col2type,dir_up2, ordered_mach ? findEncodedValue(mach, mach+"seqnum", i+1) : -1);
            else
				elems[i] = new l_node(elem.toLowerCase(),cols,coltype,dir_up,null,null,null,null,ordered_mach ? findEncodedValue(mach, mach+"seqnum", i+1) : -1);
            i++;
        }
        else
            break;
    }

    numRows = i;
    i = 0;

	elems.sort(l_sortnodes);

    while (i < numRows)
    {
        node = l_id(mach+'row'+i);
    	rnode = elems[i];
    	cols = rnode.row;

		for (idx=0; idx < node.cells.length; idx++)
			node.cells[idx].innerHTML = cols[idx];

		if (hier)
		{
			hierfld = l_elem('_hier'+i);
			hierfld.value = rnode.hier;
		}
		if (ordered_mach)
			setEncodedValue(mach,rnode.linenum,mach+'seqnum',i+1);
		i++;
    }
    l_saveprefs(mach, col, colname, coltype, dir_up);
}

function l_sortprefs(col,coltype,mach,colname)
{
    var dir_up = true;
    node = l_id(mach+'dir'+col);
    if (node.src.indexOf('nlup.gif') != -1)
        dir_up = false;
    l_saveprefs(mach, col, colname, coltype, dir_up);
}

function l_saveprefs(mach, col, colname, coltype, dir_up)
{
    if (mach != null && mach.length > 0 && document.forms[0].elements[mach+'sortidx'] != null)
    {
        if (document.forms[0].elements[mach+'sortname'].value != colname)
        {
            document.forms[0].elements[mach+'sort2name'].value = document.forms[0].elements[mach+'sortname'].value;
            document.forms[0].elements[mach+'sort2dir'].value = document.forms[0].elements[mach+'sortdir'].value;
        }
        document.forms[0].elements[mach+'sortidx'].value = col;
        document.forms[0].elements[mach+'sortname'].value = colname;
        document.forms[0].elements[mach+'sorttype'].value = coltype;
        document.forms[0].elements[mach+'sortdir'].value = dir_up ? "UP" : "DOWN";
    }
}

function l_bold(mach, chkd, rownum)
{
	var node = l_id(mach+'row'+rownum);
	for (idx=0; idx < node.cells.length; idx++)
	{
		var classname = node.cells[idx].className;
		if ( chkd && classname.indexOf('texttable') != -1 )
			classname = 'textdark'+classname.substring(classname.indexOf('texttable')+9);
		else if ( !chkd && classname.indexOf('textdark') != -1 )
			classname = 'texttable'+classname.substring(classname.indexOf('textdark')+8);
		node.cells[idx].className = classname;
	}
}

