// JavaScript Document
$(function(){
	setValidateF();
	setPrevent();
	setPestanas();
})

function setPestanas(){
	$('.pestana:eq(0)').slideDown('slow');
	$('.pestana_menu a').click(function(event){
		rel = $(this).attr('rel');
		countpestanas = 0;
		event.preventDefault();
		
		$('.pestana').slideUp('slow', function(){
			countpestanas++;
			if($('.pestana').length == countpestanas)
				$('#'+rel).slideDown('slow');
		});
		
	});
}

function setPrevent(){
	$('.prevent').click(function(event){
		event.preventDefault();
	})
}

function setValidateF(){
	$('form.validate').submit(function(event){
		$('.msg_error', cform).css('display','none');
		var post = 'ajax=1';
		var action = '';
		var prevent = 0;
		var cform = $(this);
		
		for(x=0; x < $('.input', this).length; x++){
			$('.input:eq('+x+')', this).css('border-color','');
			$('.input:eq('+x+')', this).css('background','');
			
			if($('.input:eq('+x+')', this).hasClass('checkbx')){
				if($('.input:eq('+x+')', this).is(':checked')){
					post += '&';
					post += $('.input:eq('+x+')', this).attr('name') + '=' + $('.input:eq('+x+')', this).val();
				}
			}else{
				if($('.input:eq('+x+')', this).hasClass('required') && $('.input:eq('+x+')', this).val() == ''){
					$('.input:eq('+x+')', this).css('border-color','#F00');
					$('.input:eq('+x+')', this).css('background','#ffe1e1');
				}else{
					if(post != '')post += '&';
					post += $('.input:eq('+x+')', this).attr('name') + '=' + $('.input:eq('+x+')', this).val();
				}
			}
		}
		action = $(this).attr('action');
		$.ajax({
			type: "POST",
			url: action,
			async: false,
			data: post,
			success: function(msg){
				if(msg != ''){
					ShowAlert(msg, cform);
					prevent = 1;
				}else if($('input[name="location"]', cform).val() == undefined){
					$('#btn_siguiente').css('display','block');
					prevent = 1;
				}else{
					prevent = 0;
					$('.msg_error', cform).css('display','none');
					htt = HTTP+$('input[name="location"]', cform).val();
					$(cform).attr('action', htt);
				}
			}
 		});
		if(prevent == 1)event.preventDefault();
	});
}

function ShowAlert(msg, cform){
	$('.msg_error', cform).css('display','none');
	$('.msg_error', cform).html(msg);
	$('.msg_error', cform).fadeIn('slow');
}
