/**
 * This file provide some functions which will be used on the MyOnlineSSH web site.
 * 
 * @author Samuel ROZE <samuel.roze@gmail.com>
 */

/**
 * Set some of functions/methods/operations at start-up, which will
 * help MyOnlineSSH to works fine.
 */
$(document).ready(function(){
	// Global vars
	var form = $("#connectionForm");
	
	// Hide connection message div
	set_connection_message(_('Connectez-vous'), 'info');
	
	// Check if cookies are enabled
	if (!navigator.cookieEnabled) {
		$('<div class="information error">'+_('Votre navigateur doit supporter les cookies')+'</div>').appendTo('#messages');
	}
	
	var playerVersion = swfobject.getFlashPlayerVersion();
	if (!playerVersion || playerVersion.major <= 0) {
		$('<div class="information error">'+_('Flash Player doit être installé et activé')+'</div>').appendTo('#messages');
	} else if (playerVersion.major < 9) {
		$('<div class="information error">'+_('Votre version de Flash Player n\'est pas assez récente. Mettez-la à jour.')+'</div>').appendTo('#messages');
	}	
	
	form.submit(function(){
		set_connection_message(false);
		if (check_form()) {
			// Create the socket instance
			MyOnlineSSHSocket.init();
		}
		return false;
	});
	
	$("#connectionForm input,select,textarea").removeAttr("disabled");
});

$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if(this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if(this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};

/**
 * This function is called when the form is submited and have to check
 * informations, prepare screen to SSH and call the function which will
 * initiate the socket.
 * 
 * @return void
 */
function check_form ()
{
	if ($('#connection_field_host').val() == '') {
		set_connection_message(_('L\'adresse du serveur est vide'), 'error');
	} else if ($('#connection_field_port').val() == '') {
		set_connection_message(_('Veuillez spécifier le numéro de port'), 'error');
	} else if ($('#connection_field_username').val() == '') {
		set_connection_message(_('Le nom d\'utilisateur est vide'), 'error');
	} else {
		var str = $("input[name='authentification']:checked").val();
		if (str == undefined) {
			set_connection_message(_('Séléctionnez votre méthode de connexion'), 'error');
		} else if (str == 'password') {
			if ($('#connection_field_password').val() == '') {
				set_connection_message(_('Le mot de passe est vide'), 'error');
			} else {
				return true;
			}
		} else if (str == 'private-key') {
			set_connection_message(_('L\'authentification par clé n\'est pas encore supporté'), 'error');
		} else {
			set_connection_message(_('Type d\'authentification invalide'), 'error');
		}
	}
	return false;
}

/**
 * Prints a message error in the top of the connexion div
 * 
 * @param message
 * @return void
 */
function set_connection_message (message, type)
{
	var obj = $('#connection_message');
	
	if (message == false) {
		$("div#connection_message").slideUp("slow");
	} else {
		obj.html('<div class="'+type+'">'+message+'</div>');
		$("div#connection_message").slideDown("slow");
	}
	
}

/**
 * This function will be used when changing the authentification
 * method in the connection form.
 * 
 * @param div_id
 * @return void
 */
function switch_auth (div_id)
{
	$("div#connection div.auth_method").css({display:"none"});
	$("div#connection div.auth_method#"+div_id).css({display:"block"});
}