/// <Reference Path="/CustomSource/JavaScript/jquery-1.4.1-vsdoc.js"/>
// JavaScript Document for /am/customsource/sbw/leadership_opps.cfm

//Function that allows multiple calls to functions upon loading of the web page
function addOnloadEvent(fnc) {
	if ( typeof window.addEventListener != "undefined" )
		window.addEventListener( "load", fnc, false );
	else if ( typeof window.attachEvent != "undefined" ) {
		window.attachEvent( "onload", fnc );
	}
	else {
		if ( window.onload != null ) {
			var oldOnload = window.onload;
			window.onload = function ( e ) {
				oldOnload( e );
				window[fnc]();		
			};
		}
		else 
			window.onload = fnc;
	}
}
// Returns the value of a radio input.
function getValueOfRadio() {
	var val = '';
	for (i = 0; i < document.form_leadership_opps.leadership_groupRType.length; i++) {
		if (document.form_leadership_opps.leadership_groupRType[i].checked == true) {
			val = document.form_leadership_opps.leadership_groupRType[i].value;
			break;
		}
	}
	return val;
}

function leadership_content_swap(currentItem,totalItems,baseNameDiv) { // Make sure we have numeric values for those that should be numeric.
	if (isNumeric(currentItem) && isNumeric(totalItems)) { // Make sure we have numeric values for those that should be numeric.
		var divContentDisplay = document.getElementById('leadership_link_content');
		divContentDisplay.setAttribute('style','display:block;');
		divContentDisplay.style.display = 'block';

		var divContentDisplay = document.getElementById('leadership_content');
		divContentDisplay.setAttribute('style','display:none;');
		divContentDisplay.style.display = 'none';

		for (var x = 1; x <= totalItems; x++) {
			tempObject = document.getElementById(baseNameDiv + x);
			tempObject.setAttribute('style','display:none;');
			tempObject.style.display = 'none';
		}
		if (currentItem <= totalItems) {
			tempObject = document.getElementById(baseNameDiv + currentItem);
			tempObject.setAttribute('style','display:block;');
			tempObject.style.display = 'block';
		}
	}
}

function submitTheForm() {
	importDatabaseRecords(getValueOfRadio(), document.getElementById('GroupName').value);
}
//Function that loads the drop downs with default values of All being assigned and None for the called from parameter
function loadDropDowns() {
	if ($) {
		$('#leadership_link_content').css('display', 'block');
		leadership_content_swap(1, 3, 'leadership_link_');
		$('#leadership_content').css('display', 'none');
		$('#leadership_block_static').css('display', 'none');

		$('#leadership_groupName').html("<div id='GroupName'>Loading ...</div>");

		importDropDowns('All', 'All', 'LoadTheDropDowns');
	}
}

//Calls the onload function to load the drop downs upon loading of the web page
addOnloadEvent(loadDropDowns);

//Populate the listing of groups based upon what type is selected.
function importDropDowns(GroupTypes, GroupNames, CalledFrom) {
	if ($) {
		$.ajax({
			url: "/am/customsource/sbw/leadership_opps_process.cfm?GroupTypes=" + GroupTypes + "&GroupNames=" + GroupNames + "&CalledFrom=" + CalledFrom + "&theRequest=getDropDowns",
			success: function (data) {
				var htmlContent = "";
				htmlContent += "<select id='GroupName' name='GroupName' onchange='importDropDowns(getValueOfRadio(), document.getElementById(\"GroupName\").value, \"TypeDropDown\");submitTheForm();' style='width:287px;'>";
				$(data).find('groupName').each(function () {
					htmlContent += "<option value='" + $(this).attr('value') + "'" + ($(this).attr('selected') != null ? ' selected="selected"' : '') + ">" + $(this).text() + "</option>";
				});
				htmlContent += "</select>";
				$('#leadership_groupName').html(htmlContent);
			}
		});
		// Enable the Go button.
		$('#leadership_opps_go').css('display', 'none');

		// Hide the error message.
		$('#leadership_opps_error').css('display', 'none');
	}
}

// Gets the records for display.
function importDatabaseRecords(GroupTypes, GroupNames) {
	if ($('#GroupName').val() != 'All') {
		$('#leadership_opps_content').html("<div align='center'><h2>Searching....</h2><br /><img src='/am/graphics/bigrotation.gif' /></div>");
		$('#leadership_link_content').css('display', 'none');
		$('#leadership_content').css('display', 'block');
		// Show a loading graphic while we pull in the data.
		$('#leadership_opps_description').html("<img src='/am/graphics/bigrotation.gif' />");
		$('#leadership_opps_position').html("<img src='/am/graphics/bigrotation.gif' />");
		$('#leadership_opps_subcmt').html('');
		$('#leadership_opps_details').html("<img src='/am/graphics/bigrotation.gif' />");
		$('#leadership_opps_contact').html("<img src='/am/graphics/bigrotation.gif' />");

		$.ajax({
			url: "/am/customsource/sbw/leadership_opps_process.cfm?GroupTypes=" + GroupTypes + "&GroupNames=" + GroupNames + "&theRequest=getDatabaseRecords",
			success: function (data) {
				var htmlContentDescription = "";
				var htmlContentPositions = "";
				var htmlContentDetails = "";
				var htmlContentContact = "";
				var htmlContentSubcmt = "";
				var totalResults = Number($(data).find('records').attr('recordCount'));
				if (totalResults > 0) {
					$(data).find('record').each(function () {
						var groupResult = $(this);
						// Display the group's title and description.
						htmlContentDescription += "<h2>" + groupResult.find('groupTitle').text() + "</h2>";
						htmlContentDescription += "<p>" + groupResult.find('groupDescription').text() + "</p><br />";
						$('#leadership_opps_description').html(htmlContentDescription);

						// Populate the position(s) data.
						if (groupResult.find('positions').attr('recordCount') > 0) {
							htmlContentPositions += "<h3>Leadership positions</h3>";
							groupResult.find('position').each(function () {
								var positionItem = $(this);
								htmlContentPositions += "<h4>" + positionItem.find('positionTitle').text() + "</h4>";
								htmlContentPositions += "<div style='margin-left:1em;'>";
								if (positionItem.find('positionDescription').text() != "") {
									htmlContentPositions += "<p>" + positionItem.find('positionDescription').text() + "</p>";
								}
								if (positionItem.find('positionTime').text() != '') {
									htmlContentPositions += "<p>Time estimate: " + positionItem.find('positionTime').text() + "</p>";
								}
								htmlContentPositions += "</div>";
							});
							$('#leadership_opps_position').html(htmlContentPositions);
						}

						// Populate the details data.
						htmlContentDetails += "<table summary=''><tbody>";
						if (groupResult.find('groupMeetFrequency').text() != '') {
							htmlContentDetails += "<tr><td><span>Meetings Per Year:</span></td><td>" + groupResult.find('groupMeetFrequency').text() + "</td></tr>";
						}
						if (groupResult.find('groupTeleconference').text() != '') {
							htmlContentDetails += "<tr><td><span>Meet by Phone:</span></td><td>" + groupResult.find('groupTeleconference').text() + "</td></tr>";
						}
						if (groupResult.find('groupMeetLocation').text() != '') {
							htmlContentDetails += "<tr><td><span>Meeting Location:</span></td><td>" + groupResult.find('groupMeetLocation').text() + "</td></tr>";
						}
						if (groupResult.find('groupSize').text() != '') {
							htmlContentDetails += "<tr><td><span>Leadership Group Size:</span></td><td>" + groupResult.find('groupSize').text() + "</td></tr>";
						}
						if (groupResult.find('groupProcess').text() != '') {
							htmlContentDetails += "<tr><td><span>Appointment/Election Process:</span></td><td>" + groupResult.find('groupProcess').text() + "</td></tr>";
						}
						if (groupResult.find('groupPrereq').text() != '') {
							htmlContentDetails += "<tr><td><span>Prerequisites to Joining:</span></td><td>" + groupResult.find('groupPrereq').text() + "</td></tr>";
						}
						if (groupResult.find('groupTerms').text() != '') {
							htmlContentDetails += "<tr><td><span>Term Limits:</span></td><td>" + groupResult.find('groupTerms').text() + "</td></tr>";
						}
						if (groupResult.find('groupWebURL').text() != '') {
							htmlContentDetails += "<tr><td><span>Web Page:</span></td><td><a href='" + groupResult.find('groupWebUrl').text() + "'>" + groupResult.find('groupWebTitle').text() + "</a></td></tr>";
						}
						htmlContentDetails += "</tbody></table>";
						$('#leadership_opps_details').html(htmlContentDetails);

						// Populate the contact data.
						htmlContentContact += "<div>";
						groupResult.find('groupContact').each(function () {
							// Loop through each contact.
							var currentContact = $(this);
							htmlContentContact += "<p>";
							htmlContentContact += "<strong>" + currentContact.find('groupContactTitle').text() + "</strong><br />";
							htmlContentContact += currentContact.find('groupContactName').text() + "<br />";
							if (currentContact.find('groupContactCompany').text() != '') {
								htmlContentContact += currentContact.find('groupContactCompany').text() + "<br />";
							}
							htmlContentContact += currentContact.find('groupContactAddress1').text() + "<br />";
							if (currentContact.find('groupContactAddress2').text() != '') {
								htmlContentContact += currentContact.find('groupContactAddress2').text() + "<br />";
							}
							htmlContentContact += currentContact.find('groupContactCity').text() + ", " + currentContact.find('groupContactState').text() + " " + currentContact.find('groupContactZip').text() + "<br />";
							if (currentContact.find('groupContactPhone').text() != '') {
								htmlContentContact += "Phone: " + currentContact.find('groupContactPhone').text() + "<br />";
							}
							if (currentContact.find('groupContactFax').text() != '') {
								htmlContentContact += "Fax: " + currentContact.find('groupContactFax').text() + "<br />";
							}
							if (currentContact.find('groupContactEmail').text() != '') {
								htmlContentContact += currentContact.find('groupContactEmail').text() + "<br />";
							}
							htmlContentContact += "</p>";
						});
						htmlContentContact += "</div>";
						$('#leadership_opps_contact').html(htmlContentContact);

						// Populate the listing of subcommittees, if there are any.
						if (groupResult.find('subcommittees').attr('recordCount') > 0) {
							htmlContentSubcmt += "<h3>Subcommittees</h3>";
							groupResult.find('subcommittee').each(function () {
								var subcommitteeResult = $(this);
								if (subcommitteeResult.find('subcommitteeTitle').text() != '') {
									htmlContentSubcmt += "<h4>" + subcommitteeResult.find('subcommitteeTitle').text() + "</h4>";
									// Since they're not populating any of the additional information for these, don't bother trying to output them.
								}
							});
							$('#leadership_block_static').css('display', 'block');
						}
						$('#leadership_opps_subcmt').html(htmlContentSubcmt);

					});
					$('#leadership_opps_content').html('');
				} else {
					$('#leadership_opps_content').html("<p class='error'>No results found for your search. Please try again.</p>");
				}
			}
		});
	} else {
		$('#leadership_opps_content').html("<div align='center'><p class='error'>Please choose a group.</p></div>");
	}
}
