﻿function SDMenu(id) {
	if (!document.getElementById || !document.getElementsByTagName)
		return false;
	this.menu = document.getElementById(id);
	this.submenus = this.menu.getElementsByTagName("div");
	this.remember = true;
	this.speed = 7;
	this.markCurrent = true;
	this.oneSmOnly = true;	
}

SDMenu.prototype.init = function() {    
	var mainInstance = this;    
	for (var i = 0; i < this.submenus.length; i++) {
	    if (this.submenus[i] != null) {	    
	        var actuator = this.submenus[i].getElementsByTagName("span")[0];
	        actuator.onclick = function() 
	        {
    	        mainInstance.toggleMenu(this.parentNode);
    	        //Pegando a página inicial a qual deverá ser rerirecionada no click no menu.
    	        var links = this.parentNode.getElementsByTagName("a");
    	        var redirecionar = false;
	            var pagina;
            	
	            if (links[0].id.search("MenuEstudante") != -1)
	            {
	                redirecionar = true;
	                pagina = "http://www.ciee-pe.org.br/informativo/Estudantes.aspx";
                }
                else
                {
                    if (links[0].id.search("MenuUCE") != -1)
	                {
	                    redirecionar = true;
	                    pagina = "http://www.ciee-pe.org.br/informativo/Empresas.aspx";
                    }
                    else
                    {
                        if (links[0].id.search("MenuIE") != -1)
	                    {
	                        redirecionar = true;
	                        pagina = "http://www.ciee-pe.org.br/informativo/Ies.aspx";
                        }
                        else
                        {
                            if (links[0].id.search("MenuInstitucioanl") != -1)
	                        {
	                            redirecionar = true;
	                            pagina = "http://www.ciee-pe.org.br/informativo/Institucional.aspx";
                            }
                        }
                    }
                }
                
                //retirar a linha abaixo após os teste
                redirecionar = false;
                if (redirecionar)
        	    {	    
            	    actuator.timer = setTimeout(function() {
        	            window.location = pagina;
        	            clearTimeout(actuator.timer);
        	        }, 500);
                }
		    };
		}
	}
	if (this.markCurrent) {
		var links = this.menu.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++)
			if (links[i].href == document.location.href) {
				links[i].className = "current";				
				break;
			}
	}
	if (this.remember) {
		var regex = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "=([01]+)");
		var match = regex.exec(document.cookie);
		if (match) {
			var states = match[1].split("");
			for (var i = 0; i < states.length; i++)
			{
			    if (this.submenus[i] != null) {	 				    		    
				    this.submenus[i].className = (states[i] == 0 ? "collapsed" : "");
				}
				if (states[i] == 1 && this.submenus[i] != null) {				    
			        this.expandMenu(this.submenus[i]);			   
			    }     
			}
		}
	}
};
SDMenu.prototype.toggleMenu = function(submenu) {    
	if (submenu.className == "collapsed" || submenu.className == "")
		this.expandMenu(submenu);	
	else	
		this.collapseMenu(submenu);
};
SDMenu.prototype.expandMenu = function(submenu) {    
	var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var links = submenu.getElementsByTagName("a");
			
	for (var i = 0; i < links.length; i++) 	    
		fullHeight += links[i].offsetHeight;		
		
	var moveBy = Math.round(this.speed * links.length);	
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight + moveBy;		
		if (newHeight < fullHeight){		    
			submenu.style.height = newHeight + "px";
		}else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "";
			mainInstance.memorize();
		}
	}, 34);	
	this.collapseOthers(submenu);		
};
SDMenu.prototype.collapseMenu = function(submenu) {
	var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var moveBy = Math.round(this.speed * submenu.getElementsByTagName("a").length);
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight - moveBy;
		if (newHeight > minHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "collapsed";
			mainInstance.memorize();
		}
	}, 34);
};
SDMenu.prototype.collapseOthers = function(submenu) {
	if (this.oneSmOnly) {
		for (var i = 0; i < this.submenus.length; i++)		    
			if (this.submenus[i] != null && this.submenus[i] != submenu && this.submenus[i].className != "collapsed")
				this.collapseMenu(this.submenus[i]);
	}
};
SDMenu.prototype.memorize = function() {
	if (this.remember) {
		var states = new Array();
		for (var i = 0; i < this.submenus.length; i++)
		    if (this.submenus[i] != null)
			    states.push(this.submenus[i].className == "collapsed" ? 0 : 1);
		var d = new Date();
		d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
		document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
	}
};

function atualizarMenu(saida) 
{
    var id = 'menuCIEEScript';
    var menu = document.getElementById(id);
    menu.submenus = menu.getElementsByTagName("div")
    var states = new Array();
	for (var i = 0; i < menu.submenus.length; i++)
	    if (menu.submenus[i] != null)
	    {
	        if (i == saida) 
	        {
		        states.push(menu.submenus[i].className == "collapsed" ? 1 : 1);
		    }else 
		    {
		        states.push(menu.submenus[i].className == "collapsed" ? 0 : 0);
		    }
		}
	var d = new Date();
	d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
	document.cookie = "sdmenu_" + encodeURIComponent(id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";    	
}

function tratarCaminhoRelativo(caminhaRelativo, url)
{
	var link = url.replace("~/", caminhaRelativo);
	return link;
}

/*Funções do Menu - Fim*/

/*Funções do UpdateProgress*/
var IE = document.all?true:false; //Test to see if IE
if (!IE) document.captureEvents(Event.MOUSEMOVE); //If not IE
document.onmousemove = getMouseLoc;

function Point(x,y) {  this.x = x; this.y = y; }
mouseLocation = new Point(0,0);

function getMouseLoc(e) 
{    
    if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		mouseLocation.x = e.pageX;
		mouseLocation.y = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
	    if (mouseLocation != null && document != null && document.body != null 
	        && document.documentElement != null) {
		    mouseLocation.x = e.clientX + document.body.scrollLeft
			    + document.documentElement.scrollLeft;
		    mouseLocation.y = e.clientY + document.body.scrollTop
			    + document.documentElement.scrollTop;
		}
	}   
     
    return true;
}

function positionDiv()
{    
    //Informando a posição da div hidden
    if (document.getElementById('ctl00_UProgress1_uProgressLogin') != null) 
    {          
        document.getElementById('ctl00_UProgress1_uProgressLogin').style.top = (mouseLocation.y - 30) + "px";
        document.getElementById('ctl00_UProgress1_uProgressLogin').style.left = (mouseLocation.x - 30) + "px";
        document.getElementById('ctl00_UProgress1_uProgressLogin').style.position = "absolute";                
    }
}

// Descrição    : Função utilizada para adicionar arquivos flash
// Autor        : Diego Vaz
function addSWF (URL, WIDTH, HEIGHT, TRANSPARENT)
{
    document.write (' <object classid="clsid:27CDB6E-AE6D-11cf-96B8-444553540000" ');
    document.write (' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" ');
    document.write (' width="'+ WIDTH +'" height="'+ HEIGHT +'">');
    document.write (' <param name="movie" value="'+ URL +'" />');
    document.write (' <param name="quality" value="high" />');

    if ( TRANSPARENT ) 
    {
        document.write (' <param name="Wmode" value="Transparent" />'); 
    }

    document.write (' <embed src="'+ URL +'" quality="high" ');

    if ( TRANSPARENT ) 
    {
        document.write (' Wmode = "transparent" ');
    }

    document.write (' pluginspage="http://www.macromedia.com/go/getflashplayer" ');
    document.write (' type="application/x-shockwave-flash" width="'+ WIDTH +'" height="'+ HEIGHT +'"></embed> ');
    document.write (' </object>');
}

// Descrição    : Função utilizada para abrir os popup's
// Autor        : Rafael Botelho
function popUpFull(url, tamanho, altura, scrollbars, status, location, toolbar, menubar) 
{    
    var x = window.screen.width;
    var y = window.screen.height;
    var left, top;    

    if (x==800 && y==600)
    {
        left = 150;
        top = 100;
        //Para não permitir que a janela fique muito grande
        if (altura > 547)
        {
            altura = 547;
        }
        if (tamanho > 800)
        {
            tamanho = 800;
        }
            
    }

    if (x==1024 && y==768)
    {
        left = 150;
        top = 100;
        //Para não permitir que a janela fique muito grande
        if (altura > 715)
        {
            altura = 715;
        }
        if (tamanho > 1024)
        {
            tamanho = 1024;
        }             
    }

	popUp = window.open(url, 'CIEE', 
	                    'height=' + altura + ',width=' + tamanho + ',menubar=' + menubar + 
	                    ',toolbar='+ toolbar + ',location='+ location + ',status=' + status + 
	                    ',scrollbars=' + scrollbars + ',directories=no,menu=1,resizable=no,copyhistory=no,' + 
	                    'left=' + left + ',top=' + top);
	                    
	return popUp;
}

// Descrição    : Função utilizada para abrir os popup's
// Autor        : Rafael Botelho
function popUp(url, tamanho, altura) 
{
    var scrollbars = 'yes';
    var status = 'no'; 
    var location = 'no'; 
    var toolbar = 'no';
    var menubar = 'no';    
    return popUpFull(url, tamanho, altura, scrollbars, status, location, toolbar, menubar) ;
}

function OpenPopUp(url, width, heigth, scrollbars) 
{
    var status = 'no'; 
    var location = 'no'; 
    var toolbar = 'no';
    var menubar = 'no';  
     
    return popUpFull(url, width, heigth, scrollbars, status, location, toolbar, menubar);
}

function popUpRelatorio(url)
{   
    var x = window.screen.width;
    var y = window.screen.height;
    var left, top, tamanho, altura;    

    left = 150;
    top = 100;
    altura = 547;
    tamanho = 800; 
        
    if (x==1024 && y==768)
        {
            left = 150;
            top = 100;
            altura = 715;
            tamanho = 1024;                         
        }

	janela = window.open(url, 'CIEE', 
	                    'height=' + altura + ',width=' + tamanho + ',menubar=no,toolbar=no,location=no,' + 
	                    'status=yes,scrollbars=yes,directories=no,menu=1,resizable=yes,copyhistory=no,' + 
	                    'left=' + left + ',top=' + top).moveTo(0,0);
}

// Descrição    : Função utilizada para validar um CPF
// Autor        : Rafael Botelho
function ValidaCPFHandler(src, args)
{
    args.IsValid = ValidarCPF(args.Value);    
}
function ValidarCPF(txtCPF) 
{    
    var numeros, digitos, soma, i, resultado, digitos_iguais, cpf;
    digitos_iguais = 1;
    cpf = txtCPF.replace('.', '').replace('.', '');
    cpf = cpf.replace('-', '');    
    if (cpf.length < 11)
        return false;
    if (cpf == '12345678909')
        return false;
    for (i = 0; i < cpf.length - 1; i++)
        if (cpf.charAt(i) != cpf.charAt(i + 1))
              {
              digitos_iguais = 0;
              break;
              }
    if (!digitos_iguais)
        {
        numeros = cpf.substring(0,9);
        digitos = cpf.substring(9);
        soma = 0;
        for (i = 10; i > 1; i--)
              soma += numeros.charAt(10 - i) * i;
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(0))
              return false;
        numeros = cpf.substring(0,10);
        soma = 0;
        for (i = 11; i > 1; i--)
              soma += numeros.charAt(11 - i) * i;
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(1))
              return false;
        return true;
        }
    else
        return false;
}

// Descrição    : Função utilizada para validar um CNPJ
// Autor        : Rafael Botelho
function ValidaCNPJHandler(src, args)
{
    args.IsValid = ValidarCNPJ(args.Value);    
}

function ValidarCNPJ(txtCNPJ) 
{    
    var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais, cnpj;
    digitos_iguais = 1;
    cnpj = txtCNPJ.replace('.', '').replace('.', '').replace('/', '').replace('-', '');
    
    if (cnpj.length < 14 && cnpj.length < 15)
        return false;
    for (i = 0; i < cnpj.length - 1; i++)
        if (cnpj.charAt(i) != cnpj.charAt(i + 1))
        {
            digitos_iguais = 0;
            break;
        }
        
    if (!digitos_iguais)
    {
        tamanho = cnpj.length - 2
        numeros = cnpj.substring(0,tamanho);
        digitos = cnpj.substring(tamanho);
        soma = 0;
        pos = tamanho - 7;
        for (i = tamanho; i >= 1; i--)
        {
            soma += numeros.charAt(tamanho - i) * pos--;
            if (pos < 2)
            pos = 9;
        }
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(0))
            return false;
        tamanho = tamanho + 1;
        numeros = cnpj.substring(0,tamanho);
        soma = 0;
        pos = tamanho - 7;
        for (i = tamanho; i >= 1; i--)
        {
            soma += numeros.charAt(tamanho - i) * pos--;
            if (pos < 2)
            pos = 9;
        }
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(1))
            return false;
            
        return true;
    }
    else
        return false;
}

// Descrição    : Função utilizada para validar o tamanho das senhas no site que por padrão devem ter no mínimo 6 caracteres.
// Autor        : Rafael Botelho
function ValidaTamanhoSenha(src, args)
{
    var retorno = false;    
    if (args.Value.length >= 6)
        retorno = true;
    args.IsValid = retorno;            
}

// Descrição    : Função utilizada para validar o tamanho do CNPJ base que deve ter no 8 caracteres.
// Autor        : Rafael Botelho
function ValidaTamanhoCNPJBase(src, args)
{
    var retorno = false;    
    if (args.Value.length == 8)
        retorno = true;
    args.IsValid = retorno;            
}

// Descrição    : Função utilizada para validar a quantidade mínima de palavras numa frase
// Autor        : Rafael Botelho
function ValidaQtdPalavrasNome(src, args)
{
    var retorno = false;
    if (args.Value.split(" ").length >= 2)
        retorno = true;
    args.IsValid = retorno;            
}

// Descrição    : Função utilizada para bloquear a submissão do formulário através da tecla enter.
// Autor        : Rafael Botelho
function onEnterBlock()
{
    if (window.event.keyCode == 13)
    {
        event.returnValue=false;
        event.cancel = true;
    }
}

// Descrição    : abrir e fechar uma div ao clicar.
// Autor        : Diego Vaz
function abre(div){
	var tipo = document.getElementById(div).style.display;
	if (tipo == "none")
		{
			document.getElementById(div).style.display="block";	
		}
	else
		{
			document.getElementById(div).style.display="none";	
		}

}

// Descrição    : Função simples para abrir pop up.
// Autor        : Diego Vaz
function MM_openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

//Altera a visibilidade dos controles
function Visibilidade(txtVisible, valor) {
	txtVisible.style.visibility = valor;
}