// JavaScript Document
var NS4 = (navigator.appName.indexOf("Netscape")>=0 && !document.getElementById)? true : false;
var IE4 = (document.all && !document.getElementById)? true : false;
var IE5 = (document.getElementById && document.all)? true : false;
var NS6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
var W3C = (document.getElementById)? true : false;

var TIEMPO=1;
var INTERVALO=4;
var descM=null;

var CURSOR=NS6?'pointer':'hand';

function mostrar(cual, accion) {
	document.getElementById(cual).style.visibility=accion;
}

// Control de tecla pulsada.
function pulsada(e) {
  var correctos=new Array("0","1","2","3","4","5","6","7","8","9",",");
      estado=false
          if(document.all) {
              codigoTecla = event.keyCode
              cadenaTecla = (String.fromCharCode(event.keyCode));
          } else if(document.layers) {
              codigoTecla = e.which
              cadenaTecla = String.fromCharCode(e.which);
          } else if(document.getElementById) {
              codigoTecla = (window.Event) ? e.which : e.keyCode;
              cadenaTecla=(String.fromCharCode(codigoTecla));
          }
	if((cadenaTecla==',') && (decimal==0))
		   return false;
          for(i=0;i<correctos.length;i++)  {
              if(cadenaTecla==correctos[i])
                  estado=true;
          }
  if((codigoTecla==8) || (codigoTecla==0))
	  estado=true;
          if(estado==false) {
              if(document.all)
                  event.returnValue = false;
              else
                  return false;
          }
}

function cadenaAnumero(cual) {
   var temporal=cual;
   
var vector=temporal.split(",");
if(vector.length>2) {
	return 0;
	}
if(vector.length==1)
	temporal=vector[0];
else
	temporal=vector[0]+"."+vector[1];
temporal=parseFloat(temporal);
if(isNaN(temporal))	
	temporal=0;
return temporal;

}

function numeroAcadena(cual) {
var resultado=cual.toString();
vector=resultado.split(".");
if(vector.length>1) {
   decimal=vector[1];
   if(decimal.length>2) {
	   decimal=decimal.substring(0,2)+"."+decimal.substring(2,decimal.length-2);
   		decimal=Math.round(decimal);  
	} else
		if(decimal.length<2)
			decimal+="0";
} else
	decimal="00";

	return (vector[0]+","+decimal);
}

function recalcular(objeto) {

var base=objeto.ecoPropietario.value;
var margen=objeto.margen.value;
var fijo=objeto.fijo.value;
var total=0;

base=cadenaAnumero(base);
margen=cadenaAnumero(margen);
fijo=cadenaAnumero(fijo);

total=base*margen/100+fijo;

objeto.totalComision.value=numeroAcadena(total);

}

function reformatea(valor) {
	var temporal=valor;
	var vector=temporal.split(",");
	if(temporal.length==0) {
		return ("0,00");
		}
	if(vector.length==1)
		return (temporal+",00");
	if(vector[1].length==1)
		return (vector[0]+","+vector[1]+"0");
	if(vector.length==2)
		return valor;	
	redondeo=parseFloat("0."+vector[1])*100;
	redondeo=Math.round(redondeo);
	return (vector[0]+","+redondeo);
}

function desplaza(objeto, sentido) {
	var anterior=parseInt(document.getElementById(objeto).scrollLeft);

	document.getElementById(objeto).scrollLeft=anterior+sentido;
	descM=setTimeout("desplaza('"+objeto+"',"+sentido+");",TIEMPO*100);

}

		
TRANSICION=250;
SALTOS=10;
DEGRADAR=0;
var parada=null;
var intervalo=null;
var numImagen=1;
var degradado=100;
var ahora=0;
var InterSet=null;

function valorInicial() {
	clearInterval(InterSet);
	numImagen=1;
	ahora=0;

   var elAhora=document.getElementById('imagen0');
   var elNuevo=document.getElementById('imagen1');

   elAhora.innerHTML="<img id=\"iCat\" name=\"iCat\" src=\"images/ciclico/00.jpg\" width=\"106\" height=\"120\"  class=\"grande\" />";
   elNuevo.innerHTML="<img src=\"images/ciclico/01.jpg\" width=\"106\" height=\"120\"  class=\"grande\" />";
   elAhora.style.zIndex=81;
   elNuevo.style.zIndex=80;
   	if(elAhora.filters) {
		elAhora.filters.alpha.opacity=100;
	} else if (elAhora.style.MozOpacity)
		elAhora.style.MozOpacity=1;
		else
		elAhora.style.opacity=1;
   	if(elNuevo.filters) {
		elNuevo.filters.alpha.opacity=100;
	} else if (elNuevo.style.MozOpacity)
		elNuevo.style.MozOpacity=1;
		else
		elNuevo.style.opacity=1;   
}

var transicion=new Array();

function precarga() {
	var i=0;
	
	for(i=0;i<imagenes.length;i++) {
		transicion[i]=new Image();
		transicion[i].src=imagenes[i];
	}
}

function cambioImagen(actual, imagen, id) {
   var elAhora=document.getElementById('imagen'+actual);
   var nuevo=actual==0?1:0;
   DEGRADAR=nuevo;
   var elNuevo=document.getElementById('imagen'+nuevo);
   elNuevo.innerHTML='<a href="javascript:ampliar(\'grande.php?id='+id+'\');" title="Imagen con detalle"><img name=\'iCat\' id=\'iCat\' src="'+transicion[imagen].src+'" height=\"290\" class=\"grande\" /></a>';   
//   elAhora.style.zIndex=2;
//   elNuevo.style.zIndex=5;
   	if(elAhora.filters) {
		elAhora.filters.alpha.opacity=100;
//		alert('Cambio de opacity'+elNuevo.filters.alpha.opacity)
	} else if (elAhora.style.MozOpacity)
			elAhora.style.MozOpacity=1;
		else
			elAhora.style.opacity=1;

//   status=actual+'   '+elAhora.innerHTML;
parada=null;
//pausa(nuevo);
//alert(actual+elNuevo.innerHTML);
   comenzarTransicion(actual);
}

function degradar(objetivo, imagen) {
	var elAhora=document.getElementById('imagen'+objetivo);
	var nuevo=objetivo==0?1:0;
	var elViejo=document.getElementById('imagen'+nuevo);
	if (elAhora.filters) {
		elViejo.filters.alpha.opacity=100-degradado;
//		status=degradado+" objetivo: "+objetivo;
		elAhora.filters.alpha.opacity=degradado;
	} else if (elAhora.style.MozOpacity){
			elAhora.style.MozOpacity=degradado/100;
			elViejo.style.MozOpacity=1-(degradado/100);
		}else{
			elAhora.style.opacity=degradado/100;
			elViejo.style.opacity=1-(degradado/100);
		}
	degradado-=SALTOS;
	if(degradado<=0) {
		clearInterval(InterSet);
		degradado=100;
	    	var elNuevo=document.getElementById('imagen'+nuevo);
		var elViejo=document.getElementById('imagen'+objetivo);
		if (elNuevo.filters) {
//		status=degradado+" objetivo: "+objetivo;
			elNuevo.filters.alpha.opacity=degradado;
			elViejo.filters.alpha.opacity=0;
		} else if (elNuevo.style.MozOpacity){
				elNuevo.style.MozOpacity=degradado;
				elViejo.style.MozOpacity=0;
			}else{
				elNuevo.style.opacity=degradado;
				elViejo.style.opacity=0;
			}

//		cambioImagen(objetivo, imagen);
		}
}

function comenzarTransicion(cual){
//	var elAhora=document.getElementById('imagen'+cual);
	InterSet=setInterval('degradar('+cual+')',TRANSICION);
}

function pausa(cual) {
if(parada==null) {
	parada=1;
	clearInterval(InterSet);
	setTimeout('pausa('+cual+')',1000);
	} else
	comenzarTransicion(cual);
}

function extraeID(cual) {
//	var ejemplo='/inmobiliaria/imagen.php?tabla=fotosconstruccion&tamano=grande&id=26';
	var traba=imagenes[cual].split('?');
//	var traba=ejemplo.split('?');
	var retraba=traba[1].split('&');
	var encontrado=false;
	var indice=0, puesto=-1;
	while((!encontrado || encontrado==-1) && indice<retraba.length) {
		if(retraba[indice].indexOf('id=')>-1){
			puesto=indice;
			encontrado=true;
		}
		indice++;
	}
	if(encontrado) {
		var divide=retraba[puesto].split('=');
		return divide[1];
	}
	return -1;
}

function intercambia(imagen) {
	clearInterval(InterSet);	
	cambioImagen(DEGRADAR,imagen,extraeID(imagen));
}


var ventana;
var contador=0;
var titulopordefecto = "Imagen ampliada";
function ampliar(cual,titulo){
	if(contador==1)	{
		ventana.close();
		ventana=null;
	}
	if(titulo==null){
		titulo=titulopordefecto;
	}
	ventana=window.open(cual,'ventana','resizable=no,scrollbars=no,status=no,directories=no,location=no,toolbar=0,location=0,status=0,menubar=0');
	/*
ventana.document.write('<html><head><title>' + titulo + '</title></head><body style="overflow:auto" marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" scroll="no" onUnload="opener.contador=0"><img src="' + cual + '" onLoad="opener.redimensionar(this.width, this.height)">');
	ventana.document.close();
	*/
	redimensionar(400,500);
	contador++;
}
function redimensionar(ancho,alto){
	ventana.resizeTo(ancho+12,alto+28);
	ventana.moveTo((screen.width-ancho)/2,(screen.height-alto)/2); //centra la ventana. Eliminar si no se quiere centrar el popup
}

// ---------- ejemploCapa
        var permisoN4=0;
        var leftN4;
        var topN4;
        var idCapa;
        var idCapax;
        var idCapay;
		var iex;
		var iey;
        var objeto=null;
        /**
        * funciones para manejar eventos de raton en Nestcape 4x
        */      

        function capturaN4(e)
        {
            idCapa.captureEvents(Event.MOUSEMOVE)
            leftN4=e.x
            topN4=e.y
        }               
        /**
        * funciones para arrastrar la capa en Nestcape 4x
        */
        function arrastraN4(e)
        {
            if (permisoN4==1)
            {
                idCapa.moveBy(e.x-leftN4,e.y-topN4)
                return false
            }
        }
        /**
        * funcion para detener la captura de los eventos de raton
        */
        function paraN4()
        {
            idCapa.releaseEvents(Event.MOUSEMOVE)
        }


        /**
        * funciones y variables para manejar eventos de raton en Internet Explorer y mover la capa
        */

        var permisoIE=false

        function arrastraIE(id)
        {
		if(objeto==null) return true;
            if (permisoIE==true)
            {
			window.status="id: "+id+"  "+document.all[id].style.pixelLeft+"  iex: "+iex;
                document.all[id].style.pixelLeft=idCapax+event.clientX-iex
                document.all[id].style.pixelTop=idCapay+event.clientY-iey
                return false
            }
        }
       
       
        var permisoN6=false

        function arrastraN6(e)
        {
            if (permisoN6==true)
            {
                idCapa.style.left=idCapax+e.clientX-n6x+"px"
                idCapa.style.top=idCapay+e.clientY-n6y+"px"
                return false
            }
        }       
        function paramN6(e)
        {
                n6x=e.clientX ;
                n6y=e.clientY;
                idCapa.addEventListener("mousemove", arrastraN6, false);                
        }

        function inicializa(id)
        {
            if(document.all)
            {
                iex=event.clientX;
                iey=event.clientY;
				idCapax=document.all[id].style.pixelLeft;
                idCapay=document.all[id].style.pixelTop;
                permisoIE=true;
                arrastraIE(id);
            } else if(document.getElementById)
            {
                idCapa = document.getElementById(id);
                idCapa.addEventListener("mousedown", paramN6, false);               
                idCapax=parseInt(document.getElementById(id).style.left);        
                idCapay=parseInt(document.getElementById(id).style.top);
                permisoN6=true
            }          
        }       
      
// --- fin ejemploCapa
