// this variable names the current window (for using the popUp as a remote)
window.name = "currentWindow";
// this function opens the pop-up window
var newWin;
function popUp(page, name, details) {
	newWin=window.open(page, name, details);
	newWin.focus();
	return false;
}

// print the page:
function printPage() {
	window.print();  
}

// client-side check for a good image file extension:
function checkFileExtension(theFileName) {
	if (theFileName != "") {
		// You can modify the file extentions to which ever file extensions you want to unblock
		// Example you can modify ".jpg" extension to ".bmp" extension if you want to allow ".bmp" file to upload.
		if((theFileName.lastIndexOf(".jpg") == -1) && (theFileName.lastIndexOf(".gif") == -1)) {
			alert("Only GIF and JPG extention files can be uploaded.");
			return false;
		}
	}
	return true;
}


// function checks a select box and insures there is something in it:
function checkSomethingSelected(selectCtrl, errorMessage) {
	var itemsSelected;
	itemsSelected = selectCtrl.options.length;
	if (itemsSelected == 0) {
		alert(errorMessage);
		return false;
	}
	return true;
}

// provided with a named CheckBoxList, either checks or unchecks all checkboxes
function CheckAllBoxes(source, checked) {
	var children = source.all;
	for (var i = 0; i < children.length; i++) {
		var input = children(i);
		//alert('source: ' + input.type);
		if (input.tagName == "INPUT" && input.type == "checkbox") {
			input.checked = checked;
		}
	}
}

// gets whatever radio value is selected by looping through and seeing if an item is checked
function getRadioValue(theRadioList) {
	// alert("radio length: " + theRadioList.length);
	var returnValue = "";
	var itemFound;
	var i = 0;
	while ((i < theRadioList.length) && (!(itemFound))) {
		if (theRadioList[i].checked) {
			returnValue = theRadioList[i].value;
			itemFound = true;
		}
		i++;
	}
	return returnValue;
}

// function uses the routine above to check a radio list for a selection:
function checkRadioSelected(theRadioList, errorMessage) {
	var valueSelected;
	valueSelected = getRadioValue(theRadioList);
	if (valueSelected == "") {
		alert(errorMessage);
		return false;
	}
	return true;
	/*
	var itemFound;
	var i = 0;
	while ((i < theRadioList.length) && (!(itemFound))) {
		if (theRadioList[i].checked) {
			itemFound = true;
		}
		i++;
	}
	if (!(itemFound)) {
		alert(errorMessage);
		return false;
	}
	return true;
	*/
}


// move items from one select box to the other:
function addItems(fromCtrl, toCtrl) {
	var i;
	var j;
	var itemexists;
	var nextitem;

	// step through all items in fromCtrl
	for (i = 0; i < fromCtrl.options.length; i++) {
		if (fromCtrl.options[i].selected) {
			// search toCtrl to see if duplicate
			j = 0;
			itemexists = false;
			while ((j < toCtrl.options.length) && (!(itemexists))) {
				if (toCtrl.options[j].value == fromCtrl.options[i].value) {
					itemexists = true;
					// alert(fromCtrl.options[i].value + " found!");
				}
				j++;
			}
			if (!(itemexists)) {
				// add the item
				nextitem = toCtrl.options.length;
				toCtrl.options[nextitem] = new Option(fromCtrl.options[i].text);
				toCtrl.options[nextitem].value = fromCtrl.options[i].value;
			}
		}
	}
}

function removeItems(fromCtrl) {
	var i = 0;
	var j;
	var k = 0;

	while (i < (fromCtrl.options.length - k)) {
		if (fromCtrl.options[i].selected) {
			// remove the item
			for (j = i; j < (fromCtrl.options.length - 1); j++) {
				fromCtrl.options[j].text = fromCtrl.options[j+1].text;
				fromCtrl.options[j].value = fromCtrl.options[j+1].value;
				fromCtrl.options[j].selected = fromCtrl.options[j+1].selected;
			}
			k++;
		} else {
			i++;
		}
	}
	for (i = 0; i < k; i++) {
		fromCtrl.options[fromCtrl.options.length - 1] = null;
	}
}

// this function takes all the options in a select control and
// reads them into a hidden form field for processing on postback
function makeStringFromSelect(selectCtrl) {
	var i;
	var j = 0;
	var outlist = "";

	for (i = 0; i < selectCtrl.options.length; i++) {
		if (j > 0) {
			outlist = outlist + ";";
		}
		outlist = outlist + selectCtrl.options[i].value;
		j++;
	}
	return outlist;
}

// like function above, but keeps the text & values from the select passed in
function makeTextValuePairFromSelect(selectCtrl) {
	var i;
	var j = 0;
	var seperator = "##";
	var outlist = "";
	
	for (i = 0; i < selectCtrl.options.length; i++) {
		if (j > 0) {
			outlist = outlist + ";";
		}
		// creates string like "1001##Item 1005;1010##Item 1010;" etc.
		outlist = outlist + selectCtrl.options[i].value + seperator + selectCtrl.options[i].text;
		j++;
	}
	return outlist;
}

// move items up or down in a select box - for custom ordering
function swap(up, selectCtrl) {
	var i = selectCtrl.selectedIndex;
	if (i != -1 && selectCtrl.options[i].value != "") {
		var old_value = selectCtrl.options[i].value;
		var old_text = selectCtrl.options[i].text;
		
		if( up == 0 && i < (selectCtrl.options.length - 1)) {
			selectCtrl.options[i].value = selectCtrl.options[i+1].value;
			selectCtrl.options[i].text = selectCtrl.options[i+1].text;
			selectCtrl.options[i+1].value = old_value;
			selectCtrl.options[i+1].text = old_text;
			selectCtrl.selectedIndex++;
		}
		
		else if( up == 1 && i > 0) {
			selectCtrl.options[i].value = selectCtrl.options[i-1].value;
			selectCtrl.options[i].text = selectCtrl.options[i-1].text;
			selectCtrl.options[i-1].value = old_value;
			selectCtrl.options[i-1].text = old_text;
			selectCtrl.selectedIndex--;
		}
	}
}
