///// FUNCION VALIDA QUE TENGA LOS DATOS CAPTURADOS SEAN LOS CORRECTOS 
function validaDatosFactyEnv()
  {
      var nombre           	    =  document.DatosFactyEnvio.TextNombreApellidos.value; 
	  document.DatosFactyEnvio.TextCorreo.value 			= 	quita_espaciosform(document.DatosFactyEnvio.TextCorreo.value);
	  document.DatosFactyEnvio.TextCorreoConfirmar.value 	=	quita_espaciosform(document.DatosFactyEnvio.TextCorreoConfirmar.value);
      var correo             	=  document.DatosFactyEnvio.TextCorreo.value;
	  var correo1           	=  document.DatosFactyEnvio.TextCorreoConfirmar.value;	  
  
      var calle               	=  document.DatosFactyEnvio.TextCalleNumero.value;
      var colonia           	=  document.DatosFactyEnvio.TextColonia.value;
      var poblacion       	    =  document.DatosFactyEnvio.TextPoblacion.value;
      var cp                   	=  document.DatosFactyEnvio.TextCP.value;
	  var lada                	=  document.DatosFactyEnvio.Textlada.value;	  
      var telefono          	=  document.DatosFactyEnvio.TextTel.value;	 
      var pais    		        =  document.DatosFactyEnvio.CboPais.value;	  	  	  
	  
      var razonFact            	=  quita_espaciosform(document.DatosFactyEnvio.EditRazonSocial.value);	  
      var calleFact	  			=  document.DatosFactyEnvio.EditCalleNo.value;	  
      var coloniaFact          	=  document.DatosFactyEnvio.EditColBarrioZona.value;
      var poblacionFact       	=  document.DatosFactyEnvio.EditCiuPobDel.value;	  
      var cpFact                =  document.DatosFactyEnvio.EditCP.value;	  
      var ladaFact              =  document.DatosFactyEnvio.FactTextlada.value;	  	  
      var telefonoFact        	=  document.DatosFactyEnvio.FactTextTel.value;	 	  
	  var rfcFact	            =  document.DatosFactyEnvio.EditRFC.value;	  	  	  
      var paisFact		        =  document.DatosFactyEnvio.CboPaisFact.value;	  	  	  
//     var estado    		    =  document.DatosFactyEnvio.TextEstado.value;
     if (nombre.length==0)
     {
          alert("Por favor ingresa tu Nombre y Apellidos");
		  document.DatosFactyEnvio.TextNombreApellidos.focus();
          document.DatosFactyEnvio.TextNombreApellidos.select();
     }      
	 else
	 if (correo.length==0)
     {
          alert("Por favor ingresa tu Correo Electrónico");
		  document.DatosFactyEnvio.TextCorreo.focus();
          document.DatosFactyEnvio.TextCorreo.select();
     } 
	 else
	 if (!confirma_mailform ())
	 {
		  document.DatosFactyEnvio.TextCorreoConfirmar.focus();
          document.DatosFactyEnvio.TextCorreoConfirmar.select();
	 } 
	else
	 if (calle.length==0)
     {
          alert("Por favor ingresa el Nombre de la Calle y Número");
		  document.DatosFactyEnvio.TextCalleNumero.focus();
          document.DatosFactyEnvio.TextCalleNumero.select();
     }
	 else
     if (colonia.length==0)
     {
          alert("Por favor ingresa el Nombre de la Colonia/Barrio/Zona");
		  document.DatosFactyEnvio.TextColonia.focus();
          document.DatosFactyEnvio.TextColonia.select();
     }	
	 else
     if (poblacion.length==0)
     {
          alert("Por favor ingresa el Nombre de la Ciudad/Población/Delegación");
		  document.DatosFactyEnvio.TextPoblacion.focus();
          document.DatosFactyEnvio.TextPoblacion.select();
     }     
     else
	 if ((cp.length==0)&&(pais==484))	
     {
          alert("Por favor ingresa el Código Postal");
		  document.DatosFactyEnvio.TextCP.focus();
		  document.DatosFactyEnvio.TextCP.select();
     }
	 else
	 if ((lada.length==0))
     {
          alert("Por favor ingresa la clave LADA de tu Ciudad");
		  document.DatosFactyEnvio.Textlada.focus();
          document.DatosFactyEnvio.Textlada.select();
     }	 
	 else
	 if ((telefono.length==0))
     {
          alert("Por favor ingresa el Número Telefónico");
		  document.DatosFactyEnvio.TextTel.focus();
          document.DatosFactyEnvio.TextTel.select();
     }
	 else	 
     if (!valida_varios_emailform())
     {
   	      alert("La Dirección de Correo Electrónico: "+document.DatosFactyEnvio.TextCorreo.value+" es incorrecta, verifique por favor." );
		  document.DatosFactyEnvio.TextCorreo.focus();
          document.DatosFactyEnvio.TextCorreo.select();		  
//        return false;
     }	
	  else
	  if (!val_longcpform(cp, pais))
      {
		 alert("El código postal debe tener 5 números");	
		 document.DatosFactyEnvio.TextCP.focus();
         document.DatosFactyEnvio.TextCP.select();		  		  			 
	 }
	 else
      if ((!Cp_incorrectaform ())&&(pais==484))
      {
   	      alert("El CP es incorrecto, verifique por favor." );		  
		  document.DatosFactyEnvio.TextCP.focus();
          document.DatosFactyEnvio.TextCP.select();		  		  
//        return false;
      } 
	 else
     if (!val_ladaform())
	 {
//	        return false;
	 }
	 else	 
	 if (razonFact.length==0)
     {
          alert("Por favor ingresa tu Razón Social de Facturación");
	      document.DatosFactyEnvio.EditRazonSocial.focus();
          document.DatosFactyEnvio.EditRazonSocial.select();
     } 	 
	 else
	 if (calleFact.length==0)
     {
          alert("Por favor ingresa el Nombre de la Calle y Número de Facturación");
		  document.DatosFactyEnvio.EditCalleNo.focus();
          document.DatosFactyEnvio.EditCalleNo.select();
     }
	 else
	 if (coloniaFact.length==0)
     {
          alert("Por favor ingresa el Nombre de la Colonia/Barrio/Zona de Facturación");
		  document.DatosFactyEnvio.EditColBarrioZona.focus();
          document.DatosFactyEnvio.EditColBarrioZona.select();
     }	
	 else
     if (poblacionFact.length==0)
     {
          alert("Por favor ingresa el Nombre de la Ciudad/Población/Delegación de Facturación");
		  document.DatosFactyEnvio.EditCiuPobDel.focus();
          document.DatosFactyEnvio.EditCiuPobDel.select();
     }     
	else	 
	 if ((cpFact.length==0)&&(paisFact==484))	
     {
          alert("Por favor ingresa el Código Postal de Facturación");
		  document.DatosFactyEnvio.EditCP.focus();
		  document.DatosFactyEnvio.EditCP.select();
     }
	 else
	 if ((rfcFact.length==0)&&(paisFact==484))
     {
          alert("Por favor ingresa el RFC");
	      document.DatosFactyEnvio.EditRFC.focus();
          document.DatosFactyEnvio.EditRFC.select();
     }
	 else
	 if ((ladaFact.length==0))
     {
          alert("Por favor ingresa la clave LADA de tu Ciudad de Facturación");
		  document.DatosFactyEnvio.FactTextlada.focus();
          document.DatosFactyEnvio.FactTextlada.select();
     }	 	 
	 else
	 if ((telefonoFact.length==0))
     {
          alert("Por favor ingresa el Número Telefónico de Facturación");
		  document.DatosFactyEnvio.FactTextTel.focus();
          document.DatosFactyEnvio.FactTextTel.select();
     }	 
 	 else	 
	 if (!valida_regfedform(rfcFact))
	 {
		  alert("El RFC no debe tener guiones, espacios en blanco ni caracteres especiales. Verifica por favor");
		  document.DatosFactyEnvio.EditRFC.focus();
          document.DatosFactyEnvio.EditRFC.select();
	 }
	 else	 
	 if (!valida_rfcform(rfcFact))
	 {
<!--		  alert("El RFC . Verifica por favor");-->
		  document.DatosFactyEnvio.EditRFC.focus();
          document.DatosFactyEnvio.EditRFC.select();
	 }	 
  	 else	 	 
  	 if (!val_longcpform2(cpFact, paisFact))
	 {					
			alert("El código postal debe tener 5 números");	
		  	document.DatosFactyEnvio.EditCP.focus();
          	document.DatosFactyEnvio.EditCP.select();
	 }
	 else
     if ((!Cp_incorrectaform2 ())&&(paisFact==484))
     {
   	      alert("El CP es incorrecto, verifique por favor." );		  		 
		  document.DatosFactyEnvio.EditCP.focus();
          document.DatosFactyEnvio.EditCP.select();		  		  
     } 	 
	 else
     if (!val_ladaform2())
	 {
//	        return false;
	 }
	 else
     {
     	document.DatosFactyEnvio.submit(); 			 	 
	 }	 		 		 
}	 


/////////////////////////FUNCION QUE SE DERIVAN DE validaDatosFactyEnv//////////////////
///////////////////////////////////////////////////////////////////////////////////////
  
///// FUNCION VALIDA EMAIL'S SEAN IGUALES
 function confirma_mailform ()
    {
      var mail1            	=  document.DatosFactyEnvio.TextCorreo.value;
      var mail2           	=  document.DatosFactyEnvio.TextCorreoConfirmar.value;	  	  
	  
	  if (mail1 == mail2)
	    {
		  return true;
		}  
	  else	
        {
		  alert("La(s) Dirección(es) de Correo Electrónico y la(s) confirmación(es) son diferentes, verifica por favor");
		  //document.DatosFactyEnvio.TextCorreoConfirmar.focus;
		  //document.DatosFactyEnvio.TextCorreoConfirmar.select; 	  
		  return false;
	    }  
	}     
///// FUNCION VALIDA EMAIL QUE SEA CORRECTO SU FORMATO
 function valida_varios_emailform()
   {
	  var resultado = revisa_emailform();	  
	  if( resultado > 999) 
	    {
		   return true;	
		}
	  else
	    {
		  document.DatosFactyEnvio.TextCorreo.focus;
		  document.DatosFactyEnvio.TextCorreo.select; 	

          return false;	  
		}
   }
///// FUNCION VALIDA EMAIL QUE SEA CORRECTO SU FORMATO   
 function revisa_emailform()
   {
      var corr=document.DatosFactyEnvio.TextCorreo.value;
	  var cad_aux="";	  
	  var cont = 0;
	  var j = 0;
	  var k = 0;
	  var l =false;
	  var exito = 1000;
	  var filter=/^[A-Za-z0-9][A-Za-z0-9_.-]+[A-za-z]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.-]+[A-za-z]$/;
	  arrCorreos = new Array();	 
	  for(i=0;i<=corr.length;i++)
	    {
			if (corr.substring(i, i+1) != ",")
			  {
				if (corr.substring(i, i+1) != " ")
  				  cad_aux = cad_aux+corr.substring(i, i+1);   
			  }
			else 
			  {
				if (i < (corr.length-1))   
                   {				 
			          arrCorreos[j] = cad_aux;
				      j++;
				      cad_aux = "";	
				   }
				 else
				   {
					  /*Si el último caracter es ,, entonces al último elemento del arreglo se le agrega ese símbolo*/   
					  l = true;  
				   }
			  }
		}
	  if(l==true)
	    {
   	      arrCorreos[j] = cad_aux+",";
		}
	  else
	    {
		  arrCorreos[j] = cad_aux;	
		}
	  j++;
	  if (j>1)
	    {
		  j--;	
	  	  for(k=0;k<=j;k++)
  	        { 
			  if (filter.test(arrCorreos[k])) 
   		        {
				   exito = 1000; 
		        }
		      else
			    {
			       exito = k;
				   nom_resul = quita_espaciosform(arrCorreos[k]);
				   break;
			    }			
		    }		
		}
	  else	
		{ 			
			if (filter.test(quita_espaciosform(corr)))
			  {
				 exito = 1000;  
			  }
			else
			  {
			     exito = 0;
				 nom_resul = quita_espaciosform(corr);
			  }
		}		
	  return exito;	
   }
///// FUNCION VALIDA QUITA ESPACIOS DEL TEXTO
 function quita_espaciosform(texto)
	  {
	    var i;
		var texto_nuevo = "";
	    for(i=0;i<=texto.length;i++)
		  {
		   if (texto.substring(i, i+1) != " ")
		     {   
		       texto_nuevo = texto_nuevo+texto.substring(i, i+1);
			 }  
		  }	
		return texto_nuevo;	 
	  } 
///// FUNCION RFC	  
  function valida_regfedform(str)
   {
      for(var i = 0; i <= str.length; i++)
        if (pos_javaform(str.substring(i,i+1).toUpperCase(),"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&") < 0)
          return false;
      return true;
   }	  
///// FUNCION RFC	length  
  function valida_rfcform(str)
    {
	  var rf = quita_espaciosform(str);
	  
	  if(document.DatosFactyEnvio.CboPaisFact.value==484)
	  {
		   if (rf.length<12) 
	        {
				alert("El número de caracteres para el RFC debe ser mayor de 12, verifica por favor.");
			    return false;
			 }		  	 
	  }
	  return true;	
	}

   
///// FUNCION SUB RFC	  
  function pos_javaform(c, str)
  {
   for(var i = 0; i <= str.length; i++)
    if (c == str.substring(i,i+1))
     return i;
   return -1;
  }
///// FUNCION VALIDA CP        
 function Cp_incorrectaform ()
    { 
      var s= document.DatosFactyEnvio.TextCP.value;
      var filter=/^[0-9_]+[0-9_]$/;
      if (s.length == 0 ) 
          return true; 

      if (filter.test(s))
          return true;
      else
          <!--alert("Codigo Postal incorrecto" );		  -->
          return false;
   }   
///// FUNCION VALIDA CP        
 function Cp_incorrectaform2 ()
    { 
      var s= document.DatosFactyEnvio.EditCP.value;
      var filter=/^[0-9_]+[0-9_]$/;
      if (s.length == 0 ) 
          return true; 
      if (filter.test(s))
          return true;
      else
        <!--  alert("Codigo Postal incorrecto" );-->
          return false;
   }   
///// FUNCION VALIDA CP  SU LONGITUD          
  function val_longcpform(cp, pais)
    {		
		if (pais == 484)
		   {
			  if (cp.length<5)
			    {
				  return false;
				}
		   }
		return true;   
	}

///// FUNCION VALIDA CP  SU LONGITUD FACTURACION  
  function val_longcpform2(cpFact, paisFact)
    {		
		if (paisFact == 484)
		   {
			  if (cpFact.length<5)
			    {
				  return false;
				}
		   }
		return true;   
	}
///// FUNCION VALIDA LADA
   function val_ladaform ()
    {
      var s = document.DatosFactyEnvio.Textlada.value;
      var filter=/[0-9_]/;
      if (s.length == 0 )
      {
        return true;
      }	
      if (isnumeric_javaform(s))
          return true;
      else
      {
          alert("Por favor ingresa una clave LADA válida" );
          document.DatosFactyEnvio.Textlada.focus();
          document.DatosFactyEnvio.Textlada.select();
          return false;
      }  
   }
///// FUNCION VALIDA LADA NUMERO   
  function isnumeric_javaform(str)
   {
    for(var i = 0; i <= str.length; i++)
     if (pos_javaform(str.substring(i,i+1),"0123456789") < 0)
      return false;
    return true;
   }
///// FUNCION VALIDA LADA 2
   function val_ladaform2 ()
    {
      var s = document.DatosFactyEnvio.FactTextlada.value;
      var filter=/[0-9_]/;
      if (s.length == 0 )
      {
        return true;
      }	
      if (isnumeric_javaform(s))
          return true;
      else
      {
          alert("Por favor ingresa una clave LADA válida" );
          document.DatosFactyEnvio.FactTextlada.focus();
          document.DatosFactyEnvio.FactTextlada.select();
          return false;
      }  
   }	
function ff_Pais() /*SI EL COMBO DE PAIS ESTA SELECCIONADO*/
{
vintPais = document.DatosFactyEnvio.CboPais[document.DatosFactyEnvio.CboPais.selectedIndex].value;
 if (vintPais == 484)
 	{
   	document.DatosFactyEnvio.CboEstado.options[9].selected = true;
	 }
	 else
	 {
   	document.DatosFactyEnvio.CboEstado.options[33].selected = true;		 
  	 }
}
function ff_PaisFact() /*SI EL COMBO DE PAIS ESTA SELECCIONADO*/
{
vintPais = document.DatosFactyEnvio.CboPaisFact[document.DatosFactyEnvio.CboPaisFact.selectedIndex].value;
 if (vintPais == 484)
 	{
   	document.DatosFactyEnvio.CboEstadoFact.options[9].selected = true;
	 }
	 else
	 {
   	document.DatosFactyEnvio.CboEstadoFact.options[33].selected = true;		 
  	 }
}

function validalong(nombrearea)
{
	miarea=document.getElementById(nombrearea);
	miarea.value;
	if (miarea.value.length >500)
	{
		miarea.value=miarea.value.substring(0,500);
	}
}

