// JavaScript Document
// Su explorador no soporta java o lo tiene deshabilitado; esta pagina necesita javascript para funcionar correctamente 

function validacion(formulario) {
	
	var alguna=false
	
	//Se valida el paso 1 del formulario (1: Seleccionar partner)
	var x=document.getElementsByName("a[]")
	for (var i = 0; i < x.length; i++){
		if (x[i].checked == true){
		   alguna = true
		}
	}
	if(alguna == false  && formulario.a1.value == "" ){
	   alert('En el paso 1, debe seleccionar como mínimo un SOCIO, o sugerir alguno')
	   return false
	}
	
	//Se valida el paso 2 del formulario (2: Datos de contacto)
	return contacto(formulario);	
			
}
function contacto(formulario){
	//Se valida el paso 2 del formulario (2: Datos de contacto) y/o Formulario Contacto
	
	//EMPRESA
	if (formulario.d1.value.length < 2) {
		alert('En el paso 2, el nombre de su EMPRESA debe tener como mínimo dos letras')
		return false
	}
	//PERSONA DE CONTACTO
	if (validname(formulario.d5) == false) {
		return false
	}
	//EMAIL
	if (emailCheck(formulario.d6.value)== false) {
		return false
	}	
	//ERP
	if (formulario.d3[0].selected == true) {
		alert('En el paso 2, debe seleccionar un ERP')
		return false
	}
	
	return true	
	
}
// Función para validar un nombre
function validname(nombre){
	
	var valido = 1;

	if (nombre.value.length < 3) {
		alert("En el paso 2, la PERSONA DE CONTACTO debe tener como mínimo tres letras");  
		return false
	}
	
	var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú ";
	var checkStr = nombre.value;
	   
	for (i = 0; i < checkStr.length; i++){
		ch = checkStr.charAt(i); 
		for (j = 0; j < checkOK.length; j++){
		  	if (ch == checkOK.charAt(j)){
				  valido=1;
				  j=checkOK.length;
			}
			else
			      valido=0;
		}
	}
	
	if(valido == 0){
		alert("En el paso 2, la PERSONA DE CONTACTO tiene caracteres inválidos");
		return false
	}
	else
		return true
}

//Función compleja para validar el email
function emailCheck (emailStr) {

	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	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("Su direccion de correo electrónico es incorrecta(revise @ y .'KJ)");
		return false;
	}
	
	var user=matchArray[1];
	var domain=matchArray[2];
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("El nombre de usuario del EMAIL posee caracteres inválidos.");
			return false;
	   }
	}
	
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("El nombre de dominio del EMAIL tiene caracteres inválidos.");
			return false;
		}
	}
	
	
	if (user.match(userPat)==null) {	
		alert("El nombre de usuario del EMAIL no parece ser válido.");
		return false;
	}
	
	var IPArray=domain.match(ipDomainPat);
	
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("EL número de direccion IP del EMAIL es inválida!");
				return false;
			}
		}
		return true;
	}
	
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("El nombre de dominio del EMAIL no parece ser válido.");
			return false;
		}
	}
	
	
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("La dirección del EMAIL debe terminar en un dominio conocido o uno de dos letras de algún país.");
		return false;
	}
	
	
	if (len<2) {
		alert("La direccion del EMAIL necesita poseer el nombre del servidor!");
		return false;
	}
	
	return true;
}