// Funciones en Cliente

function sendForm(frm,fields,action) {
	if (fields.check(frm)) {
		document.getElementById(frm).action=action;
		document.getElementById(frm).submit();
	}
}

function fieldsChecks() {
	this.fields=Array();
	this.fieldErr='';
	
	this.add=function add(frm,field,type,nul,title,err) {
		// Cambio de indice por clave. this.fields[this.fields.length] = Array(frm,new fieldCheck(field,type,nul,title,err));
		this.fields[field] = Array(frm,new fieldCheck(field,type,nul,title,err,0));
	}
	this.addSign=function add(frm,field,type,nul,title,err,sign) {
		// Cambio de indice por clave. this.fields[this.fields.length] = Array(frm,new fieldCheck(field,type,nul,title,err));
		this.fields[field] = Array(frm,new fieldCheck(field,type,nul,title,err,sign));
	}
	this.del=function del(field) {delete this.fields[field];}

	this.check=function check(frm) { 
		var ok=true;
		// Cambio de indice por clave. for (var i=0;i<this.fields.length;i++) {
		for (var i in this.fields) {
			if (this.fields[i][0]==frm || frm==null) {
				ok=this.fields[i][1].check();				
				if (!ok) { this.fieldErr=i; return(false);}
			}
		} 
		return(true);
	}
}

function fieldCheck(field,type,nul,title,err,sign) {
	this.field=field; // Nombre de campo
	this.type=type; // Tipo 0=>txt 1=>num 2=>fecha 3=>jpg/gif 4=>e-mail 5=>Combo 6=> List,7=> check,8=>hora,9=>url,10=>fecha hora,11=>entero,12=>euro
	this.nul=nul; // true = admite nulo
	this.title=title; // Titulo del campo a mostrar en mesaje de error
	this.err=err; 
	this.sign=sign;

	this.parseNum=function(valor) {
		var re = /[.]{1}[0-9]+[,]{1}|[.]{1}[0-9]+[.]{1}/;  if (re.test(valor)) {valor=valor.replace(/[.]/g,''); }
		re = /[,]{1}[0-9]+[.]{1}|[,]{1}[0-9]+[,]{1}/;  if (re.test(valor)) {valor=valor.replace(/[,]/g,''); }
		valor = valor.replace(/[,]/g,'.');
		if (valor.search(/[.]+[1-9]*[0]*$/)>-1) {valor = valor.replace(/[0]*$/,'');}
		return valor;
	}
	
	this.checkSign=function(valor) {
		switch (this.sign) {
			case 1: if (parseFloat(valor)<0) {return this.error(' debe ser 0 o un numero positivo');} else {break;}
			case -1: if (parseFloat(valor)>0) {return this.error(' debe ser 0 o un numero negativo');} else {break;}
			case 0:
			default: 
		}
		return this.error('');
	}
	
	this.check=function check() {		
		if (!document.getElementById(this.field)) {return(true);} // Si no existe el campo no hay nada que validar
		
		var error='';
		var valor='';
		
		if (this.type == 6) {
			this.field+='[]';
				for (j=0; j < parseInt(document.getElementById(this.field).length); j++) {
					if(document.getElementById(this.field)[j].selected){valor+=document.getElementById(this.field)[j].value+',';}
				}
		} else {
			var valor = document.getElementById(this.field).value + '';
		}

		valor = valor.replace(/^\s+/,''); // Eliminamos blancos al inicio de la cadena
		if (!this.nul && valor.length==0) { // Comprobamos .nulos
			return this.error(' es obligatorio.'); 
		} else if (this.type==0 && valor.length>0) { //Comprobamos caracter '
			var filtro = /(')/;
			if (filtro.test(valor)) { return this.error(" no admite el caracter (')."); }
		} else if (this.type==1 && valor.length!=0) { // Comprobamos numerico
			valor=this.parseNum(valor);
			if (valor.search(/[.]*$/)>-1) {valor = valor.replace(/[.]*$/,'');}	
			if (valor.replace(/^0*/,'').replace(/[.]*$/,'')!=parseFloat(valor)+'' && '0'+valor.replace(/^0*/,'').replace(/[.]*$/,'')!=parseFloat(valor)+'')  {
				return this.error(' debe ser un numero valido.');
			} else {document.getElementById(this.field).value=valor;}
			
		} else if (this.type==11 && valor.length!=0) { // Comprobamos entero
			valor=this.parseNum(valor);
			if (valor.search(/[.]*$/)>-1) {valor = valor.replace(/[.]*$/,'');}
			if (valor.replace(/^0*/,'')!=parseInt(valor)) {return this.error(' debe ser un numero entero valido.');}
			else {document.getElementById(this.field).value=valor;}
			return this.checkSign(valor);
		} else if (this.type==12 && valor.length!=0) { // Comprobamos euro
			valor=this.parseNum(valor);
			if (valor.replace(/^0*/,'').replace(/[.]*$/,'')!=parseFloat(valor)+'' && '0'+valor.replace(/^0*/,'').replace(/[.]*$/,'')!=parseFloat(valor)+'')  {
				return this.error(' debe ser un numero con dos decimales valido.');
			} else {
				valor=(Math.round(valor*100)/100).toString();
				if (valor.search(/[.]{1}/)>-1) {valor = valor + '00';} else {valor = valor + '.00';}
				document.getElementById(this.field).value=valor.substr(0,valor.indexOf('.')+3);
			}
			return this.checkSign(valor);
		} else if (!valor.length==0 && this.type==2) { // Comprobamos fecha
			//var re = /[0-3][0-9](\/|-)[0-3][0-9](\/|-)[0-9]{4}/;
			valor= valor.split('-').join('/'); // Sustituimos '-' por '/'
			var re = /[0-3][0-9](\/)[0-3][0-9](\/)[0-9]{4}/;
			if (!re.test(valor) || isNaN(Date.parse(valor))) { return this.error(' contiene una fecha incorrecta [dd/mm/yyyy].'); }
		} else if (!valor.length==0 && this.type==8) { // Comprobamos hora
			var re1 = /^[0-1]{1}[0-9]{1}:[0-5]{1}[0-9]{1}(:[0-5]{1}[0-9]{1}){0,1}$/
			var re2 = /^[2]{1}[0-4]{1}:[0-5]{1}[0-9]{1}(:[0-5]{1}[0-9]{1}){0,1}$/
			if (!re1.test(valor) && !re2.test(valor)) { return this.error(' contiene una hora incorrecta [hh:mm:ss].'); }
		} else if (!valor.length==0 && this.type==10) { // Comprobamos fecha hora
			var re1 = /^[0-3][0-9](\/|-)[0-3][0-9](\/|-)[0-9]{4} [0-1]{1}[0-9]{1}:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/;
			var re2 = /^[0-3][0-9](\/|-)[0-3][0-9](\/|-)[0-9]{4} [2]{1}[0-4]{1}:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/;
			if (!re1.test(valor) && !re2.test(valor))  {return this.error(' contiene una fecha y hora incorrectas [dd/mm/yyyy hh:mm:ss].'); }
		} else if (this.type == 3 && valor.length > 0) {
			var re= /[.]*[GIF|JPG|PNG]{1,1}$/;
			valor = valor.toUpperCase();
			if (!re.test(valor)) {return this.error(' debe ser del tipo [gif,jpg,png]');}
		} else if (this.type==4 && valor.length>0) {
		       var filtro = /^[a-z||0-9||_||-]+[.||a-z||0-9||_||-]+@[a-z||0-9||_||-]+[.||a-z||0-9||_||-]+[.][a-z]{2,4}$/;
		       if (!filtro.test(valor)) {return this.error(' no es un email valido (nombre@dominio.com)');}
		} else if (this.type==9 && valor.length>0) {
			//var filtro = /^(http:\/\/ ){0,1}[a-z]+[.||a-z||0-9||_||-][.][a-z]{2,4}$/;
			var filtro=/^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
			if (!filtro.test(valor)) {return this.error(' no es una url valida (http://www.dominio.com/...)');}
		} else if (this.type==5 && (document.getElementById(this.field).selectedIndex < -1 || document.getElementById(this.field).options[document.getElementById(this.field).selectedIndex].value < -1)) { 
			return this.error(' debe tener una seleccion.');
		} else if (this.type==6 && !this.nul && valor.length==0) {
			return this.error(' debe tener una seleccion.');
		}else if (this.type==7 && !this.nul && !document.getElementById(this.field).checked) {return this.error(' debe marcarse.');}
		
		return this.error('');
	}
	
	this.error=function(error) {
		if (error=='') { 
			if (this.err!='' && document.getElementById(this.err)) {document.getElementById(this.err).innerHTML='&nbsp;'; } 
			return(true); 
		} else {
			if (this.err!='' && document.getElementById(this.err)) { 
				document.getElementById(this.err).innerHTML='Este campo '+error; 
				window.scroll(0,document.getElementById(this.err).offsetTop);
			} else {alert('El campo ' + this.title+ error);}
			document.getElementById(this.field).focus();
			return(false);
		}
	}
}

function numformat(num,mil,dec,eur) {
	num=(Math.round(num*Math.pow(10,dec))/Math.pow(10,dec))+Math.pow(10,-(dec*2));
	num=num.toString();
	num2=num.substr(num.lastIndexOf('.'),dec+1);
	num=num.substr(0,num.lastIndexOf('.'));
	for (j=0;j<num.length+1;j++) { if (j%mil==0 && j!=num.length && j!=0) { num2=','+num.charAt(num.length-j)+num2; } else { num2=num.charAt(num.length-j)+num2; } }
	if (eur) { num2=num2.replace(/[.]/g,'|').replace(/[,]/g,'.').replace(/[|]/g,','); }
	return num2;
} 

function numval(num,eur) {
	if (eur) { return parseFloat(num.replace(/[.]/g,'').replace(/[,]/g,'.')); } else { return parseFloat(num.replace(/[,]/g,'')); }
}

function goUrl(a,id) {
	if (flds.fields[id][1].check() && document.getElementById(id).value.replace(/^\s+/,'')!='') {a.href=document.getElementById(id).value;} else {a.href="javascript:void(0);"}
}
