var DHTML = (document.getElementById || document.all || document.layers);
var ie = false;

if (document.all) {
	ie = true;
}

function changeTaskFilter(form, name, task) {
	if (name.lastIndexOf('.') != -1) {
		name = name.substr(0, name.lastIndexOf('.'));
	}

	form[name + ".task"].value = task;
	form.submit();
}

function checkAll(element, all) {
	if (element == null) {
		return;
	}

	var checked = all.checked;

	if (typeof(element[0]) == "object") {
		for (var i = 0; i < element.length; i++) {
			element[i].checked = checked;
		}
	} else {
		element.checked = checked;
	}
}

function selectAll(select) {
	var options = select.options;
	
	for (var i = 0; i < options.length; i++) {
		options[i].selected = true;
	}
}

function openPopupScrollable(page, name, width, height) {
	if (width == null) {
		width = 900;
	}

	if (height == null) {
		height = 800;
	}

	var popup = window.open(page, name, "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes");
	popup.focus();
}

function valueEngineSearch(form) {
	var popup = window.open("http://www.valuengine.com/rep/searchnc?srchfor=" + form.srchfor.value, "purchase", "menubar=yes,status=yes,toolbar=yes,width=900,height=800,resizable=yes,scrollbars=yes,");
	popup.focus();
}

function openPopup(page, name, width, height) {
	if (width == null) {
		width = 900;
	}

	if (height == null) {
		height = 800;
	}

	var popup = window.open(page, name, "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes");
	popup.focus();
}

function jumpMenu(form){
	var url = document.form.site.options[document.form.site.selectedIndex].value;
	window.location.href = url;
}

function getObj(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.all) {
		return document.all[id];
	} else {
		return null;
	}
}

function addClass(elementId, className) {
	var element = getObj(elementId);
	if (element != null) {
		element.className += (element.className.length > 0 ? ' ' : '') + className;
	}
}

function idPrint(id, text) {
	if (! DHTML) {
		return;
	}

	var obj = getObj(id);

	obj.innerHTML = "";
	obj.innerHTML = text;
}

/*
	help
*/
function help(action, page, anchor) {
	openPopup(action + "?helpPage=" + page + "#" + anchor, "helpWin");
}

function doHelp(page) {
	openPopup(page, "helpWin");
}

/*
	list manager for add, addAll, remove, removeAll
*/

/* assumes that both lists are already sorted because this will slow it down */
function selectMove(srcSelect, destSelect, all) {
	var tmp = new Array(0);
	var i = 0;
	var srcOption = null;
	var destOption = null;

	while (true) {
		if (i < srcSelect.length && srcOption == null) {
			if (all) {
				srcOption = srcSelect.options[i];
			} else {
				for (; i < srcSelect.length; i++) {
					if (srcSelect.options[i].selected) {
						srcOption = srcSelect.options[i];
						break;
					}
				}
			}
		}

		if (destSelect.length > 0 && destOption == null) {
			destOption = destSelect[0];
		}

		if (srcOption == null && destOption == null) {
			break;
		}

		if (destOption == null || (srcOption != null && srcOption.text < destOption.text)) {
			tmp.push(new Option(srcOption.text, srcOption.value));
			srcOption = null;
			srcSelect.options[i] = null;
		} else if (srcOption == null || (destOption != null && destOption.text < srcOption.text)) {
			tmp.push(new Option(destOption.text, destOption.value));
			destOption = null;
			destSelect.options[0] = null;
		} else {
			tmp.push(new Option(destOption.text, destOption.value));
			destOption = null;
			srcOption  = null;
			srcSelect.options[i] = null;
			destSelect.options[0] = null;
		}
	}

	for (i = 0; i < tmp.length; i++) {
		destSelect.options.length++;
		destSelect.options[i].text	= tmp[i].text;
		destSelect.options[i].value = tmp[i].value;
	}
}

function listDelete(srcSelect, all) {
	if (all) {
		srcSelect = 0;
	} else {
		for (var i = 0; i < srcSelect.length; ) {
			if (srcSelect.options[i].selected) {
				srcSelect.options[i] = null;
			} else {
				i++;
			}
		}
	}
}

function optionSort(a, b) {
	if (a.text < b.text) {
		return -1;
	} else if (a.text > b.text) {
		return 1;
	} else {
		return 0;
	}
}

function isEnterKey(e) {
	var key = null;

	if (ie) {
		key = event.keyCode;
	} else {
		var key = e.which;
	}

	if(key == 13){
		return true;
	} else {
		return false;
	}
}

function saveList(form, cusipList) {
	form.cusipList.value = cusipList;
	openPopup("", "ncSaveList", 400, 250);

	form.submit();
}

function trim(string) {
	string = string.replace(/^\s+/, "");
	string = string.replace(/\s+$/, "");

	return string;
}

function isValidDecimal(strValue) {
	var match = strValue.match(/^-?(\d+\.?\d*|\.\d+)$/);  

	return match != null;
}
