var old_load = null;

function Align(dgId, dgRow, dgCell, tbId, tbRow, tbCell)
{
	var dg = document.getElementById(dgId);
	var tb = document.getElementById(tbId);
	
	if ((dg == null) || (tb == null))
	{
		return false;
	}
	
	tb.rows[tbRow].cells[tbCell].align = "right";
	tb.rows[tbRow].cells[tbCell-1].width = tb.offsetWidth;
}

function validateEnter(objID)
{
	var key = window.event.keyCode;
	if (key == 13)
	{
		var obj = document.getElementById(objID);
		if (obj != null)
		{
			obj.click();
			return false;
		}				
	}
	return true;
}

function ChangeVisibility(obj)
{
	if (!obj.className)
	{
		if (obj.style.display != "none")
		{
			obj.className = "visible";
		}
		else
		{
			obj.className = "hidden";
		}
	}


	if (obj.className.toLowerCase() != "hidden")
	{
		obj.className = "hidden";
		return "6";
	}
	else
	{
		obj.className = "visible";
		return "5";
	}
}


function setFocus(obj)
{
	window.focus();
	
	var o = document.getElementById(obj);
	if(o != null)
	{
		if (o.readOnly) return;
		if (o.disabled) return;
		if (o.style.visibility.toLowerCase() == "hidden") return;
		if (o.style.display.toLowerCase() == "none") return;
		
		try
		{
			o.focus();
		}
		catch (e)
		{}
	}
}

function Load()
{	
	if (document.getElementById("txtScroll") != null)
	{
		LoadScrollState();
	}
	
	if (typeof(old_load) == 'function')
	{
		old_load();
	}
	
	if (typeof(strLoad) != 'undefined')
	{
		eval(strLoad);
	}	
}

function autoTab()
{
	var src = event.srcElement;
	if (src.tagName.toLowerCase() == "input")
	{
		if (src.type.toLowerCase() == "text")
		{
			if (src.value.length < src.maxLength)
			{
				return true;
			}
			
			var key = event.keyCode;
			if ((key == 37) || (key == 39) || (key == 16) || (key == 8) || (key == 9) || (key == 46) || (key == 13)) return true;

			if (document.selection.createRange().text.length > 0)
			{
				return true;
			}
			else
			{
				var bNext = false;
				var frm = document.forms[0];
				
				for (var i = 0; i < frm.length; i++)
				{
					var obj = frm[i];
					if (obj.tagName.toLowerCase() == "input")
					{
						if (obj.type.toLowerCase() == "text")
						{
							if (bNext)
							{
								if ((!obj.readOnly) && (!obj.disabled))
								{
									obj.focus();
									return true;
								} 
							}
							else
							{
								if (obj == event.srcElement)
								{
									bNext = true;
								}
							}
						}
					}
				}
			}
		}
	}
	
	return true;
}

function selectAll(bValue, sParent)
{
	var oParent = document.forms[0][sParent];
	if (typeof(oParent) != 'undefined')
	{
		if (typeof(oParent.length) != 'undefined')
		{
			for (var i = 0; i < oParent.length; i++)
			{
				var chk = oParent[i];
				chk.checked = bValue;		
			}	
		}
		else
		{
			oParent.checked = bValue;
		}
	}	
}

function myConfirm(Message)
{
	if (confirm(Message))
	{
		event.srcElement.click();
	}
}

function SaveScrollState(objID)
{
	var src = document.body;
	if (objID != "_document_body")
	{
		src = document.getElementById(objID);
		if (src == null)
		{
			return;
		}
	}
	
	if (src.scrollLeft > 0)
	{
		setScrollVar(src, "scrollLeft", src.scrollLeft);
	}
	
	if (src.scrollTop > 0)
	{
		setScrollVar(src, "scrollTop", src.scrollTop);
	}
	
	return true;
}

function ScrollVar(id, tipo, value)
{
	this.Tipo = tipo;
	this.Value = value;

	if (id == null)
	{
		this.Object = document.body;
		this.toString = new Function("return '_document_body:' + this.Tipo + ':' + this.Value + ';';");
	}
	else
	{
		if ((typeof(id) != "undefined") && (id != ""))
		{
			this.Object = document.getElementById(id);
			this.toString = new Function("return this.Object.id + ':' + this.Tipo + ':' + this.Value + ';';");
		}	
		else
		{
			this.Object = null;
			this.toString = new Function("return '';");
		}		
	}
}

function LoadScrollState()
{
	var obj = document.body;
	var objID = "_document_body";
	if (arguments.length > 0)
	{
		obj = arguments[0];		
		objID = obj.id;
	}
	
	if ((obj.scrollHeight > 0) || (obj.scrollWidth > 0))
	{
		var scr = getScrollVar(objID, "scrollLeft");
		if (scr != null)
		{
			obj.scrollLeft = scr.Value;
		}
		
		scr = getScrollVar(objID, "scrollTop");
		if (scr != null)
		{
			obj.scrollTop = scr.Value;
		}

		if (objID != null)
		{
			if (objID != "")
			{
				obj.onscroll = new Function("SaveScrollState('" + objID + "');");
			}
		}
	}	
	
	if (obj.hasChildNodes)
	{
		for (var i = 0; i < obj.children.length; i++)
		{
			LoadScrollState(obj.children[i]);
		}
	}
}

//OK
function setScrollVar(obj, tipo, value)
{
	var scr = null;
	var objId = "";
	if (obj == document.body)
	{
		scr = new ScrollVar(null, tipo, value);
		objId = "_document_body";
	}
	else
	{
		scr = new ScrollVar(obj.id, tipo, value);
		objId = obj.id;
		
		if (scr == null)
		{
			return;
		}
	}

	var field = document.getElementById("txtScroll");
	if (field != null)
	{
		var ini = field.value;
		var fld = field.value;
		var i = fld.indexOf(objId + ":" + tipo + ":");
		if (i >= 0)
		{
			var fim = "";			
			ini = fld.substring(0, i);
			var j = fld.indexOf(";", i);
			if (j > i)
			{
				fim = fld.substring(j+1, fld.length-1);				
			}			
			field.value = ini + scr.toString() + fim;
		}
		else
		{
			field.value += scr.toString();
		}
	}
}

//OK
function getScrollVar(objID, tipo)
{
	var field = document.forms[0]["txtScroll"];
	if (field != null)
	{
		if (field.value == "")
		{
			return null;
		}

		if (objID == "") return null;
		if (objID == null) return null;
		
		var vars = field.value.split(";");
		for (var j = 0; j < vars.length - 1; j++)
		{
			var param = vars[j];
			var data = param.split(":");	
			if (data[0] == objID)
			{
				if (objID == "_document_body")
				{
					return new ScrollVar(null, data[1], data[2]);
				}
				else
				{
					return new ScrollVar(data[0], data[1], data[2]);
				}
			}			
		}		
	}
	
	return null;
}

function setCookieVar(Name, Value)
{
	document.cookie = Name + "=" + escape(Value) + ";" + document.cookie;
}

function getCookieVar(Name)
{
	var str = document.cookie;
	var cok = str.split(";");
		
	for (var i = 0; i < cok.length; i++)
	{
		var par = cok[i].split("=");
		if (par[0].toLowerCase().trim() == Name.toLowerCase().trim())
		{
			return unescape(par[1]);
		}
	}
	
	return "";
}

function getQueryStringVar(Name)
{
	var str = document.location.search;
	if (str == "")
	{
		return "";			
	}
	
	var i = str.toLowerCase().indexOf(Name.toLowerCase() + "=");
	if (i >= 0)
	{
		i = i + Name.length;
		var j = str.indexOf("&", i);
		if (j >= 0)
		{
			str = str.substring(i + 1, j - 1);
			var a = new String();
		}
		else
		{
			str = str.substring(i + 1, str.length);
		}
		
		return str;
	}
	
	return "";
}

function ShowHide()
{
	if (arguments.length == 1)
	{
		var objID = arguments[0];
		var objCursor = event.srcElement;
		var obj = document.getElementById(objID);

		if (!obj.className)
		{
			if (obj.style.display != "none")
			{
				obj.className = "visible";
			}
			else
			{
				obj.className = "hidden";
			}
		}

		if (obj.className.toLowerCase() == "hidden")
		{
			obj.className = "visible";
			objCursor.innerHTML = "&nbsp;-&nbsp;"
			objCursor.title = "Esconder Tabela";
		}
		else
		{
			obj.className = "hidden";
			objCursor.innerHTML = "&nbsp;+&nbsp;"
			objCursor.title = "Mostrar Tabela";
		}
	}
	else if (arguments.length >= 3)
	{
		var Source = arguments[0];
		var CollapseImage = arguments[1];
		var ExpandImage = arguments[2];
		var img = event.srcElement;

		if (Source.className.toLowerCase() == "visible")
		{
			Source.className = "hidden";
			img.src = ExpandImage;
		}
		else
		{
			Source.className = "visible";
			img.src = CollapseImage;
		}
	}
	else
	{
		return;
	}

}

function setAreaMaxLength(objID, max)
{
	var obj = document.getElementById(objID);
	if (obj != null)
	{
		obj.maxLength = max;

		//Processa os eventos de entrada de dados.		
		obj.onpaste = _verifyMaxLength;
		obj.onkeydown = _verifyMaxLength;
		obj.ondrop = _verifyMaxLength;
	}
}

function _verifyMaxLength()
{
	var obj = event.srcElement;
	switch (event.type)
	{
		case "keydown":
			if (!isControlKey(event.keyCode))
			{
				if (obj.value.length >= obj.maxLength)
				{
					return false;
				}
			}
						
			break;
			
		case "paste":
			var str = clipboardData.getData("Text");
			if (obj.value.length >= obj.maxLength)
			{
				return false;
			}
			
			if ((obj.value.length + str.length) > obj.maxLength)
			{
				var i = obj.maxLength - obj.value.length;
				str = str.substring(0, i);
			}

			clipboardData.setData("Text", str);			

			break;
			
		case "drop":
			var str = event.dataTransfer.getData("Text");
			if (obj.value.length >= obj.maxLength)
			{
				return false;
			}
			
			if ((obj.value.length + str.length) > obj.maxLength)
			{
				var i = obj.maxLength - obj.value.length;
				str = str.substring(0, i);
			}

			event.dataTransfer.setData("Text", str);			

			break;
			
		default:
			return false;
	}
	
	return true;
}

function isControlKey(key)
{
	if (event.altKey)
	{
		if ((key >= 96) && (key <= 105))
		{
			return false;
		}
		
		return true;
	}
	
	if (event.ctrlKey)
	{
		return true;
	}
	
	if ((key == 32) || (key == 13))
	{
		return false;
	}
	
	if ((key >= 48) && (key <= 57))
	{
		return false;
	}
	
	if ((key >= 65) && (key <= 90))
	{
		return false;
	}
	
	if ((key >= 96) && (key <= 107))
	{		
		return false;
	}
	
	if ((key >= 109) && (key <= 111))
	{
		return false;
	}
	
	if ((key >= 186) && (key <= 194))
	{
		return false;
	}
	
	if ((key >= 219) && (key <= 222))
	{
		return false;
	}
	
	if (key == 226)
	{
		return false;
	}
	
	return true;
}

function HTMLEncode(str)
{
	str = str.replace(/&/g, "&amp;");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	str = str.replace(/=/g, "&eq;");
	return str;
}

function HTMLDecode(str)
{
	str = str.replace(/&eq;/g, "=");
	str = str.replace(/&gt;/g, ">");
	str = str.replace(/&lt;/g, "<");
	str = str.replace(/&amp;/g, "&");
	return str;
}

function showHelp(url)
{
	window.open(url, "ajuda", "toolbar=no, scrollbars=yes, resizable=yes, menubar=no, location=no, channelmode=no, directories=no, status=yes");
}

function showHint(id, css, html, edge, left, top, align, valign)
{
	var obj = document.getElementById(id);
	if (obj != null)
	{
		if (parseInt(left) == NaN)
		{
			left = 0;
		}
		
		if (parseInt(top) == NaN)
		{
			top = 0;
		}
		
		align = validateEnum(align, "Left", "Right", "Center");		
		valign = validateEnum(valign, "Top", "Bottom", "Middle");
		edge = validateEnum(edge, "TopLeft", "BottomLeft", "TopRight", "BottomRight");
		
		document.hint = document.createElement("DIV");
		with (document)
		{
			body.appendChild(hint);
			hint.style.cssText = getCssText(css);

			hint.style.filter = "progid:DXImageTransform.Microsoft.Fade(Overlap=0)";

			hint.innerHTML = html;
			
			var x = getAbsolutePosition(obj, "Left");
			var y = getAbsolutePosition(obj, "Top");
			
			var w = parseInt(obj.offsetWidth);
			var h = parseInt(obj.offsetHeight);
			
			if (align == "right")
			{
				x += w;
			}
			else if (align == "center")
			{
				x += w / 2;
			}
			
			if (valign == "bottom")
			{
				y += h;
			}
			else if (align == "middle")
			{
				y += h / 2;
			}
			
			y += top;
			x += left;
			
			w = parseInt(hint.offsetWidth);
			h = parseInt(hint.offsetHeight);
			
			switch (edge)
			{
				case "topleft":
					hint.style.left = x;
					hint.style.top = y;
					break;
					
				case "topright":
					hint.style.left = x - w;
					hint.style.top = y;
					break;			

				case "bottomleft":
					hint.style.left = x;
					hint.style.top = y - h;
					break;			

				case "bottomright":
					hint.style.left = x - w;
					hint.style.top = y - h;
					break;			
			}
						
			hint.onmouseover = hint_mouseover;
			hint.onmouseout = hint_mouseout;		
			document.onmousedown = hint_lostfocus;

			if (typeof(hint.filters) != 'undefined')
			{
				if (hint.filters.length > 0)
				{
					hint.style.display = "none";
					hint.filters[0].apply();
					hint.style.display = "";
					hint.filters[0].play();
				}
			}
		}
	}
}

function hint_mouseover()
{
	document.onmousedown = null;
}

function hint_mouseout()
{
	document.onmousedown = hint_lostfocus;
}

function hint_lostfocus()
{
	document.onmousedown = null;
	document.body.removeChild(document.hint);
	return true;
}

function validateEnum(str, arg0)
{
	if (arguments.length == 2)
	{
		return arg0;
	}

	for (var i = 1; i < arguments.length; i++)
	{
		if (arguments[i].toLowerCase() == str.toLowerCase())
		{
			return str.toLowerCase();
		}
	}	
	
	return arg0.toLowerCase();
}

function getAbsolutePosition(obj, side)
{
	var sid = 0;
	var par = obj;
	
	while (par != document.body)
	{
		if (typeof(par["offset" + side]) != 'undefined')
		{
			sid += parseInt(par["offset" + side]);
		}
		
		par = par.offsetParent;
	}	
	return sid;
}

function getCssText(ruleName)
{
    with (window.document)
    {
		for (var inStyles = 0; inStyles < window.document.styleSheets.length; inStyles++)
		{
			for (var inRules = 0; inRules < styleSheets[inStyles].rules.length; inRules++)
			{
				var objRule = styleSheets[inStyles].rules[inRules];
				if (objRule.selectorText.replace(/^\./,"").toUpperCase() == ruleName.toUpperCase())
				{
					return objRule.style.cssText;
				}
			} 
		}
    }

    return "";
}

old_load = onload;
onload = Load;







function checaCPF (CPF)
 {
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
		return false;
	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10)))
		return false;
	return true;
 }

function ValidaCPF(sender, ctl, message)
{
	cpf = sender.value;
	if(!checaCPF(cpf) && cpf != "")
	{
		ctl.innerText = message;
		ctl.className = "Erro";
	}
	else
	{
		ctl.innerText = "";
		ctl.className = "hidden";
	}
}

//Função que só permite que o usuário digite valor númerico
function IsNumeric(e)
{
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;
		if (tecla > 47 && tecla < 58) // numeros de 0 a 9
			return true;
		else
			{
				if (tecla != 8) // backspace
					event.keyCode = 0;
					//return false;
				else
					return true;
			}
}

//Função que valida data
/*
function ValidaData(data)
{
	debugger;
	dia = (data.value.substring(0,2));
	mes = (data.value.substring(3,5));
	ano = (data.value.substring(6,10));
	
	retorno = true;
	
	if ((dia < 01 ) || (dia < 01 || dia > 30) &&
		((mes == 04 || mes == 06 ||
		  mes == 09 || mes == 11)||
		  dia > 31)
		  {
		  retorno  = false;
		  }
		  
	if (mes < 01 || mes > 12)
		{
		retorno = false;
		}
		
	if (mes == 2 && (dia < 01 || dia > 29 || 
		(dia > 28 && 
		parseInt( ano / 4 ) != ano / 4 ))))
		{
			retorno  = false;
		} 
	if (retorno == false)
		{
			alert("Data Inserida não é válida:" + data.value);
			data.value = "";
			data.focus();
			return false;
		}
	
}
*/

