//  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
//
//  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
//  The following functions are released to the public domain.
//
//  This version takes a more aggressive approach to deleting
//  cookies.  Previous versions set the expiration date to one
//  millisecond prior to the current time; however, this method
//  did not work in Netscape 2.02 (though it does in earlier and
//  later versions), resulting in "zombie" cookies that would not
//  die.  DeleteCookie now sets the expiration date to the earliest
//  usable date (one second into 1970), and sets the cookie's value
//  to null for good measure.
//
//  Also, this version adds optional path and domain parameters to
//  the DeleteCookie function.  If you specify a path and/or domain
//  when creating (setting) a cookie**, you must specify the same
//  path/domain when deleting it, or deletion will not occur.
//
//  The FixCookieDate function must now be called explicitly to
//  correct for the 2.x Mac date bug.  This function should be
//  called *once* after a Date object is created and before it
//  is passed (as an expiration date) to SetCookie.  Because the
//  Mac date bug affects all dates, not just those passed to
//  SetCookie, you might want to make it a habit to call
//  FixCookieDate any time you create a new Date object:
//
//    var theDate = new Date();
//    FixCookieDate (theDate);
//
//  Calling FixCookieDate has no effect on platforms other than
//  the Mac, so there is no need to determine the user's platform
//  prior to calling it.
//
//  This version also incorporates several minor coding improvements.
//
//  **Note that it is possible to set multiple cookies with the same
//  name but different (nested) paths.  For example:
//
//    SetCookie ("color","red",null,"/outer");
//    SetCookie ("color","blue",null,"/outer/inner");
//
//  However, GetCookie cannot distinguish between these and will return
//  the first cookie that matches a given name.  It is therefore
//  recommended that you *not* use the same name for cookies with
//  different paths.  (Bear in mind that there is *always* a path
//  associated with a cookie; if you don't explicitly specify one,
//  the path of the setting document is used.)
//
//  Revision History:
//
//    "Toss Your Cookies" Version (22-Mar-96)
//      - Added FixCookieDate() function to correct for Mac date bug
//
//    "Second Helping" Version (21-Jan-96)
//      - Added path, domain and secure parameters to SetCookie
//      - Replaced home-rolled encode/decode functions with Netscape's
//        new (then) escape and unescape functions
//
//    "Free Cookies" Version (December 95)
//
//
//  For information on the significance of cookie parameters, and
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.html
//
//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0)  // Except on the Mac - ahead of its time
		date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

/* check form functions */

// cross-browser "this" finder
function getTargetElement(evt) {
	var elem;
	if (evt.target) {
		elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;
	}
	else {
		elem = evt.srcElement;
	}
	return elem;
}

function checkNumeric(field_str) {
	var num_pattern = /^[0-9]+$/;
	if (field_str.match(num_pattern)) {
		return true;
	}
	else {
		return false;
	}
}

function checkSize(field_str, min, max) {
	if ((field_str.length >= min) && (field_str.length <= max)) {
		return true;
	}
	else {
		return false;
	}
}

function formatPhone(evt) {
	var key_num;
	var key_char;
	var num_check;
	var char_check;
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	if (evt) {
		var elem = getTargetElement(evt);
		if (elem) {
			key_num = (window.event) ? evt.keyCode : ((evt.which) ? evt.which : "");
			key_char = String.fromCharCode(key_num);
			num_check = /\d/;
			if (num_check.test(key_char)) {
				if ((elem.value.length == 3) || (elem.value.length == 7)) {
					elem.value = elem.value + "-";
				}
				if (elem.value.length < 12) {
					elem.value = elem.value + key_char;
				}
				return false;
			}
			char_check = /[\x00\x08\x0D]/;
			if (char_check.test(key_char)) {
				return true;
			}
			return false;
		}
	}
}

function formatDate(evt) {
	var key_num;
	var key_char;
	var num_check;
	var char_check;
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	if (evt) {
		var elem = getTargetElement(evt);
		if (elem) {
			key_num = (window.event) ? evt.keyCode : ((evt.which) ? evt.which : "");
			key_char = String.fromCharCode(key_num);
			num_check = /\d/;
			if (num_check.test(key_char)) {
				if ((elem.value.length == 2) || (elem.value.length == 5)) {
					elem.value = elem.value + "\/";
				}
				if (elem.value.length < 10) {
					elem.value = elem.value + key_char;
				}
				return false;
			}
			char_check = /[\x00\x08\x0D]/;
			if (char_check.test(key_char)) {
				return true;
			}
			return false;
		}
	}
}

function onlyNumbers(evt) {
	var key_num;
	var key_char;
	var num_check;
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	if (evt) {
		key_num = (window.event) ? evt.keyCode : ((evt.which) ? evt.which : "");
		key_char = String.fromCharCode(key_num);
		num_check = /[\d\x00\x08\x0D]/;
		return num_check.test(key_char);
	}
}


function clearError(field_id) {
	document.getElementById(field_id + "Error").style.display = "none";
	document.getElementById(field_id + "ErrorMsg").style.display = "none";
	document.getElementById(field_id + "ErrorMsg").innerHTML = "";
	document.getElementById(field_id + "Req").style.display = "inline";

}

function errorMsg(field_id, error_num) {
	errorText = new Array("Required Field", "Invalid Email Address Format", "Invalid Phone Format", "Invalid Zip Code", "Password Must Be At Least 5 Characters", "Password Cannot Be The Same As Username", "Passwords Do Not Match");
	document.getElementById(field_id + "Req").style.display = "none";
	document.getElementById(field_id + "Error").style.display = "inline";
	document.getElementById(field_id + "ErrorMsg").innerHTML = errorText[error_num];
	document.getElementById(field_id + "ErrorMsg").style.display = "inline";
}

function checkPassword(passwd, vpasswd, username) {
	if (document.getElementById(passwd).value.length < 5) {
		errorMsg(passwd, 4);
		return false;
	}
	if (document.getElementById(passwd).value == document.getElementById(username).value) {
		errorMsg(passwd, 5);
		return false;
	}
	if (document.getElementById(passwd).value != document.getElementById(vpasswd).value) {
		errorMsg(passwd, 6);
		return false;
	}
	return true;
}

function checkZipcodePlus4(field) {
	if (field_str != "") {
		var zip_pattern = /^[0-9]{4}/;
		if (field.match(zip_pattern)) {
			return true;
		}
		return false;
	}
	else {
		return false;
	}
}

function checkZipcode(field) {
	if (field != "") {
		var zip_pattern = /^[0-9]{5}/;
		if (field.match(zip_pattern)) {
			return true;
		}
		return false;
	}
	else {
		return false;
	}
}

function checkPhone(field) {
	if (field != "") {
		var phone_pattern = /^[0-9]{3}-[0-9]{3}-[0-9]{4}/;
		if (field.match(phone_pattern)) {
			return true;
		}
		return false;
	}
	else {
		return false;
	}
}

function checkEmail(field) {
	if (field != "") {
		// check if the address fits the user@domain format. It also is used to separate the username from the domain
		var email_pattern = /^(.+)@(.+)$/;
		// pattern for matching all special characters...don't allow special characters in the address...includes ( ) < > @ , ;  : \ " . [ ]
		var special_chars = "\\(\\)<>@,; :\\\\\\\"\\.\\[\\]";
		// range of characters allowed in a  username or domainname....states which chars aren't allowed
		var valid_chars = "\[^\\s" + special_chars + "\]";
		// pattern applies if the "user" is a quoted string (in which case, there are no rules about which characters are allowed and which aren't;  anything goes)...e.g. "yak boy"@disney.com is a legal address.
		var quoted_user = "(\"[^\"]*\")";
		// pattern for domains that are IP addresses, rather than symbolic names...e.g. joe@[123.124.233.4] is a legal address...square brackets are required
		var ip_domain_pattern = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		// an atom (basically a series of non-special characters.)
		var atom = valid_chars + "+";
		// one word in the typical username...e.g. in yak.boy@somewhere.com, yak and boy are words....a word is either an atom or quoted string
		var word = "(" + atom + "|" + quoted_user + ")";
		// pattern for structure of the use
		var user_pattern = new RegExp("^" + word + "(\\." + word + ")*$");
		// pattern for structure of a normal symbolic domain, as opposed to ip_domain_pattern, shown above
		var domain_pattern = new RegExp("^" + atom + "(\\." + atom +")*$");

		// coarse pattern to break up user@domain into different pieces that are easy to analyze
		var match_array = field.match(email_pattern);
		if (match_array == null) {
		// Too many/few @'s or something;  basically, this address doesn't fit the general form of a valid address
			return false;
		}
		var user = match_array[1];
		var domain = match_array[2];
		// See if "user" is valid
		if (user.match(user_pattern) == null) {
			// user is not valid
			return false;
		}
		// if the address is at an IP make sure the IP  is valid
		var ip_array = domain.match(ip_domain_pattern);
		if (ip_array != null) {
			// an IP address
			for (var i = 1; i <= 4; i++) {
				if (ip_array[i] > 255) {
					return false;
				}
			}
			// IP ok
			return true;
		}
		// domain is symbolic name
		var domain_array = domain.match(domain_pattern);
		if (domain_array == null) {
			return false;
		}
		// break up the domain to get a count of how many atoms it consists of
		var atom_pattern = new RegExp(atom,"g");
		var dom_arr = domain.match(atom_pattern);
		var len = dom_arr.length;
		if ((dom_arr[dom_arr.length-1].length < 2) || (dom_arr[dom_arr.length-1].length > 4)) {
			// address doesn't end in a 2 - 4 letter top level domain
			return false;
		}
		// make sure there's a host name preceding the domain
		if (len < 2) {
			return false;
		}
		// all's well
		return true;
	}
	else {
		return false;
	}
}

function checkForm(thisform) {
	var field;
	var field_id;
	var check = true;
	if ((!document.getElementById) || (!document.createTextNode)) {
		return;
	}
	if (!document.getElementById("required")) {
		return;
	}
	var req_fields = document.getElementById("required_fields").value.split(", ");
	for(var i = 0; i < req_fields.length; i++) {
		field  = document.getElementById(req_fields[i]);
		field_id = field.id;
		if (!field) {
			continue;
		}
		clearError(field_id);
		switch(field.type.toLowerCase()) {
			case "text":
				changeClassName(field_id, "text");
				if (field.value == "") {
					check = false;
					errorMsg(field_id, 0);
					changeClassName(field_id, "textError");
					break;
				 }
				 else if ((field_id.toString().indexOf("email") > -1) && (!checkEmail(field.value))) {
					check = false;
					errorMsg(field_id, 1);
					changeClassName(field_id, "textError");
					break;
				}
				else if ((field_id.toString().indexOf("phone") > -1) && (!checkPhone(field.value))) {
					check = false;
					errorMsg(field_id, 2);
					changeClassName(field_id, "textError");
					break;
				}
				else if ((field_id.toString().indexOf("zip") > -1) && (!checkZipcode(field.value))) {
					check = false;
					errorMsg(field_id, 3);
					changeClassName(field_id, "textError");
					break;
				}
				break;
			case "password":
				if (field.value == "") {
					check = false;
					errorMsg(field_id, 0);
					changeClassName(field_id, "textError");
				}
				else if (!checkPassword("Password", "VerifyPassword", "LoginName")) {
					check = false;
					changeClassName(field_id, "textError");
					break;
				}
				break;
			case "textarea":
				if (field.value == "") {
					check = false;
					errorMsg(field_id);
				}
				break;
			case "checkbox":
				if (!field.checked) {
					check = false;
					errorMsg(field_id);
				}
				break;
			case "select-one":
				if ((!field.selectedIndex) && (field.selectedIndex==0)) {
					check = false;
					errorMsg(field_id);
				}
				break;
		}
	}
	if (!check) {
		document.getElementById("errorPrompt").style.display = "inline";
	}
	else if ((check) && (document.getElementById("errorPrompt").style.display == "inline")) {
		document.getElementById("errorPrompt").style.display = "none";
	}
	return check;
}

/* window popup functions */
var l_popup_win;

function popupWinClosePopup() {
	if (l_popup_win) {
		l_popup_win.close();
		l_popup_win = null;
	}
}

function jsTools_popup2(url, winParams) {
	l_popup_win = window.open(url, "KinteraSphere", winParams);
	l_popup_win.focus();
}
window.onfocus = popupWinClosePopup;

function popupStickyWindow(url, width, height, b_has_top_bars) {
	var bar_str = b_has_top_bars ? "yes" : "no";
	var curr_time = new Date();
	w_popup = window.open(url, "KinteraSphere" + curr_time.getHours() + curr_time.getMinutes() + curr_time.getSeconds(), "width=" + width + ",height=" + height + ", scrollbars, resizable, menubar=" + bar_str + ", toolbar=" + bar_str);
	if (window.focus) {
		w_popup.focus();
	}
	return false;
}

function popupVolatileWindow(url, width, height, b_has_top_bars) {
	popupWinClosePopup();
	l_popup_win = popupStickyWindow(url, width, height, b_has_top_bars);
	return l_popup_win;
}

function jsTools_popup_calendar2(root_path, element, name, type) {
	var esc;
	if (type == 1) { //input box in a form
		esc = escape(element.value) + "&inp=" + name;
	}
	else if (type == 100) {
		esc = escape(element.value) + "&inp=" + name + "&cs=1";
	}
	else if (type == 200) {
		esc = escape(element.start_month.value + "/" + element.start_day.value + "/" + element.start_year.value) + "&form=" + name + "&inp=" + type;
	}
	else if (type == 300) {
		esc = escape(element.end_month.value + "/" + element.end_day.value + "/" + element.end_year.value) + "&form=" + name + "&inp=" + type;
	}
	else {
		esc = escape(element.month.value + "/" + element.day.value + "/" + element.year.value) + "&form=" + name;
	}
	jsTools_popup(root_path + "/common/asp/calendar_popup.asp?date=" + esc, 240, 265);
}

function jsTools_popup_calendar3(root_path, element, name, type, winParams) {
	var esc;
	if (type == 1)	{ //input box in a form
		esc = escape(element.value) + "&inp=" + name;
	}
	else if (type == 100) {
		esc=escape(element.value) + "&inp=" + name + "&cs=1";
	}
	else if (type == 400) {
		esc = escape(element.value) + "&inp=" + type;
	}
	else {
		esc=escape(element.month.value + "/" + element.day.value + "/" + element.year.value) + "&form=" + name;
	}
	jsTools_popup2(root_path + "/common/asp/calendar_popup.asp?date=" + esc, "width=240, height=265," + winParams);
	return l_popup_win;
}

function jsTools_popup_calendar(element, name, type) {
	jsTools_popup_calendar2("../..", element, name, type);
}

function popUp(url, win, w, h, scrollbar, stat, resize, tools, menu, loc) {
	var newWindow;
	newWindow = window.open(url, win, "height = " + h + ", width = " + w + ", scrollbars = " + scrollbar + ", status = " + stat + ", resizable = " + resize + ", toolbar = " + tools + ", menubar = " + menu + ", location = " + loc);
	if (window.focus) {
		newWindow.focus();
	}
	return false;
}

function closeFrameWin() {
	parent.window.close();
}

function getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	if ( node == null ) {
		node = document;
	}
	if ( tag == null ) {
		tag = '*';
	}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* link toggle functions */

function cancelLink() {
	return false;
}

function disableLink(link) {
	if (link.onmouseover) {
		link.oldOnMouseOver = link.onmouseover;
	}
	link.onmouseover = cancelLink;
	if (link.onmouseout) {
		link.oldOnMouseOut = link.onmouseout;
	}
	link.onmouseout = cancelLink;
	if (link.onclick) {
		link.oldOnClick = link.onclick;
	}
	link.onclick = cancelLink;
	if (link.style) {
		link.style.cursor = "default";
	}
	link.className = "disabled";
}

function enableLink(link) {
	link.onmouseover = link.oldOnMouseOver ? link.oldOnMouseOver : null;
	link.onmouseout = link.oldOnMouseOut ? link.oldOnMouseOut : null;
	link.onclick = link.oldOnClick ? link.oldOnClick : null;
	if (link.style) {
		link.style.cursor = document.all ? "hand" : "pointer";
	}
	link.className = "";
}

function toggleLink(link) {
	if (link.disabled) {
		enableLink(link);
	}
	else {
		disableLink(link);
	}
	link.disabled = !link.disabled;
}

function toggleImg(img_id, img_src_enabled, img_src_disabled) {
	src_string = new String("");
	if (document.getElementById(img_id)) {
		src_string = String(document.getElementById(img_id).src);
		 if (src_string.indexOf("Disabled.gif") > 0 ) {
			document.getElementById(img_id).src = img_src_enabled;
		}
		else {
			document.getElementById(img_id).src = img_src_disabled;
		}
	}
}

function changeClassName(btn_id, class_name) {
	document.getElementById(btn_id).className = class_name;
}

/* misc form functions */

function completeButton () {
	changeClassName("CheckButton1", "complete");
	changeClassName("CheckButton2", "complete");
	document.getElementById("CheckButton1").disabled = true;
	document.getElementById("CheckButton2").disabled = true;
}

function checkDisabled () {
	if (document.getElementById("CancelButton1").disabled) {
		document.getElementById("CancelButton1").disabled = false;
		changeClassName("CancelButton1", "cancel");
		document.getElementById("CancelButton2").disabled = false;
		changeClassName("CancelButton2", "cancel");
		document.getElementById("SaveButton1").disabled = false;
		changeClassName("SaveButton1", "save");
		document.getElementById("SaveButton2").disabled = false;
		changeClassName("SaveButton2", "save");
	}
}

function toggleDivOn(div1) {
	if (document.getElementById(div1).style.display == "none") {
		document.getElementById(div1).style.display = "block";
	}
}

function toggleDivOff(div1) {
	if (document.getElementById(div1).style.display == "block") {
		document.getElementById(div1).style.display = "none";
	}
}

function characterLimit(formName, fieldName, charLimit, remainingCharField) {
	var charCount = document.getElementById(fieldName).value;
	var charsUsed = charCount.length;
	var remainingChars;
	if (charsUsed >= charLimit) {
		document.getElementById(remainingCharField).value = "0";
		// truncate characters after charLimit
		 document.getElementById(fieldName).value = charCount.substring(0, charLimit);
	}
	else	{
		// update available characters
		remainingChars = (charLimit - charsUsed);
		document.getElementById(remainingCharField).value = remainingChars;
	}
}

function stripNewlines(thisform, textarea_id, prompt_id, warning_id, limit_id, limit) {
	var browser = navigator.appName;
	var b_version = navigator.appVersion;
	var version = parseFloat(b_version);
	textarea_str = new String(document.getElementById(textarea_id).value);
	if ((browser == "Microsoft Internet Explorer") || (browser == "Opera")) {
		var new_textarea_str = textarea_str.replace(new RegExp( "[\r\n]{2}", "g" ), "~" );
	}
	else if (browser == "Netscape") {
		var new_textarea_str = textarea_str.replace(new RegExp( "\\n", "g" ), "~" );
	}
	document.getElementById(span_id).innerHTML = "<span style=\"display: none;\"><textarea name=\"" + textarea_id + "\" id=\"" + textarea_id + "\" rows=\"13\" cols=\"117\">" +  new_textarea_str + "<\/textarea><\/span><textarea rows=\"13\" cols=\"117\" onKeyUp=\"checkDisabled();  characterLimit('" + thisform + "', '" + textarea_id + "', '" + limit + "', '" + limit_id + "')\" >" +  textarea_str + "<\/textarea>";
}

function replaceNewlines(thisform, textarea_id, prompt_id, warning_id, limit_id, limit) {
	var browser = navigator.appName;
	var b_version = navigator.appVersion;
	var version = parseFloat(b_version);
	textarea_str = new String(document.getElementById(textarea_id).value);
	var new_textarea_str = textarea_str.replace(new RegExp( "~", "g" ), "\n");
	document.getElementById(span_id).innerHTML = "<textarea name=\"" + textarea_id + "Field3150919\" id=\"" + textarea_id + "\" rows=\"13\" cols=\"117\" onKeyUp=\"checkDisabled(); characterLimit('" + thisform + "', '" + textarea_id + "', '" + limit + "', '" + limit_id + "')\" >" +  new_textarea_str + "<\/textarea>";
}

function textAreaSetup(thisform, textarea_id, prompt_id, warning_id, limit_id, limit) {
	replaceNewlines(thisform, textarea_id, prompt_id, warning_id, limit_id, limit);
	characterLimit(thisform, textarea_id, limit, limit_id);
}

function expandCollapseDiv(div1, img1) {
	if (document.getElementById(div1).style.display=="none") {
		document.getElementById(div1).style.display="block";
		document.getElementById(img1).src="/atf/cf/{83b8cbf4-2aec-4bd0-a721-d9dd7ce7fcbb}/CollapseIcon.gif";
	}
	else if (document.getElementById(div1).style.display=="block") {
		document.getElementById(div1).style.display="none";
		document.getElementById(img1).src="/atf/cf/{83b8cbf4-2aec-4bd0-a721-d9dd7ce7fcbb}/ExpandIcon.gif";
	}
}

/* processing bar */
var progressEnd = 9;		// set to number of progress <span>'s.
var progressColor = "#999999";	// set to progress bar color
var progressInterval = 500;	// set to time between updates (milli-seconds)
var progressAt = progressEnd;
var progressTimer;

function progress_clear() {
	for (var i = 1; i <= progressEnd; i++) {
		document.getElementById("progress" + i).style.backgroundColor = "transparent";
	}
	progressAt = 0;
}

function progress_update() {
	progressAt++;
	if (progressAt > progressEnd) {
		 progress_clear();
	}
	else {
		document.getElementById("progress"+progressAt).style.backgroundColor = progressColor;
	}
	progressTimer = setTimeout("progress_update()",progressInterval);
}

function progress_stop() {
	clearTimeout(progressTimer);
	progress_clear();
}

/* misc utility */
function swapImg(img_id, new_src) {
	document.getElementById(img_id).src = new_src;
}


function getCellValue (cellOrId) {
	var cell =  typeof cellOrId == 'string' ? (document.all ? document.all[cellOrId] : document.getElementById(cellOrId)) : cellOrId;
	return cell.innerHTML;
}

function traverseNodes(root) {
	var str = '';
	var node = root,
	next_node = null;
	var i = 0;
	do {
		// check for a child
		next_node = node.firstChild;
		// if no child, just visit node
		if (next_node == null) {
			// visit node if it's an element
			if (node.nodeType == 1) {
				alert("");
			}
			// check for next node
			next_node = node.nextSibling;
		}
		// if no next node
		if (next_node == null) {
			// go to the last node's level
			var tmp = node;
			do {
				// go up one level
				next_node = tmp.parentNode;
				// stop if at the top
				if (next_node == root) {
					break;
				}
				// visit node if it's an element
				if (next_node.nodeType == 1) {
					alert("");
				}
				// go to the last node's level
				tmp = next_node;
				// check for the next node
				next_node = next_node.nextSibling;
			} while (next_node == null) // there's no next node
		}
		// if there is a child, make it the new node or if we're at the following node
		node = next_node;
	} while (node != root); // not back at the top
	return false;
}

/* gradients */
function createGradient() {
	var x, y;
	var i;
	// if not a DOM browser
	if (!document.getElementById) {
		return;
	}
	objArray = getGradientObjects();
	if (!objArray.length) {
		return;
	}
	for (i = 0; i < objArray.length; i++) {
		params = objArray[i].className.split(" ");
		// if it's IE or Opera, use the MS gradient function
		if (document.all && !window.opera) {
			// take care of some alignment goofiness
			w = objArray[i].offsetWidth + 1;
			objArray[i].style.width = w + "px";
			objArray[i].style.borderRight="1px solid #ffffff";
			// set the gradient orientation
			params[3] == "horizontal" ? gType = 1 : gType = 0;
			objArray[i].style.filter = "progid:DXImageTransform.Microsoft.Gradient(GradientType=" + gType + ",StartColorStr=\"#00" + params[1] + "\",EndColorStr=\"#99" + params[2] + "\")";
		}
		else { // Mozilla
			colorArray = createColorPath(params[1], params[2]);
			// set the first color bar location
			if (objArray[i].parentNode.id == "searchForm") {
				x = 1;
				y = -1;
			}
			else {
				x = 3;
				y = 0;
			}
			if (params[3] == "horizontal") {
				// set the width of the color bar
				w = Math.round(objArray[i].offsetWidth/colorArray.length);
				if (!w) {
					w=1;
				}
				// set height
				h = objArray[i].offsetHeight;
			}
			else { // vertical
				// set height of the color bars
				h = Math.ceil(objArray[i].offsetHeight/colorArray.length);
				if (!h) {
					h =1;
				}
				// set width
				w = objArray[i].offsetWidth;
			}
			// make a new parent node that will sit on top to the gradient element
			makeGrandParent(objArray[i]);
			// make a "light" document object to place the color bars
			tmpDOM = document.createDocumentFragment();
			for(k = 0; k < colorArray.length; k++) {
				// make the color bar
				color_bar = document.createElement("div");
				color_bar.setAttribute("style","position:absolute;z-index:0;top:" + y + "px;left:" + x + "px;height:" + h + "px;width:" + w + "px;background-color:rgb(" + colorArray[k][0] + "," + colorArray[k][1] + "," + colorArray[k][2] + ");");
				// increment the color bar location
				params[3] == "horizontal" ? x+=w : y+=h;
				tmpDOM.appendChild(color_bar);
				// stop making bars if at the end of the gradient object
				if (y >= objArray[i].offsetHeight || x >= objArray[i].offsetWidth) {
					break;
				}
			}
			// add the gradient object to the document
			objArray[i].appendChild(tmpDOM);
			// plug memory leak
			tmpDOM = null;
		}
	}
}

function getGradientObjects() {
	a = document.getElementsByTagName("*");
	objs = new Array();
	for(i = 0; i < a.length; i++) {
		class_name = a[i].className;
		if (class_name != "") {
			if (class_name.indexOf("gradient") == 0) {
				objs[objs.length] = a[i];
			}
		}
	}
	return objs;
}

function createColorPath(color1,color2) {
	// make a color array for the color bars
	colorPath = new Array();
	// set the color delta
	color_pct = 1.0;
	do {
		// convert from hex to decimal rgb and fill color array
		colorPath[colorPath.length] = setColorHue(longHexToDec(color1), color_pct,longHexToDec(color2));
		// decrement the color delta
		color_pct -= .01;
	} while (color_pct > 0);
	return colorPath;
}

function setColorHue(origin_color,opacity_percent,mask_rgb) {
	// adjust the r, g & b according to the color delta...each time this function is called the startcolor gets closer to the end color
	returnColor = new Array();
	for (w = 0; w < origin_color.length; w++) {
		returnColor[w] = Math.round(origin_color[w] * opacity_percent) + Math.round(mask_rgb[w] * (1.0 - opacity_percent));
	}
	return returnColor;
}

function longHexToDec(longHex) {
	// conver the hex rgb channels to decimal
	return new Array(toDec(longHex.substring(0,2)), toDec(longHex.substring(2,4)), toDec(longHex.substring(4,6)));
}

function toDec(hex) {
	return parseInt(hex, 16);
}

function makeGrandParent(obj) {
	disp = document.defaultView.getComputedStyle(obj,'').display;
	// if the gradient object is a block element make a div, otherwise make a span
	disp == "block" ? nSpan = document.createElement("div") : nSpan = document.createElement("span");
	// save the contents of the gradient object
	mHTML = obj.innerHTML;
	// clear out the contents of the gradient object
	obj.innerHTML = "";
	// put the saved contents into the new div or span
	nSpan.innerHTML = mHTML;
	// put the new div or span on top of the gradient object
	nSpan.setAttribute("style", "position:relative; z-index:10;");
	obj.appendChild(nSpan);
}

function adjustGradient(root) {
	var node = document.getElementById(root).firstChild;
	var ht = document.defaultView.getComputedStyle(node, "").height.toString();
	first_gradient_bar = node.nextSibling;
	node = first_gradient_bar;
	do {
		if (node.nodeName == "DIV") {
			node.style.display = "none";
		}
		node = node.nextSibling;
	} while (node != document.getElementById(root).lastChild);
	node = first_gradient_bar;
	var bar_ht = document.defaultView.getComputedStyle(node, "").height.toString();
	for (i = 0; i < (Math.floor(Number(ht.substring(0, ht.length -2)) / Number(bar_ht.substring(0, bar_ht.length -2)))); i++) {
		if (node.nodeName == "DIV") {
			node.style.display = (node.style.display == "none") ? "block" : "none";
		}
		node = node.nextSibling;
	}
}

function toggleSiblings(root, btn1, btn2) {
	var node = document.getElementById(root);
	while (node) {
		node = node.nextSibling;
		if (node == null) {
			break;
		}
		if (node.nodeType == 1) {
			if ((node.getAttribute("id") != btn1) && (node.getAttribute("id") != btn2)) {
				node.style.display = (node.style.display == "none") ? "block" : "none";
			}
		}
	}
}

/* custom search */
function toggleSearch(search_id, gradient_id, hide_id, show_id) {
	document.getElementById(hide_id).style.display = (document.getElementById(hide_id).style.display == "none") ?  "block" : "none";
	document.getElementById(show_id).style.display = (document.getElementById(show_id).style.display == "block") ?  "none" : "block";
	document.getElementById(search_id).style.display = (document.getElementById(search_id).style.display == "none") ?  "block" : "none";
	adjustGradient(gradient_id);
}

function toggleFieldset(set_id, gradient_id, hide_id, show_id) {
	document.getElementById(hide_id).style.display = (document.getElementById(hide_id).style.display == "none") ?  "block" : "none";
	document.getElementById(show_id).style.display = (document.getElementById(show_id).style.display == "block") ?  "none" : "block";
	toggleSiblings(set_id, hide_id, show_id);
	adjustGradient(gradient_id);
}

function radioGroups(thisform) {
	var i;
	var j;
	var group;
	var field;
	var aElem = thisform.elements;
	for (i = 0; i < aElem.length; i++) {
		if (aElem[i].type == "radio") {
			group = aElem[aElem[i].name];
			for (j = 0; j < group.length; j++) {
				if (group[j].checked) {
					field = "fiel" + group[j].name;
					document.getElementById(field).value = group[j].value;
					break;
				}
			}
		}
	}
}

function checkboxGroups(thisform) {
	var i;
	var j;
	var use_group;
	var group;
	var field;
	var aElem = thisform.elements;
	for (i = 0; i < aElem.length; i++) {
		if (aElem[i].type == "checkbox") {
			use_group = false;
			group = aElem[aElem[i].name];
			if (i > 0) {
				if (aElem[aElem[i].name] != aElem[aElem[i-1].name]) {
					if (group.length) {
						for (j = 0; j < group.length; j++) {
							field = "fiel" + group[j].name;
							if (group[j].checked) {
								use_group = true;
								document.getElementById(field).value = document.getElementById(field).value + "," + group[j].value + ",";
							}
							else {
								document.getElementById(field).value = document.getElementById(field).value + ",No" + group[j].value + ",";
							}
						}
						document.getElementById(field).value = (use_group) ? document.getElementById(field).value : "";
					}
				}
			}
		}
	}
}

function selectOnes(thisform) {
	var i;
	var field;
	var aElem = thisform.elements;
	for (i = 0; i < aElem.length; i++) {
		if (aElem[i].type == "select-one") {
			list = aElem[i].name.toString();
			if ((list.indexOf("op") != 0) && (list.indexOf("state") != 0)) {
				field = list.substring(0, list.length - 1);
				document.getElementById(field).value = document.getElementById(list)[document.getElementById(list).selectedIndex].value;
			}
		}
	}
}

function submitSearch(thisform) {
	radioGroups(thisform);
	checkboxGroups(thisform);
	selectOnes(thisform);
	return true;
}

function clearForm(thisform) {
	var i;
	var j;
	var elem;
	var aElem = thisform.elements;
	for (i = 0; i < aElem.length; i++) {
		if (aElem[i].type == "radio") {
			elem = aElem[aElem[i].name];
			for (j = 0; j < elem.length; j++) {
				elem[j].checked = false;
			}
		}
		if (aElem[i].type == "checkbox") {
			elem = aElem[aElem[i].name];
			if (elem.length) {
				for (j = 0; j < elem.length; j++) {
					elem[j].checked = false;
				}
			}
		}
		if (aElem[i].type == "select-one") {
			elem = aElem[i].name.toString();
			if (elem.indexOf("op") != 0) {
				document.getElementById(elem).selectedIndex = 0;
			}
		}
		if (aElem[i].type == "text") {
			elem = aElem[i].name.toString();
			document.getElementById(elem).value = "";
		}
	}
}

function getAll(thisform) {
	clearForm(thisform);
	radioGroups(thisform);
	checkboxGroups(thisform);
	selectOnes(thisform);
	document.getElementById("searchForm").submit()
}

function showDetails(id_num, radio) {
	var i;
	rl = radio.length;
	if (!(rl > 1)) {
		radio.checked = true;
	}
	else {
		for (var i = 0; i < rl; i++) {
			if (id_num == radio[i].value) {
				radio[i].checked = true;
			}
		}
	}
	SetCookie("goToDetails", 1, null, "/");
	document.docForm.Select.click();
	return false;
}

function getGoToPage(contents) {
	aContents = new Array();
	aTemp = new Array();
	var i = 0;
	var contents = contents.toLowerCase();
	aContents = contents.split("&nbsp;");
	aContents.splice((aContents.length - 1), 1)
	aContents[0] = aContents[0].replace(new RegExp( "prev"), "<img src=\"http://www.kintera.org/atf/cf/{8712CBAB-0EA9-444F-9B5D-35B87FB672ED}/PreviousArrow.gif\" width=\"6\" height=\"11\" border=\"0\" alt=\"Previous\" \/>" );
	if (aContents[0].indexOf("<a") > -1) {
		aTemp = aContents[0].split("<a ");
		aContents[0] = "<a class=\"prev\" " + aTemp[1];
	}
	aContents[(aContents.length - 1)] = aContents[(aContents.length - 1)].replace(new RegExp( "next"), "<img src=\"http://www.kintera.org/atf/cf/{8712CBAB-0EA9-444F-9B5D-35B87FB672ED}/NextArrow.gif\" width=\"6\" height=\"11\" border=\"0\" alt=\"Next\" \/>" );
	aContents[(aContents.length - 1)] = (aContents[(aContents.length - 1)].indexOf("<a") == -1) ? aContents[(aContents.length - 1)].substring((aContents[(aContents.length - 1)].lastIndexOf("<\/font>") + 8), aContents[(aContents.length - 1)].length) : aContents[(aContents.length - 1)].substring((aContents[(aContents.length - 1)].indexOf("<\/a>") + 4), aContents[(aContents.length - 1)].length);
	if (aContents[(aContents.length - 1)].indexOf("<a") > -1) {
		aTemp = aContents[(aContents.length - 1)].split("<a ");
		aContents[(aContents.length - 1)] = "<a class=\"next\" " + aTemp[1];
	}
	for (i = 1; i < aContents.length - 1; i++) {
		if ((aContents[0].indexOf("<a") > -1) && (i == 1)) {
			aContents[i] = aContents[i] + i + "<\/a>";
		}
		else if (aContents[i].indexOf("<\/font") == 1) {
			aContents[i] = aContents[i].substring(9, aContents[i].length) + i + "<\/a>";
		}
		else if ((aContents[i].indexOf("<\/a") == 1) && (aContents[i].indexOf("background-color") == -1)) {
			aContents[i] = aContents[i].substring(6, aContents[i].length) + i + "<\/a>";
		}
		else if (aContents[i].indexOf("background-color") > -1) {
			aContents[i] = "<span class=\"noLink\">" + i + "<\/span>";
		}
	}
	return contents = aContents;
}

function getNumShowing(contents) {
	return contents = contents.substring(contents.indexOf("Showing"), contents.indexOf("Records")) + "Results";
}

function cleanCell(contents) {
	aContents = new Array(3);
	if (contents.indexOf("<a") > -1) {
		aContents[0] = contents.substring(contents.indexOf("<a"), contents.indexOf("<\/a>") + 4);
	}
	else {
		aContents[0] = contents.substring(contents.indexOf("<A"), contents.indexOf("<\/A>") + 4);
	}
	aContents[0] = aContents[0].replace(new RegExp( "FulfillmentStatus", "i" ), "Status" );
	aContents[0] = aContents[0].replace(new RegExp( "Address Line 1", "i" ), "Address" );
	aContents[0] = aContents[0].replace(new RegExp( "ZIP/Postal Code", "i" ), "Zip" );
	aContents[0] = aContents[0].replace(new RegExp( "PriorityLevel", "i" ), "Priority" );
	aContents[1] = (contents.indexOf("\/Spherelite\/Images\/sort_asc_arrow.gif") > -1) ?  "<img src=\"http://www.kintera.org/atf/cf/{8712CBAB-0EA9-444F-9B5D-35B87FB672ED}/SortAsc.gif\" width=\"7\" height=\"7\" border=\"0\" alt=\"Sort\">" : "";
	if (aContents[1] == "") {
		aContents[1] = (contents.indexOf("\/Spherelite\/Images\/sort_desc_arrow.gif") > -1) ?  "<img src=\"http://www.kintera.org/atf/cf/{8712CBAB-0EA9-444F-9B5D-35B87FB672ED}/SortDesc.gif\" width=\"7\" height=\"7\" border=\"0\" alt=\"Sort\">" : "";
	}
	return aContents;
}

function formatResults() {
	aContents = new Array(3);
	var contents = "";
	var i, j, k;
	var new_row, new_cell, new_html;
	var id_pattern = /[A-Za-z0-9]{10,}/;
	var supporter_id;
	var old_pagination = document.getElementById("CLpaginationArea");
	var aTables = old_pagination.getElementsByTagName("TABLE");
	var old_tbl = aTables[1];

	top_pagination = document.createElement("div");
	num_showing = document.createElement("div");
	num_showing.setAttribute("id", "numShowing");
	new_html  = document.createTextNode(getNumShowing(getCellValue(old_tbl.rows[0].cells[0])));
	num_showing.appendChild(new_html);
	top_pagination.appendChild(num_showing);
	go_to_page = document.createElement("div");
	go_to_page.setAttribute("id", "goToPage");
	new_html  = document.createTextNode("");
	go_to_page.appendChild(new_html);
	aContents = getGoToPage(getCellValue(old_tbl.rows[1].cells[0]));
	for (i = 0; i < aContents.length; i++) {
		contents = contents + aContents[i];
	}
	go_to_page.innerHTML = contents;
	contents = "";
	top_pagination.appendChild(go_to_page);
	var mark = document.getElementById("topPaginationMarker");
	var parent_node = mark.parentNode;
	parent_node.insertBefore(top_pagination, mark);
	bottom_pagination = document.createElement("div");
	num_showing = document.createElement("div");
	num_showing.setAttribute("id", "numShowingBot");
	new_html  = document.createTextNode(getNumShowing(getCellValue(old_tbl.rows[0].cells[0])));
	num_showing.appendChild(new_html);
	bottom_pagination.appendChild(num_showing);
	go_to_page = document.createElement("div");
	go_to_page.setAttribute("id", "goToPageBot");
	new_html  = document.createTextNode("");
	go_to_page.appendChild(new_html);
	aContents = getGoToPage(getCellValue(old_tbl.rows[1].cells[0]));
	for (i = 0; i < aContents.length; i++) {
		contents = contents + aContents[i];
	}
	go_to_page.innerHTML = contents;
	contents = "";
	bottom_pagination.appendChild(go_to_page);
	var mark = document.getElementById("bottomPaginationMarker");
	var parent_node = mark.parentNode;
	parent_node.insertBefore(bottom_pagination, mark);
	var old_results = document.getElementById("CLsearchResultsArea");
	aTables = old_results.getElementsByTagName("TABLE");
	old_tbl = aTables[1];
	result_list = document.createElement("div");
	result_list.setAttribute("id", "customList");
	gradient = document.createElement("div");
	gradient.setAttribute("id", "resultsGradient");
	gradient.setAttribute("class", "set");
	result_list.appendChild(gradient);
	result_tbl = document.createElement("table");
	result_tbl.setAttribute("cellspacing", "0");
	result_tbl.cellSpacing = "0";
	// header row
	new_row = result_tbl.insertRow(0);
	new_cell  = new_row.insertCell(0);
	new_cell.setAttribute("class", "header");
	new_cell.className = "header";
	result_list.setAttribute("width", "auto");
	for (i = 1; i < old_tbl.rows[0].cells.length; i++) {
		new_cell  = new_row.insertCell(i);
		aContents = cleanCell(getCellValue(old_tbl.rows[0].cells[i]));
		if (aContents[1] == "") {
			new_cell.setAttribute("class", "header");
			new_cell.className = "header";
		}
		else {
			new_cell.setAttribute("class", "sort");
			new_cell.className = "sort";
		}
		new_html  = document.createTextNode("");
		new_cell.appendChild(new_html);
		contents = "<a href=\"\" onClick=\"return showDetails('" + supporter_id + "', document.docForm.supporter_id);\">" + contents + "<\/a>"
		new_cell.innerHTML = aContents[0] + aContents[1];
	}
	// results list
	for (i = 3; i < old_tbl.rows.length; i++) {
		new_row = result_tbl.insertRow(i - 2);
		new_cell  = new_row.insertCell(0);
		// get the value of the supporter_id
		link_cell = getCellValue(old_tbl.rows[i].cells[0]);
		aMatch = id_pattern.exec(link_cell);
		if (aMatch) {
			supporter_id = aMatch[0];
		}
		for (j = 1; j < old_tbl.rows[i].cells.length; j++) {
			new_cell  = new_row.insertCell(j);
			new_html  = document.createTextNode(getCellValue(old_tbl.rows[i].cells[j]));
			new_cell.appendChild(new_html);
			if (j == 1) {
				contents = getCellValue(new_cell);
				contents = contents.replace(new RegExp( "&amp;", "i" ), "&" );
				// turn the org name cell into a link to select the org as looked up contact
				new_cell.innerHTML = "<a href=\"\" onClick=\"return showDetails('" + supporter_id + "', document.docForm.supporter_id);\">" + contents + "<\/a>";
			}
			if (j == old_tbl.rows[i].cells.length - 1) {
				contents = getCellValue(new_cell);
				switch (contents) {
					case "1" :
						new_cell.innerHTML = "<img class=\"flag\" src=\"http://www.kintera.org/atf/cf/{8712CBAB-0EA9-444F-9B5D-35B87FB672ED}/FlagHigh.gif\" width=\"13\" height=\"12\" border=\"0\" alt=\"High Priority\" \/>";
						break;
					case "2" :
						new_cell.innerHTML = "<img class=\"flag\" src=\"http://www.kintera.org/atf/cf/{8712CBAB-0EA9-444F-9B5D-35B87FB672ED}/FlagNormal.gif\" width=\"13\" height=\"12\" border=\"0\" alt=\"Normal Priority\" \/>";
						break;
					case "3" :
						new_cell.innerHTML = "<img class=\"flag\" src=\"http://www.kintera.org/atf/cf/{8712CBAB-0EA9-444F-9B5D-35B87FB672ED}/FlagLow.gif\" width=\"13\" height=\"12\" border=\"0\" alt=\"Low Priority\" \/>";
						break;
					default:
						new_cell.innerHTML = "<img class=\"flag\" src=\"http://www.kintera.org/atf/cf/{8712CBAB-0EA9-444F-9B5D-35B87FB672ED}/FlagNormal.gif\" width=\"13\" height=\"12\" border=\"0\" alt=\"Normal Priority\" \/>";
				}				
			}
		}
	}
	gradient.appendChild(result_tbl);
	mark = document.getElementById("resultsMarker");
	parent_node = mark.parentNode;
	parent_node.insertBefore(result_list, mark);
}

function sizePage() {
	document.getElementById("__pagesize").selectedIndex = document.getElementById("pageSize").selectedIndex;
	gosize();
}

function getPageSize(contents) {
	 document.getElementById("pageSize").selectedIndex = document.getElementById("__pagesize").selectedIndex;
}

function goBack(num) {
	window.history.go(Number(num));
}
  
/* frames */
function toggleFrame(frame_id_collapse, frame_id_expand, ht1, ht2, hide_id, show_id) {
	document.getElementById(frame_id_collapse).style.height = ht1;
	document.getElementById(frame_id_expand).style.height = ht2;
	document.getElementById(hide_id).style.display = (document.getElementById(hide_id).style.display == "none") ?  "block" : "none";
	document.getElementById(show_id).style.display = (document.getElementById(show_id).style.display == "block") ?  "none" : "block";
}