function fndCnt(val,open)
{
   if (window.document.getElementById('cont'))
      {
	      if (open=="1")
	         window.open ("?op=1&ap=" + val);
	      else
		      window.cont.location.href = "?op=1&ap=" + val;
	   }
	else
		this.location.href = "?op=1&ap=" + val;

}

function goTo(url,open)
{
   if (open=="1")
	   window.open(url)
	else
		window.cont.location.href = url;
}

function fndIdioma(val)
{
	window.document.getElementById('i').idi.value=val;
	window.document.getElementById('i').submit();
}

function fndC(pf,n,pa,ifm,ida,cnt,b,str,e)
{

  	if (document.getElementById('m'))
	      w = this.document;
	else
	      w = window.frames[0].document;

	var frame = w.m

	if (e=="carr")
	{
		frame.cnt.value = w.getElementById(ida).value;
		alert('Introducirá un producto');
	}

	if (b=="a")
		frame.str.value = w.getElementById("busc2").value;
   else if (b=="f")
		frame.str.value = w.getElementById("busc1").value;

    frame.e.value = e;
	frame.pf.value = pf;
	frame.n.value = n;
	frame.pa.value = pa;
	frame.ifm.value = ifm;
	frame.ida.value = ida;
	frame.b.value = b;
	frame.submit();
}

function fndP(pc,pl,il,str,e,b)
{

	if (document.getElementById('m'))
		w = this.document;
	else
		w = window.frames[0].document;

	var frame = w.m;

	if (e=="delc" || e=="dell")
		{
			var txt = "¿Deseas borrar las linea(s) selecionada(s)?";
         if (e=="delc")
         	{
	         	chk=w.c;
	         }
	      else if (e=="dell")
	      	{
	         	chk=w.l;
	         }
         var cnt = 0;
	      for (var x=0; x < chk.length; x++) {
	         if (chk[x].checked) {
					if (cnt == 0)
		            frame.d.value += chk[x].value;
		         else
						frame.d.value += ',' + chk[x].value;
					cnt=cnt+1;
	         }
	      }

	      if (cnt==0)
	      	return;

			if (confirm(txt)==false)
				{
	            frame.d.value ="";
	            return;
				}

		}
	else if (e=="dped")
	   {
         var txt = "¿Deseas duplicar este pedido?";
         var chk=w.c;//Cheks de pedidos
         var cnt = 0;
	      for (var x=0; x < chk.length; x++) {
	         if (chk[x].checked) {
					if (cnt == 0)
		            frame.d.value += chk[x].value;
		         else
						frame.d.value += ',' + chk[x].value;
					cnt=cnt+1;
	         }
	      }
			if (cnt==0)
	      	return;

			if (confirm(txt)==false)
				{
	            frame.d.value ="";
	            return;
				}

	   }

	frame.pc.value = pc;
   frame.pl.value = pl;
   frame.il.value = il;
   frame.str.value = str;
   frame.e.value = e;
   frame.b.value = b;

	if (e=="busc")
		{
	      frame.b.value = w.getElementById("busc1").value;
	      if (frame.b.value=="")
	         return;
		}

	if (e=="busl")
		{
	   	frame.b.value = w.getElementById("busc2").value;
			if (frame.b.value=="")
				return
		}

   frame.submit();
}

function popUp(ruta) {
var left = (window.screen.width - 600)/2;
var top=(window.screen.height-400)/2;

	wnd = window.open("?op=5&i=" + ruta,'imagen','width=640,height=400,toolbar=No,location = No,scrollbars=Yes,status=No,resizable=No,fullscreen =No,top='+ top +',left='+ left +'"+"');
}

function abrirtexto(titulo,cab,txt)
	{
	   var left = (window.screen.width - 300)/2;
	   var top=(window.screen.height-300)/2;
	   OpenWindow=window.open("", "Info", 'height=300, width=300,toolbar=no,scrollbars=No,menubar=no,top='+ top +',left='+ left +'"+"');
	   OpenWindow.document.write("<TITLE> :::..." + titulo +  "...::: </TITLE>")
	   OpenWindow.document.write("<BODY>")
	   OpenWindow.document.write("<table width='100%' height='100%' border='0' cellpadding='0' cellspacing='0'>")
	   OpenWindow.document.write("<tr>")
	   OpenWindow.document.write("<td width='20' height='20'><div align='center'><img src='0/img/blanca/1.gif' width='20' height='20' /></div></td>")
	   OpenWindow.document.write("<td height='20' background='/0/img/blanca/2.gif'><div align='center'></div></td>")
	   OpenWindow.document.write("<td width='20' height='20'><div align='center'><img src='/0/img/blanca/3.gif' width='20' height='20' /></div></td>")
	   OpenWindow.document.write("</tr>")
	   OpenWindow.document.write("<tr>")
	   OpenWindow.document.write("<td width='20' background='0/img/blanca/8.gif'><div align='center'></div></td>")
	   OpenWindow.document.write("<td><div align='center'><h3><font color='#333333' size='2' face='Arial, Helvetica, sans-serif'><div align='center'>" + cab + "<br><br>" + txt + "</div></font></h3></div></td>")
	   OpenWindow.document.write("<td width='20' background='0/img/blanca/4.gif'><div align='center'></div></td>")
	   OpenWindow.document.write("</tr>")
	   OpenWindow.document.write("<tr>")
	   OpenWindow.document.write("<td width='20' height='20'><div align='center'><img src='0/img/blanca/7.gif' width='20' height='20' /></div></td>")
	   OpenWindow.document.write("<td background='0/img/blanca/6.gif'><div align='center'></div></td>")
	   OpenWindow.document.write("<td width='20' height='20'><div align='center'><img src='0/img/blanca/5.gif' width='20' height='20' /></div></td>")
	   OpenWindow.document.write("</tr>")
	   OpenWindow.document.write("</table>")
	   OpenWindow.document.write("</BODY>")
	   OpenWindow.document.write("</HTML>")
	   OpenWindow.document.close()
	}

function imprimirpedido()
{
	   var left = (window.screen.width-780)/2;
	   var top=(window.screen.height-600)/2;

			var id = "";
			var cnt = 0;

				if (document.getElementById('m'))
					w = this.document;
				else
					w = window.frames[0].document;

				chk=w.c;

				for (var x=0; x < chk.length; x++) {
					if (chk[x].checked) {
						if (cnt == 0)
							id += chk[x].value;
						else
							id += ',' + chk[x].value;
					cnt=cnt+1;
					}
				}
	   OpenWindow=window.open("?op=4&pedidos=" + id + "&contador=" + cnt , "Imprimir", 'height=600, width=780,toolbar=no,scrollbars=Yes,menubar=no,resizable=Yes,top='+ top +',left='+ left +'"+"');
}

function esEmail(id)
{
   if (id)
   {
	   var s = id.value;
	   var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	   if (s.length == 0 )
	      {
	         return false;
	      }
	   if (filter.test(s))
	      {
	         return false;
	      }
	   else
	      {
	         return true;
	      }
	}
	return true;
}

function noValido(id)
{
   if (id)
   {
	   if (id.value.length==0)
	   {
	      return true;
	   }
   }
	return false;
}

function fndValDatos()
{
   var frames = window.frames[0].document;
   var porvalidar = new Array();
   if (noValido(frames.getElementById('nombre')))
   {
      porvalidar.push("Nombre");
   }
   if (noValido(frames.getElementById('apellido1')))
   {
      porvalidar.push("apellido 1");
   }
   if (noValido(frames.getElementById('apellido2')))
   {
      porvalidar.push("apellido 2");
   }
   if (noValido(frames.getElementById('cif')))
   {
      porvalidar.push("CIF");
   }
   if (noValido(frames.getElementById('tipo')))
   {
		porvalidar.push('Tipo de via');
   }
   if (noValido(frames.getElementById('nombred')))
   {
		porvalidar.push('Nombre de direción');
   }
   if (noValido(frames.getElementById('num')))
   {
		porvalidar.push('Numero');
   }
   if (noValido (frames.getElementById('piso')))
   {
		porvalidar.push('Piso');
   }
   if (noValido (frames.getElementById('cp')))
   {
		porvalidar.push('CP');
   }
   if (noValido (frames.getElementById('provincia')))
   {
		porvalidar.push('Provincia');
   }
   if (noValido (frames.getElementById('tel')))
   {
		porvalidar.push('Teléfono');
   }
   if (noValido (frames.getElementById('fax')))
   {
		porvalidar.push('FAX');
   }
   if (esEmail(frames.getElementById('mail')))
   {
		porvalidar.push('email');
   }
   if (noValido (frames.getElementById('com')))
   {
		porvalidar.push('Comentarios');
   }
   //
   if (porvalidar.length == 0)
   {
	   if (frames.getElementById('chkterms'))
	      {
	      if (frames.getElementById('chkterms').checked != true)
	         {
	            alert ("Aceptar terminos");
	            return;
	         }
	      }

		m = window.frames[0].document.getElementById('m');
		m.enombre.value = frames.getElementById('nombre').value;
		m.eapellido1.value = frames.getElementById('apellido1').value;
		m.eapellido2.value = frames.getElementById('apellido2').value;
		m.ecif.value = frames.getElementById('cif').value;
		m.etipo.value = frames.getElementById('tipo').value;
		m.enombred.value = frames.getElementById('nombred').value;
		m.enumero.value = frames.getElementById('num').value;
 		m.episo.value = frames.getElementById('numpiso').value;
		m.ecp.value = frames.getElementById('cp').value;
		m.eprovincia.value = frames.getElementById('provincia').value;
		m.etel.value = frames.getElementById('tel').value;
		m.efax.value = frames.getElementById('fax').value;
		m.email.value = frames.getElementById('mail').value;
		m.ecom.value = frames.getElementById('com').value;
		m.e.value = "val";
		m.submit();
   }
   else
   {
		var i = 0;
		var v = "";
	   for(i=0; i<porvalidar.length; i++) // does not work
	   {
		   v = v + "\n" + porvalidar[i];
	   }
	   alert ("Datos que faltan o incorrectos:\n" + v);
   }

}
