function isEmpty(input) {
	return (!input || input == "" || input == null || input == "null");
}
function verifySearchInput(inputName) {
	var inputEl = document.getElementById(inputName);
	if (isEmpty(inputEl.value)) {
		alert("Please enter a search value.");
	}
}
function getValue(elementId) {
	var el = document.getElementById(elementId);
	if (el) {
		return el.value;
	}
	return null;
}
function getSelectedValue(selectId) {
	var selectEl = document.getElementById(selectId);
	var index = selectEl.selectedIndex;
	var selectedValue = "";
	if (index != -1 && !isEmpty(selectEl.options[index].value)) {
		selectedValue = selectEl.options[index].value;
	}
	return selectedValue;
}
function setValue(elementId, value) {
	var el = document.getElementById(elementId);
	if (el) {
		el.value = value;
	}
}
function disableInput(inputEl) {
	inputEl.disabled = true;
}
function submitForm(methodValue, formIndex) {
	if (!isEmpty(methodValue)) {
		var methodEl = document.getElementById("method");
		methodEl.value = methodValue;
	}
	var index = 0;
	if (!isEmpty(formIndex)) {
		index = formIndex;
	}
	var formEl = eval("document.forms[" + index + "]");
	formEl.submit();
}
function selectionVerified(selectId, labelStr) {
	var selectEl = document.getElementById(selectId);
	var selected = false;
	if (selectEl) {
		selected = selectEl.selectedIndex != -1;
	}
	if (!selected) {
		if (labelStr) {
			alert("Please select a " + labelStr + ".");
		} else {
			alert("Please make a selection.");
		}
	}
	return selected;
}
function deleteConfirmed(label) {
	return confirm("Are you sure you want to delete the selected " + label
			+ "?");
}
function selectAllOptions(selectId) {
	var selectEl = document.getElementById(selectId);
	for ( var i = 0; i < selectEl.options.length; i++) {
		selectEl.options[i].selected = true;
	}
}
function moveOption(selectFromId, selectToId) {
	var selectFrom = document.getElementById(selectFromId);
	var selectTo = document.getElementById(selectToId);
	var index = selectFrom.selectedIndex;
	if (index == -1) {
		return;
	}
	var selectedOption = selectFrom.options[index];
	var movedOption = new Option(selectedOption.text, selectedOption.value);
	selectFrom.options[index] = null;
	var newIndex = selectTo.length;
	selectTo.length = newIndex + 1;
	selectTo.options[newIndex] = movedOption;
	selectFrom.selectedIndex = -1;
	selectTo.selectedIndex = -1;
}
function moveOptions(selectFromId, selectToId) {
	var selectFrom = document.getElementById(selectFromId);
	var selectTo = document.getElementById(selectToId);
	var selectedIndices = new Array();
	for ( var i = 0; i < selectFrom.options.length; i++) {
		if (selectFrom.options[i].selected) {
			selectedIndices[i] = "Y";
			var selectedOption = selectFrom.options[i];
			var movedOption = new Option(selectedOption.text,
					selectedOption.value);
			var newIndex = selectTo.length;
			selectTo.length = newIndex + 1;
			selectTo.options[newIndex] = movedOption;
		}
	}
	for ( var i = selectedIndices.length - 1; i >= 0; i--) {
		if ("Y" == selectedIndices[i]) {
			selectFrom.options[i] = null;
		}
	}
	selectFrom.selectedIndex = -1;
	selectTo.selectedIndex = -1;
}

XmlHttp = function() {
	var xmlHttp = null;
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlHttp = false;
		}
	}
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			xmlHttp = false;
		}
	}
	return xmlHttp;
}
function sendAjaxRequest(url) {
	var xmlHttp = new XmlHttp();
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = function() {
		handleResponse(xmlHttp);
	}
	xmlHttp.send(null);
}
function handleResponse(xmlHttp) {
	if (xmlHttp.readyState == 4) {
		var responseText = xmlHttp.responseText;
		// alert(responseText);
		eval(responseText);
	}
}
function clearSelect(selectId, blankOptionText, blankOptionValue) {
	var selectEl = document.getElementById(selectId);
	var startIndex = 0;
	if (blankOptionText) {
		var option = new Option();
		option.value = blankOptionValue;
		option.text = blankOptionText;
		selectEl.options[0] = option;
		startIndex++;
	}
	for ( var i = startIndex; i < selectEl.options.length; i++) {
		selectEl.options[i] = null;
	}
	selectEl.options.length = blankOptionText ? 1 : 0;
}
function populateEquipmentCities(blankOptionText, updateEquipmentList) {
	var selectedState = getSelectedValue("states");
	sendAjaxRequest("/OnlineServices/equipment?method=populateCities&state="
			+ selectedState + "&blankOptionText=" + blankOptionText
			+ "&updateList=" + updateEquipmentList);
}
function populateEquipmentTerminals(blankOptionText, updateEquipmentList) {
	var selectedCity = getSelectedValue("cities");
	sendAjaxRequest("/OnlineServices/equipment?method=populateTerminals&city="
			+ selectedCity + "&blankOptionText=" + blankOptionText
			+ "&updateList=" + updateEquipmentList);
}
function updateEquipmentList() {
	var selectedTerminal = getSelectedValue("terminals");
	sendAjaxRequest("/OnlineServices/equipment?method=updateEquipmentList&terminal="
			+ selectedTerminal);
}
function deleteTableRows(tableId, startIndex, endIndex) {
	var tableEl = document.getElementById(tableId);
	for ( var i = endIndex; i >= startIndex; i--) {
		tableEl.deleteRow(i);
	}
}
function hideTableRows(tableId, startIndex) {
	var tableEl = document.getElementById(tableId);
	for ( var i = startIndex; i < tableEl.rows.length; i++) {
		tableEl.rows[i].style.display = 'none';
	}
}
function selectEquipment(tableRow) {
	tableRow.className = 'selectedRow';
	setValue('description', tableRow.cells[0].innerHTML);
	deselectEquipmentRows(tableRow, 2);
}
function deselectEquipmentRows(tableRow) {
	var standardTable = document.getElementById('standard');
	for ( var i = 2; i < standardTable.rows.length; i++) {
		if (standardTable.rows[i] != tableRow) {
			standardTable.rows[i].className = i % 2 == 0 ? "listEvenRow"
					: "listOddRow";
		}
	}
	var specialTable = document.getElementById('special');
	for ( var i = 2; i < specialTable.rows.length; i++) {
		if (specialTable.rows[i] != tableRow) {
			specialTable.rows[i].className = i % 2 == 0 ? "listEvenRow"
					: "listOddRow";
		}
	}
}
function editEquipment() {
	if (isEmpty(getValue('terminals')) && isEmpty(getValue('description'))) {
		alert("Select a terminal and the equipment item to edit.");
	} else if (isEmpty(getValue('terminals'))) {
		alert("Select a terminal.");
	} else if (isEmpty(getValue('description'))) {
		alert("Select the equipment item to edit.");
	} else {
		submitForm('editEquipment');
	}
}
function deleteEquipment() {
	if (isEmpty(getValue('terminals')) && isEmpty(getValue('description'))) {
		alert("Select a terminal and the equipment item to delete.");
	} else if (isEmpty(getValue('terminals'))) {
		alert("Select a terminal.");
	} else if (isEmpty(getValue('description'))) {
		alert("Select the equipment item to delete.");
	} else {
		if (confirm("Are you sure you want to delete the selected equipment item?")) {
			submitForm('deleteEquipment');
		}
	}
}
function selectTerminal(tableRow) {
	tableRow.className = 'selectedRow';
	setValue('terminalNum', tableRow.cells[0].innerHTML);
	deselectTerminalRows(tableRow, 1);
}
function deselectTerminalRows(tableRow, startRow) {
	var terminalTable = document.getElementById('terminals');
	for ( var i = startRow; i < terminalTable.rows.length; i++) {
		if (terminalTable.rows[i] != tableRow) {
			terminalTable.rows[i].className = i % 2 == 0 ? "listEvenRow"
					: "listOddRow";
		}
	}
}
function editTerminal() {
	if (isEmpty(getValue('terminalNum'))) {
		alert("Select a terminal to edit.");
	} else {
		submitForm('editTerminal');
	}
}
function deleteTerminal() {
	if (isEmpty(getValue('terminalNum'))) {
		alert("Select a terminal to delete.");
	} else {
		submitForm('deleteTerminal');
	}
}
function updateTerminalList() {
	var selectedCity = getSelectedValue("cities");
	sendAjaxRequest("/OnlineServices/terminals?method=updateTerminalList&city="
			+ selectedCity);
}
function populateTerminalCities(blankOptionText, updateTerminalList) {
	var selectedState = getSelectedValue("states");
	sendAjaxRequest("/OnlineServices/terminals?method=populateCities&state="
			+ selectedState + "&blankOptionText=" + blankOptionText
			+ "&updateList=" + updateTerminalList);
}
function validateTerminalNumber() {
	var terminalNumber = document.getElementById("terminalnumber");
	sendAjaxRequest("/OnlineServices/terminals?method=validateTerminalNumber&terminalNumber="
			+ terminalNumber.value);
}

function createCookie(name, value, days) {
	var expires = "";
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));

		// use this to test - adds seconds instead of days
		//date.setTime(date.getTime()+(days*1000));

		expires = "; expires=" + date.toGMTString();
	} else {
		// set an arbitrarily large date so that cookie won't expire for a couple of years
		var d = new Date(2012, 12, 31, 23, 0, 0, 0);
		expires = "; expires=" + d.toGMTString();
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for ( var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ')
			c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

function cookieCheck() {
	if (readCookie("uvLogisticsRedirect") == null) {
		createCookie("uvLogisticsRedirect", "false");
		window.location = "http://www.uvlogistics.com/landing.html";
	}
}