//<![CDATA[
var alphabet = "";
function setAlphabet(c){
	alphabet = c;
}
function chopFriendDetail(className, id, length)
{
	var FriendDetails = document.getElementsByClassName(className);
	for(var i=0;i < FriendDetails.length;i++)
	{	
		var itemid = FriendDetails[i].id
		if(itemid.indexOf(id)==0)
		{
			 var output = CutString(FriendDetails[i].innerHTML,length);
			 FriendDetails[i].innerHTML = output;
		}
	}
}

function chopFriendDetailwithClassName(className, length)
{
	var FriendDetails = document.getElementsByClassName(className);
	for(var i=0;i < FriendDetails.length;i++)
	{
		var output = CutString(FriendDetails[i].innerHTML,length);
		FriendDetails[i].innerHTML = output;
	}
}

function importContact()
{
	if ($('username').value == '')
	{
		$('infotextImportContact').innerHTML = _("Please enter email address");
		showElement('infoboxImportContact');
		return;
	}

	if ($('password').value == '')
	{
		$('infotextImportContact').innerHTML = _("Please enter password");
		showElement('infoboxImportContact');
		return;
	}

    showElement('contactlist');
	hideElement('loadcontactbutton');
    posting('contactlist');
	var opt = {
			method: 'post',
			postBody: 'id='+ourid+"&username="+$('username').value+"&domain="+ $('domain').value+"&password="+encodeURIComponent($('password').value),
			onSuccess: function(t) {
				if (t.responseText.indexOf('no contacts') != -1)
				{
					$('infotextImportContact').innerHTML = _("You have no contact in your email");
					showElement('infoboxImportContact');
					posterror('contactlist');
					hideElement('contactlist');
				}
				else if (t.responseText.indexOf('auth failed') != -1)
				{
					$('infotextImportContact').innerHTML =  _("incorrect email or password");
					showElement('infoboxImportContact');
					//$('contactlist').innerHTML = t.responseText;
					posterror('contactlist');
					showElement('contactlist');
					hideElement('importbutton');
					showElement('loadcontactbutton');
				}
				else if (t.responseText.indexOf('Could not connect to messenger service') != -1 ||
					t.responseText.indexOf('msn server error') != -1 ||
					t.responseText.indexOf('Unable to connect to msn server') != -1)
				{
					$('infotextImportContact').innerHTML = _("The destination server is not reachable. Please try again");
					showElement('infoboxImportContact');
					posterror('contactlist');
					hideElement('importbutton');
					showElement('loadcontactbutton');
				}
				else if (t.responseText.indexOf('msn errors') != -1 ||
					t.responseText.indexOf('Error: -1') != -1)
				{
					$('infotextImportContact').innerHTML = _("Retrieve data fail. Please try again.");
					showElement('infoboxImportContact');
					posterror('contactlist');
					hideElement('importbutton');
					showElement('loadcontactbutton');
				}
				else
				{
					$('contactlist').innerHTML = t.responseText;
					chopFriendDetailwithClassName('loadedContactName', 10);
					showElement('contactlist');
					showElement('importbutton');
					hideElement('loadcontactbutton');
				}
			},
			onFailure: function(t) {
				$('infotextImportContact').innerHTML = _("Fail to retrieved information. Please try again");
				showElement('infoboxImportContact');
				posterror('contactlist');
				hideElement('importbutton');
				showElement('loadcontactbutton');
			}/*,
			/*onTimeout: function(t) {
				$('infotext').innerHTML = _("Retrieve data fail. Please try again.");
				showElement('infobox');
				posterror('contactlist');
				hideElement('contactlist');
				hideElement('importbutton');
				showElement('loadcontactbutton');
			}*/
		  }
	new Ajax.Request('/friends/importcontact', opt);
}

function sendMessage()
{
    posting('infotext1');
	var opt = {
			method: 'post',
			postBody: "targetid="+friend+"&topic="+encodeURIComponent($('SendMessageTopic').value)+"&message="+encodeURIComponent($('Message').value),
			onSuccess: function(t) {
				hideElement('sendmessage');
				$('infotext1').innerHTML = _("Send message completed");
				showElement('infobox1');				
			},
			onFailure: function(t) {
				alert(_("The message cannot be sent. Please try again"));
				posterror('infotext1');
			}
		  }
  
	new Ajax.Request('/sendPrivateMessage', opt);
}

function sendComment()
{
    posting('infotext');
    var opt = {
			method: 'post',
			postBody: 'ownerid=' + friend + '&userid=' + ourid + '&text=' + encodeURIComponent($('Comment').value),
			onSuccess: function(t) {
				hideElement('sendcomment');
				$('infotext').innerHTML = _("The message has been already sent");
				showElement('infobox');
			},
			onFailure: function(t) {
				alert(_("The message cannot be sent. Please try again"));
				posterror('infotext');
			}
		}
    new Ajax.Request('/profiles/postComment', opt);
}

function sendInviteEmail()
{
	count = 0;
	email = "";
	for (i = 1;i <= 10;i++)
	{
		if ($('email'+i).value != '')
		{
			count++;
			email = email + '&email' + count + '=' + $('email'+i).value;
		}
	}
	if (count > 0)
	{
		posting('infotext');
		var opt = {
				method: 'post',
				postBody: 'id='+ourid+"&count="+count+email,
				onSuccess: function(t) {
					hideElement('invitefriend');	
					$('infotext').innerHTML = _("Send invite email success");
					showElement('infobox');												
				},
				onFailure: function(t) {
					$('infotext').innerHTML = _("Send fail. Please, try again.");
					showElement('infobox');	
					posterror('infotext');
				}
			  }
	  
		new Ajax.Request('/friends/sendinvite', opt);
	}
	else
	{
		hideElement('invitefriend');	
	}
}

function showImportContact()
{
	$('username').value="";
	$('domain').selectedIndex = 0;
	$('password').value = "";
	showElement('importcontact');
	showElement('loadcontactbutton');
	hideElement('contactlist');
	hideElement('importbutton');
}

function showSendMessage(id,display)
{
	friend = id;
	$('Message').value = '';
	$('SendMessageTopic').value='';
	$('SendMessageTo').innerHTML=display;
	showElement('sendmessage');
	
}

function showSendComment(id)
{
	friend = id;
	$('Comment').value = '';
	showElement('sendcomment');
	
}

function showInviteFriend()
{
	for (i = 1;i <= 10;i++)
	{
		$('email'+i).value = "";
	}

	showElement('invitefriend');
}

function sendInviteEmailFromImportContact(){
    var domain = $('domain').value;
	var fromemail = '';
    var params = '';
    if(domain == "twitter"){
        fromemail = $('username').value;
        params = "&domain="+domain+"&password="+$("password").value;
    }else{
        fromemail = $('username').value + "@" + domain;
    }
	var objs = $("contactlist");
	objs = objs.getElementsByClassName("friendupdateitem");
	var count = 0;
	var email = '';
	for(i = 0; i < objs.length; i++){
		var obj = objs[i].getElementsByTagName('input')[0]
		if(obj.checked){
			count++;
			email = email + '&email' + count + '=' + obj.value;
		}
        obj.checked = false;
	}
	
	if (count > 0){
		posting('contactlist');
		var opt = {
				method: 'post',
				postBody: 'id='+ourid+"&count="+count+email+"&fromemail="+encodeURIComponent(fromemail)+params,
				onSuccess: function(t) {
					$('infotext').innerHTML = _("The message has been already sent");
					showElement('infobox');	
					hideElement('importbutton');
					showElement('loadcontactbutton');
					hideElement('importcontact')
				},
				onFailure: function(t) {
					$('infotext').innerHTML = _("Send fail. Please, try again.");
					showElement('infobox');	
					posterror('contactlist');
					hideElement('importbutton');
					showElement('loadcontactbutton');
				}
			  }
	  
		new Ajax.Request('/friends/sendinvite', opt);
	}
}

function deletefriend(userid,friendid,friendname){
		var opt = {
					method: 'post',
					onSuccess: function(result){
						$('infotext').innerHTML = _("%s has been deleted from your friend list").replace("%s",friendname);
						showElement('infobox');	
						showlatestfriends(useridtmp, '0');
					},
					onFailure: function(){$('infotext').innerHTML = _("%s cannot been deleted from your friend list").replace("%s",friendname);
						showElement('infobox');	},
					onTimeout: function(){$('infotext').innerHTML = _("Time Out");showElement('infobox');}
				}
		new Ajax.Request('/friends/delete/'+useridtmp+'/'+friendid,opt);
}

function settopfriend(friendid)
{
	if(!$('loading').visible())
	{
		showElement("loading");
		var opt = {
					method: 'post',
					onSuccess: function(result){
						if (result.responseText=="OK")
						{
							hideElement('settopfriend'+friendid);
							showElement('rmtopfriend'+friendid);
							hideElement("loading");
						}
					},
					onFailure: function(){hideElement("loading");}
				}
			new Ajax.Request('/friends/addtopfriends/'+friendid,opt);
	}
}

function removetopfriend(friendid)
{
	if(!$('loading').visible())
	{
		showElement("loading");
		var opt = {
				method: 'post',
				onSuccess: function(result){
					if (result.responseText=="OK")
					{
						if(alphabet=="TOPFRIEND")
							showlatestfriends(useridtmp, '0');
						else
						{
							hideElement('rmtopfriend'+friendid);
							showElement('settopfriend'+friendid);
						}
					}
					hideElement("loading");
				},
				onFailure: function(){hideElement("loading");}
			}
		new Ajax.Request('/friends/removetopfriends/'+friendid,opt);
	}
}

function selectAllContactInFriendUpdateItem(checked){
	var objs = $("contactlist")
	objs = objs.getElementsByClassName("friendupdateitem");
	for(i = 0; i < objs.length; i++){
		objs[i].getElementsByTagName('input')[0].checked = checked;
	}
}

function showlatestfriends(userid, page,e)
{   
	if(e && e.className == 'selected')
		return
	if(e)
	{
		if (e.name == 'thaisubfilterfriend')
		{
			var tmp = document.getElementsByName('thaisubfilterfriend');
			for(var i = 0;i<tmp.length;i++)
			{
				if(tmp[i] != e)
					tmp[i].className = '';
				else
					e.className = 'selected';
			}
		}
		else if (e.name == 'engsubfilterfriend')
		{
			var tmp = document.getElementsByName('engsubfilterfriend');
			for(var i = 0;i<tmp.length;i++)
			{
				if(tmp[i] != e)
					tmp[i].className = '';
				else
					e.className = 'selected';
			}
		}
		else if (e.name == 'mainfilterfriend')
		{
			var tmp = document.getElementsByName('mainfilterfriend');
			for(var i = 0;i<tmp.length;i++)
			{
				if(tmp[i] != e)
					tmp[i].className = '';
				else
					e.className = 'selected';
			}
		}
	}
	showElement("loading");
	var opt = {
			method: 'post',
			postBody: 'id=' + userid + '&Page='+ page + '&alphabet='+alphabet,
			onSuccess: function(t) {
				var element = $('friendbox');
				element.innerHTML = t.responseText;
				chopFriendDetail('frienddisplayname', 'DisplayNameId', 10);
				chopFriendDetail('friendstatus', 'StatusId', 15);

				var options = Object.extend({
                from: (element.getStyle('display') == 'none' ? 0.0 : element.getOpacity() || 1.0),
                to:   1.0,
                afterFinishInternal: function(effect) {
                    effect.element.forceRerendering();
                    $("loading").style.display = 'none';
                },
                beforeSetup: function(effect) {
                    effect.element.setOpacity(effect.options.from).show();
                },
                durations: 0.1
            });
            new Effect.Opacity(element,options);
			},
			onFailure: function(t) {
				//alert('There is something error, plese try again.');
				hideElement("loading");
			}
		  }
	new Ajax.Request('/friends/showFriends', opt);
}
function friendsearch(userid,page)
{
		var opt = {
				method: 'post',
				postBody: 'userid='+userid+'&keyword='+ encodeURIComponent($('keyword').value)  + '&itempage=' + page,
				onSuccess: function(t) {
							$('friendsearchresult').innerHTML = t.responseText;								
				},
				onFailure: function(t) {
					
				}
			  }
	  
		new Ajax.Request('/friends/friendsearch', opt);
}
function enterpress3(e)
{
		var keynum;
		if(window.event) // IE
		  {
				keynum = e.keyCode;
		  }
		else if(e.which) // Netscape/Firefox/Opera
		  {
				keynum = e.which;
		  }

		if(keynum==13)
		{
			friendsearch(userid,'0');
		}
		return true;
}
function submitImportContact(e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return;

	if (keycode == 13){
		importContact();
	}
}
function showDeleteFriendBox(userid,friendid,display_name){
    $("deletefriendboxokbtn").onclick = function(){deletefriend(userid,friendid,display_name);hideElement('deletefriendbox');}
    $("deletefriendboxtext").innerHTML = _('Do you really want to delete %s from your friend list').replace("%s",display_name);
    showElement("deletefriendbox");
}
//]]>
