/*********************************************************************************
SelectMove.js 
re-written by Nancy Capitanio, Langtech Inc.
when form is saved, values will be passed back to a display field and 
a hidden field in the opener's form.
  
The display field will be a comma-separated list.
The hidden field will be a datalist of format key||value::

This function requires the name of the opener's form name and
the names of the opener's two fields that will take the values selected */

function moveOver(formname, select1, select2)  {
	// if no option has been selected, selectedIndex = -1
	var available = eval("document."+formname+"."+select1);
	var choice = eval("document."+formname+"."+select2);
	var boxLength = choice.length;
	var selectedItem = available.selectedIndex;
	if (selectedItem < 0) {
		return false;
	}
	else {
		var selectedText = available.options[selectedItem].text;
		var selectedValue = available.options[selectedItem].value;
		var i;
		var isNew = true;
		if (boxLength != 0) {
			for (i = 0; i < boxLength; i++) {
				thisitem = choice.options[i].value;
				if (thisitem == selectedValue) {
					isNew = false;
					break;
				}
			}
		} 
		if (isNew) {
			newoption = new Option(selectedText, selectedValue, false, false);
			choice.options[boxLength] = newoption;
			available.options[selectedItem] = null;
		}
		var len = available.length;
	}
}

function removeMe(formname, select1, select2, howMany) {
	var available = eval("document."+formname+"."+select1);
	var choice = eval("document."+formname+"."+select2);
	var boxLength = choice.length;
	var availLength = available.length;
	arrSelected = new Array();
	var count = 0;
	for (i = 0; i < boxLength; i++) {
		if (howMany == 'one') {
			if (choice.options[i].selected) {
				arrSelected[count] = choice.options[i].value;
				count++;
			}
		}
		else {
			arrSelected[count] = choice.options[i].value;
			count++;
		}
	}
	var x;
	for (i = 0; i < boxLength; i++) {
		for (x = 0; x < arrSelected.length; x++) {
			if (choice.options[i].value == arrSelected[x]) {
				newoption = new Option(choice.options[i].text, choice.options[i].value, false, false);
				available.options[available.length] = newoption;
				choice.options[i] = null;
			}
		}
		boxLength = choice.length;
	}
}

function moveAllOver(formname, select1, select2)  {
	var avail = eval("document."+formname+"."+select1);
	var assoc = eval("document."+formname+"."+select2);
	var selectedText, selectedValue, done, i;
	if (avail.length > 0) done = false;
	else done = true;
	
	i = 0;
	while (! done){
		selectedText = avail.options[i].text;
		selectedValue = avail.options[i].value;
		newoption = new Option(selectedText, selectedValue, false, false);
		assoc.options[assoc.length] = newoption;
		avail.options[i] = null;
		if (avail.length == 0) done = true;
	}
}

function moveAllBack(formname, select1, select2)  {
	var avail = eval("document."+formname+"."+select1);
	var assoc = eval("document."+formname+"."+select2);
	var selectedText, selectedValue, done, i;
	
	if (assoc.length > 0) done = false;
	else done = true;
	i = 0;
	
	while (! done) {
		selectedText = assoc.options[i].text;
		selectedValue = assoc.options[i].value;
		newoption = new Option(selectedText, selectedValue, false, false);
		avail.options[avail.length] = newoption;
		assoc.options[i] = null;
		if (assoc.length ==  0) done = true;
	}
}

function moveUp(formname, select1)  {
	var list = eval("document."+formname+"."+select1);
	var selectedText, selectedValue, done, i, tmpOpt;
	i = list.selectedIndex;
	if (i < 0) {
		return false;
	}
	else {
		listLen = list.length;
		if (listLen > 1) {
			if (i > 0) {
				selectedText = list.options[i-1].text;
				selectedValue = list.options[i-1].value;
				list.options[i-1].text = list.options[i].text;
				list.options[i-1].value = list.options[i].value;
				list.options[i].text = selectedText;
				list.options[i].value = selectedValue;
				list.selectedIndex = i-1;
			}
		}
	}
}

function moveDown(formname, select1)  {
	var list = eval("document."+formname+"."+select1);
	var selectedText, selectedValue, i;
	i = list.selectedIndex;
	if (i < 0) {
		return false;
	}
	else {
		listLen = list.length;
		if (listLen > 1) {
			if (i < listLen-1) {
				selectedText = list.options[i+1].text;
				selectedValue = list.options[i+1].value;
				list.options[i+1].text = list.options[i].text;
				list.options[i+1].value = list.options[i].value;
				list.options[i].text = selectedText;
				list.options[i].value = selectedValue;
				list.selectedIndex = i+1;
			}
		}
	}
}

function allowOneOver(formname, select1, select2)  {
	// only one item can be in the choice select box
	var available = eval("document."+formname+"."+select1);
	var choice = eval("document."+formname+"."+select2);
	var choiceLength = choice.length;
	var availLength = available.length;
	var selectedItem = available.selectedIndex;
	if (selectedItem < 0) {
		return false;
	}
	else {
		var selectedText = available.options[selectedItem].text;
		var selectedValue = available.options[selectedItem].value;
		if (choiceLength == 0) {
			newoption = new Option(selectedText, selectedValue, false, false);
			choice.options[choiceLength] = newoption;
			available.options[selectedItem] = null;
		} 
		else {
			var replacedText = choice.options[0].text;
			var replacedValue = choice.options[0].value;
			choice.options[0].text = selectedText;
			choice.options[0].value = selectedValue;
			available.options[selectedItem].text = replacedText;
			available.options[selectedItem].value = replacedValue;
		}
	}
}

function saveMe(formname, assocFieldname, openerFormName, openerFormDisplayFieldName, openerFormHiddenFieldName) {
	var choice = eval("document."+formname+"."+assocFieldname);
	var strListValues = "";
	var strDisplayValues = "";
	var boxLength = choice.length;
	for (i = 0; i < boxLength; i++) {
		strListValues = strListValues + choice.options[i].value + "||" + choice.options[i].text + "::";
		strDisplayValues = strDisplayValues + choice.options[i].text + ", ";
	}
	if (strDisplayValues.length >= 2) 
		strDisplayValues = strDisplayValues.substr(0, strDisplayValues.length-2)
	var x = eval("self.opener.document."+openerFormName+"."+openerFormHiddenFieldName);
	x.value = strListValues;
	var x = eval("self.opener.document."+openerFormName+"."+openerFormDisplayFieldName);
	x.value = strDisplayValues;
	//alert("values are: " + strDisplayValues);
}

function saveMeNoWindow(formname, assocFieldname, openerFormName, openerFormDisplayFieldName, openerFormHiddenFieldName) {
	var choice = eval("document."+formname+"."+assocFieldname);
	var strListValues = "";
	var strDisplayValues = "";
	var boxLength = choice.length;
	for (i = 0; i < boxLength; i++) {
		strListValues = strListValues + choice.options[i].value + "||" + choice.options[i].text + "::";
		strDisplayValues = strDisplayValues + choice.options[i].text + ", ";
	}
	if (strDisplayValues.length >= 2) 
		strDisplayValues = strDisplayValues.substr(0, strDisplayValues.length-2)
	var x = eval("document."+openerFormName+"."+openerFormHiddenFieldName);
	x.value = strListValues;
	var x = eval("document."+openerFormName+"."+openerFormDisplayFieldName);
	x.value = strDisplayValues;
}

function saveMeLocally(formname, assocFieldname, formHiddenFieldName, availFieldname, formHiddenAvailFieldname) {
	var choice = eval("document."+formname+"."+assocFieldname);
	var strListValues = "";
	var boxLength = choice.length;
	for (i = 0; i < boxLength; i++) {
		if (strListValues.length == 0)
			strListValues = strListValues + choice.options[i].value;
		else
			strListValues = strListValues + "," + choice.options[i].value;
	}
	var x = eval("document."+formname+"."+formHiddenFieldName);
	x.value = strListValues;
	if (availFieldname.length > 0) {
		choice = eval("document."+formname+"."+availFieldname);
		strListValues = "";
		boxLength = choice.length;
		for (i = 0; i < boxLength; i++) {
			if (strListValues.length == 0)
				strListValues = choice.options[i].value;
			else
				strListValues = strListValues + "," + choice.options[i].value;
		}
		x = eval("document."+formname+"."+formHiddenAvailFieldname);
		x.value = strListValues;
	}
}

/*********************************************************************************
open_window.js
open pop-up window, either with or without scroll bars
*/

function openNew(URL,w,h) {
	var winlft = (screen.availWidth - w)*0.3;
	var wintop = (screen.availHeight - h)*0.3;
	var Attributes =  "toolbar=0,status=0,menubar=0,scrollbars=0,width=" + w + ",height=" + h + ",left=" + winlft + ",top=" + wintop	
	var newWin = window.open(URL, "_blank", Attributes);
	newWin.focus();
}

function openNewWithScroll(URL,w,h) {
	var winlft = (screen.availWidth - w)*0.3;
	var wintop = (screen.availHeight - h)*0.3;
	var Attributes =  "toolbar=0,status=0,menubar=0,scrollbars=1,resizable=1,width=" + w + ",height=" + h + ",left=" + winlft + ",top=" + wintop	
	var newWin = window.open(URL, "_blank", Attributes);
	newWin.focus();
}

/*********************************************************************************
check/uncheck all boxes
*/

function check_uncheck_all (f, check_uncheck) {
	var ischecked = true;
	if (check_uncheck == 0)
		var ischecked = false;
	for (i = 0; i < f.elements.length; i++) {
		if (f.elements[i].type == "checkbox")
			f.elements[i].checked = ischecked;
	}
}

/***********************************************************************************
for view only pages
*/

function disablePage() {
	var obj, f;
	//alert(document.forms.length);
	for (var i=0; i<document.forms.length; i++) {
		f = document.forms[i];
		//alert(f.name);
		for (var j=0; j<f.elements.length; j++) {
			obj = f.elements[j];
			obj.disabled = true;
			
			/*if (obj.type == "radio" || obj.type == "checkbox") {
				alert(obj.name);
				for (var j=0; j < obj.length; j++) {
					obj[j].disabled = true;
				}
			}
			else obj.disabled = true; */
		}
	}
}

/********************************************************************************
show/hide div 
*/

function showhide(whichDiv,hideDiv){
	document.getElementById(hideDiv).style.display = "none";
	document.getElementById(whichDiv).style.display = "block";
	if (whichDiv == "newmember"){
		document.getElementById("nmTXT").className = "cActive";
		document.getElementById("cmTXT").className = "cInactive";
	}
	else {
		document.getElementById("cmTXT").className = "cActive";
		document.getElementById("nmTXT").className = "cInactive";
	}
}
