var z_login = 'false';

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}

	return null;
}

function SetCookie(cookieName,cookieValue,nDays) 
{
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}

function checkLogin()
{
	var username = readCookie("x_username");
	var password = readCookie("x_password");

	if (username == null || password == null){
		return;
	}
	
	if (username && password){
		z_login = 'true';
		return;
	}
}


function gobig(t, value)
{	
	t.style.width = '450px';
	t.style.height = '150px';
	t.style.color = '#000';
	
	if (t.value == value){
		t.value = '';	
	}
}

function clearv(e, o)
{
	e.style.color = '#000';
	
	if (e.value == o){
		e.value='';
	}
}


function hidePopup(id)
{
	var hidden = id + '-hidden';
	var popup = id + '-popup';
	
	$(popup).style.display = 'none';
	$(hidden).style.display = 'none';
}

function showPopup(id)
{
	var hidden = id + '-hidden';
	var popup = id + '-popup';
	
	offset = window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop;
	$(hidden).style.top = offset + 'px';
	$(hidden).style.display = '';
	$(popup).style.top = 100 + offset + 'px';
	$(popup).style.display = '';
}

function printError(msg, id)
{
	$(id).innerHTML = msg;
}

function sendmessage()
{	
	var e = 'send_message_error';
	
	if ($('to').value == ''){
		printError('you need to type in a username', e);
		return false;
	}
	
	
	if ($('subject').value == ''){
		printError('type in a subject', e);
		return false;
	}
	
	if ($('message').value == ''){
		printError('type in a message', e);
		return false;
	}
	
	$('sendmessageform').submit();
}


function savelink(id)
{
	// do vote
	new Ajax.Request("/ajax/link/do_save/" + id,
	{
		method: 'get',
		onSuccess: function(transport)
		{
			$('savelink-' + id).style.display = 'none';			
		}
	});
}

function editlink(id)
{
	hidden = 'editlink-' + id + '-hidden';
	popup = 'editlink-' + id + '-popup';
	
	offset = window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop;
	$(hidden).style.top = offset + 'px';
	$(hidden).style.display = '';
	$(popup).style.top = 100 + offset + 'px';
	$(popup).style.display = '';
}

function deletelink(id)
{
	hidden = 'deletelink-' + id + '-hidden';
	popup = 'deletelink-' + id + '-popup';
	
	offset = window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop;
	$(hidden).style.top = offset + 'px';
	$(hidden).style.display = '';
	$(popup).style.top = 100 + offset + 'px';
	$(popup).style.display = '';
}

function reportconf(id, y)
{
	if (y == 'no')
	{
		$('reportlink-' + id).style.display = 'inline';			
		$('reportconf-' + id).style.display = 'none';
		return;
	}
	else
	{
		new Ajax.Request("/ajax/link/do_report/" + id,
		{
			method: 'get',
			onSuccess: function(transport)
			{
				$('reportconf-' + id).style.display = 'none';
				$('reportlink-' + id).style.display = 'none';
			}
		});
	}
}

function reportlink(id)
{
	$('reportlink-' + id).style.display = 'none';			
	$('reportconf-' + id).style.display = 'inline';
}

function rate_object(id, rating)
{
	// do vote
	new Ajax.Request("/ajax/link/do_rate/" + id + "/" + rating,
	{
		method: 'get',
		onSuccess: function(transport)
		{
			$('rateform').innerHTML = transport.responseText;
		}
	});
}

function reportcommentconf(id, y)
{
	if (y == 'no')
	{
		$('reportcommentlink-' + id).style.display = 'inline';			
		$('reportcommentconf-' + id).style.display = 'none';
		return;
	}
	else
	{
		new Ajax.Request("/ajax/comment/do_report/" + id,
		{
			method: 'get',
			onSuccess: function(transport)
			{
				$('reportcommentconf-' + id).style.display = 'none';
				$('reportcommentlink-' + id).style.display = 'none';
			}
		});
	}
}

function reportcomment(id)
{
	$('reportcommentlink-' + id).style.display = 'none';			
	$('reportcommentconf-' + id).style.display = 'inline';
}

// comment add
function cadd()
{
	var message = $('message').value;
		
	if (message == 'enter comment' || message == ''){
		$('add_comment_error').innerHTML = 'please enter a comment';
		return;
	}
	
	$('commentform').submit();	
	
}


function replyedit(id)
{
	
}

function replycancel(id)
{
	$('commentreply-' + id).style.display = 'none';
}

function replyadd(id)
{
	var message = $('replymessage-' + id).value;
		
	if (message == 'enter comment' || message == ''){
		$('add_comment_error-' + id).innerHTML = 'please enter a comment';
		return;
	}
	
	$('commentform-' + id).submit();
}

function replyto(id)
{
	$('commentreply-' + id).style.display = 'block';
}

function editcomment(id)
{
	$('message-' + id).style.display = 'none';
	$('commentfooter-' + id).style.display = 'none';
	$('edit-' + id).style.display = 'block';
	$('edit-' + id).focus();
}

function editcancel(id)
{
	$('message-' + id).style.display = 'block';
	$('commentfooter-' + id).style.display = 'block';
	$('edit-' + id).style.display = 'none';
}

function doedit(id)
{
	var e = 'edit_comment_error-' + id;
	var message = $('editcomment-' + id).value;
		
	if (message == 'enter comment' || message == ''){
		printError('please enter a comment', e);
		return;
	}
	
	$('editform-' + id).submit();
}


/* comments */

function voteupcomment(id)
{
	// check if user is logged in
	if (z_login == 'false'){
		showLoginForm();
		return;
	}

	// do vote
	new Ajax.Request("/ajax/comment/up_vote/" + id,
	{
		method: 'get',
		onSuccess: function(transport){
			var votes = transport.responseText;

			if (votes == 'up'){
				removeVote(id);
				return;
			}
			
			// update vote count		
			$('cvotes' + id).innerHTML = votes;
			
			// update image
			$('cmodup' + id).src = '/images/mod_up.png';
			$('cmoddown' + id).src = '/images/down.png';
			
		}
	});
}

function votedowncomment(id)
{
	if (z_login == 'false'){
		showLoginForm();
		return false;
	}
	
	new Ajax.Request("/ajax/comment/down_vote/" + id,
		{
			method: 'get',
			onSuccess: function(T){
				var votes = T.responseText;
				
				if (votes == 'down'){
					removeVote(id);
					return;
				}
				
				$('cvotes' + id).innerHTML = votes;
								
				// if voted up, return up arrow to normal
				$('cmodup' + id).src = '/images/up.png'
				$('cmoddown' + id).src = '/images/mod_down.png'
			}
		}
	);
}

function shareLink()
{	
	var e = 'shareerror';
	
	if ($('to').value == ''){
		printError('you need to type in an email', e);
		return false;
	}
	
	
	if ($('from').value == ''){
		printError('type in your name', e);
		return false;
	}
	
	var v = $('message').value;
	
	if (v == ''){
		printError('type in a message', e);
		return false;
	}
	
	$('commentform').submit();
		
}