
var divMes=null;
function hiddeDivMes(){
	if(divMes){
		try{
			divMes.style.visibility="hidden";
		}catch(e){
		}
	}
}

validador.prototype.getElements=function(){

	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]);
				}
			}
		}
	}
}


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') == '1'){
				if(fo.elements[i].getAttribute('validacion')){
					this.controles.push(fo.elements[i]);
				}
			}
		}
	}
	this.divMensaje=document.createElement('div');
	this.divMensaje.className = "bgwarning";
	this.divMensaje.style.position="absolute";
	this.divMensaje.style.padding="40px 20px 0px 16px";
	this.divMensaje.style.background="url("+root+"images/globo.png) no-repeat";
	this.divMensaje.style.width="183px";
	this.divMensaje.style.height="84px";
	//this.divMensaje.style.border="";
	//this.divMensaje.style.borderColor="";
	//this.divMensaje.style.color="#CC0000";
	this.divMensaje.style.color="#000000";
	this.divMensaje.style.fontSize="13px";
	this.divMensaje.style.fontWeight="normal";
	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'){
		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;
				}

		case 'TEXTO':
			if(control.value.length>0){
				return true;
			}else{
				return false;
			}

		default:
			return false;


	}
}
validador.prototype.validarControl=function(control){
	switch(control.nodeName.toUpperCase()){
		case 'INPUT':
			return this.validarINPUT(control);

		case 'SELECT':
			return this.validarSELECT(control);

		case 'TEXTAREA':
			return this.validarTEXTAREA(control);

		default:
			return false;

	}
}
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(){

	try{
		this.getElements();
		var retorno=true;
		var control=null;
		this.ponerEstilosNormales();
		for (var i=0; i<this.controles.length; i++){
			if(this.controles[i].style.display == "none"){continue;}
			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=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);

				//left_-=270;
				top_+=10;

				if(parseInt(top_)>0 && parseInt(left_)>0){
					this.divMensaje.style.top=(top_)+"px";
					this.divMensaje.style.left=(left_)+"px";

					if(this.divMensaje.style.top && this.divMensaje.style.left){
						if(control.getAttribute("msg")){
							this.divMensaje.innerHTML=control.getAttribute("msg");
						}else{
							this.divMensaje.innerHTML="Campo obligatorio, debe llenar los campos que han sido marcados con color rojo.";
						}
						this.divMensaje.style.visibility="visible";
						divMes=this.divMensaje;
						setTimeout("hiddeDivMes()", 3500);
					}else{
						if(control.getAttribute("msg")){
							alert(control.getAttribute("msg"));
						}else{
							alert("Campo obligatorio, debe llenar los campos que han sido marcados con color rojo.");
						}
					}

				}else{
					if(control.getAttribute("msg")){
							alert(control.getAttribute("msg"));
						}else{
							alert("Campo obligatorio, debe llenar los campos que han sido marcados con color rojo.");
						}

				}


			}catch(e){

				alert(e);
				return false;
			}

		}

		return retorno;
	}catch(e){
		return false;
	}
}


