﻿var _Browser = "";
var _Errors = 0;

function MOZEventHandlers(events)
{
	var i;

	for (i = 0; i < events.length; i++)
	{	
		document.addEventListener(events[i], function (e) { window.event = e;	}, true);
	}
}

if (_Browser == "MOZILLA" || _Browser == "FIREFOX")
{
	MOZEventHandlers(["click","mousedown","mouseup","mouseover","mouseout","mousemove","keyup","keydown"]);
}

function WindowError(msg, url, line)
{
	if (_Errors < 5)
	{
		try
		{
			msg = msg.replace(/,/gi,";");
			url = url.replace(/,/gi,";");

			if (msg.indexOf("Access denied") < 0 && msg.indexOf("Acesso negado") < 0)
				AjaxGet("Ajax.aspx?type=Erro&method=Javascript&args=" + strMensagem + "," + strUrl + "," + intLinha);
		}
		catch (e)
		{}
	}

	_Errors++;
}

function GetCookie(cookieName)
{
	var cookieIni;
	var cookieEnd;
	var cookieLength;

	try
	{
		cookieIni = document.cookie.indexOf(cookieName + "=");
		cookieLength = cookieIni + cookieName.length + 1;
		cookieEnd = document.cookie.indexOf(";", cookieLength);

		if ((!cookieIni) && (cookieName != document.cookie.substring(0, cookieName.length)))
			return null;
		if (cookieIni == -1)
			return null;
		if (cookieEnd == -1)
			cookieEnd = document.cookie.length;

		return unescape(document.cookie.substring(cookieLength, cookieFim));
	}
	catch (e)
	{
		return "";
	}
}

function SetCookie(cookieName, cookieValue, cookieNoExpire)
{
	try
	{
		document.cookie = cookieName + "=" + escape(cookieValue) + ((cookieNoExpire) ? ";cookieExpires=" + (new Date().getYear() + 2) + "-01-01 01:01:01" : "");
	}
	catch (e)
	{
		alert("Não foi possível gravar o cookie " + cookieName + ".\n\nVerifique as configurações do seu browser.");
	}
}

function ShowHide(obj, show)
{
	if (show)
	{
		obj.style.display = "";
		obj.style.visibility = "visible";
	}
	else
	{
		obj.style.display = "none";
		obj.style.visibility = "hidden";
	}
}

function ImgHover(obj, hover)
{
	var src = obj.getAttribute("src");

	if (hover)
	{
		src = src.replace(".png","HV.png");
		src = src.replace(".jpg","HV.jpg");
		src = src.replace(".gif","HV.gif");
	}
	else
	{
		src = src.replace("HV.png",".png");
		src = src.replace("HV.jpg",".jpg");
		src = src.replace("HV.gif",".gif");
	}

	obj.setAttribute("src", src);
}

function ClassHover(obj, hover)
{
	var strClassName = obj.className;

	if (strClassName.indexOf("ON") <= 0 && strClassName.indexOf("OFF") <= 0)
	{
		if (hover)
			obj.className = strClassName + "HV";
		else
			obj.className = strClassName.replace("HV","");
	}
}

function GetPositionLeft(obj)
{
	var left = obj.offsetLeft;
	while(obj.offsetParent != null)
	{
		obj = obj.offsetParent;
		left += obj.offsetLeft;
	}

	return left;
}

function GetPositionTop(obj)
{
	var top = obj.offsetTop;
	while(obj.offsetParent != null)
	{
		obj = obj.offsetParent;
		top += obj.offsetTop;
	}

	return top;
}

function Refresh(timeout, url)
{
	if (!timeout || timeout < 1)
		timeout = 0;
	if (!url)
		url = document.location.href;

	window.setTimeout("document.location.href = '" + url + "';", timeout);
}

function Bookmark(url, title)
{
	if (window.external)
	{
		if (!url)
			url = document.location.href;

		if (!title)
			title = document.title;

		window.external.AddFavorite(url,title);
	}
}

function FormFocusClearValue(obj)
{
	obj.value = "";
	obj.onfocus = null;
}

function FormAutoSelectValue(obj)
{
	obj.focus();
	obj.select();
}

function DisableButton(obj)
{
	obj.disabled = true;
}