/*
* watch.js
* ScreenToaster
* Michael Bagur
*
* Special behaviour for watch video page
*
************************************************/

jQuery(function() {
	try {

		// When the ENTER key is pressed
		jQuery("#inviteFriendsEmail").keydown(function(event) {
			try {
				if(event.keyCode==13) {
					checkInviteFriends();
					return false;
				}
			} catch (e) {
				logger.error("user.js > "+e);
			}
		});
		

		
		// ENTER was pressed
		addPressEnterListenerToForm('changePasswordForm',checkChangePassword);		

	// Catch exceptions
	} catch (e) {
		logger.error("user.js > "+e);
	}
});

function checkChangePassword() {
	try {
		var error = false;
		
		jQuery('#changeNewPasswordError').hide();
		jQuery('#changeConfirmPasswordError').hide();
		
		jQuery('#changePasswordPleaseWait').show();
		jQuery('#changePasswordButtons').hide();
		
		// Do AJAX Request
		jQuery.post(
			$('changePasswordForm').action,
			{
				newPassword: $('changeNewPassword').value,
				confirmPassword: $('changeConfirmPassword').value
			},
			function(xml) {
				try  {
					jQuery('#changePasswordPleaseWait').hide();
					jQuery('#changePasswordButtons').show();
						
					// If status is OK then redirect
					if("OK"==jQuery("status",xml).text()) {
						switchPopup('editPasswordPopup', 'changePasswordConfirmPopup');
					
					// If there was an error
					} else {
						jQuery("messages",xml).each(function() {
							var message = jQuery(this);
							// Get the target and text
							var target = message.find("target",xml).text();
							var text = message.find("text",xml).text();
							// Show the errors
							jQuery("#change"+target+"Error").html(text).show();
						});
					}
				// Catch errors
				} catch (e) {
					logger.error("user.js > "+e);
				}
			}
		);
	// Catch errors
	} catch (e) {
		logger.error("user.js > "+e);
	}
	return false;
}

function checkInviteFriends() {
	try {
		var error = false;
		
		jQuery('#inviteFriendsEmailError').hide();
		
		// Do AJAX Request
		jQuery.post(
			$('inviteFriendsForm').action,
			{
				email: $('inviteFriendsEmail').value
			},
			function(xml) {
				try  {
					// If status is OK then redirect
					if("OK"==jQuery("status",xml).text()) {
						openPopup('inviteConfirmPopup');
					
					// If there was an error
					} else {
						jQuery("messages",xml).each(function() {
							var message = jQuery(this);
							// Get the target and text
							var target = message.find("target",xml).text();
							var text = message.find("text",xml).text();
							// Show the errors
							jQuery("#inviteFriends"+target+"Error").html(text).show();
						});
					}
				// Catch errors
				} catch (e) {
					logger.error("user.js > "+e);
				}
			}
		);
	// Catch errors
	} catch (e) {
		logger.error("user.js > "+e);
	}
	return false;
}

function checkUploadPicture() {
	try {
		var error = false;
		
		// Get data to check
		var uploadPictureConfirm = $('uploadPictureConfirm');
		var uploadConfirmationText = $('uploadConfirmationText');
		var uploadPictureInput = $('uploadPictureInput');

		// Remove errors
		uploadConfirmationText.style.color='black';
		jQuery('#uploadPictureInputError').hide();
		
		// Check acceptance
		if (!uploadPictureConfirm.checked) {
			uploadConfirmationText.style.color='red';
			error = true;
		}
		
		// Check file extension
		var lastDot = uploadPictureInput.value.lastIndexOf('.');
		var ext = uploadPictureInput.value.substring(lastDot+1, uploadPictureInput.value.length);
		if (ext.toLowerCase() != 'jpg' && ext.toLowerCase() != 'jpeg'  && ext.toLowerCase() != 'png' && ext.toLowerCase() != 'png') {
			jQuery('#uploadPictureInputError').html(' (Invalid format)').show();
			error = true;
		}
		
		// Submit if there are no errors
		if (!error) {
			$('uploadPictureForm').submit();
		}
	// Catch errors
	} catch (e) {
		logger.error("user.js > "+e);
	}
}

function checkEmbedLogo() {
	try {
		var error = false;
		
		// Submit if there are no errors
		if (!error) {
			$('editLogoForm').submit();
		}
		
	// Catch errors
	} catch (e) {
		logger.error("user.js > "+e);
	}
}

function editLogoClick() {
	try {
		if ($('editLogoActivate').checked) {
			jQuery('#editLogoPictureDiv').show();
		} else {
			jQuery('#editLogoPictureDiv').hide();
		}
		
	// Catch errors
	} catch (e) {
		logger.error("user.js > "+e);
	}
}

/**
* Validate the add comment form for an anonymous user
*/
function checkEditProfile() {
	try {
		var error = false;
		
		// Submit if there are no errors
		if (!error) {
			$('editProfileForm').submit();
		}
		
	// Catch errors
	} catch (e) {
		logger.error("user.js > "+e);
	}
}