function loadPages(){
	loadPage(1);
}

function loadPage(pageNum)
{
//alert(pageNum);
	//showProcessor('tblInbox');
	setSpanValue('currPageNum', pageNum);
	window.setTimeout("engTextInbox.getInboxPage(" + pageNum + ", callback_loadPage);", 1000);
}


function createPageControls(row){
	var dt = row;
	var ul = document.getElementById("pages");
	ul.innerHTML = "";
	if (dt.Pages == 1)
	{
		return;
	}	
	
	var pages = dt.Pages;
	var currP = getSpanValue('currPageNum');
	
	var imgPrev = document.createElement("img");
	imgPrev.src = "/images/arrow_medium_left.gif";
	imgPrev.alt = "Previous";
	
	var aPrev = document.createElement("a");
	aPrev.href = "javascript:loadPage(" + (parseInt(currP) - 1) + ");";
	aPrev.appendChild(imgPrev);
	
	var liPrev = document.createElement("li");
	liPrev.appendChild(aPrev);
	liPrev.className = "previous";
	
	var imgNext = document.createElement("img");
	imgNext.src = "/images/arrow_medium_right.gif";
	imgNext.alt = "Next";
	
	var aNext = document.createElement("a");
	aNext.href = "javascript:loadPage(" + (parseInt(currP) + 1) + ");";
	aNext.appendChild(imgNext);
	
	var liNext = document.createElement("li");
	liNext.appendChild(aNext);
	liNext.className = "next";	
	
	if (pages > 1)
	{
		if (getSpanValue('currPageNum') != "1")
		{
			ul.appendChild(liPrev);
		}
	}
			
		//<img src="/images/arrow_medium_left.gif" alt="Previous" />
		
		
	for (var i=0; i<pages; i++)
	{		
		var a = document.createElement("a");	
		a.innerHTML = i + 1;		
		var li = document.createElement("li");
		if ((i + 1) == getSpanValue('currPageNum'))
		{
			li.className = "current";
			a.href = "#";
		}
		else
		{
			a.href = "javascript:loadPage(" + (i + 1) + ");";
		}
		li.appendChild(a);
		ul.appendChild(li);		
	}
	if (pages > 1)
	{
		if (getSpanValue('currPageNum') != pages)
		{
			ul.appendChild(liNext);
		}
	}
	
}

function callback_loadPage(res)
{
	checkResponse(res.value);
	var dt = res.value.Tables[0];
	var totRowCount = res.value.Tables[1].Rows[0].TotMessages
	//alert (res);
	if (totRowCount!=0)
	{
		createPageControls(dt.Rows[0]);
	}	
	
//window.setTimeout("hideProcessor('tblInbox');", 1000);

	var tbl = document.getElementById("tblInbox"); 
	var rows = tbl.rows; 
    while(rows.length) // length=0 -> stop 
	{
		tbl.deleteRow(rows.length-1);
	}
		
	if (dt.Rows.length!=0)
	{
			document.getElementById("currRows").innerHTML = dt.Rows[0].lower + "&nbsp;&#8211;&nbsp;" + dt.Rows[0].upper + "&nbsp;";
			document.getElementById("totalRows").innerHTML = dt.Rows[0].total;
			
			for (var i=0; i<dt.Rows.length; i++)
				{
				
					var insertTable = tbl.insertRow(i);
					
					//var tblCell1 = insertTable.insertCell(0)
					var tblCell2 = insertTable.insertCell(0)
					var tblCell3 = insertTable.insertCell(1)
					var tblCell4 = insertTable.insertCell(2)
					var tblCell5 = insertTable.insertCell(3)
					var tblCell6 = insertTable.insertCell(4)
					var tblCell7 = insertTable.insertCell(5)
					
					//tblCell1.id = "th_" + dt.Rows[i].Text_ID;
					//tblCell1.scope="row";
					//tblCell1.className = dt.Rows[i].messageStatus;
					//tblCell1.style.align = "center";
					
					
					var delChk = document.createElement("input")
					delChk.type = "checkbox";
					delChk.name = "batchDelete";
					delChk.id = "del_" + dt.Rows[i].Text_ID;
					delChk.value = dt.Rows[i].Text_ID;								
					
					if (dt.Rows[i].IsText == "request")
					{						
						tblCell2.innerHTML = "&nbsp;";
					}
					else
					{
						tblCell2.appendChild(delChk);	
					}
					
					tblCell2.align = "center";
					tblCell2.vAlign = "center";
					
					//tblCell2.className = "first";					
					
					tblCell3.id = "td_" + dt.Rows[i].Text_ID;
					tblCell3.className = "fromTo";
					if (dt.Rows[i].IsText == "yes")
					{
						tblCell3.innerHTML = "Text";
					}
					else
					{	
						var liAvatar = document.createElement("li");
						var liUsername = document.createElement("li");
						
						var profImg = document.createElement("img");
						profImg.src = dt.Rows[i].src;
						profImg.className = "Avatar";
						
						var aProfile = document.createElement("a");
						aProfile.innerHTML = "<strong>"+dt.Rows[i].Username+"</strong>";
						aProfile.style.color="#8ad1e1";
						aProfile.href = "/profile/index.aspx?x=" + dt.Rows[i].ProfileLink;						
						liAvatar.style.width="100px";
						liAvatar.appendChild(profImg);
						liUsername.appendChild(aProfile);
											
						var avatarUL = document.createElement("ul");
						avatarUL.appendChild(liAvatar);
						avatarUL.appendChild(liUsername);
						
						tblCell3.style.width="100px";
						tblCell3.appendChild(avatarUL);
					}
					
					tblCell4.id = "td_" + dt.Rows[i].Text_ID;
					tblCell4.className = "fromTo";
					
					if (dt.Rows[i].IsText == "yes")
					{
						tblCell4.innerHTML = "Text";
					}
					else
					{					
						var spnLong2 = document.createElement("span");
						spnLong2.innerHTML = dt.Rows[i].ToUsername;
						spnLong2.id = "spn2_" + dt.Rows[i].Text_ID;
					
						var divCont2 = document.createElement("div")
						divCont2.style.overflow = "auto";
						divCont2.style.height = "150px";
						divCont2.id = "divCont2_" + dt.Rows[i].Text_ID;
					
						divCont2.appendChild(spnLong2);
										
						tblCell4.appendChild(divCont2);
					}
						
					
					var aShort = document.createElement("a");
					aShort.id = "a_" + dt.Rows[i].Text_ID;
					aShort.href = "javascript:showFullMessage(" + dt.Rows[i].Text_ID + ");";
					aShort.innerHTML = dt.Rows[i].ShortMessage;
					aShort.title= "Click to see complete message";
					
					var spnLong = document.createElement("span");
					//spnLong.style.display = "none";
					spnLong.innerHTML = "<p>" +dt.Rows[i].Message + "</p>";
					   
					spnLong.id = "spn_" + dt.Rows[i].Text_ID;
					
					var spnHiddenUsername = document.createElement("span");
					spnHiddenUsername.style.display = "none";
					spnHiddenUsername.innerHTML = dt.Rows[i].Username;
					spnHiddenUsername.id = "spn_uname_" + dt.Rows[i].Text_ID;
					
					var divCont = document.createElement("div")
					divCont.style.overflow = "auto";
					divCont.style.height = "150px";
					divCont.id = "divCont_" + dt.Rows[i].Text_ID;
					if (dt.Rows[i].messageStatus=="unread")
					{
						//alert('unread');						
						divCont.style.display = "none";						
						var aE = document.createElement("a")
						aE.href = "javascript:hideEnvelope(" + dt.Rows[i].Text_ID + ");";
						aE.id = "aE_" + dt.Rows[i].Text_ID;
						var imgE = document.createElement("img")
						imgE.src = "/images/messages/envelope.jpg";
						
						aE.appendChild(imgE);
						tblCell5.align = "center";
						tblCell5.appendChild(aE);
					}
					SplitWordsWithFixedLengthForInboxOnly(spnLong);
					divCont.appendChild(spnLong);
					
					//tblCell3.appendChild(aShort);
					tblCell5.appendChild(divCont);
					tblCell5.appendChild(spnHiddenUsername);
					
					var dl = document.createElement("dl");
					
					var dt1 = document.createElement("dt");
					dt1.innerHTML = "Received";
					
					var dd1 = document.createElement("dd");
					dd1.className = "date";
					dd1.innerHTML = dt.Rows[i].Received_Short_Date;
					
					var dt2 = document.createElement("dt");
					dt2.innerHTML = "Received";
					
					var dd2 = document.createElement("dd");
					dd2.className = "time";
					dd2.innerHTML = dt.Rows[i].time;
					
					
					dl.appendChild(dt1);
					dl.appendChild(dd1);
					
					dl.appendChild(dt2);
					dl.appendChild(dd2);
					
					tblCell6.className = "dateReceivedSent";
					tblCell6.appendChild(dl);
					
					var aRep = document.createElement("a");
					aRep.href = "javascript:showReplyMessage(" + dt.Rows[i].Text_ID + ");";
					aRep.innerHTML = "Reply";
					
					var liRep = document.createElement("li");
					liRep.className = "reply";
					liRep.appendChild(aRep);
					
					//Added by Andre 9/8/2007
					var aRepAll = document.createElement("a");
					aRepAll.href = "javascript:showReplyAllMessage(" + dt.Rows[i].Text_ID + ");";
					aRepAll.innerHTML = "Reply All";
					
					var liRepAll = document.createElement("li");
					liRepAll.className = "replyAll";
					liRepAll.appendChild(aRepAll);
					// end of changes
					
					var aFow = document.createElement("a");
					aFow.href = "javascript:showFowardMessage(" + dt.Rows[i].Text_ID + ");";
					aFow.innerHTML = "Forward";
					
					var liFow = document.createElement("li");
					liFow.className = "forward";
					liFow.appendChild(aFow);
					
					var aDel = document.createElement("a");
					aDel.href = "javascript:showRemoveMessage(" + dt.Rows[i].Text_ID + ");";
					aDel.innerHTML = "Delete";
					
					//Resize Code Start Here 
					
					var Enlarge = document.createElement("a");
					Enlarge.href = "javascript:increase('" + divCont.id + "');";
					
					
					var imgEnlarge = document.createElement("img");
					imgEnlarge.src = "/images/cuteeditor/enlarge.GIF";
					
					Enlarge.appendChild(imgEnlarge);
					
					var Reduce = document.createElement("a");
					Reduce.href = "javascript:decrease('" + divCont.id + "');";
					
					var imgReduce = document.createElement("img");
					imgReduce.src ="/images/cuteeditor/reduce.GIF";
					
					Reduce.appendChild(imgReduce);
					
					var liResize = document.createElement("li");
					liResize.className = "Resize clearfix";
											
					liResize.appendChild(Enlarge);
					liResize.appendChild(Reduce);
					
					var liDel = document.createElement("li");
					liDel.className = "delete";
					liDel.appendChild(aDel);
				
					var ulOpt = document.createElement("ul");
					
					ulOpt.appendChild(liRep);
					//Added by Andre 9/8/2007
					ulOpt.appendChild(liRepAll);
					ulOpt.appendChild(liFow);
					ulOpt.appendChild(liDel);
					ulOpt.appendChild(liResize);
					
					
					if (dt.Rows[i].IsText == "request")
					{
					
					}
					else
					{
						tblCell7.appendChild(ulOpt);
						tblCell7.className = "options";
						
					}
					stripetableBody();
				}
	}
	else
	{	
		document.getElementById("currRows").innerHTML = "0&nbsp;&#8211;&nbsp;0&nbsp;";
		document.getElementById("totalRows").innerHTML = "0";
				
		var insertTable = tbl.insertRow(i);
								
		var tblCell1 = insertTable.insertCell(0)
		tblCell1.colSpan = "5";
		tblCell1.innerHTML = "There are no messages in your inbox.";
		
	}
	resizeAvatars();
	var node = document.getElementById('messages');
    SplitWordsWithFixedLength(node);		
}

//Resize Code Start Here
var size = 150
    function increase(id)
    {
    var div = document.getElementById(id)
    size = size + 100;
    div.style.height = size + 'px';
    
    
    }
    function decrease(id)
    {
    var div = document.getElementById(id)
    if(size <= 150)
    {
    div.style.height = 150 + 'px';
    }
    else
    {
    size = size - 100;
    div.style.height = size + 'px';
    
    }
  }
//Resize Code End Here

function hideEnvelope(itemID){
	var aE = document.getElementById("aE_" + itemID);
	
	Effect.Fade("aE_" + itemID);
	window.setTimeout("Effect.Appear('divCont_" + itemID + "');", 1000);
	window.setTimeout("setMessageRead(" + itemID + ");", 1001);
	
}

function setMessageRead(itemID){
	engTextInbox.setMessReadTrue(itemID, callback_setMessageRead);
}

function callback_setMessageRead(res){
if (res.error!=null){alert(res.error);};

updateProfileDetails();
}
function showFullMessage(itemID){
//	if (document.getElementById('th_' + itemID).className == "unread") 
//		{
//		engTextInbox.setMessReadTrue(itemID, callback_showFullMessage);
//		}
	
//	var mess = getSpanValue('spn_' + itemID);
	var username = getSpanValue('spn_uname_' + itemID);
	
//	setSpanValue('fullMessage', mess);
	setSpanValue('fromUsername', username);
	openModalWindow('viewMessage');
}

function callback_showFullMessage(res){

checkResponse(res.value);

	if (res.value != 0)
		{
		document.getElementById('th_' + res.value).className =  'read';
		updateProfileDetails();
		}
		
	
}

function showRemoveMessage(itemID){
	engTextInbox.getTextMessageDetails(itemID, callback_showRemoveMessage);
	
}

function callback_showRemoveMessage(res){

checkResponse(res.value);

	var dt = res.value;
	setSpanValue('removeUserID', dt.Rows[0].Text_ID);
	setSpanValue('removeUsername',dt.Rows[0].Username);
	setSpanValue('removeDate',dt.Rows[0].Short_Received);
	openModalWindow('removeMessage');
}


function removeMessage(){
	var textID = getSpanValue('removeUserID');
	showFeedback('removeMessageActionContainer');
	engTextInbox.deleteInbMess(textID, callback_removeMessage);
	
}

function callback_removeMessage(res){

checkResponse(res.value);

	if (res.value)
		{
		feedbackValid('Deleting','Deleted');
		window.setTimeout('hideRemoveMessage();', 4000);
		}
	else
		{
		feedbackInvalid('removeMessageActionContainer');
		}
}

function hideRemoveMessage(){
	var page = getSpanValue('currPageNum');
	loadPage(page);
	closeModalWindow('removeMessage');
	cleanupFeedback('removeMessageActionContainer');
	updateProfileDetails();
}


function showReplyMessage(itemID){
	//document.getElementById('confirmSubmitComplete').style.display = 'none';
	engTextInbox.getTextMessageDetails(itemID, callback_showReplyMessage);
	
}

function callback_showReplyMessage(res){

checkResponse(res.value);

	var dt = res.value;
	//alert(dt.Rows[0].From_User_ID);
	var img = document.getElementById("imgSender");
	setSpanValue('replyUsername1',dt.Rows[0].Username);
	img.src = dt.Rows[0].SrcThumb;
	setSpanValue('replyUserID', dt.Rows[0].UserID);
	//setSpanValue('origMessage1', dt.Rows[0].Text_ID);
	setSpanValue('origMessID', dt.Rows[0].Text_ID);
	setSpanValue('replyUsername',dt.Rows[0].Username);
	setSpanValue('origMessage', dt.Rows[0].Message);
	openModalWindow('reply');
    var node = document.getElementById('origMessage');
    if (node != null) {
        SplitWordsWithFixedLength(node);
    }	
}
// Added by Andre - 10/08/2007
// New functionality of reply All
function showReplyAllMessage(itemID){
	
	engTextInbox.getMultipleRecipientsMessageDetails(itemID, callback_showReplyAllMessage);
}

function bindUserfromhome()
{
	
}

function callback_showReplyAllMessage(res){

checkResponse(res.value);

	var dt = res.value;
	var strTo ="";
	var strToIDs;
	var img = document.getElementById("imgSender1");
	//setSpanValue('replyUsername1',dt.Rows[0].Username);
	img.src = dt.Rows[0].SrcThumb;
	if((img.offsetWidth>30)||(img.offsetHeight>10))
				{
					if (img.offsetWidth >= img.offsetHeight)
						{
							img.style.width='30px';
						}
					else
						{
							img.style.height='10px';
						}
				}
	setSpanValue('profilePic', dt.Rows[0].newFrom_User_Name);
	setSpanValue('origMessageAll', dt.Rows[0].Message);
	setSpanValue('origMessIDAll', dt.Rows[0].Text_ID);
	// Loop through all the messages and concatenate the id's 
	// to whom the message was sent to.
	for (var i=0; i<dt.Rows.length; i++)
			{
			if (i==0) {
			 strTo = dt.Rows[i].Username;
			 strToIDs = dt.Rows[i].To_User_ID;
			 }
			else {
			 strTo = strTo + ", " + dt.Rows[i].Username;
			 strToIDs = strToIDs + ", " + dt.Rows[i].To_User_ID;
			 }
			}
			
			// now we need to add the original sender of the message
			//strTo, stores the usernames for display, 
			// strToIDs stores the userID's for processing
	if (dt.Rows.length == 1 ) {
		if(dt.Rows[0].newFrom_User_Name == dt.Rows[0].Username)
		{
		strTo = dt.Rows[0].newFrom_User_Name;
		strToIDs = dt.Rows[0].newFrom_User_ID; }
		else {
	    strTo = strTo + ", " + dt.Rows[0].newFrom_User_Name;
	    strToIDs = strToIDs + ", " + dt.Rows[0].newFrom_User_ID;
	   }
	}
	else {
	    strTo = strTo + ", " + dt.Rows[0].newFrom_User_Name;
	    strToIDs = strToIDs + ", " + dt.Rows[0].newFrom_User_ID;
	   }
	
	setSpanValue('replyAllUserID', strTo);
	setSpanValue('replyAllUser', strToIDs);
	
	openModalWindow('replyAll');
var node = document.getElementById('origMessageAll');
if (node != null) {
    SplitWordsWithFixedLength(node);
}  	
}



function callback_replyAllMessage(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++)
			{
			clearInputField(controls.Rows[i].controlID);
			setControlErrorMessage(controls.Rows[i].controlID,'');
			}
			switch (results.Rows[0].errMessage)
				{
					case "valid":
						feedbackValid('Replying','Replied');
						window.setTimeout("hideReplyAllMessage()", 4000);
					break
				}
		}
	else
		{
		var controls = res.value.Tables[1];
		switch (results.Rows[0].errMessage)
				{
					case "invalid":
						feedbackInvalid('replyAllMessageActionContainer');
						window.setTimeout(function(){processValidationResponse(controls)}, 2001);
					break
					case "nocredit":
						alert('You need to purchase more text credits');
					break
					case "dberror":
					feedbackInvalid('replyMessageActionContainer');
					break
					
				}
		}
}
// end of changes - Andre
// End of changes


function callback_replyMessage(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(controls.Rows[i].controlID);
			setControlErrorMessage(controls.Rows[i].controlID,'');
			}
			switch (results.Rows[0].errMessage)
				{
					case "valid":
						feedbackValid('Replying','Replied');
						window.setTimeout("hideReplyMessage()", 4000);
					break
				}
		}
	else
		{
		var controls = res.value.Tables[1];
		switch (results.Rows[0].errMessage)
				{
					case "invalid":
						feedbackInvalid('replyMessageActionContainer');
						window.setTimeout(function(){processValidationResponse(controls)}, 2001);
					break
					case "nocredit":
						alert('You need to purchase more text credits');
					break
					case "dberror":
					feedbackInvalid('replyMessageActionContainer');
					break
					
				}
		}
}

function hideReplyMessage(){

	closeModalWindow('reply');
	cleanupFeedback('replyMessageActionContainer');
}
function hideReplyAllMessage(){

	closeModalWindow('replyAll');
	cleanupFeedback('replyAllMessageActionContainer');
}

function showFowardMessage(itemID){
	//document.getElementById('confirmFowardComplete').style.display = 'none';
	
	engTextInbox.getTextMessageDetails(itemID, callback_showFowardMessage);
	
}

function callback_showFowardMessage(res){

checkResponse(res.value);
	
	var dt = res.value;
	var img = document.getElementById("imgsender2");
	setSpanValue('sendername',dt.Rows[0].Username);
	img.src = dt.Rows[0].SrcThumb;
	setSpanValue('origMessageForward', dt.Rows[0].Message);
	setSpanValue('origForwardMessID', dt.Rows[0].Text_ID);
	openModalWindow('forward');
var node = document.getElementById('origMessageForward');
if (node != null) {
    SplitWordsWithFixedLength(node);
}	
}

function getUserNameIDs(){
	var chks = document.getElementById('ulMessageToUsers').getElementsByTagName("input");
	var idList;
	var counter = 0;
	if(chks.length == 0)
	{
	idList = '';
	}
	else
	{
		for (var i=0; i<chks.length; i++)
		{
		if (chks[i].type == "checkbox")
			{
				if(counter == 0)
				{
				idList = chks[i].value;
				counter = counter + 1;
				}
				else
				{
				idList = idList + ',' + chks[i].value;
				counter = counter + 1;
				}
			}
		}
	}
	
	return idList;			
}




function hideForwardMessage(){
cleanupFeedback('forwardMessageActionContainer');

//var a = document.getElementByID('txtForwardMessage');
//alert(a);
//SetControlValue('txtForwardMessage','');
CloseWindow();
//closeModalWindow('forward');
}



function callback_AutoCompleteUsername(res)
{
    checkResponse(res.value);

	if (res.value != null)
	{
	    var ds = res.value;
	    var listControlID = "autoCompleteListtxtUsername";
	    var listControlWrapper = "txtUsernameListWrapper";
	    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:completeUsername('" + 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";
	    }
	}
}
//Copied form sendText.js
function completeUsername(accID, username){

	setSpanValue('txtUsernameErr', '');
	setSpanValue('spnUsername', username);
	setSpanValue('sendAccID', accID);
	Effect.Fade('txtUsernameListWrapper');
	var ul = document.getElementById('ulMessageToUsers');	
	var chks = document.getElementById('ulMessageToUsers').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)
		{
			resetControlErrorMessage('txtUsername','You have already selected this person');
			var chk = document.createElement("input");
			chk.type = "checkbox";
			chk.value = accID;
			chk.id = "c_"+accID;
			chk.checked = true;
			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
		{
			setControlErrorMessage('txtUsername','You have already selected this person');			
		}
		
	}
	else
	{
		var chk = document.createElement("input");
		chk.type = "checkbox";
		chk.value = accID;
		chk.id = "c_"+accID;
		chk.checked = true;
		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("txtUsername").value = "";
}

function deleteFriend(accID)
{
	var ul = document.getElementById("ulMessageToUsers");
	var chks = document.getElementById("ulMessageToUsers").getElementsByTagName("input");
	var lis = document.getElementById("ulMessageToUsers").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);
				}
		}	
	
	
}
//Inbox
function completeUsername2(accID, username){
	setSpanValue('txtUsernameErr', '');
	setSpanValue('sendAccID', accID);
	setSpanValue('spnUsername', username);
	Effect.Fade('txtUsernameListWrapper');
	//document.getElementById("sendAccID").style.display = "block";
	//setControlValue('spnUsername', username);
	

}
function completeUsername1(accID, username)
{
//completeUserList(accID, username);
	Effect.Fade('txteditUsernameListWrapper');
	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("txteditUsername").value = "";
}
function acceptFriend(itemID){
	engTextInbox.acceptFriendRequest(itemID, callback_acceptFriend);
}

function callback_acceptFriend(res){
if (res.error!=null){alert(res.error)};
checkResponse(res.value);
	if (res.value!="False")
		{
		alert(res.value + " is a friend.");
		}
	else
		{
		alert("Friend was not added.");
		}
}

function addToFriends(itemID){
	openModalWindow('viewMessage');
	
	window.setTimeout("document.getElementById('confirmSubmit').style.display = 'block';", 1000);
	
	window.setTimeout("engProfile.addFriend('" + itemID + "', callback_addToFriends);", 3001);
}

function callback_addToFriends(res){
if (res.error!=null){alert(res.error)};
checkResponse(res.value);

if (res.value == "added")
	{
	setSpanValue('addFriendUsername', getSpanValue('fromUsername'));
	window.setTimeout("document.getElementById('confirmSubmit').style.display = 'none';", 2000);
	window.setTimeout("document.getElementById('confirmSubmitComplete').style.display = 'block';", 2001);
	window.setTimeout("document.getElementById('confirmSubmitComplete').style.display = 'none';", 3001);
	var page = getSpanValue('currPageNum');
	loadPage(page);
	window.setTimeout("closeModalWindow('viewMessage');", 3000);
	}
else
	{
	document.getElementById('confirmSubmit').style.display = 'none';
	alert(getSpanValue('fromUsername') + " has been added as one of your friends.");
	
	}
}

function addEventToCal(itemID, ownerID){

	openModalWindow('viewMessageAddToCal');
	
	window.setTimeout("document.getElementById('confirmAddToCal').style.display = 'block';", 1000);
	
	window.setTimeout("engTextInbox.addOthersEventToMycal(" + itemID + "," + ownerID + ", callback_addEventToCal);", 3001);
}

function callback_addEventToCal(res){
if (res.error!=null){alert(res.error)};
checkResponse(res.value);

//if (res.value == "added")
//	{
	
	window.setTimeout("document.getElementById('confirmAddToCal').style.display = 'none';", 2000);
	window.setTimeout("document.getElementById('confirmAddToCalComplete').style.display = 'block';", 2001);
	window.setTimeout("document.getElementById('confirmAddToCalComplete').style.display = 'none';", 3001);
	//var page = getSpanValue('currPageNum');
	//loadPage(page);
	window.setTimeout("closeModalWindow('viewMessageAddToCal');", 3000);
//	}
//else
//	{
	//document.getElementById('confirmSubmit').style.display = 'none';
	//alert(getSpanValue('fromUsername') + " is aleady one of your friends.");
//	alert('false');
//	}
}

function acceptcFriendFID(receiverFID, senderFID){
	openModalWindow('friendAccepted');
	setSpanValue('recFID',receiverFID);
	setSpanValue('senFID',senderFID);
	setSpanValue('spnchildrequest','1');
	var parentsdata  =  engTextInbox.IsPCRequestPending(senderFID,receiverFID);
	if (parentsdata.value == 1) {
		setSpanValue('AcceptTitle','Parent-Child Request');
		setSpanValue('AcceptMessage','Accept the Parent-Child request and send a message:');
	}		
}

function acceptpFriendFID(receiverFID, senderFID){
	openModalWindow('friendAccepted');
	setSpanValue('recFID',receiverFID);
	setSpanValue('senFID',senderFID);
	setSpanValue('spnchildrequest','2');
	var parentsdata  =  engTextInbox.IsPCRequestPending(senderFID,receiverFID);
	if (parentsdata.value == 1) {
		setSpanValue('AcceptTitle','Child-Parent Request');
		setSpanValue('AcceptMessage','Accept the Child-Parent request and send a message:');
	}		
}

function acceptFriendFID(receiverFID, senderFID){
	openModalWindow('friendAccepted');
	setSpanValue('recFID',receiverFID);
	setSpanValue('senFID',senderFID);
	setSpanValue('spnchildrequest','0');
}


function hideAcceptFriendRequest(){

	cleanupFeedback('friendAcceptActionContainer');
	closeModalWindow('friendAccepted');
	
	var page = getSpanValue('currPageNum');
	loadPage(page);
}

function declineFriendFID(receiverFID, senderFID){

		showDeclineFriend(receiverFID, senderFID);
		
		
}

function showDeclineFriend(receiverFID, senderFID){
	openModalWindow('friendDeclined');
	setSpanValue('recFID', receiverFID);
	setSpanValue('senFID', senderFID);
	var parentsdata  =  engTextInbox.IsPCRequestPending(senderFID,receiverFID);
	if (parentsdata.value == 1) {
		setSpanValue('DeclineTitle','Parent-Child Request');
		setSpanValue('DeclineMessage','Are you sure you wish to decline the Parent-Child request?');
	}
}

function declineRequest(){
	var recFID = getSpanValue('recFID');
	var senFID = getSpanValue('senFID');
		showFeedback('friendDeclinedActionContainer');
		engTextInbox.declineFriendRequestFID(recFID, senFID, callback_declineFriendFID);
}


function callback_declineFriendFID(res){
if (res.error!=null){alert(res.error)};
checkResponse(res.value);

if (res.value!="False")
		{
		
		feedbackValid('Declining','Declined');
		window.setTimeout("hideDeclineRequest();", 4000);
		}
	else
		{
		feedbackInvalid('friendDeclinedActionContainer');
		}
		
}

function hideDeclineRequest(){
	cleanupFeedback('friendDeclinedActionContainer');
	closeModalWindow('friendDeclined');
	
	var page = getSpanValue('currPageNum');
	loadPage(page);
}


function deleteFriendFID(receiverFID, senderFID){

		showDeleteFriend(receiverFID, senderFID);
		
		
}

function showDeleteFriend(receiverFID, senderFID){
	openModalWindow('friendDeleted');
	setSpanValue('recFIDdel', receiverFID);
	setSpanValue('senFIDdel', senderFID);
	var parentsdata  =  engTextInbox.IsPCRequestPending(senderFID,receiverFID);
	if (parentsdata.value == 1) {
		setSpanValue('IgnoreTitle','Parent-Child Request');
		setSpanValue('IgnoreMessage','Are you sure you wish to ignore the Parent-Child request?');
	}
}

function deleteRequest(){
	var recFID = getSpanValue('recFIDdel');
	var senFID = getSpanValue('senFIDdel');
		showFeedback('friendDeletedActionContainer');
		engTextInbox.deleteFriendRequestFID(recFID, senFID, callback_deleteFriendFID);
}


function callback_deleteFriendFID(res){
if (res.error!=null){alert(res.error)};
checkResponse(res.value);

if (res.value!="False")
		{
		
		feedbackValid('Ignoring','Ignored');
		window.setTimeout("hideDeletedRequest();", 4000);
		}
	else
		{
		feedbackInvalid('friendDeletedActionContainer');
		}
		
}

function hideDeletedRequest(){
	cleanupFeedback('friendDeletedActionContainer');
	closeModalWindow('friendDeleted');
	
	var page = getSpanValue('currPageNum');
	loadPage(page);
}

function RemoveMessageBatch(){
	var controls = new Array();
	var cont = document.getElementsByTagName("input");
	var counter = 0;
	
	for (var i=0; i<cont.length; i++)
		{
		if (cont[i].type == "checkbox")
			{
			if (cont[i].checked)
				{
				
				controls[counter] = cont[i].value;
				counter = counter + 1;
				}
			}

		}
	
	
	if(controls.length == 0)
	{
		setControlDisplay('selectbatch','block');
		setControlDisplay('deletebatch','none');
		
		
	}
	else
	{
		setControlDisplay('deletebatch','block');
		setControlDisplay('selectbatch','none');
		
	}
	
	openModalWindow('removeMessageBatch');
	
}
function removeMessageBatch(){
	var controls = new Array();
	var cont = document.getElementsByTagName("input");
	var counter = 0;
	
	for (var i=0; i<cont.length; i++)
		{
		if (cont[i].type == "checkbox")
			{
			if (cont[i].checked)
				{
				 if (!isNaN(cont[i].value)) {
						controls[counter] = cont[i].value;
						counter = counter + 1;
					}
				}
			}

		}
	
	showFeedback('removeMessageBatchActionContainer');
	engTextInbox.deleteSelectedMessages(controls, callback_removeMessageBatch);	
}
function callback_removeMessageBatch(res)
{
if (res.error!=null){alert(res.error)};
if (res.value)
	{
	feedbackValid('Deleting','Deleted');
	window.setTimeout("hideRemoveBatchMessages();", 4000);		
	}
else
	{
	feedbackInvalid('removeMessageBatchActionContainer');
	}	
	
}
function hideRemoveBatchMessages(){
	cleanupFeedback('removeMessageBatchActionContainer');
	closeModalWindow('removeMessageBatch');
	var pageNum = getSpanValue('currPageNum');
	loadPage(pageNum);
	
}
var toggleSelect = "unchecked";
function selectAllMessages(){
	var cont = document.getElementsByTagName("input");
	
	if (toggleSelect == "unchecked")
		{
		for (var i=0; i<cont.length; i++)
			{
			if (cont[i].type == "checkbox")
				{
				cont[i].checked = "true";
				}
			}
		toggleSelect = "checked";
		}
	else
		{
		for (var i=0; i<cont.length; i++)
			{
			if (cont[i].type == "checkbox")
				{
				cont[i].checked = "";
				}
			}
		toggleSelect = "unchecked";
		}
	
	
}
function CloseWindow(){
	var ul = document.getElementById("ulMessageToUsers");
	var chks = document.getElementById("ulMessageToUsers").getElementsByTagName("input");
	var lis = document.getElementById("ulMessageToUsers").getElementsByTagName("li");
	var idlist = new Array();
	for(var i=0;i<lis.length;i++)
		{
				
					idlist[i] = document.getElementById(lis[i].id);
		
		}	
	
	for(var i=0;i<idlist.length;i++)
	{
		var li = idlist[i];
		ul.removeChild(li);
	}

		closeModalWindow('forward');
		
}
 var spanportalid;
function joinPortal(portalid)
{
	spanportalid = portalid;
	engPortal.joinPortal(portalid, callback_joinPortal)

}
function callback_joinPortal(res)
{
    var portal = engPortal.getPortalDetailsByID(spanportalid);
    
	checkResponse(res.value);
		var results  = res.value.Tables[0];
		if (results.Rows[0].result == "True")
		{
			var controls = res.value.Tables[1];

			switch(results.Rows[0].errMessage)
				{
					case "valid":
					//alert(spanportalid);
					//alert('http://www.obami.com/myCommunityPortals/index.aspx?PortalID=' + spanportalid);
					var path = '/myCommunityPortals/index.aspx?PortalID=' + spanportalid
					    alert('You have now become a member of ' + portal.value.Rows[0].Portal_Title);
						window.location = path;
						break
				}
		}
		else
		{
			var controls = res.value.Tables[1];
			switch (results.Rows[0].errMessage)
				{
					case "invalid":										
						break
					case "AccessDenied":
						alert("Sorry, you do not have access to this Portal.");
				}				
		}
}