/*
	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;
}


// 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 == '') {

		new Ajax.Request('../includes/Tools/Comments/CheckEmail.asp', {
			method: 'post',
			parameters: {
				EMail:		UserComment.EMail.value,
				Password:	UserComment.Password.value
			},
			onSuccess: function(objXML) {
				if (objXML.responseText == 'OK') {

					// Success
					SaveComment(id, UserComment);
					return true;
					
				} else {

					// Toggle Password
					UserPassword.style.display = 'block';
					return false;
					
				}
			}			 
		});


	// ----------------------------------------------------------
	// Success
	// ----------------------------------------------------------
	} else {
		
		// Success
		SaveComment(id, UserComment)
		
	}
}


// 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
		new Ajax.Updater('Replies'+id, '../includes/Tools/Comments/SaveComment.asp', {
			method: 'post',
			parameters: {
				ContentType: UserComment.ContentType.value,
				ContentID: UserComment.ContentID.value,
				ParentID: UserComment.ParentID.value,
				ProfileReference: UserComment.ProfileReference.value,
				NickName: UserComment.NickName.value,
				EMail: UserComment.EMail.value,
				Password: UserComment.Password.value,
				Comment: UserComment.Comment.value
			},
			insertion: Insertion.Bottom,
			onComplete: function(transport) {
				$('Comments').select('li').each (
					function(el) {
						if (el.style.display == 'none') { Effect.Appear(el, { duration: 1.5 }) }
					}
				)
			}
		});

		// Hide Inline Form
		ToggleReply(id)

	} else {

		// Save Comment
		new Ajax.Updater('AddComment', '../includes/Tools/Comments/SaveComment.asp', {
			method: 'post',
			parameters: {
				ContentType: UserComment.ContentType.value,
				ContentID: UserComment.ContentID.value,
				ProfileReference: UserComment.ProfileReference.value,
				NickName: UserComment.NickName.value,
				EMail: UserComment.EMail.value,
				Password: UserComment.Password.value,
				Comment: UserComment.Comment.value
			},
			insertion: Insertion.Before,
			onComplete: function(transport) {
				$('Comments').select('li').each (
					function(el) {
						if (el.style.display == 'none') { Effect.Appear(el, { duration: 1.5 }) }
					}
				)
			}
		}); 

	};
	
	// Clear Data
	UserDetails.style.display = 'none';
		
}



