// This javascript contains functions for doing various cleanup tasks


/**********************************************************************************************************************
Function:		replace
Purpose:		Replaces all occurances of one substring with another substring in a string.
Parameters:		string		(string)		The string which is having its text replaced
				text		(string)		The text to search for, which is being replaced
				by			(string)		The string that is replacing the search text
**********************************************************************************************************************/
function replace(string, text, by) {
	// Replaces all occurances of text with by in string
    
	var strLength = string.length;
	var txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) {
		return string;
	}
    
	var i = string.indexOf(text);
	if ((!i) && (text != string.substring(0,txtLength))) {
		return string;
	}
    if (i == -1) {
		return string;
	}
    
	var newstr = string.substring(0,i) + by;
    if (i+txtLength < strLength) {
        newstr += replace(string.substring(i+txtLength,strLength),text,by);
	}

    return newstr;
}


/**********************************************************************************************************************
Function:		getFileFromPath
Purpose:		Given a full path, the filename portion of the path alone is returned.
Parameters:		fullpath	(string)		The full path to the file
**********************************************************************************************************************/
function getFileFromPath(fullpath)
{
	fullpath = replace(fullpath, "\\", "/");
	var i = fullpath.indexOf("/");
	while (i > 0) {
		fullpath = fullpath.substring(i+1, fullpath.length);
		i = fullpath.indexOf("/");
	}
	return fullpath;
}


/**********************************************************************************************************************
Function:		getObj
Purpose:		Assuming a version 5 browser or higher, will return a reference to an object of the specified name
Parameters:		targetName	(string)		Name of the target object
**********************************************************************************************************************/
function getObj(targetName)
{
	var target = null;
	if (document.getElementById) { // NS6+
		target = document.getElementById(targetName);
	} else if (document.all) { // IE4+
		target = document.all[targetName];
	}
	
	return target;
}


/**********************************************************************************************************************
Function:		fillObj
Purpose:		Fills a container with HTML by using XMLHTTP
Parameters:		coming soon, in test right now
**********************************************************************************************************************/

//global stuff
var http;
if (window.XMLHttpRequest) {
	http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	http = new ActiveXObject("Microsoft.XMLHTTP");
}

function fillObj(id)
{
	var d = new Date();
	http.open("GET", "/utils/fillcolumn.cfm?id=" + id + "&time=" + d.getTime(), false);	// we pass the time so that we won't get a cached page result
	http.onreadystatechange = httpStateChange;
	try {
		http.send(null);
	}
	catch (e) {
		strErrorMsg = 'Error:\t' + e.name;
		strErrorMsg += '\n\t' + e.message;
		strErrorMsg += '\n\t' + e.number;
		strErrorMsg += '\n\t' + e.description;
		alert(strErrorMsg);
	}
}

void function httpStateChange()
{
	var o = getObj("divColumns");
	if (http.readyState == 4 && o) {
		try {
			o.innerHTML = http.responseText;
		}
		catch (e) { window.status = e.message; }
	}
}

