var showcase_timeout = 4000;
var picture_timeout = 400;
var showcase_animation;
var picture_fade =-1;

// Função de validação do form cad_empresa - Local: portal/empresas/cad
function validform(form) {

	if(form.razao_social.value == false){
		alert("Preencha a Razão social.");
		form.razao_social.focus();
		return (false);
	}

	if(form.cep.value.length < 6 ){
		alert("Digite o CEP.");
		form.cep.focus();
		return (false);
	}

	if(form.ddd.value.length == 0){
		alert("Digite o ddd do telefone da empresa.");
		form.ddd.focus();
		return (false);
	}

	if(form.fone.value.length == 0){
		alert("Digite o telefone da empresa.");
		form.fone.focus();
		return (false);
	}
	else
	{
		fone = form.fone.value;
		for( i = 0; i < fone.length; i++ )
		{
			tmp = fone.charAt(i);
			if( tmp == "." )
			{
				alert("Não cloque pontos.");
				campo.focus();
				return false;
			}
		}
	}

	var t = 0;
	for(cont=0;cont<form.email.value.length;cont++)
	{
		if(form.email.value.charAt(cont) == "@")
			{
				t = 1;
			}

		if(form.email.value.charAt(cont) == false)
				{
					t = 3;
				}
	}

	if (t == 3)
	{
		alert("O e-mail não pode ter espaços!");
		form.email.focus();
		form.email.value = "";
		return (false);
	}


	if (t != 1)
	{
		alert("O e-mail digitado é inválido.");
		form.email.focus();
		form.email.value = "";
		return (false);
	}

	var alt_midia = document.getElementById("outras_midias");
	if(alt_midia.style.display == "inline")
	{
		if(form.midia_txt.value.length == 0)
		{
			window.alert("Preencha como conheceu o NUBE");
			form.midia_txt.focus();
			return (false);
		}
	}

	return (true);
}

// Função de tratamento de números do form cad_empresa - Local: portal/empresas/cad
function tratarNumerico(campo)
{
	str = campo.value;
	for( i = 0; i < str.length; i++ )
	{
		tmp = str.charAt(i);
		if( tmp != "1" && tmp != "2" && tmp != "3" && tmp != "4" && tmp != "5" && tmp != "6" && tmp != "7" && tmp != "8" && tmp != "9" && tmp != "0" )
		{
			alert("Este campo é numérico!\nEntre com apenas números.");
			campo.focus();
			return false;
		}
	}
	return true;
}

// // Função de validação do form cadastro_empresa - Local: portal/escolas/instituicao
function validformescolas(form) {

        if(form.razao_social.value == false){
		alert("Preencha a Razão social.");
		form.razao_social.focus();
		return (false);
	}

        if(form.nome_fantasia.value == false){
		alert("Preencha o Nome Fantasia.");
		form.razao_social.focus();
		return (false);
	}

	if(form.cep.value.length < 6 ){
		alert("Digite o CEP.");
		form.cep.focus();
		return (false);
	}

	if(form.fone.value.length == 0){
		alert("Digite o fone da empresa.");
		form.fone.focus();
		return (false);
	}
	else
	{
		fone = form.fone.value;
		for( i = 0; i < fone.length; i++ )
		{
			tmp = fone.charAt(i);
			if( tmp == "." )
			{
				alert("Não cloque pontos.");
				campo.focus();
				return false;
			}
		}
	}

	if(form.fax.value.length == 0){
		alert("Digite o fax da empresa.");
		form.fax.focus();
		return (false);
	}



	var t = 0;
	for(cont=0;cont<form.email.value.length;cont++)
	{
		if(form.email.value.charAt(cont) == "@")
			{
				t = 1;
			}

		if(form.email.value.charAt(cont) == false)
				{
					t = 3;
				}
	}

	if (t == 3)
	{
		alert("O e-mail não pode ter espaços!");
		form.email.focus();
		form.email.value = "";
		return (false);
	}


	if (t != 1)
	{
		alert("O e-mail digitado é inválido.");
		form.email.focus();
		form.email.value = "";
		return (false);
	}

        if(form.contato.value == false){
		alert("Preencha o Contato.");
		form.razao_social.focus();
		return (false);
	}

	return (true);
}

// Função que imprime a hora por extenso
function datahora(){

var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var timeValue = "" + ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += (hours >= 12) ? " PM" : " AM"
timerRunning = true;

mydate = new Date();
myday = mydate.getDay();
mymonth = mydate.getMonth();
myweekday= mydate.getDate();
weekday= myweekday;
myyear= mydate.getYear();
year = myyear

if(myday == 0)
day = " Domingo, "

else if(myday == 1)
day = " Segunda, "

else if(myday == 2)
day = " Terça, "

else if(myday == 3)
day = " Quarta, "

else if(myday == 4)
day = " Quinta, "

else if(myday == 5)
day = " Sexta, "

else if(myday == 6)
day = " Sábado, "

if(mymonth == 0)
month = " de Janeiro de "

else if(mymonth ==1)
month = " de Fevereiro de "

else if(mymonth ==2)
month = " de Março de "

else if(mymonth ==3)
month = " de April de "

else if(mymonth ==4)
month = " de Maio de "

else if(mymonth ==5)
month = " de Junho de "

else if(mymonth ==6)
month = " de Julho de "

else if(mymonth ==7)
month = " de Agosto de "

else if(mymonth ==8 )
month = " de Setembro de "

else if(mymonth ==9)
month = " de Outubro de "

else if(mymonth ==10)
month = " de Novembro de "

else if(mymonth ==11)
month = " de Dezembro de "

document.write( day + myweekday + month + year + " - " + timeValue);

}

function calcular_cotas_aprendiz(campo)
{
	tratar_campo_aprendiz(campo);

	formulario = document.calculo_aprendiz_form;

	qtde_empregados = parseInt(formulario.qtde_empregados.value);

	funcoes_tecnico_superior = parseInt(formulario.funcoes_tecnico_superior.value);
	cargos_lideranca = parseInt(formulario.cargos_lideranca.value);
	cargos_vedados = parseInt(formulario.cargos_vedados.value);
	cargos_sazonais = parseInt(formulario.cargos_sazonais.value);
	cargos_risco = parseInt(formulario.cargos_risco.value);
	cargos_temporarios = parseInt(formulario.cargos_temporarios.value);
	cargos_terceirizados = parseInt(formulario.cargos_terceirizados.value);
	cargos_imorais = parseInt(formulario.cargos_imorais.value);

	total_b = funcoes_tecnico_superior + cargos_lideranca + cargos_vedados + cargos_sazonais + cargos_risco + cargos_temporarios + cargos_terceirizados + cargos_imorais;
	formulario.total_b.value = total_b;
	saldo = qtde_empregados - total_b;

	if(saldo >= 0)
	{
		cota_minima = saldo * 0.05;
		cota_maxima = saldo * 0.15;
		formulario.saldo.value = saldo;
		formulario.cota_minima.value = cota_minima;
		formulario.cota_maxima.value = cota_maxima;
	}
	else
	{
		formulario.saldo.value = 0;
		formulario.cota_minima.value = 0;
		formulario.cota_maxima.value = 0;
	}
}

function tratar_campo_aprendiz(campo)
{
	if (campo.value == '' || parseInt(campo.value) < 0 || isNaN(campo.value))
		campo.value = '0';
}

function startShowcaseAnimation(showcase)
{
   showcase_animation = setInterval( function(){nextShowcaseItem(showcase);}, showcase_timeout );
}

function stopShowcaseAnimation()
{
    clearInterval(showcase_animation);
    showcase_animation = -1;
}

function startShowcase(showcase)
{
    var lis;
    var i;
    lis = showcase.getElementsByTagName("ul")[0].getElementsByTagName("li");
    enableShowcaseItem(lis[0]);
    for (i=1;i<lis.length;i++)
    {
        disableShowcaseItem(lis[i]);
    }
    startShowcaseAnimation(showcase);
}

function enableShowcaseItem(si)
{
    si.style.display = 'block';

    if (navigator.appName.toLowerCase().indexOf("microsoft") >= 0 )
    {
        si.style.filter = "alpha(opacity=100)";
    }
    else
    {
        si.style.opacity = '1';
    }
    si.style.zIndex = '3';
}

function disableShowcaseItem(si)
{
    si.style.display='none';
    if (navigator.appName.toLowerCase().indexOf("microsoft") >= 0 )
    {
        si.style.filter = "alpha(opacity=100)";
    }
    else
    {
        si.style.opacity = '1';
    }
    si.style.zIndex = '1';
}

function play_pauseShowcase(showcase)
{
    if(showcase_animation != -1)
    {
        stopShowcaseAnimation();
    }
    else
    {
        startShowcaseAnimation(showcase);
    }
    changePlay_pauseButton(showcase);
}

function changePlay_pauseButton(s)
{
    var button = document.getElementById("play_pause");
    var attribute_name = 'class';
    var class_name;

    if(showcase_animation != -1)
    {
        class_name = 'pause';
    }
    else
    {
        class_name = 'play';
    }
    button.setAttribute(attribute_name, class_name);
}

function nextShowcaseItem(showcase)
{
    var lis;
    var i;
    var j;
    lis = showcase.getElementsByTagName("ul")[0].getElementsByTagName("li");
    i = getCurrentShowcaseIndex(lis);
    j = getNextShowcaseIndex(i, lis);
    swapShowcaseItems(lis,i,j);
}

function nextShowcaseItemManually(showcase)
{
    var busy = (picture_fade != -1);
    if ( !busy )
    {
        var active = (showcase_animation != -1);
        /*para o showcase para não acontecer de ele mudar 2
         vezes quando for clicado no ultimo instante (ao mudar ele atinge timeout)*/
        if (active)
        {
            stopShowcaseAnimation();
            /*Faz para o showcase voltar a funcionar assim que o tempo de fade
            da img terminar. Isto faz com que o próximo fade ocorra após (quase)
            exatamente "showcase_timeout" milisegundos*/
            setTimeout(function(){startShowcaseAnimation(showcase)}, picture_timeout);
        }
        nextShowcaseItem(showcase);
    }
}

function getCurrentShowcaseIndex(lis)
{
    var i=0;
    var found=false;
    while(i<lis.length && found == false)
    {
        if(lis[i].style.display == 'block')
        {
            found = true;
        }
        else
        {
            i++;
        }
    }
    return i;
}

function getNextShowcaseIndex(current, lis)
{
    return (current+1) % lis.length;
}

function getPreviousShowcaseIndex(current, lis)
{
    var t = lis.length;
    return (current+t-1) % t;
}

function previousShowcaseItem(showcase)
{
    var lis;
    var i;
    lis = showcase.getElementsByTagName("ul")[0].getElementsByTagName("li");
    i = getCurrentShowcaseIndex(lis);
    swapShowcaseItems(lis, i, getPreviousShowcaseIndex(i, lis) );
}

function previousShowcaseItemManually(showcase)
{
    var busy = (picture_fade != -1);
    if (! busy)
    {
        var active = (showcase_animation != -1);
        if (active)
        {
            /*para o showcase para não acontecer de ele mudar 2
            vezes quando for clicado no ultimo instante (ao mudar ele atinge timeout)*/
            stopShowcaseAnimation();
            /*Faz para o showcase voltar a funcionar assim que o tempo de fade
            da img terminar. Isto faz com que o próximo fade ocorra após (quase)
            exatamente "showcase_timeout" milisegundos*/
            setTimeout(function(){startShowcaseAnimation(showcase)}, picture_timeout);
        }
        previousShowcaseItem(showcase);
    }
}

function swapShowcaseItems(lis, index_from, index_to)
{
   fadeShowcaseItems(lis[index_from], lis[index_to]);
}

function fadeShowcaseItems(item_disappear, item_appear)
{
    startPictures_fade(item_disappear, item_appear);
}

function fadeItems(item_disappear, item_appear)
{
    var o = getItemOpacity(item_disappear);
    var tax  = 0.25;
    if( navigator.appName.toLowerCase().indexOf("microsoft") >= 0 )
    {
        tax = tax * 100;
    }

    if(o == 0.25 || o == 25)
    {
        disableShowcaseItem(item_disappear);
        enableShowcaseItem(item_appear);
    }
    else
    {
        o = o-tax;
        if (navigator.appName.toLowerCase().indexOf("microsoft") >= 0 )
        {
            item_disappear.style.filter = "alpha(opacity=" + o + ")";
        }
        else
        {
            item_disappear.style.opacity = o;
        }
    }
}

function getItemOpacity(item)
{
    var r=0;
    var vbegin;
    var qtde;
    if (navigator.appName.toLowerCase().indexOf("microsoft") >= 0)
    {
        r = item.style.filter;
        r.replace(/ /gi,'');
        vbegin = r.indexOf("opacity=") + "opacity=".length;
        r = parseFloat( r.substr(vbegin) );
    }
    else
    {
        r= parseFloat(item.style.opacity);
    }
    return r;
}

function stopPicture_fade()
{
    clearInterval(picture_fade);
    picture_fade = -1;
}

function startPictures_fade(item_disappear, item_appear)
{
    setTimeout( stopPicture_fade, picture_timeout);
    picture_fade = setInterval(
                                 function(){ fadeItems( item_disappear, item_appear); },
                                 (picture_timeout/10)
                );
}

function selectMidia(midia)
{
	if(midia.value == '41')
	{
		var alt_midia = document.getElementById("outras_midias");
		alt_midia.style.display="inline";
	}
	else
	{
		var alt_midia = document.getElementById("outras_midias");
		alt_midia.style.display="none";
	}
}
