function get(id) { 
	try { 
		var a = document.getElementById(id); 
	} catch(e) { 
		alert("Erro encontrado: "+e); 		
	} 
	return 	a;
}

var state = 22;

var url = '/resposta-representantes';

function changeState(codigo,pg)
{
	state = codigo;	
	
	if(!pg) 
	{
		pg = 0;
	}
	
	loadMapa(state,pg);
}

function mapa()
{
	return state;	
}

function loadMapa (codigo,pg)
{
	if(!codigo) 
	{
		codigo = 0;
	}
	
	if(!pg) 
	{
		pg = 0;
	}
	
	$.ajax(
	{
	   url: SITE + url,
	   type:'POST',
	   data: { codigo:codigo, pg:pg },
	   dataType: 'html',
	   success: function (data) {		   
		   $('#resposta_mapa').html(data);
	   }
	});
}


function viewContact(codigo)
{
	Utils.openWindow(SITE + "/janela", {'codigo':codigo}, 608, 372);
}

function viewContact2(codigo)
{
	Utils.openWindow(SITE + "/janela2", {'codigo':codigo}, 608, 372);
}


function animLinha()
{
	
	var els = $('[id*=linha]').css({"display":"block","opacity":"0"});
	
	var i = 0;
	
	var busy = false;
	
	var time = setInterval(function(){
		if (i > els.length)
			clearInterval(time);
			
		els.eq(i).animate({
			opacity: 1,
			marginTop:50
		}, 1200,"easeInOutQuart", function(){
			busy = false;	
		});
		i++;
	}, 90);
}

function callBusca()
{
	if($("#search").val() == "")
	{
		$("#search").focus();
		return false;	
	}
}

function showHide (id)
{	
	if(get(id).style.display == 'none')
	{
		get(id).style.display = 'block';
	}else{
		get(id).style.display = 'none';
	}
}

function downloadFile(codigo)
{
	var files=[codigo];if(codigo){var tempIFrame=document.createElement('iframe');tempIFrame.setAttribute('name','IFrameDownload');tempIFrame.style.border='0px';tempIFrame.style.width='0px';tempIFrame.style.height='0px';var iptCodigo=document.createElement('input');iptCodigo.type="hidden";iptCodigo.name="codigo";iptCodigo.value=codigo;var form=document.createElement('form'); form.action= SITE + '/download';form.method='POST';form.appendChild(iptCodigo);form.target='IFrameDownload';document.body.appendChild(tempIFrame);document.body.appendChild(form); form.submit();}
}

function showMessage()
{
	$.blockUI({ 
            message: $('#mensagem'), 
            fadeIn: 700, 
            fadeOut: 700, 
            showOverlay: false, 
            centerY: false, 
            css: { 
                width: '350px', 
                border: 'none', 
                padding: '5px', 
				top:"5px",
                backgroundColor: '#000', 
                '-webkit-border-radius': '10px', 
                '-moz-border-radius': '10px', 
                opacity: .6, 
                color: '#fff' 
            } 
        }); 	
}

function showMessageTimeout()
{
	$.blockUI({ 
            message: $('#mensagem'), 
            fadeIn: 700, 
            fadeOut: 700, 
			timeout: 1000,
			showOverlay: false, 
            centerY: false, 
            css: { 
                width: '350px', 
				top:"5px",
                border: 'none', 
                padding: '5px', 
                backgroundColor: '#000', 
                '-webkit-border-radius': '10px', 
                '-moz-border-radius': '10px', 
                opacity: .6, 
                color: '#fff' 
            } 
        }); 	
}


function hideMessage(erro)
{
	$.unblockUI({ 
		onUnblock: function(){$('#erro_usuario').html(erro); showMessageTimeout();} 
	}); 	
}

function validaLogin()
{
	$('#erro_usuario').html('<img src="' + DIR_IMAGES + 'loading.gif" />');
	
	showMessageTimeout();
	
	var usuario = $("#usuario").val();
	var senha = $("#senha").val();
	
	$.ajax(
	{
	   url: SITE + "/login",
	   type:'POST',
	   data: { 'usuario':usuario, 'senha':senha },
	   dataType: 'html',
	   success: function (data) {		   
		   if($.trim(data) == 'true')
		   {
			  hideMessage("Logado com sucesso, aguarde um momento");
			  
			  window.location = SITE + '/extranet';
		   }else{
		   	  hideMessage("Usuário ou Senha invalidos.");
		   }
	   }
	});	
}
