
/*#################################################################################
 * Reset all CAPTCHA instances on the page. No parameters are necessary as each
 * page should have only one CAPTCHA.
 #################################################################################*/
function reset_captcha() {
	document.getElementById('captcha').innerHTML = "<img src=includes/function_captcha.php>";
}

/*#################################################################################
 * Activates loader box and ensures loader animation doesn't freeze in Internet
 * Explorer.
 #################################################################################*/
function form_submit() {
	var load = document.getElementById('loadbox');
	document.form.submit();
	document.getElementById('loader').innerHTML = '<div id="loadbox"><span class="general">Please Wait...</span><br /><img src="images/pbar5.gif"></div>';
}

/*#################################################################################
 * Runs an AJAX script in ajax_click.php to record clicks on external links
 * 
 * Parameters:
 *		id => Link ID as used in maj_links table
 #################################################################################*/
function linkout(id) {
	var req = null;
	
	// Browser compatible AJAX connection
	if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		if (req.overrideMimeType) {
			req.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	};
	
	// Send link ID to ajax_click.php to record the action
	var queryString = "?id=" + id;
	req.open("GET", "includes/ajax_click.php" + queryString, true);
	req.send(null);
	return true;
}


/*#################################################################################
 * Runs an AJAX script in ajax_spam.php to report spam in the user comment boxes.
 * 
 * Parameters:
 *		media => Media type as used in maj_comments table
 *		id => The comment id
 *		url => A link to the page containing spam
 #################################################################################*/
function comspam(media, id, url) {
	var req = null;
	
	// Browser compatible AJAX connection
	if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		if (req.overrideMimeType) {
			req.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	};
	
	// Run script to report spam
	var queryString = "?id=" + id + "&type=" + media + "&url=" + url;
	req.open("GET", "includes/ajax_spam.php" + queryString, true);
	req.send(null);
	
	// Change the link so that users can't report again
	var item = media + '_' + id;
	document.getElementById(item).innerHTML = "(<span class=\"general\" style=\"font-size: 11px; color: red;\">Reported</span>)";
}

/*#################################################################################
 * Runs a simple Javascript that adds options to a select menu
 * 
 * Parameters:
 *		field => What field are we adding to?
 *		txt => The text to place between the OPTION tags
 *		val => The value of the option
 #################################################################################*/
function addOption(field, txt, val)
{
	var oSelect = field;
	nextOptIndex = oSelect.length + 1;
	newOpt = document.createElement( 'OPTION' );
	newOpt.text = txt;
	newOpt.value = val;
	oSelect.add( newOpt, nextOptIndex );
}

/*#################################################################################
 * This function is used with the get_dateform PHP function. It updates the drop
 * menu, giving each month its appropriate number of days
 #################################################################################*/
function setDays()
{
	var mfield = document.getElementById('monthSel');
	var dfield = document.getElementById('daysSel');
	var yfield = document.getElementById('yearSel');
	var themonth = parseInt(mfield.options[mfield.selectedIndex].value);
	var theyear = parseInt(yfield.options[yfield.selectedIndex].value);
	
	if (themonth == 1 || themonth == 3 || themonth == 5 || themonth == 7 || themonth == 8 || themonth == 10 || themonth == 12)
	{
		// Months with 31 days
		var days = 31 + 1;
	}
	else
	{
		// Months with 30 days
		var days = 30 + 1;
	}
	if (themonth == 2)
	{
		// Feb
		var days = 28 + 1;
		if (theyear != '--' && theyear % 4 == 0)
		{
			// Leap year!
			days = 29 + 1;
		}
	}
	
	// Clear days field
	dfield.innerText = '';
	addOption(dfield, '--', '');
	
	// Add options
	for(var i = 1; i < days; i++)
	{
		addOption(dfield, i, i);
	}
}

/*###################################################################################
 * Red River Timber Custom Function!
 * Runs a simple AJAX script that helps registering members pick their hunting club
 * 
 * Parameters:
 *		clubname => The name of the club to which the user belongs
 ###################################################################################*/
function huntclubs(clubname)
{
	var ajaxRequest;  // The variable that makes Ajax possible!
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				// Something went wrong! This user has an old browser that doesn't support Ajax
				alert("Your browser does not support Ajax! Some functions on this page may not work properly!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{
		// Data received! Let's process it
		if(ajaxRequest.readyState == 4)
		{
			// Get the data from the server's response and format it for use
			document.getElementById('clublist').innerHTML = ajaxRequest.responseText;
		}
	}
	
	// Set the URL to the PHP file we're going to run to get data
	var queryString = "?c=" + clubname;
	
	// Pass the data to the PHP file
	ajaxRequest.open("GET", "includes/ajax_huntclubs.php" + queryString, true);
	
	// Execute Ajax query
	ajaxRequest.send(null);
}

/*###################################################################################
 * Red River Timber Custom Function!
 * Displays a list of clubs gathered from the huntclubs function
 * 
 * Parameters:
 *		clubname => The name of the club to which the user belongs
 ###################################################################################*/
function show_clublist()
{
	var value = document.register.huntclub.value;
	
	// Show list only if the field value is 3 characters or more
	if (value.length > 3)
	{
		// Get clublist
		huntclubs(value);
		
		// Move clublist over field and make it appear
		document.getElementById('clublist').style.display = 'block';
	}
	else
	{
		document.getElementById('clublist').style.display = 'none';
	}
}

// Set the field value to the clicked link
function setclub(text)
{
	document.register.huntclub.value = text;
	document.getElementById('clublist').style.display = 'none';
}