// JavaScript Document

var HTTP_WEB_ROOT = "http://www.eliminacion-pulgones.es/";


/* -------------------- FUNCIONES COMUNES ---------------------- */


function recorta_caracter( campo, cantidad ) {
	var limite = cantidad;
	if(document.getElementById(campo).value.length > limite) {
	  document.getElementById(campo).value = document.getElementById(campo).value.substring(0,limite);
	}
}

function limit_caracter( evt, campo, cantidad ) {
	var charCode = ( evt.which ) ? evt.which : event.keyCode;
	var limite = cantidad;
    if( document.getElementById(campo).value.length > limite && charCode > 8  ) {
	  return false;
	}
	else {
		return true;
	}
}

//Ejecuta una funcion al apretar la tecla enter
function ejecuta_enter( evt, funcion ) {
    var charCode = ( evt.which ) ? evt.which : evt.keyCode
    if ( charCode == 13 ) {
    	funcion();
	} 
}

//Envia el formulario cuando clickamos la tecla enter
function envia_form_enter( evt, formulario ) {
    var charCode = ( evt.which ) ? evt.which : evt.keyCode
    if ( charCode == 13 ) {
    	document.getElementById(formulario).submit();
	} 
}

//Envia el formulario normal
function envia_form( formulario ) {
   	document.getElementById(formulario).submit();
}

function solo_precio( evt ) {
    var charCode = ( evt.which ) ? evt.which : event.keyCode

    if ( charCode > 31 && charCode != 46 && ( charCode < 48 || charCode > 57 )  ) {
    	return false;
	} 
	else {
    	return true;
	}
}

function solo_num( evt ) {
    var charCode = ( evt.which ) ? evt.which : event.keyCode
    if ( charCode != 8 && ( charCode < 48 || charCode > 57 ) ) {
    	return false;
	} 
	else {
    	return true;
	}
}

//Lee el formulario que queremos enviar y prepara una cadena para enviar por ajax
function prepara_envio( formulario ) {
	var cadena = "";
	var num_elementos = document.getElementById(formulario).length;
	for( var i=0; i<num_elementos; i++ ) {
		
		if( document.getElementById(formulario).elements[i].type != "checkbox" && document.getElementById(formulario).elements[i].type != "radio" ) {
			cadena += i>0 ? "&" : "";
			cadena += document.getElementById(formulario).elements[i].name + "=" + document.getElementById(formulario).elements[i].value.replace("&", "%26");
		}
		else {
			if ( document.getElementById(formulario).elements[i].checked == true ) {
				cadena += i>0 ? "&" : "";
				cadena += document.getElementById(formulario).elements[i].name + "=" + document.getElementById(formulario).elements[i].value.replace("&", "%26");
			}
		}
	}
	return cadena;
}

function valida_cif( cif ) {	
	/* VALIDA CIF */
	
	var no_cif = true;
	var no_dni = true;
	
	var par = 0;	
	var impar = 0;	
	var letras = "ABCDEFGHKLMNPQS";	
	var letra = cif.charAt(0); 	
	if ( cif.length != 9 ) {		
		no_cif = false;	
	} 	
	if ( letras.indexOf(letra.toUpperCase()) == -1 ) {
		no_cif = false;
	} 	
	for ( var i=2; i<8; i+=2 ) {		
		par = par+parseInt(cif.charAt(i));	
	} 	
	for ( var c=1; c<9; c+=2 ) {
		cont = 2*parseInt(cif.charAt(c));
		if ( cont > 9 ) cont = 1+(cont-10);		
		impar = impar+cont;
	} 	
	parcial = par + impar;	
	control = (10 - ( parcial % 10));	
	if ( control == 10 ) control=0; 	
	if ( control != cif.charAt(8) ) {		
		no_cif = false;
	}	
	
	/* VALIDA DNI */
	
	var numeros = cif.substring( 0,cif.length-1 );
	var letra_dni = cif.charAt( cif.length-1 );
	if ( !isNaN(letra_dni) ) {
		no_dni = false;
	}
	else{
		var cadena = "TRWAGMYFPDXBNJZSQVHLCKET";
		var posicion = numeros % 23;	
		letra = cadena.substring(posicion,posicion+1);
		if ( letra!=letra_dni.toUpperCase() ) {
			no_dni = false;
		}
	}	
	return no_dni == false && no_cif == false ? false : true;
}  


//Validamos el que las fechas introducidas sean correctas
function valida_fecha(dia_inicio, mes_inicio, anio_inicio) {
	
	var cadena_error = "";
	
	var fecha_inicio = new Date(anio_inicio, mes_inicio, dia_inicio);	
	var fecha_actual = new Date();
	
	var array_meses = new Array();
	array_meses[1] = 31;
	array_meses[2] = 29;
	array_meses[3] = 31;
	array_meses[4] = 30;
	array_meses[5] = 31;
	array_meses[6] = 30;
	array_meses[7] = 31;
	array_meses[8] = 31;
	array_meses[9] = 30;
	array_meses[10] = 31;
	array_meses[11] = 30;
	array_meses[12] = 31;
	
	var dias_mes = 0;
	
	//Miramos si el aņo es bisiesto y comprobamos que los dias introducidos sean correctos
	if ( ( anio_inicio % 4 == 0 ) && ( ( anio_inicio % 100 != 0 ) || ( anio_inicio % 400 == 0 ) ) ) {
		dias_mes = mes_inicio == 2 ? 28 : array_meses[mes_inicio];															  
		if ( dia_inicio > dias_mes ) {
			cadena_error += "\n - La fecha no es correcta";
		}																  
	}
	else {
		if ( dia_inicio > array_meses[mes_inicio] ) {
			cadena_error += "\n - La fecha no es correcta";
		}	
	}

	
	if ( fecha_inicio.getTime() < fecha_actual.getTime() ) {
		cadena_error += "\n- La fecha debe ser mayor a la actual";
	}
	
	return cadena_error;
}


/* ------------------- AVISO LEGAL ------------------------ */

//Prepara el div para mostrar un cargando del formulario
function cargando_formulario( texto ) {
	return "<div id=\"cargando_form\"><img src=\""+HTTP_WEB_ROOT+"img/cargando.gif\" /><p>"+texto+"</p></div>";
}
