/*
 * IO_OnInit ()
 * ----------------
 * This function should be called as an onLoad event handler.
 */
function IO_Init ()
{
	//alert ("IO_Init()");

	return true;
}

/*
 * Initialize all of the Querystring stuff we need.
 */
function QueryString(key)
{
	//alert ("QueryString()");
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
	//alert ("QueryString_Parse()");
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}

}
QueryString_Parse();

/*
 * clinkit
 * -----------
 * This function runs the regexp necessary to make things BufferedLink save.
 *
 * Thanks Heather!
 */
function clinkit (v, iframeName, divName, callback) {
	var clinkit = v.match(/href=(["])[^"']*\1[^>\/]*>/ig);
	var buff = v;
	
	if (!clinkit)
	{
		return buff;
	}

	/* This will allow for hyperlink support */
	for (x = 0; x < clinkit.length; x++)
	{
		var q = clinkit[x].toString().indexOf("\"");
		var q2 = clinkit[x].toString().indexOf("\"",q+1);
		var url = clinkit[x].toString().substr(q,q2-q+1).replace(/href=|([">\s])/ig,"");
		if (url != "#")
		{
			var tbuff = clinkit[x].replace(url,"#").toString().replace(/>/," onClick=\"return BufferedLink('"+iframeName+"','"+divName+"','"+url+"','"+callback+"');\">");
			buff = buff.replace(clinkit[x],tbuff);
		}
	}
	/* Thus far we cannot support any javascript in the buffered content.
		It must all reference parent namespace or it will break */
	
//	alert(buff);

	return buff;
}

/*
 * LoadRemoteFileIntoDiv (string, string, string, callback)
 * -------------------------------------------------
 * Arguments:
 *    1) The ID value for the iframe to use as a buffer
 *
 *    2) The ID value for the div to use to view the buffer
 *
 *    3) The URL for the file to load into the buffer to use in the div
 *
 *		4) The name of the callback function to use when the div has been loaded (see note about callbacks)
 *
 * Important Notes
 *    + It is *VERY* important to make sure that ALL pages loaded by this
 *      buffering function have a body onLoad attribute that will at some
 *      time make a call to BufferFrame_OnLoad() with no argument.
 *
 *    + It is also very important to make sure that any scripts you may be
 *      calling through this method will not be using the divName key on the
 *      query string.  This buffering method uses that value to tell the
 *      BufferFrame_OnLoad() function which div to insert the data into.
 *
 *		+ If you wish to have a callback, it is *VERY* important to pass the name
 *		  of the callback.  This callback may *NOT* take any arguments.
 */
function LoadRemoteFileIntoDiv (iframeName, divName, fileName, callback)
{
	//alert ("LoadRemoteFileIntoDiv()");
	var myDiv = null;
	var myFrame = null;
	var qsModifiers = "divName="+divName+"&iframeName="+iframeName+"&callback="+callback;

	if (document.all)
	{
		myDiv = document.all[divName];
		myFrame = document.all[iframeName];
	}
	else
	{
		myDiv = document.getElementById (divName);
		myFrame = document.getElementById (iframeName);
	}


	if (fileName.indexOf ("?") != -1)
	{
		fileName += "&"+qsModifiers;
	}
	else
	{
		fileName += "?"+qsModifiers;
	}

	window.frames["reviewBuffer"].location = fileName;
	
	return true;
}

/*
 * BufferFrame_OnLoad ()
 * -------------------------
 * This function should be called by any frame that is called by
 * LoadRemoteFileIntoDiv().  This function returns an eval of the callback.
 */
function BufferFrame_OnLoad ()
{
	//alert("BufferFrame_OnLoad()");
	var divName = QueryString("divName");
	var iframeName = QueryString("iframeName");
	var callback = QueryString("callback");
	var myDiv = null;
	var inLinkerator = /href="([^\w \- \. , \']+)"/;
	var buff = null;
	var replacement = null;
	
	if (document.all)
	{
		myDiv = parent.document.all[divName];
	}
	else
	{
		myDiv = parent.document.getElementById (divName);
	}

	buff = document.body.innerHTML;
	
	buff = clinkit (buff, iframeName, divName, callback);
//	alert ("buff == " + buff);
	myDiv.innerHTML = buff;
	// Reset scrolling to 0x0
	ResetScroll (myDiv);

	if (callback.indexOf ("(") != -1)
	{
		alert ("Invalid callback!");
		return false;
	}
	else
	{
		callback = "parent."+callback+"()";
	}
	
	var toReturn = eval (callback);
	
	return toReturn;
}

function BufferedLink (iframeName, divName, fileName, callback)
{
	//alert ("BufferedLink()");
	if (fileName == "#")
	{
		return false;
	}

	//alert ("BufferedLink('"+iframeName+"','"+divName+"','"+fileName+"');");
	return LoadRemoteFileIntoDiv (iframeName, divName, fileName, callback);
}

function ResetScroll (obj)
{
	obj.scrollTop = 0;
	obj.scrollLeft = 0;
	
	return true;
}

function getCookie (doc, name)
{
	var search = name + "=";

	if (doc.cookie.length > 0)
	{
		offset = doc.cookie.indexOf (search);
		if (offset != -1)
		{
			offset += search.length;
			end = doc.cookie.indexOf (";", offset);
			if (end == -1)
			{
				end = doc.cookie.length;
			}

			return unescape(doc.cookie.substring (offset, end));
		}
	}

	return null;
}

function setCookie (doc, name, value, expires)
{
	doc.cookie = name + "=" + escape(value) +
		((expires == null) ? "" : ("; expires=" + expires));
	
	return true;
}

