// 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();
})
}

var cform;
function setValidateF(){

	$('form.validate').submit(function(event){
	   
        cform = $(this);

        
        if(cform.attr('ajax') != 'done'){
            
        
            event.preventDefault();
        
            $('.msg_error', cform).css('display','none');
        
    		var post = 'ajax=1';
    		var action = '';
            var prevent = 0;
		
    		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 = $(cform).attr('action');
    		$.ajax({
    			type: "POST",
    			url: action,
    			async: false,
    			data: post,
    			success: function(msg){
    				mess = msg;
    				if(msg != ''){
    					ShowAlert(msg, cform);
    					prevent = 1;
    				}else if($('input[name="location"]', cform).val() == undefined){
    					$('#btn_siguiente').css('display','block');
    					prevent = 1;
    					if(typeof afterSubmit == 'function') { 
    						afterSubmit(); 
    					}
    				}else{
    					prevent = 0;
    					$('.msg_error', cform).css('display','none');
    					htt = '/'+( $('input[name="location"]', cform).val() );
    					$(cform).attr('action', htt);
    				}
    			}
     		});
		  
            if(prevent == 0){
				$(cform).attr('ajax','done');
				$(cform).submit();
			}
            
        }
        
	});
}

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