// JavaScript Document

function irA(elemID) {//se situacion en un elemento en la pantalla en base al id
	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail) {
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined" && navigator.appName=="Microsoft Internet Explorer" ) {
		offsetLeft += parseInt(document.body.leftMargin);
		offsetTop += parseInt(document.body.topMargin);
	}
	window.scrollTo(offsetLeft,offsetTop);
}

function convierteFecha(fechaNormal){//pasa fecha a formato mysql
	dia=fechaNormal.substring(0,2);
	mes=fechaNormal.substring(3,5);
	ano=fechaNormal.substring(6,10);
	fechafinal=ano+"-"+mes+"-"+dia;
	
	return fechafinal;	
}
function quitaComillas(texto){//quita las comillas dobles de un texto por simples
	var texto2="";
	texto2=texto.replace(/"/g,"'");
	return texto2;
}

function guardaNoticia(op,id){
	var contenido="";
	if (document.getElementById("titulon").value!=""){
		envio="op="+op+"&id="+id+"&titulo="+escape(document.getElementById("titulon").value);
		if (document.getElementById("fecha").value!=""){
			envio+="&fecha="+convierteFecha(document.getElementById("fecha").value);			
			contenido=quitaComillas(tinyMCE.get('contenidon').getContent());
			envio+="&descripcion="+escape(contenido)+"&tipo="+document.getElementById('tipon').options[document.getElementById('tipon').selectedIndex].innerHTML+"&archivo1="+escape(document.getElementById('archivoad1').options[document.getElementById('archivoad1').selectedIndex].innerHTML)+"&archivo2="+escape(document.getElementById('archivoad2').options[document.getElementById('archivoad2').selectedIndex].innerHTML)+"&archivo3="+escape(document.getElementById('archivoad3').options[document.getElementById('archivoad3').selectedIndex].innerHTML)+"&archivo4="+escape(document.getElementById('archivoad4').options[document.getElementById('archivoad4').selectedIndex].innerHTML)+"&archivo5="+escape(document.getElementById('archivoad5').options[document.getElementById('archivoad5').selectedIndex].innerHTML);
			oXML = AJAXCrearObjeto();
			oXML.open('POST','adminopt.php',true);
			oXML.onreadystatechange = function() {
				if (oXML.readyState == 1){
					document.getElementById("divnoticias").innerHTML="Guardando...";
				}else{
					if (oXML.readyState == 4){							
						document.getElementById("divnoticias").innerHTML=oXML.responseText;
						cancela();						
					}
				}
			}
			oXML.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			oXML.send(envio);			
		}else{
			alert("Selecciona la fecha");
		}
	}else{
		alert("Rellena el titulo");
	}
}

function cancela(){
	document.getElementById("titulon").value="";
	document.getElementById("fecha").value="";	
	tinyMCE.get('contenidon').setContent("");
	document.getElementById("divb1").innerHTML="<input type='button' value='Guardar' onclick='guardaNoticia(1,0)' />";
	document.getElementById("cabeceranoticia").value="<b>Nueva noticia/evento:</b>";
	var combo2 = document.getElementById("archivoad1");
   	var cantidad2 = combo2.length;
   	for (i = 0; i < cantidad2; i++) {
      	if (combo2[i].value == "Ninguno") {
         	combo2[i].selected = true;
      	}   
  	}
	var combo2 = document.getElementById("archivoad2");
   	var cantidad2 = combo2.length;
   	for (i = 0; i < cantidad2; i++) {
      	if (combo2[i].value == "Ninguno") {
         	combo2[i].selected = true;
      	}   
  	}
	var combo2 = document.getElementById("archivoad3");
   	var cantidad2 = combo2.length;
   	for (i = 0; i < cantidad2; i++) {
      	if (combo2[i].value == "Ninguno") {
         	combo2[i].selected = true;
      	}   
  	}
	var combo2 = document.getElementById("archivoad4");
   	var cantidad2 = combo2.length;
   	for (i = 0; i < cantidad2; i++) {
      	if (combo2[i].value == "Ninguno") {
         	combo2[i].selected = true;
      	}   
  	}
	var combo2 = document.getElementById("archivoad5");
   	var cantidad2 = combo2.length;
   	for (i = 0; i < cantidad2; i++) {
      	if (combo2[i].value == "Ninguno") {
         	combo2[i].selected = true;
      	}   
  	}
}

function editaNoticia(titulo,fecha,tipo,archivo1,archivo2,archivo3,archivo4,archivo5,id){
	irA("cabeceranoticia");
	document.getElementById("divb1").innerHTML="<input type='button' value='Editar' onclick='guardaNoticia(2,"+id+")' /><input type='button' value='Cancelar' onclick='cancela()' />";
	document.getElementById("cabeceranoticia").innerHTML="<b>Editar anuncio:</b>";	
	document.getElementById("titulon").value=titulo;
	document.getElementById("fecha").value=fecha;
	tinyMCE.get('contenidon').setContent(document.getElementById("ncontenido"+id).value);
	var combo = document.getElementById("tipon");
   	var cantidad = combo.length;
   	for (i = 0; i < cantidad; i++) {
      	if (combo[i].value == tipo) {
         	combo[i].selected = true;
      	}   
  	}
	var combo2 = document.getElementById("archivoad1");
   	var cantidad2 = combo2.length;
   	for (i = 0; i < cantidad2; i++) {
      	if (combo2[i].value == archivo1) {
         	combo2[i].selected = true;
      	}   
  	}
	var combo2 = document.getElementById("archivoad2");
   	var cantidad2 = combo2.length;
   	for (i = 0; i < cantidad2; i++) {
      	if (combo2[i].value == archivo2) {
         	combo2[i].selected = true;
      	}   
  	}
	var combo2 = document.getElementById("archivoad3");
   	var cantidad2 = combo2.length;
   	for (i = 0; i < cantidad2; i++) {
      	if (combo2[i].value == archivo3) {
         	combo2[i].selected = true;
      	}   
  	}
	var combo2 = document.getElementById("archivoad4");
   	var cantidad2 = combo2.length;
   	for (i = 0; i < cantidad2; i++) {
      	if (combo2[i].value == archivo4) {
         	combo2[i].selected = true;
      	}   
  	}
	var combo2 = document.getElementById("archivoad5");
   	var cantidad2 = combo2.length;
   	for (i = 0; i < cantidad2; i++) {
      	if (combo2[i].value == archivo5) {
         	combo2[i].selected = true;
      	}   
  	}
}

function borraNoticia(id){
	if (confirm("Seguro que desea borrarlo?")){
		cancela();
		oXML = AJAXCrearObjeto();
		oXML.open('POST','adminopt.php',true);
		oXML.onreadystatechange = function() {
			if (oXML.readyState == 1){
				document.getElementById("divnoticias").innerHTML="Actualizando...";
				irA("divimagenes");
			}else{
				if (oXML.readyState == 4){
					document.getElementById("divnoticias").innerHTML=oXML.responseText;
				}
			}
		}
		oXML.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		oXML.send("op=3&id="+id);
	}
}

function borraArchivo(archivo){
	if (confirm("Esta seguro de que desea eliminar ese archivo del servidor?")){
		oXML = AJAXCrearObjeto();
		oXML.open('POST','adminopt.php',true);
		oXML.onreadystatechange = function() {
			if (oXML.readyState == 1){
				document.getElementById("divarchi").innerHTML="Borrando archivo...";
			}else{
				if (oXML.readyState == 4){
					document.getElementById("divarchi").innerHTML=oXML.responseText;
					
				}
			}
		}
		oXML.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		oXML.send("op=4&archivo="+archivo);
	}
}
