// JavaScript Document
var ancho;
var alto;

function resolucion(){
	ancho=screen.width;
	alto=screen.height;
	alert(ancho);
	document.getElementById("cuerpo").style.width=ancho;
/*	document.getElementById("top").style.width = ancho;*/
	document.getElementById("middle").style.width = ancho;	
}
/*MENU PROJECTOS*/
navHover = function() {
	if( document.getElementById("navmenu-h") )
	{
		var lis = document.getElementById("navmenu-h").getElementsByTagName("LI");
		for (var i=0; i<lis.length; i++) {
			lis[i].onmouseover=function() {
				this.className+=" iehover";
			}
			lis[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" iehover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", navHover);
/*FIN MENU PROYECTOS*/



/*menu desplegable*/
 function ocultamenu(){
  var menu = document.getElementById("navmenu-h");
  menu.style.display = "none";
}
function despliega(){
  var menu = document.getElementById("navmenu-h");
    if(menu.style.display == "none"){
      menu.style.display = "block";
    }
    else{
      menu.style.display = "none";
    }
}

function changeFilterWUMSafiroMode(mode)
{
	if( mode=='user')
	{
		document.getElementById('filteruser').disabled=false;
		document.getElementById('filteruser').style.display="block";
		document.getElementById('filtertype').style.display="none";
		document.getElementById('filtertype').disabled=true;
	}
	else if( mode=='type')
	{
		document.getElementById('filteruser').style.display="none";
		document.getElementById('filteruser').disabled=true;
		document.getElementById('filtertype').disabled=false;
		document.getElementById('filtertype').style.display="block";
	}
}

function select(item, actual, count) {
	
	for(var i=0; i < count; i++)
		document.getElementById(item+(i+1)).style.display = 'none';
		
	for(i=0; i < count; i++) {
		document.getElementById("title_"+item+(i+1)).style.background="#d0e9f5";
		document.getElementById("title_"+item+(i+1)).style.fontWeight='normal';
		document.getElementById("title_"+item+(i+1)).style.borderBottomColor='#000000';
	}

	var selected = document.getElementById(item+actual);
	selected.style.display='block';
	selected.style.background="#ffffff";

	var tabtitle = document.getElementById("title_"+item+actual);
	tabtitle.style.background="#ffffff";
	tabtitle.style.fontWeight='bold';
	tabtitle.style.borderBottomColor='#ffffff';
}

function activateOrganization(campo) {
	var org1 = document.getElementById('organization');
	var org2 = document.getElementById('organizationManual');
	var opt1 = document.getElementById('optOrganization1');
	var opt2 = document.getElementById('optOrganization2');
	
	if( campo == 1 ) {
		org1.disabled = false;
		org2.disabled = true;
		opt1.checked  = true;
		opt2.checked  = false;
		org1.focus();
	}else {
		org1.disabled = true;
		org2.disabled = false;
		opt1.checked  = false;
		opt2.checked  = true;
		org2.focus();
	}
}

function trim(cadena){ return cadena.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }

function show_remember_password()
{
	var login = trim(document.getElementById("login").value);
	if( login != '' )
		document.getElementById("email").value = login;

	document.getElementById('div_remember_password').style.display='block'
}

function activatePBMQueryParameters()
{
	var query = document.getElementById('fCPcb');
	if( !query.checked )
	{
		document.getElementById('fCPcbC').disabled=true;
		document.getElementById('fCPcbS').disabled=true;
		document.getElementById('fCPQueryValue').disabled=true;		
	}
	else
	{
		document.getElementById('fCPcbC').disabled=false;
		document.getElementById('fCPcbS').disabled=false;
		document.getElementById('fCPQueryValue').disabled=false;		
	}
}

//var pattern  = new RegExp("^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$");

function addContent()
{
	var div = document.getElementById("fPMMultimedia");

	if( div )
	{
		var texto = document.getElementById("multimedia_text").value;
		var url   = document.getElementById("multimedia_url").value;
		
		if( url.trim() == '' ) alert("You must enter at least the url to be saved");
		else
		{
//			if(!pattern.test( url) )
//			    alert("The url is not valid.\nPlease, enter a valid one.");
//			else
//			{
				var tipos = div.getElementsByTagName('input');
				if( tipos.length > 0 )
				{
					var i = 0;
					var encontrado = false;
					for(i=0; i < tipos.length; i++)
					{
						if( tipos[i].checked )
						{
							var type = tipos[i].value;
							
							var div1;
							if( type == 'agenda' )            div1 = document.getElementById('fPMAgendas');
							else if( type == 'presentation' ) div1 = document.getElementById('fPMPresentations');
							else if( type == 'document' )     div1 = document.getElementById('fPMDocuments');
							else if( type == 'photo' )        div1 = document.getElementById('fPMPhotos');

							var posicion = 1;

							var params = div1.getElementsByTagName('span');
							if( params.length > 0 ) posicion = params.length + 1;
							
							var span = document.createElement("span");
							span.setAttribute('class', 'content_info');
							span.setAttribute('id', 'multimedia_'+ type + "_"+ posicion);
							
							var texto1 = document.createElement("input");
							texto1.setAttribute('value', texto);
							texto1.setAttribute('type', 'text');
							texto1.setAttribute('name', type +'_text_'+ posicion);
							texto1.setAttribute('id', type + '_text_'+ posicion);
							
							var label1 = document.createElement("label");
							label1.setAttribute('for', type +'_text_'+ posicion);
							label1.appendChild(document.createTextNode("Text "))
							
							var texto2 = document.createElement("input");
							texto2.setAttribute('value', url);
							texto2.setAttribute('type', 'text');
							texto2.setAttribute('name', type +'_url_'+ posicion);
							texto2.setAttribute('id', type + '_url_'+ posicion);
							
							var label2 = document.createElement("label");
							label2.setAttribute('for', type + '_url_'+ posicion);
							label2.appendChild(document.createTextNode("Url "))

							var enlace = document.createElement("a");
							enlace.setAttribute("id", type + "_" + posicion);
							enlace.setAttribute("href", url);
							enlace.setAttribute("target", "_blank");
							enlace.innerHTML = texto;

							var deleteContent = document.createElement("a");
							deleteContent.setAttribute("style", "cursor:pointer");
							deleteContent.setAttribute("onclick", "deleteContent('"+ type +"','"+ posicion +"')");

							var image = document.createElement("img");
							image.setAttribute("src", "media/action_delete.gif");
							image.setAttribute("alt", "Delete");
							deleteContent.appendChild(image);
							
							span.appendChild(label1);
							span.appendChild(texto1);
							span.appendChild(label2);
							span.appendChild(texto2);
							span.appendChild(deleteContent);

							div1.appendChild(span);

							encontrado = true;
						}
					}
					if( !encontrado ) alert("You must select one multimedia type in order to save it");
				}
//			}
		}
	}
	else alert("An error ocurred.\nCannot find the multimedia recipient.\nPlease reload the page. If the error persists contact with the webmaster");
}

function deleteContent(type, posicion)
{
	var div;
	if( type == 'agenda' )            div = document.getElementById('fPMAgendas');
	else if( type == 'presentation' ) div = document.getElementById('fPMPresentations');
	else if( type == 'document' )     div = document.getElementById('fPMDocuments');
	else if( type == 'photo' )        div = document.getElementById('fPMPhotos');

	var content = document.getElementById("multimedia_"+ type +"_"+ posicion);
	if( div && content )
	{
		div.removeChild(content);
		///Cambio los ids de los elementos restantes para coincidir con su posicion en el array
		var params = div.getElementsByTagName('span');
		if( params.length > 0 )
		{
			var i = 0;
			var posicion = 0;
			for(i; i < params.length; i++)
			{
				posicion = i + 1;
				
				///Actualizo el id del span
				params[i].setAttribute("id", 'multimedia_'+ type + "_"+ posicion);
				
				///Actualizo el id y el nombre del input
				params[i].getElementsByTagName("input")[0].setAttribute("id", type +'_text_'+ posicion);
				params[i].getElementsByTagName("input")[0].setAttribute("name", type +'_text_'+ posicion);
				
				params[i].getElementsByTagName("input")[1].setAttribute("id", type +'_url_'+ posicion);
				params[i].getElementsByTagName("input")[1].setAttribute("name", type +'_url_'+ posicion);
				
				///Actualizo el elemento label
				params[i].getElementsByTagName("label")[0].setAttribute("for", type + '_text_'+ posicion);
				params[i].getElementsByTagName("label")[1].setAttribute("for", type + '_url_'+ posicion);
				params[i].getElementsByTagName("label")[0].innerHTML = "Text ";
				
				///Actualizo la funcion del enlace
				params[i].getElementsByTagName("a")[0].setAttribute("onclick", "deleteContent('"+ type +"','"+ posicion +"')");
			}
		}
	}
}

function comprobarNumElements()
{
	var number = document.getElementById('rlNumElements');
	var pagination = document.getElementById('rlPagination');
	
	if( trim(number.value) == '' )
	{
		pagination.disabled = true;
		document.getElementById('txtError').innerHTML = "";
	}
	else
	{
		if( isNaN(number.value) )
		{
			pagination.disabled = true;
			document.getElementById('txtError').innerHTML = "You must enter a valid number. In other case, 0 will be assigned";
		}
		else if( number.value > 1 && pagination.disabled )
		{
			pagination.disabled = false;
			document.getElementById('txtError').innerHTML = "";
		}
		else if( number.value <= 1 )
		{
			pagination.disabled = true;
			document.getElementById('txtError').innerHTML = "";
		}
	}
}

function comprobarFormularioPublicacion()
{
	///Compruebo la fecha inicial y final
	var short = document.getElementById("fPMShortTitle").value;
	var title = document.getElementById("fPMTitle").value;
	
	var errores = "";

	if( short != null && trim(short).length == 0 ) errores = errores.concat("Short title is required\n");
	if( title != null && trim(title).length == 0 ) errores = errores.concat("Title is required\n");
	
	var correcto = true;
	if( trim(errores).length > 0 )
	{
		correcto = false;
		alert(errores);
	}

	var correcto1 = checkPublicationURL(document.getElementById('fPMAgendas'), "Agenda");
	var correcto2 = checkPublicationURL(document.getElementById('fPMPresentations'), "Presentation");
	var correcto3 = checkPublicationURL(document.getElementById('fPMDocuments'), "Document");
	var correcto4 = checkPublicationURL(document.getElementById('fPMPhotos'), "Photo");
	
	///Fuerzo el valor del textarea, ya que al ser ajax el envio del formulario no se inserta bien
	var oEditor = FCKeditorAPI.GetInstance('tlbDescription');
	document.getElementById('tlbDescription').value = oEditor.GetXHTML(false);
	
	return correcto && correcto1 && correcto2 && correcto3 && correcto4;
}

function checkPublicationURL(div, name)
{
	var params = div.getElementsByTagName('span');
	var salida = false;
	if( params.length > 0 )
	{
		var i = 0;
		for(i; i < params.length && !salida; i++)
		{
			if(!pattern.test( params[i].getElementsByTagName("input")[1].value ))
			{
			    alert("The url in position "+ (i+1) + " for the type '"+ name + "' is not valid.\nPlease, enter a valid one or delete it");
			    salida = true;
			}
		}
	}
	
	return !salida;
}

