//Proyecto: Biblioteca Javascript
//©2001 Equipo de desarrollo de Calidad y Mantenimiento.
//
function Trim(sDato)
	{
	for (i=0;i<sDato.length;i++)
	{
		if (sDato.charAt(i)==" ")
			{sDato=sDato.substring(i+1,sDato.length);i--;}
 		else
 			break;
	}
	for (i=sDato.length-1;i>=0;i--)
	{
		if (sDato.charAt(i)==" ")
 			sDato=sDato.substring(0,i);
		else
			break;
	}
	return sDato;
	}
//
var confirma_email=1;
var ant="";
function Validar_prac(theForm)
{
	if(Trim(theForm.apellido1.value)=="")
	{
		alert("Escriba un valor para el campo \"Primer Apellido\".");
		theForm.apellido1.focus();
		return (false);
	}
	if(Trim(theForm.apellido1.value).length<3)
	{
		alert("Escriba por lo menos 3 caracteres en el campo \"Primer Apellido\".");
		theForm.apellido1.focus();
		return (false);
	}
	if(theForm.apellido1.value.length>60)
	{
		alert("Escriba como máximo 60 caracteres en el campo \"Primer Apellido\".");
		theForm.apellido1.focus();
		return (false);
	}
	if(theForm.apellido2.value.length>60)
	{
		alert("Escriba como máximo 60 caracteres en el campo \"Segundo Apellido\".");
		theForm.apellido2.focus();
		return (false);
	}
	if(theForm.apellido2.value.length>60)
	{
		alert("Escriba como máximo 60 caracteres en el campo \"Segundo Apellido\".");
		theForm.apellido2.focus();
		return (false);
	}
	if(theForm.apellido2.value.length>60)
	{
		alert("Escriba como máximo 60 caracteres en el campo \"Segundo Apellido\".");
		theForm.apellido2.focus();
		return (false);
	}
	if(Trim(theForm.nombreorigen.value)=="")
	{
		alert("Escriba un valor para el campo \"Nombre\".");
		theForm.nombreorigen.focus();
		return (false);
	}
	if(Trim(theForm.nombreorigen.value).length<3)
	{
		alert("Escriba por lo menos 3 caracteres en el campo \"Nombre\".");
		theForm.nombreorigen.focus();
		return (false);
	}
	if(theForm.nombreorigen.value.length>60)
	{
		alert("Escriba como máximo 60 caracteres en el campo \"Nombre\".");
		theForm.nombreorigen.focus();
		return (false);
	}
	if(Trim(theForm.direccion.value)=="")
	{
		alert("Escriba un valor para el campo \"Dirección postal\".");
		theForm.direccion.focus();
		return (false);
	}
	if(Trim(theForm.direccion.value).length<3)
	{
		alert("Escriba por lo menos 3 caracteres en el campo \"Dirección postal\".");
		theForm.direccion.focus();
		return (false);
	}
	if(theForm.direccion.value.length>80)
	{
		alert("Escriba como máximo 80 caracteres en el campo \"Dirección postal\".");
		theForm.direccion.focus();
		return (false);
	}
	if(Trim(theForm.CP.value)=="")
	{
		alert("Escriba un valor para el campo \"Código Postal\".");
		theForm.CP.focus();
		return (false);
	}
	if(theForm.CP.value.length>15)
	{
		alert("Escriba como máximo 15 caracteres en el campo \"Código Postal\"");
		theForm.CP.focus();
		return (false);
	}
	if(Trim(theForm.localidad.value)=="")
	{
		alert("Escriba un valor para el campo \"Localidad\".");
		theForm.localidad.focus();
		return (false);
	}
	if(Trim(theForm.localidad.value).length<2)
	{
		alert("Escriba por lo menos 2 caracteres en el campo \"Localidad\".");
		theForm.localidad.focus();
		return (false);
	}
	if(theForm.localidad.value.length > 60)
	{
		alert("Escriba como máximo 60 caracteres en el campo \"Localidad\".");
		theForm.localidad.focus();
		return (false);
	}
  pos_prov=theForm.provincia.selectedIndex;
	if(theForm.provincia.options[pos_prov].text=="Seleccione provincia")
	{
		alert("Debe seleccionar un valor para el campo \"Provincia\".");
		theForm.provincia.focus();
		return (false);
	}
	if(Trim(theForm.telefono.value)=="")
	{
		alert("Escriba un valor para el campo \"Teléfono de contacto\".");
		theForm.telefono.focus();
		return (false);
	}
	if(Trim(theForm.telefono.value).length<9)
	{
		alert("Escriba por lo menos 9 caracteres en el campo \"Teléfono de contacto\".");
		theForm.telefono.focus();
		return (false);
	}
	if(theForm.telefono.value.length>30)
	{
		alert("Escriba como máximo 30 caracteres en el campo \"Teléfono de contacto\".");
		theForm.telefono.focus();
		return (false);
	}
	var checkOK="0123456789-,.";
	var checkStr=theForm.telefono.value;
	var allValid=true;
	var decPoints=0;
	var allNum = "";
	for(i=0;i<checkStr.length;i++)
	{
		ch=checkStr.charAt(i);
		for(j= 0;j<checkOK.length;j++)
			if(ch==checkOK.charAt(j))
	  		break;
		if (j==checkOK.length)
		{
			allValid=false;
			break;
		}
		if(ch==",")
		{
			allNum+=".";
			decPoints++;
		}
		else if(ch!=".")
			allNum+=ch;
	}
	if(!allValid)
	{
		alert("Escriba sólo dígitos numéricos en el campo \"Teléfono\".");
		theForm.telefono.focus();
		return (false);
	}
	if(decPoints>1)
	{
		alert("Escriba un número válido en el campo \"Teléfono\".");
		theForm.telefono.focus();
		return (false);
	}
	if(Trim(theForm.fax.value)==""){
		theForm.fax.value="";
	}else{
		if(Trim(theForm.fax.value).length<9)
		{
			alert("Escriba por lo menos 9 caracteres en el campo \"Fax\".");
			theForm.fax.focus();
			return (false);
		}
		if(theForm.fax.value.length>30)
		{
			alert("Escriba como máximo 30 caracteres en el campo \"Fax\".");
			theForm.fax.focus();
			return (false);
		}
		var checkStr=theForm.fax.value;
		var allValid=true;
		var decPoints=0;
		var allNum = "";
		for(i=0;i<checkStr.length;i++)
		{
			ch=checkStr.charAt(i);
			for(j= 0;j<checkOK.length;j++)
				if(ch==checkOK.charAt(j))
		  		break;
			if (j==checkOK.length)
			{
				allValid=false;
				break;
			}
			if(ch==",")
			{
				allNum+=".";
				decPoints++;
			}
			else if(ch!=".")
				allNum+=ch;
		}
		if(!allValid)
		{
			alert("Escriba sólo dígitos numéricos en el campo \"Fax\".");
			theForm.fax.focus();
			return (false);
		}
		if(decPoints>1)
		{
			alert("Escriba un número válido en el campo \"Fax\".");
			theForm.fax.focus();
			return (false);
		}
	}
	if(Trim(theForm.emailorigen.value)=="")
	{
		alert("Escriba un valor para el campo \"E-mail\".");
		theForm.emailorigen.value="";
		theForm.emailorigen.focus();
		return (false);
	}
	if(Trim(theForm.emailorigen.value).length<5)
	{
		alert("Escriba por lo menos 5 caracteres en el campo \"E-mail\".");
		theForm.emailorigen.focus();
		return (false);
	}
	var email = theForm.emailorigen.value;
	var cont=0;
	for (i=0; i<email.length; i++)
	{
		if (email.charAt(i) == "@")
			cont++;
	}
	if (cont>1)
	{
		alert("Introduzca un valor válido en el campo \"E-mail\".\nIntroduzca una sóla \"@\" en el campo.");
		theForm.emailorigen.focus();
		return (false);
	}
	var pos = email.indexOf("@");
	if (pos == -1)
	{
		alert("Introduzca un valor válido en el campo \"E-mail\".\nIntroduzca una \"@\" en el lugar correspondiente de su e-mail.");
		theForm.emailorigen.focus();
		return (false);
	}
	if ((pos == 0) || (pos == email.length-1))
	{
		alert("Introduzca un valor válido en el campo \"E-mail\".\nEl símbolo \"@\" no puede colocarse ni al principio ni al final de su dirección.");
		theForm.emailorigen.focus();
		return (false);
	}
	var posini=email.indexOf(".");
	if (posini==0)
	{
		alert("Introduzca un valor válido en el campo \"E-mail\".\nUna dirección de correo electrónico no puede comenzar por el símbolo\".\".");
		theForm.emailorigen.focus();
		return (false);
	}
	var pos3=email.lastIndexOf(".");
	if (pos3==-1 || pos3<pos)
	{
		alert("Introduzca un valor válido en el campo \"E-mail\".\nIntroduzca un \".\" para separar su nombre de dominio del nombre de dominio de alto nivel.");
		theForm.emailorigen.focus();
		return (false);
	}
	if ((pos3 == pos+1) || pos3 == email.length-1)
	{
		alert("Introduzca un valor válido en el campo \"E-mail\".\nUna dirección de correo electrónico no puede terminar con el símbolo\".\", ni llevar este símbolo en la posición adyacente a la \"@\".");
		theForm.emailorigen.focus();
		return (false);
	}
	if(theForm.emailorigen.value!=ant)
  	confirma_email=1;
  if(confirma_email){
		ant=theForm.emailorigen.value;
	  var checkemail="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.@";
	  var checkStr = theForm.emailorigen.value;
	  var allValid = true;
	  for(i=0;i<checkStr.length;i++)
	  {
	    ch=checkStr.charAt(i);
	    for (j=0;j<checkemail.length;j++)
	      if (ch==checkemail.charAt(j))
	        break;
	    if (j == checkemail.length)
	    {
	      allValid=false;
	      break;
	    }
	  }
	  if (!allValid)
	  {
			alert("El sistema ha detectado un caracter extraño en el campo \"E-mail\".\nRevise sus datos y en el caso de detectar algún error, modifique su dirección en el cuadro de diálogo que aparecerá cuando pulse \"Aceptar\".\nSi cree que sus datos son correctos pulse \"Aceptar\" en el cuadro de diálogo.");
			mod=prompt("Modifique su dirección o pulse \"Aceptar\" para confirmarla.",ant);
	    if (mod==ant){
	    	theForm.emailorigen.value=ant;
	    	confirma_email=0;
	    }else if (mod!=null){	
				theForm.emailorigen.value=mod;    	
		    return (false);
		  }
	  }
	}
	return (true);
}
//