// JavaScript Document
var HTTP_WEB_ROOT = "http://www.tienda-disfraces.es/";

/* -------------------- FUNCIONES COMUNES ---------------------- */
function anula_suscripcion( ) {
	var email = document.getElementById("email").value;
	if( email != "" ) {
		if( document.getElementById("email").value.search(/(^[0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,3})$/) < 0 ) {
			alert("E-mail introducido no es correcto");
		}
		else {
			document.getElementById("baja_suscripcion").submit();
		}
	}
	else {
		alert("Introduzca el e-mail");
	}
}


var con_envia_cupon = new XHConn();
var fin_envia_cupon = function (oXML) { document.getElementById("cupon").innerHTML = oXML.responseText; };
function envia_cupon( ) {
	var email = document.getElementById("email_cupon").value;
	document.getElementById("cupon").innerHTML = "<div id=\"cargando_productos\"><img src=\""+HTTP_WEB_ROOT+"img/cargando_normal.gif\" /><p>Enviando e-mail...</p></div>";
	con_envia_cupon.connect('emailing/mail_cupon.php', 'POST', 'email=' + email, fin_envia_cupon);
}

var con_muestra_cupon = new XHConn();
var fin_muestra_cupon = function (oXML) { document.getElementById("vista_previa").innerHTML = oXML.responseText; obtener_alcada('vista_previa','finestra_vista_previa') };
function muestra_cupon( ) {
	window.location.href="#";
	document.getElementById("vista_previa").className = "vista_previa_mostra_cupon";
	document.getElementById("finestra_vista_previa").className = "finestra_vista_previa_mostra";
	
	con_muestra_cupon.connect('dinamic/cupon.php', 'POST', '', fin_muestra_cupon);
}

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

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;
}  


//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 != 44 && ( charCode < 48 || charCode > 57 )  ) {
    	return false;
	} 
	else {
    	return true;
	}
}

function solo_num( evt ) {
    var charCode = ( evt.which ) ? evt.which : event.keyCode
    if ( ( charCode < 48 || charCode > 57 ) && charCode != 8 ) {
    	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;
		}
		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;
			}
		}
	}
	return cadena;
}


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

function carga_div(nombre) {
	ddrivetip('<div id=\''+nombre+'\'></div>');
}

var con_aviso_legal = new XHConn();
var fin_aviso_legal = function (oXML) { document.getElementById('aviso_legal').innerHTML = oXML.responseText; };
function aviso_legal() {
	con_aviso_legal.connect('aviso_legal.php', 'POST', '', fin_aviso_legal);
}

/*-------------------- CARGANDO CON VENTANA NEGRA ------------------------*/

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

function muestra_cargando_vista_previa(texto)
{
	window.location.href="#";
	document.getElementById("vista_previa").className = "vista_previa_mostra";
	document.getElementById("finestra_vista_previa").className = "finestra_vista_previa_mostra";
	document.getElementById("vista_previa").innerHTML = cargando_vista_previa(texto);
	obtener_alcada('vista_previa','finestra_vista_previa');
}

function oculta_cargando()
{
	document.getElementById("vista_previa").className = "vista_previa_oculta";
	document.getElementById("vista_previa").innerHTML = "";
	document.getElementById("finestra_vista_previa").className = "finestra_vista_previa_oculta";
}
