function validador(fo){
	this.formulario=fo;
	this.controles=new Array();
	if(fo){ 
		for(var i=0; i<fo.elements.length; i++){
			if (fo.elements[i].getAttribute('Req')){
				if(fo.elements[i].getAttribute('validacion')){
					this.controles.push(fo.elements[i]);
				}
			}
		}
	}
}

validador.prototype.validarINPUT=function(control){
	if(control.type.toUpperCase()=='TEXT' || control.type.toUpperCase()=='PASSWORD'){
		switch(control.getAttribute('validacion').toUpperCase()){
		case 'FECHA':
			retorno = false;
			var itm = control.value;
			if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) return true;
			if (itm.match(/^\d\d\d\d[\/-]\d\d[\/-]\d\d$/)) return true;
			if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) return true;
			return retorno;
			break;
		case 'ENT+':
			if (!isNaN(parseInt(control.value))){
				control.value=parseInt(control.value);
				if (parseInt(control.value)>0){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
			break;
		case 'ENT-':
			if (!isNaN(parseInt(control.value))){
				control.value=parseInt(control.value);
				if (parseInt(control.value)<0){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
			break;
		case 'DECIMAL':
			if (!isNaN(parseFloat(control.value))){
				control.value=parseFloat(control.value).toFixed(2);
				return true;
			}else{
				return false;
			}
			break;
		case 'TEXTO':
			if (control.value.length>0){
				return true;
			}else{
				return false;
			}
			break;
		case 'EMAIL':
			
			var validateEmail = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/.
			test(control.value);
			if(validateEmail) {
				return true;
			}	else		{
				return false;
			}
			break;
		}
	}else{
		
		return false;
	}
}

validador.prototype.ponerEstilosNormales=function(){
	
		for (var i=0; i<this.controles.length; i++){
			if (this.controles[i].className){
				if(this.controles[i].className.substring(this.controles[i].className.length-3, this.controles[i].className.length).toUpperCase()=="BAD"){
					Style=this.controles[i].className.substring(0, this.controles[i].className.length-3);
				}else{
					Style=this.controles[i].className;
				}
				this.controles[i].className=Style;
			}
		}
	
}

validador.prototype.validarSELECT=function(control){
	if (control.value==0){
		return false;
	}else{
		return true;
	}
}
validador.prototype.validarTEXTAREA=function(control){
	switch(control.getAttribute('validacion').toUpperCase()){
		case 'MENSAJE':
				if(control.value.length>5){
					return true;
				}else{
					return false;
				}
			break;//por gusto
		case 'TEXTO':
			if(control.value.length>0){
				return true;
			}else{
				return false;
			}
			break; //por gusto
		default: 
			return false;
			break; // por gusto
		
	}
}
validador.prototype.validarControl=function(control){
	switch(control.nodeName.toUpperCase()){
		case 'INPUT':
			return this.validarINPUT(control);
			break;
		case 'SELECT':
			return this.validarSELECT(control);
			break;
		case 'TEXTAREA':
			return this.validarTEXTAREA(control);
			break;
		default:
			return false;
			break;
	}
}
validador.prototype.validar=function(){
	var retorno=true;
	var control=null;
	this.ponerEstilosNormales();
	for (var i=0; i<this.controles.length; i++){
		if (this.controles[i].className){
			if(this.controles[i].className.substring(this.controles[i].className.length-3, this.controles[i].className.length).toUpperCase()=="BAD"){
				Style=this.controles[i].className.substring(0, this.controles[i].className.length-3);
			}else{
				Style=this.controles[i].className;
			}
			BADStyle=Style+"BAD";
		}
		
		if (!this.validarControl(this.controles[i])){
			this.controles[i].className=BADStyle;
			retorno=false;
			if (control==null){
				control=this.controles[i];
			}
		}
		
		
	}
	if (control!=null){
		control.focus();
	}
	return retorno;
}

