var bugRiddenCrashPronePieceOfJunk = (navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1);
var W3CDOM = (!bugRiddenCrashPronePieceOfJunk && document.getElementsByTagName && document.createElement);
var classAttr = false;

var iniArray = Array ();
iniArray.push ('js_menu');
iniArray.push ('js_forms');
iniArray.push ('js_pedido');

function js_inicializar()
{if (W3CDOM){
	for (var i=0; i<iniArray.length; i++)
	{
		eval (iniArray [i] +'();');
	}
}}

function js_menu ()
{
	// Ajustamos el menu
	var objMenuGeneral = $('menu');
	if (objMenuGeneral)
	{
		// Recorremos los LIs de menu general que no sean de clase 'separador'
		var oLis = objMenuGeneral.getElementsByTagName ('li');
		
		$A(oLis).each (function (oLi)
		{
			if (oLi.lastChild.className && oLi.lastChild.className.indexOf ('menusub') >= 0)
			{
				oLi.lastChild.style.display = 'none';
				oLi.esactivo = false;
			}
			
			if (oLi.firstChild.tagName == 'A')
			{
				oLi.firstChild.esactivo = false;
				oLi.firstChild.onclick = function ()
				{
					if (this.nextSibling && this.nextSibling.className && this.nextSibling.className.indexOf ('menusub') >= 0)
					{
						if (this.esactivo)
						{
							this.nextSibling.style.display = 'none';
							this.style.color = '#ccc';
							this.esactivo = false;
						}
						else
						{
							this.nextSibling.style.display = 'block';
							this.style.color = '#fff';
							this.esactivo = true;
						}
					}
				}
				oLi.firstChild.onmouseover = function () { this.style.color = '#fff'; }
				oLi.firstChild.onmouseout = function () { if (!this.parentNode.esactivo) this.style.color = '#ccc'; }
			}
		});
	}
}

function js_forms ()
{
	var oInputs = document.getElementsByTagName ('input');
	var oSelects = document.getElementsByTagName ('select');
	var oTextAreas = document.getElementsByTagName ('textarea');
	var oControles = Array ();
	if (oInputs)
	{
		oControles = oControles.concat ($A(oInputs));
		for (i=0; i<oInputs.length; i++)
		{
			if (oInputs [i].type == 'submit')
			{
				oInputs [i].onclick = function ()
				{
					this.disabled = true;
					if (this.value.length > 3) this.value = 'Espere...';
					else this.value = '...';
					if (this.name == 'copiar')
					{
						var hCopiar = document.createElement ('input');
						hCopiar.name = '_copiar';
						hCopiar.value = '1';
						hCopiar.type = 'hidden';
						hCopiar.className = 'inputhidden';
						this.parentNode.appendChild (hCopiar);
					}
					this.form.submit ();
				}
			}
		}
		js_inputesnumero ();
		js_inputeseuro ()
	}
	if (oSelects) oControles = oControles.concat ($A(oSelects));
	if (oTextAreas) oControles = oControles.concat ($A(oTextAreas));
	oControles.each (function (oCtrl)
	{
		if (oCtrl.type != 'submit' && !oCtrl.readonly)
		{
			oCtrl.style.background = '#eee';
			oCtrl.onfocus = function () { this.style.background = '#fff'; this.style.borderColor = '#000'; }
			oCtrl.onblur = function () { this.style.background = '#eee'; this.style.borderColor = '#ccc'; }
		}
	});
	
	js_inputesnumero ();
	js_inputeseuro ();
}

function js_inputesnumero ()
{
	if (objs = document.getElementsByClassName ('inputesnumero'))
	{
		for (i=0; i<objs.length; i++)
		{
			objs [i].onkeypress = function (e)
			{
				if (!e) var e = window.event;
				if (e.keyCode) code = e.keyCode;
				else if (e.which) code = e.which;

				if (code > 31 && (code < 48 || code > 57)
					&& code != 35 && code !=36
					&& code != 37 && code != 39
					) return false;
				else return true;
			}
		}
	}
}

function js_inputeseuro ()
{
	oEuros = document.getElementsByClassName ('inputeseuro');
	if (oEuros)
	{
		oEuros.each (function (oEuro) {
			oEuro.onblur = function () {
				this.value = parseInt (this.value.replace (/\,/g, '.'));
			}
			oEuro.onkeypress = function (e)
			{
				if (!e) var e = window.event;
				if (e.keyCode) code = e.keyCode;
				else if (e.which) code = e.which;

				if (code > 31 && (code < 48 || code > 57)
					&& code != 35 && code !=36
					&& code != 37 && code != 39
					&& code != 46) return false;
				else return true;
			}
		});
	}
}

function js_inputeseuroDecimal (valor)
{
	if (valor.length == 0) valor = '0000';
	else if (valor.length < 4)
	{
		for (var i = 0; i<(4-valor.length); i++) valor = '0'+valor;
	}
	return parseInt (valor.substr (0, valor.length-2)) +'.'+ valor.substr (valor.length-2, 2);
}

function js_formArticulo ()
{
	var rbFamilia = document.getElementsByClassName ('inputdatfam');
	if (rbFamilia)
	{
		$A(rbFamilia).each (function (oRb) {
			oRb.onclick = function ()
			{
				var fsFamilias = document.getElementsByClassName ('fsetfamilia');
				if (this.checked && fsFamilias)
				{
					$A(fsFamilias).each (function (oFs) { oFs.style.display = 'none'; });
					$('fam_'+ this.value).style.display = 'block';
				}
			}
			oRb.onclick ();
		});
	}
}

function js_pedido ()
{
	oArts = document.getElementsByClassName ('pedidointro');
	if (oArts)
	{
		$A(oArts).each (function (oArt) {
			oArt.onclick = function ()
			{
				var sArts = readCookie ('pedido');
				if (sArts) var aArts = sArts.split (',');
				else var aArts = Array ();
				sDatos = oArt.title.substr (1, oArt.title.length-2).split ('|');
				aArts.push (sDatos [0]);

				createCookie ('pedido', aArts.join (','));
			}
		});
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function js_pedidoForm ()
{
	$('formpedido').cambios = false;
	var oChks = document.getElementsByClassName ('artconfirmar');
	if (oChks)
	{
		var iData = readCookie ('pedido');
		if (iData) iData = iData.split (',');
		else iData = Array ();
		$A(oChks).each (function (oChk)
		{
			pos = iData.indexOf (oChk.value);
			if (pos >= 0)
			{
				oChk.checked = true;
				iData [pos] = null;
				
			
				oChk.onclick = function ()
				{
					if (this.checked)
					{
						Element.removeClassName (this.parentNode.parentNode, 'nosel');
						Element.addClassName (this.parentNode.parentNode, 'sel');
						$('thtotal').innerHTML = (parseFloat ($('thtotal').innerHTML) + parseFloat (this.parentNode.parentNode.lastChild.innerHTML)).toFixed (2) +'&euro;';
						
						$('pedn').innerHTML = parseInt ($('pedn').innerHTML)+1;
						$('pede').innerHTML = ($('thtotal').innerHTML);
					}
					else
					{
						Element.removeClassName (this.parentNode.parentNode, 'sel');
						Element.addClassName (this.parentNode.parentNode, 'nosel');
						$('thtotal').innerHTML = (parseFloat ($('thtotal').innerHTML) - parseFloat (this.parentNode.parentNode.lastChild.innerHTML)).toFixed (2) +'&euro;';
					}
					$('formpedido').cambios = true;
				}
			}
			else
			{
				oChk.checked = false;
				Element.addClassName (oChk.parentNode.parentNode, 'nosel');
				$('thtotal').innerHTML = (parseFloat ($('thtotal').innerHTML) - parseFloat (oChk.parentNode.parentNode.lastChild.innerHTML)).toFixed (2) +'&euro;';
				oChk.parentNode.parentNode.parentNode.removeChild (oChk.parentNode.parentNode);
			}
		});
	}

	window.onunload = function ()
	{
		
		if ($('formpedido').cambios)
		{
			if (confirm ('Ha modificado el listado de productos sin enviar el formulario. ¿Desea que aquellos que no han sido marcados sean retirados del pedido?'))
			{
				var oChks = document.getElementsByClassName ('artconfirmar');
				if (oChks)
				{
					var sTotal = 0;
					var aArts = Array ();
					for (var i=0; i<oChks.length; i++)
					{
						if (oChks [i].checked)
						{
							sTotal += parseFloat (oChks [i].title);
							aArts.push (oChks [i].value);
						}
					}
					createCookie ('pedido', aArts.join (','));
					createCookie ('pedidoeuro', sTotal);
				}
			}
		}
	}
}

window.onload = js_inicializar;