function actualizarCantidad(idproducto, idcompra){
	if(isNaN(document.getElementById("txtCantidad"+idproducto).value) || document.getElementById("txtCantidad"+idproducto).value<1){
		document.getElementById("txtCantidad"+idproducto).value = 1;
		simple_ajax("ajaxActualizarCantidad.php", "GET", "prod="+idproducto+"&compra="+idcompra+"&cant="+document.getElementById("txtCantidad"+idproducto).value);
		//showAlert(28);
	}else{
		simple_ajax("ajaxActualizarCantidad.php", "GET", "prod="+idproducto+"&compra="+idcompra+"&cant="+document.getElementById("txtCantidad"+idproducto).value);
	}
}

/**************************************************************/
/************** ENCUESTAS *************************************/
/**************************************************************/

function votar(id_encuesta){
	
	id_campo = document.getElementById("idcampo"+id_encuesta).value;
	arrValores = document.getElementsByName("rbCampo"+id_campo);
	valor = "";
	for(i=0; i<arrValores.length; i++){
		if(arrValores[i].checked){
			valor = arrValores[i].value;
		}
	}
	if(valor!=""){
		simple_ajax("ajaxVotar.php", "GET", "id="+id_encuesta+"&campo="+id_campo+"&valor="+valor);
	}else{
		showAlert(25);
	}

}




/**************************************************************/
/************** CATEGORIA COLECCIONABLES **********************/
/**************************************************************/

function mostrarColeccion(){
	
	id = document.getElementById("tipo").value;
	window.location = "ficha.php?id="+id;
	return false;
}


function suscribirColeccion(){
	
	id = document.getElementById("tipo").value;
	window.location = "suscripcion.php?id="+id;
	return false;
}

/**************************************************************/
/************************* CARRITO ****************************/
/**************************************************************/

var t;
function showCart(bMostrar, delay){						

			clearTimeout(t);								

			document.getElementById("rollover").style.top = (getAbsY(document.getElementById('carrito'))+44)+'px';
			document.getElementById("rollover").style.left = (getAbsX(document.getElementById('carrito'))-89)+'px';			
			document.getElementById("parcheCarrito").style.top = (getAbsY(document.getElementById('comprar')))+'px';
			document.getElementById("parcheCarrito").style.left = (getAbsX(document.getElementById('comprar'))+50)+'px';			

			var strJS = "document.getElementById('rollover').style.display='" + (bMostrar?'block':'none') + "';";
			strJS += "document.getElementById('parcheCarrito').style.display='" + (bMostrar?'block':'none') + "';";
			t = setTimeout(strJS, delay);
}

/**************************************************************/
/********** LISTADO NOVEDADES, PROMOCIONES, TIENDA ***********/
/**************************************************************/

function agregarCarrito(idProducto){	
	
	simple_ajax("ajaxAgregarACarrito.php", "GET", "id="+idProducto+"&accion=1");

}
function agregarCarritoBajatelo(idProducto, tipo){	
	
	simple_ajax("ajaxAgregarACarrito.php", "GET", "id="+idProducto+"&accion=1&tipo="+tipo);

}


function buscar(){
	var frm = document.selector;
	frm.submit();
}

/**************************************************************/
/******************** REGISTRO / LOGIN ************************/
/**************************************************************/
function registro(){
		Lightbox.showBoxByAJAX('registro.php',382,310);
}
function registrook(){
		Lightbox.showBoxByAJAX('registrook.php',382,298);
}

function login(){
		Lightbox.showBoxByAJAX('login.php',310,188);
}

function showAlert(strMensaje, intIntentos){
	if(intIntentos==undefined) intIntentos = 0;
	
	if(isNaN(strMensaje)){
		strMensaje = strMensaje.replace(/\n/g, "<br/>");
	}
	
	try{
		Lightbox.showBoxByAJAX('alertas.php?alerta=' + strMensaje,310,188);
	}catch(err){
		if(intIntentos<4){
			setTimeout("showAlert('" + strMensaje + "', " + (intIntentos+1) + ");",1500);
		}
	}
}

function showAlert2(strMensaje, intIntentos, alto){
	if(intIntentos==undefined) intIntentos = 0;
	
	if(isNaN(strMensaje)){
		strMensaje = strMensaje.replace(/\n/g, "<br/>");
	}
	
	try{
		Lightbox.showBoxByAJAX('alertas2.php?al='+alto+'&alerta=' + strMensaje,500,alto);
	}catch(err){
		if(intIntentos<4){
			setTimeout("showAlert2('" + strMensaje + "', " + (intIntentos+1) + ", " + alto + ");",1500);
		}
	}
}

function isEmailAddress(theElement)
{
var s = theElement.value;
//var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;

var strRegExp = "^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$";
var objRegExp = new RegExp(strRegExp)

if (s.length == 0 ) return true;
if (objRegExp.test(s))
return false;
else
return true;
}


function loguear(){
	var frm = document.frmLogueo;
  var strError = "";

	if(document.getElementById("txtEmail").value==""){
		strError += "Por favor, ingrese el campo email<br />";
	}else if(isEmailAddress(document.getElementById("txtEmail"))){
		strError += "El email ingresado es incorrecto<br />";
	}
	if(document.getElementById("txtPass").value==""){
		strError += "Por favor, ingrese el campo contraseña";
	}

  if (strError == "") {
    simple_ajax("ajaxLogin.php", "GET", "email="+document.getElementById("txtEmail").value+"&pass="+document.getElementById("txtPass").value);
  } else {
    document.getElementById("mensaje").innerHTML = strError;
  }          
}

function registrar(){
	var frm = document.frmRegistro;
  var strError = "";

	if(document.getElementById("txtNombres").value==""){
		strError = "Por favor, ingrese el campo nombre";
	}else	if(document.getElementById("txtApellidos").value==""){
		strError += "Por favor, ingrese el campo apellido\n";
	}else if(document.getElementById("txtEmails").value==""){
		strError += "Por favor, ingrese el campo email\n";
	}else if(isEmailAddress(document.getElementById("txtEmails"))){
		strError += "El email ingresado es incorrecto\n";
	}else if(document.getElementById("txtPass").value==""){
		strError += "Por favor, ingrese el campo contraseña\n";
	}else if(document.getElementById("txtPass2").value==""){
		strError += "Por favor, ingrese el campo repetir contraseña\n";
	}else if(document.getElementById("txtPass").value != document.getElementById("txtPass2").value){
 		strError += "Las contraseñas deben ser iguales\n";
 	}else if(!document.getElementById("chkAceptar").checked){
    	strError += "Debe aceptar las condiciones de uso.\n";
    } 	
 
  if (strError == ""){
 
  	document.getElementById("hidRegistro").value = 1;
		//frm.submit();

    ajax("ajaxRegistro.php", "POST", document.frmRegistro, "");
 
  } else {
    document.getElementById("mensaje").innerHTML = strError;
  }          
}

/**************************************************************/
/************************ AYUDA/FAQS **************************/
/**************************************************************/

function mostrarPregunta(id){
	document.getElementById("pregunta").innerHTML = document.getElementById("pregunta"+id).innerHTML;
	document.getElementById("respuesta").innerHTML = document.getElementById("respuesta"+id).innerHTML;	
}


/**************************************************************/
/************************** FICHA *****************************/
/**************************************************************/
function mandaURLVideo(video) {
	var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
	var PeliculaObj = InternetExplorer? player_video : document.player_video; 
	PeliculaObj.abreVideo(video);
}

function favoritos(URL, nombre){
if ((navigator.appName=="Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=4)) {
var url=URL;
var titulo=nombre;
window.external.AddFavorite(url,titulo);
} else {
if(navigator.appName == "Netscape")
showAlert (7);
}
}

function valorar(id_producto, valoracion){
	simple_ajax('ajaxValorarProducto.php','GET','id_producto=' + id_producto + '&valoracion=' + valoracion);
}

function cambiarValoracion(valoracion){
	var arrNumeros = new Array('cero','uno','dos','tres','cuatro','cinco');
	document.getElementById('spnValorar').className = arrNumeros[valoracion];
}

/**************************************************************/
/************************ ENVIAR EVENTO ***********************/
/**************************************************************/
function enviarEvento(){
		var frm = document.frmEvento;

    var strError = "";
    var objCheck;

    objCheck = new check("frmEvento");

    objCheck.checkString("txtTitulo", "Título", 2, 250, false);
    
    objCheck.checkDate("txtFecha", "Fecha", false);
    
    objCheck.checkString("txtContenido", "Descripción", 2, 615, false);

    strError = objCheck.toString();
    if (strError == "") {
         	frm.hidOperacion.value = 1;
          frm.submit ();
    } else {
          showAlert (strError);
    }
}

/**************************************************************/
/************************ ENVIAR COMENTARIO ***********************/
/**************************************************************/
function enviarComentario(){
		var frm = document.frmComentario;

    var strError = "";
    var objCheck;

    objCheck = new check("frmComentario");

    objCheck.checkString("txtContenido", "Comentario", 2, 370, false);

    strError = objCheck.toString();
    if (strError == "") {
         	frm.hidOperacion.value = 1;
          frm.submit ();
    } else {
          showAlert (strError);
    }
}

/**************************************************************/
/************************ CONCURSOS ***************************/
/**************************************************************/
	function seleccionar(id,opcion){
		var anterior = document.getElementById('hidIdOpcion' + id).value;
		document.getElementById('hidIdOpcion' + id).value = opcion;
		
		document.getElementById('lnkOpcion' + id + '_' + opcion).className = 'concur_combi2';
		
		if(anterior!="" && anterior!=opcion) document.getElementById('lnkOpcion' + id + '_' + anterior).className = 'concur_combi';
	}
	function concursar(id_concurso, id_campo){
		
		if(document.getElementById('hidIdOpcion' + id_campo).value != ""){
			var valor = parseInt(document.getElementById('hidIdOpcion' + id_campo).value) + 1;
			simple_ajax("ajaxConcursar.php", "GET", "id="+id_concurso+"&campo="+id_campo+"&valor="+valor);
		}else{
			showAlert(26);
		}
		
	
	}

/**************************************************************/
/************************ RECORDAR PASS **********************/
/**************************************************************/
function recordar(){
		var frm = document.frmRecordar;

    var strError = "";
    var objCheck;

    objCheck = new check("frmRecordar");

    objCheck.checkEmail("txtEmailRecordar", "Email", 2, 250, false);

    strError = objCheck.toString();
    if (strError == "") {
         	frm.hidOperacion.value = 1;
          frm.submit ();
    } else {
          showAlert (strError);
    }
}

/**************************************************************/
/************************** MIS DATOS *************************/
/**************************************************************/
  function modificarDatos() {
    var frm = document.frmDatos;

    var strError = "";
    var objCheck;

    frm.txtFechaNacimiento.value = frm.txtFecha1.value + "/" + frm.txtFecha2.value + "/" + frm.txtFecha3.value;

    objCheck = new check("frmDatos");
    objCheck.checkString("txtNombre", "nombre", 2, 50, false);
    objCheck.checkString("txtApellido", "apellidos", 2, 50, false);
    objCheck.checkEmail("txtEmail", "email", 7, 255, false);
    objCheck.checkString("txtCalle", "calle", 2, 50, false);
    objCheck.checkString("txtTipoVia", "teléfono", 2, 35, false);
    objCheck.checkString("txtNumero", "número", 1, 10, false);
    objCheck.checkString("txtCodigoPostal", "codigo postal", 2, 10, false);
    objCheck.checkString("txtPoblacion", "población", 2, 50, false);
    objCheck.checkString("txtProvincia", "provincia", 2, 50, false);
    objCheck.checkString("txtTelefono", "teléfono", 2, 30, true);
    objCheck.checkString("txtMovil", "móvil", 2, 30, true);
    //objCheck.checkString("txtNIF", "NIF", 1, 9, false);
    objCheck.checkDate("txtFechaNacimiento", "Fecha de Nacimiento", false);
    strError = objCheck.toString();
    var strNIF = validarNIF(frm.txtNIF.value);
		if(strNIF!=""){
	  	strError += strNIF;
	  }
	  if(document.getElementById("txtTelefono").value == ""  && document.getElementById("txtMovil").value == ""){
	  	strError += "Debe completar al menos un teléfono.\n";
	  }
	  
    if (strError == "") {
           	frm.hidOperacion.value = 1;
            frm.submit ();
    } else {
            showAlert (strError);
    }
  }

/**************************************************************/
/*************************** COMPRA ***************************/
/**************************************************************/
  function registrarCliente() {
    var frm = document.frmRegSusc;

    var strError = "";
    var objCheck;
    
    
    frm.txtFechaNacimiento.value = frm.txtFecha1.value + "/" + frm.txtFecha2.value + "/" + frm.txtFecha3.value;

    objCheck = new check("frmRegSusc");
    objCheck.checkString("txtNombre", "nombre", 2, 50, false);
    objCheck.checkString("txtApellido", "apellidos", 2, 50, false);
    objCheck.checkEmail("txtEmailR", "email", 7, 255, false);
    objCheck.checkString("txtCalle", "calle", 2, 50, false);
    objCheck.checkString("txtTipoVia", "teléfono", 2, 35, false);
    objCheck.checkString("txtNumero", "número", 1, 10, false);
    objCheck.checkString("txtCodigoPostal", "codigo postal", 2, 10, false);
    objCheck.checkString("txtPoblacion", "población", 2, 50, false);
    objCheck.checkString("txtProvincia", "provincia", 2, 50, false);
    objCheck.checkString("txtTelefono", "teléfono", 2, 30, true);
    objCheck.checkString("txtMovil", "móvil", 2, 30, true);
    objCheck.checkDate("txtFechaNacimiento", "Fecha de Nacimiento", false);
    if(document.getElementById("txtPassword")!=null){
    	objCheck.checkString("txtPassword", "contraseña", 5, 50, false);
		
	    objCheck.checkString("txtPassword2", "repetir contraseña", 5, 50, false);
	  }
    strError = objCheck.toString();
    
    
    
    var strNIF = validarNIF(frm.txtNIF.value);
		if(strNIF!=""){
	  	strError += strNIF;
	  }
	  
	  if(document.getElementById("txtTelefono").value == "" && document.getElementById("txtMovil").value == ""){
	  	strError += "Debe completar al menos un teléfono.\n";
	  }
    
    if(!document.getElementById("chkAceptar").checked){
    	strError += "Debe aceptar las condiciones de uso.\n";
    }
    
		if(document.getElementById("txtPassword")!=null){
	    if(document.getElementById("txtPassword").value != document.getElementById("txtPassword2").value){
	    	strError += "Las contraseñas no coincide.\n";
	    }
	  }
    
    if (strError == "") {
    		if(document.getElementById("cmbDesde")!=null){
    			frm.hidDesde.value = document.getElementById("cmbDesde").value;  	    		
    		}
        frm.submit();
    } else {
        showAlert (strError);
    }
  }
  function loginCliente() {
    var frm = document.frmLoginSusc;

    var strError = "";
    var objCheck;

    objCheck = new check("frmLoginSusc");
    objCheck.checkString("txtPasswordL", "password", 2, 50, false);
    objCheck.checkEmail("txtEmailL", "email", 7, 100, false);
    strError = objCheck.toString();
    if (strError == "") {
    		if(document.getElementById("cmbDesde")!=null){
    			frm.hidDesde.value = document.getElementById("cmbDesde").value;  	    		
    		} 
        frm.submit ();
    } else {
        showAlert (strError);
    }
  }
  
  function validarNIF(abc)	{
		dni=abc.substring(0,abc.length-1)
		let=abc.charAt(abc.length-1)
		if (!isNaN(let) || isNaN(dni)) {
		  return "El campo NIF debe tener 8 números y una letra.\n";
		}else	 {
		  cadena="TRWAGMYFPDXBNJZSQVHLCKET"
		  posicion = dni % 23
		  letra = cadena.substring(posicion,posicion+1)
		  if (letra!=let.toUpperCase()){
		    return "El NIF introducido no es válido.\n";
		   }
		 }
		return "";
	}

  function validaFormaPago(){
  	var bError = false;
  	var rbFormaPago_value = "";
  	
  if(document.frmSusc.rbFormaPago!=undefined){
  	
		if (document.frmSusc.rbFormaPago.length!=undefined){
			for (var i=0; i < document.frmSusc.rbFormaPago.length; i++) {
	    		if (document.frmSusc.rbFormaPago[i].checked) {
	    			rbFormaPago_value = document.frmSusc.rbFormaPago[i].value;
	    			break;
	    		}
	    	}
		}else{
			rbFormaPago_value = document.frmSusc.rbFormaPago.value;
		}
		if (rbFormaPago_value==2){
			var banco 		= document.getElementById('txtCuenta1');
			var sucursal 	= document.getElementById('txtCuenta2');
			var dc 				= document.getElementById('txtCuenta3');
			var cuenta 		= document.getElementById('txtCuenta4');
		
		  if (banco.value == ""  || sucursal.value == "" || dc.value == "" || cuenta.value == ""){
		    showAlert(8);
		    bError = true;
		  } else {
		    if (banco.value.length != 4 || sucursal.value.length != 4 || dc.value.length != 2 || cuenta.value.length != 10){
		      showAlert(9);
			    bError = true;
		    } else {
		      if (!numerico(banco.value) || !numerico(sucursal.value) || !numerico(dc.value) || !numerico(cuenta.value)){ 
		        showAlert(9);
				    bError = true;
			    } else {
		        if (!(obtenerDigito("00" + banco.value + sucursal.value) == parseInt(dc.value.charAt(0))) || 
		            !(obtenerDigito(cuenta.value) == parseInt(dc.value.charAt(1)))) {
		          showAlert(10);
					    bError = true;
						}
		      }
		    }
		  }
		}else if(rbFormaPago_value==3){
			
			if(document.getElementById('cmbTarjeta') != null){
				var tipo 		  = document.getElementById('cmbTarjeta');
				var tarjeta 	= document.getElementById('txtCuenta');
				var caducidad = document.getElementById('txtCaducidad');
				var strError  = "";
				if(tipo.value == ""){
					strError += "Debe seleccionar una opción para el campo tipo\n";
					bError = true;
				}
				if(tarjeta.value == ""){
					strError += "Debe completar el campo Nº de tarjeta\n";
					bError = true;
				}
				if(caducidad.value == ""){
					strError += "Debe completar el campo caducidad\n";
					bError = true;
				}
				if(strError!=""){
					showAlert (strError);
				}
			}
		}else if(rbFormaPago_value==""){
      showAlert(27);
	    bError = true;
		}
			
	}		
		if(!bError){
			document.frmSusc.submit();
		}
  	
  }
  
	function obtenerDigito(valor){
	  valores = new Array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
	  control = 0;
	  for (i=0; i<=9; i++)
	    control += parseInt(valor.charAt(i)) * valores[i];
	  control = 11 - (control % 11);
	  if (control == 11) control = 0;
	  else if (control == 10) control = 1;
	  return control;
	}
	function numerico(valor){
	  cad = valor.toString();
	  for (var i=0; i<cad.length; i++) {
	    var caracter = cad.charAt(i);
		if (caracter<"0" || caracter>"9")
		  return false;
	  }
	  return true;
	}
  
	function accionProductoCarrito(id_producto, tipo){
		if(document.getElementById("chkProducto"+id_producto).checked){
			accion = 1;
		}else{
			accion = 0;
		}
		simple_ajax("ajaxAgregarACarrito.php", "GET", "id="+id_producto+"&accion="+accion+"&tipo="+tipo);

		
	}

/**************************************************************/
/******************* BUSQUEDA AVANZADA ************************/
/**************************************************************/
function mostrarFiltrosProductos(bChecked){
	if(bChecked){
		document.getElementById('filtrosProductos').className = '';
		document.getElementById('lnkBusqueda1').style.display = 'none';
	}else{
		document.getElementById('filtrosProductos').className = 'ifrmDownload';
		document.getElementById('lnkBusqueda1').style.display = '';
	}
}

function buscarAvanzada(){
	var bChecked = false;
	var frm = document.frmBuscar;
	
	if(document.getElementById('chkBusqueda1').checked) bChecked = true;
	if(document.getElementById('chkBusqueda2').checked) bChecked = true;
	if(document.getElementById('chkBusqueda3').checked) bChecked = true;
	if(document.getElementById('chkBusqueda4').checked) bChecked = true;

	if(bChecked){
		frm.submit();
	}else{
		showAlert(12);
	}
}
	
function irA(strUrl){
	if(strUrl==undefined) strUrl = "index.php";
	window.location.href = strUrl;
}
/**************************************************************/
/*************************** FICHA DVD ************************/
/**************************************************************/

function getHeight(divId){
	var divHeight = 0;
	document.getElementById(divId).style.height="auto";
	if(document.getElementById(divId).clientHeight) {
		divHeight = document.getElementById(divId).clientHeight;
	} else if(document.getElementById(divId).offsetHeight) {
		divHeight = document.getElementById(divId).offsetHeight;
	} else if(document.getElementById(divId).innerHeight) {
		divHeight = document.getElementById(divId).innerHeight;
	} else if(document.getElementById(divId).scrollHeight) {
		divHeight = document.getElementById(divId).scrollHeight;
	}
	if(divHeight>0) divHeight = divHeight - 65;
	if(divHeight<0) divHeight = 0;
	return divHeight;
}


function arreglarAlto(div1, div2){
	
	if((getHeight(div1)+63) > (205)){
		document.getElementById(div2).style.height = (getHeight(div1)+58)+"px";	
	}else{

		document.getElementById(div1).style.height = (205)+"px";	
	
	}
}
