var divMes=null;
function hiddeDivMes(){
	if(divMes){
		try{
			divMes.style.visibility="hidden";
		}catch(e){
		}
	}
}
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]);
				}
				else{
					alert('El campo ' + fo.elements[i].name + ' tiene el atributo req, pero le falta el atributo validacion.') ;
				}
			}
		}
	}
	this.divMensaje=document.createElement('div');
	this.divMensaje.style.position="absolute";
	this.divMensaje.style.padding="20px 10px 20px 30px";
	this.divMensaje.style.backgroundImage="url(images/globo.gif)";
	this.divMensaje.style.width="150px";
	this.divMensaje.style.heigth="75px";
	this.divMensaje.style.border="1px";
	this.divMensaje.style.borderColor="#ffffff";
	this.divMensaje.style.color="#000000";
	this.divMensaje.style.fontSize="10px";
	this.divMensaje.style.fontWeight="bold";
	this.divMensaje.innerHTML=" msgtext ";
	this.divMensaje.style.visibility="hidden";
	this.formulario.appendChild(this.divMensaje);
	this.divMensaje.style.zIndex=1000;
}

validador.prototype.validarINPUT=function(control){
	if(control.type.toUpperCase()=='TEXT' || control.type.toUpperCase()=='PASSWORD' || control.type.toUpperCase()=='HIDDEN'){
		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.getAttribute('multiple')){
		return this.validarMULTIPLE_SEL(control) ;
	}
	else{
		if (control.value==0){
			return false;
		}else{
			return true;
		}
	}
}

validador.prototype.validarMULTIPLE_SEL = function(control){

	var options = control.getElementsByTagName('option') ;
	var haySeleccionado = false ;

	for(var i = 0 ; i < options.length ; i++){
		if(options[i].selected == true){
			haySeleccionado = true ;
			break ;
		}
	}
	return haySeleccionado ;
}

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.getLeft=function(elemento){
 	try{

 		if (elemento.offsetParent){
 			return (elemento.offsetLeft + (this.getLeft(elemento.offsetParent))) ;
	 	} else{
	 		return (elemento.offsetLeft) ;
	    }
 	}catch(e){
 		return e;
 	}


}
validador.prototype.getTop=function(elemento){
 	try{
 		if (elemento.offsetParent){
 		return (elemento.offsetTop + (this.getTop(elemento.offsetParent))) ;
	 	} else{
	 		return (elemento.offsetTop) ;
	    }
 	}catch(e){
 		return e;
 	}


}
validador.prototype.validar=function(){

	var retorno=true;
	var control=null;

	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{
				if(this.controles[i].type.toUpperCase() != 'HIDDEN'){
					alert("El control: "+this.controles[i].name+" no tiene definicion de estilo");
					return false;
			}
		}
		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();

		try{
			var left_=this.getLeft(control);
			var top_=this.getTop(control);

			if(left_ < 1){
				left_=this.getLeft(control.parentNode);
				top_=this.getTop(control.parentNode);
			}

			if(parseInt(top_)>0 && parseInt(left_)>0){
				this.divMensaje.style.top=(top_-10)+"px";
				this.divMensaje.style.left=(left_+75)+"px";

				if(this.divMensaje.style.top && this.divMensaje.style.left){
					if(control.getAttribute("msg")){
						this.divMensaje.innerHTML=control.getAttribute("msg");
					}else{
						this.divMensaje.innerHTML="This field can't be empty";
					}
					this.divMensaje.style.visibility="visible";
					divMes=this.divMensaje;
					setTimeout("hiddeDivMes()", 4000);
				}else{
					if(control.getAttribute("msg")){
						alert(control.getAttribute("msg"));
					}else{
						alert("This field can't be empty");
					}
				}

			}else{
				if(control.getAttribute("msg")){
						alert(control.getAttribute("msg"));
					}else{
						alert("This field can't be empty");
					}

			}


		}catch(e){
			alert(e);
		}

	}

	return retorno;
}

