//Form_Comprobar
//Change_Formato
//Change_Background_Color
//Change_Background_Imagen
//Change_Imagen
//Change_Clase
//Change_Style
//Change_Atributo
//Change_Value
//Cambiar_Portada
//Precarga_Imagen

//dni
//NumAccept
var patr_Car = /[A-Za-z0-9ÑñáéíóúÁÉÍÚÓüÜ\+ª‘’\\/?!¡¿~\*]/;
var patr_Ema = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
var patr_Dni = "TRWAGMYFPDXBNJZSQVHLCKET";
function Form_Comprobar(a,b,c,d)
{	//a	=	Formulario
	//b	= 	1 o 0 | error si esta vacio | error de sistema
	//c	=	Frase Inicial
	//d	=	
	
	frm = document.forms[a];
	
	vac = b.split("|");
	link = "";	
	cam = d.split("@@@");
	var ok = true;
	var na = false; //Quiere decir que todos estan llenos.
	var error = "";
	var erros = "";
		
	if (cam != "")
	{	for (valor in cam)
		{	val = cam[valor].split("|");
							
			res = val[0];	//Resto de Capo
			tip = val[1];	//tipo de validacion		
			sig = val[2];	//signo en la validacion
			pat = val[3];	//Patron comparativo en la validacion
			err = val[4];	//Error si no se valida
			obl = val[5];	//Obligatorio o no.
			enl = val[6];	//Incluri en enlace
			
			
			switch (tip)
			{	
				case "fun":	
							if (obl == 1)
							{ 	valor = eval('frm["'+c+res+'"].value');
								
								var err_m = eval(pat+'("'+valor+'","'+err+'");'); 
								if (err_m != 0)
								{	var error = error + err_m;
									var ok = false;
								}
													
							}
							break;	
				case "pat":	
				            
							if((eval(pat+'.test(frm["'+c+res+'"].value)') == false) && (obl == 1))
							{   var ok = false;
								
								var error = error + err+"\n";
							}							
							break;
				case "val":										
							if (frm[c+res] != undefined) 
							{	
								if(eval('frm["'+c+res+'"].value.length'+sig+pat))
								{   
									
									if (obl == 1)
									{	var ok = false;
																						
										if (vac[0] == 1)
										{	if (eval('frm["'+c+res+'"].value.length != 0'))
											{	if (err != 0)
												{	var error = error + err+"\n";										
												}								
											}
											else
											{	na = true; // no estan todos llenos
											}																				
										}
										else
										{	ok = false;																																
											if (err != 0)
											{	var error = error + err+"\n";										
											}
										}
									}																								
								}																												
							}
							else
							{	var erros = vac[2];							
							}
			}
		
			if (enl != "1")
			{	
				if (tip == "chk")
				{	if (frm[c+res] != undefined)
					{	if (frm[c+res].checked == true) {value = 1;} else {value = 0;}
						link +='&'+eval('frm["'+c+res+'"].id')+"="+value;
					}	
				}
				else
				{	link +='&'+eval('frm["'+c+res+'"].id')+"="+eval('frm["'+c+res+'"].value');
				}
			}
			
				
		}	
		

	}	
	
	//sistema
	if (erros == "")
	{	if (ok == false)
		{	
			if ((na == true) && (vac[0] == 1))
			{	var ok = false
				var error = vac[1]
											 
			}	
			else
			{	
				if (error == "")
				{	var ok = true;
				}	
			}		
		}				
	}
	else
	{	var ok = false
		var error = erros;
	}
	if (ok == false)
	{	alert(error);
		link = false;
	}

	return link;
}
function esp(a,b,c)
{   
	espacios = a.split(" ");
	err = 0;
	for (valor in espacios)
	{	if (espacios[valor].length > 15)
		{	err = b+"\n";
		}
	}
	return err;
}

function dni(a,b)
{	//a	= DNI	
	//b = error
	//c = obligacion	
	if (a.length == 9)
	{	pos = a.substr(0,8) % 23		
		let = patr_Dni.substring(pos,pos+1)		  
		if (let != a.substr(8,9))
		{	err = b+"\n";
		}
		else
		{	err = 0
		}
	}
	else
	{	err = b+"\n";
	}
	return err;
	
}
function Change_Formato(a,b,c,d,e)
{	//a = Cambiar Color Background
	//b = Cambiar Imagen Background
	//c = Cambiar Imagen
	//d = Cambiar Clase 
	//e = Cambiar Bordes
	if (a != "n")	{	Change_Background_Color(a);	}
	if (b != "n")	{	Change_Background_Imagen(b);}
	if (c != "n")	{	Change_Imagen(c);			}
	if (d != "n")	{	Change_Clase(d);			}
	if (e != "n")	{	Change_Style(e);			}	
}
function Change_Background_Color(z)
{	cambios = z.split("@@@");
	for (valor in cambios)
	{	id = cambios[valor].substr(0,cambios[valor].lastIndexOf("|"));
		color = cambios[valor].substr(cambios[valor].lastIndexOf("|")+1,cambios[valor].length);				
		ele = document.getElementById(id)		
		ele.style['background'] = color		
	}		
}
function Change_Background_Imagen(z)
{	cambios = z.split("@@@");
	for (valor in cambios)
	{	id = cambios[valor].substr(0,cambios[valor].lastIndexOf("|"));
		imagen_change = cambios[valor].substr(cambios[valor].lastIndexOf("|")+1,cambios[valor].length);
		ele = document.getElementById(id)		
		ele.style['background'] = "url("+imagen_change+")";
	}		
}
function Change_Imagen(z)
{	cambios = z.split("@@@");	
	for (valor in cambios)
	{	id = cambios[valor].substr(0,cambios[valor].lastIndexOf("|"));		
		imagen_change = cambios[valor].substr(cambios[valor].lastIndexOf("|")+1,cambios[valor].length);				
		ele = document.getElementById(id)				
		ele['src'] = imagen_change
	}		
}

function Change_Clase(z)
{	cambios = z.split("@@@");
	for (valor in cambios)
	{	id = cambios[valor].substr(0,cambios[valor].lastIndexOf("|"));
		clase = cambios[valor].substr(cambios[valor].lastIndexOf("|")+1,cambios[valor].length);
		ele = document.getElementById(id)
		ele.className=clase;
	}		
}
function Change_Style(z)
{	cambios = z.split("@@@");
	for (valor in cambios)
	{	valores = cambios[valor].split("|");
		id = valores[0];		
		estilo = valores[1];		
		valor = valores[2];						
		ele = document.getElementById(id)
		eval('ele.style["'+estilo+'"]= valor')
	}		
}
function Change_Atributo(a,b,c)
{	//a		= ELEMENTO
	//b		= Atributo
	//c		= Valor	
	a.style[b] = c;
}
function Change_Value(a,b,c)
{	//a		= ELEMENTO
	//b		= Atributo
	//c		= Valor	
	a[b] = c;
}
function Change_Value_Id(a,b,c)
{	//a		= ELEMENTO
	//b		= Atributo
	//c		= Valor
	
	id = document.getElementById(a);

	
	eval('id[b] = c');
}
function Change_Valor(a,b,c)
{	//a		= ELEMENTO
	//b		= Atributo
	//c		= Valor	
	id = document.getElementById(a);	
	id[b] = c;
	
}
function Cambiar_Portada(a,b,c)
{	//a	=	Numero de Alertas existentes
	//b	=	Numero de Alerta Pulsada
	//c	=	Numero de Alerta Pulsada
	//d	=	Numero de Alerta
	
	if (document.getElementById('imagen').style['display'] == 'none')
	{	document.getElementById('imagen').style['display'] = 'inline';
	}
		
	for (contador_alertas = 1 ; contador_alertas <= a ; contador_alertas++)
	{	if (contador_alertas == b)
		{	document.getElementById("alerta"+contador_alertas).style["opacity"] = "1";			
			document.getElementById("alerta"+contador_alertas).style["filter"] = "alpha(opacity=100)";			
			var_datos = document.getElementById("Datos_Alerta"+c).value;
			
			array_datos = var_datos.split("&&&");
			//alert(document.getElementById("Datos_Alerta"+c).value);
			imagen_a_mostrar = eval("imagen"+b).src;
			
			document.getElementById("imagen").style['background'] = "url("+imagen_a_mostrar+")";										
			document.getElementById("mensaje").innerHTML = 	array_datos[2];						
			document.getElementById("titular").innerHTML = "<strong>"+array_datos[3]+"</strong>";
			document.getElementById("id_noticya").value = 	array_datos[4];						
			document.getElementById("ajx_value").value = array_datos[5];		
			document.getElementById("act_value").value = array_datos[6];
		}
		else
		{	document.getElementById("alerta"+contador_alertas).style["opacity"] = "0.4";
			document.getElementById("alerta"+contador_alertas).style["filter"] = "alpha(opacity=40)";
		}
	}		
}
function PreCarga_Imagen (a, b) 
{	if (document.images)
	{	eval (a +' = new Image()')	   				
		eval (a +'.src = "' + b + '"')			
	}
}

var nav4 = window.Event ? true : false;
//onKeyPress="return numAccept(event)"
function numAccept(evt)
{	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57

	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57));
}
var nav4 = window.Event ? true : false;
function carAccept(evt)
{	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57		
	var key = nav4 ? evt.which : evt.keyCode;	
	return ( key <= 13 || key == 32 || (key >= 48 && key <= 57)||(key >=65  && key <= 90)||(key >=97  && key <= 122) || key == 193 || key == 201  || key == 205 || key == 211  || key == 218  || key ==225  || key ==233  || key ==237  || key ==243  || key ==250  || key ==241 );
}
function retAccept(evt)	//Acceptacion de Retorno de Carros
{	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57		
	var key = nav4 ? evt.which : evt.keyCode;	
	return ( key <= 13 || key == 32 || key == 43 || key == 44 || key == 46 || key == 58 || key == 59 || key == 61 || key == 33 || key == 161 || key == 191 || key == 63 || (key >= 48 && key <= 57)||(key >=65  && key <= 90)||(key >=97  && key <= 122) || key == 193 || key == 201  || key == 205 || key == 211  || key == 218  || key ==225  || key ==233  || key ==237  || key ==243  || key ==250  || key ==241 );
}		
function solAccept(evt)	//Acceptacion de Retorno de Carros
{	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57		
	var key = nav4 ? evt.which : evt.keyCode;	
	return ( key <= 12 || key == 32 || key == 43 || key == 44 || key == 46 || key == 58 || key == 59 || key == 61 || key == 33 || key == 161 || key == 191 || key == 63 || (key >= 48 && key <= 57)||(key >=65  && key <= 90)||(key >=97  && key <= 122) || key == 193 || key == 201  || key == 205 || key == 211  || key == 218  || key ==225  || key ==233  || key ==237  || key ==243  || key ==250  || key ==241 );
}
function Obtener_Altura(evt,m,e)
{	
	if (navegador == 1)
	{	return eval('evt.'+m);
		
	}
	else
	{	
		return eval('evt.'+e);	
	}	
}
