// This javascript contains functions for doing various cleanup tasks

var SAVE_TIMEOUT = 10;
var ajaxpath = "/community/ajax.cfm";




/**********************************************************************************************************************
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:		refreshcaptcha
Purpose:		Allow the user to refresh a captcha image on the screen dynamically
Parameters:		suffix		(string)		The suffix of the fields that will be addressed
											seed#suffix#:		The hidden form field that holds the seed
											_ct#suffix#:		The hidden form field that holds the call time
											captch#suffix#:		The text field the user types in to match the captcah
**********************************************************************************************************************/
function refreshcaptcha(suffix, autofocus) {
	var width = 200;
	var height = 40;
	var seed = Math.random();
	var _ct = new Date().getTime();		// make sure we pass in a new value so the captcha image isn't cached
	seed = Math.ceil(seed * 100000);		// generate a random seed for our captcha image between 1 and 100000
	$("#seed"+suffix).val(seed);
	$("#_ct"+suffix).val(_ct);
	$("#captcha"+suffix).val("");
	if (autofocus) {
		$("#captcha"+suffix).focus();
	}
	$("#divCAPTCHA"+suffix).html("<img width=\"" + width + "\" height=\"" + height + "\" src=\"/images/captcha.cfm?width=" + width + "&height=" + height + "&seed=" + seed + "&_ct=" + _ct + "\" />");
}


/**********************************************************************************************************************
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 ListFind(l,v,d){
	l += ""; // cheap way to convert to a string
	if(!d){d = ",";}
	var r = 0;
	var listToArray = l.split(d);
	for (var i=0; i < listToArray.length; i++){
		if (listToArray[i] == v){
			r = i + 1;
			break;
		}
	}
	return r;
}

function ListLen(l,d){
	l += ""; // cheap way to convert to a string
	if(!d){d = ",";}
	if(l.length){return l.split(d).length;}
	return 0;
}

function ListAppend(l, v, d){
	l += ""; // cheap way to convert to a string
	if(!d){d = ",";}
	var r = "";
	if (this.ListLen(l)){
		r = l + d + v;
	} else {
		r = v;
	}
	return r;
}

function ListGetAt(l, p, d){
		l += ""; // cheap way to convert to a string
		if(!d){d = ",";}
		return l.split(d)[p - 1];
	}

function ListDeleteAt(l, p, d){
		l += ""; // cheap way to convert to a string
		if(!d){d = ",";}
		var i,posInList;
		var posInArray = p - 1;
		var thisD 	= "";
		var r = "";
		for(i = 0; i < l.split(d).length; i++){
			if (i != posInArray){
				posInList = i + 1;
				if (r.length){
					thisD 	= d;
				}
				r += thisD + this.ListGetAt(l, posInList, d);
			}
		}
		return r;
	}


//global stuff
var http;
if (window.XMLHttpRequest) {
	http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	http = new ActiveXObject("Microsoft.XMLHTTP");
}


function httpStateChange()
{
	var o = getObj("divColumns");
	if (http.readyState == 4 && o) {
		try {
			o.innerHTML = http.responseText;
		}
		catch (e) { window.status = e.message; }
	}
}



document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};


var query_string= new Array(); 
if (location.search){ 
	var vals=location.search.substr(1).split("&");
	var item;
	
	for (var i in vals) { 
		
		if (vals[i].toString().substr(0,8) != 'function') {
			item = vals[i]
			vals[i] = vals[i].replace(/\+/g, " ").split("="); 
			// if this is target parameter, then we want to take all the value after it because taregt parameter could contains & and = in its value
			if (vals[i][0] == 'target') {
				query_string[unescape(vals[i][0])] = unescape(location.search.substr(location.search.search(/target/i)).replace(vals[i][0]+'=',""));
			}
			else query_string[unescape(vals[i][0])] =unescape(item.replace(vals[i][0],"").substr(1)); 
		}
	}
}

trackImgObject = function(imgID,src){
		$.post("/trackImages/_index.cfm",{   
		       objectValue  : imgID,  
		       filePath     : src
		     });  
}

var toggleContent = function(classname,id,onlabel,offlabel) {
	if ($('.'+classname).css('display')=='none') { 
		$('.'+classname).show();
		if (offlabel!=null) $('#'+id).html(offlabel);
	} else { 
		$('.'+classname).hide();
		if (onlabel!=null) $('#'+id).html(onlabel);
	}
}

var FormSubmitManual = function (id,method,action) {
	var sAction = location.href;
	var sMethod = "POST";
	
	
	if ($('#'+id).get(0)!=null) {
		
		if (action!=null) sAction = action;
		else sAction = $('#'+id).attr('action');
		if (method!=null) sMethod = method;
		else sMethod = $('#'+id).attr('method');
		
		$('#'+id).attr('action',sAction);
		$('#'+id).attr('method',sMethod);
		$('#'+id).submit();
	}
}
