// JavaScript Document
function doNav(theUrl) {
	document.location.href = theUrl;
}
function changeCursor(tableRow, highLight) {
	if (highLight) {
		tableRow.style.cursor="pointer";
	}
}
function accountHover(tableRow, highLight) {
	if (highLight) {
		tableRow.className = "accounthover";
		tableRow.style.cursor="pointer";
	} else {
		tableRow.className = "account";
	}
}
var loader = new Image();
loader.src = 'images/red_loader.gif';
function pleaseWait(table,row,cell) {
	//var button = document.getElementById('submitbutton');
	var x=document.getElementById(table).rows[row].cells;
	x[cell].innerHTML='<img src="images/red_loader.gif" align="absbottom"> Please Wait...';
	//button.style.color="#999999";
	//button.disabled=true;
}
function printPage() {
	window.print();
}
function deleteUser(id) {
	if(confirm('Are you sure you want to delete this user?')) {
		doNav('admin_users.php?action=process&section=delete&id='+id);
	}
}
function deleteThis(id,type) {
	if(confirm('Are you sure you want to completely remove this '+type+'?')) {
		var url = $(id).href;
		doNav(url);
	}
	return false;
}
function deleteParagraph(id,page) {
	if(confirm('Are you sure you want to delete this paragraph?')) {
		doNav('page_info.php?action=process&section=delete&id='+id+'&return='+page);
	}
}
function deleteNews(id) {
	if(confirm('Are you sure you want to delete this piece of news?')) {
		doNav('news.php?action=process&section=delete&id='+id);	
	}
}
function deleteResource(id) {
	if(confirm('Are you sure you want to delete this resource?')) {
		doNav('resources.php?action=process&section=delete&id='+id);	
	}
}
function deleteLink(id) {
	if(confirm('Are you sure you want to delete this link?')) {
		doNav('links.php?action=process&section=delete&id='+id);	
	}
}
function showResourceDescription(id) {
	new Effect.toggle('file-'+id,'slide');	
}

// calendar function
var pagename = 'calendar.php';
var adminpagename = 'admin_calendar.php';
function updateDate() {
	var month = document.getElementById("top_month").value;
	var year = document.getElementById("top_year").value;
	document.location.href = "calendar.php?month="+month+"&year="+year;
}
function updateAdminDate() {
	var month = document.getElementById("top_month").value;
	var year = document.getElementById("top_year").value;
	document.location.href = "admin_calendar.php?month="+month+"&year="+year;
}
function getDDCal(id,month,year) {
	if(document.getElementById('cal'+id).style.display == 'none') {
		new Effect.Appear('cal'+id,{duration:.2});
	}
	document.getElementById('cal'+id).innerHTML = '<div id="contentLoading" style="display:none;" align="center"><br>Loading...<br /><br /><img src="images/gray_loader.gif" width="24" height="24" /></div>';
	new Effect.Appear('contentLoading',{duration:.1});
	new Ajax.Request('dd_cal.php?month='+month+'&year='+year+'&id='+id,
		 {
			method:'get',
			onComplete: function(transport){
				new Effect.Fade('contentLoading',{duration:.1});
				var response = transport.responseText || "no response text";
				document.getElementById('cal'+id).innerHTML = response;
				$('innercal'+id).hide();
			  	new Effect.Appear('innercal'+id, { duration:.3, queue: 'end' });
			},
			onFailure: function(){ alert('Something went wrong...Please select a date again.') }
		  });
}
function getDDCal2(id,month,year) {
	new Ajax.Request('dd_cal.php?month='+month+'&year='+year+'&id='+id,
		 {
			method:'get',
			onComplete: function(transport){
				var response = transport.responseText || "no response text";
				document.getElementById('cal'+id).innerHTML = response;
			},
			onFailure: function(){ alert('Something went wrong...Please select a date again.') }
		  });
}
function closeDDCal(id) {
	new Effect.Fade('cal'+id,{duration:.2});
}
function selectDay(id,month,day,year) {
	
	new Effect.Fade('cal'+id,{duration:.2});
	document.getElementById('caldate'+id).value = month+'/'+day+'/'+year;
}
function repeatsEveryWeek() {
	var repeatEvery=document.getElementById("repeatEveryWeek");
	if (repeatEvery.options[repeatEvery.selectedIndex].text != '1') {
		var x=document.getElementById("repeatsEvery").rows["0"].cells;
		x["1"].innerHTML='weeks';
	} else {
		var x=document.getElementById("repeatsEvery").rows["0"].cells;
		x["1"].innerHTML='week&nbsp;';
	}
}
function repeatsEveryMonth() {
	var repeatEvery=document.getElementById("repeatEveryMonth");
	if (repeatEvery.options[repeatEvery.selectedIndex].text != '1') {
		var x=document.getElementById("repeatsEvery").rows["0"].cells;
		x["1"].innerHTML='months';
	} else {
		var x=document.getElementById("repeatsEvery").rows["0"].cells;
		x["1"].innerHTML='month&nbsp;';
	}
}
function repeatConfig() {
	var repeat=document.getElementById("repeatDropDown");
	if (repeat.options[repeat.selectedIndex].text == 'Weekly') {
		var x=document.getElementById("repeats001").rows["0"].cells;
		x["0"].innerHTML='<table id="repeatsEvery" cellpadding="2" cellspacing="2" style="border:solid; border-width:1px; border-color:#CCCCCC; border-collapse:collapse;"><tr><td align="left" valign="top" class="pagetext" width="140">Repeats every: <select name="repeatsevery" id="repeatEveryWeek" onchange="repeatsEveryWeek()"><option>1</option><option>2</option><option>3</option><option>4</option></select></td><td align="left" class="pagetext" valign="top" width="260">week&nbsp;</td></tr><tr><td align="left" class="pagetext" colspan="2" valign="top">Repeat On: <input type="checkbox" name="sunday" value="0">S&nbsp;&nbsp;<input type="checkbox" name="monday" value="1">M&nbsp;&nbsp;<input type="checkbox" name="tuesday" value="2">T&nbsp;&nbsp;<input type="checkbox" name="wednesday" value="3">W&nbsp;&nbsp;<input type="checkbox" name="thursday" value="4">T&nbsp;&nbsp;<input type="checkbox" name="friday" value="5">F&nbsp;&nbsp;<input type="checkbox" name="saturday" value="6">S&nbsp;&nbsp;</td></tr></table><img src="images/spacer.gif" width="100%" height="5">';
	} else if (repeat.options[repeat.selectedIndex].text == 'Monthly') {
		var x=document.getElementById("repeats001").rows["0"].cells;
		x["0"].innerHTML='<table id="repeatsEvery" cellpadding="2" cellspacing="2" style="border:solid; border-width:1px; border-color:#CCCCCC; border-collapse:collapse;"><tr><td align="left" valign="top" class="pagetext" width="140">Repeats every: <select name="repeatsevery" id="repeatEveryMonth" onchange="repeatsEveryMonth()"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option></select></td><td align="left" class="pagetext" valign="top" width="250">month&nbsp;</td></tr><tr><td align="left" class="pagetext" colspan="2" valign="top">Repeat By: <input type="radio" name="repeatby" value="month"> day of the month&nbsp;&nbsp;<input type="radio" name="repeatby" value="week"> day of the week&nbsp;&nbsp;</td></tr></table><img src="images/spacer.gif" width="100%" height="5">';
	} else {
		var x=document.getElementById("repeats001").rows["0"].cells;
		x["0"].innerHTML='<img src="images/spacer.gif" width="100%" height="5">';
	}
}
function fullDay(element) {
	if (element.checked) {
		var x=document.getElementById("when").rows["0"].cells;
		x["1"].innerHTML="&nbsp;to&nbsp;";
	} else {
		var x=document.getElementById("when").rows["0"].cells;
		x["1"].innerHTML='<input type="text" name="start_time" value="8:00am" size="8">&nbsp;to&nbsp;<input type="text" name="end_time" value="9:00am" size="8">';
	}
}
function toggleAppear(id) {
	var divs = $$('div.editdiv');
	for(i=0;i<divs.length;i++) {
		if(divs[i]['id'] == 'edit'+id) {
			new Effect.toggle(divs[i]['id'],'appear',{duration:.2});		
		} else {
			new Effect.Fade(divs[i]['id'],{duration:.2});
		}
	}
}
function deleteEvent(id,m,y) {
	if(confirm('Are you sure you want to delete this event?')) {
		doNav(adminpagename+'?action=process&section=delete&id='+id+'&month='+m+'&year='+y);	
	}
}
function deleteEventSeries(id,m,y) {
	if(confirm('Are you sure you want to delete this series of events?')) {
		doNav(adminpagename+'?action=process&section=deleteseries&seriesid='+id+'&month='+m+'&year='+y);	
	}
}
function togglePopUp(id,flip) {
	var divs = $$('div.eventPopUp');
	for(i=0;i<divs.length;i++) {
		if(divs[i]['id'] == id) {
			if(flip == 'yes') {
				var pos = $('a-'+id).viewportOffset();
				new Effect.Move(divs[i]['id'],{x:pos['left']-187,y:pos['top']+13,mode:'absolute',duration:0});
			} else {
				var pos = $('a-'+id).viewportOffset();
				new Effect.Move(divs[i]['id'],{x:pos['left']-3,y:pos['top']+13,mode:'absolute',duration:0});
			}
			new Effect.toggle(divs[i]['id'],'appear',{duration:.1});
		} else {
			if(flip == 'yes') {
				var pos = $('a-'+divs[i]['id']).viewportOffset();
				new Effect.Move(divs[i]['id'],{x:pos['left']-187,y:pos['top']+13,mode:'absolute',duration:0});
			} else {
				var pos = $('a-'+divs[i]['id']).viewportOffset();
				new Effect.Move(divs[i]['id'],{x:pos['left']-3,y:pos['top']+13,mode:'absolute',duration:0});
			}
			new Effect.Fade(divs[i]['id'],{duration:.1});
		}
	}
}

//photos and categories
function deleteGallery(id) {
	var remove = confirm('This will delete this gallery as well as all photos and categories within this gallery.  This action is unrecoverable. Are you sure you want to delete this gallery?');
	if(remove == true) {
		doNav('admin_pictures.php?action=process&section=deletecat&catid='+id);	
	}
}
function deletePhoto(catid,id) {
	var remove = confirm('You are about to delete this photo from the database.  This action is unrecoverable. Are you sure you want to delete this photo?');
	if(remove == true) {
		doNav('admin_pictures.php?action=process&section=deletephoto&catid='+catid+'&id='+id);	
	}
}
/*function getThumbs(catid) {
	if(document.getElementById('content').style.display != 'none') {
		new Effect.Fade('thumbs');
	}
	new Ajax.Request('modules/thumbs.php?catid='+catid,
		 {
			method:'get',
			onLoading: function(t) {
			  	if(document.getElementById('content').style.display == 'none') {
					new Effect.BlindDown('content',{duration:1.5});
				}
				document.getElementById('content').innerHTML = '<div id="contentLoading" style="display:none;">Loading...<br /><br /><img src="images/ajax-loader-1.gif" width="24" height="24" /></div>';
			  	new Effect.Appear('contentLoading',{duration:.8});
			},
			onComplete: function(transport){
				new Effect.Fade('contentLoading',{duration:.3});
			  var response = transport.responseText || "no response text";
			  document.getElementById('content').innerHTML = response;
			  	new Effect.Appear('thumbs', { queue: 'end' });
			},
			onFailure: function(){ alert('Something went wrong...') }
		  });
}*/
function openPhoto(id) {
	var w = document.viewport.getWidth();
	var start = w/2;
	var left = start - 350;
	new Effect.Move('loadingFrame',{x:left,y:0,mode:'absolute',duration:0});
	new Effect.SlideDown('loadingFrame',{duration:.5});
	new Ajax.Request('ajax_largeimage.php?id='+id,
		 {
			method:'get',
			onSuccess: function(text){
			  var response = text.responseText || "no response text";
			  document.getElementById('content').innerHTML = response;
				var w = document.viewport.getWidth();
				var start = w/2;
				var left = start - 350;
				new Effect.Move('largePhotoFrame',{x:left,y:0,mode:'absolute',duration:0});
			  document.getElementById('img'+id).onload = function() {
			    new Effect.SlideUp('loadingFrame',{duration:.5});
				new Effect.SlideDown('largePhotoFrame',{queue:'end'});  
			  }
			},
			onFailure: function(){ 
				alert('Something went wrong...')
				new Effect.SlideUp('loadingFrame');
			}
		  });
}
function closePhoto() {
	new Effect.SlideUp('largePhotoFrame');	
}

//schedule
function updateTeam() {
	$('grayLoader').show();
	var team = document.getElementById('team');
	var newteam = team.options[team.selectedIndex].value;
	doNav(pagename+'?action=viewteam&team='+newteam);
}
function updateScheduleDate() {
	var month = document.getElementById("top_month").value;
	var year = document.getElementById("top_year").value;
	document.location.href = pagename+"?month="+month+"&year="+year;
}
function updateAdminScheduleDate() {
	var month = document.getElementById("top_month").value;
	var year = document.getElementById("top_year").value;
	document.location.href = adminpagename+"?action=schedule&month="+month+"&year="+year;
}
function deleteScheduleEvent(id,m,y) {
	if(confirm('Are you sure you want to delete this event?')) {
		doNav(adminpagename+'?action=process&section=delete&id='+id+'&month='+m+'&year='+y);	
	}
}
function deleteScheduleEventSeries(id,m,y) {
	if(confirm('Are you sure you want to delete this series of events?')) {
		doNav(adminpagename+'?action=process&section=deleteseries&seriesid='+id+'&month='+m+'&year='+y);	
	}
}
function deleteTeam(id) {
	if(confirm('Are you sure you want to delete this team?')) {
		doNav('admin_standings.php?action=process&section=deleteteam&teamid='+id);	
	}
}
// registration functions
function registrationSettingsType() {
	$('grayLoader').show();
	var type = document.getElementById('type');
	var typeid = type.options[type.selectedIndex].value;
	doNav('admin_registration.php?action=selecttype&typeid='+typeid);
}
function changePaymentMethod(payment) {
	if(payment == 'credit') {
		new Effect.BlindUp('checkdiv',{duration:.5});
		new Effect.BlindDown('creditdiv',{duration:.5});
	} else if(payment == 'check') {
		new Effect.BlindUp('creditdiv',{duration:.5});
		new Effect.BlindDown('checkdiv',{duration:.5});
	}
}
function updateRegType() {
	var regType = document.getElementById('registrationtype');
	var id = regType.options[regType.selectedIndex].value;
	new Ajax.Request('ajax_payment.php?id='+id,
		 {
			method:'get',
			onSuccess: function(text){
			  var response = text.responseText || "no response text";
			  document.getElementById('paymentMethodDiv').innerHTML = response; 
			  new Effect.SlideDown('paymentMethodDiv',{duration:.5});
			},
			onFailure: function(){ 
				alert('Something went wrong...');
			}
		  });
}
function copyFathersInfo() {
	var copyinfo = document.getElementById('fathers_info').checked;
	if(copyinfo) {
		var last = $('last').value;	
		var address = $('address').value;
		var city = $('city').value;
		var state = $('state').value;
		var zip = $('zip').value;
		var phone = $('phone').value;
		$('father_last').value = last;
		$('father_address').value = address;
		$('father_city').value = city;
		$('father_state').value = state;
		$('father_zip').value = zip;
		$('father_phone').value = phone;
	} else {
		$('father_last').value = '';
		$('father_address').value = '';
		$('father_city').value = '';
		$('father_state').value = '';
		$('father_zip').value = '';
		$('father_phone').value = '';
	}
}
function copyMothersInfo() {
	var copyinfo = document.getElementById('mothers_info').checked;
	if(copyinfo) {
		var last = $('last').value;	
		var address = $('address').value;
		var city = $('city').value;
		var state = $('state').value;
		var zip = $('zip').value;
		var phone = $('phone').value;
		$('mother_last').value = last;
		$('mother_address').value = address;
		$('mother_city').value = city;
		$('mother_state').value = state;
		$('mother_zip').value = zip;
		$('mother_phone').value = phone;
	} else {
		$('mother_last').value = '';
		$('mother_address').value = '';
		$('mother_city').value = '';
		$('mother_state').value = '';
		$('mother_zip').value = '';
		$('mother_phone').value = '';
	}
}
function deleteRegistration(id) {
	if(confirm('Deleting this registrant will erase all of their information from the database.  Their credit card will need to be refunded manually.  This action is unrecoverable.  Are you sure you want to delete this registrant?')) {
		doNav('admin_registration.php?action=process&section=delete&id='+id);
	}
}
function deleteGame(gameid,teamid) {
	if(confirm('Are you sure you want to delete this game?')) {
		doNav('admin_standings.php?action=process&section=deletegame&gameid='+gameid+'&teamid='+teamid);
	}
}
var fadeImage = function() {
	new Effect.Appear('fadephoto');
	new Effect.Fade('loadingDiv');
	}
function showPhoto(id) {
	if(document.getElementById('fadephoto').style.display != 'none') {
	new Effect.Fade('fadephoto');
	}
	new Ajax.Request('modules/photolarge.php?photoid='+id,
		 {
			method:'get',
			onLoading: function() {
				new Effect.Appear('loadingDiv');
			},
			onSuccess: function(text){
			  var response = text.responseText || "no response text";
			  document.getElementById('bigphoto').innerHTML = response;
			  document.getElementById('img'+id).onload = fadeImage;
			},
			onFailure: function(){ alert('Something went wrong...') }
		  });
}
function sendTestNewsletter() {
	var email = prompt("Please enter 1 email address to send this test to");
	doNav('newsletter.php?action=sendtest&email='+email);
}
function showStandings() {
	if($('show').checked) {
		new Effect.SlideDown('showDiv',{duration:1});	
	} else {
		new Effect.SlideUp('showDiv',{duration:1});
	}
}
function addEmail(el,i) {
	var el = $(el).identify();
	$('emailContainer').insert('<div id="emaildiv'+i+'"><input type="text" name="email[]" value="" id="email'+i+'" size="40" /> <a href="#" onclick="return removeEmail(\''+i+'\');" class="required">Remove</a></div>');
	i++;
	$(el).onclick = function() { return addEmail(el,i); };
	return false;
}
function removeEmail(i) {
	$('emaildiv'+i).remove();
	return false;
}