var bugRiddenCrashPronePieceOfJunk = (navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1);
var W3CDOM = (!bugRiddenCrashPronePieceOfJunk && document.getElementsByTagName && document.createElement);
var onLoadObj = new Object;

onLoadObj ['ini1'] = 'js_menu';
onLoadObj ['textAreaMax'] = 'js_setMaxLength';
onLoadObj ['chkImgs'] = 'js_chkImgs';
onLoadObj ['marcarTodos'] = 'js_chkMarcarTodos';

function js_inicializar()
{if (W3CDOM){
	for (var i in onLoadObj)
	{
		if (onLoadObj [i])
		{
			try {
				if (onLoadObj [i].indexOf ('(') > 0)
				{
					eval (onLoadObj [i]);
				}
				else
				{
					eval (onLoadObj [i] +'()');
				}
			} catch (er) {}
		}
	}
}}

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function Array_push() {
	var A_p = 0
	for (A_p = 0; A_p < arguments.length; A_p++) {
		this[this.length] = arguments[A_p]
	}
	return this.length
}
if (typeof Array.prototype.push == "undefined") {
	Array.prototype.push = Array_push
}

function Array_shift() {
	var A_s = 0
	var response = this[0]
	for (A_s = 0; A_s < this.length-1; A_s++) {
		this[A_s] = this[A_s + 1]
	}
	this.length--
	return response
}
if (typeof Array.prototype.shift == "undefined") {
	Array.prototype.shift = Array_shift
}

function js_menu ()
{
	// Ajustamos el menu
	var idMenuGeneral = 'menu';
	var objMenuGeneral = $(idMenuGeneral);
	if (objMenuGeneral)
	{
		// Recorremos los As de menu general que no sean de clase 'separador'
		var x = objMenuGeneral.getElementsByTagName ('a');
		for (var i=0;i<x.length;i++)
		{
			// a su primer hijo (<a>) lo adjudicamos como lanzador del menu
			js_menu_opcevent (x[i]);
		}
	}
}

function js_menu_opcevent (objeto)
{
	objeto.style.background = 'url(img/'+ objeto.getAttribute ('id') +'_0.gif) no-repeat right';

	objeto.onmouseover = function () {
		if (!this.style.background.match (/1/))
		{
			estadoImg = /(0|1)/;
			estadoCapturado = estadoImg.exec (this.style.background);
			if (estadoCapturado = estadoImg.exec (this.style.background))
			{
				this.estadoActual = estadoCapturado [1];
			}
			this.style.background = this.style.background.replace (/0|1/, 1);
		}
	}
	objeto.onmouseout = function () {
		this.style.background = this.style.background.replace (/0|1/, this.estadoActual);
	}
}

function js_setMaxLength()
{
	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');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = js_checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function js_checkMaxLength()
{
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function js_chkImgs ()
{
	if (booChkImgs)
	{
		imgChks = getElementsByClass ('imgcheckbox', null, 'img');
		if (imgChks.length > 0)
		{
			var pattVal = /(^|\s)chkval(\d+)(\s|$)/;
	
			for (i=0; i<imgChks.length; i++)
			{
				valSearch = imgChks [i].className.match (pattVal);
				if (valSearch != null)
				{
					imgChks [i].value = valSearch [2];
				}
				else imgChks [i].value = 0;

				if (imgChks [i].value >= 0 && imgChks [i].value <= 9)
					imgChks [i].value = (imgChks [i].value ? imgChks [i].value : '0');
				else
					imgChks [i].value = '0';
				if (valSearch == null) imgChks [i].className += ' chkval' + imgChks [i].value;
				
				imgChks [i].onclick = function ()
				{
					if (this.value != 3)
					{
						antValue = this.value;
						var pattern = new RegExp ("(^|\\s)chkval"+ antValue +"(\\s|$)");
						this.value ++;
						if (this.value > 9) this.value = 0;
						else if (this.className.indexOf ('chksino') > 0)
						{
							if (this.value == 2) this.value = 8;
							if (this.value > 8) this.value = 0;
						}
						else if (this.className.indexOf ('chkhersino') > 0)
						{
							if (this.value == 2) this.value = 8;
						}
						else if (this.className.indexOf ('chkher') > 0)
						{
							if (this.value == 3) this.value = 0;
						}
						else
						{
							if (this.value == 3) this.value = 8;
							if (this.value > 8) this.value = 0;
						}
						this.className = this.className.replace (pattern, ' chkval'+ this.value);
						js_chkImgsSubs (this);
					}
				}
			}
	
			if (subCategorias = $('enviarcategorias'))
			{
				subCategorias.onclick = function ()
				{
					var pattCat = /(^|\s)chkcat(\d+)(\s|$)/;
					if (inpCategorias = $('categorias'))
					{
						idsChks = new Array ();
						imgChks = getElementsByClass ('imgcheckbox', null, 'img');
						for (i = 0; i<imgChks.length; i++)
						{
							if (imgChks [i].value != 0 && imgChks [i].value != 3)
							{
								imgCatId = imgChks [i].className.match (pattCat);
								idsChks.push (imgCatId [2] +'|'+ imgChks [i].value);
							}
						}
						if (idsChks.length > 0)
						{
							inpCategorias.value = idsChks.join (',');
						}
					}
					else if (gCategorias = $('gcategorias'))
					{
						idsChks = new Array ();
						imgChks = getElementsByClass ('imgcheckbox', null, 'img');
						for (i = 0; i<imgChks.length; i++)
						{
							if (imgChks [i].value != 0 && imgChks [i].value != 3)
							{
								imgCatId = imgChks [i].className.match (pattCat);
								idsChks.push (imgCatId [2] +'|'+ imgChks [i].alt +'|'+ imgChks [i].value);
							}
						}
						if (idsChks.length > 0)
						{
							gCategorias.value = idsChks.join (',');
						}
					}
				}
			}
			
			if (subCategorias = $('enviareventos'))
			{
				subCategorias.onclick = function ()
				{
					var pattCat = /(^|\s)chkcat(\d+)(\s|$)/;
					if (inpCategorias = $('cateventos'))
					{
						idsChks = new Array ();
						imgChks = getElementsByClass ('imgcheckbox', null, 'img');
						for (i = 0; i<imgChks.length; i++)
						{
							if (imgChks [i].value != 0 && imgChks [i].value != 3)
							{
								imgCatId = imgChks [i].className.match (pattCat);
								idsChks.push (imgCatId [2] +'|'+ imgChks [i].value);
							}
						}
						if (idsChks.length > 0)
						{
							inpCategorias.value = idsChks.join (',');
						}
					}
					else if (gCategorias = $('gcategorias'))
					{
						idsChks = new Array ();
						imgChks = getElementsByClass ('imgcheckbox', null, 'img');
						for (i = 0; i<imgChks.length; i++)
						{
							if (imgChks [i].value != 0 && imgChks [i].value != 3)
							{
								imgCatId = imgChks [i].className.match (pattCat);
								if (objValcar = $('valcar'+ imgChks [i].alt + imgCatId [2]))
								{
									valopc = objValcar.options [objValcar.selectedIndex].value;
								}
								else valopc = -1;
								idsChks.push (imgCatId [2] +'|'+ imgChks [i].alt +'|'+ imgChks [i].value + (valopc >= 0 ? '|'+ valopc : ''));
							}
						}
						if (idsChks.length > 0)
						{
							gCategorias.value = idsChks.join (',');
						}
					}
				}
			}
			
			if (subPermisos = $('enviarpermisos'))
			{
				subPermisos.onclick = function ()
				{
					var pattCat = /(^|\s)chkcat(\d+)(\s|$)/;
					if (gpermisos = $('gpermisos'))
					{
						idsChks = new Array ();
						imgChks = getElementsByClass ('imgcheckbox', null, 'img');
						for (i = 0; i<imgChks.length; i++)
						{
							if (imgChks [i].value != 9)
							{
								imgCatId = imgChks [i].className.match (pattCat);
								idsChks.push (imgChks [i].alt +'|'+ imgChks [i].value);
							}
						}
						if (idsChks.length > 0)
						{
							gpermisos.value = idsChks.join (',');
						}
					}
				}
			}
		}
	}
}

function js_chkImgsSubs (imgChk)
{
	cerca = false;
	subDiv = false;
	for (i=0; i<imgChk.parentNode.childNodes.length; i++)
	{
		if (imgChk.parentNode.childNodes [i] == imgChk)
		{
			cerca = true;
		}
		if (cerca && (imgChk.parentNode.childNodes [i].tagName == 'DIV'))
		{
			cerca = false;
			subDiv = imgChk.parentNode.childNodes [i];
		}
	}
	
	if (subDiv)
	{
		subChks = getElementsByClass ('imgcheckbox', subDiv, 'img');
		if (subChks.length > 0)
		{
			for (i=0; i<subChks.length; i++)
			{
				if (subChks [i].value != 1)
				{
					var pattern = new RegExp ("(^|\\s)chkval\\d(\\s|$)");
					subChks [i].value = (imgChk.value == 2 ? 3 : '0');
					subChks [i].className = subChks [i].className.replace (pattern, ' chkval'+ (imgChk.value == 2 ? 3 : '0'));
				}
			}
		}
	}
}

function js_chkMarcarTodos ()
{
	subEnls = getElementsByClass ('marcarTodos', $('cuerpo'), 'a');
	if (subEnls.length > 0)
	{
		for (i=0; i<subEnls.length; i++)
		{
			subEnls [i].onclick = function ()
			{
				inputs = this.parentNode.parentNode.getElementsByTagName ('INPUT');
				for (i=0; i<inputs.length; i++) if (inputs [i].type == 'checkbox') inputs [i].checked = true;
				return false;
			}
		}
		
		subEnls = getElementsByClass ('desmarcarTodos', $('cuerpo'), 'a');
		if (subEnls.length > 0)
		{
			for (i=0; i<subEnls.length; i++)
			{
				subEnls [i].onclick = function ()
				{
					inputs = this.parentNode.parentNode.getElementsByTagName ('INPUT');
					for (i=0; i<inputs.length; i++) if (inputs [i].type == 'checkbox') inputs [i].checked = false;
					return false;
				}
			}
		}
	}
}

var booChkImgs = true;


window.onload = js_inicializar;
