function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}


function cambia_sesion_info(id){
	parent.ajaxload("sesion_info","_ajax/sesion_info.php?id="+id+"");
}

function quita_favorito(id){
/*	if (confirm("Seguro quiere quitar la propiedad de sus favoritos?")){*/	
	if(document.getElementById("listado_favoritos")){
			var d = document.getElementById("listado_favoritos");
			var olddiv = document.getElementById("aviso_"+id+"");
			olddiv.innerHTML="";
			//d.removeChild(olddiv);
	
		}else{
			var divid = document.getElementById("favorito_"+id+"");
			divid.innerHTML="";
		
			var ahref= document.createElement("a");
			ahref.href="./usuarios/agregar-favoritos/"+id+"";
			ahref.title="Agregar a Favoritos";
			ahref.rel="favoritos";
			ahref.target="ifpopup";
			ahref.innerHTML="Agregar a Favoritos";
		
			var span = document.createElement("span");
			span.setAttribute("class","favoritos");
			
			span.appendChild(ahref);
			divid.appendChild(span);
		}
/*	}*/
}


function agrega_favorito(id){
	var divid = document.getElementById("favorito_"+id+"");
	divid.innerHTML="";

	var ahref= document.createElement("a");
	ahref.href="./usuarios/quitar-favoritos/"+id+"";
	ahref.title="Agregar a Favoritos";
	ahref.rel="favoritos";
	ahref.target="ifpopup";
	ahref.innerHTML="Quitar de Favoritos";

	var span = document.createElement("span");
	span.setAttribute("class","favoritos");
	
	span.appendChild(ahref);
	divid.appendChild(span);
}



function renovar(id,publicado){
	var faja=document.getElementById("faja_"+id);
	if(publicado==0){
		faja.src="img/faja_no_publicado.gif";
		faja.alt="Aviso No Publicado";
	}else{
		faja.parentNode.removeChild(faja);
	}
	var span=document.getElementById("renovar_"+id);
	span.parentNode.removeChild(span);
}

function ocultar(id,renovable){
	if(!document.getElementById("faja_"+id)){
		var divid = document.getElementById("portafaja_"+id+"");
		divid.innerHTML="";
		var faja= document.createElement("img");
		faja.id="faja_"+id;
		faja.setAttribute("class","faja");
		faja.src="img/faja_no_publicado.gif";
		faja.alt="Aviso No Publicado";
		divid.appendChild(faja);
	}else{
		var faja=document.getElementById("faja_"+id);
	}
	if(renovable==1){
		faja.src="img/faja_vencido.gif";
		faja.alt="Aviso Vencido";
	}else if(renovable==2){
		faja.src="img/faja_renovable.gif";
		faja.alt="Aviso Renovable";
	}
	document.getElementById("a_publicar_"+id).innerHTML="Publicar";
	document.getElementById("a_publicar_"+id).titlo="Publicar este Aviso";
}
function publicar(id,renovable){
	if(!document.getElementById("faja_"+id)){
		var divid = document.getElementById("portafaja_"+id+"");
		divid.innerHTML="";
		var faja= document.createElement("img");
		faja.id="faja_"+id;
		faja.setAttribute("class","faja");
		divid.appendChild(faja);
	}else{
		var faja=document.getElementById("faja_"+id);
	}
	if(renovable==1){
		faja.src="img/faja_vencido.gif";
		faja.alt="Aviso Vencido";
	}else if(renovable==2){
		faja.src="img/faja_renovable.gif";
		faja.alt="Aviso Renovable";
	}else{
		faja.parentNode.removeChild(faja);
	}
	document.getElementById("a_publicar_"+id).innerHTML="Ocultar";
	document.getElementById("a_publicar_"+id).titlo="Ocultar este Aviso";
}

function renovar_clasificado(id){
	var faja=document.getElementById("faja_"+id);
	faja.parentNode.removeChild(faja);
	var span=document.getElementById("renovar_"+id);
	span.parentNode.removeChild(span);
}

function eliminar(tipo,id){
	if (confirm("Seguro quiere eliminar el item seleccionado?")){
	  location.href=("usuarios/"+tipo+"_eliminar.php?del="+id);
	 }
}


function set_hijo(padre,hijo,sufijo){
	if(document.getElementById("box_agregar_"+hijo+sufijo+"")){
		var presel=document.getElementById(hijo+sufijo).value;
		if(padre.value!=0){
			document.getElementById("box_agregar_"+hijo+sufijo+"").style.display="block";
			document.getElementById("agregar_"+hijo+sufijo+"").innerHTML="agregar "+hijo+" en "+padre.options[padre.selectedIndex].text;
			document.getElementById("agregar_"+hijo+sufijo+"").href="extras/agregar-"+hijo+"/?superior="+padre.value+"&campo="+hijo+sufijo;
	
		}else{
			document.getElementById("box_agregar_"+hijo+sufijo+"").style.display="none";
		}
	}
	ajaxload("sel_"+hijo+sufijo+"","_ajax/select_"+hijo+".php?superior="+padre.value+"&presel="+presel+"&sufijo="+sufijo+"");
}


function select_provincia(padre,sufijo){
	var hijo="localidad";
	set_hijo(padre,hijo,sufijo);
	if(document.getElementById("barrio"+sufijo+"")){document.getElementById("barrio"+sufijo+"").innerHTML="";}
	if(document.getElementById("box_agregar_barrio"+sufijo+"")){document.getElementById("box_agregar_barrio"+sufijo+"").style.display="none";}
	
}
/*
function ajax_waiter() {
	if (ajax_listo==0) {
		setTimeout(ajax_waiter, 100);
		return;
	} else {
		return
	}
}
*/
function select_localidad(padre,sufijo){
	//si hay barrio
	if(document.getElementById("sel_barrio"+sufijo+"")){
		var hijo="barrio";
		if(padre.value!=0&&document.getElementById("map_canvas")){
			afterload="gmap_relocate();";
		}
		set_hijo(padre,hijo,sufijo);
	}
}

function select_tipo(tipo_id){
	afterload="select_operacion(document.getElementById('operacion'));"
	ajaxload("form_contextual","_ajax/form_contextual.php?tipo="+tipo_id+"");
}

function select_operacion(operacion){
	//habilitar o deshabilitar campos
	if(operacion.value==3){
		document.getElementById("periodo").style.visibility="visible";
		if(document.getElementById("box-capacidad")){document.getElementById("box-capacidad").style.display="block";}
	}else{
		document.getElementById("periodo").style.visibility="hidden";
		if(document.getElementById("box-capacidad")){
			document.getElementById("box-capacidad").style.display="none";
			document.getElementById("cantidad_capacidad").value=0;
		}
	}
	
}

function select_rubro(padre,sufijo){
	if(document.getElementById("sel_subrubro"+sufijo+"")){
		var hijo="subrubro";
		set_hijo(padre,hijo,sufijo);
	}
}




function agregar_geo_fin(tipo,presel,superior,campo){
	var padre=document.getElementById(campo);
	if(tipo==1){
		var tipo_geo="localidad";var hijo="barrio";
		afterload="agregar_geo_fin_2('"+sufijo+"','"+campo+"');";
	}
	if(tipo==2){var tipo_geo="barrio";var hijo="";}
	
	var sufijo="_id";
	/*if(document.getElementById("box_agregar_"+hijo+sufijo+"")){
		if(padre.value!=0){
			document.getElementById("box_agregar_"+hijo+sufijo+"").style.display="block";
			document.getElementById("agregar_"+hijo+sufijo+"").innerHTML="agregar "+hijo+"";
			document.getElementById("agregar_"+hijo+sufijo+"").href="extras/agregar-"+hijo+"/?superior="+presel+"&campo="+hijo+sufijo;
	
		}else{
			document.getElementById("box_agregar_"+hijo+sufijo+"").style.display="none";
		}
	}*/
	ajaxload("sel_"+tipo_geo+"_id","_ajax/select_"+tipo_geo+".php?superior="+superior+"&sufijo="+sufijo+"&presel="+presel+"");
	fauxpopup();
}

function agregar_geo_fin_2(sufijo,campo){
	var padre=document.getElementById(campo);
	select_localidad(padre,sufijo)
	
}


function agregar_tipo_fin(presel,superior){
	afterload="select_tipo("+presel+");"
	ajaxload("sel_tipo_id","_ajax/select_tipo.php?presel="+presel+"")
	fauxpopup();

}

function agregar_subrubro_fin(presel,superior){
	ajaxload("sel_subrubro_id","_ajax/select_subrubro.php?superior="+superior+"&presel="+presel+"")
	fauxpopup();
}



function agregar_img_fin(img_id,imagen){
	var a_img=parent.document.getElementById("imagen_"+img_id+"");
	a_img.href="imagenes/"+imagen+"";
	a_img.innerHTML="<img src='resized/80x60/"+imagen+"'/>";
	parent.fauxpopup();
}

function agregar_logo_fin(img_id,imagen){
	var span_img=parent.document.getElementById("imagen_"+img_id+"");
	span_img.innerHTML="<img src='resized/80x60/"+imagen+"'/>";
	parent.fauxpopup();
}

function iniciar_sesion_fin(){
	ajaxload("sesion_info","_ajax/sesion_info.php")
}


function limite_texto(textarea, resto, maximo){
	if (textarea.value.length > maximo) {
		textarea.value = textarea.value.substring(0, maximo);
	} else {
		resto.innerHTML = maximo - textarea.value.length;
	}
}



function fauxpopup(ancho,alto){
	if(document.getElementById("capa").style.display=="block"){
		document.getElementById("fauxpp").innerHTML="<iframe width='400' height='200' class='iframe' frameborder='0' scrolling='no' id='ifpopup' name='ifpopup'></iframe>"
		document.getElementById("capa").style.display="none";
		document.getElementById("fauxpp").style.display="none";
		document.getElementById("fauxpp").style.width="400px";
		document.getElementById("fauxpp").style.height="200px";
		var arr_pagesize=getPageSize();
		var left=(arr_pagesize[0]-400)/2;
		document.getElementById("fauxpp").style.left=left+"px";
		var oSelects=document.getElementsByTagName("select");
		for(var i=0;i<oSelects.length;i++){
			oSelects[i].className=oSelects[i].className.replace(" selHide","");
			oSelects[i].className=oSelects[i].className.replace("selHide","");
		}
	}else{
		var arr_pagesize=getPageSize();
		var arr_pagescroll=getPageScroll();
		var top=arr_pagescroll[1]+50;
		document.getElementById("fauxpp").style.top=top+"px";
		document.getElementById("capa").style.height=arr_pagesize[1]+"px";
		
		document.getElementById("capa").style.display="block";
		document.getElementById("fauxpp").style.display="block";
		
		var oSelects=document.getElementsByTagName("select");
		for(var i=0;i<oSelects.length;i++){
			oSelects[i].className+=" selHide";
		}
	}
}

function ajaxnoload(url) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
	  alert ("Su navegador no soporta AJAX!");
	  return;
	 } 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

var divid="";
var afterload="";

function ajaxload(caja,url) {
	//afterload=postfunction;
	divid=caja;
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
	  alert ("Su navegador no soporta AJAX!");
	  return;
	 } 
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
	
function GetXmlHttpObject() {
  var xmlHttp=null;
  try{// Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
   }
  catch (e){ // Internet Explorer
    try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
    catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
   }
  return xmlHttp;
}

function stateChanged() {
/*
	if (xmlHttp.readyState==1){//request seteado
		document.getElementById(divid).innerHTML="<p>cargando...</p>";
	}
	if (xmlHttp.readyState==2){//request enviado
		document.getElementById(divid).innerHTML="<p>cargando...</p>";
	}
	if (xmlHttp.readyState==3){//requesten proceso
		document.getElementById(divid).innerHTML="<p>cargando...</p>";
	}
*/
	if (xmlHttp.readyState==4){//request completo
		document.getElementById(divid).innerHTML=xmlHttp.responseText;
		eval(afterload);
		afterload="";
	}
}

function getPageScroll(){
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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;
	}
	var windowWidth, windowHeight;
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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;
	}
//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

