﻿function str2Array(lista,separador)

{

	var campo = "";

	var MyArray = new Array()

	  

	for (var c=0;c<lista.length;c++){

		if(lista.substr(c,1) == separador){ MyArray.push(campo);campo="";}

	  else campo = campo + lista.substr(c,1);

	}

	MyArray.push(campo);

	

	

	return MyArray;

}



function Validacion(form,lista)

{	

		var MyArray = new Array();

		MyArray=str2Array(lista,'|')

		

		CamposKo = "";

		strRazon = "";

		Cont = 0;

		

		for (c=0;c<MyArray.length;c++)

		{

			var MyCampo = new Array();

			MyCampo=str2Array(MyArray[c],';')

			var strTipo = MyCampo[0].substr(0,3);

			var el = null;

			var strTituloCampo = "";

		

			//expresion regular emails

			var reg_exp=/^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$/

		

			// instancio el objeto actual

			el = form[MyCampo[0]];  //		//el = document.getElementById(MyCampo[0]);

			//alert(MyCampo[0] + "-" + el);

		

			// defino el ttulo por si el valor no corresponde

			if(MyCampo.length == 1)

				strTituloCampo = MyCampo[0].substr(3,(MyCampo[0].length - 3));

			else

				strTituloCampo = MyCampo[1];

				

			strTituloCampo = strTituloCampo.replace("_"," ");

			

			// empiezo a comprovar campos

			if(strTipo == "txt" || strTipo == "pwd" )    // texto obligatorio

			{

				if( (el.value.length<1))

				{

					CamposKo += strTituloCampo + strRazon + "\n";

				}

			}

			else if(strTipo == "int")  	     // es un nmero

			{

				if (isNaN(el.value) || el.value.length == 0)    //el mtodo "isNaN" comprueba si el valor No es un nmero

				{

					CamposKo += strTituloCampo + Traducciones(intIdIdioma, 6) + strRazon + "\n";

				}

			}

			else if(strTipo == "eml")       // es un email

			{

				if(!reg_exp.test(el.value))

				{

					CamposKo += strTituloCampo + Traducciones(intIdIdioma, 5) + strRazon + "\n";

				}

			}

			else if(strTipo == "rdo")       // es un Radio

			{

				var blnObjSelected = false;

				

				for(var i=0;i<el.length;i++)

				{

					if(el[i].checked == true) blnObjSelected = true;

				}

				

				if(!blnObjSelected) CamposKo += strTituloCampo + Traducciones(intIdIdioma, 3) + strRazon + "\n";

			}

			else if(strTipo == "cbo")       // es un ComboBox

			{

				if(el.selectedIndex < 1 ) CamposKo += strTituloCampo + Traducciones(intIdIdioma, 4) + strRazon + "\n";

			}

			else if(strTipo == "chk")       // es un Checkbox

			{

				if(el.checked) CamposKo += strTituloCampo + strRazon + "\n";

			}

			

			strRazon = "";

		}

			

		if(CamposKo != "")

		{

			var msg = Traducciones(intIdIdioma, 1) + CamposKo + Traducciones(intIdIdioma, 2);

			alert(msg);

			return false;

		}

		else

		{

			//var el = document.getElementById(document.Form[0].name);

			//el.submit();

			return true;

		} 

}



function ExisteCadena(strCadena,strCaracter){

	var devuelve = false;

	for (c=0;c<strCadena.length;c++){

		if(strCadena.substr(c,1) == strCaracter){

		devuelve = true;

		 c = strCadena.length;

	  }     

	}

	return devuelve

}



function Traducciones(IdIdioma, intFrase)

{

	var traducciones = new Array(3);

	traducciones[0] = new Array(6);

	traducciones[1] = new Array(6);
	
	traducciones[2] = new Array(6);



	traducciones[0][0] = "ATENCIÓN!\nLos siguientes campos son obligatorios: \n\n";

	traducciones[0][1] = "\n\nRectifique por favor.";

	traducciones[0][2] = " (tiene que seleccionar una opción";

	traducciones[0][3] = " (tiene que seleccionar una opción";

	traducciones[0][4] = " (no válido)";

	traducciones[0][5] = " (número no válido)";

	

	traducciones[1][0] = "WARNING!\nThe following fields are required: \n\n";

	traducciones[1][1] = "\n\nPlease correct it.";

	traducciones[1][2] = " (you must select an option";

	traducciones[1][3] = " (you must select an option";

	traducciones[1][4] = " (invalid)";

	traducciones[1][5] = " (number no valid)";
	

	traducciones[2][0] = "ATENÇÂO!\nOs seguintes campos são  obrigatórios: \n\n";

	traducciones[2][1] = "\n\nRetifique por favor.";

	traducciones[2][2] = " (por favor escolha uma opçâo";

	traducciones[2][3] = " (por favor escolha uma opçâo";

	traducciones[2][4] = " (não válido)";

	traducciones[2][5] = " (não válido)";

	

	return traducciones[IdIdioma - 1][intFrase - 1];



}

	