// JavaScript Document
var editdelsectionid;
var editdelsectiontype;

function addimagesection(image)
{
	parent.document.getElementById("photolist").innerHTML = parent.document.getElementById("photolist").innerHTML + '<td id="' + image + '"><a target="_blank" href="../imagesmain/' + image + '" rel="thumbnail"><img height="50" src="../imagesmain/' + image + '" /></a><p align="center" id="bo' + image + '"><a href="javascript:void(0)" onclick="delimagesection(\'' + image + '\')">Borrar</a></p></td>';
}

function uploadimagesection(form)
{
	document.getElementById("uploadimageForm").action = "../addimagesection.php";
	form.submit();
}

function delimagesection(name)
{
	var url = "../delimagesection.php?name=" + name;
	
	var xmlHttp = newXMLHttpRequest();
	
  	// Conexión con el servidor
  	xmlHttp.open("GET", url, true);
	
	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType("application/x-www-form-urlencoded");
		}
	}	
	else if (window.ActiveXObject) 
	{ 
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}
	
  	// función de recepción del código HTML
  	xmlHttp.onreadystatechange = function() 
	{
		if (xmlHttp.readyState == 4) 
		{
			if (xmlHttp.status == 200) 
			{
				var nodo = document.getElementById("photolist");
				nodo.removeChild(nodo.childNodes[nodo.childNodes.length - 1]);
			}
		}
	}

  	// No se envía nada al servidor
  	xmlHttp.send(null);
}

function getimagesmain()
{
	var url = "../getimagesmain.php";
		
	var xmlHttp = newXMLHttpRequest();
	
  	// Conexión con el servidor
  	xmlHttp.open("GET", url, true);
	
	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType("application/x-www-form-urlencoded");
		}
	}	
	else if (window.ActiveXObject) 
	{ 
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}
	
  	// función de recepción del código HTML
  	xmlHttp.onreadystatechange = function() 
	{
		if (xmlHttp.readyState == 4) 
		{
			if (xmlHttp.status == 200) 
			{
				var ne = eval(xmlHttp.responseText);
				var i = 0;
				document.getElementById("photolist").innerHTML = "";
				for (i; i < ne.length; i++)
				{	
						addimagesection(ne[i]);
						parent.document.getElementById("bo" + ne[i]).style.display = 'block';
				}
			}
		}
	}

  	// No se envía nada al servidor
  	xmlHttp.send(null);
}

function getsections(type)
{
	var url;
	if (type == "s")	
		url = "../getsections.php";
	else if (type == "ss")
		url = "../getsubsections.php";
		
	var xmlHttp = newXMLHttpRequest();
	
  	// Conexión con el servidor
  	xmlHttp.open("GET", url, true);
	
	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType("application/x-www-form-urlencoded");
		}
	}	
	else if (window.ActiveXObject) 
	{ 
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}
	
  	// función de recepción del código HTML
  	xmlHttp.onreadystatechange = function() 
	{
		if (xmlHttp.readyState == 4) 
		{
			if (xmlHttp.status == 200) 
			{
				if (type == "s")
				{
					document.getElementById("sbelong").innerHTML = xmlHttp.responseText;
					document.getElementById("editsection").innerHTML = xmlHttp.responseText;
				}
				else if (type == "ss")
					document.getElementById("editsubsection").innerHTML = xmlHttp.responseText;
				
			}
		}
	}

  	// No se envía nada al servidor
  	xmlHttp.send(null);
}

function getsection(main)
{
	if (main == 1)
	{
		document.getElementById("content").style.visibility = 'hidden';
	}
	var url;
	if (editdelsectiontype == "s")	
		url = "../getsection.php";
	else if (editdelsectiontype == "ss")
		url = "../getsubsection.php";
		
	var xmlHttp = newXMLHttpRequest();
	
  	// Conexión con el servidor
  	xmlHttp.open("GET", url + "?id=" + editdelsectionid + "&main=" + main, true);
	
	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType("application/x-www-form-urlencoded");
		}
	}	
	else if (window.ActiveXObject) 
	{ 
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}
	
  	// función de recepción del código HTML
  	xmlHttp.onreadystatechange = function() 
	{
		if (xmlHttp.readyState == 4) 
		{
			if (xmlHttp.status == 200) 
			{
				if (main == 0)
				{
				if (editdelsectiontype == "s")
				{
					document.getElementById("sectioname").value = document.getElementById("editsection").options[document.getElementById("editsection").selectedIndex].text;
					document.getElementById("stype").selectedIndex = 0;
					tinyMCE.activeEditor.setContent(xmlHttp.responseText);
				}
				else if (editdelsectiontype == "ss")
				{
					var ne = eval(xmlHttp.responseText);
					document.getElementById("sectiondis").style.display = 'block';
					document.getElementById("sbelong").value = ne[1];
					document.getElementById("sectioname").value = document.getElementById("editsubsection").options[document.getElementById("editsubsection").selectedIndex].text;
					document.getElementById("stype").selectedIndex = 1;
					tinyMCE.activeEditor.setContent(ne[0]);
				}
				}
				if (main == 1)
				{
					var ne = eval(xmlHttp.responseText);
					document.getElementById("content").innerHTML = '<h1>' + ne[1] + '</h1><hr noshade="noshade" size="1" />' + ne[0] + '<hr noshade="noshade" size="1" />';
					var myFx = new Fx.Style('content', 'opacity',{duration: 350}).start(0,1);
				}
			}
		}
	}

  	// No se envía nada al servidor
  	xmlHttp.send(null);
}


function delsection()
{
	var url;
	if (editdelsectiontype == "s")	
		url = "../delsection.php";
	else if (editdelsectiontype == "ss")
		url = "../delsubsection.php";
		
	var xmlHttp = newXMLHttpRequest();
	
  	// Conexión con el servidor
  	xmlHttp.open("GET", url + "?id=" + editdelsectionid, true);
	
	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType("application/x-www-form-urlencoded");
		}
	}	
	else if (window.ActiveXObject) 
	{ 
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}
	
  	// función de recepción del código HTML
  xmlHttp.onreadystatechange = function() 
	{
		if (xmlHttp.readyState == 4) 
		{
			if (xmlHttp.status == 200) 
			{
				if (xmlHttp.responseText.substring(1,3) == "OK")
				{
					getsections("s");
					getsections("ss");
					alert("seccion o subseccion borrada correctamente");
				}
				else
					alert(xmlHttp.responseText);
				
			}
		}
	}

  	// No se envía nada al servidor
  	xmlHttp.send(null);
}

function addsection(form)
{
	var name = form.sectioname.value;
	var content = tinyMCE.activeEditor.getContent();
	var typ = form.stype.value;
	var sectionid;
	
	var url;
	if (typ == "s")
		url = "../addsection.php";
	else if (typ == "ss")
	{
		url = "../addsubsection.php";
		sectionid = form.sbelong.value;
	}
	var sectionquery = '<?xml version="1.0" encoding="utf-8"?><section></section>';
	
	var xmlDoc;
	var x;
	var newel;
	var newtext;
	
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(sectionquery);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		var parser=new DOMParser();
		xmlDoc=parser.parseFromString(sectionquery,"text/xml");
	}
	
	x=xmlDoc.getElementsByTagName('section');
  newel=xmlDoc.createElement('name');
	newtext=xmlDoc.createTextNode(name);
	newel.appendChild(newtext);
	x[0].appendChild(newel);
	
	if (typ = "ss")
	{
	x=xmlDoc.getElementsByTagName('section');
  newel=xmlDoc.createElement('sectionid');
	newtext=xmlDoc.createTextNode(sectionid);
	newel.appendChild(newtext);
	x[0].appendChild(newel);
	}
	
	x=xmlDoc.getElementsByTagName('section');
  newel=xmlDoc.createElement('content');
	newtext=xmlDoc.createTextNode(content);
	newel.appendChild(newtext);
	x[0].appendChild(newel);
	
	if (window.ActiveXObject)
		var xmlString = xmlDoc.xml;
	else if (document.implementation && document.implementation.createDocument)
		var xmlString = (new XMLSerializer()).serializeToString(xmlDoc);
	
	var xmlHttp = newXMLHttpRequest();
	
  	// Conexión con el servidor
  	xmlHttp.open("POST", url, true);
	
	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType("text/xml");
		}
	}	
	else if (window.ActiveXObject) 
	{ 
		xmlHttp.setRequestHeader('Content-Type', 'text/xml');
	}
	
  	// función de recepción del código HTML
  	xmlHttp.onreadystatechange = function() 
	{
		if (xmlHttp.readyState == 4) 
		{
			if (xmlHttp.status == 200) 
			{
				if (xmlHttp.responseText.length > 2)
				{
					alert(xmlHttp.responseText);
				}
				else
				{
					getsections("s");
					getsections("ss");
					alert("seccion creada");
				}	
			}
		}
	}

  	// No se envía nada al servidor
  	xmlHttp.send(xmlString);
}

function editsections(form)
{
	var name = form.sectioname.value;
	var content = tinyMCE.activeEditor.getContent();
	var typ = form.stype.value;
	var sectionid;
	
	var url;
	if (editdelsectiontype == "s")
		url = "../editsection.php";
	else if (editdelsectiontype == "ss")
	{
		url = "../editsubsection.php";
		sectionid = form.sbelong.value;
	}
	var sectionquery = '<?xml version="1.0" encoding="utf-8"?><section></section>';
	
	var xmlDoc;
	var x;
	var newel;
	var newtext;
	
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(sectionquery);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		var parser=new DOMParser();
		xmlDoc=parser.parseFromString(sectionquery,"text/xml");
	}
	x=xmlDoc.getElementsByTagName('section');
  newel=xmlDoc.createElement('id');
	newtext=xmlDoc.createTextNode(editdelsectionid);
	newel.appendChild(newtext);
	x[0].appendChild(newel);
	
	x=xmlDoc.getElementsByTagName('section');
  newel=xmlDoc.createElement('name');
	newtext=xmlDoc.createTextNode(name);
	newel.appendChild(newtext);
	x[0].appendChild(newel);
	
	if (typ = "ss")
	{
	x=xmlDoc.getElementsByTagName('section');
  newel=xmlDoc.createElement('sectionid');
	newtext=xmlDoc.createTextNode(sectionid);
	newel.appendChild(newtext);
	x[0].appendChild(newel);
	}
	
	x=xmlDoc.getElementsByTagName('section');
  newel=xmlDoc.createElement('content');
	newtext=xmlDoc.createTextNode(content);
	newel.appendChild(newtext);
	x[0].appendChild(newel);
	
	if (window.ActiveXObject)
		var xmlString = xmlDoc.xml;
	else if (document.implementation && document.implementation.createDocument)
		var xmlString = (new XMLSerializer()).serializeToString(xmlDoc);
	
	var xmlHttp = newXMLHttpRequest();
	
  	// Conexión con el servidor
  	xmlHttp.open("POST", url, true);
	
	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType("text/xml");
		}
	}	
	else if (window.ActiveXObject) 
	{ 
		xmlHttp.setRequestHeader('Content-Type', 'text/xml');
	}
	
  	// función de recepción del código HTML
  	xmlHttp.onreadystatechange = function() 
	{
		if (xmlHttp.readyState == 4) 
		{
			if (xmlHttp.status == 200) 
			{
				if (xmlHttp.responseText.length > 2)
				{
					alert(xmlHttp.responseText);
				}
				else
				{
					getsections("s");
					getsections("ss");
					alert("seccion editada");
					document.getElementById('editsectionButton').style.display = 'none';
					document.getElementById('addsectionButton').style.display = 'block';
					document.getElementById('sectiondis').style.display = 'none';
					document.getElementById('stype').selectedIndex = 0;
				}
			}
		}
	}

  	// No se envía nada al servidor
  	xmlHttp.send(xmlString);
}

function getsectionsmain()
{
	var xmlHttp = newXMLHttpRequest();
	
	xmlHttp.open("GET", '../getsectionsmain.php', true);
  	
  if (window.XMLHttpRequest) 
	{ // Mozilla, Safari,...
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType("application/x-www-form-urlencoded");
		}
	}	
	else if (window.ActiveXObject) 
	{ 
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}	
  	
	// función de recepción del código HTML
  	
	xmlHttp.onreadystatechange = function() 
	{
		if (xmlHttp.readyState == 4) 
		{
			if (xmlHttp.status == 200) 
			{
				document.getElementById('menu').innerHTML = xmlHttp.responseText;
			}
		}
	}

  	// No se envía nada al servidor
  	xmlHttp.send(null);
}

