
	/** preload the loader image */
	var loaderImage = new Image();
	loaderImage.src = 'media/js/AJAXTable/imglib/loader.gif';
	
	
	/*
	 * flip to the specified page of the ajax table
	 * @param	String tableid the id of the table 
	 * @param	Integer page the page number
	*/ 
	function ajaxTable_loadPage(tableid, page)
	{
		// save the current state of the table to refresh later
		window[tableid+"page"] = page;
		
		// build the query string
		var querystring = '&p='+page;
		
		// send the post via AJAX
		ajaxTable_call(tableid, querystring);
	
	}
	
	
	/*
	 * changes the specified ajax table sorting algorithm
	 * @param	String tableid the id of the table we're loading
	 * @param	String sortfield the field to sort by
	 * @param	String sortorder the order to sort by
	*/ 
	function ajaxTable_sort(tableid, sortfield, sortorder)
	{
		var querystring = "&sf="+sortfield;
		querystring += "&so="+sortorder;
		querystring += "&p=1";
		
		ajaxTable_call(tableid, querystring);
	}
	
	
	/*
	 * Displays table in 'loading' mode 
	 * @param	String tableid the id of the table we're loading
	*/ 
	function ajaxTable_loading(tableid)
	{
		// show processing indication to user
		var headingCell = findDOM(tableid+"Header");
		headingCell.getElementsByTagName('td')[1].innerHTML = "<img src=\"media/js/AJAXTable/imglib/loader.gif\" class=\"ajaxtable_reload_image\" alt=\"Reload Table\" align=\"absmiddle\" title=\"Reload Table\" /> Loading....";
		
		var tbody = findDOM(tableid+"_tbody");
		if(tbody)
		{
			trs = tbody.getElementsByTagName('tr');
			for(var i=0; i<trs.length; i++)
			{
				trs[i].className += ' transparent';
			}
		}
		
		//disable form elements
		var table = findDOM(tableid+"DataDiv");
		var fe = table.getElementsByTagName('input');
		if(fe)
		{
			for(i=0;i<fe.length; i++)
			{
				fe[i].onclick = '';
				fe[i].onchange = false;
				fe[i].disabled = 'true';
			}
		}
		var selects = table.getElementsByTagName('select');
		if(selects)
		{
			for(i=0;i<selects.length; i++)
			{
				selects[i].onchange = false;
				selects[i].disabled = 'true';
			}
		}
		
		/** disable links */
		var a = table.getElementsByTagName('a');
		if(a)
		{
			for(i=0;i<a.length;i++)
			{
				a.href = 'javascript:;';
				a.onclick = function(){
					return false;
				}
			}
		}
	}
	
	function ajaxTable_finishedLoading(tableid)
	{
		var headingCell = findDOM(tableid+"Header");
		headingCell.getElementsByTagName('td')[1].innerHTML = "<img style=\"cursor:pointer;\" class=\"ajaxtable_reload_image\" alt=\"Reload Table\" align=\"absmiddle\" title=\"Reload Table\" src=\"media/js/AJAXTable/imglib/loader_static.gif\" onclick=\"ajaxTable_refresh('"+tableid+"');\" /> "+window[tableid+"Title"];
	}
	
	
	/*
	 * grabs the specified ajax table's content via ajax and injects it into the table
	 * @param	String tableid the id of the table we're loading
	 * @param	String querystring 
	*/ 
	function ajaxTable_call(tableid, querystring)
	{
		/** if an onSearchStart callback has been set, run it here */
		if(window[tableid+"_onRequestStart"])
		{
//			window[tableid+"_onRequestStart"](tableid);
		}
		// get the table data div
		var filltablediv = findDOM(tableid+"DataDiv");
		
		ajaxTable_loading(tableid);
		
		// create an ajax object 
		var ajax = new SPAjax();
		
		// define the handler function to process the response from the server
		var handler_function = function( response_text )
		{
			//do all the processing with the response to build the table
			filltablediv.innerHTML = response_text;
			ajaxTable_finishedLoading(tableid);
				
			/** if an onRequestComplete callback has been set, run it here */
			if(window[tableid+"_onRequestComplete"])
			{
				window[tableid+"_onRequestComplete"](tableid, response_text);
			}
		}
		
		ajax.doGet(window[tableid+"PostUrl"] + querystring, handler_function);
		
	}
	
	//Refreshes the ajax table data at its current page, sortorder & search
	function ajaxTable_refresh(tableid)
	{
		ajaxTable_call(tableid, "&r=true");
	}
	
	
	/*
	 * call a url via ajax, after optionally accepting a confirmation message, then refresh the table and optionally echo a response
	 * @param	String tableid the id of the table we're refreshing
	 * @param	String msg the "are you sure?" message, or false if none
	 * @param	String url the url to call
	 * @param	Boolean acceptresponse if true, anything echoed by the function will be returned and shown as a message at the top of the table
	*/ 
	function ajaxTable_ajaxAction_confirm(tableid, msg, url, acceptResponse)
	{
		if(msg == false || confirm(msg))
		{
			ajaxTable_loading(tableid);
			
			// create an ajax object 
			var ajax = new SPAjax();
			
			var handler_function = function(response_text) 
			{
				ajaxTable_refresh(tableid);
				
				if(acceptResponse && response_text)
				{
					ajaxTable_showMessage(tableid+"Message", response_text);
				}
			}
			
			ajax.doGet(url+'&l=l_passthru', handler_function);
		}
	}
	
	
	/*
	 * show a message at the top of the ajax table, which appears then fades
	 * @param	String div the div to show the message in
	 * @param	String message the message to show
	*/ 
	function ajaxTable_showMessage(div, message)
	{
		if(!window['messageid']) window['messageid'] = 0;
		window['messageid'] ++;
		message = '<div style="" id="ajaxMessage'+window['messageid']+'">'+message+'</div>';
		var messageCell = findDOM(div);
		messageCell.innerHTML = message+messageCell.innerHTML;
		messageCell.style.display = "block";
		ajaxTable_message_fade('ajaxMessage'+window['messageid'], 100, 0, 4000);
	}
	
	
	/*
	 * set the opacity of the specified div
	 * @param	Integer opacity (0 to 100)
	 * @param	String the div id
	*/ 
	function changeOpac(opacity, id)
	{
	    var object = document.getElementById(id).style;
	    object.opacity = (opacity / 100);
	    object.MozOpacity = (opacity / 100);
	    object.KhtmlOpacity = (opacity / 100);
	    object.filter = "alpha(opacity=" + opacity + ")";
	    if (opacity == 0)
	    {
	    	object.display = "none";
	    }
	}
	
	
	/*
	 * fades the specified div from one opacity to another over a specified time frame
	 * @param	String div id
	 * @param	Integer starting opacity
	 * @param	Integer ending opacity
	 * @param	Integer time to fade (milliseconds)
	*/ 
	function ajaxTable_message_fade(id, opacStart, opacEnd, millisec) 
	{
	    //speed for each frame
	    var speed = Math.round(millisec / 100);
	    var timer = 0;
	
	    //determine the direction for the blending, if start and end are the same nothing happens
	    if(opacStart > opacEnd) 
	    {
	        for(i = opacStart; i >= opacEnd; i--) 
	        {
	            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
	            timer++;
	        }
	    }
	    else if(opacStart < opacEnd) 
	    {
	        for(i = opacStart; i <= opacEnd; i++)
	        {
	            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
	            timer++;
	        }
	    }
	}

	
	/*
	 * search the specified table for the specified string
	 * @param	String tableid
	 * @param	String string
	*/ 
	function ajaxTable_search(tableid, str)
	{
		/** remove double quotes from search.  they're not needed and cause a world of hurt */
		str = str.replace('"', '');
		
		if(window[tableid+"searchtimeout"] !== 0)
		{
			clearTimeout(window[tableid+"searchtimeout"]);
			window[tableid+"searchtimeout"] = 0;
		}
		
		window[tableid+"searchtimeout"] = setTimeout('ajaxTable_call("'+tableid+'", "&f='+str+'");', 500);
	}
	
	
	/*
	 * change the specified AJAX Table's sorting algorithm
	 * @param	String tableid
	 * @param	String newsort
	*/ 
	function ajaxTable_change_sort(tableid, newsort)
	{
		window[tableid+"newsort"] = newsort;
		ajaxTable_refresh(tableid);
	}
	
	/**
	 * Specify a callback function to call when a request is initiated 
	 * @param	String tableid
	 * @param	Function func
	 */
	function ajaxTable_setOnRequestStart(tableid, func)
	{
		window[tableid+"_onRequestStart"] = func;
	}
	
	/**
	 * Specify a callback function to call when a request is completed 
	 * @param	String tableid
	 * @param	Function func
	 */
	function ajaxTable_setOnRequestComplete(tableid, func)
	{
		window[tableid+"_onRequestComplete"] = func;
	}