
function stringCount(sSource, sFind)
{
	var b = false;
	var iSourceLength = 0;
	var iFindLength = 0;
	var iReturn = 0;
	
	try {
		iSourceLength = sSource.length;
		iFindLength = sFind.length;
	} catch (e) {
	} finally {}

	for (var i=0; i<iSourceLength; i+=iFindLength) 
	{
		if (iSourceLength > 0 && iFindLength > 0 && iFindLength <= iSourceLength )
		{
			b = (sSource.substr(i, iFindLength) == sFind);

			if (b == true) {
				iReturn++;
			}
		}
	}
	

	
	return iReturn;
}


function startsWith(sSource, sFind)
{
	var b = false;
	var iSourceLength = 0;
	var iFindLength = 0;
	
	try {
		iSourceLength = sSource.length;
		iFindLength = sFind.length;
	} catch (e) {
	} finally {}

	if (iSourceLength > 0 && iFindLength > 0 && iFindLength <= iSourceLength )
	{
		b = (sSource.substr(0, iFindLength) == sFind);
	}
	
	return b;
}

function endsWith(sSource2, sFind2)
{
	var b = false
	var sSource = "" + sSource2
	var sFind = "" + sFind2



	if (sFind.length == 0) {
		b = true
	} else {

	if (sSource.length > 0 && sFind.length > 0) {
		b = (sSource.substr(sSource.length - sFind.length, sFind.length) == sFind)		
	}
	}

	return b
}

function formatDate(sDay, sMonth, sYear)
{
	var iYear = "0000" + Number(sYear);
	var iMonth = "00" + Number(sMonth);
	var iDay = "00" + Number(sDay);
	return iYear.substr(iYear.length-4,4) + iMonth.substr(iMonth.length-2,2) + iDay.substr(iDay.length-2,2);
}

function dateToJulianDay(sDay, sMonth, sYear)
{
	var iYear = Number(sYear);
	var iMonth = Number(sMonth);
	var iDay = Number(sDay);
	
	var a = Math.floor((14 - iMonth) / 12);	
	var y = iYear + 4800 - a;
	var m = iMonth + 12 * a - 3;

	return iDay + Math.floor((153 * m + 2) / 5) + 365 * y + Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400) - 32045;
}
	
function formatJulianDay(sJulian)
{
	var iJulian = Number(sJulian);	
	
	var a = iJulian + 32044;
	var b = Math.floor((4 * a + 3) / 146097);
	var c = a - Math.floor((b * 146097) / 4);
	
	var d = Math.floor((4 * c + 3) / 1461);
    var e = c - Math.floor((1461 * d) / 4);
    var m = Math.floor((5 * e + 2) / 153);

    var day = e - Math.floor((153 * m + 2) / 5) + 1;
    var month = m + 3 - 12 * Math.floor(m / 10);
    var year = b * 100 + d - 4800 + Math.floor(m / 10);
    
    return day + "-" + month + "-" + year;
}


function setFormDate(sField, sDate)
{
	//alert(sDate);
	var obj
	
	obj = document.getElementById(sField + "_dia")
	obj.value = Number(sDate.substr(8, 2))
	
	obj = document.getElementById(sField + "_mes")
	obj.value = Number(sDate.substr(5, 2))
	
	obj = document.getElementById(sField + "_ano")
	obj.value = Number(sDate.substr(0, 4))	
}

function setFocus()
{
	var aTextBoxes = document.body.getElementsByTagName("INPUT");
	for (var k=0; k < aTextBoxes.length; k++) {
		if (aTextBoxes[k].getAttribute("type") == "text") {
			aTextBoxes[k].focus();
			break;
		}	
	}
}

function processTree(groupid, prefixo)
{
	try {
		if (prefixo == null) prefixo = "";
		var aDivs = document.body.getElementsByTagName("TR");
		var sGroup;
		var i;
		var bAction = false;
		var iLevel = 0;
		//alert(stringCount(groupid, "."));
		var iTopLevel = stringCount(groupid, "."); 
		
		var oImg;
		//var oImg = document.getElementById(imagemid);
		//bAction = endsWith(oImg.src, "seta_direita.gif");
		
		var aImgs = document.body.getElementsByTagName("IMG");
		for (i=0; i<aImgs.length; i++) {
			sGroup = aImgs[i].getAttribute("group");
			if (sGroup != null && sGroup != "") {
				if (startsWith(sGroup, groupid + ".")) {
				    if (endsWith(aImgs[i].src, "seta_baixo.gif")) {				    
						aImgs[i].src = "/DesktopModules/PC_Graficos/images/" + prefixo + "seta_direita.gif";
					}
				} else {
					if (sGroup == groupid) {
						oImg = aImgs[i];
						bAction = endsWith(oImg.src, "seta_direita.gif");
						//alert("B:" + bAction);
					}
				}
			}
		}
						

		
		//alert("B:" + bAction);
		
		if (!bAction) {
			oImg.src = "/DesktopModules/PC_Graficos/images/" + prefixo + "seta_direita.gif";
		} else {
			oImg.src = "/DesktopModules/PC_Graficos/images/" + prefixo + "seta_baixo.gif";
		}
				
		for (i=0; i<aDivs.length; i++) {
			sGroup = aDivs[i].getAttribute("group");
			if (sGroup != null && sGroup != "") {
				iLevel = stringCount(sGroup, "."); 

				if (startsWith(sGroup, groupid + ".")) {
				
					if (bAction) {
						//alert("level:" + iLevel + " top:" + iTopLevel);
						if (iLevel == iTopLevel+1) {
							aDivs[i].style.display = "block";
						}
					} else {
						if (iLevel > iTopLevel) {
							aDivs[i].style.display = "none";
						}
					}
					
				}
			}
		}
	}catch (e) {
	}
	
	window.status = '';
	return false;
}


function PrinterFriendly(s)
{
	var theform;
	if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
		theform = document.forms["Form1"];
	}
	else {
		theform = document.Form1;
	}
	theform.target = '_blank'; 
	theform['printer_friendly'].value = s; 
	theform.submit();
}

function selectListBoxOptions(listbox, tfalse)
{
	var n = listbox.options.length;
	for (var i=0; i<n; i++) {
		listbox.options[i].selected = tfalse;
	}
}

function selectListBoxOptionsAndPost(listbox, tfalse)
{
	var n = listbox.options.length;
	for (var i=0; i<n; i++) {
		listbox.options[i].selected = tfalse;
	}
	__doPostBack(' + listbox.id + ','')
}
