/*
	EMERSON COLLEGE
		Applicant File Javascript
		
*/

function appfile_getInformation(search_id_lastname) {
	// This function pulls the information from the form and spits out the raw XML if sucessful.
	
	// Variables (Even though we're sending as POST, you have to declair vars as if it was a GET string.
		var opt_getInformation = "&id_lastname="+search_id_lastname;
	// Request data from the Ajax Pull
		var getInformationAjax = new Ajax.Updater(
				'applicant_file_xhtml',
				ajaxurl_root,
				{
					method: 'post',
					evalScripts: true,
					parameters: "&id=file_getStudent"+opt_getInformation
				}
				);
		
}

function appfile_getDocument(docs_form_student_id, docs_form_major, docs_form_type) {
	
	$('docs_form_student_id').value = docs_form_student_id;
	$('docs_form_major').value = docs_form_major;
	$('docs_form_type').value = docs_form_type;

	$('docs_form').submit();
		
}

function changeAppt(major) {
	// This function calls event location/times applicable to ID
		
		var opt_getInformationEvents = "&id_lastname="+$F('student_id');
		var opt_getInformationMajor = "&majorNumber="+major;
		var getInformationEvents = new Ajax.Updater(
										"info_change_appt", 
											ajaxurl_root, { 
											method: 'post', 
											parameters: "&id=file_getStudentAppt"+opt_getInformationEvents+opt_getInformationMajor,
											on500: function(x) {
												$("debug").innerHTML = x.responseText;
											},
											onSuccess: changeAppt_complete(major),
											onFailure: ("Change Appt call failed.")
										}
									);
}

function changeAppt_complete(major) {
	
	$('changeAppt'+major).innerHTML = changeApt_getButtons(2,major);
	Effect.BlindDown('info_change_appt');
	if (Element.visible('info_appt_msg') === true) {
		Effect.BlindUp('info_appt_msg');
	}
	
}

function changeAppt_cancel(major) {
	
	$('changeAppt'+major).innerHTML = changeApt_getButtons(1,major);
	//Effect.BlindUp('saveAppt');
	Effect.BlindUp('info_change_appt',{afterFinish: function(x) { $('info_change_appt').innerHTML = "Loading..."; } } );
	
}

function changeApt_getButtons(type,major) {
	
	/*
		Returns innerHTML content for various buttons
			type
				0 = New Appointment
				1 = Change an Appointment
				2 = Change in process
			major
				Number of slot effected.
	*/
	
	if (major == undefined) {
		alert('changeApt_getButtons('+type+',null)\nNo major!');
		major = 0;
	}
	
	var returnContent;
	
	switch (type) {
		
		case 0:
			// Make a NEW appointment
			returnContent = "<input type='button' name='changeAppointmentButton_"+major+"' id='changeAppointmentButton_"+major+"' value='Make an Appointment' onclick='javascript:changeAppt("+major+");' \/>";
			break;
			
		case 1:
			// Change an appointment
			returnContent = "<input type='button' name='changeAppointmentButton_"+major+"' id='changeAppointmentButton_"+major+"' value='Change Appointment' onclick='javascript:changeAppt("+major+");' \/>";
			break;
			
		case 2:
			// Appointment Change in Process
			returnContent = "Changing Appointment...";
			break;
			
		default:
			returnContent = 'Error: changeApt_getButtons(null,'+major+') - No type';
			break;
			
	}
	
	return returnContent;
	
}

function saveAppt(major,admin,cancel) {
	
	// Condition the admin variable
		if (admin == undefined) {
			var adminAddVar = 0;
		} else {
			var adminAddVar = admin;
		}
	// Condition of replace or remove booking
		if (cancel != undefined && cancel == 1) {
			var actionVar = "cancel";
		} else {
			var actionVar = "add";
		}
	// Condition variables
		if ($F('booked_prop1') != "") {
			// We're sending prop1
				slotType = 1;
				booked_prop1 = $F('booked_prop1');
		} else {
			// Not sending a prop1
				slotType = 0;
				booked_prop1 = "";
		}
	// Check if slotID is proper
		if ( $F('paappEventsLocation_main') > 0 || actionVar == "cancel") {
		// Build Ajax Vars string
			opt_saveAppt_keyID = "&id_lastname="+$F('student_id')
				+"&slotID="+$F('paappEventsLocation_main')
				+"&slotType="+slotType
				+"&booked_prop1="+booked_prop1
				+"&major="+$F('major'+major)
				+"&action="+actionVar
				+"&adminadd="+adminAddVar;
				
			if (Application['staging'] === true) {
				//alert(opt_saveAppt_keyID);
			}
			
			// Make the post
				var saveAppt_submit_ajax = new Ajax.Request(
						ajaxurl_root,
						{
							method: 'post',
							parameters: "&id=events_changeAppt"+opt_saveAppt_keyID,
							onSuccess: saveAppt_success,
							onFailure: function(x) { alert("Save Appt Failed"); $('debug').innerHTML = x.responseText; }
						}
						);
				
			if (Application['staging'] === true) {
				//alert(saveAppt_submit_ajax);
			}
				
			Effect.Appear('info_appt_msg');
			//Effect.Fade('saveAppt');
			Effect.Fade('info_change_appt');
		} else {
			alert("Please select an audition slot before continuing.");
		}
}

function saveAppt_success(x) {
	
	// Phaze XML to usable data
		var phazed_xml = x.responseXML.getElementsByTagName('paapp');
		
	// Let's make all the vars managaable:
		if (phazed_xml[0].getElementsByTagName('status')[0].firstChild != null) {
			var pickEventConfirm_alert = phazed_xml[0].getElementsByTagName('status')[0].firstChild.data;
		}
	
	// Process the results
		if (pickEventConfirm_alert == 0) {
			// Slot is saved.
				$('info_appt_msg').innerHTML = "Appointment slot successfully changed.";
		} else if (pickEventConfirm_alert == 1) {
			// Required information not sent
				$('info_appt_msg').innerHTML = "All of the required information wasn't sent. <a href='/?id=booking_list'>Please reload this page and try again.</a>";
		} else if (pickEventConfirm_alert == 2) {
			// All slots are filled.
				$('info_appt_msg').innerHTML = "The slot booked has reached capacity.";
		} else if (pickEventConfirm_alert == 3) {
			// All slots are filled.
				$('info_appt_msg').innerHTML = "Your login session has expired. <a href='/?id=booking_list'>Please login and try again.</a>";
		} else if (pickEventConfirm_alert == 4) {
			// Required information not sent
				$('info_appt_msg').innerHTML = "All of the required information wasn't sent. <a href='/?id=booking_list'>Please reload this page and try again.</a>";
		} else if (pickEventConfirm_alert == 5) {
			// Attempting an Admin Add without permission
				$('info_appt_msg').innerHTML = "You are attempting to add to the administrative capacity without proper permission. <span style='color:#ff0000;'>The slot was <strong>not saved<\/strong>.<\/span>";
		} else {
			// Catch-all fail
				$('info_appt_msg').innerHTML = "<span style='color:#ff0000;'>Error #"+pickEventConfirm_alert+"<\/span>";
		}			
		
		appfile_getInformation($F('student_id'));
	
}

function getNotes() {
	// This function calls notes applicable to ID

	if(Element.visible('info_notes') == true) {
					Effect.toggle('info_notes','blind');
			} else {
		
		var opt_getInformationNotes = "&id_lastname="+$F('student_id');
		var getInformationNotes = new Ajax.Updater(
										"info_notes", 
											ajaxurl_root, { 
											method: 'post', 
											parameters: "&id=file_getNotes"+opt_getInformationNotes,
											on500: function(x) {
											$("debug").innerHTML = x.responseText;
											},
											onComplete: function(x) { Effect.toggle('info_notes','blind'); },
											onFailure: function(x) { alert("Notes get call failed."); }
										}
									);
			}
}

function getNotes_refresh() {
	// This function refreshes notes applicable to ID
		
		var opt_getInformationNotes = "&id_lastname="+$F('student_id');
		var getInformationNotes = new Ajax.Updater(
										"info_notes", 
											ajaxurl_root, { 
											method: 'post', 
											parameters: "&id=file_getNotes"+opt_getInformationNotes,
											on500: function(x) {
											$("debug").innerHTML = x.responseText;
											},
											onFailure: function(x) { alert("Notes get call failed."); }
										}
									);
		
		saveNotes_success();		
		
}

function saveNotes(list_refresh) {
	
	// Starting Vars
		if (list_refresh == undefined) {
			list_refresh = 0;
		}
		
	// Prepare body text
		fixed_body = $F('systemNotes_body').replace(/</,'&lt;');
		fixed_body = fixed_body.replace(/>/,'&gt;');
	
	// Save notes
		opt_saveNotes_keyID = "&id_lastname="+$F('student_id')
			+"&type=1"
			+"&level="+$F('systemNotes_level')
			+"&subject="+$F('systemNotes_subject')
			+"&body="+encodeURIComponent(fixed_body);

	// Make the post
		var saveNotes_submit_ajax = new Ajax.Request(
				ajaxurl_root,
				{
					method: 'post',
					parameters: "&id=file_saveNotes"+opt_saveNotes_keyID,
					onSuccess: function(x) {
						if (list_refresh == 0) {
							getNotes_refresh();
						} else {
							saveNotes_success_checkout();
						}
					},
					onFailure: function(x) { alert("Save Notes Failed");$('debug').innerHTML = x.responseText; }
				}
				);
}

function saveNotes_success() {
		$('info_notes_msg').innerHTML = "Notes Added. See above for updated listing.";
		new Effect.Appear('info_notes_msg');
}

function saveNotes_success_checkout() {
	
	// Hide note taking display.
		new Effect.BlindUp('checkout_notesSave');
	// Display sucessful note save
		$('info_notes_msg').innerHTML = "<h3>Note successfully saved!<\/h3>";
		new Effect.BlindDown('info_notes_msg');
		
}

function savePackage(major,comments,status,type,marker) {
	
	// Treatment of vars
		opts = new Array();
		
		if (major != "") {
			opts['major'] = major;
		}
		
		if ($F('student_id') != "") {
			opts['student_id'] = $F('student_id');
		}
		
		if (type == "") {
			opts['type'] = 1;
		} else {
			opts['type'] = type;
		}
		
		if (status != "") {
			opts['status'] = status;
		}
		
		opts['comments'] = encodeURIComponent(comments);
		opts['zone'] = marker;
		
		
	// Send AJAX
		var savePackage_ajax = new Ajax.Request(
			ajaxurl_root,
			{
				method: 'post',
				parameters: "&id=file_saveReceipt"
					+ "&major=" + opts['major']
					+ "&comments=" + opts['comments']
					+ "&type=" + opts['type']
					+ "&student_id=" + opts['student_id']
					+ "&status=" + opts['status']
					+ "&zone=" + opts['zone']
					,
				onSuccess: function(x) {
					savePackage_success(x)
				},
				onFailure: function(x) {
					alert("savePackage_ajax() failed");
					$('debug').innerHTML = x.responseText;
				}
			}
			);
	
}

	function savePackage_success(x) {
		var phazed_xml = x.responseXML.getElementsByTagName('paapp');
		var code = phazed_xml[0].getElementsByTagName('code')[0].firstChild.data;
		var type = phazed_xml[0].getElementsByTagName('type')[0].firstChild.data;
		var zone = phazed_xml[0].getElementsByTagName('zone')[0].firstChild.data;
		
		// Variable Prep
		if (code == 1 || code == 0) {
			displayColor = 'rgb(0,153,0)';
		} else {
			displayColor = 'rgb(255,0,0)';
		}
		
		// Display changes
		$('package_link_'+zone).innerHTML = type;
		$('package_link_'+zone).style.color = displayColor;
		$('package_link_'+zone).style.fontWeight = 'bold';
		new Effect.BlindUp('package_actions_'+zone,{duration:0.5});
	
	}
	
function getXmlValue(VarField,VarXML) {
	//alert(VarField+", "+VarXML);
	if (VarXML.getElementsByTagName(VarField)[0].firstChild.data == undefined) { 
		return "???";
	} else {
		return VarXML.getElementsByTagName(VarField)[0].firstChild.data;
	}
}
