$(document).ready(
	function() {
		$("#login_form").submit(
			function() {
				
				// On détecte le type, message d'erreur si rien n'est choisi
				var extranet_type = $("input[name='type']:checked").val();
				if(extranet_type === undefined) {
					$("#login_box").removeClass().addClass('messageboxerror').text(login_strings.type_error[0]).fadeIn(1000);
					return false;
				}

				if(extranet_type == 'ftp') {
					// On valide avec rumpus
					document.location='http://server.kinos.ca:10000/?login=' + $('#username').val() + ':' + $('#password').val();
				} else {

					// Remove all the class add the messagebox classes and start fading
					$("#login_box").removeClass().addClass('messagebox').text(login_strings.validating[0]).fadeIn(1000);

					// Check the username exists or not from ajax
					$.post(base_url + 'extranet/auth/ajax.html',{ username:$('#username').val(),password:$('#password').val() },
					function(data) {
						if(data=='yes') { // If correct login detail
							$("#login_box").fadeTo(200,0.1,function() { // Start fading the messagebox
								// Add message and change the class of the box and start fading
								$(this).html(login_strings.ok[0]).addClass('messageboxok').fadeTo(900,1,function() {
									// Redirect to secure page
									document.location= base_url + 'extranet/';
								});
							});
						} else {
							$("#login_box").fadeTo(200,0.1,function() { // Start fading the messagebox
								// Add message and change the class of the box and start fading
								$(this).html(login_strings.extranet_error[0]).addClass('messageboxerror').fadeTo(900,1);
							});
						}
					});
				}

				return false; //not to post the  form physically
			});

			// Now call the ajax also focus move from 
			// $("#password").blur(function() {
			// 	$("#login_form").trigger('submit');
			// });

			$('#username').focus(function() {
				$('#username').val('');
			});

			$('#password').focus(function() {
				$('#password').val('');
			});

});
