var objeto = false;
var cadena;
var NombreSpan;

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function Capturar() {
  window.open('capturar.php','oportunidades','width=700,height=520, top=' + sacatop(520) + ',left=' + sacaleft(700) + ',scrollbars=yes, dependent=yes');
}

function Registro() {
  window.open('registro.php','oportunidades','width=795,height=607, top=' + sacatop(607) + ',left=' + sacaleft(795) + ',scrollbars=yes, dependent=yes');
}

function Accesar() {
  window.open('cuenta.php','oportunidades','width=795,height=607, top=' + sacatop(607) + ',left=' + sacaleft(795) + ',scrollbars=yes, dependent=yes');
}

function Colectiva(id) {
  try { document.getElementById("DivFormularioEmail").style.display = "none"; }
  catch (e) { }
  
  window.open('colectiva.php?ID=' + id,'oportunidades','width=685,height=520, top=' + sacatop(520) + ',left=' + sacaleft(685) + ',scrollbars=no, dependent=yes');
}

function Imprimir() {
  window.print();
}

function Salir() {
  location.href = "index.php?accion=salir";
}

function LeeCategorias(ElSelect,valor) {
  if (ElSelect == "2") {
	document.getElementById("TRCategoria2").style.display = "none";
    document.getElementById("SpanCategoria2").innerHTML = "";
	document.getElementById("TRCategoria3").style.display = "none";
    document.getElementById("SpanCategoria3").innerHTML = "";
  }
  if (ElSelect == "3") {
	document.getElementById("TRCategoria3").style.display = "none";
    document.getElementById("SpanCategoria3").innerHTML = "";
  }
  
  if (valor != 0) {
	NombreSpan = "Cargando" + (ElSelect - 1);
		
	cadena = "?accion=LeeCategoria&numero=" + ElSelect + "&valor=" + valor;
		
	LeeDatos();
  }
}

function trim(str) {
  return str.replace(/^\s*|\s*$/g,"");
}

function LeeCategoriasImpresa(ElSelect,valor) {
  if (ElSelect == "2") {
    document.getElementById("SpanCategoria2").innerHTML = "<select style='width:140px'><option></option></select>";
    document.getElementById("SpanCategoria3").innerHTML = "<select style='width:140px'><option></option></select>";
  }
  if (ElSelect == "31") {
    document.getElementById("SpanCategoria3").innerHTML = "<select style='width:140px'><option></option></select>";
  }
  
  if (valor != 0) {
	NombreSpan = "Cargando" + (ElSelect - 1);
		
	cadena = "?accion=LeeCategoriaImpresa&numero=" + ElSelect + "&valor=" + valor;
		
	LeeDatos();
  }
}


function LeeCiudades(valor) {
  //document.getElementById("TRCiudades").style.display = "none";
  //document.getElementById("SpanCiudades").innerHTML = "";
  
  if (valor != 0) {
	NombreSpan = "CargandoCiudades";
		
	cadena = "?accion=LeeCiudades&valor=" + valor;
	
	LeeDatos();
  }
}

function LeeCiudadesX(valor) {
  //document.getElementById("TRCiudades").style.display = "none";
  //document.getElementById("SpanCiudades").innerHTML = "";
  
  if (valor != 0) {
	NombreSpan = "CargandoCiudades";
		
	cadena = "?accion=LeeCiudadesX&valor=" + valor;
	
	LeeDatos();
  }
}

function LeeCiudadesMain(valor) {
  //document.getElementById("TRCiudades").style.display = "none";
  //document.getElementById("SpanCiudades").innerHTML = "";
  
  NombreSpan = "CargandoCiudades";

  cadena = "?accion=LeeCiudadesMain&valor=" + valor;
  
  LeeDatos();
}

function LeeDatos() {
  crearObjeto();
  
  if (objeto.readyState != 0) {
    alert('Error al crear el objeto XML. El Navegador no soporta AJAX');
  } else {
    // Preparar donde va a recibir el Resultado
    objeto.onreadystatechange = procesaResultado;
    // Enviar la consulta
    objeto.open("GET", "ajax.php" + cadena + "&random=" + Math.random(), true);
    objeto.send(null);
  }
}

function procesaResultado() {
  // Si aun esta revisando los datos...
  if (objeto.readyState == 1) {
	document.getElementById(NombreSpan).style.visibility = "visible";
  }
  
  // Si el estado es 4 significa que ya termino
  if (objeto.readyState == 4) {
	document.getElementById(NombreSpan).style.visibility = "hidden";
	
	datos = objeto.responseText;
	
	if (datos) {
	  separa = datos.split("\|");
	  	  
	  if (separa[0] != "-")	{
	    document.getElementById(separa[0]).style.display = "block";
	  }
      document.getElementById(separa[1]).innerHTML = separa[2];
	}
  } 
  
}

function crearObjeto() {
  // --- Crear el Objeto dependiendo los diferentes Navegadores y versiones ---
  try { objeto = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) {
    try { objeto = new ActiveXObject("Microsoft.XMLHTTP"); }
    catch (E) { objeto = false; }
  }
  
  // --- Si no se pudo crear... intentar este ultimo metodo ---
  if (!objeto && typeof XMLHttpRequest != 'undefined') { objeto = new XMLHttpRequest(); }
} 

function sacatop(Alto) {
  Medida_1 = ((screen.height / 2) - (Alto / 2));
  return Medida_1 - 30;
}

function sacaleft(Ancho) {
  Medida_1 = ((screen.width / 2) - (Ancho / 2));
  return Medida_1 - 10;
}

// Inicio - Para imagenes tipo botones

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3) {
    if ((x=MM_findObj(a[i]))!=null){
	  document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
    }
  }
}

// Fin - Para imagenes tipo botones

function ScrollY() {
	if (typeof window.pageYOffset != 'undefined') {
	  return window.pageYOffset - 17;
	} else {
	  return document.documentElement.scrollTop;
	}
}

function AnchoVentana() {
	if (FF()) {
		return window.innerWidth;
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		return document.documentElement.clientWidth;
	}
	else {
		return document.getElementsByTagName('body')[0].clientWidth;
	}
}
 
// Ver cuanto mide el Alto dentro de la ventana
// Sirve para por ejemplo ver si el navegador lo hisieron mas pequeño y asi ajustar ciertas cosas como DIV's
function AltoVentana() {
	if (FF()) {
		return window.innerHeight;
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		return document.documentElement.clientHeight;
	}
	else {
		return document.getElementsByTagName('body')[0].clientHeight;
	}
}

function FF() {
	if (typeof window.pageXOffset != 'undefined') {
		return true;
	} else {
		return false;
	}
}

function mostrar_espera() {
  el_div_espera_1.style.top = parseInt((parseInt(AltoVentana() / 2) - 100) + ScrollY()) + "px";
  el_div_espera_1.style.left = parseInt(parseInt(AnchoVentana() / 2) - 145) + "px";
  
  el_div_espera_1.style.visibility = "visible";
  
  el_div_espera_2.style.height = AltoVentana() + "px";
  el_div_espera_2.style.width = AnchoVentana() + "px";
   
  el_div_espera_2.style.display = "block";
  
  el_div_espera_2.style.top = ScrollY() + "px";
    
  window.onscroll = mostrar_espera_2;
  window.onresize = mostrar_espera_2;
}

function mostrar_espera_2() { 
  el_div_espera_1.style.top = parseInt((parseInt(AltoVentana() / 2) - 100) + ScrollY()) + "px";
  el_div_espera_1.style.left = parseInt(parseInt(AnchoVentana ()/ 2) - 145) + "px";
  
  el_div_espera_2.style.height = AltoVentana() + "px";
  el_div_espera_2.style.width = AnchoVentana() + "px";
  
  el_div_espera_2.style.top = ScrollY() + "px";
}


function ocultar_espera() {
  el_div_espera_1.style.visibility = "hidden";
  el_div_espera_2.style.display = "none";
  
  window.onscroll = null;
  window.onresize = null;
}

