function trimMultiValue(val) {// given a string of values e.g val1; val2; val3; ... return val1;val2;val3;... (remove spaces)	while (val.indexOf("; ") != -1) {		val = val.replace("; ", ";");	}	return val;}function toggleSubscription(anchor, id, name) {	var doc = document.forms[0];		var addAry = doc.SubscriptionsAdd.value.split("; ");	var removeAry = doc.SubscriptionsRemove.value.split("; ");	var currentIds = doc.SubscriptionsIDs.value.split("; ");	var changeIds =  doc.SubscriptionsChange.value.split("; ");		var addAry2 = new Array();	var removeAry2 = new Array();	var addAryIds = new Array();	var addAryTitles = new Array();	var removeAryTitles = new Array();		if (anchor.innerText == "unsubscribe" || anchor.innerText == "D\u00E9sabonnez-moi") {		//*** UN-SUBSCRIBE		anchor.disabled = true;		//add to the remove ary		if (arrayGetIndex(currentIds, id) != -1 && arrayGetIndex(removeAry, name + "|" + id) == -1) {			removeAry[removeAry.length] = name + "|" + id;			changeIds[arrayGetIndex(changeIds, id)] = "";		} else {			return;		}				//remove it from the add ary		for (var i=0; i<addAry.length; i++) {			if (addAry[i] != null && addAry[i] != "" && addAry[i] != name + "|" + id ) {				addAry2[addAry2.length] = addAry[i];				addAryIds[addAryIds.length] = addAry[i].substring(addAry[i].indexOf("|")+1);				addAryTitles[addAryTitles.length] = addAry[i].substring(0, addAry[i].indexOf("|"));			}		}		//rebuild the remove aray		for (var i=0; i<removeAry.length; i++) {			if (removeAry[i] != null && removeAry[i] != "") {				removeAry2[removeAry2.length] = removeAry[i];				removeAryTitles[removeAryTitles.length] = removeAry[i].substring(0, removeAry[i].indexOf("|"));			}		}			} else {			//*** SUBSCRIBE		anchor.disabled = true;				//add to the add ary		if (arrayGetIndex(currentIds, id) == -1 && arrayGetIndex(addAry, name + "|" + id) == -1) {					addAry[addAry.length] = name + "|" + id;			changeIds[changeIds.length] = id;		} else {			return;		}				//remove it from the remove ary		for (var i=0; i<removeAry.length; i++) {			if (removeAry[i] != null && removeAry[i] != "" && removeAry[i] != name + "|" + id ) {				removeAry2[removeAry2.length] = removeAry[i];				removeAryTitles[removeAryTitles.length] = removeAry[i].substring(0, removeAry[i].indexOf("|"));			}		}		//rebuild the add aray		for (var i=0; i<addAry.length; i++) {			if (addAry[i] != null && addAry[i] != "") {				addAry2[addAry2.length] = addAry[i];				addAryIds[addAryIds.length] = addAry[i].substring(addAry[i].indexOf("|")+1);				addAryTitles[addAryTitles.length] = addAry[i].substring(0, addAry[i].indexOf("|"));			}		}	}									doc.SubscriptionsAdd.value = addAry2.join("; ");	doc.SubscriptionsRemove.value = removeAry2.join("; ");	doc.SubscriptionsChange.value = changeIds.join("; ");		document.getElementById("reqAdd").innerText = addAryTitles.join("\n");	document.getElementById("reqRemove").innerText = removeAryTitles.join("\n");		}function arrayGetIndex(ary, elem) {	for (var i=0; i<ary.length; i++) {		if (ary[i] == elem) return i;	}	return -1;}function subscriptionsOnClick() {	var doc = document.forms[0];	// clear the existing values; reset in this function every time	doc.SubscriptionsAdd.value = "";	doc.SubscriptionsRemove.value = "";	// get all checkboxes	var checks = document.getElementsByName("SubscriptionsChange");	// get start values as array (IDs)	var startValsStr = doc.SubscriptionsStart.value;	startValsStr = trimMultiValue(startValsStr);	var startVals = startValsStr.split(";");	var found = false;	var reqAdd = new Array();	var reqRemove = new Array();	// for each check, add to the add/remove fields as necessary based on start value	for (var i=0; i<checks.length; i++) {		found = false;		var check = checks[i];		if (check.checked) {			for (var k=0; k<startVals.length; k++) {				if (check.value == startVals[k].split("|")[0]) {					found = true;					break;				}			}			if (!found) {				// add				doc.SubscriptionsAdd.value += check.nextSibling.innerText + "|" + check.value + ";";				reqAdd[reqAdd.length] = check.nextSibling.innerText;			}		} else {			for (var k=0; k<startVals.length; k++) {				if (check.value == startVals[k].split("|")[0]) {					found = true;					break;				}			}			if (found) {				// remove				doc.SubscriptionsRemove.value += check.nextSibling.innerText + "|" + check.value + ";";				reqRemove[reqRemove.length] = check.nextSibling.innerText;			}		}	}	// set UI display	document.getElementById("reqAdd").innerText = reqAdd.join("\n");	document.getElementById("reqRemove").innerText = reqRemove.join("\n");}// check/uncheck all options in a specified checkboxfunction checkboxes(chkbox, checked){	for (var i = 0; i < chkbox.length; i++)	{		chkbox[i].checked = checked;	}}/********** NEW SUBSCRIPTION STUFF - ERIC**********/function loadAllSubscriptionDivs(){	var labels = new Array();		// determine link labels based on global language value	if (gLang == "fr")	{		labels["unsubscribe"] = "D\u00E9sabonnez-moi";		labels["subscribe"] = "Abonnez-moi";		labels["cancelAdd"] = "Annulez";		labels["cancelRemove"] = "Annulez";	}	else	{		labels["unsubscribe"] = "unsubscribe";		labels["subscribe"] = "subscribe";		labels["cancelAdd"] = "cancel";		labels["cancelRemove"] = "cancel";	}		// selected	loadSubscriptionDiv("SubscriptionsIDs", "unsubscribe", labels["unsubscribe"],	"selectedSubs");	// available	loadSubscriptionDiv("SubscriptionsAvailable", "subscribe", labels["subscribe"],	"availableSubs");	// add	loadSubscriptionDiv("SubscriptionsAdd", "cancelAdd", labels["cancelAdd"],	"reqAdd");	// remove	loadSubscriptionDiv("SubscriptionsRemove", "cancelRemove", labels["cancelRemove"],	"reqRemove");}function loadSubscriptionDiv(fieldName, fnName, opName, divName){		/* 2007-10-26: getElementById(fieldName) is broken in Firefox, should use the form element*/	/* // available	var field = document.getElementById(fieldName);	*/	var field = document.forms[0].elements.namedItem(fieldName);		//alert("form " + document.forms[0].name + ", " + fieldName + ": " + (field==null ? "is null" : "name=" + (field.name)) );		var ids = trimMultiValue(field.value);		var html = "<table>";		if (ids != "")	{		ids = ids.split(";");		for (var i = 0; i < ids.length; i++)		{			html += "<tr><td>";			var id = ids[i];			html += gSubscriptions[id] + "&nbsp;&nbsp;</td><td style=\"width:100px;text-align:center\"><a href=\"javascript:" + fnName + "('"			+ id + "')\">" + opName + "</a></td></tr>";		}	}	html += "</table>";	var div = document.getElementById(divName);	div.innerHTML = html;}function subscribe(id){	moveId(id, "SubscriptionsAvailable", "SubscriptionsAdd");	loadAllSubscriptionDivs();}function unsubscribe(id){	moveId(id, "SubscriptionsIDs", "SubscriptionsRemove");	loadAllSubscriptionDivs();}function cancelAdd(id){	moveId(id, "SubscriptionsAdd", "SubscriptionsAvailable");	loadAllSubscriptionDivs();}function cancelRemove(id){	moveId(id, "SubscriptionsRemove", "SubscriptionsIDs");	loadAllSubscriptionDivs();}function moveId(id, source, dest){	var field = document.forms[0].elements.namedItem(source);	var x = field.value.split("; ");	x = removeElement(x, id);	field.value = x.join("; ");		field = document.forms[0].elements.namedItem(dest);	x = field.value;		if (x == "")	x = id;	else			x += "; " + id;	field.value = x;}function removeElement(arr, elem){	var retval = new Array();	var i, j = 0;		for(i = 0; i < arr.length; i++)	{		if (arr[i] != elem) retval[j++] = arr[i];	}		return retval;}
