﻿// JavaScript Document
function validartexto(checkStr, minimo)
{
	var allValid = true;
	var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú " + "0123456789" + " ºª\\!|@·#$%&/()=?¿¡*+Ç-_.:,;<>´ "+ String.fromCharCode(13) + "\n";
	
	if (checkStr.length < minimo) 
	{
		return 1;
	}
	 
	if(allValid)
	{
		var arroba = true;
		for (i = 0; i < checkStr.length; i++) {
			ch = checkStr.charAt(i);
			if (ch == "@") arroba = false;
			for (j = 0; j < checkOK.length; j++)
				if (ch == checkOK.charAt(j))
					break;
			if (j == checkOK.length) { 
				allValid = false; 
				break; 
			}
		}
		
		if (!allValid) 
		{ 
			return 2;
		} 
		else
		{
			return 0;
		}
	}
}

function validaremail(checkStr, obligatorio)
{
	var allValid = true;
	var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ" + "abcdefghijklmnñopqrstuvwxyz " + "0123456789" + "@-_.";
	
	if (checkStr.length < 1 && obligatorio) 
	{
		return 1;
	}
	
	if(allValid)
	{
		var arroba = 0;
		var punto = false;
		for (i = 0; i < checkStr.length; i++) 
		{
			ch = checkStr.charAt(i);
			if (ch == "@" && i > 0) arroba++;
			if (ch == "." && arroba == 1) punto = true;
			for (j = 0; j < checkOK.length; j++)
				if (ch == checkOK.charAt(j))
					break;
			if (j == checkOK.length) 
			{ 
				allValid = false; 
				break; 
			}
		}
		
		if(!allValid)
		{
			return 2;
		}
		else
		{
			if (arroba != 1 || !punto) 
			{ 
				return 3;
			} 
			else
			{
				return 0;
			}
		}
	}
}

function validartelefono(checkStr, obligatorio)
{
	var allValid = true;
	var checkOK = "0123456789" + " \\|#/()*+-_.:,;"+ String.fromCharCode(13);
	
	if (checkStr.length < 1 && obligatorio)
	{
		return 1;
	}
	 
	if(allValid)
	{
		var arroba = true;
		for (i = 0; i < checkStr.length; i++) {
			ch = checkStr.charAt(i);
			if (ch == "@") arroba = false;
			for (j = 0; j < checkOK.length; j++)
				if (ch == checkOK.charAt(j))
					break;
			if (j == checkOK.length) { 
				allValid = false; 
				break; 
			}
		}
		
		if (!allValid) 
		{ 
			return 2;
		} 
		else
		{
			return 0;
		}
	}
}

function validar(formulario)
{
	if(formulario.checkbox.checked)
	{
		//document.getElementById("checkbox").style.border = "2px solid #009999";
		
		var salida = true;
		var error ="";
		var foco = 20;
		var marcovalidados = new Array(20);
		var d=0;
		var marcoerrores = new Array(20);
		var c=0;
		
		var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú " + "0123456789" + " ºª\\!|@·#$%&/()=?¿¡*+Ç-_.:,;<>´ "+ String.fromCharCode(13) + "\n";
		
		// VALIDAR EMPRESA
		r = validartexto(formulario.c_empresa.value, 1);
		if(!r)
		{
			marcovalidados[d] = "c_empresa";
			d++;
		}
		else
		{
			if(r == 1)
				error="\""+errores[0][0]+"\", "+errores[1][0]+".\n";
			else
				error="\""+errores[0][0]+"\" "+errores[1][1]+".\n";
			
			marcoerrores[c] = "c_empresa";
			c++;
			
			foco = 1;
		}
		
		// VALIDAR CONTACTO
		r = validartexto(formulario.c_contacto.value, 1);
		if(!r)
		{
			marcovalidados[d] = "c_contacto";
			d++;
		}
		else
		{
			if(r == 1)
				error+="\""+errores[0][1]+"\", "+errores[1][0]+".\n";
			else
				error+="\""+errores[0][1]+"\" "+errores[1][1]+".\n";
			
			marcoerrores[c] = "c_contacto";
			c++;
			
			if(foco > 2) 
				foco = 2;
		}

		// VALIDAR EMAIL
		r = validaremail(formulario.c_email.value, true);
		if(!r)
		{
			marcovalidados[d] = "c_email";
			d++;
		}
		else
		{
			if(r == 1)
				error+="\""+errores[0][2]+"\", "+errores[1][0]+".\n";
			else
			{
				if(r == 2)
					error+= ""+errores[1][1]+".\n";
				else
					error+= ""+errores[1][2]+".\n";
			}
			
			marcoerrores[c] = "c_email";
			c++;
			
			if(foco > 3) 
				foco = 3;
		}
		
		// VALIDAR DESCRIPCION
		r = validartexto(formulario.c_descrip.value, 1);
		if(!r)
		{
			marcovalidados[d] = "c_descrip";
			d++;
		}
		else
		{
			if(r == 1)
				error+="\""+errores[0][5]+"\", "+errores[1][0]+".\n";
			else
				error+="\""+errores[0][5]+"\" "+errores[1][1]+".\n";
			
			marcoerrores[c] = "c_descrip";
			c++;
			
			if(foco > 6) 
				foco = 6;
		}
		
		// VALIDAR CANTIDAD
		r = validartexto(formulario.c_cantidad.value, 1);
		if(!r)
		{
			marcovalidados[d] = "c_cantidad";
			d++;
		}
		else
		{
			if(r == 1)
				error+="\""+errores[0][6]+"\", "+errores[1][0]+".\n";
			else
				error+="\""+errores[0][6]+"\" "+errores[1][1]+".\n";
			
			marcoerrores[c] = "c_cantidad";
			c++;
			
			if(foco > 7) 
				foco = 7;
		}

		// VALIDAR ENVIOS ANUALES
		if(formulario.c_nenvios.value.length > 0)
		{
			r = validartexto(formulario.c_nenvios.value, 1);
			if(!r)
			{
				marcovalidados[d] = "c_nenvios";
				d++;
			}
			else
			{
				if(r == 1)
					error+="\""+errores[0][7]+"\", "+errores[1][0]+".\n";
				else
					error+="\""+errores[0][7]+"\" "+errores[1][1]+".\n";
				
				marcoerrores[c] = "c_nenvios";
				c++;
				
				if(foco > 8) 
					foco = 8;
			}
		}
		else
			document.getElementById("c_nenvios").style.border = "2px solid #009999";

		// VALIDAR DETALLES
		if(formulario.c_detalles.value.length > 0)
		{
			r = validartexto(formulario.c_detalles.value, 1);
			if(!r)
			{
				marcovalidados[d] = "c_detalles";
				d++;
			}
			else
			{
				if(r == 1)
					error+="\""+errores[0][8]+"\", "+errores[1][0]+".\n";
				else
					error+="\""+errores[0][8]+"\" "+errores[1][1]+".\n";
				
				marcoerrores[c] = "c_detalles";
				c++;
				
				if(foco > 9) 
					foco = 9;
			}
		}
		else
			document.getElementById("c_detalles").style.border = "2px solid #009999";

		// VALIDAR TELEFONO
		r = validartelefono(formulario.c_tlf.value, true);
		if(!r)
		{
			marcovalidados[d] = "c_tlf";
			d++;
		}
		else
		{
			if(r == 1)
				error+="\""+errores[0][3]+"\", "+errores[1][0]+".\n";
			else
				error+="\""+errores[0][3]+"\" "+errores[1][3]+".\n";
			
			marcoerrores[c] = "c_tlf";
			c++;
			
			if(foco > 4) 
				foco = 4;
		}
		
		// VALIDAR FAX
		if(formulario.c_fax.value.length > 0)
		{
			r = validartelefono(formulario.c_fax.value, false);
			if(!r)
			{
				marcovalidados[d] = "c_fax";
				d++;
			}
			else
			{
				if(r == 2)
					error+="\""+errores[0][3]+"\" "+errores[1][3]+".\n";
				
				marcoerrores[c] = "c_fax";
				c++;
				
				if(foco > 4) 
					foco = 4;
			}
		}
		else
			document.getElementById("c_fax").style.border = "2px solid #009999";
		
		// VALIDAR FECHA LIMITE
		if(formulario.c_fechalim.value.length > 0)
		{
			r = validartexto(formulario.c_fechalim.value, 1);
			if(!r)
			{
				marcovalidados[d] = "c_fechalim";
				d++;
			}
			else
			{
				if(r == 1)
					error+="\""+errores[0][9]+"\", "+errores[1][0]+".\n";
				else
					error+="\""+errores[0][9]+"\" "+errores[1][1]+".\n";
				
				marcoerrores[c] = "c_fechalim";
				c++;
				
				if(foco > 10) 
					foco = 10;
			}
		}
		else
			document.getElementById("c_fechalim").style.border = "2px solid #009999";
		
		// VALIDAR ENTREGA
		if(formulario.c_entrega2.value.length > 0)
		{
			r = validartexto(formulario.c_entrega2.value, 1);
			if(!r)
			{
				marcovalidados[d] = "c_entrega2";
				d++;
			}
			else
			{
				if(r == 1)
					error+="\""+errores[0][11]+"\", "+errores[1][0]+".\n";
				else
					error+="\""+errores[0][11]+"\" "+errores[1][1]+".\n";
				
				marcoerrores[c] = "c_entrega2";
				c++;
				
				if(foco > 12) 
					foco = 12;
			}
		}
		else
			document.getElementById("c_entrega2").style.border = "2px solid #009999";
		
		// VALIDAR PLAZO DE ENTREGA
		if(formulario.c_plazo.value.length > 0)
		{
			r = validartexto(formulario.c_plazo.value, 1);
			if(!r)
			{
				marcovalidados[d] = "c_plazo";
				d++;
			}
			else
			{
				if(r == 1)
					error+="\""+errores[0][12]+"\", "+errores[1][0]+".\n";
				else
					error+="\""+errores[0][12]+"\" "+errores[1][1]+".\n";
				
				marcoerrores[c] = "c_plazo";
				c++;
				
				if(foco > 13) 
					foco = 13;
			}
		}
		else
			document.getElementById("c_plazo").style.border = "2px solid #009999";
		
		// VALIDAR PACKING
		if(formulario.c_packing.value.length > 0)
		{
			r = validartexto(formulario.c_packing.value, 1);
			if(!r)
			{
				marcovalidados[d] = "c_packing";
				d++;
			}
			else
			{
				if(r == 1)
					error+="\""+errores[0][13]+"\", "+errores[1][0]+".\n";
				else
					error+="\""+errores[0][13]+"\" "+errores[1][1]+".\n";
				
				marcoerrores[c] = "c_packing";
				c++;
				
				if(foco > 14) 
					foco = 14;
			}
		}
		else
		{
			document.getElementById("c_packing").style.border = "2px solid #009999";
		}

		
		if(c>0)
		{
			salida = false;
			alert(error);
			//alert(foco);
			
			if(foco < 6)
			{
				Desplegar2(miAcordeon, "TSAccordionContent262480", "titlesectionForm", 1);
				MM_showHideLayers('btSeguir','','show');
				//document.getElementById("capa1").style.visibility = "visible";
				document.getElementsByClassName("titlesectionForm")[0].style.visibility = "visible";
			}

			for(i=0;i<20;i++)
			{
				if(marcoerrores[i]!= null)
					document.getElementById(marcoerrores[i]).style.border = "2px solid #B04F58";
				if(marcovalidados[i] != null)
					document.getElementById(marcovalidados[i]).style.border = "2px solid #009999";
			}
			/*
			switch(foco)
			{
				case 1:
					formulario.c_empresa.focus();
					break;
				
				case 2:
					formulario.c_contacto.focus();
					break;
				
				case 3:
					formulario.c_email.focus();
					break;
	
				case 4:
					formulario.c_tlf.focus();
					break;			
				
				case 5:
					formulario.c_fax.focus();
					break;
					
				case 6:
					formulario.c_descrip.focus();
					break;
	
				case 7:
					formulario.c_cantidad.focus();
					break;

				case 8:
					formulario.c_nenvios.focus();
					break;
					
				case 9:
					formulario.c_detalles.focus();
					break;
				
				case 10:
					formulario.c_fechalim.focus();
					break;
				
				case 11:
					formulario.c_entrega.focus();
					break;
			
				case 12:
					formulario.c_entrega2.focus();
					break;
				
				case 13:
					formulario.c_plazo.focus();
					break;
			}
			*/
		}
		
		return (salida); 
	}
	else
	{
		//document.getElementById("checkbox").style.border = "2px solid #B04F58";
		alert(errores[1][4]);
		return(false);
  	}
}