// @cvs-id $Id: lib.js,v 1.12 2007/02/05 15:27:35 nsadmin Exp $ $Name: bridge-release-2_6_1-pre2 $
// Copyright 2004-2006 MedTouch LLC

// Cross-browser utilities

var agt = navigator.userAgent.toLowerCase();
var is_mac = (agt.indexOf("mac") != -1);

function getByID( id, doc )
{
    if ( doc == null ) doc = 'document';

    if (eval(doc).getElementById) {
	// Newer browsers
	return eval(doc).getElementById(id);
    } else if (eval(doc).all) {
	// IE4
	return eval(doc).all[id];
    } else if (eval(doc).layers) {
	// NS4
	return eval(doc).layers[id];
    } else {
	alert( 'We couldn\'t recognize your browser.  Please use a different one or upgrade to the newest version.' );
	window.close();
    }
	return "";
}


function getXMLRequest () {
  if (window.ActiveXObject  && !is_mac) {
    return new window.ActiveXObject("Msxml2.XMLHTTP");
  } else if (window.XMLHttpRequest) {
    return new XMLHttpRequest();
  }
}

// Useful color changing functions to highlight changed data or fields that
// need to be changed

function changeColor( id, color ) {
    var tag = getByID( id );
    if (tag.style)
	tag.style.backgroundColor = color;
    else if (tag.document && tag.document.bgColor)
	tag.document.bgColor = color;
}
 
var changedColor = '#ffddee';
var unchangedColor = 'white';

function markChanged( id ) {
    changeColor('row.'+id, changedColor);
} 

function markUnchanged( id ) {
    changeColor('row.'+id, unchangedColor);
} 

// onSubmit handlers for double-click protection
 
function refuseDoubleClick()
{
        alert('It looks like this form has already been submitted.  In order to prevent duplicate or broken data, we are refusing this submission.  You may re-load this page to re-submit');
        return false;
}
 
function testDoubleClick()
{
        return confirm('It looks like this form has already been submitted. If you re-submit it it could result in duplicate, or even broken, data.  Do you wish to proceed?');
}

var doubleClickProtect = false;

// If submitted once, use one of the above functions.
function checkDoubleClick( force )
{
    if ( doubleClickProtect ) {
	if ( force ) {
	    return refuseDoubleClick();
	} else {
	    return testDoubleClick();
	}
    } else {
	doubleClickProtect = true;
	return true;
    }
}

// Utilities for making alternating colors
var colorchanger = 0;
function makeCol (Color1, Color2)
{
    colorchanger++
    if (colorchanger % 2 == 0) {
	colorchanger = 0;
	document.write ("<TR bgcolor = "+ Color1 + ">");
    } else {
	document.write ("<TR bgcolor = "+ Color2 + ">");	
	
    }
}

function lastmakeCol (Color1, Color2)
{
    if (colorchanger % 2 == 0) {
	colorchanger = 0;
	document.write ("<TR bgcolor = "+ Color1 + ">");
    } else {
	document.write ("<TR bgcolor = "+ Color2 + ">");
    }
}

// Form validation (by paulm@oho.com)

function test_item_name (item_name, thisform)
{
    return (test_item (thisform[item_name]));

}

function set_id (item_name, id, thisform)
{
    if (!thisform[item_name].id) {
	thisform[item_name].id = id;
    }

}

function test_item (item)
{
var i;
/* alert (item.type + "  " + item.name); */
    
if (item.type == "text" || item.type == "textarea" || item.type=="password") {
    if (item.value != "") {
	return true;
    } else {
	return false;
    }
} else if (item.type == "checkbox" || item.type == "radio") {
    return item.checked;
} else if (item.type == "select-one") {
    for (i = 0; i < item.length; i++) 
    {
	if (item[i].selected && item[i].value != "") {
		return true;
	}
    }
    return false;
} else {
    for (i = 0; i < item.length; i++) 
    {
	if (test_item(item[i])) {
		return true;
	}
    }
}
return false;
}

function test_items (thisform)
{
    var i;	
    var emptyitems = "";
    for (i=0; i < thisform.length; i++) {
	
//	thisform[i].id = "TEST";
	if (thisform[i].id != "" && thisform[i].id.substring(thisform[i].id.length - 2) == "rq" ) {
	    if (!thisform[i].id) {
		var j;
		var item = thisform[i];
		for (j=0; i < item.length; j++) 
		{		
		    if (item[j].id != "") 
		    {
		        if (test_item_name (item.name, thisform))
			{

			} else {
			    if (thisform[i].id) {
				emptyitems += thisform[i].id;
			    } else {
			 	emptyitems += thisform[i].name;
			    }
			    emptyitems += "\n";
		
			}
		    }
		}
	     } else {
		if (test_item_name (thisform[i].name, thisform)) {

		} else {
		    if (thisform[i].id) {
			changeColor (thisform[i].id, changedColor);
			thisform[i].onfocus=function onfocus(event) {
			    changeColor(this.id,unchangedColor);
			};
			emptyitems += thisform[i].id.substring(0, thisform[i].id.length - 2);
		    } else {
			emptyitems += thisform[i].name;
		    }
		    emptyitems += "\n";
		}
	    }
	}
    }
    if (emptyitems != "") {
	alert ("You need to enter information for the following items: \n\n" + emptyitems);
	return false;
    } else {
	return true;
    }
}

// Display the source of the document (including everything that has been
// written into it via DHTML)
// tigre@ybos.net 2004-12-17

function showCurrentSource (doc, returnWin) {
    if (!doc) doc = document;
    var newWin = window.open(null,
			     'source_view',
			     'width=800,height=600,resizable=yes,toolbar=no,location=on,scrollbars=yes,status=no');
    newWin.document.write('<code><pre>\n'+doc.body.innerHTML.replace(/</g, '&lt;')+'</pre></code>');
    if (returnWin) {
	return newWin;
    }
}

// Snagged these two functions from http://www.quirksmode.com/js/findpos.html

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
	while (obj.offsetParent)
	{
	     curleft += obj.offsetLeft
	     obj = obj.offsetParent;
	}
    }
    else if (obj.x)
	curleft += obj.x;
    return curleft;
}


function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
	while (obj.offsetParent)
	{
	     curtop += obj.offsetTop
	     obj = obj.offsetParent;
	}
    }
    else if (obj.y)
	curtop += obj.y;
    return curtop;
}
function findPosYx(obj)
{	
    var curtop = 0;
	var title = "";
    if (obj.offsetParent)
    {
	while (obj.offsetParent)
	{
	     if (title != "") {
			title += ", "
		}
	     title += obj.tagName;
		if (obj.id) {
			title += "(" + obj.id + ")";
		} else {
			if (obj.className) {
				title += "{" + obj.className + "}";	
			}
		}
	     title += " = " + obj.offsetTop;
	     curtop += obj.offsetTop;
	     obj = obj.offsetParent;
		if (!obj.offsetParent) {
			if (obj.style.position == "relative" || obj.style.position == "absolute" ) {
				
			}
		}
	}
	     curtop += obj.offsetTop;
		title += " --- " + obj.tagName;
	     title += " = " + obj.offsetTop;
	     
    }
    else if (obj.y) {
	curtop += obj.y;
	
	}
	title += " curtop = " + curtop;
	document.title = title;	
    return curtop;
}

// radioGetVal

function radioGetVal(form, fieldName) {
    var radio = form[fieldName];
    var buttons = radio.length;
    var i = 0;
    var selection;
    while (i < buttons)  {
	if (radio[i].checked) {
	    selection = radio[i].value;
	    break;
	}
	i++;
    }
    return selection;
}

// Encode the values of the fields listed in an array into a URLstring

function fieldsToQuery (arr) {
  var query = "";
  var field;
  for (var i = 0; i < arr.length; i++) {
    field = getByID(arr[i]);
    if (field.type != "checkbox" || field.checked) {
      value = field.value;
    } else {
      value = "";
    }
    if (i > 0) query += "&";
    query += encodeURIComponent(arr[i]) + "=" + encodeURIComponent(value);
  }
  return query;
}


// Flash stuff?  Who put this in?

var MM_contentVersion = 6;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
                var words = navigator.plugins["Shockwave Flash"].description.split(" ");
            for (var i = 0; i < words.length; ++i)
            {
                if (isNaN(parseInt(words[i])))
                continue;
                var MM_PluginVersion = words[i];
            }
        var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0
   && (navigator.appVersion.indexOf("Win") != -1)) {
        document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
        document.write('on error resume next \n');
        document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
        document.write('</SCR' + 'IPT\> \n');
}
// added by BD 1/20
function openNewWin(page, win_name, width, height, scrollbars, resize) {
        if(scrollbars) {
		if (resize) {
			var remote = window.open(page,win_name,"width=" + width + ",height=" + height + ",scrollbars=yes,resizable=yes,status=no,location=no");
		} else {
        	        var remote = window.open(page,win_name,"width=" + width + ",height=" + height + ",scrollbars=yes,resizable=no,status=no,location=no");
		}
	} else {
		if (resize) {
	                var remote = window.open(page,win_name,"width=" + width + ",height=" + height + ",scrollbars=no,resize=yes,status=no,location=no");
		} else {
		        var remote = window.open(page,win_name,"width=" + width + ",height=" + height + ",scrollbars=no,resize=no,status=no,location=no");
		}
        }
	remote.focus();
}
 
// added by BD 1/20
function randQuote() {
        var index = Math.floor(Math.random() * 12);
        quote = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
        document.write('<img src="/images/quotes/quote-' + quote[index] + '.gif" border="0" width="476" height="77">');
}

function showobj(objid) {
	var obj = getByID(objid);
	obj.style.visibility = '';
	obj.style.top = '';
	obj.style.left = '';
	obj.style.position = '';
}	
function hideobj(objid) {
	var obj = getByID(objid);
	obj.style.visibility = 'hidden';
	obj.style.top = '-100000px';
	obj.style.left = '-100000px';
	obj.style.position = 'absolute';
}
function toggleobj(objid) {
	var obj = getByID(objid);
	if (obj.style.visibility == 'hidden') {
		showobj(objid);
	} else {
		hideobj(objid);
	}
}	
function toggle2(objid) {
	var obj = getByID(objid);
	if (obj.style.display == "none") {
		obj.style.display = '';
	} else {
		obj.style.display = "none";
	}
}
var ua = navigator.userAgent.toLowerCase();
var isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1)); 
		
function calendar_change_boxes (type) {
	if (type == "reminder") {
		getByID('savetomypicks').style.display='none';
		if (getByID('reminder').style.display == 'none') {
			getByID('reminder').style.display = '';
			getByID('calendarboxes').className = 'setReminder';
		} else {
			getByID('reminder').style.display = 'none';
			getByID('calendarboxes').className = '';	
		}
	} else {
		getByID('reminder').style.display='none';
		if (getByID('savetomypicks').style.display == 'none') {
			getByID('savetomypicks').style.display = '';
			getByID('calendarboxes').className = 'setSavetomypicks';
		} else {
			getByID('savetomypicks').style.display = 'none';
			getByID('calendarboxes').className = '';	
		}
	
	}
}
function show_div (id) {
	getByID(id).style.display='block';
}
function hide_div(id) {
	getByID(id).style.display='none';
}
function add_my_picks (event_id) {
		xmlquick('/calendar/add-my-pick?event_id=' + event_id, 'myPicks');	
	}

function calendar_boxes_select_event (event_id) {
	if (getByID('reminder').style.display == "") {
		getByID('calendarboxes_event_id').value=event_id;
		if (getByID('calendarboxes_email').value=="") {
			alert('You need to fill out your email address first');
			getByID('calendarboxes_email').focus();
			return;
		}
		xmlpostform(getByID('calendarboxes_form'), 'remind_message', 'Saving', 0);
	} 
	if (getByID('savetomypicks').style.display == "") {
		add_my_picks(event_id);
	}
}







// Table Sorting Begin

var ts_debug_p = false;
var ts_output = "";
var ts_date = new Date();
var ts_start = ts_date.valueOf( );

var ts_style = 3;
function tableSortStyleObj(style,className,bgColor) {
	
	if (!bgColor) {
		bgColor = "";
	}
	this.style=style;
	this.className=className,
	this.bgColor=bgColor;
}


var ts_new_table_body = "";
var ts_new_table_body_array = new Array();
var ts_style_array;



function tableSort(table_id, compareFunc) {
	var table = getByID(table_id);
	
	var posY=findPosY(table);
	var posX=findPosX(table);
	

	var div = document.createElement("div");
	div.style.position = 'absolute';
	div.style.top = posY;
	div.style.left = posX;

	var mytablebody = table.getElementsByTagName("tbody")[0];
	var mytable_rows = mytablebody.getElementsByTagName('tr');
	num_rows = mytable_rows.length;

	var height = findPosY(mytable_rows[num_rows - 1]) - findPosY(table);
	height = (height * (num_rows + 1)) / (num_rows - 1);


	div.style.height = height;
	if (table.style && table.style.width) {
		div.style.width = table.style.width;
	} else {
		div.style.width = '100%';
	}

	div.style.backgroundColor = '#FFFFFF';
	div.style.opacity=".80";
	div.style.filter="alpha(opacity=80)";

	div.style.color='red';
	div.style.fontSize='30px';
	div.style.padding="25px";
	div.style.zIndex="1000";	

	div.id="ts_div_message_" + table_id;
	div.innerHTML = 'Sorting table<br />This may take some time.';
	document.body.appendChild(div);
	setTimeout("tableSort_main('" + table_id + "', " + compareFunc + ")", 100);
}

function tableSort_main(table_id, compareFunc) {
	if (ts_debug_p && ts_output == "") {
		ts_output = "";
		var d = new Date();
		ts_start = d.valueOf( );

		var d = new Date();
		var ts_time = d.valueOf( );
	
		ts_output += "Enter tableSort: " + (ts_time - ts_start) + "\n";
	}
	// table_id is an id of a table
	// compareFunc is a function that will do the comparisons
	//		Should take in two rows to compare, and return true if they should be swaped.

	var mytable = getByID(table_id);

	var mytablebody = mytable.getElementsByTagName("tbody")[0];
	var mytable_rows = mytablebody.getElementsByTagName('tr');
	num_rows = mytable_rows.length;



	if (ts_style == 1) {
		var height = findPosY(mytable_rows[num_rows - 1]) - findPosY(mytable);
		height = (height * (num_rows + 1)) / (num_rows - 1);
		mytable.oldheight = mytable.style.height;
	
		mytable.style.height = height;
	}
	var sort_array = new Array();
	var dont_sort_array = new Array(mytable_rows.length);
	ts_style_array = new Array(mytable_rows.length);


	i = 0;

	// Moves all the elements into arrays 
	// sort_array will be sorted
	// dont_sort_array will store items that shouldn't be sorted

	// ts_style_array stores style, className and bgcolor of the row.  When we swap rows around generally we want the styles to stay where they were (ie ever other row change color)

	
	if (ts_debug_p) {
		var d = new Date();
		var ts_time = d.valueOf( );
	
		ts_output += "Create Arrays: " + (ts_time - ts_start) + "\n";
	}


	for(i=0; i < mytable_rows.length; i++) {
		if (mytable_rows[i].getAttribute('no_sort_p')) {
			dont_sort_array[i] = mytable_rows[i];
		} else {
			sort_array[sort_array.length] = mytable_rows[i];
		}
		ts_style_array[i] = new tableSortStyleObj(mytable_rows[i].style.cssText, mytable_rows[i].className, mytable_rows[i].bgColor);

	}

	if (ts_debug_p) {
		var d = new Date();
		var ts_time = d.valueOf( );
		var pre_sort = ts_time;
		ts_output += "Pre Sort: " + (ts_time - ts_start) + "\n";
	}
	sort_array.sort(compareFunc);
	if (ts_debug_p) {
		var d = new Date();
		var ts_time = d.valueOf( );
	
		timeInnerSort = ts_time - pre_sort;
		ts_output += "Number of Sorts: " + sortByColumntimes + "\n";
		ts_output += "Time In Sorts Inner: " + timeInnerSort + "\n";
		ts_output += "Average Time In Sorts Inner: " + (timeInnerSort / sortByColumntimes)  + "\n";
		ts_output += "Post Sort: " + (ts_time - ts_start) + "\n";
	}



	if (ts_style != 2) {	
		ts_new_table_body = document.createElement("tbody");	
		mytable.replaceChild(ts_new_table_body, mytablebody);
	} else {
		ts_new_table_body = mytablebody;	
	}
	//mytablebody = ts_new_table_body;
	
	if (ts_debug_p) {	
		var d = new Date();
		var ts_time = d.valueOf( );
	
		ts_output += "Post Clear Rows: " + (ts_time - ts_start) + "\n";
	}
	var rows = 0;
	
	// Check to see if any "non-sorted" rows need to be added to table
	while (dont_sort_array[rows]) {
		ts_new_table_body_array[rows] = dont_sort_array[rows];
//		ts_new_table_body.appendChild(dont_sort_array[rows]);
		// Restore the style
/*
		dont_sort_array[rows].style.cssText=ts_style_array[rows].style;
		dont_sort_array[rows].className=ts_style_array[rows].className;
		dont_sort_array[rows].bgColor=ts_style_array[rows].bgColor;
*/
		rows++;
	}
	for (i = 0; i < sort_array.length; i++) {

		if (sort_array[i]) {
			// Add row to table
			ts_new_table_body_array[rows] = sort_array[i];
		//	ts_new_table_body.appendChild(sort_array[i]);
/*
			sort_array[i].style.cssText=ts_style_array[rows].style;
			sort_array[i].className=ts_style_array[rows].className;
			sort_array[i].bgColor=ts_style_array[rows].bgColor;
*/
			rows++;
				// Check to see if any "non-sorted" rows need to be added to table
			while (dont_sort_array[rows]) {
				ts_new_table_body_array[rows] = dont_sort_array[rows];
			//	ts_new_table_body.appendChild(dont_sort_array[rows]);
/*				dont_sort_array[rows].style.cssText=ts_style_array[rows].style;
				dont_sort_array[rows].className=ts_style_array[rows].className;
				dont_sort_array[rows].bgColor=ts_style_array[rows].bgColor;
*/
				rows++;
			}
		}
	}
	
	

//	alert(ts_new_table_body_array[1]);
	for (var i = 0; i < ts_new_table_body_array.length; i++) {
		setTimeout("add_row(" + i + ")", i * 2);	
	}
//	mytable.appendChild(ts_new_table_body);
	if (ts_debug_p) {	
		var d = new Date();
		var ts_time = d.valueOf( );
	
		ts_output += "End Sort: " + (ts_time - ts_start) + "\n";
	}
//	setTimeout("post_ts('" + table_id + "')", 0);

}
function post_ts (table_id) {
	var table = getByID(table_id);
	if (ts_style == 1) {	
		table.style.height = table.oldheight;	
	}
	var div_id="ts_div_message_" + table_id;
	var div = getByID(div_id);

	div.parentNode.removeChild(div); 
	
	if (ts_debug_p) {
		var d = new Date();
		var ts_time = d.valueOf( );
		ts_output += "Post Rendering: " + (ts_time - ts_start) + "\n";
		alert(ts_output);
		ts_output = "";
	}
	ts_new_table_body = "";
	ts_new_table_body_array = new Array();
	ts_style_array = ""

}

function add_row(row_id) {

//	console.log(row_id);
	ts_new_table_body_array[row_id].style.cssText=ts_style_array[row_id].style;
        ts_new_table_body_array[row_id].className=ts_style_array[row_id].className;
        ts_new_table_body_array[row_id].bgColor=ts_style_array[row_id].bgColor;	
	ts_new_table_body.appendChild(ts_new_table_body_array[row_id]);
	if (row_id == ts_new_table_body_array.length - 1) {
		setTimeout("post_ts('" + ts_new_table_body.parentNode.id + "')", 0);
	}
}

// Table Sorting End


function getObjElementById(obj, element_id, type) {
	var elements = obj.getElementsByTagName(type);
	for (i = 0; i < elements.length; i++) {
		if (elements[i].id== element_id) {
			return elements[i];
		}
	}
}



// Table Sorting Default Sorts





var default_tableSortHistory = new Array();
ts_debug_p = false;
function default_tableSortHistoryObject_create (key, reverse_p, pre_loaded_p) {
	this.key = key;
	this.reverse_p = reverse_p;
	this.preloaded_p = pre_loaded_p;
	
}

/*
var new_History = new default_tableSortHistoryObject_create('att_first_names', false, true);	
default_tableSortHistory[default_tableSortHistory.length]=new_History;

new_History = new default_tableSortHistoryObject_create('att_last_name', false, true);	
default_tableSortHistory[default_tableSortHistory.length]=new_History;

new_History = new default_tableSortHistoryObject_create('att_company', false, true);	
default_tableSortHistory[default_tableSortHistory.length]=new_History;

new_History = new default_tableSortHistoryObject_create('ticket_type', false, true);	
default_tableSortHistory[default_tableSortHistory.length]=new_History;

new_History = new default_tableSortHistoryObject_create('event_name', false, true);	
default_tableSortHistory[default_tableSortHistory.length]=new_History;
*/
 function hasAttribute(attribute,obj) {

          var out=false,counter=0;

          if(obj.attributes.length) {

               for(counter=0; counter<obj.attributes.length; counter++) {

                    if(obj.attributes[counter].name==attribute) {

                         out=true;

                         break;
                    }
               }
          }

          return out;
     }

function default_tableSort(table_id, col_id) {
	if (ts_debug_p && ts_output == "") {
		ts_output = "";
		var d = new Date();
		ts_start = d.valueOf( );

		var d = new Date();
		var ts_time = d.valueOf( );
	
		ts_output += "Enter tableSort: " + (ts_time - ts_start) + "\n";
	}
	var last = "";
	var reverse_p = false;
	var new_p = true;
	if (default_tableSortHistory.length > 0 && default_tableSortHistory[0].preloaded_p) {
		for (var i = 0; i < default_tableSortHistory.length; i++) {
			last = default_tableSortHistory[i].key;
			reverse_p = default_tableSortHistory[i].reverse_p;
			preloaded_p = default_tableSortHistory[i].preloaded_p;
			if (preloaded_p) {
				default_pretableSort(table_id, last);
				default_tableSortHistory[i] = new default_tableSortHistoryObject_create(last, reverse_p, false);
			}	
		}
	}
	for (var i = 0; i < default_tableSortHistory.length; i++) {	
		last = default_tableSortHistory[i].key;
		reverse_p = default_tableSortHistory[i].reverse_p;
		while (last == col_id && i < default_tableSortHistory.length) {
			
			new_p = false;
			for (j = i; j < default_tableSortHistory.length - 1; j++) {
				default_tableSortHistory[j] = default_tableSortHistory[j+1]
				
		
			}
			if (i < default_tableSortHistory.length) {
				last = default_tableSortHistory[i].key;
				reverse_p = default_tableSortHistory[i].reverse_p;		
				default_tableSortHistory.pop();
			}
		}
	}
	if (new_p) {
		if (ts_debug_p) {	
			var d = new Date();
			var ts_time = d.valueOf( );
	
			ts_output += "Pre default_pretableSort: " + (ts_time - ts_start) + "\n";
		}
		
		default_pretableSort(table_id, col_id);		
		
		if (ts_debug_p) {	
			var d = new Date();
			var ts_time = d.valueOf( );
	
			ts_output += "Post default_pretableSort: " + (ts_time - ts_start) + "\n";
		}
	}
	if (last == col_id && !reverse_p) {
		var new_History = new default_tableSortHistoryObject_create(col_id, true, false);
	} else {
		var new_History = new default_tableSortHistoryObject_create(col_id, false, false);
	}
	default_tableSortHistory[default_tableSortHistory.length]=new_History;
	if (ts_debug_p) {
		var output = "";

		for (var i = 0; i < default_tableSortHistory.length; i++) {
			key = default_tableSortHistory[i].key;
			reverse_p = default_tableSortHistory[i].reverse_p;
			output += "key: " + key + " -- reverse_p: " + reverse_p	+ "\n";
		}
		
		if (window.console) {
			console.log(output);
		} else {
			output = output.replace(/\n/gi, "\\n");
			setTimeout("throw Error('" + output + "')", 0);
		}
	} 

	
	tableSort(table_id, sortByColumn);

}

function default_pretableSort_row(table_id, col_id, row1) {
	var row1_id = row1.id;
			var cell1 = "";
			
				var cell1_id = row1_id.replace("row", "cell") + "_" + col_id;
				cell1 = eval("row1.cell_" + col_id + "= getObjElementById(row1, cell1_id, 'td')");
				var cell1_value = cell1.getAttribute('sort_value');
			if (!cell1_value) {
			       if (cell1.hasAttribute) {
					if (!cell1.hasAttribute('sort_value')) {
						cell1_value = cell1.innerHTML.toLowerCase().replace(/\s+$/, '');
						cell1_value = cell1_value.replace(/^\s+/, '');	
					} 
				} else {
					if (cell1_value != "" && cell1.innerHTML != "" && !hasAttribute('sort_value' , cell1)) {
						cell1_value = cell1.innerHTML.toLowerCase().replace(/\s+$/, '');
						cell1_value = cell1_value.replace(/^\s+/, '');
					
					}
				} 
			}
			if (cell1_value == "") {
				cell1_value = "zzzzzzzzzzzzzzzzzzzzzzzzzzzz";
			}
		//	console.log(cell1_value);
			cell1.sort_value = cell1_value;
}


function default_pretableSort(table_id, col_id) {
	var mytable = getByID(table_id);
//	alert(col_id);
	var mytablebody = mytable.getElementsByTagName("tbody")[0];
	var mytable_rows = mytablebody.getElementsByTagName('tr');
	for(var i=0; i < mytable_rows.length; i++) {
		if (mytable_rows[i].getAttribute('no_sort_p') != 'true') {
			row1 = mytable_rows[i];
			default_pretableSort_row(table_id, col_id, row1);
		}
	}
	
		
}
var sortByColumntimes = 0;
function sortByColumn(row1, row2) {
	var d = new Date();
	var ts_time = d.valueOf( );
	
//	ts_output += "Enter sortByColumn: " + (ts_time - ts_start) + "\n";
	
	return sortByColumn_inner(row1, row2, default_tableSortHistory.length - 1);
}


var timeInnerSort = 0;
function sortByColumn_inner(row1, row2, history) {
	sortByColumntimes++;
	
	col_id = default_tableSortHistory[history].key;
	reverse_p = default_tableSortHistory[history].reverse_p;

		var cell1_value = eval("row1.cell_" + col_id).sort_value;
	
		var cell2_value = eval("row2.cell_" + col_id).sort_value;
		
	
	var return_val = ""
	if (cell1_value == cell2_value) {
		
		if (history == 0) {
			return_val =  0;
		} else {
			return_val =  sortByColumn_inner(row1, row2, history - 1);
		}		
	} else if (cell1_value < cell2_value) {
		//	
		if (reverse_p) {
			return_val = 1;
		} else {
			return_val = -1
		}
	} else {
		if (reverse_p) {
			return_val = -1;
		} else {
			return_val = 1;
		}
	}
//	console.log("-" + cell1_value + "- vs -" + cell2_value + "- = " + return_val);
//	setTimeout("throw Error('" + cell1_value + " vs " + cell2_value + " = " + return_val + "')", 0);	

	return return_val;
}

