function objetoAjax(){
        var xmlhttp=false;
        try {
               xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
               try {
                  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
               } catch (E) {
                       xmlhttp = false;
               }
        }
 
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
               xmlhttp = new XMLHttpRequest();

        }

        return xmlhttp;

}

 

function consulta(tipo,valor,div,id_galeria){
			if(valor==""){
				document.getElementById('seleccion').style.display= "none";
				document.getElementById('upload').innerHTML= "";
				document.getElementById('dos').style.display= "none";
				document.getElementById('div_select').style.display= "none";
				relaciones("afuera");
			}
		if (tipo=="galeria"){
			relaciones("afuera");
			}
        divResultado = document.getElementById(div);
        divResultado.style.display="block";
        ajax=objetoAjax();
        ajax.open("GET", "consultas.php?tipo="+tipo+"&valor="+valor+"&galeria="+id_galeria, true);
        ajax.onreadystatechange=function() {
               if (ajax.readyState==4) {
                       divResultado.innerHTML = ajax.responseText;
               }
        }
        ajax.send(null)
}

/* agregar una funcion de consulta de novedades, y otra de novedades y estaria todo listo! */

function seleccion(valor,texto,seccion){
	if(valor!="" && valor!="add"){
		document.getElementById('seleccion').style.display= "none";
		document.getElementById('seleccion').innerHTML= "Galeria seleccionada: "+ texto;
		$("#seleccion").fadeIn(1000);
		document.getElementById('dos').style.display= "block";
		document.getElementById('upload').innerHTML= ""
		addField();
	}
	else if(valor=="add"){
		document.getElementById('seleccion').style.display= "none";
		document.getElementById('seleccion').innerHTML= "<form name='ingreso' action='' onsubmit='ingresar(\""+seccion+"\"); return false'><label>Nombre del evento: <input type='text' id='evento' name='evento'></label><input type='submit' value='Ingresar'/></form>";
		$("#seleccion").fadeIn(1000);
		document.getElementById('dos').style.display= "none";
		document.getElementById('upload').innerHTML= ""
	}
	else if(valor==""){
		document.getElementById('seleccion').style.display= "none";
		document.getElementById('upload').innerHTML= "";
		document.getElementById('dos').style.display= "none";
	}
}	

function ingresar(seccion){
	    divResultado = document.getElementById("seleccion");
	    evento = document.getElementById("evento").value;
	    ajax=objetoAjax();
	    ajax.open("POST", "acciones.php", true);
	    ajax.onreadystatechange=function() {
	           if (ajax.readyState==4) {
	                   divResultado.innerHTML = ajax.responseText;
	                   document.getElementById("select").value="";
	                   document.getElementById("seccion_select").value="";
	                   
	           }
	    }
	    ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	    ajax.send("acc=in&seccion="+seccion+"&evento="+evento);
}

function relaciones(valor){
	if (valor=="adentro"){	
	$("#tres").fadeIn(2000);
	$("#novedades").fadeIn(2000);
	$("#calendario").fadeIn(2000);
	$("#cuatro").fadeIn(2000);
	document.getElementById('div_mes_nove').style.display= "block";
	document.getElementById('div_mes_calen').style.display= "block";
	} else if(valor=="afuera"){
	$("#tres").fadeOut(1000);
	$("#novedades").fadeOut(1000);
	$("#calendario").fadeOut(1000);
	$("#cuatro").fadeOut(1000);
	}
}

function hace_rel(id_elem,id_galeria,tipo){
	if (tipo==1){
		divResultado = document.getElementById("informa_nove");
      ajax=objetoAjax();
      ajax.open("GET", "acciones.php?acc=mod&tipo=nov&id_galeria="+id_galeria+"&id_elemen="+id_elem, true);
      ajax.onreadystatechange=function() {
      	if (ajax.readyState==4) {
      		divResultado.innerHTML = ajax.responseText;
      		document.getElementById('cont_nove').style.display= "none";
      		document.getElementById('div_mes_nove').style.display= "none";
      		document.getElementById('mes_nove').value= "";
      		setTimeout(function(){$("#novedades").fadeOut(1000);document.getElementById('informa_nove').innerHTML= "";},2000);
      	}
      }
		ajax.send(null)
	}
	else if (tipo==2){
		divResultado = document.getElementById("informa_calen");
      ajax=objetoAjax();
      ajax.open("GET", "acciones.php?acc=mod&tipo=cal&id_galeria="+id_galeria+"&id_elemen="+id_elem, true);
      ajax.onreadystatechange=function() {
      	if (ajax.readyState==4) {
      		divResultado.innerHTML = ajax.responseText;
      		document.getElementById('cont_calen').style.display= "none";
      		document.getElementById('div_mes_calen').style.display= "none";
      		document.getElementById('mes_calen').value= "";
      		setTimeout(function(){$("#calendario").fadeOut(1000);document.getElementById('informa_calen').innerHTML= "";},2000);
      	}
      }
		ajax.send(null)
	}
}

// FUNCIONES PARA AGREGAR ARCHIVOS. NO ES DE AJAX //
var numero = 0;

c= function (tag) { // Crea un elemento
return document.createElement(tag);
}
d = function (id) { // Retorna un elemento en base al id
return document.getElementById(id);
}
e = function (evt) { // Retorna el evento
return (!evt) ? event : evt;
}
f = function (evt) { // Retorna el objeto que genera el evento
return evt.srcElement ?  evt.srcElement : evt.target;
}

function cambia(id){
	document.getElementById(id).innerHTML = "se esta subiendo";
}

addField = function () {
	   container = d('upload');
	   ++numero;
	   
	   span = c('form');
	   span.className = 'form';
	   span.id = 'file' + numero;
	   span.method="post";
	   span.enctype="multipart/form-data";
	   span.action="upload.php";
	   span.target="iframeUpload" + numero;

	   field = c('INPUT');   
	   field.name = 'fotos';
	   field.type = 'file';
	   field.onchange = function(){addField();document.getElementById('uploader'+(numero-1)).innerHTML='<img src="fotos/barra.gif"/>'; document.getElementById('file' + (numero-1)).submit();relaciones("adentro");};
	   field.accept="image/jpg";
	   field.className = 'field';
	   
	   field2 = c('INPUT');   
	   field2.name = 'evento';
	   field2.type = 'hidden';
	   field2.value = document.getElementById('select').value;
	   
	   field3 = c('INPUT');   
	   field3.name = 'numero';
	   field3.type = 'hidden';
	   field3.value = numero;
	   
	   iframe = c('iframe');
	   iframe.className = 'iframe';
	   iframe.id="iframeUpload" + numero;
	   iframe.name="iframeUpload"+numero;
	   
	   subir = c('span');
	   subir.id= "uploader"+numero;
	   subir.className = 'upload';

	   span.appendChild(field);
	   span.appendChild(iframe);
	   span.appendChild(field2);
	   span.appendChild(field3);
	   span.appendChild(subir);
	   container.appendChild(span);
}
removeField = function (evt) {
	lnk = f(e(evt));
	span = d(lnk.name);
	span.parentNode.removeChild(span);
}
