var inputValue = [];

function getValue(elementId) {
	value = document.getElementById(elementId).value;
	return value;
}

function inputIsBlank(elementId) {
	if (getValue(elementId) == "" || getValue(elementId) == null) {
		return true;
	} else {
		return false;
	}
}

function setFocus(elementId) {
	document.getElementById(element_id).focus
}

function focusInput(elementId, type) {
	document.getElementById(elementId).style.color = '#000000';
	if (type != "pwd") {
		if (!inputIsBlank(elementId)) {
			if (inputValue[elementId] == null || inputValue[elementId] == "") {
				inputValue[elementId] = getValue(elementId);
				document.getElementById(elementId).value = "";
			} else {
				if (getValue(elementId) == inputValue[elementId]) {
					document.getElementById(elementId).value = "";
				}
			}
		}
	} else { // If Password field...
			if (inputValue[elementId] == null || inputValue[elementId] == "") {
				inputValue[elementId] = getValue(elementId);
				document.getElementById(elementId).value = "";
				document.getElementById(elementId).setAttribute('type', 'password');
			} else {
				if (getValue(elementId) == inputValue[elementId]) {
					changeInputType(elementId, "password");
					//document.getElementById(elementId).setAttribute('type', 'password');
					document.getElementById(elementId).value = "";
					document.getElementById(elementId).focus();
				}
			}
	}
	document.getElementById(elementId).focus();
}

function blurInput(elementId, type) {
	if (inputIsBlank(elementId)) {
		if (document.getElementById(elementId).type == "password") changeInputType(elementId, "text");
		document.getElementById(elementId).value = inputValue[elementId];
		document.getElementById(elementId).style.color = '#555555';
	}
}

function focusSelect(elementId) {
	document.getElementById(elementId).style.color = '#000000';
}

function blurSelect(elementId) {
	if(inputValue[elementId] == document.getElementById(elementId).value) {
		document.getElementById(elementId).style.color = '#555555';
	}
}

function changeInputType(oldElement, oType) {
	var newObject = document.createElement('input');
	var oldObject = document.getElementById(oldElement);
	newObject.type = oType;
	if(oldObject.size) newObject.size = oldObject.size;
	if(oldObject.value) newObject.value = oldObject.value;
	if(oldObject.name) newObject.name = oldObject.name;
	if(oldObject.id) newObject.id = oldObject.id;
	if(oldObject.onfocus) newObject.onfocus = oldObject.onfocus;
	if(oldObject.onblur) newObject.onblur = oldObject.onblur;
	if(oldObject.className) newObject.className = oldObject.className;
	oldObject.parentNode.replaceChild(newObject,oldObject);
	return newObject;
}

function submitForm() {
	netreg = document.getElementsById("netreg");
	for (var counter = 0; counter < netreg.length; counter++) {
		alert('test');
//		if (netreg.elements[i].value == "" || netreg.elements[i].value == null) {
//			alert(netreg.elements[i].id);
//		}
	}
	return false;
}

// var thisFormArray = ['input1', 'input2', 'input3', 'input4']; (Required IDs)
function required(formArray) {
	var blank = null;
	var inputObj = null;
	for (input in formArray) {
		inputId = formArray[input];
		inputObj = document.getElementById(inputId);
		switch(inputObj.type){
			case "text":
			case "textarea":
				if (inputObj.value == "" || inputObj.value == null || inputObj.value == inputValue[inputId]) {
					if (blank == null || blank == "") {
						blank = inputValue[inputId];
					} else {
						blank += ", " + inputValue[inputId];
					}
				}
				break;
			case "select-one":
				if (inputObj.selectedIndex == -1 || inputObj.selectedIndex == 0) { //inputObj.options[inputObj.selectedIndex].text == ""
					if (blank == null || blank == "") {
						blank = inputObj.options[0].text;
					} else {
						blank += ", " + inputObj.options[0].text;
					}
				}
				break;
//			case "select-multiple":
//				if (inputObj.selectedIndex == -1){
//					alertMsg += " - " + fieldDescription[i] + "\n";
//				}
//				break;
			default:
		}
//		if (inputObj.type == undefined) {
//			var blnchecked = false;
//			for (var j = 0; j < inputObj.length; j++){
//				if (inputObj[j].checked){
//					blnchecked = true;
//				}
//			}
//			if (!blnchecked) {
//				alertMsg += " - " + fieldDescription[i] + "\n";
//			}
//		}
	}
	if (blank) {
		alert("Missing required fields: " + blank);
		return false;
	} else {
		return true;
	}
}

function carAppNotes(carAppID) {
	var notesContainer = document.getElementById('carapp-notes');
	var editLink = document.getElementById('carapp-notes-link');
	var notesContent = document.getElementById('carapp-notes-content');
	var notesEditor = document.getElementById('carapp-notes-editor');
	if (editLink.innerHTML == 'Edit') {
		var notes = trim(notesContent.innerHTML);
		var newHTML = "<textarea id='carapp-notes-editor'>" + notes + "</textarea><br>" +
		"<a href='#' onclick='carAppNotes(\"" + carAppID + "\");' style='font-size:11px;'>" +
		"<span id='carapp-notes-link'>Done</span></a>";
	} else {
		var notes = trim(notesEditor.value);
		if (notes == null || notes == "") notes = "There are no notes about this applicant.";
		var newHTML = "<pre id='carapp-notes-content'>" + notes + "</pre>&nbsp; " +
		"<a href='#' onclick='carAppNotes(); return false;' style='font-size:11px;'>" +
		"<span id='carapp-notes-link'>Edit</span></a>";
		ajax.post("appviewer/user-app-edit.php", "carapp_id=" + carAppID + "&carapp_notes=" + escape(notes),
		function(response) { if (response != "true") alert("There was an error saving. Please try again.") });
	}
	notesContainer.innerHTML = newHTML;
}

function toggleRead(carAppID) {
	var readStatus = document.getElementById("carapp-read-status");
	var readImage = document.getElementById("carapp-read-image");
	var status = null;
	switch (readStatus.innerHTML) {
		case "Read":
			status = "no";
			readStatus.innerHTML = "Unread";
			readImage.innerHTML = "<img src='/images/icon-mail-unread.png' border='0'>";
			break;
		default:
		case "Unread":
			status = "1";
			readStatus.innerHTML = "Read";
			readImage.innerHTML = "<img src='/images/icon-mail-read.png' border='0'>";
			break;
	}
	ajax.post("appviewer/user-app-edit.php", "carapp_id=" + carAppID + "&carapp_viewed=" + escape(status), function(response) {
		if (response != "true") alert("There was an error changing the status.")
	});
}

function togglePriority(carAppID) {
	var priorityStatus = document.getElementById("carapp-priority-status");
	var priorityImage = document.getElementById("carapp-priority-image");
	var status = null;
	switch (priorityStatus.innerHTML) {
		case "Blank":
			status = "1";
			priorityStatus.innerHTML = "Incomplete";
			priorityImage.innerHTML = "<img src='/images/icon-blue.png' border='0'>";
			break;
		case "Incomplete":
			status = "2";
			priorityStatus.innerHTML = "No";
			priorityImage.innerHTML = "<img src='/images/icon-red.png' border='0'>";
			break;
		case "No":
			status = "3";
			priorityStatus.innerHTML = "Maybe";
			priorityImage.innerHTML = "<img src='/images/icon-yellow.png' border='0'>";
			break;
		case "Maybe":
			status = "4";
			priorityStatus.innerHTML = "Yes";
			priorityImage.innerHTML = "<img src='/images/icon-green.png' border='0'>";
			break;
		default:
		case "Yes":
			status = "no";
			priorityStatus.innerHTML = "Blank";
			priorityImage.innerHTML = "<img src='/images/icon-grey.png' border='0'>";
			break;
	}
	ajax.post("appviewer/user-app-edit.php", "carapp_id=" + carAppID + "&carapp_priority=" + escape(status), function(response) {
		if (response != "true") alert("There was an error changing the status.");
	});
}

function carAppDelete(carAppID) {
	ajax.post("appviewer/user-app-edit.php", "carapp_id=" + carAppID + "&carapp_delete=delete", function() {
		if (response == "true") {
			location.href = "viewapp.php";
		} else {
			alert("There was an error deleting the application.");
		}
	});
}

