

$(document).ready(function(){
	

		
    function disableTab(e) {
        var TABKEY = 9;
        if(e.keyCode == TABKEY) {
            if(e.preventDefault) {
                e.preventDefault();
            }
            if(e.stopPropagation) {
            	e.stopPropagation(); 
            }
            return false;
        }
    }
    $('#email').keydown(disableTab).keypress(disableTab) ;
    $('#zona').keydown(disableTab).keypress(disableTab) ;
    $('.comments').keydown(disableTab).keypress(disableTab) ;
    
    function validFields(step) {
    	
    	if ( step == 3 ) {
    		
    		if ( ! $('input#nacimiento').val() ) {
	    		$('div.error.nacimiento').fadeIn('slow') ;
				$('input#nacimiento').focus(function(){ $(this).next('div').fadeOut('slow'); });
	    		return false ;
    		}
    	}
    	return true ;
    }
    
	function thanksMsg() {
		document.body.style.cursor = 'auto';
		$('#gracias').html('Gracias por tu inter&eacute;s en trabajar con nosotros, nos comunicaremos contigo a la brevedad').fadeIn();
	}

	function submitEmail() {
		var ok = true ;
	 	var name = $('#name').val();
		var email = $('#email').val();
		var lastname = $('#lastname').val();
		document.body.style.cursor = 'progress';
	
		$.ajax({
			type: "POST",
			url: "savePrimaryData",
			data: { name:name , email:email, lastname:lastname },
			async: false, // Para que espere la respuesta
			success: 
				function(respuesta) {
					try {
						data = eval(respuesta) ;
					  	var salida = '' ;
					  	salida += data.email + "\n";
					  	salida += data.name + "\n"; 
					  	salida += data.lastname + "\n"; 
					  	salida += data.valid + "\n"; 
					  	if ( ! data.valid ) {
					  		salida +=  'ERROR : Por favor, llena todos los campos !  ';
							if ((data.name)!=null) { 
								
								$('div.error:eq(0)').html(data.name).fadeIn('slow') ;
								$('input#name').focus(function(){ $(this).next('div').fadeOut('slow'); });
								ok = false ;
							}
							if (data.lastname != null) {
								$('div.error:eq(1)').html(data.lastname).fadeIn('slow') ;
								$('input#lastname').focus(function(){ $(this).next('div').fadeOut('slow'); });
								ok = false ;
							}
						    
							if (data.email != null){
								$('div.error:eq(2)').html(data.email).fadeIn('slow') ;
								$('input#email').focus(function(){ $(this).next('div').fadeOut('slow'); });
								ok = false ;
							}
						}
	
					}catch ( e ) {
						ok = false ;
					}	
					document.body.style.cursor = 'auto'; 
				}
		});
		return ok;
	} 


	$('div.category').hide();
	$('div.category:eq(0)').show();
	$('h3.category:eq(0)').removeClass('plus').addClass('less');
	$('h3.category').click(function(){
		$('h3.category').removeClass('less');
		$('h3.category').addClass('plus') ;
		$(this).addClass('less');
		$('div.category').slideUp('slow');							
		$(this).next('div.category').slideDown('slow');


	});
	
	

	var labels = new Array();
	labels[0] = 'Datos B&aacute;sicos';
	labels[1] = 'Conocimientos';
	labels[2] = 'Datos personales';
	labels[3] = 'Conocimientos Adicionales';
	labels[4] = 'Finalizaci&oacute;n y Env&iacute;o';

	/* Generamos los globos con las labels de acuerdo al array 'labels' */
	for(i=1; i<=labels.length; i++) {
		var clase = i;
		var label = i - 1;
		$('#steps').before('<h2 class="balloon step' + clase + ' off">' + labels[label] + '</h2>');		
	}
	
	/* Reseteamos el scroll y canvas */
	$.scrollTo.defaults.axis = 'x'; 			
	$('div#canvas').scrollTo( 0 );
	$.scrollTo( 0 );	

	/* Reseteamos la ubicación, por defecto es el paso 1 */
	var current = 1;
	
	/* Definimos cuantos pasos va tener */
	var firstStep = 1;
	var lastStep = labels.length;
	
	/* Mostramos el paso 1 */
	whereIam();

	$('.stars-wrapper').stars();

	/* Que pasa si hacemos click en siguiente ? */
	$('li#next').click(function(){ 
		if ( validFields(current) ) {
			if ( current == 1 ) {
				ok = submitEmail();
				if (ok) current++ ;
			}else  {
				current++ ;
			}
			whereIam();
		}
	});
	
	/* Que pasa si hacemos click en anterior ? */
	$('li#previous').click(function(){
		current --;
		whereIam();
	});
		
	/* Mostramos el paso actual y nos posicionamos en él */
	function whereIam() {
		$('.balloon, #steps li').removeClass('on').addClass('off');
		$('h2.step' + current + ', li.step' + current ).removeClass('off').addClass('on');
		$('li#next p').before('<p>' + labels[current] + '</p>').remove();
		$('li#previous p').before('<p>' + labels[current-2] + '</p>').remove();
		if (current == firstStep) { $('li#previous').removeClass('on').addClass('off') } else { $('li#previous').removeClass('off').addClass('on'); }
		if (current == lastStep) { $('li#next').removeClass('on').addClass('off') } else { $('li#next').removeClass('off').addClass('on'); }
		$('#canvas').stop().scrollTo('fieldset.step' + current + '', 400);
	}
	
	/* Guardamos el nombre en tiempo real */
	$('input#name').keyup(function(){
		$('p.description strong').before('<strong>' + $(this).val() + '</strong>').remove();	
	});

	$('textarea.comments').focus(function(){ 
		$(this).val('');
	});

	$('input:text, textarea').happyTextbox();	
	
	$('input#nacimiento').datepicker({ yearRange: '1950:1995' });	


	$("#wajobs").submit( function() {
		$('#sendit , li#previous , fieldset:last-child p.description:eq(1)').fadeOut();
		document.body.style.cursor = 'progress'; 
		setTimeout(thanksMsg, 2000) ;
		return true ;
	});
	
	$('#wajobs').ajaxForm(function(){
		thanksMsg() ;
	});
	

	
});
