function validador(fo){
	this.formulario=fo;
	this.customBadStyle = false ;
}

validador.prototype.validarINPUT=function(control){

	if(control.type.toUpperCase() == 'FILE'){

		if (control.value.length > 0){

			if(document.getElementById('msg_for_files'))
				document.getElementById('msg_for_files').parentNode.removeChild(document.getElementById('msg_for_files')) ;

			return true;
		}else{

			if(document.getElementById('msg_for_files'))
				msg_div = document.getElementById('msg_for_files') ;
			else
				msg_div = document.createElement('div') ;

			msg_div.id 		= 'msg_for_files'				;
			msg_div.style.color	= '#f00'						;
			msg_div.innerHTML	= 'Por favor seleccione un archivo'	;
			control.parentNode.appendChild(msg_div)			;
			return false ;
		}
	}
	else 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;
		case 'CAPTCHA':

			if(control.value == document.contact.comprobar.value){
				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.controles=new Array();

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

	//for(var i = 0 ; i < this.controles.length ; i++){
		//alert(this.controles[i].name + ' ----> ' + this.controles[i].getAttribute('Req')) ;
	//}

	this.ponerEstilosNormales();

	for (var i=0; i<this.controles.length; i++){
		BADStyle="indefinido";
		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";
		}else{
			alert("El control: "+this.controles[i].name+" no tiene definicion de estilo");

			return false;
		}


		if (!this.validarControl(this.controles[i])){
			this.controles[i].className= (this.customBadStyle != false ? this.customBadStyle : BADStyle);
			retorno=false;
			if (control==null){
				control=this.controles[i];
			}
		}


	}
	if (control!=null){
		control.focus();
	}
	return retorno;
}

