/**
 * Contact JavaScript.
 *
 * This file contains the JavaScript for manipulating and validating the
 * standard contact info form.
 *
 * @author Daniel J. Summers <daniel@djs-consulting.com>
 * @version $Revision: 5 $
 * @since 1
 * @package NSXapp
 * @subpackage View
 */

/**
 * Add a phone number.
 *
 * @param $pbSetFocus Whether to set the focus of the fields once the data is added.
 * @return True if successful, false if the number fails validation.
 */
function addPhone(pbSetFocus) {
	
	if (!validatePhone("")) {
		return false;
	}
	
	var oAreaCode = getElement("txtAreaCode");
	var oExchange = getElement("txtExchange");
	var oNumber = getElement("txtNumber");
	var oExtension = getElement("txtExtension");
	var oPhoneType = getElement("txtPhoneType");
	
	// Create the entries.
	var oNewPhone = document.createElement("input");
	oNewPhone.type = "hidden";
	oNewPhone.name = "contactPhone[]";
	oNewPhone.value = oAreaCode.value + "-" + oExchange.value + "-" + oNumber.value + "-" + oExtension.value;
	
	var oNewPhoneType = document.createElement("input");
	oNewPhoneType.type = "hidden";
	oNewPhoneType.name = "contactPhoneType[]";
	oNewPhoneType.value = oPhoneType.options[oPhoneType.selectedIndex].value;
	
	var oNewPhoneId = document.createElement("input");
	oNewPhoneId.type = "hidden";
	oNewPhoneId.name = "contactPhoneId[]";
	oNewPhoneId.value = 0;
	
	// Create a nice display phone number.
	var sPhoneNumber = "(" + oAreaCode.value + ") " + oExchange.value + "-" + oNumber.value;
	if (oExtension.value.length > 0) {
		sPhoneNumber += " x" + oExtension.value;
	}
	
	// Add them to the div.
	var oDiv = getElement("divPhoneNumbers");
	oDiv.innerHTML += "<div class=\"cntr\"><span class=\"nsxFormLabel\">Phone Number</span>: "
			+ sPhoneNumber + " - <span class=\"nsxFormLabel\">Type</span>: "
			+ oPhoneType.options[oPhoneType.selectedIndex].text + "</div>";
	oDiv.appendChild(oNewPhone);
	oDiv.appendChild(oNewPhoneType);
	
	// Show the div.
	oDiv.style.display = "block";
	
	// Set the flag that a phone number has been added.
	getElement("txtAddedPhone").value = "yes";
	
	// Clear the entry fields and set the cursor at the phone number field.
	oExchange.value = oNumber.value = oExtension.value = "";
	oPhoneType.selectedIndex = 0;
	if (pbSetFocus) {
		oExchange.focus();
	}
	return true;
}

/**
 * Validate a phone number (new or existing).
 *
 * @param string psSuffix The suffix for the ID on each field.
 * @return True if valid, false if not.
 */
function validatePhone(psSuffix) {
	
	// Validate the area code.
	if (!validateNumericString(getElement("txtAreaCode" + psSuffix).id, 3, "The area code entered is not valid")) {
		return false;
	}
	
	// Validate the exchange.
	if (!validateNumericString(getElement("txtExchange" + psSuffix).id, 3, "The first part of the phone number is not valid.")) {
		return false;
	}
	
	// Validate the number.
	if (!validateNumericString(getElement("txtNumber" + psSuffix).id, 4, "The second part of the phone number is not valid.")) {
		return false;
	}
	
	// Validate the extension, if entered.
	var oExtension = getElement("txtExtension" + psSuffix);
	oExtension.value = trim(oExtension.value);
	
	if (oExtension.value.length > 0) {
		if (!validateNumericString(oExtension.id, oExtension.value.length, "The extension is not valid.")) {
			return false;
		}
	}
	
	// Make sure a type has been selected.
	if (!validateDropdown(getElement("txtPhoneType" + psSuffix).id, "You must select a phone type.")) {
		return false;
	}
	return true;
}

/**
 * Delete a phone number that existed when the page was displayed.
 *
 * @param int piId The ID of the phone number to delete.
 */
function deletePhone(piId) {
	getElement("divMorePhone").removeChild(getElement("tblPhone" + piId));
}

/**
 * Add an e-mail address.
 *
 * @param $pbSetFocus Whether to set the focus of the fields once the data is added.
 * @return True if successful, false if the e-mail address fails validation.
 */
function addEmail(pbSetFocus) {
	
	if (!validateEmail("")) {
		return false;
	}
	
	var oEmail = getElement("txtEmail");
	var oEmailType = getElement("txtEmailType");
	var oPrimary = getElement("chkPrimary");
	
	// Create the entries.
	var oNewEmail = document.createElement("input");
	oNewEmail.type = "hidden";
	oNewEmail.name = "contactEmail[]";
	oNewEmail.value = oEmail.value;
	
	var oNewEmailType = document.createElement("input");
	oNewEmailType.type = "hidden";
	oNewEmailType.name = "contactEmailType[]";
	oNewEmailType.value = oEmailType.options[oEmailType.selectedIndex].value;
	
	var oNewEmailId = document.createElement("input");
	oNewEmailId.type = "hidden";
	oNewEmailId.name = "contactEmailId[]";
	oNewEmailId.value = 0;
	
	// Add them to the div.
	var oDiv = getElement("divEmailAddresses");
	oDiv.innerHTML += "<div class=\"cntr\"><span class=\"nsxFormLabel\">E-mail Address</span>: "
			+ oEmail.value + " - <span class=\"nsxFormLabel\">Type</span>: "
			+ oEmailType.options[oEmailType.selectedIndex].text + "</div>";
	oDiv.appendChild(oNewEmail);
	oDiv.appendChild(oNewEmailType);
	
	// Show the div.
	oDiv.style.display = "block";
	
	// Set the flag that an e-mail address has been entered.
	getElement("txtAddedEmail").value = "yes";
	
	// Clear the entry fields and set the cursor at the e-mail address field.
	oEmail.value = "";
	oEmailType.selectedIndex = 0;
	
	if (pbSetFocus) {
		oEmail.focus();
	}
	return true;
}

/**
 * Validate an e-mail address (new or existing).
 *
 * @param string psSuffix The suffix for the ID on each field.
 * @return True if it's good, false if not.
 */
function validateEmail(psSuffix) {
	
	var oEmail = getElement("txtEmail" + psSuffix);
	
	// Make sure an e-mail address has been entered.
	if (!validateString(oEmail.id, "You must enter an e-mail address.")) {
		return false;
	}
	
	// Make sure that the e-mail address is valid.
	if ((oEmail.value.indexOf("@") < 0)
	|| (oEmail.value.substring(oEmail.value.indexOf("@")).indexOf(".") < 0)) {
		alert("The e-mail address is not valid - it must be in the form of \"name@example.com\".");
		oEmail.className = "errorField";
		oEmail.focus();
		return false;
	}
	else {
		oEmail.className = "";
	}
	
	// Make sure a type has been selected.
	if (!validateDropdown(getElement("txtEmailType" + psSuffix).id, "You must select an e-mail address type.")) {
		return false;
	}
	return true;
}

/**
 * Delete an e-mail address that existed when the page was displayed.
 *
 * @param int piId The ID of the e-mail address to delete.
 */
function deleteEmail(piId) {
	getElement("divMoreEmail").removeChild(getElement("tblEmail" + piId));
}

/**
 * Hide the address fields for volunteers where no address is known.
 */
function checkNoAddress() {
	if (isChecked("chkNoAddress")) {
		getElement("tblAddress").style.display = "none";
		// Fill with dummy data so it'll pass validation.
		getElement("txtAddress").value = "Address Not Known";
		getElement("txtCity").value = "Unknown";
		getElement("txtState").selectedIndex = 32;
		getElement("txtZip").value = "00000";
	}
	else {
		// Clear the dummy data so the user doesn't know about it.
		getElement("txtAddress").value = getElement("txtCity").value =
				getElement("txtZip").value = "";
		getElement("tblAddress").style.display = "";
	}
}

/**
 * Ensure that contact information is valid.
 *
 * @return True if it's good, false if not.
 */
function validateContact() {
	
	// Address is a required entry.
	if (!validateString("txtAddress", "You must enter an address.")) {
		return false;
	}
	// City is a required entry.
	if (!validateString("txtCity", "You must enter a city.")) {
		return false;
	}
	// State is a required entry.
	if (!validateDropdown("txtState", "You must select a state.")) {
		return false;
	}
	// ZIP code is a required entry.
	if (!validateString("txtZip", "You must enter a ZIP code.")) {
		return false;
	}
	// Check to see if there are unadded phone numbers or e-mail addresses.
	if (!checkForUnaddedPhoneAndEmail()) {
		return false;
	}
	// At least one phone or e-mail address is required.
	if (!validatePhoneAndEmail()) {
		return false;
	}
	return true;
}

/**
 * Add phone or e-mail addresses that have been entered, but the user didn't
 * click "Add Phone" or "Add E-mail Address".
 *
 * @return False if one of the adds fails, true otherwise.
 */
function checkForUnaddedPhoneAndEmail() {
	
	// Check for phone number that has been entered but not saved.
	if ((trim(getElement("txtExchange").value) > "")
			|| (trim(getElement("txtNumber").value) > "")
			|| (trim(getElement("txtExtension").value) > "")) {
		if (!addPhone()) {
			return false;
		}
	}
	
	// Check for e-mail address that has been entered but not saved.
	if (trim(getElement("txtEmail").value) > "") {
		if (!addEmail()) {
			return false;
		}
	}
	
	return true;
}

/**
 * Ensure that the user has entered at least one phone number or one e-mail
 * address, and confirm if they have not entered any of a given type.
 *
 * @return True if phone and e-mail are good (or confirmed good), false if not.
 */
function validatePhoneAndEmail() {
	
	var oPhone = getElement("txtAddedPhone");
	var oEmail = getElement("txtAddedEmail");
	var iExistingPhone = document.getElementsByName("contactPhoneId[]").length;
	var iExistingEmail = document.getElementsByName("contactEmailId[]").length;
	
	if ((oPhone.value == "no") && (oEmail.value == "no") && (iExistingPhone == 0) && (iExistingEmail == 0)) {
		// No phone AND no e-mail has been entered.
		alert("You have not entered any phone numbers or e-mail addresses.  At least one between the two is required.");
		getElement("txtExchange").focus();
		return false;
	}
	
	if ((oPhone.value == "no") && (iExistingPhone == 0)) {
		// No phone has been entered.
		if (!confirm("You have not entered any phone numbers.  Are you sure this is what you mean to do?")) {
			return false;
		}
	}
	
	if ((oEmail.value == "no") && (iExistingEmail == 0)) {
		// No e-mail address has been entered.
		if (!confirm("You have not entered any e-mail addresses.  Without these, the system will not be able "
				+ "to automatically notify the individual.  Are you sure this is what you mean to do?")) {
			return false;
		}
	}
	
	// Check any existing phone numbers.
	var oPhones = document.getElementsByName("contactPhoneId[]");
	for (i = 0; i < oPhones.length; i++) {
		if (oPhones[i].value > 0) {
			if (validatePhone(oPhones[i].value)) {
				// Update the hidden elements, so the updated value is submitted.
				getElement("hidPhone" + oPhones[i].value).value
						= getElement("txtAreaCode" + oPhones[i].value).value + "-"
							+ getElement("txtExchange" + oPhones[i].value).value + "-"
							+ getElement("txtNumber" + oPhones[i].value).value + "-"
							+ getElement("txtExtension" + oPhones[i].value).value;
				getElement("hidPhoneType" + oPhones[i].value).value
						= getElement("txtPhoneType" + oPhones[i].value).value;
			}
			else {
				return false;
			}
		}
	}
	
	// Check any existing e-mail addresses.
	var oEmails = document.getElementsByName("contactEmailId[]");
	for (i = 0; i < oEmails.length; i++) {
		if (oEmails[i].value > 0) {
			if (validateEmail(oEmails[i].value)) {
				// Update the hidden elements, so the updated value is submitted.
				getElement("hidEmail" + oEmails[i].value).value 
						= getElement("txtEmail" + oEmails[i].value).value;
				getElement("hidEmailType" + oEmails[i].value).value
						= getElement("txtEmailType" + oEmails[i].value).value;
			}
			else {
				return false;
			}
		}
	}
	
	return true;
}