/************************************************************************ 
Author: Eric Simmons
Contact: info@jswitch.com
Website: http://www.jswitch.com
Version: 1.0 4/2005       
Browser Target: Mozilla 6+/FireFox Netscape 6+, IE 5.0+
Type : XP style sliding dropdown menus (aka Switch Menu II on Dynamicdrive.com)
Note: Modification by Dynamicdrive.com to dynamically determine sub menus widths

DISCLAIMER:
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT
ANY EXPRESS OR IMPLIED WARRANTIES, JSWITCH.COM
IS NOT RESPONSIBLE FOR ANY ADVERSE AFFECTS TO
YOUR COMPUTER OR SYSTEMS RUNNING THIS SCRIPT.

LICENSE:
YOU ARE GRANTED THE RIGHT TO USE THE SCRIPT
PERSONALLY OR COMMERCIALLY. THE AUTHOR, WEBSITE LINKS 
AND LICENSE INFORMATION IN THE HEADER OF THIS SCRIPT
MUST NOT BE MODIFIED OR REMOVED. 

v 1.0
XP style sliding Menu Bar
***********************************************************************/

var TIMER_SLIDE = null;
var OBJ_SLIDE;
var OBJ_VIEW;
var PIX_SLIDE = 10; //this is the amount of slide/DELAY_SLIDE
var NEW_PIX_VAL;
var DELAY_SLIDE = 30; //this is the time between each call to slide
var DIV_HEIGHT = 22; //value irrelevant
var SUB_MENU_NUM =0;
var RE_INIT_OBJ = null;
var bMenu = document.getElementById("curMenu");
var MainDiv,SubDiv

//DD added code
document.write('<div id="tempcontainer" class="mainDiv" style="visibility: hidden; position: absolute"></div>')

function Init(objDiv)
{
    if (TIMER_SLIDE == null)
    {
        SUB_MENU_NUM = 0;
        MainDiv = objDiv.parentNode;
        SubDiv =  MainDiv.getElementsByTagName("DIV").item(0);
        SubDiv.onclick = SetSlide;
        
        OBJ_SLIDE = MainDiv.getElementsByTagName("DIV").item(1)
        OBJ_VIEW = OBJ_SLIDE.getElementsByTagName("DIV").item(0);

				document.getElementById("tempcontainer").innerHTML=MainDiv.getElementsByTagName("DIV").item(2).innerHTML //DD added code
				DIV_HEIGHT=document.getElementById("tempcontainer").offsetHeight //DD added code
        
        for (i=0;i<OBJ_VIEW.childNodes.length;i++)
        {
            if (OBJ_VIEW.childNodes.item(i).tagName == "SPAN")
            {
                SUB_MENU_NUM ++;
                OBJ_VIEW.childNodes.item(i).onmouseover= ChangeStyle;
                OBJ_VIEW.childNodes.item(i).onmouseout= ChangeStyle;
            }
        }   
        
              NEW_PIX_VAL = parseInt(MainDiv.getAttribute("state")); 
    }

}
function SetSlide()
{   
			if (window.TIMER_SLIDE) clearInterval(TIMER_SLIDE) //DD added code
      if (TIMER_SLIDE == null && this.parentNode == MainDiv)
            TIMER_SLIDE = setInterval('RunSlide()', DELAY_SLIDE);
      else
      {
          RE_INIT_OBJ = this;
          setTimeout('ReInit()', 200);
      }
}

function ReInit(obj)
{
    Init(RE_INIT_OBJ);
    TIMER_SLIDE = setInterval('RunSlide()', DELAY_SLIDE);
    RE_INIT_OBJ = null;
}

function RunSlide()
{

    if (OBJ_VIEW.getAttribute("state") == 0)
    {

        NEW_PIX_VAL += PIX_SLIDE;
        OBJ_SLIDE.style.height = NEW_PIX_VAL;

        if (NEW_PIX_VAL >= DIV_HEIGHT) //DD modified code
        {
            clearInterval(TIMER_SLIDE);
            TIMER_SLIDE = null;
            OBJ_VIEW.style.display = 'inline';
            OBJ_VIEW.setAttribute("state","1")
            MainDiv.setAttribute("state",NEW_PIX_VAL);
        }
    } else
    {
        OBJ_VIEW.style.display = 'none';
        NEW_PIX_VAL -= PIX_SLIDE;
        if(NEW_PIX_VAL > 0)OBJ_SLIDE.style.height = NEW_PIX_VAL;
        if (NEW_PIX_VAL <= 0)
        {
            NEW_PIX_VAL = 0;
            OBJ_SLIDE.style.height = NEW_PIX_VAL
            clearInterval(TIMER_SLIDE);
            TIMER_SLIDE = null;
            OBJ_VIEW.setAttribute("state","0")
            MainDiv.setAttribute("state",NEW_PIX_VAL);
        }
    }
}

function ChangeStyle()
{
    if (this.className == this.getAttribute("classOut"))
        this.className = this.getAttribute("classOver");
    else
        this.className = this.getAttribute("classOut");
}

/***************************************************************************************************************************
****************************************************************************************************************************
****************************************************************************************************************************
***************************************************************************************************************************/

function VerificaData(objeto,iCount,NomObj)
{
	var sData = "";

	var campo;
	with(document.Form1)
	{
		sData = objeto;
		campo = eval(NomObj);
		
		if (IsDate(sData) == false && sData != "")
		{
			alert("Data inválida.");
			campo.value = "";
			campo.focus();
			return;
		}
	}
}
function Mascara(formato, keypress, objeto)
{
	var Result = "";
	var abre = "";
	var fecha = "";
	campo = eval (objeto);
			
	//campo = eval (objeto);
	// CEP
	if (formato=='CEP')
	{
		separador = '-'; 
		conjunto1 = 5;
		if (campo.value.length == conjunto1)
		{
			campo.value = campo.value + separador;
		}
	}

	// DATA
	if (formato=='DATA')
	{
		separador = '/'; 
		conjunto1 = 2;
		conjunto2 = 5;
		if (campo.value.length == conjunto1)
		{
			campo.value = campo.value + separador;
		}
		if (campo.value.length == conjunto2)
		{
			campo.value = campo.value + separador;
		}
	}

	// TELEFONE
	if (formato=='TELEFONE')
	{
		separador = '-'; 
		abre = '(';
		fecha = ')';
		conjunto2 = 2;
		conjunto1 = 7;
		
		if (campo.value.length == conjunto2)
		{
			campo.value = abre + campo.value + fecha;
		}
		
		if (campo.value.length == conjunto1)
		{
			campo.value = campo.value + separador;
		}
		if (campo.value.length == 13 && campo.value.substring(8,9) != '-')
		{
			campo.value = campo.value.substring(0,7) + campo.value.substring(8,9) + '-' + campo.value.substring(9,13)
		}
	}

	// CPF
	if (formato=='CPF')
	{
		separador1 = '.'; 
		separador2 = '-'; 
		conjunto1 = 3;
		conjunto2 = 7;
		conjunto3 = 11;
		if (campo.value.length == conjunto1)
		{
			campo.value = campo.value + separador1;
		}
		if (campo.value.length == conjunto2)
		{
			campo.value = campo.value + separador1;
		}
		if (campo.value.length == conjunto3)
		{
			campo.value = campo.value + separador2;
		}
	}

	// CNPJ
	if (formato=='CNPJ')
	{
		separador1 = '.'; 
		separador2 = '/'; 
		separador3 = '-'; 
		conjunto1 = 2;
		conjunto2 = 6;
		conjunto3 = 10;
		conjunto4 = 15;
		if (campo.value.length == conjunto1)
		{
			campo.value = campo.value + separador1;
		}
		if (campo.value.length == conjunto2)
		{

			campo.value = campo.value + separador1;
		}
		if (campo.value.length == conjunto3)
		{
			campo.value = campo.value + separador2;
		}
		if (campo.value.length == conjunto4)
		{
			campo.value = campo.value + separador3;
		}
	}
}
function IsDate (Dado)
{
    var DiasMes = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
    var Dia, Mes, Ano;
    var Result = false;

	// Pré-analisa o String:
	if (Dado != "")
	{
		if ((Dado.length == 10) && (Dado.substr(2,1) == "/") && (Dado.substr(5,1) == "/"))
		{
			// Levanta Campos:
			if (Number (Dado.substr(0,2))) Dia = Dado.substr(0,2);
			if (Number (Dado.substr(3,2))) Mes = Dado.substr(3,2);
			if (Number (Dado.substr(6,4))) Ano = Dado.substr(6,4);

			// Analisa Ano e Mês:
			if ((Ano > 0) && (Mes >= 1) && (Mes <= 12))
			{
				// Analisa Dia:
				if ((Dia >= 1) && (Dia <= DiasMes[Mes - 1]))
				{
					// Analisa os casos não-bissextos:
					if ((Mes == 2) && (!AnoBissexto (Ano)))
					{
						if (Dia <= 28) Result = true;
					}
					else
					{
						Result = true;
					}
				}
			}
		}
	}		
	return Result;
}

function AnoBissexto (Ano)
{
	if (((Ano % 4)==0) && ((Ano % 100)!=0) || ((Ano % 400)==0))
		return true;
	else
		return false;
}
function VerificaData(objeto,iCount,NomObj)
{
	var sData = "";
	var campo;
	with(document.Form1)
	{
		sData = objeto;
		campo = eval(NomObj);
		
		if (IsDate(sData) == false && sData != "")
		{
			alert("Data invalida.");
			campo.value = "";
			campo.focus();
			return;
		}
	}
}

function Filtro(Objeto,tpCampo)
{
	//Determina o navegador e sua versão.
	var isMinNS6 = (navigator.appName.indexOf("Netscape") >= 0 &&
	                parseFloat(navigator.appVersion) >= 5) ? 1 : 0;
	var isMinIE4 = (document.all) ? 1 : 0;

	if (isMinIE4)
	{
		/* Code keys
		8 = backspace
		9 = tab
		16 = shift
		36 = home
		37 = left arrow
		39 = rigth arrow
		46 = delete
		*/
/*		if(window.event.keyCode == 37 ||
		window.event.keyCode == 8 ||
		window.event.keyCode == 36 ||
		window.event.keyCode == 39 ||
		window.event.keyCode == 46 ||
		window.event.keyCode == 16 ||
		window.event.keyCode == 9)
			return;*/
	}
	else if (! isMinNS6)
		return
		
	Objeto.value = PassaDominio(Objeto.value, "0123456789");
	if (tpCampo == "numero" || tpCampo == 0)
	{
		Objeto.value = FmtCurr(Objeto.value);
	}
	if (tpCampo == "valor" || tpCampo == 1)
	{
		Objeto.value = FmtValor(Objeto.value);
	}
}
function FmtCurr(Dado)
{
  var Result, i;
  
  Result = Dado;
  return Result;
}

function PassaDominio(StrDado, Dominio)
{
	var i, j, c;
	var Result;

	Result = "";
	for (i=0; i<StrDado.length; i++)
		{
		c = StrDado.substr(i,1);
		for (j=0; j<Dominio.length; j++)
			{
			if (c == Dominio.substr(j,1)) break;
			}
		if (j < Dominio.length)
			{
			Result = Result + c;
			}
		}
	return Result;
}
function FmtValor(Dado)
{
  var Result, i;
  
  if (Dado.length > 2)
  {
    Result = "," + Dado.substr(Dado.length-2, 2);
    for (i=5; i<=Dado.length; i+=3)
    {
      Result = Dado.substr(Dado.length-i, 3) + Result;
      if (Dado.length > i) Result = "." + Result;
    }
    Result = Dado.substr(0, Dado.length-i+3) + Result;
  }
  else
  {
   	 Result = Dado;
  }
  
	if (Result.indexOf("-") > -1)
	{
		if (Result.substring(1,2) == ".")
		{
			Result = Result.substring(0,1) + Result.substring(2,Result.length)
		}
	}  
  
  return Result;
}
