//funcion para marcar los radio buttons del pago
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function emailCheck (emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		alert("La dirección de e-mail es incorrecta")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
    	alert("La dirección de e-mail es incorrecta.")
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
	    	if (IPArray[i]>255) {
	        	alert("La dirección de e-mail es incorrecta")
				return false
			}
		}
	return true
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("La dirección de e-mail es incorrecta.")
	    return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
		alert("la dirección de e-mail es incorrecta.")
		return false
	}
	if (len<2) {
   		var errStr="La dirección de e-mail es incorrecta"
		alert(errStr)
		return false
	}
return true;
}

function foto (welke) {
	if (document.getElementById) {
		document.getElementById('screenshot').src = welke.href;
		return false;
	}
}

function toonverberg( targetId ){
  if (document.getElementById){
  		target = document.getElementById( targetId );
  			if ((target.style.display == "none")||(target.style.display == "")){
  				target.style.display = "inline";
  			} else {
  				target.style.display = "none";
  			}
  	}
}

function ira_marcas(){if (document.frmMarcas.marcas.value!=0){location = document.frmMarcas.marcas.options[document.frmMarcas.marcas.selectedIndex].value}}

var nav4 = window.Event ? true : false;
function acceptNum(evt){
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
var key = nav4 ? evt.which : evt.keyCode;
return (key <= 13 || (key >= 48 && key <= 57));
}

function mostrar(c){
	if(document.getElementById(""+c+"").style.display=="none"){
		document.getElementById(""+c+"").style.display="block";
	}else{
		document.getElementById(""+c+"").style.display="none";
	}
	
}

function anadirCarrito(c){
	formulario=document.forms.frmProducto;
	if(formulario.opciones_procesar.value==1 && formulario.opciones.selectedIndex==0){
			alert("Debes escoger una opción para el producto");
			formulario.opciones.focus();
	}else if(formulario.cantidad.value=="" || formulario.cantidad.value==0){
		alert("Debes comprar como mínimo una unidad del producto");
		formulario.cantidad.focus();
	}else{
		formulario.action="carrito_operaciones.php?accion=anadir&id_producto="+c;
		formulario.submit();
	}
}

function eliminarProducto(c){
	location.href='carrito_operaciones.php?accion=eliminar_producto&id_itemCarrito='+c;
}
function aumentarProducto(c){
	location.href='carrito_operaciones.php?accion=aumentar_producto&id_itemCarrito='+c;
}
function disminuirProducto(c){
	location.href='carrito_operaciones.php?accion=disminuir_producto&id_itemCarrito='+c;
}
function mostrarEmpresa(){
	if(document.forms.datos.tipo[0].checked==true){
		document.getElementById("empresaTR").style.display="none";
		document.getElementById("nifTR").style.display="none";
		document.forms.datos.empresa.value="";
		document.forms.datos.nif.value="";
	}else{
		document.getElementById("empresaTR").style.display="block";
		document.getElementById("nifTR").style.display="block";
	}
}
function mostrarDireccionFactura(){
	if(document.forms.datos.direccion_factura[0].checked==true){
		document.getElementById("tblFacturacion").style.display="none";
	}else{
		document.getElementById("tblFacturacion").style.display="block";
	}
}
function validarEnvio(){
	formulario=document.forms.datos;
	comprobado=1;
	if(!emailCheck(formulario.email.value)){
		formulario.email.focus();
		comprobado=0;
	}
	if(formulario.tipo[1].checked==true && comprobado==1){
		if(formulario.empresa.value=="" && comprobado==1){
			alert("Debes completar el nombre de la empresa");
			formulario.empresa.focus();
			comprobado=0;
		}
		if(formulario.nif.value=="" && comprobado==1){
			alert("Debes introducir el NIF/CIF");
			formulario.nif.focus();
			comprobado=0;
		}
	}
	if(formulario.nombre.value=="" && comprobado==1){
		alert("Debes introducir el nombre");
		formulario.nombre.focus();
		comprobado=0;
	}
	if(formulario.apellidos.value=="" && comprobado==1){
		alert("Debes introducir los apellidos");
		formulario.apellidos.focus();
		comprobado=0;
	}
	if(formulario.calle.value=="" && comprobado==1){
		alert("Debes introducir la Calle");
		formulario.calle.focus();
		comprobado=0;
	}
	if(formulario.cp.value=="" && comprobado==1){
		alert("Debes introducir el código postal");
		formulario.cp.focus();
		comprobado=0;
	}
	if(formulario.poblacion.value=="" && comprobado==1){
		alert("Debes introducir la población");
		formulario.poblacion.focus();
		comprobado=0;
	}
	if(formulario.pais.value=="" && comprobado==1){
		alert("Debes introducir el país");
		formulario.pais.focus();
		comprobado=0;
	}
	if(formulario.vender_internacional.value==0){
		if(formulario.provincia.selectedIndex==0 && comprobado==1){
			alert("Debes seleccionar la provincia");
			formulario.provincia.focus();
			comprobado=0;
		}
	}else{
		if(formulario.provincia.value=="" && comprobado==1){
			alert("Debes seleccionar la provincia");
			formulario.provincia.focus();
			comprobado=0;
		}
	}
	if(formulario.direccion_factura.value==1){
		if(formulario.direccion_factura[1].checked==true && comprobado==1){
			if(formulario.factura_empresa.value=="" && comprobado==1){
				alert("Debes completar el nombre de la empresa");
				formulario.factura_empresa.focus();
				comprobado=0;
			}
			if(formulario.factura_nif.value=="" && comprobado==1){
				alert("Debes introducir el NIF/CIF");
				formulario.factura_nif.focus();
				comprobado=0;
			}
			if(formulario.factura_nombre.value=="" && comprobado==1){
				alert("Debes introducir el nombre");
				formulario.factura_nombre.focus();
				comprobado=0;
			}
			if(formulario.factura_apellidos.value=="" && comprobado==1){
				alert("Debes introducir los apellidos");
				formulario.factura_apellidos.focus();
				comprobado=0;
			}
			if(formulario.factura_calle.value=="" && comprobado==1){
				alert("Debes introducir la calle");
				formulario.factura_calle.focus();
				comprobado=0;
			}
			if(formulario.factura_cp.value=="" && comprobado==1){
				alert("Debes introducir el código postal");
				formulario.factura_cp.focus();
				comprobado=0;
			}
			if(formulario.factura_poblacion.value=="" && comprobado==1){
				alert("Debes introducir la población");
				formulario.factura_poblacion.focus();
				comprobado=0;
			}
			if(formulario.vender_internacional.value==0){
				if(formulario.factura_provincia.selectedIndex==0 && comprobado==1){
					alert("Debes seleccionar la provincia");
					formulario.factura_provincia.focus();
					comprobado=0;
				}
			}else{
				if(formulario.factura_provincia.value=="" && comprobado==1){
					alert("Debes seleccionar la provincia");
					formulario.factura_provincia.focus();
					comprobado=0;
				}
			}
			if(formulario.factura_pais.value=="" && comprobado==1){
				alert("Debes introducir el país");
				formulario.factura_pais.focus();
				comprobado=0;
			}
		}
	}
	if(comprobado==1){
		formulario.action="pedido_datos.php?accion=procesar&tipo=sin_registro";
		formulario.submit();
	}
}
function copiarDatos(){
	formulario=document.forms.datos;
	formulario.factura_empresa.value=formulario.empresa.value;
	formulario.factura_nif.value=formulario.nif.value;
	formulario.factura_nombre.value=formulario.nombre.value;
	formulario.factura_apellidos.value=formulario.apellidos.value;
	formulario.factura_calle.value=formulario.calle.value;
	formulario.factura_cp.value=formulario.cp.value;
	formulario.factura_poblacion.value=formulario.poblacion.value;
	formulario.factura_pais.value=formulario.pais.value;
	formulario.factura_telefono.value=formulario.telefono.value;
}
