function js_es_decimal (objeto, valorActual) {
	// onkeypress="return js_es_decimal(event);"
	// decimal se marcan con '.' (charCode 46)
	var charCode
	if (document.getElementById && !document.all) charCode = objeto.which
	else charCode = objeto.keyCode

	if (charCode > 31 && (charCode < 48 || charCode >57) && charCode!=46) return false 
	else {
		if (charCode == 46 && valorActual.indexOf ('.') == -1) return true;
		else if (charCode != 46)  return true;
		else return false;
	}
}
function val_es_numero (e) {
	var evt = e || window.event;
	if (window.gecko) c = evt.charCode;
	else c = evt.keyCode;
	
	if (c > 31 && (c < 48 || c > 57) && c != 46) return false;
	else return true;
}

function val_es_cp (e) {
	var evt = e || window.event;
	if (window.gecko) c = evt.charCode;
	else c = evt.keyCode;

	if (c > 32 && (c < 48 || c > 57) && c != 46 && c != 63) {
		evt.preventDefault ();
		return false;
	}
	else {
		val_es_cpprov (this, this.value.substr (0, 2));
		return true;
	}
}
function val_es_cpprov (oThis, v) {
	if (oThis.lastCP != v) {
		vi = parseInt(v, 10);
		if (v.length < 2) oModBuscs.pr.forEach (function (oPr) { oPr.options [0].selected = true; });
		else if (isNaN (vi) || (vi < 1) || (vi > 52)) {
			console.log (vi);
			if (!isNaN (vi) && ((vi < 1) || (vi > 52))) window.alert ('El código postal debe empezar por un número entre 00 y 52');
		} else {
			oThis.lastCP = v;
			if ($defined (provincias)) {
				$A(provincias).forEach (function (prov) {
					if (oThis.lastCP == prov.substr (6, 2)) {
						if ($('au')) $('au').seleccionar (prov.substr (0, 2));
						else if (oThis.form.au) oThis.form.au.seleccionar (prov.substr (0, 2));
						if ($('pr')) $('pr').seleccionar (prov.substr (3, 2));
						else if ($defined(oModBuscs.pr)) oModBuscs.pr.forEach (function (o) { o.seleccionar (prov.substr (3, 2)); });
						else if (oThis.form.pr) oThis.form.pr.seleccionar (prov.substr (3, 2));
					}
				}.bind (this));
			}
		}
	}
}

function js_menu () {
	// Si existe, obtenemos el id de la zona
	tagBodies = document.getElementsByTagName ('body');
	var zonaActiva = tagBodies [0].getAttribute ('id');
	if (zonaActiva != '') zonaActiva = zonaActiva.substr (3);
	else zonaActiva = false;
	
	// Ajustamos el menu
	if ($('mnuh')) {
		//nImg = $(document.createElement ('img'));
		//nImg.setAttribute ('src', 'img.1024/mnuh_1.gif');
		//nImg.setAttribute ('width', '100');
		//nImg.setAttribute ('height', '20');
		$ES('#mnuh li').forEach (function (oLi) {
			if (oLi.id && oLi.id.indexOf ('mnu') >= 0) {
				//nImg.clone ().injectTop (oLi);
				//oLi.addEvent ('mouseenter', function () {
				//	this.getFirst ().setStyle ('visibility', 'visible');
				//});
				//oLi.addEvent ('mouseleave', function () {
				//	this.getFirst ().setStyle ('visibility', 'hidden');
				//});
				if (oLi.getLast ().getTag () == 'ul') {
					oLi.addEvent ('mouseenter', function () {
						this.setStyle ('height', 28+($ES('li', this).length*20)); 
						this.getLast ().setStyle ('display', 'block');
					});
					oLi.addEvent ('mouseleave', function () {
						this.setStyle ('height', 28);
						this.getLast ().setStyle ('display', 'none');
					});
				}
			}
		});
	}
}

function js_frm_ayuda (objAyuda)
{
	// Ajusta el formato para los textos de ayuda
	if (objAyuda.childNodes.length)
	{
		if (objAyuda.firstChild.tagName == 'IMG')
		{
			objAyuda.firstChild ['ayuda'] = objAyuda.firstChild.getAttribute ('alt');
			objAyuda.firstChild.setAttribute ('alt', '');
			objAyuda.firstChild.onmouseover = function () {
				var formayuda = new js_obj (divapoyoNombre)
				if (formayuda) {
					formayuda.obj ['ayuda'] = js_txt_leer (formayuda.obj);
					js_txt_escribir (formayuda.obj, this ['ayuda']);
					formayuda.style.border = 'red solid 1px';
					temp = setTimeout (function () { formayuda.style.border = '#09f solid 1px'; }, 1000);
	
				}
			}
			objAyuda.firstChild.onmouseout = function () {
				var formayuda = new js_obj (divapoyoNombre);
				if (formayuda)
				{
					js_txt_escribir (formayuda.obj, formayuda.obj ['ayuda']);
					formayuda.style.border = '#09f solid 1px';
				}
			}
		}
	}
}

function js_frm_ajustarapoyo ()
{
	var divapoyo = new js_obj (divapoyoNombre);
	if (divapoyoMover && divapoyo.style)
	{
		if (window.innerHeight)
		{
			pos = window.pageYOffset
		}
		else if (document.documentElement && document.documentElement.scrollTop)
		{
			pos = document.documentElement.scrollTop
		}
		else if (document.body)
		{
			pos = document.body.scrollTop
		}
		if (pos < divapoyoTop) pos = divapoyoTop;
		else pos += divapoyoTop;
		if (pos == divapoyoTopOld)
		{
			divapoyo.style.top = pos+'px';
		}
		divapoyoTopOld = pos;
		if (divapoyoMover) temp = setTimeout('js_frm_ajustarapoyo()',500);
	}
}

function js_acaprovinciachange (objname, norellenar)
{
	objcp = document.getElementById (objname);
	objprov = document.getElementById ('prov'+objname);
	
	if ((objcp.value.length < 5) && !norellenar) {
		while (objcp.value.length < 5) objcp.value = objcp.value + '0';
	}
	else if ((objcp.value.length < 5) && norellenar) {
		while (objcp.value.length < 5) objcp.value = objcp.value + '?';
	}
	
	if (parseInt (objcp.value.substr (0, 2), 10) > 52) {
		objcp.value = '52' + objcp.value.substr (2, 3);
		alert ('El cód. postal empieza por una cifra superior a 52, reducimos a 52.');
	}
	else if ((objcp.value.substr (0, 1) == '?') || (objcp.value.substr (0, 1) == '?')) {}
	else if (parseInt (objcp.value.substr (0, 2), 10) < 1) {
		objcp.value = '01' + objcp.value.substr (2, 3);
		alert ('El cód. postal empieza por una cifra inferior, aumentamos a 01.');
	}
	objprov.value = '';

	for (i=0;i<provincias.length;i++) {
		if (provincias [i].substr (6, 2) == objcp.value.substr (0, 2)) {
			cambiado = true;
			objprov.value = provincias [i].substr (12);
		}
	}
}

function js_acaprovinciaporcp (objname, objeto, permitidos)
{
	if (document.getElementById && !document.all)
		charCode = objeto.which
	else
		charCode = objeto.keyCode

	if (!permitidos) permitidos = new Array();

	if (charCode > 32 && (charCode < 48 || charCode > 57) && !permitidos.has (charCode))
	{
		return false;
	}
	else
	{
		objcp = document.getElementById (objname);
		objprov = document.getElementById ('prov'+objname);
		cambiado = false;
		if ((objcp.value == '') && (charCode >= 54) && !permitidos.has (charCode))
		{
			alert ('El código postal debe empezar por un dígito entre 0 y 5');
			return false;
		}
		else
		{
			if (objcp.value.length >= 2)
			{
				objprov.value = '';	
				for (i=0;i<provincias.length;i++)
				{
					if (provincias [i].substr (6, 2) == objcp.value.substr (0, 2))
					{
						cambiado = true;
						objprov.value = provincias [i].substr (12);
					}
				}
			}
			
			return true;
		}
	}
}

function js_acaacademiasselect (id, obj, tipo)
{
	if (!tipo) tipo = "";
	else tipo = "&t=" + tipo;

	return window.open('academias.select.php?id=' + id + tipo, '_blank', 'width=550,height=300,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
}

function js_textarea_maxLength ()
{
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++)
	{
		if (x[i].getAttribute('maxlength'))
		{
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			counterClone.style.textAlign = 'right';
			counterClone.style.paddingRight = '20px';
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = js_textarea_checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function js_textarea_checkMaxLength()
{
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
	{
		this.relatedElement.style.color = '#f66';
		this.value = this.value.substr (0, 250);
	}
	else
	{
		this.relatedElement.style.color = '#000';
	}
	this.relatedElement.firstChild.nodeValue = currentLength;
}

function js_fechaactual (nObj) {
	if ($(nObj).value == "") {
		d = new Date();
		dia = String (d.getDate ());
		if (dia.length < 2) dia = "0" + dia;
		mes = String (d.getMonth () + 1);
		if (mes.length < 2) mes = "0" + mes;
		year = d.getFullYear ();
		$(nObj).value = dia +"/"+ mes +"/"+ year;
	}
}

function js_inputTextArrayAdd (oA, sId) {
	var oNuevoBR = document.createElement ('BR');
	var oNuevoInput = document.createElement ('INPUT');
	oNuevoInput.type = 'text';
	oNuevoInput.id = sId+'[]';
	oNuevoInput.name = sId+'[]';
	oA.parentNode.parentNode.parentNode.previousSibling.firstChild.appendChild (oNuevoBR);
	oA.parentNode.parentNode.parentNode.previousSibling.firstChild.appendChild (oNuevoInput);
	return false;
}

function js_menuPoste () {
	$$('#cuerpodrc li').each (function (o) {
		if (o.getProperty ('id') != '') {
			o.addEvent ('mouseenter', function () {
				this.setStyle ('backgroundPosition', '0px 0px');
				if (this.getLast ().getTag () == 'ul') this.getLast ().setStyle ('display', 'block');
			});
			o.addEvent ('mouseleave', function () {
				this.setStyle ('backgroundPosition', '100px 0px');
				if (this.getLast ().getTag () == 'ul') this.getLast ().setStyle ('display', 'none');
			});
		}
	});
}

function changeInputType(oldElm, iType, iValue, blankValue, noFocus) {
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || !document.getElementById || !document.createElement) return;
  if(window.ie){
    var newElm=document.createElement('input');
    newElm.type=iType;
  } else {
    var newElm=document.createElement('span');
    newElm.innerHTML='<input type="'+iType+'" name="'+oldElm.name+'">';
    newElm=newElm.firstChild;
  }
  var props=['name','id','className','size','tabIndex','accessKey'];
  for(var i=0,l=props.length;i<l;i++){
    if(oldElm[props[i]]) newElm[props[i]]=oldElm[props[i]];
  }
  newElm.onfocus=function(){return function(){
    if(this.hasFocus) return;
    var newElm=changeInputType(this,'password',iValue,
      (this.value.toLowerCase()==iValue.toLowerCase())?true:false);
    if(newElm) {
    	newElm.hasFocus=true;
    	$(newElm).setStyle ('color', '#000000');
    }
  }}();
  newElm.onblur=function(){return function(){
    if(this.hasFocus)
    if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
    	$(this).setStyle ('color', '#999999');
      changeInputType(this,'text',iValue,false,true);
    }
  }}();
  newElm.hasFocus=false;
  if(!blankValue) {
  	newElm.value=iValue;
  	$(newElm).setStyle ('color', '#999999');
  }
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!window.ie && !blankValue) {
  	newElm.value=iValue;
  	$(newElm).setStyle ('color', '#999999');
  }
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm=newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}

function js_indexBuscador () {
	if ($('drclogin')) {
		changeInputType ($('urpassw'), 'text', 'clave de usuario', false, true);
		oIT = $('urlogin');
		oIT.value = oIT.title;
		oIT.setStyle ('color', '#999999');
		oIT.addEvent ('focus', function () {
			if (this.value == this.title) {
				this.value = '';
				$(this).setStyle ('color', '#000000');
			}
		});
		oIT.addEvent ('blur', function () {
			if (this.value.length == 0) {
				this.value = this.title;
				$(this).setStyle ('color', '#999999');
			}
		});
		
		$('drcloginform').addEvent ('submit', function (e) {
			conTexto = false;
			if ($('urlogin').value == $('urlogin').title) $('urlogin').value = '';
			else conTexto = true;
		});
		$('drcloginform').addEvent ('submit', function () {
			 wgestion = window.open('https://www.academias.com/adm.index.php', 'academiasssl', 'location=no,menubar=no,toolbar=yes,status=yes,scrollbars=yes,resizable=yes,width=800');
			 this.submit ();
		});
	}
}

window.addEvent ('domready', js_menu);
window.addEvent ('domready', js_menuPoste);
window.addEvent ('domready', js_indexBuscador);