function fileSuffix(imgName,suffix,addRemove) {
	var theImgParts = imgName.split("/");
	var theImgName = theImgParts[theImgParts.length-1];
	var theImgNameParts = theImgName.split(".");
	if (theImgNameParts.length > 1) {
		if (addRemove=='add') {
			//if suffix is not alread on the end of the image
			if (theImgNameParts[theImgNameParts.length-2].indexOf(suffix) == -1)
				theImgNameParts[theImgNameParts.length-2] = theImgNameParts[theImgNameParts.length-2] + suffix;
		} else {
			var theImgNamePartsParts = theImgNameParts[theImgNameParts.length-2].split(suffix);
			theImgNameParts[theImgNameParts.length-2] = theImgNamePartsParts.join("");
		}
	}
	theImgName = theImgNameParts.join(".");
	theImgParts[theImgParts.length-1] = theImgName;
	theFullImgName = theImgParts.join("/");
	return theFullImgName;
}
function preloadImg(theImg) {
	var theFullImgName = fileSuffix(theImg.src,"_on",'add');
	var preloadImg = new Image();
	preloadImg.src = theFullImgName;
}
function swapImg(theImg,overOut) {
	if (typeof(theImg) == "object") {
		theImg.src = (overOut=="over") ? fileSuffix(theImg.src,"_on","add") : fileSuffix(theImg.src,"_on","remove");
	}
}

function swapImgId(idName, overOut) {
	swapImg(fixElement(idName), overOut);
}

function NewWindow(url, windowName, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
	win = window.open(url, windowName, winprops);
	if (parseInt(navigator.appVersion) >= 4) { 
		win.window.focus(); 
	}
}

function in_array(haystack,needle) {
	var foundIt = false;
	flatStack = "|:|" + haystack.join("|:|") + "|:|";
	if (flatStack.indexOf("|:|" + needle + "|:|") > -1) foundIt = true;
	return foundIt;
}

function IndexOf (needle, haystack) {
	if (!haystack.indexOf) {
		haystack.prototype.indexOf = function (needle, start) {
			for (var i = (start || 0); i < this.length; i++) {
				if (this[i] == needle) {
					return i;
				}
			}
			return -1;
		}
	}
	
	return haystack.indexOf(needle);
}

function deleteEmptyArrayVals(oldArr) {
	newArr = new Array();
	for (i=0;i<oldArr.length;i++) {
		if (oldArr[i] != "") {
			newArr[newArr.length] = oldArr[i];
		}
	}
	return newArr;
}

function assembleUrl(urlParts) {
	url = "";
	url += urlParts["pageParts"].join("/");
	if (urlParts["server"]) {
		if (!urlParts["protocol"]) urlParts["protocol"] = "http";
		url = urlParts["protocol"] + "://" + urlParts["server"] + "/" + url;
	}
	queryString = "";
	queryStringParts = urlParts["queryStringParts"];
	for (var key in queryStringParts) {
		if (queryString) queryString += "&";
		queryString += key + "=" + queryStringParts[key];
	}
	if (queryString) url += "?" + queryString;
	return url;
}

function dissectUrl(urlString) {
	var urlParts = new Array();
	urlString = urlString.toString();
	//protocol
	if (urlString.indexOf("://") > -1) firstSplit = urlString.split("://");
	if (typeof(firstSplit) != "undefined") {
		urlParts["protocol"] = firstSplit[0];
		urlString = firstSplit[1];
	}
	//query string
	if (urlString.indexOf("?") > -1) secondSplit = urlString.split("?");
	var queryStringVars = new Array();
	if (typeof(secondSplit) != "undefined") {
		queryString = secondSplit[1];
		queryStringPairs = queryString.split("&");
		for (i=0;i<queryStringPairs.length;i++) {
			queryStringPairParts = queryStringPairs[i].split("=");
			qsKey = queryStringPairParts[0];
			qsVal = queryStringPairParts[1];
			queryStringVars[qsKey] = qsVal;
		}
		urlString = secondSplit[0];
	}
	urlParts["queryStringParts"] = queryStringVars;
	//server & pages
	if (urlString.indexOf("/") > -1) thirdSplit = urlString.split("/");
	if (typeof(thirdSplit) != "undefined") {
		pageParts = new Array();
		for (i=1;i<thirdSplit.length;i++) pageParts[pageParts.length] = thirdSplit[i];
		urlParts["pageParts"] = pageParts;
		urlParts["server"] = thirdSplit[0];
	} else {
		urlParts["page"] = urlString;
	}
	return urlParts;
}

function queryStringVar(url,varKey,varVal) {
	urlParts = url.split("?");
	urlPage = urlParts[0];
	queryVars = new Array();
	if (urlParts.length > 1) {
		queryString = urlParts[1];
		queryPairs = queryString.split("&");
		for (i=0;i<queryPairs.length;i++) {
			queryVars[queryPairs[i].split("=")[0]] = queryPairs[i].split("=")[1];
		}
	}
	if (varKey) queryVars[varKey] = varVal;
	returnUrl = urlPage;
	queryString = "";
	for (var key in queryVars) {
		if (queryString != "") queryString += "&";
		queryString += key + "=" + queryVars[key];
	}
	if (queryString) returnUrl += "?" + queryString;
	return returnUrl;
}

function splitQueryVars(queryString) {
	var queryVars = new Array();
	queryPairs = queryString.split("&");
	for (i=0;i<queryPairs.length;i++) {
		queryVars[queryPairs[i].split("=")[0]] = queryPairs[i].split("=")[1];
	}
	return queryVars;
}


//2011-07
//condensed UI preferences toggle, hopefully condensed into a single cookie; does not affect images or plus/minus
//see Util.php for php utilization
function recordUIOption (optionname, optionvalue) {
	var cv = getCookie("uiopt");
	if (cv) {
		//get the settings
		cvextracted = cv.split("|");
		cvarray = [];
		for (i = 0; i < cvextracted.length; i++) {
			x = cvextracted[i].split("=");
			cvarray[x[0]] = x[1];
		}
		cvarray[optionname] = optionvalue;
		//save the setting
		newcvarray = [];
		for (var key in cvarray) {
			newcvarray.push(key + "=" + cvarray[key]);
		}
		newcv = newcvarray.join("|");
	}
	else {
		newcv = optionname + "=" + optionvalue;
	}
	//alert("uiopt: " + newcv);
	setCookie("uiopt", newcv);
}

function toggleSection(sectionName) {
	var theImg = fixElement(sectionName+'Arrow');
	var thePlusMinus = fixElement(sectionName+'PlusMinus');
	if (getDisplay(fixElement(sectionName))=='block') {
		setDisplay(fixElement(sectionName),'none'); 
		if (theImg) theImg.src='/img/template/heading_arrow_dn.gif';
		else if (thePlusMinus) setInnerHTML(thePlusMinus,"+");
		setCookie(sectionName,"none");
	} else {
		setDisplay(fixElement(sectionName),'block');
		if (theImg) theImg.src='/img/template/heading_arrow_up.gif';
		else if (thePlusMinus) setInnerHTML(thePlusMinus,"-");
		setCookie(sectionName,"block");
	}
}
function selectAll(whichSection,onOff) {
	var i,theDiv;
	if (theDiv = fixElement(whichSection)) {
		for (i=0;i<theDiv.getElementsByTagName("input").length; i++) {
			var theField = theDiv.getElementsByTagName("input")[i];
			if (theField.type=="checkbox") {
				theField.checked = ((onOff=="on") ? true : false);
			}
		}
	}
}
function updateDate(whichDate,isComplete) {
	var now = new Date();
	var yearField = fixElement(whichDate+'Year');
	var monthField = fixElement(whichDate+'Month');
	var dayField = fixElement(whichDate+'Day');
	var hourField = fixElement(whichDate+'Hour');
	var minuteField = fixElement(whichDate+'Minute');
	var amPmField = fixElement(whichDate+'AmPm');
	
	if (!isComplete) {
		if( yearField   ) yearField.selectedIndex = 0;
		if( monthField  ) monthField.selectedIndex = 0;
		if( dayField    ) dayField.selectedIndex = 0;
		if( hourField   ) hourField.selectedIndex = 0;
		if( minuteField ) minuteField.selectedIndex = 0;
		if( amPmField   ) amPmField.selectedIndex = 0;
	} else {
		//year
		if( yearField ){
			for (i=0;i<yearField.options.length;i++) {
				if (yearField.options[i].value==now.getFullYear()) {
					yearField.selectedIndex = i;
					break;
				}
			}
		}
		//month
		if( monthField ){
			for (i=0;i<monthField.options.length;i++) {
				if (monthField.options[i].value==(now.getMonth()+1)) {
					monthField.selectedIndex = i;
					break;
				}
			}
		}
		//day
		if( dayField ){
			for (i=0;i<dayField.options.length;i++) {
				if (dayField.options[i].value==now.getDate()) {
					dayField.selectedIndex = i;
					break;
				}
			}
		}
		//hour
		if( hourField ){
			var nowHour = now.getHours();
			if (nowHour == 0) {
				nowHour = 12;
				if( amPmField ) amPmField.selectedIndex=0;
			} else if (nowHour >= 13) {
				nowHour-=12;
				if( amPmField ) amPmField.selectedIndex=1;
			} else {
				if( amPmField ) amPmField.selectedIndex=0;
			}
			for (i=0;i<hourField.options.length;i++) {
				if (hourField.options[i].value==nowHour) {
					hourField.selectedIndex = i;
					break;
				}
			}
		}
		//minute
		if( minuteField ){
			for (i=0;i<minuteField.options.length;i++) {
				if (minuteField.options[i].value < now.getMinutes()) {
					minuteField.selectedIndex = i;
				}
			}
		}
	}
}

function insertValueToText (field,code,plain) {
	if (plain == 0) {
		FCKeditorAPI.GetInstance(field).InsertHtml(code);
	}
	else {
		insertAtCursor(field, code);
	}
}

function isDefined (variable) {
    return ((typeof variable === 'undefined') ? false : true);
}

function clearHtmlMessages (id) {
	var delay;
	var d = arguments[1];

	if (d) delay = d;
	else delay = 2000;

	setTimeout ("clearHtmlMessagesx('" + id + "')", delay);
}

function clearHtmlMessagesx (id) {
  document.getElementById(id).style.display = "none";
}


Number.prototype.formatMoney = function(c, d, t){
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)
    + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

/* I don't think this function is used */
function toggle (e, showhideonoff) {
	var d = '';
	var x;

	x = document.getElementById(e);
	if (!x) {
		x = document.getElementsByName(e);
		if (x[0]) x = x[0];
	}
		
	if (showhideonoff == 1 || showhideonoff == 'on') d = 'block';
	if (showhideonoff == 0 || showhideonoff == 'off') d = 'none';
	
	if (!showhideonoff) {
		if (x.style.display == 'none') x.style.display = 'block';
		else if (x.style.display == 'block') x.style.display = 'none';
	}
	else if (d != '') {
		x.style.display	= d;
	}
	
	return;
}
function urlEncode(string) {
	return escape(this._utf8_encode(string));
}
 
function urlDecode(string) {
	return this._utf8_decode(unescape(string));
}
function _utf8_encode(string) {
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return utftext;
}
function _utf8_decode(utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}

	return string;
}
