function findPage(page) {
   var url = 'controller.php?action=Page&id='+page;
   var parametro = '';
   var objAjaxLivros = new Ajax.Request( url,{ method: 'post', parameters: parametro, onComplete: updateContent });
   $('content').innerHTML = "<center><br><br><br><br><br><br><br><br><span class=titulo_lote> Carregando, Aguarde...</span><br><br><img src=imgs/loading.gif width=35 height=35><br><br><br><br><br><br><br><br><br><br><br></center>";
}

function updateContent(resposta)	{
   $('content').innerHTML = resposta.responseText;
}

function sendFormOpportunity() {
   var url = 'controller.php?action=FormOportunidade';
   var id_oportunidade = $F('id_oportunidade');
   var nome = $F('nome');
   var email = $F('email');
   var empresa = $F('empresa');
   var endereco = $F('endereco');
   var cidade = $F('cidade');
   var estado = $F('estado');
   var cep = $F('cep');
   var ddd = $F('ddd');
   var telefone = $F('telefone');
   var mensagem = $F('mensagem');
   var valor = $F('valor');
   document.getElementById('form_opportunity').style.display = "none"; 
   document.getElementById('msgForm').style.display = "block"; 
   
   var parametro = 'id_oportunidade=' + id_oportunidade +'&nome=' + nome + '&email=' + email + '&empresa=' + empresa + '&endereco=' + endereco + '&cidade=' + cidade + '&estado=' + estado + '&cep=' + cep + '&ddd=' + ddd + '&telefone=' + telefone + '&mensagem=' + mensagem + '&valor=' + valor;
   var objAjaxLivros = new Ajax.Request( url,{ method: 'post', parameters: parametro, onComplete: returnFormOpportunity });
   $('msgForm').innerHTML = "<center><br><br><br><br><br><br><br><br><span class=titulo_lote> Enviando formulário, Aguarde...</span><br><br><img src=imgs/loading.gif width=35 height=35><br><br><br><br><br><br><br><br><br><br><br></center>";
}

function returnFormOpportunity(resposta) {
   $('msgForm').innerHTML = resposta.responseText;
}

function historyBackFormOpportunity(){
   document.getElementById('msgForm').style.display = "none"; 
   document.getElementById('form_opportunity').style.display = "block";   
}

function Leilao(ID) {
   window.open("http://www.satoleiloes.com.br/controller.php?action=Leilao&id="+ID);
}

function Oportunidade(ID) {
   if( screen.width > 800 ) {
window.open("http://www.satoleiloes.com.br/controller.php?action=Oportunidade&id="+ID, "Oportunidade", "menubar=1,toolbar=1,location=1,directories=1,status=1,scrollbars=1,resizable=1,width=760,height=650,top=0,left=0");
   }else{
window.open("http://www.satoleiloes.com.br/controller.php?action=Oportunidade&id="+ID, "Oportunidade", "menubar=1,toolbar=1,location=1,directories=1,status=1,scrollbars=1,resizable=1,width=760,height=535,top=0,left=0");
   }
}

function CadastroOnLine() {
   window.open("http://satoleiloes.com.br/controller.php?action=CadastroOnLine", "CadastroOnLine", "menubar=1,toolbar=1,location=1,directories=1,status=1,scrollbars=1,resizable=1,width=700,height=400,top=80,left=80");
}

function openLeilaoOnLine() {
   window.open("http://www.hiperbid.com.br/", "LeilaoOnLine");
}

function visualizarEdital(ID) {
   if( screen.width > 800 ) {
 window.open('controller.php?action=VisualizarEdital&id='+ID, 'VisualizarEdital', 'menubar=1,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=0,width=900,height=650,top=5,left=50');
   }else{
 window.open('controller.php?action=VisualizarEdital&id='+ID, 'VisualizarEdital', 'menubar=1,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=0,width=650,height=470,top=5,left=70');
   }
}

function imprimeLotes(ID) {
   if( screen.width > 800 ) {
      window.open("controller.php?action=ImprimeLotes&id="+ID, "ImprimeLotes", "menubar=1,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=0,width=650,height=650,top=5,left=180");
   }else{
      window.open("controller.php?action=ImprimeLotes&id="+ID, "ImprimeLotes", "menubar=1,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=0,width=650,height=470,top=5,left=70");
   }
}

function cpf_check(num_cpf) {
   num_cpf=num_cpf.toString().replace(".","");
   num_cpf=num_cpf.toString().replace(".","");
   num_cpf=num_cpf.toString().replace("-","");
   num_cpf=num_cpf.toString().replace("-","");
   num_cpf=num_cpf.toString().replace("/","");

   if (num_cpf.length != 11) {
      sim=false;
   }
   else {
      if(num_cpf=='00000000000' || (num_cpf=='11111111111') || (num_cpf=='22222222222') || (num_cpf=='33333333333') || (num_cpf=='44444444444') || (num_cpf=='55555555555') || (num_cpf=='66666666666') || (num_cpf=='77777777777') || (num_cpf=='88888888888') || (num_cpf=='99999999999'))	{
         sim=false;
      }
      else {
         sim=true;
      }
   }
	
   if (sim ) { // valida o primeiro digito
      for (i=0;((i<=(num_cpf.length-1))&& sim); i++) {
         val = num_cpf.charAt(i)
         if	((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4") 
            && (val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {
               sim=false
         }
      }
			
      if (sim) {
         soma = 0
         for (i=0;i<=8;i++) {
            val = eval(num_cpf.charAt(i))
            soma = soma + (val*(i+1))
         }
         resto = soma % 11
         if (resto>9) dig = resto -10
         else dig = resto
         if (dig != eval(num_cpf.charAt(9))) { sim=false }
         else {// valida o segundo digito
            soma = 0
            for (i=0;i<=7;i++)	{
               val = eval(num_cpf.charAt(i+1))
               soma = soma + (val*(i+1))
            }
            soma = soma + (dig * 9)
            resto = soma % 11
            if (resto>9) dig = resto -10
            else dig = resto
            if (dig != eval(num_cpf.charAt(10))) { sim = false }
            else sim = true
         }
      }
   }
	
	if (sim) { 
	return true; 
	}
	else
	return false;
}


// Photo Gallery //

function showPhotoGallery(id_leilao,lote){
   var url = 'controller.php?action=CallPhotoGallery&id='+id_leilao+'&&lote='+lote;	
   var parametro = 'id='+id_leilao+'&lote='+lote;	
   var objAjaxLivros = new Ajax.Request( url,{ method: 'post', parameters: parametro, onComplete: returnShowPhotoGallery });
   document.getElementById('photogallery'+lote).innerHTML = "<br /><br /><br /><img src=imgs/loading.gif><br /><br /><br />";
}

function returnShowPhotoGallery(resposta){
   var alltxt = resposta.responseText;
   var cods = alltxt.split('|');
   var lote = cods[0];
   var txt = cods[1];
   var htm = cods[2];
   document.getElementById('photogallery'+lote).innerHTML = htm;
   document.getElementById('view_photos'+lote).innerHTML = txt;
}

function closePhotoGallery(id_leilao,lote){
   document.getElementById('view_photos'+lote).innerHTML = '<img src="imgs/icon_view_photos.jpg" border="0" /><br /><br /><a href="javascript: showPhotoGallery('+id_leilao+','+lote+')">Ver Fotos</a>';
   document.getElementById('photogallery'+lote).innerHTML = '';
}

function callPhoto(id_leilao,lote,foto){
   var url = 'controller.php?action=CallPhoto&id='+id_leilao+'&lote='+lote+'&foto='+foto;	
   var parametro = 'id='+id_leilao+'&lote='+lote+'&foto='+foto;	
   var objAjaxLivros = new Ajax.Request( url,{ method: 'post', parameters: parametro, onComplete: showPhoto });
}

function showPhoto(resposta){
   document.getElementById('photoStage').innerHTML = resposta.responseText;
   showOverlay();
}

function closePhoto(){
   var bgPhotoStage = document.getElementById('bgPhotoStage');
   var photoStage = document.getElementById('photoStage');	
   bgPhotoStage.style.display = "none";
   photoStage.innerHTML = "";
   photoStage.style.display = "none";
}

function callPhotoOpportunity(id_opportunity,foto){
   var url = 'controller.php?action=CallPhotoOpportunity&id='+id_opportunity+'&foto='+foto;	
   var parametro = 'id='+id_opportunity+'&foto='+foto;	
   var objAjaxLivros = new Ajax.Request( url,{ method: 'post', parameters: parametro, onComplete: showPhotoOpportunity });
}

function showPhotoOpportunity(resposta){
   document.getElementById('photoStage').innerHTML = resposta.responseText;
   showOverlay();
}

function getPageSize(){
// getPageSize()
// Returns array with page width, height and window width, height
   var xScroll, yScroll;
   var windowWidth, windowHeight;
   
   if (window.innerHeight && window.scrollMaxY) {	
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
   } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
   } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
   }
   if (self.innerHeight) {	// all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
   } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
   } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
   }	
   // for small pages with total height less then height of the viewport
   if(yScroll < windowHeight) pageHeight = windowHeight;
   else                       pageHeight = yScroll;

   // for small pages with total width less then width of the viewport
   if(xScroll < windowWidth) pageWidth = windowWidth;
   else                      pageWidth = xScroll;
   
   arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
}

function showOverlay() {
   var sizesPage = getPageSize();	 
   var offsetTop = (document.documentElement.scrollTop != 0)?document.documentElement.scrollTop:document.body.scrollTop;
   var centerPage = ((arrayPageSize[0]-710)/2);
   var bgPhotoStage = document.getElementById('bgPhotoStage');
   var photoStage = document.getElementById('photoStage');
	  
   bgPhotoStage.style.height = (arrayPageSize[1]+30) + 'px'; // fundo com o tamanho total da página.
   bgPhotoStage.style.display = "block";
   photoStage.style.marginTop = offsetTop + 'px';
   photoStage.style.marginLeft = centerPage + 'px';
   photoStage.style.display = "block";
}

// Photo Gallery //

function formatValue(campo,tammax,teclapres) {
   var tecla = teclapres.keyCode;
   var vr = limpaSinais(campo.value);
   var tam = vr.length;
	
   if (tam < tammax && tecla != 8){ tam = vr.length + 1 ;  }
	
   if (tecla == 8 ){	
      vr = limpaSinais(campo.value);
      tam = vr.length+1;
   }
			
   if ((tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) || (tecla == 8)){      
      tam = tam - 1;	  
      if ( tam <= 2 ){ campo.value = vr ; }        
      if ( (tam > 2) && (tam <= 5) ){ campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam) ; }
      if ( (tam >= 6) && (tam <= 8) ){ campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;  }
      if ( (tam >= 9) && (tam <= 11) ){ campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
      if ( (tam >= 12) && (tam <= 14) ){ campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
   }
}

function limpaSinais(str){
   str = str.replace( "/", "" );
   str = str.replace( "/", "" );
   str = str.replace( ",", "" );
   str = str.replace( ".", "" );
   str = str.replace( ".", "" );
   str = str.replace( ".", "" );
   str = str.replace( ".", "" );
   return str;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function clearFilterSearchAuctions(){
   var url = 'controller.php?action=FormSearchAuctions';
   document.getElementById('categoria_pesquisa_leilao').value = "0"; 
   document.getElementById('btnClearSearchAuctions').style.display = "none"; 
   
   var parametro = 'categoria_leilao=0';
   var objAjaxLivros = new Ajax.Request( url,{ method: 'post', parameters: parametro, onComplete: returnFormSearchAuctions });
   $('boxAuctions').innerHTML = "<div id=boxLoadingSearch>Carregando, aguarde...</div>";
}

function searchAuctions(){
   var url = 'controller.php?action=FormSearchAuctions';
   var categoria_pesquisa_leilao = $F('categoria_pesquisa_leilao');
   if (categoria_pesquisa_leilao==0) return;
   document.getElementById('btnClearSearchAuctions').style.display = "block";
   
   var parametro = 'categoria_pesquisa_leilao=' + categoria_pesquisa_leilao;
   var objAjaxLivros = new Ajax.Request( url,{ method: 'post', parameters: parametro, onComplete: returnFormSearchAuctions });
   $('boxAuctions').innerHTML = "<div id=boxLoadingSearch>Carregando, aguarde...</div>";
}

function returnFormSearchAuctions(resposta) {
   $('boxAuctions').innerHTML = resposta.responseText;
}

function highlightOnBtnCategorySearch(btn){
   if (document.getElementById(btn).className != "bt_lot_category_active") {
	   document.getElementById(btn).className = "bt_lot_category_on";
   }
}

function highlightOffBtnCategorySearch(btn){
   if (document.getElementById(btn).className != "bt_lot_category_active") {
	   document.getElementById(btn).className = "bt_lot_category_off";
   }
}

function searchLotByCategory(btn,idCategory){
   var id = $('id_leilao').value;
   var qtdCategory = $('qtd_category').value;
   for (i=1;i<=qtdCategory;i++){
      document.getElementById('btnCategory_'+i).className = "bt_lot_category_off";
   }
   document.getElementById('btnCategory_'+btn).className = "bt_lot_category_active";
   document.getElementById('descricao_pesquisa_lote').value="";
   document.FormSearchLotsAuction.tipo_pesquisa_lote[0].checked=true;
   changeTipoPesquisaLote('D');
   
   var url = 'controller.php?action=searchLotByCategory&categoria_lote='+idCategory+'&id='+id;	
   var parametro = 'categoria_lote='+idCategory+'&id='+id;
   var objAjaxLivros = new Ajax.Request( url,{ method: 'post', parameters: parametro, onComplete: showSearchLotByCategory });
   $('boxLots').innerHTML = "<div id=boxLoadingSearch>Carregando, aguarde...</div>";
}

function searchLotBySearchType(){ 
   var id = $('id_leilao').value;
   var descricaoPesquisaLote = $F('descricao_pesquisa_lote');
   var cidadePesquisaLote = $F('cidade_pesquisa_lote');
   var tipoPesquisaLote = "";
   var ordemPesquisaLote = "";
   
   for( i = 0; i < document.FormSearchLotsAuction.ordem_pesquisa_lote.length; i++ ){
      if( document.FormSearchLotsAuction.ordem_pesquisa_lote[i].checked == true ){
		  ordemPesquisaLote = document.FormSearchLotsAuction.ordem_pesquisa_lote[i].value;
	  }
   }
   for( i = 0; i < document.FormSearchLotsAuction.tipo_pesquisa_lote.length; i++ ){
      if( document.FormSearchLotsAuction.tipo_pesquisa_lote[i].checked == true ){
		  tipoPesquisaLote = document.FormSearchLotsAuction.tipo_pesquisa_lote[i].value;
	  }
   }
   
   var url = 'controller.php?action=searchLotBySearchType&tipo_pesquisa_lote='+tipoPesquisaLote+'&id='+id+'&descricao_pesquisa_lote='+descricaoPesquisaLote+'&cidade_pesquisa_lote='+cidadePesquisaLote+'&ordem_pesquisa_lote='+ordemPesquisaLote;	
   var parametro = 'tipo_pesquisa_lote='+tipoPesquisaLote+'&id='+id+'&descricao_pesquisa_lote='+descricaoPesquisaLote+'&cidade_pesquisa_lote='+cidadePesquisaLote+'&ordem_pesquisa_lote='+ordemPesquisaLote;
   var objAjaxLivros = new Ajax.Request( url,{ method: 'post', parameters: parametro, onComplete: showSearchLotByCategory });
   $('boxLots').innerHTML = "<div id=boxLoadingSearch>Carregando, aguarde...</div>";
}

function showSearchLotByCategory(resposta){
   document.getElementById('boxLots').innerHTML = resposta.responseText;
}

function clearFilterSearchLotsAuction(){
   var idLeilao = $('id_leilao').value;
   var qtdCategory = $('qtd_category').value;
   for (i=1;i<=qtdCategory;i++){
      document.getElementById('btnCategory_'+i).className = "bt_lot_category_off";
   }
   document.getElementById('cidade_pesquisa_lote').options(0).selected = true;
   document.getElementById('ordem_pesquisa_lote_1').checked = true;
   document.getElementById('tipo_pesquisa_lote_1').checked = true;
   document.getElementById('descricao_pesquisa_lote').value = "";
   changeTipoPesquisaLote('D');
   
   var url = 'controller.php?action=searchLotBySearchType&tipo_pesquisa_lote=D&id='+idLeilao;	
   var parametro = 'tipo_pesquisa_lote=D&id='+idLeilao;
   var objAjaxLivros = new Ajax.Request( url,{ method: 'post', parameters: parametro, onComplete: showSearchLotByCategory });
   $('boxLots').innerHTML = "<div id=boxLoadingSearch>Carregando, aguarde...</div>";
}

function changeTipoPesquisaLote(value){
   if (value=="D") {
      document.getElementById('searchLotByDescription').style.display = "block";
      document.getElementById('searchLotByLocation').style.display = "none";
   }
   if (value=="L") {
      document.getElementById('searchLotByDescription').style.display = "none";
      document.getElementById('searchLotByLocation').style.display = "block";
   }
}

