// JavaScript Document
$.ajaxSetup ({  
    cache: false  
});

function checkIfLoggedIn()
{
	var result = true;
	$('#messageloader').load('ajaxcallpass.php?function=checkifloggedin', null, function() {
		var retval = document.getElementById('messageloader').innerHTML;																																																																								
		document.getElementById('messageloader').innerHTML = '';
		if(retval == 'nologin')
		{
			window.location = 'login';
			result = false;
		}
	});
	return result;
}

function detectIE()
{
	var browserName=navigator.appName;
    if (browserName=="Microsoft Internet Explorer")
	{
		return true;
	} else {
		return false;
	}
}

function isInteger(val)
{
    if(val==null)
    {
        return false;
    }
    if (val.length==0)
    {
        return false;
    }
    for (var i = 0; i < val.length; i++) 
    {
        var ch = val.charAt(i);
        if (i == 0 && ch == "-")
        {
            continue;
        }
        if (ch < "0" || ch > "9")
        {
            return false;
        }
    }
    return true;
}

function checkLogin()
{
	displayLoginBox(false);
        var result = true;
	if(document.getElementById('username').value == '')
	{
		$('#messagebox').load('messagegenerator.php?message=26');
		result = false;
	} else {
		$('#messageloader').load('ajaxcallpass.php?function=authenticate&username='+document.getElementById('username').value+'&password='+MD5(document.getElementById('password').value), null, function() {
			var retvals = document.getElementById('messageloader').innerHTML;
			//alert(retvals);
			retvals = retvals.split(",");
			retcode = parseInt(retvals[0]);
			document.getElementById('messageloader').innerHTML = '';
			if(retcode == 1)
			{
				var destpage = retvals[1];
				window.location = destpage;
			} else {
				$('#messagebox').load('messagegenerator.php?message='+retcode);
			}});
	}
	return result;
}

function displayLoginBox(booldisplay)
{
    if(booldisplay)
    {
        document.getElementById('loginpanel').style.display = '';
        document.getElementById("grayBG").style.display="block";
    } else {
        document.getElementById('loginpanel').style.display = 'none';
        document.getElementById("grayBG").style.display="none";
    }
}

function displayUsersFoundHelpful(postid)
{
    document.getElementById('lightbox_content').innerHTML = document.getElementById(postid+'_likes').innerHTML;
    document.getElementById('lightbox').style.display="block";
    document.getElementById("grayBG").style.display="block";
}

function closeLightbox()
{
    document.getElementById('lightbox_content').innerHTML = '';
    document.getElementById('lightbox').style.display="none";
    document.getElementById("grayBG").style.display="none";
}

function changeUserHelpfulStatus(postid, ajaxcommand)
{
    if(ajaxcommand == 'markhelpful')
        {
            var linktext = "Unhelpful";
            var newcommand = 'markunhelpful';
        } else {
            var linktext = "Helpful";
            var newcommand = 'markhelpful';
        }
    $('#messageloader').load('ajaxcallpass.php?function='+ajaxcommand+'&postid='+postid, null, function() {
        $('#helpful_'+postid).load('ajaxcallpass.php?function=displaylikes&postid='+postid, null, function() {
            if (document.getElementById('mosthelpful_'+postid)) {
                $('#mosthelpful_'+postid).load('ajaxcallpass.php?function=displaylikes&postid='+postid);
            }
            $('#forum_mosthelpful').load('ajaxcallpass.php?function=gethelpfulpost');
            document.getElementById('helpful_link_'+postid).innerHTML = linktext;
            document.getElementById('helpful_link_'+postid).onclick = function() { changeUserHelpfulStatus(postid, newcommand); }
        });
    });
}

function showPostReplyField(postid)
{
    document.getElementById('add_reply_text_'+postid).style.display = '';
    $('.add_reply_text').hide();
    $('#add_reply_text_'+postid).show();
}

function savePostReply(postid)
{
    var replytext = document.getElementById('reply_post_'+postid).value;
    if(replytext != "")
        {
    $('#messageloader').load('ajaxcallpass.php?function=savereplypost&postid='+postid+'&replytext='+escape(replytext), null, function() {
        
        if(document.getElementById('reply_control_'+postid))
            {
        if(document.getElementById('reply_control_'+postid).innerHTML == '')
        {
           var showall = 'Y';   
        } else {
           var showall = 'N';
        }
            }
        $('#forum_replies_'+postid).load('ajaxcallpass.php?function=refreshreplies&postid='+postid+'&show_replies='+showall, null, function() {
             $('.add_reply_text').hide();
        });
    });
        }
}

function showAllReplies(postid)
{
    document.getElementById('old_posts_'+postid).style.display = '';
    document.getElementById('reply_control_'+postid).innerHTML = '';
}

$(".add_reply_text").focusout(function()
{
    $(this).parent().get(0).fadeOut();
    $(".reply_inputbox").hide();
} );

function saveDiscussion()
{
    var disctext = document.getElementById('discussion_inputbox').value;
    var disctitle = document.getElementById('discussiontitle_inputbox').value;
    if(disctext != "" && disctitle != "")
        {
    $('#messageloader').load('ajaxcallpass.php?function=savediscussion&disctext='+escape(disctext)+'&disctitle='+escape(disctitle), null, function() {
        $('#forumbody').load('ajaxcallpass.php?function=refreshforum', null, function() {
            $('#create_discussion_form').hide();
        });
        $('#forum_recentpost').load('ajaxcallpass.php?function=getrecentpost');
        $('#forum_mosthelpful').load('ajaxcallpass.php?function=gethelpfulpost');
    });
        }
}

function deletePost(postid)
{
   $('#messageloader').load('ajaxcallpass.php?function=deletepost&postid='+postid, null, function() {
        var replyto = document.getElementById('messageloader').innerHTML;
        if(replyto == '0')
        {
             if (document.getElementById('recent_post_block_'+postid)) {
                $('#forum_recentpost').load('ajaxcallpass.php?function=getrecentpost');
             }
             if (document.getElementById('helpful_post_block_'+postid)) {
                $('#forum_mosthelpful').load('ajaxcallpass.php?function=gethelpfulpost');
             }
             document.getElementById('post_block_'+postid).innerHTML = '';
        } else {
             document.getElementById('reply_block_'+postid).innerHTML = '';
             var showall = 'Y';
             if(document.getElementById('reply_control_'+postid).innerHTML == '')
             {
                var showall = 'Y';
             } else {
                var showall = 'N';
             }
             $('#forum_replies_'+replyto).load('ajaxcallpass.php?function=refreshreplies&postid='+replyto+'&show_replies='+showall);
        }
   });
}

function editPost(postid, boxpost)
{
    var prefix = '';
    if(boxpost) prefix = 'boxpost_';
    document.getElementById(prefix+'post_content_'+postid).style.display = 'none';
    document.getElementById(prefix+'editpost_container_'+postid).style.display = '';
}

function saveEditedPost(postid, boxpost)
{
    var prefix = '';
    if(boxpost) prefix = 'boxpost_';
    var postcontent = document.getElementById(prefix+'edit_post_input_'+postid).value;
    if(postcontent != "")
    {
        $('#messageloader').load('ajaxcallpass.php?function=editpost&postid='+postid+'&postcontent='+escape(postcontent), null, function() {
            document.getElementById(prefix+'post_content_'+postid).innerHTML = postcontent;
            document.getElementById(prefix+'post_content_'+postid).style.display = '';
            document.getElementById(prefix+'editpost_container_'+postid).style.display = 'none';
            $('#messageloader').innerHTML = '';
        });
    }
}

function cancelEditPost(postid, boxpost)
{
    var prefix = '';
    if(boxpost) prefix = 'boxpost_';
    document.getElementById(prefix+'post_content_'+postid).style.display = '';
    document.getElementById(prefix+'editpost_container_'+postid).style.display = 'none';
}

function showDiscussionForm()
{
    $('#create_discussion_form').fadeIn();
    document.getElementById('discussiontitle_inputbox').value = '';
    document.getElementById('discussion_inputbox').value = '';
}

function cancelDiscussion()
{
    $('#create_discussion_form').fadeOut();
    document.getElementById('discussiontitle_inputbox').value = '';
    document.getElementById('discussion_inputbox').value = '';
}

function showQuestionForm()
{
    $('#ask_question_form').fadeIn();
}

function deleteQuestion(quesid)
{
    if(confirm('Are you sure you want to delete this question?'))
        {
    $('#ansrow_'+quesid).fadeOut();
    $('#messageloader').load('adminajax.php?function=deletequestion&quesid='+quesid);
        }
}

function cancelNewQuestion()
{
    $('#ask_question_form').fadeOut();
    document.getElementById('question').value = '';
}
