function validarDOC(valor){
	var regexp = /^([0-9]{8,8}[a-zA-Z])$/;
	var regexp2 = /^([ABCDEFGHKLMNPQSabcdefghklmnpqs][0-9]{7,7}[0-9A-J])$/;
	if(regexp2.test(valor)) {
		return validarCIF(valor);
	}else if(regexp.test(valor)) 
	{
		return validarDNI(valor);
	}
	return false;
}
function validarDNI(valor)
{
	var letras = 'T,R,W,A,G,M,Y,F,P,D,X,B,N,J,Z,S,Q,V,H,L,C,K,E,T'.split(',');
	var pos = valor.substr(0,valor.length-1) % 23
	var letra = valor.substr(valor.length-1,valor.length);
	if (letra.toUpperCase() == letras[pos]) 
	{
		return true;
	}
	return false;
}
 function validarCIF(texto){
        var pares = 0;
        var impares = 0;
        var suma;
        var ultima;
        var unumero;
        var uletra = new Array("J", "A", "B", "C", "D", "E", "F", "G", "H", "I");
        var xxx;
        texto = texto.toUpperCase();
        var regular = new RegExp(/^[ABCDEFGHKLMNPQSabcdefghklmnpqs]\d\d\d\d\d\d\d[0-9,A-J]$/g);
         if (!regular.exec(texto)) return false;
         ultima = texto.substr(8,1);
         for (var cont = 1 ; cont < 7 ; cont ++){
             xxx = (2 * parseInt(texto.substr(cont++,1))).toString() + "0";
             impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
             pares += parseInt(texto.substr(cont,1));
         }
         xxx = (2 * parseInt(texto.substr(cont,1))).toString() + "0";
         impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
         
         suma = (pares + impares).toString();
         unumero = parseInt(suma.substr(suma.length - 1, 1));
         unumero = (10 - unumero).toString();
         if(unumero == 10) unumero = 0;
         
         if ((ultima == unumero) || (ultima == uletra[unumero]))
             return true;
         else
             return false;

 }
 function validarEMAIL(valor)
{
	var regexp = /^([\w-\.]+)@([\w-\.]+)\.[a-zA-Z]{2,4}$/;
	return regexp.test(valor)
}
function validarTFN(valor,tipo)
{
	var regexp = /^[689]{1}[0-9]{8}$/;
	return regexp.test(valor)
}
var Validator = {
	formID:'',
	Version: 'V0.1_r0'
	, Libname: 'Validate Form'
	, Owner: 'Canaryweb S.L.'
	, Author: 'CanaryWeb S.L.'
	, error: false
	, error2: false
	, resalt:false
	, foco: false
	, rMSG:''
	,  load: function(form) {
		Validator.formID=form;
	},
	Info:function(){
		alert(Validator.Version);
	}
	, Inspect: function(){
		var form=document.getElementById(Validator.formID);
		for(i=0;i<form.length;i++){
			Validator.error2=false;
			Validator.resalt=false;
			var anchor = form.elements[i];
			//alert(anchor.style.display);
			//alert(anchor.name+' = '+ anchor.value+'\nValidacion: '+anchor.className);
			if(anchor.type.toLowerCase()=='radio'){
				var anchor2;
				end=true;
				for(l=1;end;){
					anchor2=form.elements[i];
					if(!(anchor.name==anchor2.name) || anchor2.type.toLowerCase()!='radio'){
						end=false;
						i--;
						l--;
					}else{
						i++;
						l++;
					}						
				}
				//var classAttribute = String(anchor.getAttribute('class'));
				var classAttribute = String(anchor.className);
				if (classAttribute!='null'){
					classAttribute=classAttribute.split(' ');
				}
				for(j=0;j<classAttribute.length;j++){
					if(classAttribute[j]=='RADIO'){
						Validator.ValCHK(anchor.name,l);
					}			
				}
			}else{
				var classAttribute = String(anchor.className);
				if (classAttribute!='null'){
					classAttribute=classAttribute.split(' ');
					if(!anchor.disabled){
						for(j=0;j<classAttribute.length;j++){
							validation=classAttribute[j];
							Validator.ValField(anchor,validation);
						}
					}
				}
			}

		}
		if(!Validator.error){
			return true;
		}else if(Validator.error){
			alert('Se produjeron los siguientes errores:\n'+Validator.rMSG+'\nPor favor, revise los campos resaltados.');
			Validator.error=false;
			Validator.error2=false;
			Validator.resalt=false;
			Validator.foco=false;
			Validator.rMSG='';
			return false;
		}else{
			return false;
		}
		
	}
	, preInspect: function(array){
		for(i=0;i<array.length;i++){
			Validator.error2=false;
			Validator.resalt=false;
			var anchor = array[i];
			if(anchor.type.toLowerCase()=='radio'){
				var anchor2;
				end=true;
				for(l=1;end;){
					anchor2=array[i];
					if(!(anchor.name==anchor2.name) || anchor2.type.toLowerCase()!='radio'){
						end=false;
						i--;
						l--;
					}else{
						i++;
						l++;
					}						
				}
				var classAttribute = String(anchor.className);
				if (classAttribute!='null'){
					classAttribute=classAttribute.split(' ');
				}
				for(j=0;j<classAttribute.length;j++){
					if(classAttribute[j]=='RADIO'){
						Validator.ValCHK(anchor.name,l);
					}			
				}
			}else{
				var classAttribute = String(anchor.className);
				if (classAttribute!='null'){
					classAttribute=classAttribute.split(' ');
					if(!anchor.disabled){
						for(j=0;j<classAttribute.length;j++){
							validation=classAttribute[j];
							Validator.ValField(anchor,validation);
						}
					}
				}
			}

		}
		if(!Validator.error){
			return true;
		}else if(Validator.error){
			alert('Se produjeron los siguientes errores:\n'+Validator.rMSG+'\nPor favor, revise los campos resaltados.');
			Validator.error=false;
			Validator.error2=false;
			Validator.resalt=false;
			Validator.foco=false;
			Validator.rMSG='';
			return false;
		}else{
			return false;
		}
		
	}
	, ValField: function(anchor,validation){
		if(validation=='R'){
			if(anchor.value==''){
				Validator.rMSG+='- '+anchor.title+' es un campo requerido\n';
				Validator.resalt=true;
				Validator.error=true;
				Validator.error2=true;
			}
			if(Validator.error&&!Validator.foco){
				Validator.foco=true;
				anchor.focus();
			}
		}
		if(validation=='RC'){
			if(!document.getElementById(anchor.lang).checked){
				if(anchor.value==''){
					Validator.rMSG+='- '+anchor.title+' es un campo requerido\n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}
				if(Validator.error&&!Validator.foco){
					Validator.foco=true;
					anchor.focus();
				}
			}
		}
		if(validation=='DNI'){
			if(anchor.value!=''){
				if(!validarDOC(anchor.value)){
					Validator.rMSG+='- '+anchor.title+ ': La letra no es correcta \n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}
				if(Validator.error&&!Validator.foco){
					Validator.foco=true;
					anchor.focus();
				}
			}
		}
		if(validation=='EMAIL'){
			if(anchor.value!=''){
				if(!validarEMAIL(anchor.value)){
					Validator.rMSG+='- '+anchor.title+ ': No es una dirección de correo válida. \n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}
				if(Validator.error&&!Validator.foco){
					Validator.foco=true;
					anchor.focus();
				}
			}
		}
		if(validation=='INT'){
			if(anchor.value!=''){
				var regexp = /^[0-9]*$/;
				if(!regexp.test(anchor.value)) {
					Validator.rMSG+='- '+anchor.title+ ': Debe ser un número entero.\n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}
				if(Validator.error&&!Validator.foco){
					Validator.foco=true;
					anchor.focus();
				}
			}
		}
		// admite números enteros y flotantes
		if(validation=='FLOAT'){
			if(anchor.value!=''){
				anchor.value = anchor.value.replace(/,/,".");
				num = parseFloat(anchor.value);
				if (isNaN(num)) {
					Validator.rMSG+='- '+anchor.title+ ': Debe ser un número.\n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}
				if(Validator.error&&!Validator.foco){
					Validator.foco=true;
					anchor.focus();
				}
			}
		}

		if(validation=='SELECT'){
			if(anchor.value==anchor.lang){
				Validator.rMSG+='- El valor elegido para '+anchor.title.toLowerCase()+ ' no es válido \n';
				Validator.resalt=true;
				Validator.error=true;
				Validator.error2=true;
			}
			if(Validator.error&&!Validator.foco){
				Validator.foco=true;
				anchor.focus();
			}
		}
		if(validation=='TFN'){
			if(anchor.value!=''){
				if(!validarTFN(anchor.value)){
					Validator.rMSG+='- '+anchor.title+ ': No es un teléfono válido. \n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}
				if(Validator.error&&!Validator.foco){
					Validator.foco=true;
					anchor.focus();
				}
			}
		}

		if(validation.match(/NUM/)){
			if(anchor.value!=''){
				var max=validation.split('#')[1];
					//alert(anchor.value);
				if(max){
					eval('compare='+anchor.value+max.substr(0,1)+'='+max.substr(1)+'?false:true');
					//alert(compare);
				}
				var regexp = /^([0-9]+)$/;
				if(!regexp.test(anchor.value)){
					Validator.rMSG+='- '+anchor.title+ ': No es un valor numérico. \n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}else if(compare){
					parsea=max.substr(0,1)=='<'?'superior':'inferior';
					Validator.rMSG+='- '+anchor.title+ ': No puede ser '+parsea+' a '+max.substr(1)+' meses. \n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}else if(anchor.length!=anchor.maxlength){
					Validator.rMSG+='- '+anchor.title+ ': No tiene la longitud adecuada. \n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}
				if(Validator.error&&!Validator.foco){
					Validator.foco=true;
					anchor.focus();
				}
			}
		}
		if(validation=='LENMAXN'){
			if(anchor.value!=''){
				var regexp = /^([0-9]+)$/;
				if(!regexp.test(anchor.value)){
					Validator.rMSG+='- '+anchor.title+ ': No es un valor numérico. \n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}else if(anchor.value.length!=anchor.maxLength){
					Validator.rMSG+='- '+anchor.title+ ': No tiene la longitud adecuada. Escriba '+anchor.maxLength+' caracteres. \n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}
				//alert(anchor);
				if(Validator.error&&!Validator.foco){
					Validator.foco=true;
					anchor.focus();
				}
			}
		}
		if(validation.match(/IMPORTE/)){
			var compare=false;
			if(anchor.value!=''){
				anchor.value = anchor.value.replace(/,/,".");
				var max=validation.split('#')[1];
					//alert(validation);
				if(max){
					eval('compare='+anchor.value+max.substr(0,1)+'='+max.substr(1)+'?false:true');
					//alert(compare);
				}
				num = parseFloat(anchor.value);
				if (isNaN(num)) {
					Validator.rMSG+='- '+anchor.title+ ': No tiene un formato adecuado. \n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}else if(compare){
					parsea=max.substr(0,1)=='<'?'mayor':'menor';
					Validator.rMSG+='- '+anchor.title+ ': Su valor no puede ser '+parsea+' de '+max.substr(1)+'€. \n';
					Validator.resalt=true;
					Validator.error=true;
					Validator.error2=true;
				}
				if(Validator.error&&!Validator.foco){
					Validator.foco=true;
					anchor.focus();
				}
			}
		}
		if(Validator.resalt){
			anchor.style.border='2px red dotted';
		}else{
			anchor.style.border='1px #A5ACB2 solid';
		}
	}
	, ValCHK: function(campo,ncampos){
		var onechk=false,val,reference;
		for(p=1;p<=ncampos;p++){
			reference=campo+"_"+p;
			val=document.getElementById(reference);
			if(val.checked && !onechk)
				onechk=true;
		}
		if(!onechk){
			val=document.getElementById(campo+"_1");
			Validator.rMSG+='- '+val.title+ '\n';
			Validator.error=true;
			if(Validator.error&&!Validator.foco){
				Validator.foco=true;
				val.focus();
			}
		}
	}
	/*,ValCCC:function (){
	}*/
}

