// events.js

   var eventSignupRequest;

		function getRandomReason() {
			var crazyReasons = new Array("My teenage son joined the circus",
							   	   		 "Broken instruments don't need repairs",
										 "Band students don't need music",
										 "Students can walk to competitions",
										 "Uniforms? How about t-shirts & jeans?",
										 "We don't need band, leadership, or guard camps",
										 "Snacks are for Wimpy from Popeye",
										 "I'm donating $500,000 to the band",
										 "I'm donating $500,000 to the band",
										 "I'm donating 1 million dollars to the band",
										 "I'm donating 2 million dollars to the band",
										 "I'll personally fund the band indefinitely",
										 "I want Winter guard to practice in my back yard",
										 "I'm personally cleaning up the field",
										 "I'll drive everyone to competitions in my car",
										 "I'll purchase sheet music for the band",
										 "I'm donating an annual band scholorship",
										 "Last year's props should last 5 years",
										 "I'm moving to Iceland",
										 "I have swine flu... and chicken pox",
										 "I like to pinch people",
										 "Smelling beer gives me gas",
										 "I have hairy feet",
										 "I'm having my back waxed",
										 "I've got to trim my nose hairs... it's an all day event... trust me",
										 "I ran out of deodorant",
										 "I've fallen and I can't get up",
										 "I have to wash my hair",
										 "I have Agoraphobia",
										 "I have Pedaspheraphobia... it's genetic",
										 "I'm being abducted by aliens",
										 "I enjoy running from angry mobs of parents",
										 "I just don't like you people",
										 "My legs were amputated this week",
										 "There's a lion in the street",
										 "I'm planning to be deathly ill",
										 "I'm in a full body cast");
			var index = Math.floor(Math.random() * (crazyReasons.length));
			var crazyReason = crazyReasons[index];
			return crazyReason;
		}
		
	function saveEventSignupChanges(action, eventId, userId, eventRoleId, studentName, rewardPoints, ditchReason) {
    	// Perform a synchronous request to add a user event signup.
		var retValue = 1;
    	var url = "support_fundraising_events_signup_db.php";
    	eventSignupRequest.open("POST", url, false);
    	eventSignupRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		eventSignupRequest.send("action=" + action 
								+ "&eventId=" + eventId 
								+ "&userId=" + userId 
								+ "&eventRoleId=" + eventRoleId
								+ "&studentName=" + encodeURIComponent(studentName)
								+ "&rewardPoints=" + rewardPoints
								+ "&ditchReason=" + ditchReason);

		try {
			var msg = eventSignupRequest.responseText;
    		if (msg != null && msg.length > 0) {
			    if (msg.search(/NOT successful/i) == -1) { 
				    // Success!
					retValue = 0;
    			} else {
    	  	        alert(msg);
    			}
    		} else {
    	  	    alert("Error saving event signup information.  Please contact the webmaster.");
    		}
		} catch (ex) {
		}
		
		return retValue;
	}

function signup(button, eventId, userId, eventRoleId, studentsString, rewardPoints, ajaxFunction) {
	button.disabled = true;

	eventSignupRequest = getXMLHttpRequest();
	
	var status = -1;
	var students = "";
	if (studentsString != null && studentsString != "") {
	    students = studentsString.split(";");
	}
	
	if (students.length > 1) {
	    // open dialog box
		status = openDialog("support_fundraising_events_signup_dialog.php?action=create&eventId=" + eventId
				   	 			+ "&eventRoleId=" + eventRoleId
								+ "&rewardPoints=" + rewardPoints, "270px", "370px");
	} else {
	    if (students != "" && students[0] != "") {
		    studentName = students[0];
		} else {
		    studentName = "";
		}
	    status = saveEventSignupChanges("create", eventId, userId, eventRoleId, studentName, rewardPoints, null);
	}
	
	if (status == 0) {
		eval(ajaxFunction);
	    alert("You are now signed up!");
	} else {
	    button.disabled = false;
	}
}

function updateSignup(button, eventId, userId, studentsString, ajaxFunction) {
	button.disabled = true;

	var students = "";
	if (studentsString != null && studentsString != "") {
	    students = studentsString.split(";");
	}
	
	if (students != "" && students.length >= 1) {
	    // open dialog box
		var status = openDialog("support_fundraising_events_signup_dialog.php?action=update&eventId=" + eventId, "330px", "390px");
		if (status == 0) {
			eval(ajaxFunction);
		    alert("Your changes have been saved.");
		} else {
	        button.disabled = false;
		}
	} else {
	    alert("An error has occurred.  Please contact the webmaster.");
	}
}

function rescindSignup(button, eventId, userId, ajaxFunction) {
	button.disabled = true;

	eventSignupRequest = getXMLHttpRequest();

	var status = openDialog("support_fundraising_events_signup_dialog.php?action=delete" 
		   	 				+ "&eventId=" + eventId
							+ "&userId=" + userId
		   	 				, "330px", "390px");
	if (status == 0) {
	    eval(ajaxFunction);
	    alert("You are no longer signed up.");
	} else {
	    button.disabled = false;
	}
}
