// JavaScript Document

//-----------------------------------------------------------------

function check_topic() {

	var errmsg = '';
	
	if (document.getElementById('topic').value == '') {
		errmsg = errmsg + '- Please enter a topic\n';
	}
	if (document.getElementById('message').value == '') {
		errmsg = errmsg + '- Please enter a message\n';
	}
	
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	if (errmsg == '') { 
		document.forum.submit();
	}

}

function check_message() {

	var errmsg = '';
	
	if (document.getElementById('message').value == '') {
		errmsg = errmsg + '- Please enter a message\n';
	}
	
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	if (errmsg == '') { 
		document.forum_message.submit();
	}

}

//-----------------------------------------------------------------

function delete_thread(id,topic) {
	var conf = confirm("Are you sure you want to delete this thread?\n\n-- " + topic + " --");

	if(conf) {
		window.open("/forum/delete_thread.asp?id=" + id,"_self")
	}
}

function delete_message_first(id,message) {
	var conf = confirm("Are you sure you want to delete this message?\n\n-- " + message + " --\n\nSince this is the first message in this thread, the entire thread will be deleted.");

	if(conf) {
		window.open("/forum/delete_message.asp?id=" + id,"_self")
	}
}

function delete_message(id,message) {
	var conf = confirm("Are you sure you want to delete this message?\n\n-- " + message + " --");

	if(conf) {
		window.open("/forum/delete_message.asp?id=" + id,"_self")
	}
}

function lock_thread(id,topic) {
	var conf = confirm("Are you sure you want to lock this thread?\n\n-- " + topic + " --");

	if(conf) {
		window.open("/forum/lock_thread.asp?id=" + id,"_self")
	}
}

function unlock_thread(id,topic) {
	var conf = confirm("Are you sure you want to unlock this thread?\n\n-- " + topic + " --");

	if(conf) {
		window.open("/forum/unlock_thread.asp?id=" + id,"_self")
	}
}

function sticky_thread(id,topic) {
	var conf = confirm("Are you sure you want to sticky this thread?\n\n-- " + topic + " --");

	if(conf) {
		window.open("/forum/sticky_thread.asp?id=" + id,"_self")
	}
}

function unsticky_thread(id,topic) {
	var conf = confirm("Are you sure you want to unsticky this thread?\n\n-- " + topic + " --");

	if(conf) {
		window.open("/forum/unsticky_thread.asp?id=" + id,"_self")
	}
}

//-----------------------------------------------------------------

function textCounter(field,countfield,maxlimit) {
	countfield.value = maxlimit - field.value.length - 1;
	
	if (field.value.length >= maxlimit) {
		field.value = field.value.substring( 0, maxlimit-1 );
		alert( 'Message can only contain ' + maxlimit + ' characters.' );
		return false;
	}
}

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder)
{
	if (window.event&&event.srcElement.value.length>=maxlength)
		return false;
	else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
		var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
	if (pressedkey.test(String.fromCharCode(e.which)))
		e.stopPropagation()
	}
}

function countlimit(maxlength,e,placeholder)
{
	var theform=eval(placeholder)
	var lengthleft=maxlength-theform.value.length
	var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
	if (window.event||e.target&&e.target==eval(placeholder))
	{
		if (lengthleft<0)
		theform.value=theform.value.substring(0,maxlength)
		placeholderobj.innerHTML=lengthleft
	}
}

function displaylimit(thename,theid,thelimit)
{
	var theform=theid!=""? document.getElementById(theid) : thename
	var limit_text='<span id="'+theform.toString()+'" class="showspan">'+thelimit+'</span> characters remaining'
	if (document.all||ns6)
		document.write(limit_text)
	if (document.all){
		eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
		eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
	}
	else if (ns6){
		document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
		document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
	}
}

//-----------------------------------------------------------------

function applyTag(obj, tag)
{
	wrapText(obj, '<'+tag+'>', '</'+tag+'>');
}

function wrapText(obj, beginTag, endTag)
{
	if(typeof obj.selectionStart == 'number')
	{
		// Mozilla, Opera, and other browsers
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;
		
		obj.value = obj.value.substring(0, start) + beginTag + obj.value.substring(start, end) + endTag + obj.value.substring(end, obj.value.length);
	}
	else if(document.selection)
	{
		// Internet Explorer

		// make sure it's the textarea's selection
		obj.focus();
		var range = document.selection.createRange();
		if(range.parentElement() != obj) return false;

	    if(typeof range.text == 'string')
	        document.selection.createRange().text = beginTag + range.text + endTag;
	}
	else
		obj.value += text;
}

//-----------------------------------------------------------------

function CheckContactForm() {

	var errmsg = '';
	
	if (document.getElementById('Name').value == '') {
		errmsg = errmsg + '- Please enter your name\n';
	}

	apos=document.getElementById('Email').value.indexOf("@")
	dotpos=document.getElementById('Email').value.lastIndexOf(".")
	if (apos<1||dotpos-apos<2) {
		errmsg = errmsg + '- Please correct your email address\n';
	}

if (document.getElementById('Comments').value == '') {
		errmsg = errmsg + '- Please enter your comments\n';
	}
	
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	if (errmsg == '') { 
		document.contact_form.submit();
	}

}

//-----------------------------------------------------------------

function CheckRegistration() {

	var errmsg = '';
	
	// Check first name to see if it's blank
	if (document.getElementById('First Name').value == '') {
		errmsg = errmsg + '- Please enter your first name\n';
	}
	// Check last name to see if it's blank
	if (document.getElementById('Last Name').value == '') {
		errmsg = errmsg + '- Please enter your last name\n';
	}
	
	// Check formatting of email address
	var apos=document.getElementById('Email').value.indexOf("@")
	var dotpos=document.getElementById('Email').value.lastIndexOf(".")
	if (apos<1||dotpos-apos<2) {
		errmsg = errmsg + '- Please correct your email address\n';
	}
	// Check formatting of email address confirmation
	var apos=document.getElementById('Email Confirm').value.indexOf("@")
	var dotpos=document.getElementById('Email Confirm').value.lastIndexOf(".")
	if (apos<1||dotpos-apos<2) {
		errmsg = errmsg + '- Please correct your confirmation email address\n';
	}
	// Check to make sure email and email confirmation are the same
	if (document.getElementById('Email').value != '' && document.getElementById('Email Confirm').value != '' && document.getElementById('Email').value != document.getElementById('Email Confirm').value) {
		errmsg = errmsg + '- Please make sure your email address is entered the same both times\n';
	}
	
	// Check username to see if it's blank
	if (document.getElementById('Username').value == '') {
		errmsg = errmsg + '- Please enter a username\n';
	}
	// Username must have between 3-10 characters
	if (document.getElementById('Username').value.length < 3 || document.getElementById('Username').value.length > 10) {
		errmsg = errmsg + '- Your username must contain between 3 and 10 characters\n';
	}
	// Username can contain ONLY the following characters
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
	var checkStr = document.getElementById('Username').value;
	var allValid = true;
	var i = '';
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		errmsg = errmsg + '- Your username must contain only letters, numbers and underscores\n';
	}
	
	// Check password to see if it's blank
	if (document.getElementById('Password').value == '') {
		errmsg = errmsg + '- Please enter a password\n';
	}
	// Check password confirmation to see if it's blank
	if (document.getElementById('Password Confirm').value == '') {
		errmsg = errmsg + '- Please confirm your password\n';
	}
	// Check to see that password and password confirmation are the same
	if (document.getElementById('Password').value != '' && document.getElementById('Password Confirm').value != '' && document.getElementById('Password').value != document.getElementById('Password Confirm').value) {
		errmsg = errmsg + '- Please make sure your password is entered the same both times\n';
	}
	// Password must have at least 6 characters
	if (document.getElementById('Password').value.length < 6) {
		errmsg = errmsg + '- Your password must contain at least 6 characters\n';
	}
	// Password can contain ONLY the following characters
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
	var checkStr = document.getElementById('Password').value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		errmsg = errmsg + '- Your password must contain only letters, numbers and underscores\n';
	}
	

	// If there's any errors, display them
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	// If there's no errors, submit the form
	if (errmsg == '') { 
		document.registration_form.submit();
	}

}

function CheckConfirmation() {

	var errmsg = '';
	
	// Check username to see if it's blank
	if (document.getElementById('Username').value == '') {
		errmsg = errmsg + '- Please enter your username\n';
	}
	// Username must have between 3-10 characters
	if (document.getElementById('Username').value.length < 3 || document.getElementById('Username').value.length > 10) {
		errmsg = errmsg + '- Your username must contain between 3 and 10 characters\n';
	}
	// Username can contain ONLY the following characters
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
	var checkStr = document.getElementById('Username').value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		errmsg = errmsg + '- Your username must contain only letters, numbers and underscores\n';
	}
	
	// Check confirmation key to see if it's blank
	if (document.getElementById('ConfirmationKey').value == '') {
		errmsg = errmsg + '- Please enter your confirmation key\n';
	}
	// Confirmation key can contain ONLY the following characters
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var checkStr = document.getElementById('ConfirmationKey').value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		errmsg = errmsg + '- Your confirmation key must contain only letters and numbers\n';
	}
	

	// If there's any errors, display them
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	// If there's no errors, submit the form
	if (errmsg == '') { 
		document.confirmation_form.submit();
	}

}

function CheckDenial() {

	var errmsg = '';
	
	// Check username to see if it's blank
	if (document.getElementById('Username').value == '') {
		errmsg = errmsg + '- Please enter your username\n';
	}
	// Username must have between 3-10 characters
	if (document.getElementById('Username').value.length < 3 || document.getElementById('Username').value.length > 10) {
		errmsg = errmsg + '- Your username must contain between 3 and 10 characters\n';
	}
	// Username can contain ONLY the following characters
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
	var checkStr = document.getElementById('Username').value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		errmsg = errmsg + '- Your username must contain only letters, numbers and underscores\n';
	}
	
	// Check confirmation key to see if it's blank
	if (document.getElementById('ConfirmationKey').value == '') {
		errmsg = errmsg + '- Please enter your confirmation key\n';
	}
	// Confirmation key can contain ONLY the following characters
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var checkStr = document.getElementById('ConfirmationKey').value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		errmsg = errmsg + '- Your confirmation key must contain only letters and numbers\n';
	}
	
	// Check reason to see if it's blank
	if (document.getElementById('Reason').value == '') {
		errmsg = errmsg + '- Please give a reason for denial\n';
	}
	

	// If there's any errors, display them
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	// If there's no errors, submit the form
	if (errmsg == '') { 
		document.denial_form.submit();
	}

}

function CheckForgot() {

	var errmsg = '';
	
	// Check to make sure they've entered a username OR email address
	if (document.getElementById('Username').value == '' && document.getElementById('Email').value == '') {
		errmsg = errmsg + '- You must enter your username OR email address\n';
	}

	if (document.getElementById('Email').value != '') {
		// Check formatting of email address
		apos=document.getElementById('Email').value.indexOf("@")
		dotpos=document.getElementById('Email').value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) {
			errmsg = errmsg + '- Please correct your email address\n';
		}
	}


	// If there's any errors, display them
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	// If there's no errors, submit the form
	if (errmsg == '') { 
		document.forgot_form.submit();
	}

}

function CheckReset() {

	var errmsg = '';
	
	// Check username to see if it's blank
	if (document.getElementById('Username').value == '') {
		errmsg = errmsg + '- Please enter your username\n';
	}
	// Check confirmation key to see if it's blank
	if (document.getElementById('Confirmation Key').value == '') {
		errmsg = errmsg + '- Please enter your confirmation key\n';
	}
	
	// Check password to see if it's blank
	if (document.getElementById('Password').value == '') {
		errmsg = errmsg + '- Please enter a password\n';
	}
	// Check password confirmation to see if it's blank
	if (document.getElementById('Password Confirm').value == '') {
		errmsg = errmsg + '- Please confirm your password\n';
	}
	// Check to see that password and password confirmation are the same
	if (document.getElementById('Password').value != '' && document.getElementById('Password Confirm').value != '' && document.getElementById('Password').value != document.getElementById('Password Confirm').value) {
		errmsg = errmsg + '- Please make sure your password is entered the same both times\n';
	}
	// Password must have at least 6 characters
	if (document.getElementById('Password').value.length < 6) {
		errmsg = errmsg + '- Your password must contain at least 6 characters\n';
	}
	// Password can contain ONLY the following characters
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
	var checkStr = document.getElementById('Password').value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		errmsg = errmsg + '- Your password must contain only letters, numbers and underscores\n';
	}


	// If there's any errors, display them
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	// If there's no errors, submit the form
	if (errmsg == '') { 
		document.reset_form.submit();
	}

}

//-----------------------------------------------------------------

function CheckEvent() {

	var errmsg = '';
	
	// Check event date to see if it's blank
	if (document.getElementById('event_date').value == '') {
		errmsg = errmsg + '- Please select a date\n';
	}
	
	// Check if start time is same as end time
	if (document.getElementById('start_hour').value == document.getElementById('end_hour').value && document.getElementById('start_min').value == document.getElementById('end_min').value && document.getElementById('start_ampm').value == document.getElementById('end_ampm').value && document.getElementById('noend').checked == false) {
		errmsg = errmsg + '- Start time and end time cannot be the same\n';
	}
	
	// Check event to see if it's blank
	if (document.getElementById('event').value == '') {
		errmsg = errmsg + '- Please enter your event info\n';
	}	

	// If there's any errors, display them
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	// If there's no errors, submit the form
	if (errmsg == '') { 
		document.add_event.submit();
	}

}

function CheckUpdateEvent() {

	var errmsg = '';
	
	// Check event date to see if it's blank
	if (document.getElementById('event_date').value == '') {
		errmsg = errmsg + '- Please select a date\n';
	}
	
	// Check if start time is same as end time
	if (document.getElementById('start_hour').value == document.getElementById('end_hour').value && document.getElementById('start_min').value == document.getElementById('end_min').value && document.getElementById('start_ampm').value == document.getElementById('end_ampm').value && document.getElementById('noend').checked == false) {
		errmsg = errmsg + '- Start time and end time cannot be the same\n';
	}
	
	// Check event to see if it's blank
	if (document.getElementById('event').value == '') {
		errmsg = errmsg + '- Please enter your event info\n';
	}	

	// If there's any errors, display them
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	// If there's no errors, submit the form
	if (errmsg == '') { 
		document.edit_event.submit();
	}

}

function DeleteEvent(id,text)
{
	var where_to= confirm("Are you sure you wish to delete this event?\n\n- " + text);
	if (where_to== true) {
		window.location="/admin/calendar/delete_event_db.asp?id=" + id;
	}
}

function CheckEventConfirmation() {

	var errmsg = '';
	
	// Check confirmation key to see if it's blank
	if (document.getElementById('ConfirmationKey').value == '') {
		errmsg = errmsg + '- Please enter your confirmation key\n';
	}
	// Confirmation key can contain ONLY the following characters
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var checkStr = document.getElementById('ConfirmationKey').value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		errmsg = errmsg + '- Your confirmation key must contain only letters and numbers\n';
	}
	

	// If there's any errors, display them
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	// If there's no errors, submit the form
	if (errmsg == '') { 
		document.confirmation_form.submit();
	}

}

//-----------------------------------------------------------------

function CheckNews() {

	var errmsg = '';
	
	// Check title to see if it's blank
	if (document.getElementById('title').value == '') {
		errmsg = errmsg + '- Please enter a title\n';
	}
	
	// Check news to see if it's blank
	if (document.getElementById('newstext').value == '') {
		errmsg = errmsg + '- Please enter your news\n';
	}	

	// If there's any errors, display them
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	// If there's no errors, submit the form
	if (errmsg == '') { 
		document.add_news.submit();
	}

}

function CheckUpdateNews() {

	var errmsg = '';
	
	// Check title to see if it's blank
	if (document.getElementById('title').value == '') {
		errmsg = errmsg + '- Please enter a title\n';
	}
	
	// Check news to see if it's blank
	if (document.getElementById('newstext').value == '') {
		errmsg = errmsg + '- Please enter your news\n';
	}	

	// If there's any errors, display them
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	// If there's no errors, submit the form
	if (errmsg == '') { 
		document.edit_news.submit();
	}

}

function DeleteNews(id,text)
{
	var where_to= confirm("Are you sure you wish to delete this news entry?\n\n- " + text);
	if (where_to== true) {
		window.location="/admin/news/delete_news_db.asp?id=" + id;
	}
}

//-----------------------------------------------------------------

function CheckPassword() {

	var errmsg = '';
	
	if (document.getElementById('newpassword').value != document.getElementById('newpassword2').value) {
		errmsg = errmsg + '- Your new passwords do not match. Please re-enter them to make sure they are correct.\n';
	}
	// Password must have at least 6 characters
	if (document.getElementById('newpassword').value.length < 6) {
		errmsg = errmsg + '- Your new password must contain at least 6 characters\n';
	}
	// Password can contain ONLY the following characters
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
	var checkStr = document.getElementById('newpassword').value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		errmsg = errmsg + '- Your new password must contain only letters, numbers and underscores\n';
	}
	
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	if (errmsg == '') { 
		document.changepass_form.submit();
	}

}

//-----------------------------------------------------------------

function CheckSport() {

	var errmsg = '';
	
	if (document.getElementById('sport').value == '') {
		errmsg = errmsg + '- Please enter a sport.\n';
	}
	
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	if (errmsg == '') { 
		document.add_sport.submit();
	}

}

function CheckGame() {

	var errmsg = '';
	
	if (document.getElementById('sport').value == '') {
		errmsg = errmsg + '- Please select a sport.\n';
	}
	
	if (document.getElementById('gameday').value == '') {
		errmsg = errmsg + '- Please select a date.\n';
	}
	
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	if (errmsg == '') { 
		document.add_game.submit();
	}

}

function CheckPhotos() {

	var errmsg = '';
	
	if (document.getElementById('whichsport').value == '') {
		errmsg = errmsg + '- Please select a sport.\n';
	}
	
	if (document.getElementById('game').value == '') {
		errmsg = errmsg + '- Please select a game.\n';
	}
	
	if (document.getElementsByName('file_0').value == '' && document.getElementsByName('file_1').value == '' && document.getElementsByName('file_2').value == '' && document.getElementsByName('file_3').value == '' && document.getElementsByName('file_4').value == '' && document.getElementsByName('file_5').value == '' && document.getElementsByName('file_6').value == '' && document.getElementsByName('file_7').value == '' && document.getElementsByName('file_8').value == '' && document.getElementsByName('file_9').value == '' && document.getElementsByName('file_10').value == '') {
		errmsg = errmsg + '- You must select at least 1 photo.\n';
	}
	
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	if (errmsg == '') { 
		document.add_photos.submit();
	}

}

/**
 * Convert a single file-input element into a 'multiple' input list
 *
 * Usage:
 *
 *   1. Create a file input element (no name)
 *      eg. <input type="file" id="first_file_element">
 *
 *   2. Create a DIV for the output to be written to
 *      eg. <div id="files_list"></div>
 *
 *   3. Instantiate a MultiSelector object, passing in the DIV and an (optional) maximum number of files
 *      eg. var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
 *
 *   4. Add the first element
 *      eg. multi_selector.addElement( document.getElementById( 'first_file_element' ) );
 *
 *   5. That's it.
 *
 *   You might (will) want to play around with the addListRow() method to make the output prettier.
 *
 *   You might also want to change the line 
 *       element.name = 'file_' + this.count;
 *   ...to a naming convention that makes more sense to you.
 * 
 * Licence:
 *   Use this however/wherever you like, just don't blame me if it breaks anything.
 *
 * Credit:
 *   If you're nice, you'll leave this bit:
 *  
 *   Class by Stickman -- http://www.the-stickman.com
 *      with thanks to:
 *      [for Safari fixes]
 *         Luis Torrefranca -- http://www.law.pitt.edu
 *         and
 *         Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com
 *      [for duplicate name bug]
 *         'neal'
 */
function MultiSelector( list_target, max ){

	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many files?
	this.files = 0;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};
	
	/**
	* Add a new file input element
	*/
	this.addElement = function( element ){

		// Make sure it's a file input element
		if( element.tagName == 'INPUT' && element.type == 'file' ){

			// Element name -- what number am I?
			element.name = 'file_' + this.count;

			// Add reference to this object
			element.multi_selector = this;

			// What to do when a file is selected
			element.onchange = function(){

				// New file input
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';
				new_element.className = 'new_fields';

				// Add new element
				this.parentNode.insertBefore( new_element, this );

				// Apply 'update' to element
				this.multi_selector.addElement( new_element );

				// Update list
				this.multi_selector.addListRow( this );

				// Hide this: we can't use display:none because Safari doesn't like it
				this.style.position = 'absolute';
				this.style.left = '-1000px';

			};
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.files >= this.max ){
				element.disabled = true;
			};

			// File element counter
			this.count++;
			this.files++;
			// Most recent element
			this.current_element = element;
			
		} else {
			// This can only be applied to file input elements!
			alert( 'Error: not a file input element' );
		};

	};

	/**
	* Add a new row to the list of files
	*/
	this.addListRow = function( element ){

		// Row div
		var new_row = document.createElement( 'div' );

		// Delete button
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.className = 'delete_button';
		new_row_button.value = 'Delete';

		// References
		new_row.element = element;

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.removeChild( this.parentNode );

			// Decrement counter
			this.parentNode.element.multi_selector.files--;

			// Re-enable input element (if it's disabled)
			this.parentNode.element.multi_selector.current_element.disabled = false;

			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			return false;
		};

		// Set row value
		// Use this if you want to display the full path
		// new_row.innerHTML = element.value;
		// Use this if you want to display only the filename
		//element_work = element.value;
		//element_tab = element_work.split("\\");
		//nbr_elements = element_tab.length;
		//new_row.innerHTML = element_tab[nbr_elements-1];
		var c = element.value.lastIndexOf('\/'); //For Unix, etc.
		if (c != -1)
		html = element.value.substring(c+1);
		else {
		var c = element.value.lastIndexOf('\\'); //Win
		if (c != -1)
		html = element.value.substring(c+1);
		else
		html = element.value;
		}

		// Add button
		// new_row.appendChild( new_row_button );
		new_row.appendChild( new_row_button );
		var new_row_text = document.createElement( 'span.photo_name' );
		new_row.appendChild(new_row_text);
		new_row_text.innerHTML = '&nbsp;&nbsp;&nbsp;' + html;

		// Add it to the list
		this.list_target.appendChild( new_row );
		
	};

};

function CheckDeleteSport(id) {
	var conf = confirm("If you delete this sport, all albums and photos uploaded in this sport will also be deleted.\n\nAre you sure you want to delete this sport?\n");
	
	if(conf) {
		window.open("/admin/photos/delete_sport_db.asp?id=" + id,"_self")
	}
}

function CheckDeleteGame(id) {
	var conf = confirm("If you delete this game/event, all photos uploaded in this album will also be deleted.\n\nAre you sure you want to delete this game/event?\n");
	
	if(conf) {
		window.open("/admin/photos/delete_game_db.asp?id=" + id,"_self")
	}
}

function CheckDeletePhotos() {
	count = 0;

	for(x=0; x<document.delete_photos.pic_id.length; x++) {
		if(document.delete_photos.pic_id[x].checked==true) {
			count++
		}
	}
	
	if(count==0) {
		alert ("You haven't selected any photos to delete");
	} else {
		var conf = confirm("Are you sure you want to delete these photos?\n");
		
		if(conf) {
			document.delete_photos.submit();
		}
	}
}