// blsac.js
// javascript specific to the blsac project. references objects
// and methods in the mediumUtils namespace (found in mediumUtils.js)
// (c) 2006 David D'Amico

// Rollover method specifically for our nav, automates the process
// a bit. We have a pre-determined suffix appended to the 'rollover'
// versions of the menu graphics, which is inserted into or
// removed from the target image's src property. The target image
// object + the altered src are then passed to a generic image
// swapping method
function navRollover (targetImage) {
	
	
	var navRolloverSuffix = "_OVER";
	var fileExtension = ".gif";
	var newImageSrc = "";
	
	if (targetImage.src.indexOf(navRolloverSuffix) == -1) {
		newImageSrc = targetImage.src.replace(fileExtension, (navRolloverSuffix + fileExtension));
	}
	
	else if (targetImage.src.indexOf(navRolloverSuffix) != -1) {
		newImageSrc = targetImage.src.replace(navRolloverSuffix, "");
	}
	
	mediumUtils.swapImage(targetImage, newImageSrc);
}


// Validator for this site's contact form specifically, just checks
// for a valid email address and makes sure the message isn't too long
function validateContactForm () {
	
	// set the name of the target form
	var contactFormName = "contact_submit";
	
	//fetch the target form
	var targetForm = document.getElementById(contactFormName);
	
	//validate email address
	if (!(mediumUtils.validateEmailString(targetForm.email.value))) {
		alert("Please enter a valid email address.");
		return false;
	}
	
	//ensure message is no longer than 1000 characters
	else if (targetForm.message.value.length > 1000) {
		alert("Message length is limited to 1000 characters.\nPlease adjust your message accordingly.");
	}
	
	else {
		targetForm.submit();
	}
	
}


// Validator for this site's mailing list form specifically, just checks
// for a valid email address
function validateMailingList () {
	
	// set the name of the target form
	var mailingListFormName = "mailingListForm";
	
	//fetch the target form
	var targetForm = document.getElementById(mailingListFormName);
	
	//make sure required fields are not blank
	if (targetForm.name.value == ""
		|| targetForm.level.value == ""
		|| targetForm.region.value == "") {
		alert("Please complete all the required fields.");
		return false;
	}
	
	//validate email address
	else if (!(mediumUtils.validateEmailString(targetForm.email.value))) {
		alert("Please enter a valid email address.");
		return false;
	}
	
	else {
		targetForm.submit();
	}
}




// Here we have a script used only on the two executive
// pages, which works with the scriptaculous library to turn the
// executive blocks on and off

// of course, we must maintain state on which exec block
// is currently on, and we do that in this var
var execDivThatIsOn = "";

// this function is just a proxy between the onclick
// events and Effect.toggle --before it turns on
// the requested block, it turns off whichever one
// is currently on
function toggleExecutive (execDivToTurnOn) {
	
	// turn off the executive block that's on now
	//alert(execDivThatIsOn + " | " + execDivToTurnOn);
	if (execDivThatIsOn != execDivToTurnOn) {
		
		if (execDivThatIsOn != "") {
			Effect.toggle(execDivThatIsOn, 'appear', {duration: 0});
		}
	
		// turn on the next one
		Effect.toggle(execDivToTurnOn, 'appear', {duration: 0});
	
		// remember which one is on now
		execDivThatIsOn = execDivToTurnOn;
	}
}



// A proxy for the mediumUtils form checking function,
// passes the input along, and either passes along an
// error message, or submits the form.
function blsacCheckForm (targetFormName, requiredFieldNames, validatedEmailFieldNames, lengthRestrictedFieldNames) {
	var formCheckReturnValue = mediumUtils.checkForm(targetFormName, requiredFieldNames, validatedEmailFieldNames, lengthRestrictedFieldNames);
	
	if (formCheckReturnValue == true) {
		var targetForm = document.getElementById(targetFormName);
		targetForm.submit();
		//alert("in real life, I would have just submitted the form");
	}
	
	else { alert(formCheckReturnValue); }
}