/*--------------------------------------------------------------------*\
| This file is part of Videocore 3.1                                  *|
|---------------------------------------------------------------------*|
| Copyright (C) 2008 Silversoft Solutions Ltd. All Rights Reserved    *|
| This file may not be redistributed in whole or significant part     *|
| Videocore is not Free Software | http://www.videocore.com           *|
|-------------------------------------------------------------------- *|
| For more information see http://www.videocore.com/license.html      *|
\*--------------------------------------------------------------------*/

var User =
{
	SendMessage: function ()
	{
		// ##### FORM ELEMENT #####
		var form = get_element ('form[message]');
	
		// ##### GET ALL FIELDS OF THE FORM #####
		var inputs = form.getElementsByTagName ('input');
		var textareas = form.getElementsByTagName ('textarea');
		
		// ##### LOOP THROUGH ALL OF THE INPUTS #####
		for (var input = 0; input < inputs.length; ++input)
		{
			if (inputs[input].value.length == 0)
			{
				Dialog.Error('Please complete the form before sending the message.');
				return false;
			}
		}
		
		// ##### CHECK MESSAGE #####
		if (textareas[0].value.length == 0)
		{
			Dialog.Error('Please complete the form before sending the message.');
			return false;
		}
		
		// ##### SUCCESSFUL #####
		form.submit();
	},

	MessageReply: function ()
	{
		// ##### FORM ELEMENT #####
		var form = get_element ('form[reply]');

		// ##### GET ALL FIELDS OF THE FORM #####
		var textareas = form.getElementsByTagName ('textarea');

		// ##### CHECK REPLY #####
		if (textareas[0].value.length == 0)
		{
			Dialog.Error('Please complete the form before replying to this message.');
			return false;
		}

		// ##### MAKE AJAX CALL #####
		load_ajax (BASE_URL + 'user.php?ajax=1&do=message_reply&message_id=' + get_element('message_id').value + '&reply=' + encodeURIComponent(textareas[0].value), 'User._messageReplyReturned');
	},

	_messageReplyReturned: function (array)
	{
		// ##### CHECK FOR ERROR #####
		if (ajax_returned_error (array))
		{
			return false;
		}

		// ##### UPDATE TEXTAREA #####
		get_element ('form[reply]').getElementsByTagName ('textarea')[0].value = '';

		// ##### GET LIST #####
		var replies = get_element('message_reply_list');

		// ##### ADD TO LIST #####
		replies.innerHTML += array['reply'][0];
	},

	FileComment: function ()
	{	
		// ##### FORM ELEMENT #####
		var form = get_element ('form[comment]');

		// ##### GET ALL FIELDS OF THE FORM #####
		var textareas = form.getElementsByTagName ('textarea');

		// ##### CHECK REPLY #####
		if (textareas[0].value.length == 0)
		{
			Dialog.Error('Please complete the form before replying to this message.');
			return false;
		}

		// ##### MAKE AJAX CALL #####
		load_ajax (BASE_URL + 'user.php?ajax=1&do=file_comment&file_id=' + get_element('file_id').value + '&comment=' + encodeURIComponent(textareas[0].value), 'User._fileCommentReturned');
	},

	_fileCommentReturned: function (array)
	{
		// ##### CHECK FOR ERROR #####
		if (ajax_returned_error (array))
		{
			return false;
		}

		// ##### UPDATE TEXTAREA #####
		get_element ('form[comment]').getElementsByTagName ('textarea')[0].value = '';
		
		// ##### HIDE NO COMMENT MESSAGE #####
		hide (get_element ('no_comments'));

		// ##### GET LIST #####
		var replies = get_element('comment_reply_list');

		// ##### ADD TO LIST #####
		replies.innerHTML += array['comment'][0];
	},

	ChangePassword: function ()
	{
		// ##### GET ALL FIELDS OF THE FORM #####
		var current_password = get_element ('current_password');
		
		// ##### FORM ELEMENT #####
		var form = get_element ('form[change_password]');
		var inputs = form.getElementsByTagName ('input');

		// ##### CHECK EMPTY PASSWORDS #####
		if (current_password.value.length == 0)
		{
			Dialog.Message ('You must enter your current password.');
			return false;
		}

		// ##### CHECK EMPTY PASSWORDS #####
		if (inputs[0].value.length == 0 || inputs[1].value.length == 0)
		{
			Dialog.Message ('You must complete the form.');
			return false;
		}

		// ##### CHECK MATCHING PASSWORDS #####
		if (inputs[0].value != inputs[1].value)
		{
			Dialog.Message ('Your passwords must match.');
			return false;
		}

		// ##### MAKE AJAX CALL #####
		load_ajax (BASE_URL + 'user.php?ajax=1&do=change_password&current_password=' + encodeURIComponent(current_password.value) + '&password=' + encodeURIComponent(inputs[0].value) + '&password_confirm=' + encodeURIComponent(inputs[1].value), 'User._changePassword');
	},

	_changePassword: function (array)
	{
		// ##### CHECK FOR ERROR #####
		if (ajax_returned_error (array))
		{
			return false;
		}

		// ##### SUCCESS #####
		Dialog.Message (array['message'][0]);
	},

	ChangeEmail: function ()
	{
		// ##### GET ALL FIELDS OF THE FORM #####
		var current_password = get_element ('current_password');
		
		// ##### FORM ELEMENT #####
		var form = get_element ('form[change_email]');
		var inputs = form.getElementsByTagName ('input');

		// ##### CHECK EMPTY PASSWORDS #####
		if (current_password.value.length == 0)
		{
			Dialog.Message ('You must enter your current password.');
			return false;
		}

		// ##### CHECK EMPTY PASSWORDS #####
		if (inputs[0].value.length == 0)
		{
			Dialog.Message ('You must complete the form.');
			return false;
		}

		// ##### MAKE AJAX CALL #####
		load_ajax (BASE_URL + 'user.php?ajax=1&do=change_email&current_password=' + encodeURIComponent(current_password.value) + '&email=' + encodeURIComponent(inputs[0].value), 'User._changeEmail');
	},

	_changeEmail: function (array)
	{
		// ##### CHECK FOR ERROR #####
		if (ajax_returned_error (array))
		{
			return false;
		}

		// ##### SUCCESS #####
		Dialog.Message (array['message'][0]);
	},
	
	AddFavorite: function (file_id)
	{
		// ##### MAKE AJAX CALL #####
		load_ajax (BASE_URL + 'user.php?ajax=1&do=add_favorite&file_id=' + file_id, 'User._favoriteReturned');

		if (document.getElementById('status_add_fav') != null)
		{
			document.getElementById('status_add_fav').style.display = 'none';
		}
		if (document.getElementById('status_remove_fav') != null)
		{
			document.getElementById('status_remove_fav').style.display = 'block';
		}
	},

	RemoveFavorite: function (file_id)
	{
		// ##### MAKE AJAX CALL #####
		load_ajax (BASE_URL + 'user.php?ajax=1&do=remove_favorite&file_id=' + file_id, 'User._favoriteReturned');

		if (document.getElementById('status_add_fav') != null)
		{
			document.getElementById('status_add_fav').style.display = 'block';
		}
		if (document.getElementById('status_remove_fav') != null)
		{
			document.getElementById('status_remove_fav').style.display = 'none';
		}
	},

	_favoriteReturned: function (array)
	{
		// ##### CHECK FOR ERROR #####
		if (ajax_returned_error (array))
		{
			return false;
		}

		// ##### SHOW SUCCESS MESSAGE #####
		//Dialog.Message (array['message'][0]);
	}
}

