//////////////////////////////////////////////////////////////////////////////
//peopleValidateNewAccount
//
//Desc: makes sure that all the fields on the people page create account form
//are entered correctly before submitting.
//////////////////////////////////////////////////////////////////////////////
function peopleValidateNewAccount(){
	
	var submit = true;
	
	//First, make sure passwords match
	var pass1 = getEl("Password");
	var pass2 = getEl("Confirm_Password");
	
	if(pass1.value!=pass2.value){
		alert("Passwords don't match!");
		submit=false;
	}//end if
	
	//Second, make sure the email addresses match
	var email1 = getEl("Email");
	var email2 = getEl("Confirm_Email");
	
	if(email1.value!=email2.value && submit==true){
		alert("Email addresses don't match!");
		submit=false;
	}//end if
	
	//Third, make sure there's a username
	var user = getEl("User_Name");
	if(user.value.length<1 && submit==true){
		alert("You must enter a user name!");
		submit=false;
	}//end if
	
	//Fourth, make sure first and last names have been entered
	var first = getEl("First_Name");
	if(first.value.length<1 && submit==true){
		alert("You must enter a first name!");
		submit=false;
	}//end if
	
	var last = getEl("Last_Name");
	if(last.value.length<1 && submit==true){
		alert("You must enter a last name!");
		submit=false;
	}//end if
	
	
	//The finale, if everything checked out ok we can submit.
	if(submit)
		document.signup_form.submit();	
	else
		return false;
		
}//end function

//////////////////////////////////////////////////////////////////////////////
//updateDiv
//
//Desc: Calls a special version of updateContentArea just for use with
//run.php
//////////////////////////////////////////////////////////////////////////////
function updateDiv(divName,funct_name,param1,param2,param3,param4){
	
	//Die if the divName is bad
	var target = document.getElementById(divName);
	if(target==null && divName.length>0){
		console.error("updateDiv failed to find target: ",divName);
		return;
	}//end if
	
	
	var vars = "function_name="+funct_name+"&param1="+param1+"&param2="+param2+"&param3="+param3+"&param4="+param4;	
	updateContentArea(divName,vars,'../includes/php/run.php');
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//getEl
//
//Desc: A faster and easier to type document.getElementById()
//////////////////////////////////////////////////////////////////////////////
function getEl(el){
	return document.getElementById(el);
}//end function
//////////////////////////////////////////////////////////////////////////////
//showEl
//
//Desc: Sets target display to block
//////////////////////////////////////////////////////////////////////////////
function showEl(target){
	var obj = getEl(target);
	if(obj==null){
		alert(target+" = "+obj);
		return false;
	}else{
		obj.style.display="block";
		return true;
	}//end else
}//end function
//////////////////////////////////////////////////////////////////////////////
//hideEl
//
//Desc: Sets target display to none
//////////////////////////////////////////////////////////////////////////////
function hideEl(target){
	var obj = getEl(target);
	if(obj==null){
		alert(target+" = "+obj);
		return false;
	}else{
		obj.style.display="none";
		return true;
	}//end else
}//end function
//////////////////////////////////////////////////////////////////////////////
//toggleEl
//
//Desc: Switches target between block and none
//////////////////////////////////////////////////////////////////////////////
function toggleEl(target){
	var obj = getEl(target);
	if(obj==null){
		alert(target+" = "+obj);

	}else{
		if(obj.style.display=="none"){
			showEl(target);
		}else{
			hideEl(target);
		}//end if
	}//end else	
}//end function

//Used ont eh scores page. Closes all other opened items when hit
function toggleEl2(target){
	
	//toggle this element.
	var obj = getEl(target);
	if(obj==null){
		alert(target+" = "+obj);

	}else{
		if(obj.style.display=="none"){

			//close all other elements
			//$(".scores2").hide();

			showEl(target);

		}else{
			hideEl(target);
		}//end if
	}//end else	
}//end function


//////////////////////////////////////////////////////////////////////////////
//deletePhotoGallery
//
//Desc: Accepts a gallery id and calls ajax to perform a delete operation on 
//a given photo gallery
//////////////////////////////////////////////////////////////////////////////
function deletePhotoGallery(Gallery_ID){
	
	if(confirm("Do you want to delete this gallery?")){
		divName = "album"+Gallery_ID;
		updateDiv(divName,"deletePhotoGallery",Gallery_ID);
	}
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//deleteVideoGallery
//
//Desc: Accepts a gallery id and calls ajax to perform a delete operation on 
//a given video gallery
//////////////////////////////////////////////////////////////////////////////
function deleteVideoGallery(Gallery_ID){
	
	if(confirm("Do you want to delete this gallery?")){
		divName = "album"+Gallery_ID;
		updateDiv(divName,"deleteVideoGallery",Gallery_ID);
	}
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//joinGroup
//
//Desc: This function fires when the join this group button is fired. 
//It passed the user's id to a ajax for handling. The join group button is basically
//a one click form.
//////////////////////////////////////////////////////////////////////////////
function joinGroup(GroupID,UserID){
	updateDiv('joinGroup','joinGroup',GroupID,UserID);
}//end function
//////////////////////////////////////////////////////////////////////////////
//leaveGroupText
//
//Desc: used by the leave group text link, calls the
//leave group text routine.
//////////////////////////////////////////////////////////////////////////////
function leaveGroupText(GroupID,UserID){
	if(confirm("Do you really want to leave this group?")){
		var divName = "group"+GroupID;
		updateDiv(divName,"leaveGroupText",GroupID,UserID);
	}//end if
}//end function
//////////////////////////////////////////////////////////////////////////////
//addFriend
//
//Desc: called by the add friend button. 
//////////////////////////////////////////////////////////////////////////////
function addFriend(user_id,friend_id){
	var divName = "box_button"+friend_id;
	updateDiv(divName,"addFriend",user_id,friend_id);
}//end function
//////////////////////////////////////////////////////////////////////////////
//removeFriend
//
//Desc: called by the remove friend button
//////////////////////////////////////////////////////////////////////////////
function removeFriend(user_id,friend_id){
	var divName = "friend"+friend_id;
	updateDiv(divName,"removeFriend",user_id,friend_id);
}//end function
//////////////////////////////////////////////////////////////////////////////
//performChange
//
//my-messages-inbox.php
//
//Desc: This accepts a value which represents an action to be performed upon
//all checked messages on the page.
//
//Every message on the page has a radio button that is named sequentially from
//0 to $limit. Simply increment through and setTimeouts on updateDiv actions to
//perform the desired action on them all. 
//
//Its doubtful that we'll ever display 100 messages on one page. This should
//be a safe limit. If more than 100 messages show, this script will need to 
//be updated.
//////////////////////////////////////////////////////////////////////////////
function performChange(action){
	
	if(action==0){
		return; //do nothing
	}else if(action=="delete"){
		if(!confirm("Really delete selected messages?")){
			return;
		}//end if
	}//end else

	//Pass the page's info to an ajax function and then refresh the page.
	
	//1. Get list of selected elements
	var selected = "";
	
	for(x=0;x<100;x++){
		var el = document.getElementById('selected'+x);
		
		if(el==null) break;
		
		if(el.checked==true){
			selected = selected+el.value+"|";
		}//end if	
	}//end
	
	var vars = "function_name=updateMessages&param1="+action+"&param2="+selected;
	updateContentArea('ajaxTarget',vars,"run.php","redirect('my-messages-inbox.php')");	
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//redirect
//
//Desc: changes location.href to new url
//////////////////////////////////////////////////////////////////////////////
function redirect(url){
	location.href=url;
}//end function
//////////////////////////////////////////////////////////////////////////////
//changeGallery
//
//Desc: This function moves a video from one gallery to another
//////////////////////////////////////////////////////////////////////////////
function changeGallery(video_id,gallery_id){
	if(confirm("Move this video to another gallery?")){
		var divName = "video_"+video_id;
		updateDiv(divName,"changeGallery",video_id,gallery_id);
	}else{
		return;
	}//end else
}//end function
//////////////////////////////////////////////////////////////////////////////
//getScores
//
//Desc: This function calls ajax to print content for the scores page
//////////////////////////////////////////////////////////////////////////////
function getScores(Team_ID){
	updateDiv('scoresRight','getScores',Team_ID);
}//end function
//////////////////////////////////////////////////////////////////////////////
//savePlayerUpdate
//
//Desc: Called by roster-edit.php. Grabs values from form elements positions and
//player number. Passes these to a php function
//////////////////////////////////////////////////////////////////////////////
function savePlayerUpdate(player_id,bgcolor){
	var positions = getEl('positions'+player_id);
	var player_number = getEl('player_number'+player_id);
	
	var divName = "player"+player_id;	
	updateDiv(divName,'savePlayerUpdate',player_id,positions.value,player_number.value,bgcolor);
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//removePlayer
//
//Desc: A function that confirms the user really wants to remove a player
//from a roster before the php runs to remove them
//////////////////////////////////////////////////////////////////////////////
function removePlayer(player_id){
	if(confirm("Really remove this player from this roster?")){
		var divName = "player"+player_id;
		updateDiv(divName,'removePlayer',player_id);
	}//end if
}//end if
//////////////////////////////////////////////////////////////////////////////
//resetPassword
//
//Desc: gets the user name from the DOM and then calls ajax to perform reset and
//notify actions.
//////////////////////////////////////////////////////////////////////////////
function resetPassword(){
	var username = getEl('passwordUserName');
	username = username.value;
	updateDiv('lostPassword','resetPassword',username);	
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateGradField
//
//
//Desc: checks school type and disabled graduation date if the user selected
//none as their school
//////////////////////////////////////////////////////////////////////////////
function updateGradField(){
	var sid = getEl('School_ID');
	var grad = getEl('Graduation_Date');
	var label = getEl('GradLabel');
	if(sid.value==0){
		grad.style.display="none";
		grad.value=0;
		label.style.display="none";
	}else{
		grad.style.display="inline";
		label.style.display="inline";
	}//end if
}//end
//////////////////////////////////////////////////////////////////////////////
//deleteMessage
//
//
//Desc: called from the inbox to delete a message
//////////////////////////////////////////////////////////////////////////////
function deleteMessage(ID,start,limit){
	
	if(confirm("Delete this message?")){
		updateContentArea('messagesList', "action=deleteMessage&ID="+ID+"&start="+start+"&limit="+limit, '../includes/php/inc.functions-jeremy_ajax.php');	
	}//end if
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//deleteMessage2
//
//
//Desc: called from the message profile page to delete a message
//////////////////////////////////////////////////////////////////////////////
function deleteMessage2(ID,start,limit){
	
	if(confirm("Delete this message?")){
		updateContentArea('messageBox', "action=deleteSingleMessage&ID="+ID+"&start="+start+"&limit="+limit, '/includes/php/inc.jeremy_ajax.php', 'pageJump(1)');	
	}//end if
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//launchChat()
//
//
//Desc: opens the chat window
//////////////////////////////////////////////////////////////////////////////
function launchChat(prepend){

	window.open(prepend+'chat.php','Chat','width=650,height=400,resizable=1');

}//end function
//////////////////////////////////////////////////////////////////////////////
//deleteMySayIt
//
//
//Desc: confirms and then calls ajax to nuke the given sayit
//////////////////////////////////////////////////////////////////////////////
function deleteMySayIt(ID){
	if(confirm("Delete this Say It?")){
		updateDiv('sayits','deleteMySayIt',ID);
	}//end if
}//end function
function deleteGroupSayIt(ID){
	if(confirm("Delete this Say It?")){
		updateDiv('sayits','deleteGroupSayIt',ID);
	}//end if
}//end function
//////////////////////////////////////////////////////////////////////////////
//deleteComment
//
//
//Desc: confirms and then calls ajax to nuke the given sayit
//////////////////////////////////////////////////////////////////////////////
function deleteComment(ID){
	if(confirm("Delete this comment?")){
		
		$('#mediaComments').load('../includes/php/run.php?function_name=deleteMediaComment&param1='+ID);
		
		//updateDiv('sayits','deleteMediaComment',ID);
	}//end if
}//end function
//////////////////////////////////////////////////////////////////////////////
//showInvitation
//
//
//Desc: asks the user if they want to accept the invitation to join the
//given group. Updates the db depending on their answer
//////////////////////////////////////////////////////////////////////////////
function showInvite(group_id,user_id){
	
	if(confirm("You've been invited to join this group. Do you accept the invitation?")){

		var vars = "function_name=inviteResponse&param1="+group_id+"&param2="+user_id+"&param3=1";
	
	}else{
	
		var vars = "function_name=inviteResponse&param1="+group_id+"&param2="+user_id+"&param3=0";
		
	}//end else

	updateContentArea('ajaxTarget',vars,"../run.php","redirect('groups-profile.php?Group_ID="+group_id+"')");	
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//showhidedate
//
//
//Desc: used by the login-registration page. If the grade selected is 'not applicable'
//then it makes the grad date field vanish.
//////////////////////////////////////////////////////////////////////////////
function showhidedate(value){
	
	//value is the currently selected value of the pulldown.
	var el1 = document.getElementById('Graduation_Date_month');
	var el2 = document.getElementById('Graduation_Date_day');
	var el3 = document.getElementById('Graduation_Date_year');
	
	if(el1){

		if(value==1){
			//they chose not applicable, hide the field
			el1.style.display="none";
			el2.style.display="none";
			el3.style.display="none";
		}else{
			el1.style.display="inline";
			el2.style.display="inline";
			el3.style.display="inline";			
		}//end else

	}//end if
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//scaleImg
//
//
//Desc: enforces a maximum width/height on an image.
//var img = an object passed in usally as this onload
//var max_size = size in pixels the height and width are not allowed to exceed 
//////////////////////////////////////////////////////////////////////////////
function scaleImg(img,max_size){
	
	var height = img.height;
	var width = img.width;
	
	
	//Find the longest side and check to see if it exceeds that max
	if(height>=width){
		//perform stuff based on height
		
		//Is it too big?
		if(height>max_size){
			//yes, resize it with respect to the aspect ratio

			//find the ratio
			var ratio = width/height;
			
			//reset the height
			img.height=max_size;
			height=img.height;
			
			//reset the width
			img.width=(height*ratio);
			
		}//end if
		
	}else{
		//perform stuff based on width
		
		//Is it too big?
		if(width>max_size){
			//yes, resize it with respect to the aspect ratio
			
			//find the ratio
			var ratio = height/width;
			
			//reset width
			img.width=max_size;
			width=img.width;
			
			//reset height
			img.height = (width*ratio);
			
		}//end if
		
	}//end if

	
}//end function
//////////////////////////////////////////////////////////////////////////////
//tagTeam
//
//
//Desc: This calls the functions needed to tag an entire team roster to an
//image or video.
//////////////////////////////////////////////////////////////////////////////
function tagTeam(Team_ID,Media_ID,Gallery_ID,Media_Type){

	var div = 'peopleList_'+Media_ID;
	
	updateDiv(div,'tagTeam',Team_ID,Media_ID,Gallery_ID,Media_Type);
	
}//end function


function checkAgree(){
	if(document.getElementById('terms_agree').checked==true){
		return true;
	}else{
		window.alert("You must agree to the terms and conditions");
		return false;
	}
}