//Administrador
document.onkeydown = checkKeycode

var keycode;

function checkKeycode(e) 
{


    if (window.event) 
    { 
        keycode = window.event.keyCode;
    }
    else if (e) 
    {
        keycode = e.which;
    }
    
    if(keycode == 27)
    {
			var obj = document.getElementById("ResultBusca");	
			var tdCima = document.getElementById("td_pesquisa_geral");
			var tdBaixo = document.getElementById("td_pesquisa_baixo");
			var tdResult = document.getElementById("td_pesquisa_result");
		
       	obj.className = "close";
		tdCima.innerHTML = "";
		tdBaixo.innerHTML = "";	
		tdResult.innerHTML = "";	
    }

}


function focuses(obj)
{
	document.getElementById(obj).focus();	
}
function DateMask(obj)
{
	var value = obj.value;
	
	if(value.length == 2)
	{
		obj.value += "/";	
	}
	if(value.length == 5)
	{
		obj.value += "/";	
	}
}
function AbreVisualizacaoEvento(id)
{
    
    var obj = document.getElementById("OpenMeio");
     
	if(navigator.appName == "Microsoft Internet Explorer")
	{ 
		obj.className = "abreDivForMeioIE";
	}
	else
	{
		obj.className = "abreDivForMeio";
	}
    OpenConteudoEventoMeioAjax(id);
 
}

function AbreVisualizacaoEventoFoto()
{
 
    var obj = document.getElementById("OpenMeio");
     
	if(navigator.appName == "Microsoft Internet Explorer")
	{ 
		obj.className = "abreDivForMeioIE";
	}
	else
	{
		obj.className = "abreDivForMeio";
	}
	
    OpenBuscaEventoForFoto();
 
}

function divNoMeio()
{
     
	if(navigator.appName != "Microsoft Internet Explorer")
	{
		
    	var obj = document.getElementById("OpenMeio");
		obj.style.top = window.scrollY  + "px";	
		//alert(window.scrollY); 
	}
}

function AbreDivMeio()
{
	setInterval("divNoMeio()", 300);	
}

function AbreVisualizacaoFotoMeio(id)
{
    var obj = document.getElementById("OpenMeio");
     
	if(navigator.appName == "Microsoft Internet Explorer")
	{ 
		obj.className = "abreDivForMeioIE";
	}
	else
	{
		obj.className = "abreDivForMeio";
	}
    AbreEventoCadFotoMEIO(id);
}


function CloseAll(div)
{
	var obj = document.getElementById(div);
	
	obj.className = "close";
}

function DeletaSimNao(url)
{
		if(confirm("Tem certeza que deseja deletar esse registro?"))
		{
			top.location.href= url;	
		}
}

function AbreBusca(value)
{
	var obj = document.getElementById("ResultBusca");	
	var tdCima = document.getElementById("td_pesquisa_geral");
	var tdBaixo = document.getElementById("td_pesquisa_baixo");
	var tdResult = document.getElementById("td_pesquisa_result");
	
	if(keycode != 27)
	{
		if(value != "")
		{
			obj.className = "DivBusca";
			tdCima.innerHTML = "<span style='cursor: pointer;' onclick='AbreUrlBuscaEvento(" + '"' + value + '"' + ")';> Pesquisar <span class='td_pesquisa_geral_bold'>\"" + value + "\"</span> em todos os eventos.</span>";
			tdBaixo.innerHTML = "<span class='td_pesquisa_baixo_bold'>\"" + value + "\"</span> na lista direta de eventos.";
		 
			BuscaEventoAdmin(value);
		}
		else
		{
			obj.className = "close";
			tdCima.innerHTML = "";
			tdBaixo.innerHTML = "";	
			tdResult.innerHTML = "";	
		}
	}
}

function AbreResultBuscaEventoFoto(value)
{
	var obj = document.getElementById("ResultBuscaEventoFoto");
	
	if(keycode != 27)
	{
		if(value != "")
		{
			obj.className = "DivBusca";
			BuscaEventoFotoAdmin(value);
		}
		else
		{
			obj.className = "close";
			obj.innerHTML = "";
		}
	}
}


function AbreBuscaFotos(value)
{
	var obj = document.getElementById("ResultBusca");	
	var tdCima = document.getElementById("td_pesquisa_geral");
	var tdBaixo = document.getElementById("td_pesquisa_baixo");
	var tdResult = document.getElementById("td_pesquisa_result");
	
	if(keycode != 27)
	{
		if(value != "descrição")
		{
			obj.className = "DivBusca";
			tdCima.innerHTML = "<span style='cursor: pointer;' onclick='AbreUrlBuscaFoto(" + '"' + value + '"' + ")';> Pesquisar <span class='td_pesquisa_geral_bold'>\"" + value + "\"</span> em todos os eventos.</span>";
			tdBaixo.innerHTML = "<span class='td_pesquisa_baixo_bold'>\"" + value + "\"</span> na lista direta de eventos.";
		 
			BuscaFotoAdmin(value);
		}
		else
		{
			obj.className = "close";
			tdCima.innerHTML = "";
			tdBaixo.innerHTML = "";	
			tdResult.innerHTML = "";	
		}
	}
}

function AbreUrlBuscaFoto(value)
{
	top.location.href = "?mod=busca_fotos&value=" + value;
}

function AbreUrlBuscaEvento(value)
{
	top.location.href = "?mod=busca_completaEventos&value=" + value;
}



function passwordStrength(password) {
    score = 0
	
	var username = document.getElementById("txtLogin").value;
	var objReturn = document.getElementById("returnResultPass");
	
	if(password == "")
	{
		objReturn.innerHTML = "<img src=\"../Imagens/gif/help_nada.gif\">"; 		
	}

	
    //password < 4
    if (password.length > 0 && password.length < 4 ) 
	{ 
		objReturn.innerHTML = "<img src=\"../Imagens/gif/help_fraca.gif\">"; 
	}
	

    //password == username
    if (password.toLowerCase()==username.toLowerCase()) 
	{
		objReturn.innerHTML = "<img src=\"../Imagens/gif/help_nada.gif\">"; 
	}

    //password length
    score += password.length * 1
    score += ( checkRepetition(1,password).length - password.length ) * 1
    score += ( checkRepetition(2,password).length - password.length ) * 1
    score += ( checkRepetition(3,password).length - password.length ) * 1
    score += ( checkRepetition(4,password).length - password.length ) * 1

    //password has 3 numbers
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5

    //password has 2 sybols
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5

    //password has Upper and Lower chars
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10

    //password has number and chars
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15
    //
    //password has number and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15

    //password has char and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15

    //password is just a nubers or chars
    if (password.match(/^w+$/) || password.match(/^d+$/) )  score -= 10

    //verifing 0 < score < 100
    if ( score < 0 )  score = 0
    if ( score > 100 )  score = 100
	
	if(score < 10 && password != "")
	{
			objReturn.innerHTML = "<img src=\"../Imagens/gif/help_fraca.gif\">"; 
	}
    if (score > 10 && score < 34 )  
	{
		objReturn.innerHTML = "<img src=\"../Imagens/gif/help_media.gif\">"; 
	}
    if (score > 34 && score <= 68 ) 
	{
		objReturn.innerHTML = "<img src=\"../Imagens/gif/help_forte.gif\">"; 
	}
	
}

function checkRepetition(pLen,str) {
    res = ""
    for ( i=0; i<str.length; i++ ) {
        repeated=true
        for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
            repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
        if (j<pLen) repeated=false
        if (repeated) {
            i+=pLen-1
            repeated=false
        }
        else {
            res+=str.charAt(i)
        }
    }
    return res
}

function AbreDivAjuda()
{
	var obj = document.getElementById("balaoAjuda");
	obj.className = "balaoFundoSenha";	
}

function ValidaCamposAndSubmit()
{
	var nome = document.getElementById("txtNome");
	var sobrenome = document.getElementById("txtSobreNome");
	var login = document.getElementById("txtLogin");
	var senha = document.getElementById("txtSenha");
	var senha2 = document.getElementById("txtSenha2");
		
	if(document.getElementById("idUserHi").value == "")
	{
		if(nome.value.length <= 3)
		{
			alert("É necessario pelo menos 3 caracteres no campo nome");	
			nome.focus();
		}
		
		else if(sobrenome.value.length <= 3)
		{
			alert("É necessario pelo menos 3 caracteres no campo sobrenome");	
			sobrenome.focus();
		}
		
		else if(login.value.length <= 4)
		{
			alert("É necessario pelo menos 4 caracteres no campo login");	
			login.focus();
		}
		
		else if(senha.value.length <= 4)
		{
		
			alert("É necessario pelo menos 4 caracteres no campo senha");	
			senha.focus();
			
		}
		
		else if(senha.value != senha2.value)
		{
			alert("O campo senha e o confirma senha não são iguais, porfavor digite novamente.");
			senha2.value = "";
			senha.value = "";
			senha.focus();
		}
		
		else
		{
			document.getElementById("form1").submit();	
		}
	}
	else
	{
		if(nome.value.length <= 3)
		{
			alert("É necessario pelo menos 3 caracteres no campo nome");	
			nome.focus();
		}
		
		else if(sobrenome.value.length <= 3)
		{
			alert("É necessario pelo menos 3 caracteres no campo sobrenome");	
			sobrenome.focus();
		}
		
		else if(login.value.length <= 4)
		{
			alert("É necessario pelo menos 4 caracteres no campo login");	
			login.focus();
		}
		
		else if(senha.value != senha2.value)
		{
			alert("O campo senha e o confirma senha não são iguais, porfavor digite novamente.");
			senha2.value = "";
			senha.value = "";
			senha.focus();
		}
		
		else
		{
			document.getElementById("form1").submit();	
		}
	}

}

//Cliente

function AbreDivOutrosEventos()
{
	
	var obj = document.getElementById("divOutrosEventos");
	obj.className = "divOutrosEventos";
}

function MudaTitle(string)
{
	document.title = string;	
}
function LimpaEmail()
{
	var obj = document.getElementById("emailInsert");
	
	if(obj.value == "e-mail")
	{
		obj.value = "";	 
	}
}

function VoltaEmail()
{
	var obj = document.getElementById("emailInsert");
	
	if(obj.value == "" || obj.value == " " || obj.value == "  " || obj.value == "   " || obj.value == "     ")
	{
		obj.value = "e-mail";	
	}	
}
