// Function retrieved from: http://cass- hacks.com/articles/code/js_url_encode_decode/   
function URLEncode (clearString) {   
  var output = '';   
  var x = 0;   
  clearString = clearString.toString();   
  var regex = /(^[a-zA-Z0-9_.]*)/;   
  while (x < clearString.length) {   
    var match = regex.exec(clearString.substr(x));   
    if (match != null && match.length > 1 && match[1] != '') {   
        output += match[1];   
      x += match[1].length;   
    } else {   
      if (clearString[x] == ' ')   
        output += '+';   
      else {   
        var charCode = clearString.charCodeAt(x);   
        var hexVal = charCode.toString(16);   
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();   
      }   
      x++;   
    }   
  }   
  return output;   
}   

// Retrieved from: http://www.htmlcodetutorial.com/forms/index_famsupp_157.html   
function SubmitOnEnter(myfield, e)   
{   
    var keycode;   
    if (window.event)   
        keycode = window.event.keyCode;   
    else if (e)   
        keycode = e.which;   
    else  
        return true;   
    if (keycode == 13)   
    {   
        SearchSite();   
        return false;   
    }   
    else  
        return true;   
}   


function SearchSite()   
{   
document.location.href='http://guildtag.com/pages/google.aspx?cx=partner-pub-0115174733771851%3Ajz36qt-a1ra&cof=FORID:9&ie=UTF-8&q=' + URLEncode(document.getElementById('q').value) + '&sa=Search';   
}  

function SetFocus(x) {
    document.getElementById(x).focus();
}

function imagesize(x, minheight, minwidth, maxheight, maxwidth) {
if (document.getElementById(x).style.position != 'absolute') {
var left = (document.body.clientWidth - maxwidth - 20) / 2;
document.getElementById(x).style.position = 'absolute';
document.getElementById(x).style.border = '10px solid #666';
document.getElementById(x).style.width = maxwidth + 'px';
document.getElementById(x).style.height = maxheight + 'px';
document.getElementById(x).style.top = '0px';
document.getElementById(x).style.left = left + 'px';
}
else {
document.getElementById(x).style.position = '';
document.getElementById(x).style.border = '';
document.getElementById(x).style.width = minwidth + 'px';
document.getElementById(x).style.height = minheight + 'px';
}
}

function Search() {
var y = document.getElementsByTagName('*');
for (var i = 0; i < y.length; i++) {
if (y[i].id.indexOf('btn_quick_search') != -1) {
y[i].disabled = false;
break;
}
}
for (var i = 0; i < y.length; i++) {
if (y[i].id.indexOf('txt_quick_search') != -1) {
y[i].value = '';
break;
}
}
}


function show(x) {
    var posx = 0, posy = 0;
    if (e == null) e = window.event;
    if (e.pageX || e.pageY) {
        posx = e.pageX; posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        if (document.documentElement.scrollTop) {
            posx = e.clientX + document.documentElement.scrollLeft;
            posy = e.clientY + document.documentElement.scrollTop;
        }
        else {
            posx = e.clientX + document.body.scrollLeft - 20 + 'px';
            posy = e.clientY + document.body.scrollTop + 5 + 'px';
        }
    }

    document.getElementById(x).style.display = 'block';
    document.getElementById(x).style.top = posy;
    document.getElementById(x).style.left = posx;
}

function hide(x) {
    document.getElementById(x).style.display = 'none';
}

function display(x) {
    var y = document.getElementsByTagName('div');
    for (var i = 0; i < y.length; i++) {
        if (y[i].id.substr(0, 5) == 'info_' && y[i].style.display == 'block')
            y[i].style.display = 'none';
    }
    if (x != 'info_') {
        if (document.getElementById(x).style.display == 'block')
            document.getElementById(x).style.display = 'none';
        else
            document.getElementById(x).style.display = 'block';
    }
}


function ActivitySayCreate(friend_user_id) {

    var comment = $('#activity_say_txt').val();
    comment = comment.replace(/'/g, "\\'");

    $.ajax({
        type: "POST",
        url: "/api/api.asmx/ActivitySayCreate",
        contentType: "application/json; charset=utf-8",
        data: "{ 'friend_user_id':'" + friend_user_id + "','comment':'" + comment + "' }",
        dataType: "json",
        success: AjaxSucceeded,
        error: AjaxFailed
    });

    function AjaxSucceeded(result) {
        if (result.d == "success") {
            $('#activity_say_container').hide();
            //$('#activity_say_success').fadeIn(30).fadeOut(30).fadeIn(30).fadeOut(30).fadeIn(30);
            $('#activity_say_success').show().fadeTo(200, 0.3).fadeTo(200, 1).fadeTo(200, 0.3).fadeTo(200, 1).fadeTo(200, 0.3).fadeTo(200, 1);
        }
        else if (result.d == "") {
            alert('Please wait a moment before making another comment.');
        }
    }
    function AjaxFailed(result) {
        alert(result.status + ' ' + result.statusText);
    }
}

function ToggleActivityComment(activity_id) {
    if ($('#activity_comment_create_' + activity_id).is(':visible'))
        $('#activity_comment_create_' + activity_id).hide();
    else {
        $('#activity_comment_create_' + activity_id).show();
        $('#activity_comment_create_txt_' + activity_id).focus();

    }
}

function ActivityCommentCreate(activity_id) {

			var comment = $('#activity_comment_create_txt_' + activity_id).val();
			comment = comment.replace(/'/g, "\\'");
			
			$.ajax({
				type: "POST",
				url: "/api/api.asmx/ActivityCommentCreate",
				contentType: "application/json; charset=utf-8",
				data: "{ 'activity_id':'" + activity_id + "','comment':'" + comment + "' }",
				dataType: "json",
				success: AjaxSucceeded,
				error: AjaxFailed
			});

			function AjaxSucceeded(result) {
				$('#activity_comment_container_' + activity_id).prepend(result.d);
				$('#activity_comment_create_' + activity_id).hide();
			}
			function AjaxFailed(result) {
				//alert(result.status + ' ' + result.statusText);
			}
		}

		function ToggleActivityComment(activity_id) {
			if ($('#activity_comment_create_' + activity_id).is(':visible'))
				$('#activity_comment_create_' + activity_id).hide();
			else {
				$('#activity_comment_create_' + activity_id).show();
				$('#activity_comment_create_txt_' + activity_id).focus();

			}
		}

	function ActivityDelete(activity_id) {
			$("#activity_id_" + activity_id).addClass("ghost");

			$.ajax({
				type: "POST",
				url: "/api/api.asmx/ActivityDelete",
				contentType: "application/json; charset=utf-8",
				data: "{ 'activity_id':'" + activity_id + "' }",
				dataType: "json",
				success: AjaxSucceeded,
				error: AjaxFailed
			});

			function AjaxSucceeded(result) {
				$("#activity_id_" + activity_id).hide();
			}
			function AjaxFailed(result) {
			    alert(result.status + ' ' + result.statusText);
			}
		}
		
		function FriendRequest(friend_id) {
			$("#profile_user_friend_request").addClass("ghost");

			$.ajax({
				type: "POST",
				url: "/api/api.asmx/FriendRequest",
				contentType: "application/json; charset=utf-8",
				data: "{ 'friend_id':'" + friend_id + "' }",
				dataType: "json",
				success: AjaxSucceeded,
				error: AjaxFailed
			});

			function AjaxSucceeded(result) {
			    $("#profile_user_friend_request").children().remove();
			    $("#profile_user_friend_request").append(result.d);
			}
			function AjaxFailed(result) {
			    $("#profile_user_friend_request").children().remove();
			    $("#profile_user_friend_request").append('Error...');
			}
		}