loadTopics = function()
{
	engBlogTopics.getTopics(callback_loadTopics);
	engAlbum.getAlbumsForDropDown(loadAlbums);
	engAlbum.getAlbumsForDropDown(loadEditAlbums);	
}

callback_loadTopics = function(res)
{
	checkResponse(res.value);
	
	var dt = res.value;
	var tbl = document.getElementById("tblTopics");
	var rows = tbl.rows; 
    while(rows.length) // length=0 -> stop 
	{
		tbl.deleteRow(rows.length-1);
	}
	if (dt.Rows.length!=0)
	{
		for (var i=0; i<dt.Rows.length; i++)
		{
			var insertTable = document.createElement("tr");
			tbl.appendChild(insertTable);
							
			var tblCell1 = insertTable.insertCell(0)
		//	var tblCell5 = insertTable.insertCell(1)
			var tblCell2 = insertTable.insertCell(1)
			var tblCell3 = insertTable.insertCell(2)
			var tblCell4 = insertTable.insertCell(3)
			
			
			
			tblCell1.style.width="70%";
			//tblCell5.style.width="40%";
			tblCell2.style.width="10%";
			tblCell3.style.width="10%";
			tblCell4.style.width="10%";			
				
			var aTopic = document.createElement("a");
			aTopic.innerHTML = dt.Rows[i].Title;
			aTopic.href = "javascript:goToTopic(" + dt.Rows[i].topic_id + ");";
			
			tblCell1.appendChild(aTopic);
			
			if ((dt.Rows[i].Album_Title == null)||(dt.Rows[i].Album_ID == 0))
			{
		
			}
			else
			{
				var aAlbum = document.createElement("a");
				aAlbum.href = "javascript:goToAlbumFromBlog(" + dt.Rows[i].Album_ID + ");";
				aAlbum.innerHTML = dt.Rows[i].Album_Title;
				aAlbum.className = "photoAlbum";
				
				
				tblCell1.appendChild(aAlbum);
					
			}
			
			var spnPosts = document.createElement("span");
			spnPosts.className = "commentsSynopsis";
			spnPosts.innerHTML = dt.Rows[i].PostCount + " Post(s)";
			
			tblCell1.appendChild(spnPosts);
			
			
			var dtPosts = engBlogTopics.getPostsForTopic(dt.Rows[i].topic_id)
			if (dtPosts != null) {
				if (dtPosts.value.Rows.length > 0) {
					for (var a =0; a < (dtPosts.value.Rows.length ); a++) {
						var aPosts = document.createElement("a");
						aPosts.href = "javascript:goToPost("+dtPosts.value.Rows[a].Post_ID+");";
						aPosts.innerHTML = dtPosts.value.Rows[a].Post_Title;
						aPosts.className="blogPosts";
						tblCell1.appendChild(aPosts);
						var dtComments = engBlogTopics.getCommentsForPosts(dtPosts.value.Rows[a].Post_ID)
						var aComment = document.createElement("a");
						if (dtComments != null) {
							if (dtComments.value.Rows.length > 0) {
								
								if (dtComments.value.Rows.length == 1)
									aComment.innerHTML = dtComments.value.Rows.length + " comment";
								else
									aComment.innerHTML = dtComments.value.Rows.length + " comments";
								
								aComment.href = "javascript:goToPostComments(" + dtComments.value.Rows[0].Post_ID + ");";				
														
							}
						}
						aComment.className="blogComments";
						tblCell1.appendChild(aComment);
					}
				}
			}	
				
			var spnReadMore = document.createElement("a");
			spnReadMore.href = "javascript:goToTopic(" + dt.Rows[i].topic_id + ");";					
			spnReadMore.innerHTML = "Read More";
			tblCell1.appendChild(spnReadMore);
			
					/*var photodiv= document.createElement("div");
					photodiv.id="thumbs";
					photodiv.innerHTML="&nbsp;"
					var photos = engBlogTopics.getPhotosForTopicAlbum(dt.Rows[i].Album_ID)				
					
					if (photos !=null)
					{
						if (photos.length != 0)
						{						
							for (var j=0; j<photos.value.Rows.length; j++)
							{		
								var img = document.createElement("img");
								
								img.src = photos.value.Rows[j].Source;							
								img.style.border="1px solid #ffffff";											
								
								var aImg = document.createElement("a");
								aImg.href = "javascript:goToPhoto(" + photos.value.Rows[j].ID + ");";							
								aImg.appendChild(img);
								
								img.onmouseover=function(){ this.style.border='1px solid #a7a6a3'; };
								img.onmouseout=function(){ this.style.border='1px solid #ffffff'; };
								
								aImg.appendChild(img);
								photodiv.appendChild(aImg);
								
								var spnSpace = document.createElement("span");
								spnSpace.innerHTML = "&nbsp;&nbsp;";
								photodiv.appendChild(spnSpace)
							}						
						}					
					}			
					tblCell5.appendChild(photodiv);	
								*/
			
			
			
			tblCell2.className = "dateCreated";
			tblCell2.innerHTML = dt.Rows[i].Topic_Short_Date;
			
			tblCell3.className = "dateCreated";
		//	if (dt.Rows[i].Description == "Group Access")
		//	{
		//		var aGroup = document.createElement("a");
		//		aGroup.innerHTML = dt.Rows[i].Description;
		//		aGroup.href = "javascript:loadGroups(" + dt.Rows[i].topic_id + ");";
		//		tblCell3.appendChild(aGroup);
		//	}
		//	else
		//	{
				tblCell3.innerHTML = dt.Rows[i].Description;
		//	}
			var aEdit = document.createElement("a");
			aEdit.innerHTML = "Edit";
			aEdit.href = "javascript:showEditTopic(" + dt.Rows[i].topic_id + ");";
			
			var liEdit = document.createElement("li");
			liEdit.className = "edit";
			
			liEdit.appendChild(aEdit);
			
			var aDelete = document.createElement("a");
			aDelete.innerHTML = "Delete";
			aDelete.href = "javascript:showRemoveTopic(" + dt.Rows[i].topic_id + ");";
			
			var liDelete = document.createElement("li");
			liDelete.className = "delete";
			
			liDelete.appendChild(aDelete);
			
			var ul = document.createElement("ul");
			
			ul.appendChild(liEdit);
			ul.appendChild(liDelete);
			if (i==0)
				{
				tblCell4.className = "options firstOptions";	
				//tblCell1.className = "first";	
				}
			else
				{
				tblCell4.className = "options";
				}
			
			
			tblCell4.appendChild(ul);
			}
		}
		else
		{
			var insertTable = document.createElement("tr");
			tbl.appendChild(insertTable);
									
			var tblCell1 = insertTable.insertCell(0)
			tblCell1.colSpan = "4";
			tblCell1.id = "cell1";
			tblCell1.innerHTML = "You have not created any blog categories yet";
		}
		stripetableBody();
}

loadAlbums = function(res)
{
	checkResponse(res.value);
	var control = document.getElementById("ddAlbum");
	control.innerHTML = "";
	var dt = res.value;
	var def = document.createElement("option");
	def.innerHTML = "No Album";
	def.value = "0";
	
	control.appendChild(def);
	
	for (var i=0; i<dt.Rows.length; i++)
	{
		var opt = document.createElement("option");
		opt.innerHTML = dt.Rows[i].Album_Title;
		opt.value = dt.Rows[i].Album_ID;
		control.appendChild(opt)
	}
										
	
}

loadEditAlbums = function(res)
{
	checkResponse(res.value);
	var control = document.getElementById("ddEditAlbum");
	control.innerHTML = "";
	var dt = res.value;
	
	var def = document.createElement("option");
	def.innerHTML = "No Album";
	def.value = "0";
	
	control.appendChild(def);
	
	for (var i=0; i<dt.Rows.length; i++)
	{
	var opt = document.createElement("option");
	opt.innerHTML = dt.Rows[i].Album_Title;
	opt.value = dt.Rows[i].Album_ID;
	
	control.appendChild(opt)
	}
	
	
		
	
}

function getGroupsIDs(controlID){
	var chks = document.getElementById(controlID).getElementsByTagName("input");
	var idList = new Array();
	var counter = 0;
	for (var i=0; i<chks.length; i++)
		{
		if (chks[i].type == "checkbox")
			{
				if (chks[i].checked)
					{
					idList[counter] = chks[i].value;
					counter = counter + 1;	
					}		
				
				
			}
		}
	return idList;
}


addNewTopic = function()
{	

	var Description = getControlValue('txtDescription');
	var Security = getSelectedRadioValue('radSecurity');
	var Album = getDropDownSelected('ddAlbum');
	
	var friendsIDs;

	friendsIDs = getFriendsIDs("ulFriends");


	if(friendsIDs[0] == void 0)
		{
			friendsIDs[0] = 0;
		}
	
	var goupsIDs;
	goupsIDs = getGroupsIDs("ulGroupsList");

	if(goupsIDs[0] == void 0)
		{
			goupsIDs[0] = 0;
		}
	
	showFeedback('addTopicActionContainer');
	
	
	engBlogTopics.addBlogTopic('txtDescription', Description, Security, Album, friendsIDs, goupsIDs, callback_addNewTopic);
}

callback_addNewTopic = function(res)
{
	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++)
		{
			clearInputField('txtDescription');
			setControlErrorMessage(controls.Rows[i].controlID,'');
		}
		switch (results.Rows[0].errMessage)
		{
			case "valid":
			
			feedbackValid('Saving','Saved');
			//goToTopic(TopicID);
			window.setTimeout('hideAddTopic();', 4000);
			
			break
		}
	}								
	else
	{
		var controls = res.value.Tables[1];
		switch (results.Rows[0].errMessage)
		{
			case "invalid":
			feedbackInvalid('addTopicActionContainer');
			window.setTimeout(function(){processValidationResponse(controls)}, 2001);
			break
		}
		}
}



hideAddTopic = function()
{
	//loadTopics();
	//goToTopic(TopicID);
	window.location = "topic.aspx";
	closeModalWindow('addTopic');
	cleanupFeedback('addTopicActionContainer');
	
}

var currentItem;

showEditTopic = function(itemID)
{
	//editTopicActionContainer
	setSpanValue('editTopicID',itemID);
	currentItem = itemID
	engBlogTopics.populateTopicObject(itemID,callback_populateTopic);
}

callback_populateTopic = function(res)
{
	if (res.error!=null){alert(res.error)};
	
	checkResponse(res.value);
	
	var topic = res.value;
	
	setControlValue('txtEditDescription', topic.Title);
	setSelectedRadioValue('radEditSecurity', topic.SecurityID);
	
	
	setDropDownSelected('ddEditAlbum', topic.AlbumID);
	var ul = document.getElementById("ulEditFriendsList");	
	ul.innerHTML = "";	
	
	switch (topic.SecurityID)
		{
			case 0:
				showEditPublicPrivate();
			break
			case 1:
			
				showEditPublicPrivate();
			break
			case 4:
				if (topic.users!=null)
					{
						for (var j = 0;j<topic.users.length;j++)
							{
							var chk = document.createElement("input");
							chk.type = "checkbox";
							chk.value = topic.users[j].UserID;
							chk.id = "c_"+topic.users[j].UserID;
							chk.style.display = "none"; 
							
							var lbl = document.createElement("label");
							lbl.innerHTML = topic.users[j].Username;
							lbl.id = "l_"+topic.users[j].UserID;
							
							var img = document.createElement("img");
							img.id = "i_"+topic.users[j].UserID;
							img.alt ="Remove";
							img.src="/images/icons/delete.gif";
							
							var aImg = document.createElement("a");
							aImg.href ="javascript:deleteEditFriend(" + topic.users[j].UserID + ");";
							aImg.id = "a_"+topic.users[j].UserID;
							aImg.appendChild(img);
							
							var li = document.createElement("li");
							li.id="l_"+topic.users[j].UserID;
							
							li.appendChild(chk);
							li.appendChild(lbl);
							li.appendChild(aImg);	
									
							ul.appendChild(li);
							}
					
					}
		
				showEditFriendsOptions();
			break
			case 2:
				var chks = document.getElementById("ulEditGroupsList").getElementsByTagName("input");
				
				for (var i = 0;i<chks.length;i++)
					{

							
					chks[i].checked = false;
								

					}
					
				for (var i = 0;i<chks.length;i++)
					{
					for (var j = 0;j<topic.groups.length;j++)
						{
							if (chks[i].value == topic.groups[j].GroupID)
								{
									chks[i].checked = true;
								}
						}
					}
					
				showEditGroupsOptions();
			break
		}
		
	openModalWindow('editTopic');
}


function refreshEditUsersOptions() {
  engBlogTopics.PopulateTopicSharedUsers(currentItem, callback_refreshEditUsersOptions);
}

function callback_refreshEditUsersOptions(res) {
	if (res.error!=null){alert(res.error)};

	var topic = res.value;
	var ul = document.getElementById("ulEditFriendsList");	
	ul.innerHTML = "";
				if (topic.users!=null)
					{
						for (var j = 0;j<topic.users.length;j++)
							{
							var chk = document.createElement("input");
							chk.type = "checkbox";
							chk.value = topic.users[j].UserID;
							chk.id = "c_"+topic.users[j].UserID;
							chk.style.display = "none"; 
							
							var lbl = document.createElement("label");
							lbl.innerHTML = topic.users[j].Username;
							lbl.id = "l_"+topic.users[j].UserID;
							
							var img = document.createElement("img");
							img.id = "i_"+topic.users[j].UserID;
							img.alt ="Remove";
							img.src="/images/icons/delete.gif";
							
							var aImg = document.createElement("a");
							aImg.href ="javascript:deleteEditFriend(" + topic.users[j].UserID + ");";
							aImg.id = "a_"+topic.users[j].UserID;
							aImg.appendChild(img);
							
							var li = document.createElement("li");
							li.id="l_"+topic.users[j].UserID;
							
							li.appendChild(chk);
							li.appendChild(lbl);
							li.appendChild(aImg);	
									
							ul.appendChild(li);
							}
					
					}	
	showEditFriendsOptions();
}

function refreshEditGroupsOptions() {
  engBlogTopics.populateTopicGroups(currentItem, callback_refreshEditGroupsOptions);
}

function callback_refreshEditGroupsOptions(res) {
	if (res.error!=null){alert(res.error)};

	var topic = res.value;
	var ul = document.getElementById("ulEditFriendsList");	
	ul.innerHTML = "";

				var chks = document.getElementById("ulEditGroupsList").getElementsByTagName("input");
				
				for (var i = 0;i<chks.length;i++)
					{

							
					chks[i].checked = false;
								

					}
				if (topic.groups != null) {		
					for (var i = 0;i<chks.length;i++)
						{
						for (var j = 0;j<topic.groups.length;j++)
							{
								if (chks[i].value == topic.groups[j].GroupID)
									{
										chks[i].checked = true;
									}
							}
						}
				}	
				showEditGroupsOptions();					
	showEditGroupsOptions();
}


editTopic = function()
{
	var TopicID = getSpanValue('editTopicID');
	var Description = getControlValue('txtEditDescription');
	var Security = getSelectedRadioValue('radEditSecurity');
	var Album = getDropDownSelected('ddEditAlbum');
	
	var friendsIDs;

	friendsIDs = getFriendsIDs("ulEditFriendsList");


	if(friendsIDs[0] == void 0)
		{
			friendsIDs[0] = 0;
		}
		
	var goupsIDs;
	goupsIDs = getGroupsIDs("ulEditGroupsList");

	if(goupsIDs[0] == void 0)
		{
			goupsIDs[0] = 0;
		}
		
	showFeedback('editTopicActionContainer');
	
	engBlogTopics.editBlogTopic(TopicID, 'txtEditDescription', Description, Security, Album, friendsIDs, goupsIDs, callback_editTopic);
}

callback_editTopic = function(res)
{
	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++)
			{
			clearInputField('txtEditDescription');
			setControlErrorMessage(controls.Rows[i].controlID,'');
			}
			switch (results.Rows[0].errMessage)
				{
					case "valid":
						
						feedbackValid('Saving','Saved');
						window.setTimeout('hideEditTopic();', 4000);
					break
				}
		}
	else
		{
		var controls = res.value.Tables[1];
		switch (results.Rows[0].errMessage)
				{
					case "invalid":
						feedbackInvalid('editTopicActionContainer');
						window.setTimeout(function(){processValidationResponse(controls)}, 2001);
					break
				}
		}
}

hideEditTopic = function()
{
	loadTopics();
	closeModalWindow('editTopic');
	cleanupFeedback('editTopicActionContainer');
}

showRemoveTopic = function(itemID)
{
	setSpanValue('removeTopicID', itemID);
	engBlogTopics.populateTopic(itemID, callback_populateRemoveTopic);
}

callback_populateRemoveTopic = function(res)
{
	checkResponse(res.value);
	
var dt = res.value;
	setSpanValue('removeTopicName', dt.Rows[0].Title);
	setSpanValue('postCount', dt.Rows[0].Post_Count);
	openModalWindow('deleteTopic');
}

removeTopic = function()
{
	var topicID = getSpanValue('removeTopicID');
	showFeedback('deleteTopicActionContainer');
	engBlogTopics.deleteTopic(topicID, callback_removeTopic);
}

callback_removeTopic = function(res)
{
	if (res.error!=null){alert(res.error)};
	if (res.value)
		{
		feedbackValid('Deleting','Deleted');
		window.setTimeout("hideRemoveTopic();", 4000);
		}
	else
		{
		feedbackInvalid('deleteTopicActionContainer');
		}
}

hideRemoveTopic = function()
{
	engBlogTopics.getTopics(callback_loadTopics);
	closeModalWindow('deleteTopic');
	cleanupFeedback('deleteTopicActionContainer');
}

goToTopic = function(itemID)
{
	engBlogTopics.goToTopicPosts(itemID, callback_goToTopic);
}

callback_goToTopic = function(res)
{
	if (res.value == "invalid")
	{
		location = "/login/index.aspx";
	}
	window.location = "topic.aspx";
}

goToAlbumFromBlog = function(itemID)
{
	engAlbum.goToAlbumPhotos(itemID, callback_goToAlbumPhotos);
}

callback_goToAlbumPhotos= function(res)
{
	if (res.value == "invalid")
	{
		location = "/login/index.aspx";
	}
	window.location = "/myPhotos/photoAlbum.aspx";
}

loadGroups = function(itemID)
{
	setSpanValue('grpTopicID', itemID);
	openModalWindow('groups');
	engBlogTopics.getGroupsForTopicID(itemID, callback_loadGroupsForTopic);
	engBlogTopics.getGroupsNotForTopicID(itemID, callback_loadGroupsNotForTopic);
}

callback_loadGroupsForTopic = function(res)
{
	var dt = res.value;
	
	var ul = document.getElementById("ulTopicInGroups");
	ul.innerHTML = "";
	
	if (dt.Rows.length!=0)
	{
		for (var i=0; i<dt.Rows.length; i++)
		{
			var a = document.createElement("a");
			a.innerHTML = dt.Rows[i].Description;
			a.href = "#";
			
			var span = document.createElement("span");
			span.appendChild(a);
			
			var aRemove = document.createElement("a");
			aRemove.href = "javascript:removeTopicFromGroup(" + dt.Rows[i].BIG_ID + ");";
			aRemove.innerHTML = "Remove";
			aRemove.className = "remove";
			aRemove.title = "Remove this topic from GroupName."
			
			
			var li = document.createElement("li");
			li.className = "clearfix";
	
			li.appendChild(span);
			li.appendChild(aRemove);
			
			ul.appendChild(li);
		}		
	}
	else
	{
		var li = document.createElement("li");
		li.innerHTML = "Not groups have access";
		
		ul.appendChild(li);
	}
}

callback_loadGroupsNotForTopic = function(res)
{
	var dt = res.value;	
	var ul = document.getElementById("ulTopicNotInGroups");
	ul.innerHTML = "";
	
	if (dt.Rows.length!=0)
	{
		for (var i=0; i<dt.Rows.length; i++)
		{
			var a = document.createElement("a");
			a.innerHTML = dt.Rows[i].Description;
			a.href = "#";
			
			var span = document.createElement("span");
			span.appendChild(a);
			
			var aAdd = document.createElement("a");
			aAdd.href = "javascript:addTopicToGroup(" + dt.Rows[i].Group_ID + ");";
			aAdd.innerHTML = "Add";
			aAdd.className = "add";
			aAdd.title = "Add this topic to GroupName."
			
			var li = document.createElement("li");
			li.className = "clearfix";
	
			li.appendChild(span);
			li.appendChild(aAdd);
			
			ul.appendChild(li);
		}
	}
	else
	{
		var li = document.createElement("li");
		li.innerHTML = "All groups have access";	
		ul.appendChild(li);
	}
}

removeTopicFromGroup = function(itemID)
{
	engBlogTopics.unAssignTopicFromGroup(itemID, callback_reloadGroups)
}

addTopicToGroup = function(itemID)
{
	var topicID = getSpanValue('grpTopicID');	
	engBlogTopics.assignTopicToGroup(itemID, topicID, callback_reloadGroups)
}

callback_reloadGroups = function(res)
{
	var itemID = getSpanValue('grpTopicID');
	if (res.value)
	{
		engBlogTopics.getGroupsForTopicID(itemID, callback_loadGroupsForTopic);
		engBlogTopics.getGroupsNotForTopicID(itemID, callback_loadGroupsNotForTopic);
	}
}

//share with friends

function showFriendsOptions(){
	document.getElementById("addEventFriends").style.display = "block";
	document.getElementById("addGroups").style.display = "none";
}

function hideFriendsOptions(){
	document.getElementById("addEventFriends").style.display = "none";
}

function showGroupsOptions(){
	document.getElementById("addEventFriends").style.display = "none";
	document.getElementById("addGroups").style.display = "block";
		
}

function showPublicPrivate(){
	document.getElementById("addEventFriends").style.display = "none";
	document.getElementById("addGroups").style.display = "none";
		
}

function showEditFriendsOptions(){
	document.getElementById("editFriendContainer").style.display = "block";
	document.getElementById("editGroups").style.display = "none";
}

function hideEditFriendOptions(){
	document.getElementById("editFriendContainer").style.display = "none";
}

function showEditGroupsOptions(){
	document.getElementById("editFriendContainer").style.display = "none";
	document.getElementById("editGroups").style.display = "block";
		
}

function showEditPublicPrivate(){
	document.getElementById("editFriendContainer").style.display = "none";
	document.getElementById("editGroups").style.display = "none";
		
}

function callback_AutoCompleteFriends(res){

checkResponse(res.value);

	if (res.value != null)
		{
		var ds = res.value;
		var listControlID = "autoCompleteListtxtFriends";
		var listControlWrapper = "txtFriendsListWrapper";
		document.getElementById(listControlID).innerHTML = "";
		
		var ul = document.createElement("ul");
		ul.id = "autoList";
		ul.setAttribute("onMouseOut","this.style.display='none;");
		if (ds != null && typeof(ds) == "object" && ds.Rows.length != 0)
			{
			
			for (var i=0; i<ds.Rows.length; i++)
				{
				var li = document.createElement("li");
				var a = document.createElement("a");
			
				a.href = "javascript:completeFriends('" + ds.Rows[i].UserID + "','" + ds.Rows[i].Username + " ("+ds.Rows[i].firstname + " " + ds.Rows[i].lastname+")"+ "');void(0);";
				a.innerHTML = ds.Rows[i].Username+ " ("+ds.Rows[i].firstname + " " + ds.Rows[i].lastname+")";
				
				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 completeFriends(accID, username)
{
	Effect.Fade('txtFriendsListWrapper');
	var ul = document.getElementById("ulFriendsList");	
	var chks = document.getElementById("ulFriendsList").getElementsByTagName("input");
	var counter = 0;
	if(chks.length != 0)
	{
		for(var i=0;i<chks.length;i++)
		{
				if(accID == chks[i].value)
				{
					counter = 1;
				}
		}			
			
		if(counter == 0)
		{
			var chk = document.createElement("input");
			chk.type = "checkbox";
			chk.value = accID;
			chk.id = "c_"+accID;
			chk.style.display = "none"; 
			var lbl = document.createElement("label");
			lbl.innerHTML = username;
			lbl.id = "l_"+accID;
			var img = document.createElement("img");
			img.id = "i_"+accID;
			img.alt ="Remove";
			img.src="/images/icons/delete.gif";
			var aImg = document.createElement("a");
			aImg.href ="javascript:deleteFriend(" + accID + ");";
			aImg.id = "a_"+accID;
			aImg.appendChild(img);
			var li = document.createElement("li");
			li.id="l_"+accID;
			li.appendChild(chk);
			li.appendChild(lbl);
			li.appendChild(aImg);			
			ul.appendChild(li);
		}
		else
		{
			alert('You have already selected to share the event with this person.');
		}
		
	}
	else
	{
		var chk = document.createElement("input");
		chk.type = "checkbox";
		chk.value = accID;
		chk.id = "c_"+accID;
		chk.style.display = "none"; 
		var lbl = document.createElement("label");
		lbl.innerHTML = username;
		lbl.id = "l_"+accID;
		var img = document.createElement("img");
		img.id = "i_"+accID;
		img.alt ="Remove";
		img.src="/images/icons/delete.gif";
		var aImg = document.createElement("a");
		aImg.href ="javascript:deleteFriend(" + accID + ");";
		aImg.id = "a_"+ accID;
		aImg.appendChild(img);
		var li = document.createElement("li");
		li.id="l_"+accID;
		li.appendChild(chk);
		li.appendChild(lbl);
		li.appendChild(aImg);			
		ul.appendChild(li);
	}
	document.getElementById("txtFriends").value = "";
}

function deleteFriend(accID)
{
	var ul = document.getElementById("ulFriendsList");
	var chks = document.getElementById("ulFriendsList").getElementsByTagName("input");
	var lis = document.getElementById("ulFriendsList").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_AutoCompleteeditFriends(res){

checkResponse(res.value);

	if (res.value != null)
		{
		var ds = res.value;
		var listControlID = "autoCompleteListtxtEditFriends";
		var listControlWrapper = "txtEditFriendsListWrapper";	
		document.getElementById(listControlID).innerHTML = "";
		
		var ul = document.createElement("ul");
		ul.id = "autoList";
		
		if (ds != null && typeof(ds) == "object" && ds.Rows.length != 0)
			{
			
			for (var i=0; i<ds.Rows.length; i++)
				{
				var li = document.createElement("li");
				var a = document.createElement("a");
			
				a.href = "javascript:completeEditUsername('" + ds.Rows[i].UserID + "','" + ds.Rows[i].Username + " ("+ds.Rows[i].firstname + " " + ds.Rows[i].lastname+")" + "');void(0);";
				a.innerHTML = ds.Rows[i].Username + " ("+ds.Rows[i].firstname + " " + ds.Rows[i].lastname+")";
				
				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, username)
{
	Effect.Fade('txtEditFriendsListWrapper');
	var ul = document.getElementById("ulEditFriendsList");	
	var chks = document.getElementById("ulEditFriendsList").getElementsByTagName("input");
	var counter = 0;
	if(chks.length != 0)
	{
		for(var i=0;i<chks.length;i++)
		{
				if(accID == chks[i].value)
				{
					counter = 1;
				}
		}			
			
		if(counter == 0)
		{
			var chk = document.createElement("input");
			chk.type = "checkbox";
			chk.value = accID;
			chk.id = "c_"+accID;
			chk.style.display = "none"; 
			var lbl = document.createElement("label");
			lbl.innerHTML = username;
			lbl.id = "l_"+accID;
			var img = document.createElement("img");
			img.id = "i_"+accID;
			img.alt ="Remove";
			img.src="/images/icons/delete.gif";
			var aImg = document.createElement("a");
			aImg.href ="javascript:deleteEditFriend(" + accID + ");";
			aImg.id = "a_"+accID;
			aImg.appendChild(img);
			var li = document.createElement("li");
			li.id="l_"+accID;
			li.appendChild(chk);
			li.appendChild(lbl);
			li.appendChild(aImg);			
			ul.appendChild(li);
		}
		else
		{
			alert('You have already selected to share the event with this person.');
		}
		
	}
	else
	{
		var chk = document.createElement("input");
		chk.type = "checkbox";
		chk.value = accID;
		chk.id = "c_"+accID;
		chk.style.display = "none"; 
		var spn = document.createElement("span")
		spn.innerHTML = accID;
		
		var lbl = document.createElement("label");
		lbl.innerHTML = username;
		lbl.id = "l_"+accID;
		var img = document.createElement("img");
		img.id = "i_"+accID;
		img.alt ="Remove";
		img.src="/images/icons/delete.gif";
		var aImg = document.createElement("a");
		aImg.href ="javascript:deleteEditFriend(" + accID + ");";
		aImg.id = "a_"+ accID;
		aImg.appendChild(img);
		var li = document.createElement("li");
		li.id="l_"+accID;
		li.appendChild(chk);
		//li.appendChild(spn);
		li.appendChild(lbl);
		li.appendChild(aImg);			
		ul.appendChild(li);
	}
	document.getElementById("txtEditFriends").value = "";
}


function deleteEditFriend(accID)
{
	var ul = document.getElementById("ulEditFriendsList");
	var chks = document.getElementById("ulEditFriendsList").getElementsByTagName("input");
	var lis = document.getElementById("ulEditFriendsList").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 getFriendsIDs(controlID){
	var chks = document.getElementById(controlID).getElementsByTagName("input");
	var idList = new Array();
	var counter = 0;
	for (var i=0; i<chks.length; i++)
		{
		if (chks[i].type == "checkbox")
			{
						
				idList[counter] = chks[i].value;
				counter = counter + 1;
				
			}
		}
	return idList;
}

function getFriendsCheckedIDs(controlID){
	var chks = document.getElementById(controlID).getElementsByTagName("input");
	var idList = new Array();
	var counter = 0;
	for(var i=0; i<chks.length; i++)
		{
			if(chks[i].type == "checkbox")
				{
					if(chks[i].checked)
					{
						idList[counter] = chks[i].value;
						counter = counter + 1;
					}
				}
		}
		return idList;
}
		