/**
 * Donate Form JavaScript.
 *
 * This JavaScript file manipulates and validates the Donate Stuff form.
 *
 * @author Daniel J. Summers <daniel@djs-consulting.com>
 * @version $Revision: 5 $
 * @since 1
 * @package NSXapp
 * @subpackage View
 */

function setCursorOnLookUpAddress() {
	getElement("txtLookupEmail").focus();
}

function lookUpAddress() {
	var sUrl = getElement("url").value + getElement("txtLookupEmail").value;
	runAjaxRequest(sUrl, setResults);
}

/**
 * Set Results.
 *
 * Tell the user their lookup was successful, or tell them that their address
 * was not found.
 */
function setResults() {
	
	if (oXmlRequest.readyState == 4) {
		if (oXmlRequest.responseText == "no") {
			alert("No volunteer was found with that e-mail address.\n"
					+ "You may try again, or enter your contact information below.");
		}
		else {
			doc = parseXmlDocument(oXmlRequest.responseText);
			alert("Your volunteer ID was found - you do not need to enter any contact information");
			getElement("fldVolunteerLookup").style.display =
					getElement("divDonorInformation").style.display = "none";
			getElement("divLookedUpName").style.display = "block";
			getElement("divLookedUpName").innerHTML +=
					doc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
			getElement("txtLookedUpId").value =
					doc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
		}
	}
}

/**
 * Check Delivery Times.
 *
 * Show or hide the stuff drop-off dates based on the value of the checkbox
 * indicating that the donor has items to drop off.
 */
function checkDeliveryTimes() {
	if (isChecked("chkDeliver")) {
		getElement("divDropOffSchedule").style.display = "block";
	}
	else {
		getElement("divDropOffSchedule").style.display = "none";
	}
}

/**
 * Check Early Date.
 *
 * Identify "Date" as a required field if the "drop off early" box is checked.
 */
function checkEarlyDate() {
	if (isChecked("chkEarly")) {
		getElement("lblEarlyDate").className = "required";
		getElement("txtEarlyDate").focus();
	}
	else {
		getElement("lblEarlyDate").className = "";
	}
}

/**
 * Validate Form.
 *
 * Ensure that valid information is entered.
 */
function validateForm() {
	
	if (getElement("txtLookedUpId").value == "") {
		// Name and contact info must be valid.
		if (!validateString("txtFirstName", "You must enter your first name.")) {
			return false;
		}
		if (!validateString("txtLastName", "You must enter your last name.")) {
			return false;
		}
		if (!validateContact()) {
			return false;
		}
	}
	
	// One of the "what and when" boxes must be checked.
	if ((!isChecked("chkDeliver")) && (!isChecked("chkPickup"))
			&& (!isChecked("chkEarly")) && (!isChecked("chkValuable"))) {
		alert("You have not indicated that you have any stuff to donate!");
		getElement("chkDeliver").focus();
		return false;
	}
	
	// If "Deliver" is checked, a timeframe must be selected.
	if (isChecked("chkDeliver")) {
		if (!validateOptionGroup("donDropOff", "You must select a time to drop off your stuff.", "", "")) {
			return false;
		}
	}
	
	// If "Must Donate Early" is checked, a valid date must be entered.
	if (isChecked("chkEarly")) {
		if (!validateDate("txtEarlyDate", "The Last Possible Drop-Off Date is not valid.")) {
			return false;
		}
	}
	
	// If there are no comments in the box, ensure at least one item has a
	// non-zero, non-blank quantity in it.
	if (getElement("txtComments").value == "") {
		var bOneEntered = false;
		var oQtys = document.getElementsByName("donItemQuantity[]");
		for (i = 0; i < oQtys.length; i++) {
			if ((oQtys[i].value > "") && (isNumeric(oQtys[i].value))) {
				bOneEntered = true;
				break;
			}
		}
		if (!bOneEntered) {
			alert("You have not entered any items to donate, nor any comments.\n"
					+ "We can't read your mind!  :)");
			oQtys[0].focus()
			return false;
		}
	}
	
	// It's good!
	return true;
}