/////////////////////////////////////////////////////////////////
// Clase FormValidator                                         //
///////////////////////////////////////////////////////////////// 

function FormValidator(nombreForm, arrayCampos) {
	this.nombre = nombreForm;
	this.arrayCampos = arrayCampos;
	this.mandatoryErrorMsg = "Lista de Campos Obligatorios:";
	this.validateErrorMsg = "Lista de campos validados con Error:";
}

FormValidator.prototype.getNombre = function() {
	return this.nombre;
}

FormValidator.prototype.validate = function() {
	if(!this.validateMandatoryFields()) {
		if(!this.validateFields()) {
			return true;
		}
	}
	return false;
}

FormValidator.prototype.validateMandatoryFields = function() {
	var hayError = false;
	var msgError = this.mandatoryErrorMsg;
	for (var i = 0; i < this.arrayCampos.length; i++) {
		var campoAct = new Campo();
		campoAct = this.arrayCampos[i];
		
		var campo = document.getElementById(campoAct.getId()) || campoAct.getId();
		if( campo == null ) {
			hayError = true;
			msgError += "\n "+campoAct.getNombre()+" "+campoAct.getValidatorType().getMensaje();
			break;	
		}
		
		var valorCampo = campo.value;
		valorCampo = valorCampo.split('');
		var valN = [];
		for(var p = 0; p < valorCampo.length ; p++){
			if(valorCampo[p] == ('\n') || valorCampo[p] == ('\r')){
				valorCampo[p] = [];
			}
			valN[valN.length] = valorCampo[p];
		}
		valorCampo = valN.join('');
		//alert('valorCampo:'+valorCampo+'#');
		valorCampo = Trim(valorCampo);
		if((campoAct.isMandatory()) && ((valorCampo == '') || ((valorCampo == '-1') && campo.selectedIndex == 0)) && (!campo.disabled)) {
			hayError = true;
			msgError += "\n " + campoAct.getNombre();
			
			if( Trim(campoAct.claseNOK) ) {
				campo.className = campoAct.claseNOK;
			}
		}
		else {
			if( Trim(campoAct.claseOK) ) {
				campo.className = campoAct.claseOK;
			}
		}
	}
	if(hayError) {
		alert(msgError);
	}
	return hayError;
}

FormValidator.prototype.validateFields = function() {
	var hayError = false;
	var msgError = this.validateErrorMsg ;
	
	for(var i = 0; i < this.arrayCampos.length; i++) {
		var campoAct = new Campo();
		campoAct = this.arrayCampos[i];
		var campo = document.getElementById(campoAct.getId()) || campoAct.getId();
		
		if( campo == null) {
			hayError = true;
			msgError += "\n "+campoAct.getNombre()+" "+campoAct.getValidatorType().getMensaje();
			break;	
		}
		
		var valorCampo = campo.value;
		//valorCampo = Trim(valorCampo);
		var validadorAct = campoAct.getValidatorType();		
		
		if ((validadorAct!="") && (valorCampo.length!=0) && (!campo.disabled) ) {	
			var validacion = validadorAct.validate(valorCampo);
			if(!validacion) {	
				//Msg de error, y hay error
				hayError = true;
				msgError += "\n "+campoAct.getNombre()+" "+campoAct.getValidatorType().getMensaje();
				
				if( Trim(campoAct.claseNOK) ) {
					campo.className = campoAct.claseNOK;
				}
			}
			else {
				if( Trim(campoAct.claseOK) ) {	
					//Validacion de NIF
					/*
  				if(validacion.substring(0,3)=='NIF') {
						document.getElementById(campoAct.getId()).className = campoAct.claseOK;
						document.getElementById(validacion.substring(4,validacion.length)).value=validacion.substring(3,4)
					}
					*/
					//Validacion para NumSS
					if(validacion.length > 4 && this.sonDigitos(validacion.substring(0,1))) {
						campo.className = campoAct.claseOK;
						campo.value = validacion
					} //Resto de Validaciones
					else {					
						campo.className = campoAct.claseOK;
					}
				}
			}
		}
	}
	if (hayError) {
		alert (msgError);
	}
	return hayError;
}

FormValidator.prototype.setErrorMsgs = function( mandatoryErrorMsg, validateErrorMsg ) {
	this.mandatoryErrorMsg = mandatoryErrorMsg;
	this.validateErrorMsg = validateErrorMsg ;
}

FormValidator.prototype.sonDigitos = function(numero) {
	var xcadena = ""+numero;
	for(var i = 0; i < xcadena.length; i++) {
		var lcaracter = xcadena.charAt(i);
		if(lcaracter  < "0" || lcaracter > "9") {
			return false;
		}
	}
	return true;
}

/////////////////////////////////////////////////////////////////
// Clase Campo                                                 //
///////////////////////////////////////////////////////////////// 

function Campo(nombreField, idField, isMandatory, validatorType, claseOK, claseNOK) {
	this.nombre = nombreField; 		// Label del campo
	this.id = idField; 						// Identificador del campo o referencia al objeto campo
	this.mandatory = isMandatory; // true en caso de ser obligatorio rellenarlo, false en caso contrario
	this.tipo = validatorType; 		// Tipo de validador
	this.claseOK = claseOK;    		// clase a aplicar al campo si se valida correctamente
	this.claseNOK = claseNOK;  		// clase a aplicar al campo si es obligatorio o erroneo 
}

Campo.prototype.getNombre = function() {
	return this.nombre;
}

Campo.prototype.getId = function() {
	return this.id;
}

Campo.prototype.isMandatory = function() {
	return this.mandatory;
}

Campo.prototype.getValidatorType = function() {
	return this.tipo;
}















