/*
	Title: Comment Engine (Social Tools) JavaScript [Version 1]
	Description: JavaScript 
	Author: James Hartcher
	Created: 16/10/2008
	
	Copyright © 2008 Internet Design Studios Pty Ltd, All Rights Reserved
	www.idstudios.com.au
*/

// Toggle Reply (Method)
function ToggleReply(id) {

	// Load Controls
	if (document.getElementById('ReplyControls'+id).innerHTML == '') {
	
		// Prepare Form HTML
		ReplyControls = document.getElementById('PostComment').innerHTML;

		// Swap Elements			
		ReplyControls = ReplyControls.replace('AddComment()', 'AddComment('+id+')');
		ReplyControls = ReplyControls.replace('PreviewComment()', 'PreviewComment('+id+')');
		ReplyControls = ReplyControls.replace('CommentForm', 'CommentForm'+id);
		
		// Insert HTML
		document.getElementById('ReplyControls'+id).innerHTML = ReplyControls;
		
		// Set Variables
		document.getElementById('CommentForm'+id).ParentID.value = id;
		document.getElementById('CommentForm'+id).Comment.value = '';
		
	}
	
	// Toggle Visibility
	if (document.getElementById('ReplyControls'+id).style.display == 'block') {
		document.getElementById('ReplyControls'+id).style.display = 'none';
		document.getElementById('ReplyControls'+id).innerHTML = '';
	} else {
		document.getElementById('ReplyControls'+id).style.display = 'block';
	}
	
	return false;
}

// Toggle Edit (Method)
function ToggleEdit(id) {

	// Load Controls
	if (document.getElementById('ReplyControls'+id).innerHTML == '') {
	
		// Prepare Form HTML
		ReplyControls = document.getElementById('PostComment').innerHTML;

		// Swap Elements			
		ReplyControls = ReplyControls.replace('AddComment()', 'AddComment('+id+')');
		ReplyControls = ReplyControls.replace('PreviewComment()', 'PreviewComment('+id+')');
		ReplyControls = ReplyControls.replace('CommentForm', 'CommentForm'+id);
		
		// Insert HTML
		document.getElementById('ReplyControls'+id).innerHTML = ReplyControls;
		
		// Set Variables
		document.getElementById('CommentForm'+id).ParentID.value = id;
		document.getElementById('CommentForm'+id).Comment.value = '';
		
	}
	
	// Toggle Visibility
	if (document.getElementById('ReplyControls'+id).style.display == 'block') {
		document.getElementById('ReplyControls'+id).style.display = 'none';
		document.getElementById('ReplyControls'+id).innerHTML = '';
	} else {
		document.getElementById('ReplyControls'+id).style.display = 'block';
	}
	
	return false;
}


// Add Comment (Save) (Method)
function AddComment(id) {

	// Find Comment Form
	if (id != null) {
		UserComment = document.getElementById('CommentForm'+id);	
	} else {
		UserComment = document.getElementById('CommentForm');
	}

	// Find User Details
	UserDetails = UserComment.getElementsByTagName('div')[0];
	UserPassword = UserComment.getElementsByTagName('div')[3];


	// ----------------------------------------------------------
	// Check for Comment
	// ----------------------------------------------------------
	if (UserComment.Comment.value == '') {
		
		// Error
		alert('Sorry, you need to type in a comment');
		return false;


	// ----------------------------------------------------------
	// Check User Details
	// ----------------------------------------------------------
	} else if ((UserComment.ProfileReference.value == '') && (UserComment.EMail.value == '' || UserComment.NickName.value == '')) {
		
		// Toggle User Details
		UserDetails.style.display = 'block';
		return false;


	// ----------------------------------------------------------
	// Check User Details
	// ----------------------------------------------------------
	} else if (UserComment.ProfileReference.value == '') {

	$.post(ajaxPrefix + "includes/tools/comments/checkemail.aspx", { email: UserComment.EMail.value, password: UserComment.Password.value },
                function (data) {
                    if (data == 'OK') {

                        // Success
                        SaveComment(id, UserComment);
                        return true;

                    } else {

                        // Toggle Password
                        UserDetails.style.display = 'block';
                        UserPassword.style.display = 'block';
                        //alert(UserComment.EMail.value);
                        //alert(UserComment.Password.value);
                        //alert(data);
                        return false;

                    }
                });


	// ----------------------------------------------------------
	// Success
	// ----------------------------------------------------------
	} else {
		
		// Success
		SaveComment(id, UserComment);
		UserComment.Comment.value = '';
		
	}
}


// Save Comment (Method)
function SaveComment(id, UserComment) {

	// Find User Details
	UserDetails = UserComment.getElementsByTagName('div')[0];
	UserPassword = UserComment.getElementsByTagName('div')[3];

	// Remember Details
	if (UserComment.ProfileReference.value == '') {
		document.getElementById('CommentForm').EMail.value = UserComment.EMail.value;
		document.getElementById('CommentForm').NickName.value = UserComment.NickName.value;
	}

	// Save Comment
	if (id != null) {

		// Save Reply
        $.post(ajaxPrefix + "includes/tools/comments/savecomment.aspx", {
				    ContentType: UserComment.ContentType.value,
				    ContentID: UserComment.ContentID.value,
				    ParentID: UserComment.ParentID.value,
				    ProfileReference: UserComment.ProfileReference.value,
				    NickName: UserComment.NickName.value,
				    EmailAddress: UserComment.EMail.value,
				    Password: UserComment.Password.value,
				    Comment: UserComment.Comment.value
                },
                function(html){
                    $("#Replies"+id).prepend(html);
                });

		// Hide Inline Form
		ToggleReply(id)

	} else {

		// Save Comment
        $.post(ajaxPrefix + "includes/tools/comments/savecomment.aspx", {
				    ContentType: UserComment.ContentType.value,
				    ContentID: UserComment.ContentID.value,
				    ProfileReference: UserComment.ProfileReference.value,
				    NickName: UserComment.NickName.value,
				    EmailAddress: UserComment.EMail.value,
				    Password: UserComment.Password.value,
				    Comment: UserComment.Comment.value
                },
                function(html){
                    $("#AddComment").before(html);
                });

	};
	
	// Clear Data
    UserComment.Comment.value = ''
	UserDetails.style.display = 'none';
		
}


// More Comments (Method)
function ShowAllComments(id) {

	// Find Comment Form
	if (id != null) {
		UserComment = document.getElementById('CommentForm'+id);	
	} else {
		UserComment = document.getElementById('CommentForm');
	}
    
    // Show Loading...
    $("#CommentsShowAll").html('Loading comments.. please wait');
    
	// Get Comments
    $.post(ajaxPrefix + "includes/tools/comments/getcomments.aspx", {
			    ContentType: UserComment.ContentType.value,
			    ContentID: UserComment.ContentID.value
            },
            function(html){
                $(".ShowAll").before(html);
                $(".ShowAll").hide();
            });

    // Return False    
    return false;

}


// Change Visibility (if moderator)
function HideComment(commentID) {

    // Moderate Comment
    $.post(ajaxPrefix + "includes/tools/comments/moderate.aspx", { commentID: commentID, Visibility: 'Hide' },
            function(data){
		        if (data == 'OK') {
                    $("#Comment"+commentID).hide();
		        }
            });
            
    return false;

}
