var txt_RUT_Empresa = 'RUT Empresa';
var txt_RUT_Admin = 'RUT Administrador';
var txt_PIN = '';

/* FUNCIONES INICIALES ****************/
window.addEvent('domready', function() {
	//carrusel
	//carruselScroll();
	
	//fade in login
	//$('mi_entel').fade(0,0);
	//$('boton_fadein').addEvent('click', function(){ $('mi_entel_inicio').fade(0); $('mi_entel').fade(1); });
	//$('boton_fadeout').addEvent('click', function(){ $('mi_entel_inicio').fade(1); $('mi_entel').fade(0); });

	//mensaje de error
	$('alerta_error').fade(0,0);
	$('q').value = "";
	eventualizaLoginEmpresa('EMPATRUT', txt_RUT_Empresa);
	eventualizaLoginAdmin('USURUT', txt_RUT_Admin);
	eventualizaLoginPIN('USUPAS', txt_PIN);
	eventualizaLoginLabelClave()
});
/*************************************/

function eventualizaLoginEmpresa(campo, valor) {
	document.getElementById(campo).onfocus = function() {
		if(this.value == valor) {
			this.value = '';
		}
	};
	document.getElementById(campo).onblur = function() {
		if(this.value != '') {
			//validaFormulario(); 
			validaLoginRUT(campo, 'Debes ingresar un RUT Empresa válido.');
		} else {		
			this.value = valor;
			limpiarCasillas([campo],'input_cuenta');
			
			if($('USURUT').value == txt_RUT_Admin && $('USUPAS').value == txt_PIN) {
				$('alerta_error').fade(0);
			}
		}
	};
	/*document.getElementById(campo).onkeypress = function(event) {
		return soloNumeros(event);
	};*/
}

function eventualizaLoginAdmin(campo, valor) {
	document.getElementById(campo).onfocus = function() {
		//this.maxLength = 9;
		if(this.value.substr(0,3) == valor.substr(0,3)) {
			this.value='';
		}
	};
	document.getElementById(campo).onblur = function() {
		//this.maxLength = 12;
		if(this.value != '') {
			//validaFormulario(); 
			validaLoginRUT(campo, 'Debes ingresar un RUT Administrador válido.');
		} else {	
			this.value = valor;
			limpiarCasillas([campo],'input_cuenta');
			
			if($('EMPATRUT').value == txt_RUT_Empresa && $('USUPAS').value == txt_PIN) {
				$('alerta_error').fade(0);
			}
		}
	};
	/*document.getElementById(campo).onkeypress = function(event) {
		return soloRUT(event);
	};*/
}

function eventualizaLoginLabelClave() {
	document.getElementById('labelClave').onclick = function() {
		this.style.display = 'none';
		document.getElementById('USUPAS').focus();
	};
}

function eventualizaLoginPIN(campo, valor) {
	document.getElementById(campo).onfocus = function() {
		if(this.value == valor) {
			this.value='';
			document.getElementById('labelClave').style.display = 'none';
		}
	};
	document.getElementById(campo).onblur = function() {
		if(this.value != '') {
			//validaFormulario(); 
			validaLoginPIN();
		} else {			
			this.value = valor;
			document.getElementById('labelClave').style.display = 'block';
			
			limpiarCasillas([campo],'input_cuenta');
			if($('EMPATRUT').value == txt_RUT_Empresa && $('USURUT').value == txt_RUT_Admin) {
				$('alerta_error').fade(0);
			}
		}
	};
	document.getElementById(campo).onkeyup = function() {
		if(this.value.length==20) { 
			//validaFormulario(); 
			validaLoginPIN();
		}
	};
}

function soloRUT(evt) {
	var key = evt.keyCode ? evt.keyCode : evt.which ;
	return (key <= 31 || (key >= 48 && key <= 57) || key == 75 || key == 107); 
}

function soloNumeros(evt){
	var key = evt.keyCode ? evt.keyCode : evt.which ;
	return (key <= 31 || (key >= 48 && key <= 57)); 
}


function validaLoginRUT(campo, msg) {
	var errores = new Array();
	formatearRut(campo);
	
	limpiarCasillas([campo],'input_cuenta');
	if($(campo).value!="" && !validarRutCompleto(campo,"10")){ errores.push([campo,msg]); }
	
	if(errores.length > 0) {
		mostrarErrores(errores);
		return false;
	} else {
		$('alerta_error').fade(0); //requiere mootools
		return true;
	}
}

function validaLoginPIN() {
	var errores = new Array();
	
	limpiarCasillas(["USUPAS"],'input_cuenta');
	//if($("USUPAS").value!="" && !validacionAlfanumerica("USUPAS","4")){ errores.push(["USUPAS","Clave incorrecta."]); }
	if($("USUPAS").value.length < 4) { errores.push(["USUPAS","Clave debe tener 4 caracteres como minimo."]); }

	if(errores.length > 0) {
		mostrarErrores(errores);
		return false;
	} else {
		$('alerta_error').fade(0); //requiere mootools
		return true;
	}
}
