req = new Array();

function insertarPoblacion(id, poblacion) {

	if (poblacion.length == 5) {
		getAjax('gestion/include/ajax.php', 'cp='+poblacion, 'out_ajax', id+'_loading');
	}
}

function getAjax(url, valores, elemento, cargando) {

	 //alert(valores);

	 try {
     req[elemento] = new XMLHttpRequest(); /* e.g. Firefox */
     } catch(e) {
       try {
       req[elemento] = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
       } catch (e) {
         try {
         req[elemento] = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
         } catch (E) {
          req[elemento] = false;
         }
       }
     }

	 document.getElementById(cargando).style.visibility="visible";
     errorTxt = "Ocurrio un error";

     req[elemento].open("POST",url,true);
     req[elemento].onreadystatechange = function() {respuestaContenido(elemento, errorTxt, cargando);};
     req[elemento].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
     req[elemento].send(valores);
}


function respuestaContenido(elemento, errorTxt, cargando) {
   var output = '';
   var codigo_pre = new Array();
   var codigo = new Array();

   if(req[elemento].readyState == 4) {
   	  if(req[elemento].status == 200) {
         output = req[elemento].responseText;
         document.getElementById(cargando).style.visibility="hidden";
		 document.getElementById(elemento).innerHTML=output;
		 try {
			 codigo_pre = req[elemento].responseText.split('<!-- JSX');
		 	 if(typeof(codigo_pre[1])!='undefined') codigo = codigo_pre[1].split('-->');
			 if(typeof(codigo[0])!='undefined') eval(codigo[0]);
		 } catch (e) { }
      }
   }
}

function validar_form_login() {
	login = document.getElementById('login').value;
	pass = document.getElementById('passwd').value;

	if (login.length != 0 && pass.length != 0)
		document.getElementById('flogin').submit();
	else {
			alert("<?php echo _('Por favor no te dejes ningun campo por rellenar!');?>");
			return false;
	}
}

function cat_op(elemento, id, op) {

	var val = (elemento.checked) ? 1 : 0;
	//alert('id='+id+'&'+op+'='+val);
	getAjax('modules/catalogo/modificar_precio.php', op+'_q=1&id='+id+'&'+op+'='+val , id, 'load_'+id);
}

function setImagenPrimeraCatalogo(id, producto, imagen) {
	getAjax('modules/catalogo/modificar_precio.php', 'setprim=1&imagen='+imagen+'&producto='+producto, id, 'load_'+id);
}

function setImagenPrimeraInmuebles(id, producto, imagen) {
	getAjax('modules/catalogo/modificar_precio.php', 'setpriminm=1&imagen='+imagen+'&producto='+producto, id, 'load_'+id);
}

function tempResetPlantillas(tiempo) {
	setTimeout('resetSelectorPlantillas();', (tiempo*1000))
}

function resetSelectorPlantillas() {
	var capa = document.getElementById('capa_seleccion');
	capa.style.visibility="hidden";
	document.getElementById('enviarPlantillaPedidoId').value = 0;
}

function enviar_plantilla(num) {
	var pedido = document.getElementById('enviarPlantillaPedidoId').value;
	location.href="panel.php?opcion=226&id_pedido="+pedido+"&plantilla="+num;
}


function selectorPlantillas(elemento, id_ped) {

	var capa = document.getElementById('capa_seleccion');
	var ancho_capa = capa.offsetWidth;
	var ancho_elemento = elemento.offsetWidth;

	var posicionTop = obtenerPosicion(elemento).top;
	var posicionLeft = obtenerPosicion(elemento).left;

	capa.style.top = (posicionTop+15) +"px";
	capa.style.left = (posicionLeft-(ancho_capa/2)) + (ancho_elemento/2) +"px";

	capa.style.visibility="visible";
	
	document.getElementById('enviarPlantillaPedidoId').value = id_ped;
	
	tempResetPlantillas(6);
}

function obtenerPosicion(element) {

  if (!element) return { top:0,left:0 };

  var y = 0;
  var x = 0;
  while (element.offsetParent) {
    x += element.offsetLeft;
    y += element.offsetTop;
    element = element.offsetParent;
  }
  return {top:y,left:x};
}

