/*ACENTOS, eÑes y demás
á = \341 	Á = \301	à = \340	À = \300
é = \351	É = \311	è = \350	È = \310
í = \355	Í = \315	ì = \354	Ì = \314
ó = \363	Ó = \323	ò = \362	Ò = \322
ú = \372	Ú = \332	ù = \371	Ù = \331
ü = \374	Ü = \334	

ñ = \361	Ñ = \321
º = \260	ª = \252
*/

function validaEmail(elEmail){
	/*Con expresiones regulares
	NOTA: No se contemplan extensiones de dominio de más de 4 caracteres (.museum, .store) son mut poco frecuentes
	      No se contemplan las direcciones de correo con formato IP
	1.- \w+							---> que tenga al menos una palabra
	2.- ((-\w+)|(\.\w+)|(\_\w+))*	---> que tenga cero o más palabras precedidas de un (.), un (-) o un (_)
	3.- \@							---> una y sólo una @
	4.- [A-Za-z0-9]					---> sólo caracteres normales y números
	5.- ((.|-)[A-Za-z0-9]+)*		---> solo caracteres normales y números precedidos de un (.) o un (-)
	6.- \.							---> el último punto
	7.- [A-Za-z]{2,4}				---> de 2 a 4 caracteres después del punto. (4 por .info, .name)
	*/
	//	return (elEmail.search( /\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,4}/ ) != -1);
	// ¿Soporta expresiones regulares?
	var soporta = false;
	if (window.RegExp) {
		var aux = "a";
		var auxReg = new RegExp(aux);
		if (auxReg.test(aux)) soporta = true;
	}
	if (!soporta) return (elEmail.indexOf(".") > 2) && (elEmail.indexOf("@") > 0);
	var exp1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)");
	var exp2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
	return (!exp1.test(elEmail) && exp2.test(elEmail));
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=validaEmail(val);
        if (!p) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } 
  if (document.getElementById("leoCondiciones")){
	  if (!document.getElementById("leoCondiciones").checked)  errors+='- You should read and accept Terms Conditions.\n';
  }
  if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_validaForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validaForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=validaEmail(val);
        if (!p) errors+='- '+nm+' debe contener una direcci\363n e-mail correcta.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' debe contener un n\372mero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' debe contener un n\372mero entre '+min+' y '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es obligatorio.\n'; }
  } 
  if (document.getElementById("leoCondiciones")){
	  if (!document.getElementById("leoCondiciones").checked)  errors+='- Debes leer y aceptar las condiciones del pedido.\n';
  }
  if (errors) alert('Se han producido los siguientes errores:\n'+errors);
  document.MM_returnValue = (errors == '');
}

var hayUni = 0;
function hayPais(idioma){
	var pais = document.getElementById("pais");
	var uni = '';
	var errors = '';
	var i = 0;
	
	if (pais.value == '')
		if (idioma == 0)
			errors += '- Por favor, selecciona el pa\355s de destino para calcular los portes.\n';
		else
			errors += '- Please, select destination country to calculate shipping.\n';
	i=0;
	hayUni = 0;
	while (document.getElementById("unidades" + i)){
		uni = document.getElementById("unidades" + i).value;
		if (isNaN(uni)){
			if (idioma == 0)
				errors += '- Unidades debe contener un n\371mero.\n';
			else
				errors += '- Quantity must contain a number.\n';
		}else{
			if (uni != "0") hayUni = 1;
		}
		i++;
	}
  	if (errors) alert(errors);
  	document.MM_returnValue = (errors == '');
}

function continuaComprando(idioma){
	var estaAbierta = false;
	try {
		var ventana = opener.window;
		estaAbierta = true;
	} catch (error){
		estaAbierta = false
	} finally {
		if (estaAbierta){
			this.close();
		}else{
			if (idioma == 0)
				document.location.href = "http://www.mandalaluz.com/"
			else
				document.location.href = "http://www.mandalaartlight.com/";
		};
	}
}

function muestraFormulario(idioma){
	var errors = '';

	hayPais(idioma);

	if (document.MM_returnValue){
		if (hayUni == 0){
			if (idioma == 0)
				errors += '- Debe haber al menos una obra.\n';
			else
				errors += '- Must be at least one picture.\n';
		}else{
			document.getElementById("muestraDatos").value = "1";
		}
	
	 	if (errors) alert(errors);
	  	document.MM_returnValue = (errors == '');
	}
}
function abreFormulario(muestra){
	if (muestra){
		document.getElementById("datosPedido").style.display = "block";
		var paisSel = document.getElementById("pais");
		document.getElementById("_Pais").value = paisSel.options[paisSel.selectedIndex].text;
	}
}
function queFormaPago(pago){
	if (pago.value == 'tpv'){
		document.getElementById("numCuenta").style.display = "none";
	}else{
		document.getElementById("numCuenta").style.display = "block";
	}
}

function AbreVentana(pagina, nombre, ancho, alto, barra_scroll,resiz) {
	var winl = (screen.width - ancho) / 2;
	var wint = (screen.height - alto) / 2;
	winprops = 'height='+alto+',width='+ancho+',top='+wint+',left='+winl+',scrollbars='+barra_scroll+',resizable='+resiz;
	win = window.open(pagina, nombre, winprops);
	if (parseInt(navigator.appVersion) >= 4) win.focus();
}
