// Funciones en Cliente

//le enviamos por parámetros el VALUE del campo de texto que contiene la hora a validar
function ValidaHora(campo)
{
        var ValHora = /^(0|00|1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23)\:([0-5]0|[0-5][1-9])$/
        if ( !(ValHora.test( campo )) ) 
        { 
                alert("la Hora introducida no es válida.");
                return false;
        }else{return true;}
}

function validar() {
	for (i=0; i<validar.arguments[0].length; i++) { 
		ok = ValidaCampo(validar.arguments[0][i],validar.arguments[1][i],validar.arguments[2][i],validar.arguments[3][i],validar.arguments[4])
		if (!ok) break 
		}
	return(ok);
	}

function ValidaCampo() { // Nombre de campo, Tipo 0=>txt 1=>num 2=>fecha 3=>jpg/gif 4=>e-mail 5=>Combo, Obligatorio? (boolean)
	var error = "";
	frm = eval(document.forms[ValidaCampo.arguments[4]]) 
    var formuId = ValidaCampo.arguments[4];
    if (document.all) frm=document.all[formuId];
    else if (document.getElementById) frm = document.getElementById(formuId);
    else if (document.layers) frm = document.forms[formuId];
	var valor = frm[ValidaCampo.arguments[0]].value + "";
	valor = valor.replace(/(\s+)/,"") // Eliminamos blancos al inicio de la cadena		
	if (!ValidaCampo.arguments[2] && valor.length == 0) // Comprobamos nulos
		error = "El campo " + ValidaCampo.arguments[3] + " es obligatorio" ;
	else if (ValidaCampo.arguments[1] == 0 && valor.length > 0) {
		var filtro = /(')/;
		if (filtro.test(valor))
			error = "El campo " + ValidaCampo.arguments[3] + " no admite el caracter (') .";
		}
	else if (ValidaCampo.arguments[1] == 1 && valor.length != 0 && valor + "" != parseFloat(valor) + "") // Comprobamos numerico
		error = "El campo " + ValidaCampo.arguments[3] + " debe ser un numero"; 	
	else if (ValidaCampo.arguments[1] == 2 && isNaN(Date.parse(valor))) // Comprobamos fecha
		error = "El campo " + ValidaCampo.arguments[3] + " contiene una fecha incorrecta";
	else if (ValidaCampo.arguments[1] == 3 && valor.length > 0)
		{ 	re1 = /(GIF)$/
			re2 = /(JPG)$/	
			valor = valor.toUpperCase();
			if (!re1.test(valor) && !re2.test(valor))
				error = "El archivo " + ValidaCampo.arguments[3] + " debe tener extension GIF ó JPG";
		}
	else if (ValidaCampo.arguments[1] == 4 && valor.length > 0)
		{
			var filtro = /^[a-z||0-9||_||-]+[.][a-z||0-9||_||-]+@[a-z||0-9||_||-]+[.][a-z]{2,3}$/;
			var filtro1 = /^[a-z||0-9||_||-]+@[a-z||0-9||_||-]+[.][a-z]{2,3}$/;                                                      
			var filtro2 = /^[a-z||0-9||_||-]+@[a-z||0-9||_||-]+[.][a-z||0-9||_||-]+[.][a-z]{2,3}$/;                                                                                        
			var filtro3 = /^[a-z||0-9||_||-]+[.][a-z||0-9||_||-]+@[a-z||0-9||_||-]+[.][a-z||0-9||_||-]+[.][a-z]{2,3}$/;
			if (!filtro.test(valor) && !filtro1.test(valor) && !filtro2.test(valor) && !filtro3.test(valor))
				error = "El campo " + ValidaCampo.arguments[3] + " no es correcto";
		}
	else if (ValidaCampo.arguments[1] == 5 && (frm[ValidaCampo.arguments[0]].selectedIndex < 0 || frm[ValidaCampo.arguments[0]].options[frm[ValidaCampo.arguments[0]].selectedIndex].value < 0))
		error = "El campo " + ValidaCampo.arguments[3] + " debe tener una seleccion";
	if (error == "")
		return(true);
	else {
		alert(error);
		frm[ValidaCampo.arguments[0]].focus();	
		return(false);
		}
	}
	

//Funcíón que elimina el contenido de textareas y cajas de texto
function clearForm(formulario){
  var formu;
  if (document.all) formu = eval("document.all."+formulario);
  else if (document.layers) formu = document.forms[formulario];
  else if (document.getElementById) formu = document.getElementById(formulario);
  for (var i=0; i<formu.elements.length;i++){
    var tempRef = formu.elements[i];
    if (tempRef.type=="text" || tempRef.type=="textarea") tempRef.value="";
  }
}


//Las dos funciones siguientes reciben el nombre del textarea y número de caracteres admitidos	
//Ej de llamada: onKeyDown="javascript:Cuenta('formu.txtTextoDetalle',1000);"
function Cuenta(TAElement,Num)
{
//var Formu;
if(document.all) Formu = eval("document.all."+TAElement);
else if (document.layers) Formu = document.forms[TAElement];
else if (document.getElementById) Formu = document.getElementById(TAElement);
var ValRef=false;
var key = (document.layers)?e.which:event.keyCode;
if(Formu.value.length>Num&&(key!=8&&key!=46&&key!=38&&key!=37&&key!=39&&key!=40))
	{
	alert("Ha sobrepasado el número de caracteres válido ("+Num+") para este Campo.");
	contenido = Formu.value;
	setTimeout("restaurarContenido('"+contenido+"',Formu)",0);
	}
}//Formu.value=Formu.value.subString(0,Formu.value.length-1);

function restaurarContenido(contenido,Formu)
{
	Formu.value=contenido;
}


//Función para rellenar las combos del menú izquierdo y enviar el resultado a Catalogo.asp
function EnviaCatalogo(NC)
{
	if(NC==1)
	{Serie = document.formu.lstLampMenu.options[document.formu.lstLampMenu.selectedIndex].value;
	document.formu.action='catalogo.asp?IdF=1&IdS='+Serie+'&IdP=0';}
	else if(NC==2)
	{Serie = document.formu.lstPlafMenu.options[document.formu.lstPlafMenu.selectedIndex].value;
	document.formu.action='catalogo.asp?IdF=2&IdS='+Serie+'&IdP=0';}
	else if(NC==3)
	{Serie = document.formu.lstFocoMenu.options[document.formu.lstFocoMenu.selectedIndex].value;
	document.formu.action='catalogo.asp?IdF=3&IdS='+Serie+'&IdP=0';}
	document.formu.submit();
}
