loadfriends = function()
{
	engFriends.getFriends(callback_loadfriends);
}

callback_loadfriends = function(res)
{
	checkResponse(res.value);
	
	var dt = res.value;
	var ul = document.getElementById("ulFriends"); 
	ul.innerHTML = "";
		
	if (dt.Rows.length!=0)
	{
		for (var i=0; i<dt.Rows.length; i++)
		{
			var li = document.createElement("li")
			
			var img = document.createElement("img")
			img.src = dt.Rows[i].src;
			img.alt = "";
			
			
			var aProfile = document.createElement("a")
			aProfile.href = "/profile/index.aspx?x=" + dt.Rows[i].link;
			aProfile.title = "View profile.";
			aProfile.appendChild(img);
			
			//aProfile.innerHTML = dt.Rows[i].Username;
			
			var spnUsername = document.createElement("span")
			spnUsername.innerHTML = dt.Rows[i].Username;
			
			aProfile.appendChild(spnUsername);
			
			
			li.appendChild(aProfile)
			
			var spnPlace = document.createElement("span")
			if (dt.Rows[i].Timezone!=null)
				{
				spnPlace.innerHTML = dt.Rows[i].City + dt.Rows[i].Country_Desc;
				}
			else
				{
				spnPlace.innerHTML = dt.Rows[i].Country_Desc;
				}
			li.appendChild(spnPlace)
			
			var spnFullname = document.createElement("span")
			if (dt.Rows[i].showRealName)
				{
				spnFullname.innerHTML = dt.Rows[i].Fullname;
				li.appendChild(spnFullname)
				}

				
			var aRemove = document.createElement("a")
			aRemove.innerHTML = "Remove";
			aRemove.href = "javascript:showRemoveFriend(" + dt.Rows[i].FID + ");";
			aRemove.className = "remove";
			
			li.appendChild(aRemove)
						
			ul.appendChild(li)
		}
	}
	else
	{
	
	}
	//stripetableBody();
}

addFriend = function()
{
	var Email = getControlValue('txtEmail');
	
	showFeedback('addFriendActionContainer');
	engFriends.friend_add('txtEmail', Email, callback_addNewFriend);	
}	

callback_addNewFriend = function(res)
{
	if (res.error!=null){alert(res.error)};
	checkResponse(res.value);
	var results = res.value.Tables[0];
	
	if (results.Rows[0].result == "True")
	{
		var controls = res.value.Tables[1];
		for (var i=0; i<controls.Rows.length; i++)
		{
			setControlErrorMessage(controls.Rows[i].controlID,'');
		}
		switch (results.Rows[0].errMessage)
		{
			case "invite":
				feedbackValid('Checking','Not yet an obami user');
				//setSpanValue('inviteFriendEmail',getControlValue('txtEmail'));
				//window.setTimeout("showInviteUser();", 4000);
				window.setTimeout("cleanupFeedback('addFriendActionContainer');", 4000);
			break
		}
	}
	else
	{
		var controls = res.value.Tables[1];
		switch (results.Rows[0].errMessage)
		{
			case "invalid":
				feedbackInvalid('addFriendActionContainer');
				window.setTimeout(function(){processValidationResponse(controls)}, 2001);
			break
			case "exists":
				feedbackValid('Checking','Found');
				window.setTimeout(function(){showAddRegFriend(res.value.Tables[2])}, 4000);
				
			break
			case "NoMatches":
				feedbackValid('Checking','No Matches Found');
				window.setTimeout("cleanupFeedback('addFriendActionContainer');", 4000);				
			break
			case "friend":
				if(results.Rows[0].errValue == 1)
				{
				feedbackValid('Checking','User is already one of your friends');
				window.setTimeout("hideAddFriend();", 4000);
				window.setTimeout("cleanupFeedback('addFriendActionContainer');", 4000);
				}
				else
				{
				feedbackValid('Checking','Friend request pending');
				window.setTimeout("hideAddFriend();", 4000);
				window.setTimeout("cleanupFeedback('addFriendActionContainer');", 4000);
				}
			break
			case "OwnEmailid":
			feedbackValid('Checking','You cannot send a friend request to yourself');
			//window.setTimeout(function(){processValidationResponse(controls)}, 2001);
			window.setTimeout("hideAddFriend();", 4000);
			window.setTimeout("cleanupFeedback('addFriendActionContainer');", 4000);
			break
		}
	}
}


hideAddFriend = function()
{
	setSpanValue('confirmAddFriend',"");
	setControlDisplay('confirmAddFriend','none');
	closeModalWindow('addFriend');
}

showInviteUser = function()
{
	//openModalWindow('inviteUser');
	cleanupFeedback('addFriendActionContainer');
	showChildPanel('addFriendSlide','inviteSlide');
}

sendEmailInvite = function()
{
	var email = getSpanValue('inviteFriendEmail');
	showFeedback('sendInviteActionContainer');
	engFriends.sendInvite(email, callback_sendEmailInvite);
}

callback_sendEmailInvite = function(res)
{
	if (res.error!=null){alert(res.error)};
	checkResponse(res.value);

	switch (res.value)
	{
		case "sent":
		feedbackValid('Sending','An invite has been sent to: ' + getSpanValue('inviteFriendEmail'));
		
		window.setTimeout("hideInviteFriend();", 4000);
		
		break
		case "mailoff":
		alert('email off');
		window.setTimeout("hideInviteFriend();", 750);
		break
		case "notsent":
		alert('email not sent');
		break
	}
}

hideInviteFriend = function()
{	
	closeModalWindow('inviteUser');
	cleanupFeedback('sendInviteActionContainer');
	window.setTimeout("hideAddFriend();", 500);
}

showAddRegFriend = function(res)
{
	cleanupFeedback('addFriendActionContainer');
	var emails = '';
	
	for (var a = 0; a < res.Rows.length; a++) {
		emails += res.Rows[a].email + ',';
	}
	
	engFriends.getRegUserDetails(emails, callback_getRegUserDetails)
}

callback_getRegUserDetails = function(res)
{
	if (res.error!=null){alert(res.error)};
	checkResponse(res.value);

	var ds = res.value;
	
		
	if (ds.Tables.length == 1) {
	  document.getElementById('oneEmail').style.display = 'block';
	  document.getElementById('multipleEmails').style.display = 'none';
	  document.getElementById('editMyFriends').style.display = 'none';	  
	  setSpanValue('addRegFriendID', ds.Tables[0].Rows[0].encUserID);
	  setSpanValue('addRegFriendEmail', ds.Tables[0].Rows[0].Username + "("+ ds.Tables[0].Rows[0].Firstname + " " + ds.Tables[0].Rows[0].Lastname+ ") ");
      var dtDetails =  engFriends.GetUserDetailsByUserID(ds.Tables[0].Rows[0].UserID);	
        if (dtDetails.value.Rows[0].status != 'Pupil') {
         var linkAsParent =  document.getElementById('linkAsParent');
            linkAsParent.style.display = 'inline';
        }  	
	  
	}
	else {
		var emails = '';
		for(var a = 0; a < ds.Tables.length; a++) {
			var dt = ds.Tables[a];
				if (dt !=null)
					{
						emails += dt.Rows[0].Email + ",";
					}		
		}
		//this is checked in the autocomplete.js file to pass the varibales for the search
		setSpanValue('emailsIDs',emails);
		document.getElementById('multipleEmails').style.display = 'block';
		document.getElementById('editMyFriends').style.display = 'block';
		document.getElementById('oneEmail').style.display = 'none';
	}
	document.getElementById('addFriendSlide').style.display = 'none';
	document.getElementById('addRegSlide').style.display = 'block';
	
	if (ds.Tables.length > 1) {
		beginAutoCompleteAll('txtEditMyFriends',0);
	}
}



hideAddRegFriend = function()
{
	//closeModalWindow('addRegUser');
	cleanupFeedback('addRegUserActionContainer');
	backToParentPanel('addFriendSlide','addRegSlide');

	window.setTimeout("closeModalWindow('addFriend');",750);
	window.setTimeout("document.location.reload();", 1600);
}

function deleteMyFriend(accID)
{
	var ul = document.getElementById("ulEditMyFriendsList");
	var chks = document.getElementById("ulEditMyFriendsList").getElementsByTagName("input");
	var lis = document.getElementById("ulEditMyFriendsList").getElementsByTagName("li");
	for(var i=0;i<chks.length;i++)
		{
				if(accID == chks[i].value)
				{
					var li = document.getElementById(lis[i].id);
					ul.removeChild(li);
				}
		}	
	
	
}

function callback_AutoCompleteEditMyFriends(res)
{
	checkResponse(res.value);
	if (res.value != null)
	{
		var ds = res.value;
		var listControlID = "autoCompleteListtxtEditMyFriends";
		var listControlWrapper = "txtEditMyFriendsListWrapper";
		document.getElementById(listControlID).innerHTML = "";
		
		var ul = document.createElement("ul");
		ul.id = "autoList";
		
		if (ds != null && typeof(ds) == "object" && ds.Tables.length != 0)
		{
			for (var i=0; i<ds.Tables.length; i++)
			{
				var li = document.createElement("li");
				var a = document.createElement("a");
			
				a.href = "javascript:completeEditUsername('" + ds.Tables[i].Rows[0].UserID + "','" +  ds.Tables[i].Rows[0].Username + "("+ ds.Tables[i].Rows[0].Firstname + " " + ds.Tables[i].Rows[0].Lastname+ ") ','" + ds.Tables[i].Rows[0].encUserID +"',"+ds.Tables[i].Rows[0].UserID +");void(0);";
				
				if (ds.Tables[i].Rows[0].status == "Pupil")  {
					a.innerHTML =  ds.Tables[i].Rows[0].Username + "("+ ds.Tables[i].Rows[0].Firstname + " " + ds.Tables[i].Rows[0].Lastname+ ") " +ds.Tables[i].Rows[0].School + ", " + ds.Tables[i].Rows[0].Grade;
				}
				else {
					a.innerHTML =  ds.Tables[i].Rows[0].Username + "("+ ds.Tables[i].Rows[0].Firstname + " " + ds.Tables[i].Rows[0].Lastname+ ") " +ds.Tables[i].Rows[0].School;
		
				}
				
				li.style.width = "25em";
				li.appendChild(a);
				ul.appendChild(li)
			}				
			document.getElementById(listControlID).appendChild(ul);
			document.getElementById(listControlWrapper).style.display = "block";
			document.getElementById(listControlID).style.display = "block";
		}
		else
		{
			document.getElementById(listControlWrapper).style.display = "none";
			document.getElementById(listControlID).style.display = "none";
		}		
	}
}


function completeEditUsername(accID, userDetails , encUserID,UserID)
{
	  document.getElementById('oneEmail').style.display = 'block';
	  setSpanValue('addRegFriendID', encUserID);
	  setSpanValue('addRegFriendEmail', userDetails); 
	  
	  
	  var dtDetails =  engFriends.GetUserDetailsByUserID(UserID);	  	 
	  
	  var x = -1;
	  x = engFriends.isFriendRequestPending(encUserID); 
	  if ( x.value == 0  ) {
	        alert('There is already a friend request pending between the two of you.');
	  }
	  
	  if (dtDetails.value.Rows[0].status != 'Pupil') {
	    var linkAsParent =  document.getElementById('linkAsParent');
	    linkAsParent.style.display = 'inline';
	  }  
	  else {
	    var linkAsParent =  document.getElementById('linkAsParent');
	    linkAsParent.style.display = 'none';	  
	  }
}


showRemoveFriend = function(itemID)
{
	setSpanValue('removeFriendID', itemID);
	engUsersStandard_dev.getUserInfoForId(itemID, callback_showRemoveFriend);
}

callback_showRemoveFriend = function(res)
{
	checkResponse(res.value);
	var dt = res.value;
	setSpanValue('friendName', dt.Rows[0].Username);
	openModalWindow('removeFriend');
}

removeFriend = function()
{
	var friendID = getSpanValue('removeFriendID');
	showFeedback('removeFriendActionContainer');
	engFriends.removeFriendByUserID(friendID, callback_removeFriend);
}

callback_removeFriend = function(res)
{
	checkResponse(res.value);
	if (res.value)
	{
		feedbackValid('Removing','Removed');
		window.setTimeout("hideRemoveFriend();", 4000);
	}
	else
	{
		feedbackInvalid('removeFriendActionContainer');
	}
}

hideRemoveFriend = function()
{
	//loadfriends();
	window.setTimeout("document.location.reload();", 1600);
	closeModalWindow('removeFriend');
	cleanupFeedback('removeFriendActionContainer');
}

hideAddFriend = function()
{
	backToParentPanel('addFriendSlide','addRegSlide');
	closeModalWindow('addFriend');
}

function closeAllChildModals(parentControl){
	var children = document.getElementsByClassName('childModal');
	for (var i = 0;i<children.length;i++)
		{
		children[i].style.display = "none";
		}
	document.getElementById(parentControl).style.display = "block";
			
			new Effect.Morph(parentControl,
			{
   				style: 'opacity:1;' +
     				   'filter:alpha(opacity=100);',
   				duration: 0.3
   			});
}

function backToParentPanel(parentControl, controlToFade)
{
    var linkAsParent =  document.getElementById('linkAsParent');
    linkAsParent.style.display = 'none';
	document.getElementById('addFriendSlide').style.display = 'block';
	document.getElementById('addRegSlide').style.display = 'none';
	cleanupFeedback('addFriendActionContainer');
	/*new Effect.Morph(controlToFade,
	{
   		style: 'opacity:0;' +
     		   'filter:alpha(opacity=0);',
   		duration: 0.3,
   		afterFinish:function()
		{
			document.getElementById(controlToFade).style.display = "none";
			document.getElementById(parentControl).style.display = "block";
			
			new Effect.Morph(parentControl,
			{
   				style: 'opacity:1;' +
     				   'filter:alpha(opacity=100);',
   				duration: 0.3,
   				afterFinish:function()
   				{
   					
   					switch (CurrentModalWindow)
   						{
   						case "addFriend":
   						//	var catID = getSpanValue('currentCatID');
   						//	showCatTrans(catID);
   						//	loadBudgetDetails();
						//	loadCats();
   						break
   						}	
   				}
			});
		}
	});*/
}

function showChildPanel(parentControl, childControl)
{
	new Effect.Morph(parentControl,
	{
   		style: 'opacity:0;' +
     		   'filter:alpha(opacity=0);',
   		duration: 0.3,
   		afterFinish:function()
		{
			document.getElementById(parentControl).style.display = "none";
			document.getElementById(childControl).style.display = "block";
			
			new Effect.Morph(childControl,
			{
   				style: 'opacity:1;' +
     				   'filter:alpha(opacity=100);',
   				duration: 0.3
			});
		}
	});
}
