// JavaScript Document
var ie  = document.all;
var nn6 = document.getElementById && !document.all;
 
var isdrag=false;
var x,y;
var dobj;
 
function movemouse(e){
 if (isdrag){
  dobj.style.left = nn6 ? (tx+e.clientX-x)+"px" : tx+event.clientX-x;
  dobj.style.top  = nn6 ? (ty+e.clientY-y)+"px" : ty+event.clientY-y;
  return false;
 }
}
 
function selectmouse(e) {
 var fobj = nn6 ? e.target : event.srcElement;
 var topelement = nn6 ? "HTML" : "BODY";
 while (fobj.tagName!=topelement && fobj.className!="ampliacion"){
  fobj = nn6 ? fobj.parentNode : fobj.parentElement;
 }
 if (fobj.className=="ampliacion"){
  isdrag = true;
  //dobj  = fobj.cloneNode(true);
  dobj = fobj;
  
  if (dobj.filters) {
   try {
    dobj.style.filter="alpha(opacity=70)";
   }
   catch (e) { }
  }
  else if (dobj.style.opacity) {
   dobj.style.opacity = .7;
  }
  
  tx = parseInt(dobj.style.left+0);
  ty = parseInt(dobj.style.top+0);
  x = nn6 ? e.clientX : event.clientX;
  y = nn6 ? e.clientY : event.clientY;
  
  document.onmousemove=movemouse;
  return false;
 }
}

 
function unselectmouse(e){
 if (dobj){
  if (dobj.filters) {
   try {
    dobj.style.filter="alpha(opacity=100)";
   }
   catch(e){ }
  }
 }
  isdrag=false;
  
 dobj=null;
}
 
document.onmousedown = selectmouse;
document.onmouseup   = unselectmouse;








function muestra_imagen(id,archivo,ancho,alto){
	//xInnerHtml('c1','')
	xWidth ('ampliacion',ancho + 12);
	xHeight ('ampliacion',alto + 6 + 20 + 18);
	xWidth ('c1',ancho);
	xHeight ('c1',alto);
	
	xWidth ('cerrarampliacion',ancho);
	
	imagen = new Image(ancho,alto); 
	imagen.src = "http://www.tiendaminiaturas.net/imagenes/articulos/" + id + "/" + archivo; 
	
	document.getElementById('c1').innerHTML ='<img src="http://www.tiendaminiaturas.net/imagenes/articulos/' + id + '/' + archivo + '" width="' + ancho + '" height="' + alto + '" border="0" align="middle" style="border: 1px solid #663300">';	
		
	pos_left = parseInt((xClientWidth()-ancho+3)/2);
	pos_top = xScrollTop() + parseInt((xClientHeight()-alto+10)/2);
	//if (pos_top>150) pos_top = 150
	if (pos_top<10) { pos_top=10; }
	//pos_left=200; // Mueve la ventana, el margen izquierdo
	xMoveTo('ampliacion',pos_left,pos_top);
	setTimeout("xShow('ampliacion')",100);
}

function cerrar_ampliacion(){
	xHide('ampliacion');
}

function cargarImagenes() {
	var cargaImagen = new Array();
	for (var i = 0; i < arguments.length; i++) {
		cargaImagen[i] = new Image(); 
		cargaImagen[i].src = arguments[i]; 
	} 
} 

function validarBusqueda() {
	if (document.getElementById('buscar').value == "") {
		alert('Por favor, introduce alguna palabra para comenzar la búsqueda.');	
		return false;
	}
	return true;
}
function cesta(idArticulo) {
	var cantidad = document.getElementById("cantidad").value;
	window.location="cesta.php?idA="+idArticulo+"&cantidad="+cantidad;
}
function checkEmail(str)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(str)){
		return true;
	}
	return false;
}
function validaReg() {
	if (document.frmRegistro.nombre.value == "" || document.frmRegistro.apellidos.value == "") {
		alert('Por favor, introduce tu nombre y apellidos.');
		return false;
	} else {
		if (document.frmRegistro.dnicif.value == "") {
			alert('Por favor, introduce tu NIF/CIF/NIE');
			return false;
		} else {
			if (document.frmRegistro.direccion.value == "" || document.frmRegistro.poblacion.value == "" || document.frmRegistro.cp.value == "" ||
				document.frmRegistro.provincia.value == "" || document.frmRegistro.pais.value == "" || document.frmRegistro.telefono.value == "" ||
				document.frmRegistro.movil.value == "") {
				alert('Por favor, introduce todos tus datos de contacto (dirección, población, cp, país, teléfono, móvil)');
				return false;
			} else { 
				if (document.frmRegistro.email.value != document.frmRegistro.email2.value) {
					alert('Las dirección de correo electrónico que has introducido no coincide con su confirmación.');
					return false;
				} else {
					if (!checkEmail(document.frmRegistro.email.value)) {
						alert('Por favor, introduce una dirección de correo electrónico válida.');
						return false;
					} else {
						if (document.frmRegistro.password.value != document.frmRegistro.password2.value) { 
							alert('La contraseña que has introducido no coincide con su confirmación');
							return false;
						} else {
							if (document.frmRegistro.password.value.length < 6 || document.frmRegistro.password.value.length > 10) {
								alert('La contraseña debe tener un mínimo de 6 caracteres y un máximo de 10.');	
								return false;
							} else {
								if (document.frmRegistro.txtimagen.value == "") {
									alert('Por favor, introduce en texto de la imagen.');
									return false;
								} else {
									return true;	
								}
							}
						}
					}
				}
			}
		}
	}
}

function validaVal() {
	
	if (document.frmValidar.v_pass.value == "" || !checkEmail(document.frmValidar.v_email.value)) {
		alert('Debes introducir una dirección de correo electrónico y una contraseña validas.');
		return false;
	} else { 
		return true;
	}
}
function validaRec() {
	
	if (!checkEmail(document.frmRegenerar.r_email.value)) {
		alert('Debes introducir una dirección de correo electrónico para comenzar el proceso de regeneración de la contraseña.');
		return false;
	} else { 
		return true;
	}
}
function validaPass() {
	if (!checkEmail(document.frmNuevaPass.email.value)) {
		alert('Debes introducir una dirección de correo electrónico válida.');
		return false;
	} else { 
		if (document.frmNuevaPass.passwd.value != document.frmNuevaPass.passwd2.value) { 
			alert('La contraseña que has introducido no coincide con su confirmación');
			return false;
		} else {
			if (document.frmNuevaPass.passwd.value.length < 6 || document.frmNuevaPass.passwd.value.length > 10) {
				alert('La contraseña debe tener un mínimo de 6 caracteres y un máximo de 10.');	
				return false;
			} else {
				return true;	
			}
		}
	}
}
function activacionEnvio() {
	if (document.getElementById("envio").checked == true) {
		document.getElementById("en_direccion").disabled = false;
		document.getElementById("en_poblacion").disabled = false;
		document.getElementById("en_cp").disabled = false;
		document.getElementById("en_provincia").disabled = false;
		document.getElementById("en_pais").disabled = false;
		
	} else {
		document.getElementById("en_direccion").disabled = true;
		document.getElementById("en_poblacion").disabled = true;
		document.getElementById("en_cp").disabled = true;
		document.getElementById("en_provincia").disabled = true;
		document.getElementById("en_pais").disabled = true;		
	}
}
function confirmarDatos(accion) {
	if (document.frmDatos.nombre.value == "" || document.frmDatos.apellidos.value == "") {
		alert('Por favor, introduce tu nombre y apellidos.');
	} else {
		if (document.frmDatos.dnicif.value == "") {
			alert('Por favor, introduce tu NIF/CIF/NIE');
		} else {
			if (document.frmDatos.direccion.value == "" || document.frmDatos.poblacion.value == "" || document.frmDatos.cp.value == "" ||
				document.frmDatos.provincia.value == "" || document.frmDatos.pais.value == "" || document.frmDatos.telefono.value == "" ||
				document.frmDatos.movil.value == "") {
				alert('Por favor, introduce todos tus datos de contacto (dirección, población, cp, país, teléfono, móvil)');
			} else { 
				if (document.frmDatos.email.value != document.frmDatos.email2.value) {
					alert('Las dirección de correo electrónico que has introducido no coincide con su confirmación.');
				} else {
					if (!checkEmail(document.frmDatos.email.value)) {
						alert('Por favor, introduce una dirección de correo electrónico válida.');
					} else {
						if (document.frmDatos.txtimagen.value == "") {
							alert('Por favor, introduce en texto de la imagen.');
						} else {
							if (document.frmDatos.envio.checked == true) {
								if (document.frmDatos.en_direccion.value == "" || document.frmDatos.en_poblacion.value == "" || document.frmDatos.en_cp.value == "" || 
									document.frmDatos.en_provincia.value == "" || document.frmDatos.en_pais.value == "") {
										alert("Debes completar los datos de la dirección de entrega.");
								} else {
									
									if (accion == 2) {
										
										if (document.getElementById('pago_0').checked == false && document.getElementById('pago_1').checked == false) {
											alert("Por favor, selecciona una forma de pago.");
										} else {										
											if (document.frmDatos.condiciones.checked == false) {
												alert("Debes aceptar las condiciones de compra y devolución para finalizar tu compra.");
											} else {
												document.frmDatos.accion.value = accion;
												document.frmDatos.submit();										
											}
										}
									} else {
										document.frmDatos.accion.value = accion;
										document.frmDatos.submit();
									}										
								}
							} else {
								if (accion == 2) {
									if (document.getElementById('pago_0').checked == false && document.getElementById('pago_1').checked == false) {
										alert("Por favor, selecciona una forma de pago.");
									} else {									
									
										if (document.frmDatos.condiciones.checked == false) {
											alert("Debes aceptar las condiciones de compra y devolución para finalizar tu compra.");
										} else {
											document.frmDatos.accion.value = accion;
											document.frmDatos.submit();										
										}
									}
								} else {
									document.frmDatos.accion.value = accion;
									document.frmDatos.submit();
								}
							}							
						}
					}
				}
			}
		}
	}	
}
function validaContacto() {
	if (document.frmContacto.contacto.value == "" || !checkEmail(document.frmContacto.email.value) || document.frmContacto.telefono.value == "" || document.frmContacto.observaciones.value == "") {
		alert('Por favor, rellena correctamente todos los campos del formulario.');
		return false;
	} else {
		return true;
	}	
}