function emptySelect(where, first_line) {
    var where, first_line;
    var childs = where.childElements();
    
    childs.each(function(e) {
        e.remove(); 
    }); 

    if(first_line != null) {
        where.options[0] = new Option(first_line);
        where.options[0].value = 0;
    }
}


function updateSelect(where, data, keys, first_line) {
    var where, data, keys, first_line, l;
    
    if (first_line != null) {
        where.options[0] = new Option(first_line);
        where.options[0].value = 0;
    }

    for (var i = 0; i < data.length; i++) {
        l = where.options.length;
        where.options[l] = new Option(data[i][keys[1]].unescapeHTML(), data[i][keys[0]]);
    }
}

var equipamento = 0;
function toggleEquipamento() {
    $('equipamento').toggle();
    
    /*if(equipamento) {
        new Effect.SlideUp('equipamento', { duration: 1 });
        $('equipbtn').replace('Ver equipamento');
        $('equipbtn').onclick = toggleEquipamento;
        equipamento = 0;
    }
    else {
        new Effect.SlideDown('equipamento', { duration: 1 });
        $('equipbtn').replace('Fechar equipamento');
        $('equipbtn').onclick = toggleEquipamento;
        equipamento = 1;
    }*/
}

function openLightBox() {
    Dialog.info($('lightbox_src').innerHTML, {windowParameters: {className: 'alphacube', width:492, height:347}})    
}

function openMsgLightBox(msg) {
    $('lightbox_msg').update(msg);
    Dialog.info($('lightbox_msg_src').innerHTML, {windowParameters: {className: 'alphacube', width:492, height:347}})
}

function addAlert(car_id) {
    new Ajax.Request('/ajax/addAlert/'+car_id,  
                    {method: 'get',
                      onSuccess: function(transport) {
                                     alert(transport.responseJSON.msg);
                                 },
                      onFailure: function(transport) {
                                     
                                 }
                        });
}

function removeAlert(ale_id) {
    if(confirm('Tem a certeza que deseja remover este alerta?'))
        window.location = '/omeug2/alertas/remover/'+ale_id;
}

function addFavorite(car_id) {
    new Ajax.Request('/ajax/addFavorite/'+car_id,  
                    {method: 'get',
                      onSuccess: function(transport) {
                                     alert(transport.responseJSON.msg);
                                 },
                      onFailure: function(transport) {
                                     
                                 }
                        });
}

function removeFavorite(car_id) {
    if(confirm('Tem a certeza que deseja remover este automóvel dos favoritos?'))
        window.location = '/omeug2/favoritos/remover/'+car_id;
}

function toggleRecover() {
    $('myg2_login', 'myg2_recoverpass').each(Element.toggle);
    
    if($('myg2_login').visible())
        $('recover_pass').update('Recuperar Password »');
    else
        $('recover_pass').update('Login »');
    
    if($('myg2_error'))
        $('myg2_error').update('');
}

function toggleRadio(id) {
    $(id).checked = true;
}

function toggleAvancada(){
	if(($('formularioPesquisaAvancada').style.display == 'none')||($('formularioPesquisaAvancada').style.display == '')){
		$('formularioPesquisaAvancada').style.display = 'block';
		$('triggerA').style.textDecoration = 'none';
		$('imgA').src = '/layouts/img/pesquisar/icon_arrow_up.gif'
		$('pesquisaToggler').style.backgroundImage = 'url(/layouts/img/pesquisar/bg_pesquisa_avancada_on.gif)';
	}else{
		$('formularioPesquisaAvancada').style.display = 'none';
		$('triggerA').style.textDecoration = 'underline';
		$('imgA').src = '/layouts/img/pesquisar/icon_arrow_down.gif'
		$('pesquisaToggler').style.backgroundImage = 'url(/layouts/img/pesquisar/bg_pesquisa_avancada_off.gif)';
	}
}

function toggleExtras(){
	if(($('extrasLista').style.display == 'none')||($('extrasLista').style.display == '')){
		$('extrasLista').style.display = 'block';
		$('triggerB').style.textDecoration = 'none';
		$('imgB').src = '/layouts/img/pesquisar/icon_arrow_up.gif'
		$('extrasToggler').style.backgroundImage = 'url(/layouts/img/pesquisar/bg_adicionar_extras_on.gif)';
	}else{
		$('extrasLista').style.display = 'none';
		$('triggerB').style.textDecoration = 'underline';
		$('imgB').src = '/layouts/img/pesquisar/icon_arrow_down.gif'
		$('extrasToggler').style.backgroundImage = 'url(/layouts/img/pesquisar/bg_adicionar_extras_off.gif)';
	}
}

function toggleCheck(inputID){
	if(($(inputID).checked == false) || ($(inputID).checked == '')){
		$(inputID).checked = true;
	}else{
		$(inputID).checked = false;
	}
}

function hideInfo(toShow,total)
{
	for(i = 1; i <= total; i++)
	{
		// se for o q estava activo, puxa-o pra cima
		if(i == toShow)
			Effect.SlideUp($('info'+i), { duration: 1.5 });
		$('info'+i).fade({ duration: 1.5 });
		$('button'+i).removeClassName('on');
		$('button'+i).addClassName('off');
	}
}

function toggleInfo(toShow, total)
{
	// se ja estiver activo, desactiva-o
	if($('button'+toShow).hasClassName('on'))
	{
		hideInfo(toShow,total);
		return false;
	}
	
	for(i = 1; i <= total; i++)
	{
		if(i != toShow)
		{
			// se n for para mostrar MAS tiver ligado, puxa para cima
			if($('button'+i).hasClassName('on'))
				Effect.SlideUp($('info'+i), { duration: 1.5 });
			$('info'+i).fade({ duration: 1.5 });
			$('button'+i).removeClassName('on');
			$('button'+i).addClassName('off');
		}
	}
	
	Effect.SlideDown($('info'+toShow), { duration: 1.5 });
	$('button'+toShow).removeClassName('off');
	$('button'+toShow).addClassName('on');
	return false;
}

function hideFaq(toShow,total)
{
	for(i = 1; i <= total; i++)
	{
		// se for o q estava activo, puxa-o pra cima
		if(i == toShow)
			Effect.SlideUp($('faq'+i), { duration: 1.0 });
		$('faq'+i).fade({ duration: 1.0 });
		$('toggler'+i).removeClassName('on');
		$('toggler'+i).addClassName('off');
		$('span'+i).innerHTML = "Clique para expandir este tema";
	}
}

function toggleFaq(toShow,total)
{
	//togglerX // spanX // faqX
	//mudar class // mudar texto // aplicar efeito
	
	// se ja estiver activo, desactiva-o
	if($('toggler'+toShow).hasClassName('on'))
	{
		hideFaq(toShow,total);
		return false;
	}
	
	
	for(i = 1; i <= total; i++)
	{
		if(i != toShow)
		{
			if($('toggler'+i).hasClassName('on'))
				$('faq'+i).hide();
            
			$('faq'+i).fade({ duration: 1.0 });
			$('toggler'+i).removeClassName('on');
			$('toggler'+i).addClassName('off');
			$('span'+i).innerHTML = "Clique para expandir este tema";
		}
	}
	
	Effect.SlideDown($('faq'+toShow), { duration: 1.0 });
	$('toggler'+toShow).removeClassName('off');
	$('toggler'+toShow).addClassName('on');
	$('span'+toShow).innerHTML = "Clique para fechar este tema";
	return false;
}

function moveArrow(lista_id,img_id)
{
	$(img_id).setStyle(
						{  top: parseInt($(img_id).getStyle('top')) + 1});
}



function showDetalhes(h) {
    for(i = 1; i < 4; i++) {
        if(i == h) { 
            document.getElementById(i).style.display = 'block';
			document.getElementById(i*100).style.backgroundImage = 'url(/layouts/img/apolices/icon_menos.gif)';
			document.getElementById(i*10).style.display = 'none';
        }
        else { 
            document.getElementById(i).style.display = 'none';
			document.getElementById(i*10).style.display = 'inline';
			document.getElementById(i*100).style.backgroundImage = 'url(/layouts/img/apolices/icon_mais.gif)';
        }
    }
}

function toggleDiv(divid){
	if((document.getElementById(divid).style.display == 'none')||(document.getElementById(divid).style.display == '')){
		document.getElementById(divid).style.display = 'block';
	}else{
		document.getElementById(divid).style.display = 'none';
	}
}

function mostraAmigo() {
	document.getElementById('labelAmigo').className = 'active';
	document.getElementById('labelAmigo').innerHTML = '<a onclick="javascript: mostraAmigo();" title="Enviar a um amigo"><img src="images/simulacao/icon_enviar.gif" width="12" height="17" alt="Enviar a amigo" /></a><a onclick="javascript: mostraAmigo();" title="Enviar a um amigo">Enviar a amigo</a><span>&nbsp;</span>';
	document.getElementById('formAmigo').style.display = 'block';
	document.getElementById('labelContacto').className = 'inactive';
	document.getElementById('formContacto').style.display = 'none';
	document.getElementById('labelContacto').innerHTML = '<a onclick="javascript: mostraContacto();" title="Contactem-me"><img src="images/simulacao/icon_contacto.gif" width="14" height="17" alt="Contactem-me" /></a><a  onclick="javascript: mostraContacto();" title="Contactem-me">Contactem-me &raquo;</a><span>&nbsp;</span>';
	document.getElementById('opcoes').style.borderBottom = 'none';
}

function mostraContacto() {
	document.getElementById('labelAmigo').className = 'inactive';
	document.getElementById('formAmigo').style.display = 'none';
	document.getElementById('labelAmigo').innerHTML = '<a onclick="javascript: mostraAmigo();" title="Enviar a um amigo"><img src="images/simulacao/icon_enviar.gif" width="12" height="17" alt="Enviar a amigo" /></a><a onclick="javascript: mostraAmigo();" title="Enviar a um amigo">Enviar a amigo &raquo;</a><span>&nbsp;</span>';
	document.getElementById('labelContacto').className = 'active';
	document.getElementById('labelContacto').innerHTML = '<a onclick="javascript: mostraContacto();" title="Contactem-me"><img src="images/simulacao/icon_contacto.gif" width="14" height="17" alt="Contactem-me" /></a><a  onclick="javascript: mostraContacto();" title="Contactem-me">Contactem-me</a><span>&nbsp;</span>';
	document.getElementById('formContacto').style.display = 'block';
	document.getElementById('opcoes').style.borderBottom = 'none';
}

function selectGender(h)
{
	if(h == 'm')
	{
		document.getElementById('masculino').checked = 'checked';
		document.getElementById('feminino').checked = false;
	}
	else
	{
		document.getElementById('feminino').checked = 'checked';
		document.getElementById('masculino').checked = false;
	}
}

/* Viral */
function sendActivationMail() {
    
    $('resend_msg').update('A enviar...');
    $('resend').show();
    $('resend_loader').show();
    
    new Ajax.Request('/omeug2/reenviar/',
                     {
                     method: 'get',
                     onSuccess: function() {
                                    $('resend_loader').hide();
                                    $('resend_msg').update('<strong>O seu email de activação foi enviado com sucesso.</strong>').show();
                                },
                     onFailure: function() {
                                    setTimeout('sendActivationMail()', 1000);
                                }
                     });    
}

function sendQuizTaf() {
    $('tafmsgcont').show();
    $('taf_loader').show();
    $('taf_msg').update('A enviar...');
    
    $('quiztaf').request({
                         onComplete: function(transport) {
                                        var json = transport.responseJSON;
                         
                                        $('taf_loader').hide();
                                        $('taf_msg').update(json.msg);
                                        
                                        if(json.status == 1) {
                                            $('quiztaf').reset();
                                            populateTafList(json.list);
                                        }
                                        
                                     }
                         }
                         );
    
    return false;    
}

function populateTafList(list) {
    $('taf_list').update('');
    
    list.each(function(elem) {
                $('taf_list').update($('taf_list').innerHTML + '<label>' + elem + '</label>');
              });
}

function resendTaf() {
    
    $('tafresendcont').show();
    $('tafresend_loader').show();
    $('tafresend_msg').update('A enviar...');
    
    new Ajax.Request('/omeug2/reenviartaf/',
                     {
                     method: 'get',
                     onSuccess: function(transport) {
                        $('tafresend_loader').hide();
                        $('tafresend_msg').update(transport.responseJSON.msg);
                     },
                     onFailure: function() {
                        setTimeout('resendTaf()', 1000);
                     }
                     });
    
}
