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 getTextCookie(name)
{
    var val = $.cookie(name);
    if (val == null) return '';
    val = val.replace(/\+/g, "%20")
    return unescape(val);
}

function userLoggedIn()
{	
  if ($.cookie('LoginUserId') && $.cookie('LoginLoggedIn'))
  	return 1;
  else
  	return 0;
}

function hasYEProfile()
{
	if ($.cookie('LoginYEProfile'))
		return 1;
	else
		return 0;
}

function hasWebmail()
{
	if ($.cookie('LoginWebmail'))
		return 1;
	else
		return 0;
}

function getFullName()
{
	if ($.cookie('LoginUserFullName'))
		return unescape($.cookie('LoginUserFullName').replace(/\+/g, " ")).substr(0, 19);
	else
		return '';
}

function getUser()
{
	return $.cookie('LoginUserId');
}

function getUserEmail()
{
	return $.cookie('LoginUserEmail');
}

function getUserGender()
{
	if ($.cookie('LoginUserGender') && $.cookie('LoginUserGender') == 'F')
	{
		return 'F';
	}
	else
	{
		return 'M';
	}
}

function getAgeGroup()
{
	if ($.cookie('LoginUserAgeGroup'))
	{
		return $.cookie('LoginUserAgeGroup');
	}
	else
	{
		return 'default';
	}
}

function getSTH()
{
	if ($.cookie('LoginUserSTH'))
	{
		return 'y';
	}
	else
	{
		return 'n';
	}
}

function getNotificationCount()
{
    return $.cookie('ye_not_cnt');
}

function loginError()
{
    //alert(getTextCookie('LoginError'));
    return (unescape(getTextCookie('LoginError')));
}

function replaceUserEmail()
{
	if (userLoggedIn())
	{
		$('.user_logged_email_replace').html(getUserEmail());
	}
}

function replaceUserFullName()
{
	if (userLoggedIn())
	{
		$('.user_logged_full_name_replace').html(getFullName());
	}
}

function replaceYEProfileURLView()
{
	if (userLoggedIn() && hasYEProfile())
	{
		$('.user_logged_ye_profile_replace').attr('href', '/youreverton/?id=' + getUser() + '&tab=0');
	}
}

function replaceYEWebmailURL()
{
	if (userLoggedIn())
	{
		$('#user_ye_webmail_url').attr('href', '/youreverton/?id=' + getUser() + '&tab=6');
	}
}

function replaceYEProfileURLEdit()
{
	if (userLoggedIn() && hasYEProfile())
	{
		$('.user_logged_ye_profile_replace').attr('href', '');
	}
}

function replaceYEProfileNotificationCount()
{
    if (userLoggedIn() && hasYEProfile())
    {
    	if (getNotificationCount() == 0)
    	{
    		$('#user_ye_notification_holder').hide();
    	}
    	else
    	{
    	    $('#user_ye_notification_count').html(getNotificationCount());
    	}
    }
    else
    {
    	$('#user_ye_notification_holder').hide();
    }
}

function determineLoginBox()
{
	if (userLoggedIn())
    {		
		$('.user_logged_in_no').hide();
    	$('.user_logged_in_yes').show();
    }	
}

function determineLoginBoxYE()
{
	if (userLoggedIn() && hasYEProfile())
	{
		$('#user_logged_ye_no').hide();
		$('#user_logged_ye_yes').show();
	}
	else if (userLoggedIn)
	{
		$('#user_logged_ye_no').show();
		$('#user_logged_ye_yes').hide();
	}
}

function determineProfileBtn()
{
	if (userLoggedIn() && hasYEProfile() == 0)
	{
		$('#user_ye_profile_btn').html('Create Profile');
	}
}

function determineLoginBoxMail()
{
	if (userLoggedIn() && hasWebmail())
	{
		$('#user_logged_webmail_yes').show();
	}
	else if (userLoggedIn)
	{
		$('#user_logged_webmail_yes').hide();
	}
}

function determineAddMyArticles()
{
	if (userLoggedIn() && hasYEProfile())
	{
		$('#user_logged_my_articles_no').hide();
		$('#user_logged_my_articles_yes').show();
	}
}

function determineCommentForm()
{
	if (userLoggedIn())
	{
			$('#user_logged_can_comment_yes').show();
			$('#user_logged_can_comment_no').hide();
	}
}

function determineCommentYEFriends()
{
	//if ($('#user_logged_can_comment_yes') && userLoggedIn())
	{
		var queryString = '';

		$("p[id^='commentye_']").each(function(i) {
		 	var commentYE		= $(this).attr("id");
		 	var commentName = commentYE.split("_");

		 	queryString += commentName[1]+';';
		});
		$.get("/scripts/ajax/comments_ye_friend.scr.php", { 'friends' : queryString },
		 		function(data) {
		 		    var comments = data.split(";");
		 		    if(comments && comments.length)
		 		    {
		 		       for(i = 0; i < comments.length; i++)
		 		       {
		 		           var comment = comments[i].split(":");
		 		           var output = '';

		 		           // for these constants see the script called above
		 		           if(comment[1] == 1)
		 		               output = '<a href="' + '/scripts/sn/mates.php?action=add&id=' + comment[0] + '">Add To Mates</a>';
		 		           else if(comment[1] == 2)
		 		               output = 'Mate Request Pending';
		 		           else if(comment[1] == 3)
		 		               output = '<a href="/youreverton/?tab=0&id=' + comment[0] + '">View Mates Profile</a>';

		 		           var o = $('#commentye_'+comment[0]);
	 		               o.html(output);

		 		       }
		 		    }
		 		}, "html");
   	}
}

function determineComments()
{
    if (userLoggedIn())
    {
		var NotLoggedIn = document.getElementById('loginmsg');
        var LoggedIn = document.getElementById('mycommscontainer');
		//document.write(getTextCookie('LoginUserFullName'));
		if (document.getElementById('commname'))
			document.getElementById('commname').innerHTML = getTextCookie('LoginUserFullName');
		//document.getElementById('commemail').innerHTML = getTextCookie('LoginLoggedIn');

        NotLoggedIn.style.display = 'none';
        LoggedIn.style.display = '';


    }
}

function determineAuctionPayments()
{
  if (userLoggedIn())
  {
	  $.getJSON("/scripts/ajax/auctionPayments.scr.php", { userId : getUser() }, function (response) {
		  
		  if (response.length > 0)
		  {
			$("#eauction_payment_item").html(response[0].title);
			$("#eauction_payment_link").attr("href", "https://www.evertonfc.com/eauction/?mode=payment&paymentId=" + response[0].payment_id);
			  
			$(".user_logged_in_yes").hide();
			$("#eauction_payments").show();  
		  }
		  
		  // if response
		  	// set auction fields
	  		// display auction link
	  
	  });
  }
}

function replaceTotalThumbs(pageId)
{
	if ($('.replace_total_thumbs_up'))
	{
		$.get("/scripts/ajax/thumbs.scr.php", { page_id: pageId },
			function(data) {
				$('.replace_total_thumbs_up').html(data);
			}, "html");
	}
}

function placeCommentThumb(commentId, thumbCurrent)
{
  $.get("/scripts/ajax/commentthumbup.scr.php", { cid: commentId, thumbs: thumbCurrent },
  function(data) {
  	$('#comment_thumb_' + commentId).html(data);
  }, "html");
}

function determineCommentThumbs(pid)
{
    $.get("/scripts/ajax/comment_thumbs.scr.php", { 'pid' : pid },
      function(data) {

          var comments = data.split(';');

          if(comments && comments.length)
          {
              for(i = 0; i < comments.length; i++)
              {
                  var comment = comments[i].split(':');

                  var o = $('#comment_thumb_'+comment[0]);

                  if(o && o.html)
                  {
                      var output = '<span onclick="javascript:placeCommentThumb(\'';
                          output += comment[0] + '\', ';
                          output += '\'' + comment[1] + '\');">';

                      if (comment[1] > 0)
                      	output += '+ ';

                     			output += comment[1];
                          output += '</span>';

                      o.html(output);
                  }
              }
          }

      }, "html");
}
