
function selectAll(theField) {
	var tempval = theField; //eval("document."+theField)
	tempval.focus();
	tempval.select();
	try
	{
		therange=tempval.createTextRange();
		therange.execCommand("Copy");
	}
	catch(e){ }
}

function hideSection(class_name)
{
	var toHide = getElementsByClassName(document.body,'TR',class_name);
	
	var disp = 'none';
	if(toHide[0].style.display == 'none')
	{
		disp = 'block';
	}
	for(var el in toHide)
	{
		toHide[el].style.display = disp;
	}
}

function hideFloatBox(e)
{
	var src = nn6 ? e.target: event.srcElement;
	if(src.tagName == 'OPTION' || src.tagName == 'SELECT')
	{
		return;
	}

	var editBox = document.getElementById('floatbox');
	var el;
	if(editBox.style.display == 'block')
	{
		el = editBox;
	}else{
		document.onclick = null;
		return;
	}
	var pos = findPos(el);
	var cursor = getScrollXY();
	var clientX = (nn6 ? e.clientX : event.clientX) + cursor[0];
	var clientY = (nn6 ? e.clientY : event.clientY) + cursor[1];
	
	if(clientX < pos[0] || clientX > pos[0] + el.offsetWidth || clientY < pos[1] || clientY > pos[1] + el.offsetHeight)
	{
		el.style.display = 'none';
		document.onclick = null;
	}
}

function addShout()
{
	var msg = document.getElementById('shout_message');
	var url = '/ajax.php';
	if(msg.value.length >= 2)
	{
		var params = 'mode=profile&action=shout&id=' + profileId.toString() + '&message=' + encodeURIComponent(msg.value);
	}
	else
	{
		alert('Message too short.');
		return;
	}
	startRequest(url, params, function()
									   {
									   	if(xhr.readyState == 4)
										{
											var res = eval('(' + xhr.responseText + ')');
											document.getElementById('shouts').innerHTML = res['shouts'];
										}
									   });
	msg.value = '';
	return false;
}

function deleteShout(id)
{
	if(!confirm('Are you sure you want to delete this shout?'))
	{
		return false;
	}
	var url = '/ajax.php';
	var params = 'mode=profile&action=deleteshout&id=' + encodeURIComponent(id.toString());
	startRequest(url, params, function()
									   {
										   if(xhr.readyState == 4)
										   {
											   var res = eval('(' + xhr.responseText + ')');
											   if(res['success'] == true)
											   {
												   var s = document.getElementById('profile_shout_' + res['id']);
												   document.getElementById('shouts').removeChild(s);
											   }
											   else
											   {
												   alert('Delete failed. Refresh the page and try again.');
											   }
										   }
									   });
}

function addLinkForm(linkType)
{
	var sel;
	if(linkType == 'link')
	{
		sel = document.getElementById('addlink');
	}
	else if(linkType == 'im')
	{
		sel = document.getElementById('addim');
	}
	if(sel == null || sel.value == '')
	{
		return;
	}
	var url = '/ajax.php';
	var params = 'mode=profile&action=addlinkform&id=' + profileId.toString() + '&type=' + encodeURIComponent(sel.value);
	if(linkType == 'link')
	{
		startRequest(url,params,function()
									 {
										 if(xhr.readyState == 4)
										 {
											 var res = eval('(' + xhr.responseText + ')');
											 var div = document.getElementById('addlinkform');
											 document.getElementById('addimform').innerHTML = '';
											 div.innerHTML = res['form'];
										 }
									 });
	}
	else if(linkType == 'im')
	{
		startRequest(url,params,function()
									 {
										 if(xhr.readyState == 4)
										 {
											 var res = eval('(' + xhr.responseText + ')');
											 var div = document.getElementById('addimform');
											 document.getElementById('addlinkform').innerHTML = '';
											 
											 div.innerHTML = res['form'];
										 }
									 });
	}
}

function addLink()
{
	var type = document.getElementById('addlinktype');
	var netid = document.getElementById('network_id');
	var title = document.getElementById('link_url');
	if(type == null || netid == null)
	{
		return;
	}
	
	var url = '/ajax.php';
	var params = 'mode=profile&action=addlink&id=' + profileId.toString() + '&type=' + type.value + '&netid=' + encodeURIComponent(netid.value);
	if(title != null)
	{
		params += '&title=' + encodeURIComponent(title.value);
	}
	startRequest(url,params,function()
									 {
										 if(xhr.readyState == 4)
										 {
											 var res = eval('(' + xhr.responseText + ')');
											 
											 document.getElementById('addimform').innerHTML = '';
											 document.getElementById('addlinkform').innerHTML = '';
											 
											 document.getElementById('linklist').innerHTML = res['links'];
											 document.getElementById('imlist').innerHTML = res['im'];
										 }
									 });
	document.getElementById('floatbox').style.display = 'none';
}

function deleteLink(linkId)
{
	if(linkId < 1)
	{
		return;
	}
	
	if(!confirm('Are you sure you want to delete the selected link?'))
	{
		return;
	}
	var url = '/ajax.php';
	var params = 'mode=profile&action=deletelink&id=' + profileId.toString() + '&link=' + linkId.toString();
	startRequest(url,params,function()
									 {
										 if(xhr.readyState == 4)
										 {
											 var res = eval('(' + xhr.responseText + ')');
											 
											 document.getElementById('addimform').innerHTML = '';
											 document.getElementById('addlinkform').innerHTML = '';
											 
											 document.getElementById('linklist').innerHTML = res['links'];
											 document.getElementById('imlist').innerHTML = res['im'];
										 }
									 });
	document.getElementById('floatbox').style.display = 'none';
}

function deletePhoto(photo)
{
	if(confirm('Are you sure you want to delete this photo? This action can not be undone.'))
	{
		window.location = '../photos?action=delete&photo=' + photo;
	}
}

var counter = 0;
var lastSearch = '';
function friendSearch()
{
	var sb = document.getElementById('friendsearch');
	var d = new Date();
	
	if(sb.value == '')
	{
		counter = 0;
		lastSearch = '';
	}
	else if(sb.value != lastSearch)
	{
/*		counter = 1;
		lastSearch = sb.value;
	}
	else if(counter > 0 && counter < 1)
	{
		counter++;
	}
	else if(counter == 1)
	{
*/
		lastSearch = sb.value;
		counter = 0;
		var url = '/ajax.php';
		var params = 'mode=profile&action=friendsearch&id=' + profileId.toString() + '&string=' + encodeURIComponent(sb.value) + '&dummy=' + encodeURIComponent(d.getSeconds());
		
		var sr = document.getElementById('searchresults');
		//sr.innerHTML = '';
		sr.style.backgroundImage = 'url(\'/images/profile/spinner2-black.gif\')';
		sr.style.backgroundRepeat = 'no-repeat';
		sr.style.backgroundPosition = 'center';
		
		ajax(params,true,true,function()
										 {
											 if(xhr.readyState == 4)
											 {
												 try
												 {
													 var res = eval('(' + xhr.responseText + ')');
													 
													 var sr = document.getElementById('searchresults');
													 sr.innerHTML = res['results'];
													 sr.style.backgroundImage = '';
												 }
												 catch(e)
												 {
													 alert(xhr.responseText);
												 }
											 }
										 });
	}
	//window.setTimeout('friendSearch();',1000);
}

function addFriend(id)
{
	var url = '/ajax.php';
	var params = 'mode=profile&action=addfriend&id=' + profileId.toString() + '&friend=' + id.toString();
	startRequest(url,params,function()
									 {
										if(xhr.readyState == 4)
										{
											try
											{
												var res = eval('(' + xhr.responseText + ')');
												
												if(res['success'] == true)
												{
													window.location = location.pathname;
												}
												else
												{
													alert(res['response']);
												}
											}
											catch(e)
											{
											}
										}
									 });
}

function deleteFriend(id)
{
	if(!confirm('Are you sure you want to remove this friend?'))
	{
		return;
	}
	var url = '/ajax.php';
	var params = 'mode=profile&action=deletefriend&id=' + profileId.toString() + '&friend=' + id.toString();
	startRequest(url,params,function()
									 {
										if(xhr.readyState == 4)
										{
											try
											{
												var res = eval('(' + xhr.responseText + ')');
												
												if(res['success'] == true)
												{
													window.location = location.pathname;
												}
												else
												{
													alert(res['response']);
												}
											}
											catch(e)
											{
											}
										}
									 });
}