/**
 * JS
 * (c) 2007 Darío G. Ruellan / D²
 */

$(function() {
	
	// Form 1
	if ( $('#form1').html() ) {
		
		testChars();  
	
		$('#m').keypress(function(e){
			var unicode=e.keyCode? e.keyCode : e.charCode;
			return unicode != 13;
		});
		$('#m').keyup(function(){testChars()});
		
		$('#form1').submit(function(){
			var bad = false;
			//Nombre
			if ( !$('#no').val() > "" ) {
				$('#no').addClass('bad_border');
				bad = true;
			} else {
				$('#no').removeClass();
			}
			if ( !$('#ap').val() > "" ) {
				$('#ap').addClass('bad_border');
				bad = true;
			} else {
				$('#ap').removeClass();
			}
			if ( !$('#apen').val() > "" ) {
				$('#apen').addClass('bad_border');
				bad = true;
			} else {
				$('#apen').removeClass();
			}
			if ( !email_filter.test($('#em').val()) ) {
				$('#em').addClass('bad_border');
				bad = true;
			} else {
				$('#em').removeClass();
			}
			if ( $("#form1 input:checkbox").is(":checked") == false ) {
				$('#options_bad').addClass('bad_border');
				bad = true;
			} else {
				$('#options_bad').removeClass();
			}
			
			if ( !bad ) {
				$('#sending').css("display","inline");
				return true;
			} else {
				$('#error').slideDown();
				return false;
			}
		});
	}
	
	// Form 2
	if ($('#form2').html()) {
		$('#btn_back').click(function(){
			$("#form2 input[@name='step']").val('1');
		});
	}
	
});

// -- Cuenta los catacteres del formulario --

function testChars() {
	var max_chars = 350;
	var chars = $("#m").val().length;
	var chars_remaining = max_chars - chars;
	
	if ( chars > max_chars ) {
		$('#m').val( $('#m').val().substring(0, max_chars) );
		chars_remaining = 0;
	}
	
	$('.max_m').html(chars_remaining+" caracteres restantes de un total de 350");
}