function submitAVForm()
{

		if(!validateForm()){

			return false;

		} else {

			var fromLocation = location.href.split("from=");
			if(fromLocation.length>1) {
				fromLocation = fromLocation[1];
				document.filtro.from.value = fromLocation;
			}else {
				fromLocation = null;
			}

			//alert(fromLocation);

			saveUserDate();
			checkSaveData();
			
			/*
			url = "skip_filter.php";
			if(fromLocation) {
			//	url+="?from="+fromLocation;
			}
			//location.href = url;
			*/
			document.filtro.submit();


		}

}


/* FILTRO.JS */


var years_19 = new Array(35,34);
var years_20 = new Array(92,138);
var years_21 = new Array(16,53,77,83,85,133,135,151,186);
var years_25 = new Array(0,84);
var blacklist = new Array(2,9,14,17,27,52,59,63,80,86,87,93,94,96,98,104,127,131,134,162,167,168,174,181,187,191);

var error_copies = {
	fecha : 'La fecha es incorrecta. Revísala.',
	menor : 'No está permitido el acceso a menores de edad.'
};

var country;
var userData = [];

$(function(){	
	$(document).pngFix();
	$('input').numeric();
	$('input.image').show();
	
	setInputs();
	getUser();

});


/* GESTION DE COOKIES DEL USUARIO */

function saveUserDate(){
 	userData[0] = $('input#dia').val();
	userData[1] = $('input#mes').val();
	userData[2] = $('input#anyo').val();
	userData[3] = $('select#pais :selected').text();
}

function checkSaveData(){
	if($('input#guardar').attr('checked')){
		//var str = 'day='+userData[0]+'&month='+userData[1]+'&year='+userData[2]+'&country='+userData[3];
		//eraseSavedData();
		//createCookie('roncacique', str, 365);
	}else{
		var str = 'day='+userData[0]+'&month='+userData[1]+'&year='+userData[2]+'&country='+userData[3];
		//eraseSavedData();
		//createCookie('roncacique', str, 0);
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";

	// there needs to be the domain set for this cookie string
	// this needs to be set to .ron-cacique.com so it works
	// across all staging and production environments

	document.cookie = name+"="+value+expires+"; path=/";

}

function readCookie(name) {

	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;

 }

function eraseSavedData(){
	 //eraseCookie('roncacique');
}

function eraseCookie(name) {
 	//createCookie(name,"",-1);
 }

function getUser() {

	var c = readCookie('roncacique');

	if(c == null || c == 'null'){
		return;
	}

	var params = c.split(',');

	var day = readCookie('dia');
	setInput('dia', day);

	var month = readCookie('mes');
	setInput('mes', month);	

	var year = readCookie('anyo');
	setInput('anyo', year);	

	var country = readCookie('pais');
	setCountry(country);

	$('input#guardar').attr('checked' , 'checked');
}

function setInput(_input , _value){
	$('#'+_input).val(_value);
}

function setCountry(_value){
	var total = $('select#pais option');
	
	for(var i = 0; i < total.length; i++){
		if(total[i].text == _value){
			total[i].selected = true;
		}
	}
	
 }

/* FIN GESTION DE COOKIES DEL USUARIO */

function validateForm(){
	var fecha = $('input#anyo').val() + '/' + $('input#mes').val() + '/' + $('input#dia').val();
	var isValid = true;
	
	if($('input#dia').val().length < 1 || $('input#mes').val().length < 1 || $('input#anyo').val().length < 4 || $('input#anyo').val() < 1900){
		setError();
		return false;
	}
		
	$('input.text').each(function(i){
		if($(this).val() == '' || $(this).val() == $(this).attr('defaultValue')){
			isValid = false;
		}
	});

	if(!mesValido()){
		setError();
		return false;
	}else{
		if(isValid == true && checkValidAge(calcularEdad(fecha))){
			return true;
		}else{
			setError();
			return false;
		}
	}
}

function mesValido(){
	elMes = parseInt($('input#mes').val());
	elDia = parseInt($('input#dia').val());
	elAnyo = parseInt($('input#anyo').val());
	
	if(elMes>12){
		return false;
	}
	
	// MES FEBRERO
	if(elMes == 2){
		if(esBisiesto(elAnyo)){
			if(parseInt(elDia) > 29){
				return false;
			}
			else
			return true;
		}
	
		else{
			if(parseInt(elDia) > 28){
				return false;
			}
			else{
				return true;
			}
		}
	}
	//RESTO DE MESES
	if(elMes== 4 || elMes==6 || elMes==9 || elMes==11){
		if(parseInt(elDia) > 30){
			return false;
		}
	}

	if(elMes == 1 || elMes == 3 || elMes == 5 || elMes == 7 || elMes == 8 || elMes == 10 || elMes == 12){
		if(parseInt(elDia) > 31){
			return false;
		}
	}
		

	return true;
}

function esBisiesto(anio){
	var BISIESTO;
	
	if(parseInt(anio)%4 == 0){
		if(parseInt(anio)%100 == 0){
			if(parseInt(anio)%400 == 0){
				BISIESTO = true;
			}
			else{
				BISIESTO = false;
			}
		}
		else{
			BISIESTO = true;
		}
	}
	else
	BISIESTO = false;

	return BISIESTO;
}


function checkValidAge(edad){
	var countryCode = $('select').val();
	
	for (var i in blacklist){
		if(countryCode == blacklist[i]){
			return false;
		}
	}
		
	if(edad < 18){
		return false;
	}
	
	if(edad >= 18 && edad < 19){
		for (var i in years_19){
			if(countryCode == years_19[i]){
				return false;
			}
		}
	}
	
	if(edad >= 18 && edad < 20){
		for (var i in years_20){
			if(countryCode == years_20[i]){
				return false;
			}
		}
	}
	
	if(edad >= 18 && edad < 21){		
		for (var i in years_21){
			if(countryCode == years_21[i]){
				return false;
			}
		}
	}
	
	if(edad >= 18 && edad < 25){
		for (var i in years_25){
			if(countryCode == years_25[i]){
				return false;
			}
		}
	}

	return true;
}

function setError(){
	$('h2').addClass('error');
}

function setInputs(){
	var valor = '';
	$('input').bind('focus' , function(){
		$('h2').removeClass('error');
	   	valor = $(this).val();
		if($(this).val() == $(this).attr('defaultValue')){
			$(this).val('');	
		}
	});
	
	$('input').bind('blur' , function(){
		checkFocus($(this));
	});
}

function checkFocus(el){
	if(el.val() == ''){
		el.val(el.attr('defaultValue'));
	}
}
function calcularEdad(fecha){
    var hoy = new Date()
    var array_fecha = fecha.split("/");
    var ano;
    var mes;
    var dia;
	
    if (array_fecha.length!=3)
       return false;

    ano = parseInt(array_fecha[0]);
	
    if (isNaN(ano))
       return false;

    mes = parseInt(array_fecha[1]);
	
    if (isNaN(mes))
       return false;

    dia = parseInt(array_fecha[2]);
	
    if (isNaN(dia))
       return false;


    if (ano <= 99)
       ano += 1900;

    edad = hoy.getUTCFullYear() - ano - 1; //-1 porque no se si ha cumplido años ya este año

    if (hoy.getMonth() + 1 - mes < 0) //+ 1 porque los meses empiezan en 0
       return edad;
	   
    if (hoy.getMonth() + 1 - mes > 0)
       return edad+1;

    if (hoy.getUTCDate() - dia >= 0)
       return edad + 1;

    return edad;
}