addEvent(window, "load", sortables_init);

var holdtempvars = new Array();
var mainreq = null;
var formatrow = new Array();
var specialformat = "";
var filtersOn = new Array();
var excludes = new Array();
var issortedtable = "";
var mstate = new Array;
var mopen = 0;
var justopened = 0;
var menucounter = 5;

var SORT_COLUMN_INDEX;
var topZ = 1;
var startX;
var startY;
startX = 175;
startY = 175;
nextID = 1;

function getE(elName) {
return document.getElementById(elName);
};

function hideShow(subel, toggleimage, useupimage, usedownimage, setto) {
        var ii = 1;
        var stayin = 1;
        while (stayin) {
                var tname = subel + ii;
                var ikmail = getE(tname);
                if (ikmail) {
                        if (setto == "show" || (!setto && ikmail.style.display == "none")) {
                                ikmail.style.display = "";
				if (!setto) {
					setto = "show";
				}
                        } else if ((setto == "hide" || setto == "none") || (!setto && ikmail.style.display != "none")) {
                                ikmail.style.display = "none";
				if (!setto) {
					setto = "none";
				}
                        }
                        ii++;
                } else {
                        stayin = 0;
                }
        }
	if (toggleimage) {
		var imi = getE(toggleimage);
		if (setto == "show") {
			var useimage = usedownimage;
			if (!usedownimage) {
				useimage = "/web/down_arrow_indicator.gif";
			}
		} else {
			var useimage = useupimage;
			if (!useupimage) {
				useimage = "/web/up_arrow_indicator.gif";
			}
		}
		imi.src = useimage;
	}
}

function showdiv(divname, divval, imname, useupimage, usedownimage, tabletr, icname) {
        var adiv = document.getElementById(divname);
	if (holdtempvars[divname]) {
		holdtempvars[divname] = 0;
		adiv.innerHTML = '';
		if (imname) {
			var imi = getE(imname);
			var useimage = useupimage;
			if (!useimage) {
				useimage = "/web/up_arrow_indicator.gif";
			}
			imi.src = useimage;
		}
		if (tabletr && icname) {
			var itab = getE(tabletr);
			itab.className = icname;
		}
	} else {
		holdtempvars[divname] = 1;
		if (divval.indexOf("STATEHOLDER") > -1) {
			divval = divval.replace(/STATEHOLDER/g, getstatech());
		}
		adiv.innerHTML = divval;
		if (imname) {
			var imi = getE(imname);
			var useimage = usedownimage;
			if (!useimage) {
				useimage = "/web/down_arrow_indicator.gif";
			}
			imi.src = useimage;
		}
		if (tabletr && icname) {
			var itab = getE(tabletr);
			itab.className = "no" + icname;
		}
	}
}

function unique() {
var dusetime = new Date();
var usetime = "" + dusetime;
usetime = usetime.replace(/ /g, "");
usetime = usetime.replace(/\,/g, "");
usetime = usetime.replace(/\(/g, "");
usetime = usetime.replace(/\)/g, "");
usetime = usetime.replace(/\-/g, "");
usetime = usetime.replace(/\:/g, "");
return usetime;
}

function select_all_del(formname, mainel, subel) {
	var elem = document.getElementById(formname).elements;
	var sel = document.getElementById(mainel).checked;
	var sublen = subel.length;
	for (i=0; i< elem.length; i++) {
		var kmail = elem[i].name;
		if (kmail.substring(0, sublen) == subel) {
			var ikmail = document.getElementById(kmail);
			ikmail.checked = sel;
		}
	}
}

function textMax (field, max, nmleft) {
	var leftt = max - field.value.length;
	if (field.value.length > max) {
		leftt = 0;
		field.value = field.value.substring(0, max);
	}
	var nmleftf = document.getElementById(nmleft);
	nmleftf.innerHTML = '' + leftt + ' Characters Left';
}

function numOnly(val) {
        val = "" + val;
        val = val.replace(/\$/, "");
if (val == '&nbsp;') {
        return 0;
} else if (val) {
        var neg = 0;
        if (val.substring(0, 1) == '-') {
                neg = 1;
        }
        val = val.replace(/\./, "555333222187");
        val = val.replace(/[\D]/g, "");
        val = val.replace(/555333222187/, ".");
        var retval = Number(val);
        if (neg) {
                retval = retval * (-1);
        }
        return retval;
} else {
        return 0;
}
}

function money(itax) {
        itax = itax + "..";
        var taxarr = itax.split(".");
        var jtax = taxarr[0];
        var ctax = "" + taxarr[1];
        while (ctax.length < 2) {
                ctax = ctax + "0";
        }
        if (ctax.length > 2) {
                if (ctax.substring(2, 3) == "0") {
                        ctax = ctax.substring(0, 2);
                } else {
                        ctax = ctax.substring(0, 2);
                        ctax++;
                        if (ctax > 99) {
                                ctax = "00";
                                jtax++;
                        }
                }
        }
        var addzero = 0;
        if (!ctax) {
                ctax = '00';
        }
        ctax = "" + ctax;
        if (ctax.substring(0, 1) == '0') {
                addzero = 1;
        }
        ctax = "" + numOnly(ctax);
        if (ctax.length < 2 && addzero) {
                ctax = '0' + ctax;
        }
        while (ctax.length < 2) {
                ctax = ctax + "0";
        }

	var rett = '';
        while (jtax.length > 3) {
		var wlength = jtax.length;
		rett = "," + jtax.substring(wlength - 3, wlength) + rett;
		jtax = jtax.substring(0, wlength - 3);
	}
	jtax = jtax + rett;

        itax = jtax + "." + ctax;
return "$" + itax;
}


function gJax(fullurl, returnto) {
if (mainreq != null && mainreq.readyState != 0 && mainreq.readyState != 4) {
	mainreq.abort();
};

try {
	mainreq = new XMLHttpRequest();
} catch (error) {
	try {
		mainreq = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (error) {
		mainreq = null;
		return false;
	};
};

if (!returnto) {
	returnto = "justDoneSave";
}

mainreq.onreadystatechange = eval(returnto);
var curDateTime = new Date();
mainreq.open("GET", fullurl + "&igjax=" + curDateTime);
mainreq.send(null);

return true;
}


function justDoneSave() {
if (mainreq.readyState == 4) {
try {
if (mainreq.status == 200) {
} else if (mainreq.status != 0) {
};
} catch (error) {
};
};
return true;
};

function zipLookup(zipfield, cityfield, statefield, countryfield) {
	var country = getE(countryfield).value;
	if (country == "US") {
		var zipfield1 = getE(zipfield);
		var zip = zipfield1.value;
		zip = zip.replace(/\-/, "555333222187");
        	zip = zip.replace(/[\D]/g, "");
		zip = zip.replace(/555333222187/, "-");
		zipfield1.value = zip;
		if (zip.length > 4) {
			gJax("/doziplookup.dhtml?zip=" + zip + "&city=" + cityfield + "&state=" + statefield + "&zipfield=" + zipfield, "zipLookupReturn");
		}
	}
}

function zipLookupReturn() {
if (mainreq.readyState == 4) {
try {
if (mainreq.status == 200) {
	var resptext = mainreq.responseText;
	if (resptext.length > 6) {
		resptext = resptext.replace(/\n/, "");
		var values = resptext.split("||");
		var cityfield = getE(values[0]);
		cityfield.value = values[1];
		var statefield = getE(values[2]);
		statefield.value = values[3];
		var zipfield = getE(values[4]);
		zipfield.value = values[5];
	}
} else if (mainreq.status != 0) {
};
} catch (error) {
};
};
return true;
};

function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function graphchange(selector, useridText, replocation) {
	var gselected = selector.value;
	if (!useridText) {
		useridText = '?a=1';
	}
	gJax("/reporter.dhtml" + useridText + "&action=showgraph&changeloc=" + replocation + "&file=" + gselected, "displayGraph");
}

function displayGraph() {
if (mainreq.readyState == 4) {
try {
if (mainreq.status == 200) {
var resptext = mainreq.responseText;
eval(resptext);
} else if (mainreq.status != 0) {
};
} catch (error) {
};
};
return true;
};


function sortables_init() {
    // Find all tables with class sortable and make them sortable
    if (!document.getElementsByTagName) return;
    tbls = document.getElementsByTagName("table");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) {
						issortedtable = thisTbl.id;
            ts_makeSortable(thisTbl);
        }
    }
}

function ts_makeSortable(table) {
    if (table.rows && table.rows.length > 0) {
        var firstRow = table.rows[0];
    }
    if (!firstRow) return;
    
    // We have a first row: assume it's the header, and make its contents clickable links
    for (var i=0;i<firstRow.cells.length;i++) {
        var cell = firstRow.cells[i];
	var innert = cell.innerHTML;
	if (innert.indexOf('type="checkbox"') > 0) {
	} else {
        var txt = ts_getInnerText(cell);
				var filtertext = "";

				if (filterData[i]) {
					filtertext = "<a href='javascript:filterRow(" + i + ")'><img src=\"/web/up_arrow_indicator.gif\" border=\"0\" id=\"tabupind" + i + "\"></a><div id=\"filtercolarea" + i + "\"></div>";
				}
				cell.id = "sorttablecell" + i;
        cell.innerHTML = '<a href="#" style="text-decoration:none" class="sortheader" '+ 
        'onclick="ts_resortTable(this, '+i+');return false;">' + 
				txt+'<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a>' + filtertext;

    	}
    }
}

function filterRow(column) {
					var ufilterData = filterData[column];
					var headtail = ufilterData.split("|||");
	var tabupind = getE("tabupind" + column);
	if (filtersOn[column]) {
		saveFilterValues(column);

		tabupind.src = "/web/up_arrow_indicator.gif";
		var d = document.getElementById("dragTitle" + String(filtersOn[column]));
		d.parentNode.removeChild(d);

		var d2 = document.getElementById("dragContent" + String(filtersOn[column]));
		d2.parentNode.removeChild(d2);
		filtersOn[column] = 0;
	} else {
		tabupind.src = "/web/down_arrow_indicator.gif";
		var fcol = getE("filtercolarea" + column);
		CreateDropdownWindow(headtail[0], 300, findPosX(fcol), findPosY(fcol), headtail[1], 'filterRow(' + column + ')');
		filtersOn[column] = nextID - 1;
		restoreFilterValues(column);
	}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while(1) {
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	} else if(obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent) {
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	} else if(obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function jstrprintf() {
    len = arguments.length;
    if (len == 0) { return; }
    if (len == 1) { return arguments[0]; }
    
    var result;
    var regexstr;
    var replstr;
    var formatstr;
    var re;
    
    result = "";
    regexstr = "";
    replstr = "";
    formatstr = arguments[0];
    
    for (var i=1; i<arguments.length; i++) {
        replstr += String(i+100) + arguments[i]  + String(i + 100);
        regexstr += String(i+100) + "(.*)" + String(i+100);
    }
    re = new RegExp(regexstr);
    var result;
    result = replstr.replace(re, formatstr);
    return result;
}

function AddPx(num) {
    return String(num) + "px";
}

function findParentDiv(obj) {
    while (obj) {
        if (obj.tagName.toUpperCase() == "DIV") {
            return obj;
        }
        
        if (obj.parentElement) {
            obj = obj.parentElement;
        }
        else {
            return null;
        }
    }
    return null;
}

function findParentTagById(obj, parentname) {
    while (obj) {
        if (obj.id.match(parentname)) {
            return obj;
        }
        
        if (obj.parentElement) {
            obj = obj.parentElement;
        }
        else {
            return null;
        }
    }
    return null;
}

function CreateDropdownWindow(caption, theWidth, inxpos, inypos, contentSource, closecode) {
	caption = "&nbsp;" + caption;
	if (inxpos && inypos) {
		startX = inxpos;
		startY = inypos;
	}
	var headw = theWidth - 2;
    var newdiv;
    newdiv = document.createElement("div");
    newdiv.id = "dragTitle" + String(nextID);
    newdiv.className = "divDragTitle";
    newdiv.style.width = "" + headw + "px";
    newdiv.style.left = AddPx(startX);
    newdiv.style.top = AddPx(startY);
    newdiv.style.zIndex = topZ;
    newdiv.innerHTML = jstrprintf(
        '<table><tr><td>$1</td>' + 
        '<td style="text-align:right">' +
        '<img src="/web/down_arrow_indicator.gif" class="divTitleButton" id="dragButton$2" ' + 
        'onmousedown="javascript:' + closecode + '" /></td>' +
        '</tr></table>',
        caption, nextID);
    
       // IE doesn't support addEventListener, so check for its presence
       if (newdiv.addEventListener) {
           // firefox, etc.
           newdiv.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
           newdiv.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
           newdiv.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
       }
       else {
           // IE
           newdiv.attachEvent("onmousemove", function(e) { return mouseMove(e) });
           newdiv.attachEvent("onmousedown", function(e) { return mouseDown(e) });
           newdiv.attachEvent("onmouseup", function(e) { return mouseUp(e) });
       }
    document.splash.appendChild(newdiv);

    var newdiv2;
    newdiv2 = document.createElement("div");
    newdiv2.id = "dragContent" + String(nextID);
    newdiv2.className = "divDragContent";
    newdiv2.style.width = "" + theWidth + "px";
    newdiv2.style.left = AddPx(startX);
    newdiv2.style.top = AddPx(startY + 20);
    newdiv2.style.zIndex = topZ;
		newdiv2.innerHTML = contentSource;
    
       if (newdiv2.addEventListener) {
           // firefox, etc.
           newdiv2.addEventListener("mousedown", function(e) { return contentMouseDown(e) }, true);
       } else {
           // IE
           newdiv2.attachEvent("onmousedown", function(e) { return contentMouseDown(e) });
        }
    document.splash.appendChild(newdiv2);
    
    // Save away the content DIV into the title DIV for 
    // later access, and vice versa
    newdiv.content = newdiv2;
    newdiv2.titlediv = newdiv;

    topZ += 1;
    startX += 25;
    startY += 25;
    // If you want you can check when these two are greater than
    // a certain number and then rotate them back to 100,100...
    
    nextID++;
}

function toggleContentWin(id) {
    var elem = document.getElementById("dragContent" + String(id));
    var img = document.getElementById("dragButton" + String(id));

    if (elem.style.display == "none") {
        // hidden, so unhide
        elem.style.display = "block";
        
        // Change the button's image
        img.src = "/images/buttontop.gif";
}
    else {
        // showing, so hide
        elem.style.display = "none";

        // Change the button's image
        img.src = "/images/buttonbottom.gif";
    }
}

// Drag methods
var dragObjTitle = null;
var dragOffsetX = 0;
var dragOffsetY = 0;

function contentMouseDown(e) {
    // Move the window to the front
    // Use a handy trick for IE vs FF
    var dragContent = e.srcElement || e.currentTarget;
    if ( ! dragContent.id.match("dragContent")) {
        dragContent = findParentTagById(dragContent, "dragContent");
    }
    if (dragContent) {
        dragContent.style.zIndex = topZ;
        dragContent.titlediv.style.zIndex = topZ;
        topZ++;
    }
}

function mouseDown(e) {
    // These first two lines are written to handle both FF and IE
    var curElem = e.srcElement || e.target;
    var dragTitle = e.currentTarget || findParentDiv(curElem);
    if (dragTitle) {
        if (dragTitle.className != 'divDragTitle') {
            return;
        }
    }
    
    // Start the drag, but first make sure neither is null
    if (curElem && dragTitle) {
    
        // Attach the document handlers. We don't want these running all the time.
        addDocumentHandlers(true);
    
        // Move this window to the front.
        dragTitle.style.zIndex = topZ;
        dragTitle.content.style.zIndex = topZ;
        topZ++;
    
        // Check if it's the button. If so, don't drag.
        if (curElem.className != "divTitleButton") {
            
            // Save away the two objects
            dragObjTitle = dragTitle;
            
            // Calculate the offset
            dragOffsetX = e.clientX - 
                dragTitle.offsetLeft;
            dragOffsetY = e.clientY - 
                dragTitle.offsetTop;
                
            // Don't let the default actions take place
            if (e.preventDefault) {
                e.preventDefault();
            }
            else {
                document.onselectstart = function () { return false; };
                e.cancelBubble = true;
                return false;
            }
        }
    }
}

function mouseMove(e) {
    // If not null, then we're in a drag
    if (dragObjTitle) {
    
        if (!e.preventDefault) {
            // This is the IE version for handling a strange
            // problem when you quickly move the mouse
            // out of the window and let go of the button.
            if (e.button == 0) {
                finishDrag(e);
                return;
            }
        }
    
        dragObjTitle.style.left = AddPx(e.clientX - dragOffsetX);
        dragObjTitle.style.top = AddPx(e.clientY - dragOffsetY);
        dragObjTitle.content.style.left = AddPx(e.clientX - dragOffsetX);
        dragObjTitle.content.style.top = AddPx(e.clientY - dragOffsetY + 20);
        if (e.preventDefault) {
            e.preventDefault();
        }
        else {
            e.cancelBubble = true;
            return false;
        }
    }
}

function mouseUp(e) {
    if (dragObjTitle) {
        finishDrag(e);
    }
}

function finishDrag(e) {
    var finalX = e.clientX - dragOffsetX;
    var finalY = e.clientY - dragOffsetY;
    if (finalX < 0) { finalX = 0 };
    if (finalY < 0) { finalY = 0 };

    dragObjTitle.style.left = AddPx(finalX);
    dragObjTitle.style.top = AddPx(finalY);
    dragObjTitle.content.style.left = AddPx(finalX);
    dragObjTitle.content.style.top = AddPx(finalY + 20);
    
    // Done, so reset to null
    dragObjTitle = null;
    addDocumentHandlers(false);
    if (e.preventDefault) {
        e.preventDefault();
    }
    else {
        document.onselectstart = null;
        e.cancelBubble = true;
        return false;
    }
}

function addDocumentHandlers(addOrRemove) {
    if (addOrRemove) {
        if (document.body.addEventListener) {
            // firefox, etc.
            document.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
            document.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
            document.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
        }
        else {
            // IE
            document.onmousedown = function() { mouseDown(window.event) } ;
            document.onmousemove = function() { mouseMove(window.event) } ;
            document.onmouseup = function() { mouseUp(window.event) } ;
        }
    } else {
        if (document.body.addEventListener) {
            // firefox, etc.
            //remove.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
            //remove.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
            //remove.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
        } else {
            // IE
            // Be careful here. If you have other code that sets these events,
            // rather than just clear them out.
            document.onmousedown = null;
            document.onmousemove = null;
            document.onmouseup = null;
        }
    }
}




function ts_getInnerText(el) {
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) return el.innerText;	//Not needed but it is faster
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += ts_getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}

function ts_resortTable(lnk,clid) {
    // get the span
    var span;
    for (var ci=0;ci<lnk.childNodes.length;ci++) {
        if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
    }
    var spantext = ts_getInnerText(span);
    var td = lnk.parentNode;
    var column = clid || td.cellIndex;

    var table = getParent(td,'TABLE');
		
		var tt = typeof table.rows[1].cells[column];
    
    // Work out a type for the column
    if (table.rows.length <= 1) return;
    var itm = ts_getInnerText(table.rows[1].cells[column]);
	if (itm == "" && lnk.childNodes.length > 1) {
    		itm = ts_getInnerText(table.rows[2].cells[column]);
	}
    sortfn = ts_sort_caseinsensitive;
		specialformat = "";
		if (formatrow[column]) {
			var fmat = formatrow[column];
			if (fmat.substring(0, 4) == "DATE") {
				specialformat = fmat.substring(4);
				sortfn = new_sort_date;
			} else if (fmat == "NUM") {
				sortfn = new_num_sort;
			}
		} else {
	if (itm == "") {
		sortfn = new_num_sort;
	} else {
    		if (itm.match(/^.[\/-]...[\/-]....$/)) {
			sortfn = new_sort_date;
			specialformat = maindateformat;
		}
    		if (itm.match(/^..[\/-]...[\/-]....$/)) {
			sortfn = new_sort_date;
			specialformat = maindateformat;
		}
    		if (itm.match(/^..[\/-]..[\/-]....$/)) {
			sortfn = new_sort_date;
			specialformat = maindateformat;
		}
    		if (itm.match(/^...[\/-]..[\/-]....$/)) {
			sortfn = new_sort_date;
			specialformat = maindateformat;
		}
    		if (itm.match(/^...[\/-].[\/-]....$/)) {
			sortfn = new_sort_date;
			specialformat = maindateformat;
		}
    		if (itm.match(/^.[\/-]...[\/-]..$/)) {
			sortfn = new_sort_date;
			specialformat = maindateformat;
		}
    		if (itm.match(/^..[\/-]...[\/-]..$/)) {
			sortfn = new_sort_date;
			specialformat = maindateformat;
		}
    		if (itm.match(/^..[\/-]..[\/-]..$/)) {
			sortfn = new_sort_date;
			specialformat = maindateformat;
		}
    		if (itm.match(/^...[\/-]..[\/-]..$/)) {
			sortfn = new_sort_date;
			specialformat = maindateformat;
		}
    		if (itm.match(/^...[\/-].[\/-]..$/)) {
			sortfn = new_sort_date;
			specialformat = maindateformat;
		}
    		if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = new_sort_date;
    		if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = new_sort_date;
    		if (itm.match(/^[\d\.\,\$]+$/)) sortfn = new_num_sort;
    		if (itm.match(/^\$/)) sortfn = new_num_sort;
    		if (itm.match(/\%$/)) sortfn = new_num_sort;
	}
		}
    SORT_COLUMN_INDEX = column;
    var firstRow = new Array();
    var newRows = new Array();
    for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
    for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }

    newRows.sort(sortfn);

    if (span.getAttribute("sortdir") == 'down') {
        ARROW = '&nbsp;&nbsp;&uarr;';
        newRows.reverse();
        span.setAttribute('sortdir','up');
    } else {
        ARROW = '&nbsp;&nbsp;&darr;';
        span.setAttribute('sortdir','down');
    }
    
    // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
    // don't do sortbottom rows
		var onclass = "1";
    for (i=0;i<newRows.length;i++) {
			if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) {
				if (excludes[newRows[i].id]) {
				    } else {
				newRows[i].className = "highlighthead" + onclass;
				}
				table.tBodies[0].appendChild(newRows[i]);
				if (excludes[newRows[i].id]) {
				    } else {
				if (onclass == 1) {
					onclass = 2;
				} else {
					onclass = 1;
				}
				}
			}
		}
    // do sortbottom rows only
    for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}
    
    // Delete any other arrows there may be showing
    var allspans = document.getElementsByTagName("span");
    for (var ci=0;ci<allspans.length;ci++) {
        if (allspans[ci].className == 'sortarrow') {
            if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
                allspans[ci].innerHTML = '&nbsp;&nbsp;&nbsp;';
            }
        }
    }
        
    span.innerHTML = ARROW;
}

function getParent(el, pTagName) {
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
		return el;
	else
		return getParent(el.parentNode, pTagName);
}

function goodDate(idte) {
		if (specialformat.indexOf("-") > -1) {
			var parts1 = idte.split("-");
			var def = specialformat.split("-");
			var dlen = def.length;
			var year1;
			var mon1;
			var day1;
			for (i = 0; i < dlen; i++) {
				if (def[i] == "DD") {
					day1 = parts1[i];
				} else if (def[i] == "MM") {
					mon1 = parts1[i];
				} else if (def[i] == "YYYY") {
					year1 = parts1[i];
				} else if (def[i] == "YY") {
					year1 = "20" + parts1[i];
				} else if (def[i] == "MON" || def[i] == "MONTH") {
					mon1 = parts1[i].toLowerCase().substring(0, 3);
					if (mon1 == "jan") {
						mon1 = "01";
					} else if (mon1 == "feb") {
						mon1 = "02";
					} else if (mon1 == "mar") {
						mon1 = "03";
					} else if (mon1 == "apr") {
						mon1 = "04";
					} else if (mon1 == "may") {
						mon1 = "05";
					} else if (mon1 == "jun") {
						mon1 = "06";
					} else if (mon1 == "jul") {
						mon1 = "07";
					} else if (mon1 == "aug") {
						mon1 = "08";
					} else if (mon1 == "sep") {
						mon1 = "09";
					} else if (mon1 == "oct") {
						mon1 = "10";
					} else if (mon1 == "nov") {
						mon1 = "11";
					} else if (mon1 == "dec") {
						mon1 = "12";
					}
				}
			}
			if (mon1.length == 1) {
				mon1 = '0' + mon1;
			}
			if (day1.length == 1) {
				day1 = '0' + day1;
			}
			return year1 + "-" + mon1 + "-" + day1;
		} else {
			return idte;
		}
}

function new_sort_date(a,b) {
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
		var dt1 = goodDate(aa);
		var dt2 = goodDate(bb);

    if (dt1==dt2) return 0;
    if (dt1<dt2) return -1;
    return 1;
}

function ts_sort_date(a,b) {
    // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
    if (aa.length == 10) {
        dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
    } else {
        yr = aa.substr(6,2);
        if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
        dt1 = yr+aa.substr(3,2)+aa.substr(0,2);
    }
    if (bb.length == 10) {
        dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
    } else {
        yr = bb.substr(6,2);
        if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
        dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
    }
    if (dt1==dt2) return 0;
    if (dt1<dt2) return -1;
    return 1;
}

function ts_sort_currency(a,b) { 
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
    return parseFloat(aa) - parseFloat(bb);
}

function ts_sort_numeric(a,b) {
    aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
    if (isNaN(aa)) aa = 0;
    bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX])); 
    if (isNaN(bb)) bb = 0;
    return aa-bb;
}

function new_num_sort(a,b) {
    aa = numOnly(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
    if (isNaN(aa)) aa = 0;
    bb = numOnly(ts_getInnerText(b.cells[SORT_COLUMN_INDEX])); 
    if (isNaN(bb)) bb = 0;
    return aa-bb;
}

function ts_sort_caseinsensitive(a,b) {
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}

function ts_sort_default(a,b) {
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}


function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
} 

function instantFilter(ccontrol, oncol, bytype) {
	var ilist = instantfilters[oncol].split("|");

	oncol = oncol + 1;
	var ilen = ilist.length - 1;
	for (var i = 0; i < ilen; i++) {
		var onoff = "";
		var online = "";
		
		if (bytype) {
			var cval;
			var itype = typeof ccontrol;
			if (itype == "object") {
				cval = ccontrol.value;
			} else {
				cval = ccontrol;
			}
			var recdata = ilist[i].split("=");
			online = recdata[0];
			if (cval == recdata[1] || !cval) {
				onoff = "on";
			} else {
				onoff = "none";
			}
		} else {
			online = ilist[i];
			if (ccontrol.checked) {
				onoff = "none";
			} else {
				onoff = "on";
			}
		}
		if (online) {
			if (onoff == "none") {
				if (excludes["mat" + online]) {
					excludes["mat" + online] = excludes["mat" + online] + oncol + "a";
				} else {
					excludes["mat" + online] = "" + oncol + "a";
				}
				var mat = getE("mat" + online);
				mat.style.display = "none";
			} else if (onoff == "on") {
				var eclud = excludes["mat" + online];
				if (eclud) {
					var repl = oncol + "a";
					var re = new RegExp(repl, 'g');
					eclud = eclud.replace(re, "");
					excludes["mat" + online] = eclud;
					if (!eclud) {
						var mat = getE("mat" + online);
						mat.style.display = "";
					}
				}
			}
		}
	}
	var table = getE(issortedtable);
	
	var firstRow = new Array();
	var newRows = new Array();
	for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
	for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }
	var onclass = "1";
	for (i=0;i<newRows.length;i++) {
		if (excludes[newRows[i].id]) {
		} else {
			newRows[i].className = "highlighthead" + onclass;
			if (onclass == 1) {
				onclass = 2;
			} else {
				onclass = 1;
			}
		}
	}
}

function showhide(navdiv, clickopen, openonly) {
	if (mstate[navdiv] == 'block') { 
		if (openonly) {
			return;
		}
		mstate[navdiv] = 'none';
		mopen = 0;
		justopened = 0;
		CloseAll();
	} else { 
		mopen = numOnly(navdiv);
		CloseAll(mopen);
		mstate[navdiv] = 'block';
		if (clickopen) {
			justopened = 1;
		}
	} 
	var inavdiv = getE(navdiv);
	inavdiv.style.display = mstate[navdiv]
	inavdiv.style.top = 18;
	inavdiv.style.left = -146;
}

function CloseAll(isopen) {
	if (isopen) {
		getE("n" + isopen).src = 'web/blue_circle_nav.gif';
	}

	for (i = 1; i <= menucounter; i++) {
		if (!isopen || isopen != i) {
			mstate["mmenu" + i] = "none";
			getE("mmenu" + i).style.display = "none";
			getE("n" + i).src = "web/gry_circle_nav.gif";
		}
	}
}

function hide(event) {
	if (event.srcElement.tagName == "DIV") {
		CloseAll();
	}
}

function checkClose() {
	if (mopen) {
		if (justopened) {
			justopened = 0;
		} else {
			mopen = 0;
			CloseAll();
		}
	}
}

function popupwindow(file,name) {
popupwindow1=open(file, name,"width=413,height=413,scrollbars=yes,menubar=no");
}

