// selectedDonationURL is a state variable containing the url to point to when opening the popup window
// Pre-select a 4Teachers tool from the dropdown menu (see generateDonationOptions) based on a GET parameter (if specified)
selectedTool = getUrlParameterValue('giveto');
if (selectedTool == '') {
	// No tool specified; default to all 4Teachers tools
	selectedDonationURL = 'http://www.kuendowment.org/depts/crl/4tchrs';
}
else {
	// Get the url for the selected 4Teachers tool
	$.get('./supportUrlHandler.php', { urlFetchMode: 'single' , selected: selectedTool },
	function(data) {
		if (data == '') {
			// Just in case supportUrlHandler doesn't return any data, use default value
			selectedDonationURL = 'http://www.kuendowment.org/depts/crl/4tchrs';
		}
		else {
			selectedDonationURL = data;
		}
	});
}

/*
* Manipulate the DOM and assign event handlers after the document has loaded
*/
$(document).ready(function() {
	generateDonationOptions();
	
	$('#donation-options').change(function() {
		// the url is stored in the Option.value
		selectedDonationURL = this.options[this.selectedIndex].value;
	});
	$('#donate-now').click(function() {
		openGiveNowWindow(selectedDonationURL);
	});
});

/*
* Generate dynamic markup for donation options
* This is our version of a noscript handler (since this will only show if scripting is enabled)
*/
function generateDonationOptions() {
	// Generate the donation form contents after it has been created
	$('#donation-links-container').live('donation-form-created', function() {
		// Make an AJAX call, passing the decoded GET param (decodeURIComponent does not decode + as a space)
		$.get('./supportUrlHandler.php', { urlFetchMode: 'all', selected: decodeURIComponent(selectedTool).replace(/\+/g, ' ') },
		function(data) {
			$('#donation-options').html(data);
			$('select#donation-options').css('width', 'auto');
			$('input#donate-now').css('position', 'absolute');  // Set this style property on the button after populating the SELECT for correct positioning
		});
	});
	
	// Create the donation form to replace the existing content
	var output = '\n<form action="" method="post" onsubmit="return false;">\n<div>';
	output += '<select name="donation-options" id="donation-options">\n';
	output += '</select>&nbsp;';
	output += '<input id="donate-now" type="image" value="Donate" src="/images/donate-button.jpg" alt="Give Now">\n</div>\n</form>';
	$('#donation-links-container').html(output);
	
	// Trigger the donation form contents creation event
	$('#donation-links-container').trigger('donation-form-created');
}

/*
* Extract the value of a GET parameter specified by paramName
* Uses regular expressions to match the first occurrence of 
* Regular expressions borrowed from http://www.netlobo.com/url_query_string_javascript.html
*/
function getUrlParameterValue(paramName) {
	// GET params can be arrays, so escape the [ and ] with backslashes
	paramName = paramName.replace(/[\[]/, "\\\[").replace(/[\]]/,"\\\]");
	
	// Create the regex for finding the named GET parameter within the url
	var searchRegex = "[\\?&]" + paramName + "=([^&#]*)";
	var regex = new RegExp(searchRegex);
	
	var results = regex.exec(window.location.href);
	if (results == null) {
		// If no match was found, return an empty string
		return "";
	}
	// Else, the GET parameter value should be the backreference value
	// (The 1st return value is the entire regex match; 2nd is the backreference)
	return results[1];
}

/*
* Open a new window to the GiveNow page specified by URL
*/
function openGiveNowWindow(URL) {
	window.open(URL, '4TeachersSupport', 'width=915,height=600,status=1,toolbar=0,menubar=1,location=0,resizable=1,scrollbars=1');
}

