//load the news
function ajaxRequestNews(){
	var numNewsItemsToShow = 4;
	ajaxGenericCall('XmlGMServlet?action_type=news_actions&action=get_top_news&num_news=' + numNewsItemsToShow + '&lang=' + selectedLangId,
					function(conn, response, options)
					{
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if (failure != null)
							showErrorDialog('ERROR!!',failure);
						else
						{
							var xml = conn.responseXML.getElementsByTagName('xml')[0];
							var xsl = conn.responseXML.getElementsByTagName('xsl')[0].childNodes[0].nodeValue;
							removeAllNodeChilds(Ext.getDom('menu_noticies'));
							result = displayResult(xml, xsl, 'menu_noticies');
						}
					},
					null
	);		
}

//load the menu
function ajaxRequestMainMenu(){
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=get_menu_main_page&lang=' + selectedLangId,
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if(failure != null)
						{	//in case there was an error show it
							showErrorDialog('ERROR!!',failure);
						}
						else
						{
							var xml = conn.responseXML.getElementsByTagName('xml')[0];
							var xsl = conn.responseXML.getElementsByTagName('xsl')[0].childNodes[0].nodeValue;
							removeAllNodeChilds(Ext.getDom('main_menu'));
							var result = displayResult(xml, xsl, 'main_menu');
							
							/*
							new Rico.Accordion($$('div.nav_titol_menu'), $$('div.contingut_menu'), {
								panelHeight: 300,
								hoverClass: 'nav_titol_menu_hover',
								selectedClass: 'nav_titol_menu_selected'
							});*/							
						}						
					},
					null
	);				
}


//Main page
function ajaxRequestMainPage(){
	ajaxGenericCall('XmlGMServlet?action_type=main_page_actions&action=get_main_page&lang=' + selectedLangId,
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if(failure != null){	//in case there was an error show it
							showErrorDialog('ERROR!!',failure);
						}else{
							var xml = conn.responseXML.getElementsByTagName('xml')[0];
							var xsl = conn.responseXML.getElementsByTagName('xsl')[0].childNodes[0].nodeValue;
							removeAllNodeChilds(Ext.getDom('frame_central'));
							var result = displayResult(xml, xsl, 'frame_central');
							
							/*
							var roundCorners = Rico.Corner.round.bind(Rico.Corner);
							roundCorners('presentation_text');
							roundCorners('quick_link_border_1');
							roundCorners('quick_link_border_2');
							roundCorners('quick_link_border_3');
							*/
						}						
					},
					null
	);				
}

function requestLanguages(){
	ajaxGenericCall('XmlGMServlet?action_type=language_actions&action=get_languages_representation', 
		function(conn, response, options){
			var failure = lookForXMLFailureResponse(conn.responseXML);
			if (failure != null) { //in case there was an error show it
				showErrorDialog('ERROR!!', failure);
			}
			else {
				//display the languages
				var xml = conn.responseXML.getElementsByTagName('xml')[0];
				var xsl = conn.responseXML.getElementsByTagName('xsl')[0].childNodes[0].nodeValue;
				removeAllNodeChilds(Ext.getDom('index_page_language_selector'));
				var result = displayResult(xml, xsl, 'index_page_language_selector');
					
				//save the lang fields to create the menues for editing	
				langFields = xmlLanguageResponseToArray(xml);		
			}
		},
	null);
}

function ajaxRequestLogin(name, password){
	//var name = Ext.getDom("LogName");
	//var password = Ext.getDom("LogPassword");
	ajaxGenericCall('XmlGMServlet?action_type=log_actions&action=log_in&name=' + name + '&password=' + password, 
		function(conn, response, options){
			var failure = lookForXMLFailureResponse(conn.responseXML);
			if (failure != null) { //in case there was an error show it
				showErrorDialog('ERROR!!', failure);
				ajaxRequestMainMenu();
				ajaxRequestMainPage();
				ajaxRequestNews();		
			}
			else {				
				ajaxRequestMainMenu();
				ajaxRequestMainPage();
				ajaxRequestNews();		
			}
		}, null);
}

function ajaxRequestLogout(){
	ajaxGenericCall('XmlGMServlet?action_type=log_actions&action=log_out', 
		function(conn, response, options){
			var failure = lookForXMLFailureResponse(conn.responseXML);
			if (failure != null) { //in case there was an error show it
				showErrorDialog('ERROR!!', failure);
			}
			else {
				ajaxRequestMainMenu();
				ajaxRequestMainPage();
				ajaxRequestNews();		
			}
		}, null);
}

function readMainPage(){
	ajaxRequestMainMenu();
	ajaxRequestNews();
	ajaxRequestMainPage();
}

function changeLanguage(id){	
	selectedLangId = id;
	ajaxRequestMainMenu();
	ajaxRequestNews();
	
	//check wich page to load	
	if(itemType == 'mainPage'){
		ajaxRequestMainPage();	
	}else if(itemType == 'menuItem'){
		loadMenuItemIntoMainPage(lastItemLoaded,null);
	}else if(itemType == 'newsItem'){
		//loadMenuItemIntoMainPage(152,null);
		//loadNewsItemIntoDivElem(lastItemLoaded,'show_news_container');
		loadNewsItemIntoMainPage(152, lastItemLoaded, 'show_news_container');
	}else if(itemType == 'quickLinkItem'){
		loadQuikLinkItemIntoMainPage(lastItemLoaded);
	}else{
		ajaxRequestMainPage();
	}
	
	//change the rss feed
	var rss = Ext.getDom('rss_feed_id');	
	if(selectedLangId == '1'){ //english
		rss.href = './files/rss/rss_1.xml';
	}else if(selectedLangId == '2'){ //spanish
		rss.href = './files/rss/rss_2.xml';
	}else if(selectedLangId == '3'){ //catalan
		rss.href = './files/rss/rss_3.xml';
	}else{ //default english
		rss.href = './files/rss/rss_1.xml';
	}
	
	
	//change the language of the main page
	//changeLanguageForElement("MainPageHomeLabel", selectedLangId);
	//changeLanguageForElement("MainPageSiteMapLabel", selectedLangId);
	//changeLanguageForElement("MainPageLoginLabel", selectedLangId);
	changeLanguageForElement("MainPageLogoutLabel", selectedLangId);
	changeLanguageForElement("MainPageNoticiesLabel", selectedLangId);
	//changeLanguageForElement("MainPageAboutLabel", selectedLangId);
	changeLanguageForElement("MainPageContactLabel", selectedLangId);
}

function loadMenuItemIntoMainPage(id, additionalFunction){
	var scope = additionalFunction;	
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=get_menu_destination&id=' + id + '&lang=' + selectedLangId, 
		function(conn, response, options){
			var failure = lookForXMLFailureResponse(conn.responseXML);
			if (failure != null) { //in case there was an error show it
				showErrorDialog('ERROR!!', failure);
			}
			else {
				lastItemLoaded = id;
				itemType = "menuItem";
					
				var html = conn.responseXML.getElementsByTagName('html')[0];
				var javascript = conn.responseXML.getElementsByTagName('javascript')[0];
				var frame_central = Ext.getDom('frame_central');
				var newContent = findDirectTextNodeValue(html);
				if(!Ext.isEmpty(newContent,false)){
					removeAllNodeChilds(Ext.getDom(frame_central));					
					frame_central.innerHTML = newContent;
				}
				
				//load the javascript
				if(!Ext.isEmpty(javascript)){
					var javascriptCode = findDirectTextNodeValue(javascript);
					eval(javascriptCode);
				}	
			}			
		}, scope);	
}

function loadNewsItemIntoMainPage(itemID, newsId, divName){
	var config = {newsId: newsId, divName: divName};
	
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=get_menu_destination&id=' + itemID + '&lang=' + selectedLangId, 
		function(conn, response, options){
			var failure = lookForXMLFailureResponse(conn.responseXML);
			if (failure != null) { //in case there was an error show it
				showErrorDialog('ERROR!!', failure);
			}
			else {				
				lastItemLoaded = newsId;
				itemType = "newsItem";
					
				var html = conn.responseXML.getElementsByTagName('html')[0];
				var javascript = conn.responseXML.getElementsByTagName('javascript')[0];
				var frame_central = Ext.getDom('frame_central');
				var newContent = findDirectTextNodeValue(html);
				if(!Ext.isEmpty(newContent,false)){
					removeAllNodeChilds(Ext.getDom(frame_central));					
					frame_central.innerHTML = newContent;
				}
				
				//load the javascript
				if(!Ext.isEmpty(javascript)){
					var javascriptCode = findDirectTextNodeValue(javascript);
					eval(javascriptCode);
				}
				
				//load the item
				//loadNewsItemIntoDivElem(response.scope.newsId, response.scope.divName);
			}			
		}, config);	
}

function loadNewsItemIntoDivElem(id,divName){
	ajaxGenericCall('XmlGMServlet?action_type=news_actions&action=get_item_representation&id=' + id + '&lang=' + selectedLangId, 
		function(conn, response, options){
			var failure = lookForXMLFailureResponse(conn.responseXML);
			if (failure != null) { //in case there was an error show it
				showErrorDialog('ERROR!!', failure);
			}
			else {				
				lastItemLoaded = id;
				itemType = "newsItem";						
				var xml = conn.responseXML.getElementsByTagName('xml')[0];
				var xsl = conn.responseXML.getElementsByTagName('xsl')[0].childNodes[0].nodeValue;
				removeAllNodeChilds(Ext.getDom(response.scope));
				var result = displayResult(xml, xsl, response.scope);		
			}			
		}, divName);	
}

function loadQuikLinkItemIntoMainPage(id){
	ajaxGenericCall('XmlGMServlet?action_type=quick_link_actions&action=get_item_with_language&id=' + id + '&lang=' + selectedLangId, 
		function(conn, response, options){
			var failure = lookForXMLFailureResponse(conn.responseXML);
			if (failure != null) { //in case there was an error show it
				showErrorDialog('ERROR!!', failure);
			}
			else {
				lastItemLoaded = id;
				itemType = "quickLinkItem";				
				var xml = conn.responseXML.getElementsByTagName('xml')[0];
				var xsl = conn.responseXML.getElementsByTagName('xsl')[0].childNodes[0].nodeValue;
				removeAllNodeChilds(Ext.getDom('frame_central'));
				var result = displayResult(xml, xsl, 'frame_central');		
			}			
		}, null);	
}

function loadLink(link) {
	var target = loadXMLDoc(link);
	
	var html = target.getElementsByTagName('html')[0];
	var javascript = target.getElementsByTagName('javascript')[0];
	var frame_central = Ext.getDom('frame_central');
	var newContent = XMLToString(findFirstChildXMLDoc(html)); //(new XMLSerializer()).serializeToString(findFirstChildXMLDoc(html)); //findDirectTextNodeValueDirectLink(html);
	
	removeAllNodeChilds(Ext.getDom(frame_central));
	frame_central.innerHTML = newContent;
	
	
	//if(!Ext.isEmpty(newContent, false)) {
		//removeAllNodeChilds(Ext.getDom(frame_central));
		//frame_central.appendChild(html);					
		//frame_central.innerHTML = newContent;
	//}
	
	if(!Ext.isEmpty(javascript)){
		var javascriptCode = findDirectTextNodeValue(javascript);
		eval(javascriptCode);
	}		
}


/* This is much faster than using (el.innerHTML = str) when there are many
existing descendants, because in some browsers, innerHTML spends much longer
removing existing elements than it does creating new ones. */
function replaceHtml(el, html) {
       var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
       var newEl = document.createElement(oldEl.nodeName);
        // Preserve the element's id and class (other properties are lost)
       newEl.id = oldEl.id;
       newEl.className = oldEl.className;
       // Replace the old with the new
       newEl.innerHTML = html;
       oldEl.parentNode.replaceChild(newEl, oldEl);
       /* Since we just removed the old element from the DOM, return a reference
       to the new element, which can be used to restore variable references. */
       return newEl;
};

