/*
Library to deal with XML data retrieval from the server and the insertion in the webpage DOM
It handles the initial page load and the language change (involving an AJAX call for the new
xml files).
Eventually this library should be implemented in PHP in the server.
*/


//retrieved information
var http_request;
var http_request2;
var http_request3;
var domXML;
var domXML2;

//http request and support files for html AJAX requests
//the divHTML array contains the div info for all pages loaded into DIVs
var divHTML = new Array();;
var html_request = new Array();

//to generate the tabID's
var tab_number = 0;
//arrays to keep track of the tabs created
var tablist_id = new Array();
var tablist_multidiv = new Array();
var tablist_title = new Array();


/*
This deals with parameters and loads the page
It receives the default html page language 
*/
function pageLoad(default_lang)
{
	//set the current language to the one selected
	current_language = default_lang;
	var internal_tab = null;

	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) 
	{
		var pos = parms[i].indexOf('=');
		if (pos > 0) 
		{
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			//list of accepted params
			if(key == "lang")
			{
				current_language = val;
			}
			if(key == "tab")
			{
				internal_tab = val;
				//need to take out the %20
    			var re = new RegExp("%20", "g");     
    			internal_tab = internal_tab.replace(re, " "); 
			}
			if(key == "tab_id")
			{
				initial_id = val;
			}
		}
	}
	
	//load the page
	getMainPage(current_language);
	if(internal_tab != null)
	{
		initial_tab = internal_tab;
	}
}


/*
Initial function of the parser. When passing it a language id it sends an ajax request
 for the main page information to the server. It is called upon startup. Once the xml is 
 retrieved it calls ProcessMainXML
*/
function getMainPage(language)
{
	//hide the default page data
	document.getElementById("default_page").style.display="none";
	//hide the clock
	//document.getElementById("progress").style.display="none";

	//get an instance of the HTTPrequest object
	if(window.XMLHttpRequest)// && navigator.appName.indexOf("Microsoft")!=0)
	{
		http_request = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}

	//Asynchronous request.... first batch
	http_request.onreadystatechange = processMainXML;
		
	//get the name of the file to retrieve from the xhtml
	//this file is number 1, main page XML

	var meta_tags = document.getElementsByTagName("meta");
	var data_file = null;
	for(var i=0;i<meta_tags.length;i++)
	{
		var name = meta_tags.item(i).attributes.getNamedItem("name");
		if(name != null)
		{
			if(name.nodeValue == "winery")
			{
				data_file = meta_tags.item(i).attributes.getNamedItem("content").nodeValue;
			}
			if(name.nodeValue == "date_modified")
			{
				date_modified = meta_tags.item(i).attributes.getNamedItem("content").nodeValue;
			}
		}
	}
	if(data_file == null)
	{
		alert("No data file found in html meta tags");
	}
	

	http_request.open('GET', data_file, true);

	http_request.send(null);
	
}

/*switches the main screen from one language to another*/
/*No need to reload the XML for the main page, all info is already there*/
/*for the content page the file name is gotten from the main xml*/
function switchMainPage(language)
{
	//set the current language to the one selected
	current_language = language;

	processMainXML();

}

/*This function processes any type of data returned by the retrieved xml document*/
/* Should make the initial website totally emply and create all necessary divs in here*/
function processMainXML()
{
	if (http_request.readyState == 4)
	if (http_request.status == 200)
	{
		domXML = http_request.responseXML;

		/*get the list of XML tags searching for the info for the different languages*/
		var xml_list = domXML.getElementsByTagName("language");
		var lang_data = null;
		for(var i=0; i<xml_list.length; i++)
		{
			if(xml_list.item(i).attributes.getNamedItem("id").nodeValue == current_language)
			{
				//language to work with
				lang_data = xml_list.item(i);
			}
		}
		
		/*MAIN WEBSITE REGIONS*/
		
		/* do not delete all, only the obsolete content
		var tmp = document.getElementById("content"); if(tmp){remove_elem(tmp);}
		var document_body = document.getElementsByTagName("body").item(0); //where all divs hang from
		var document_content = add_elem({elem_type:"div", elem_dad: document_body, elem_id: "content"});
		*/
		var document_content = document.getElementById("content");
		
		/**WARNING: in order not to overuse the absolute positioning let's insert the divs in the
					order that they appear in the screen and position them with relative.
					To dothis, we first position the structure divs and then add their content*/

		/*TOP_MENU*/
		var top_menu = document.getElementById("top_menu");
		remove_children(top_menu);

		/*MAIN_TOP*/
		var main_top = document.getElementById("main_top");
		/*I consider 3 areas in top: top_left, top_right and top_center and create their divs first*/
		var top_left = document.getElementById("top_left");
		remove_children(top_left);
		var top_center = document.getElementById("top_center");
		remove_children(top_center);
		var top_right = document.getElementById("top_right");
		remove_children(top_right);
		//create embelishments
		create_embelishments(main_top, "main_left_border");
		create_embelishments(main_top, "main_right_border");

		/*MAIN_LEFT*/
		var main_left = document.getElementById("main_left"); 
		remove_children(main_left);
		//create embelishments
		create_embelishments(main_left, "side_top_border");
		create_embelishments(main_left, "side_bottom_left_border");
		
		/*MAIN_CENTER*/
		var main_center = document.getElementById("main_center");
		remove_children(main_center);
					
		/*MAIN_RIGHT*/
		var main_right = document.getElementById("main_right");
		remove_children(main_right);
		//create embelishments
		create_embelishments(main_right, "side_top_border");
		create_embelishments(main_right, "side_bottom_right_border");
		
		/*MAIN_BOTTOM*/
		//var main_bottom = add_elem({elem_type:"div", elem_dad: document_content, elem_id:"main_bottom"});
		var main_bottom = document.getElementById("main_bottom");
		remove_children(main_bottom);
		
		/*END OF LAYOUT DATA*/
			
		/*Send second XML request*/
		/*Needs to be after the definition of the divs or IE gives problems after cashing it*/
		if(lang_data == null)
		{
			alert("No information available for the desired language, please select one of the flags");
		}
		else
		{
			/*INFO FILE, second XML request + HTML request*/

			info_file = lang_data.attributes.getNamedItem("file").nodeValue;				
			//Asynchronous request....
			//get an instance of the HTTPrequest object
			if (window.XMLHttpRequest)
			{
				http_request2 = new XMLHttpRequest();
				//http_request3 = new XMLHttpRequest();
			}
			else if(window.ActiveXObject)
			{
				http_request2 = new ActiveXObject( "Microsoft.XMLHTTP" );
				//http_request3 = new ActiveXObject( "Microsoft.XMLHTTP" );
			}
			http_request2.onreadystatechange = setMultiDiv;
			http_request2.open('GET',info_file, true);
			http_request2.send(null); /*WARNING: need to put null, or firefox does not work*/

			//http_request3.onreadystatechange = setMultiDiv;
			//http_request3.open('GET',info_file + ".html",true);
			//http_request3.send(null); /*WARNING: need to put null, or firefox does not work*/				
		}
	
		/*START OF INFO DATA*/
	
		/*fill the main_top information*/
		
		var top_children = lang_data.getElementsByTagName("main_top").item(0).childNodes;
		for(var k=0; k<top_children.length; k++)
		{
			var elem_classID = null;
			if(top_children.item(k).attributes)
			{
				elem_classID = top_children.item(k).attributes.getNamedItem("classID").nodeValue;

				/*find which one is his dad*/
				var elem_dad = null;
				var regexp = /top_left/;
				if(elem_classID.match(regexp)){elem_dad = top_left;}
				else
				{
					var regexp = /top_center/;
					if(elem_classID.match(regexp)){elem_dad = top_center;}
					else
					{
						var regexp = /top_right/;
						if(elem_classID.match(regexp)){elem_dad = top_right;}
						else
						{
							alert("No dad found");
						}
					}
				}
			}

			if(top_children.item(k).tagName == "p")
			{				
				var p_text = top_children.item(k).firstChild.nodeValue;
				add_elem({elem_type:"p", elem_dad:elem_dad, elem_text:p_text, elem_class:elem_classID});
			}
			else if(top_children.item(k).tagName == "img")
			{
				var img_alt = top_children.item(k).attributes.getNamedItem("alt").nodeValue;
				var img_src = top_children.item(k).attributes.getNamedItem("src").nodeValue;
				add_elem({elem_type:"img", elem_dad:elem_dad, elem_alt:img_alt, elem_class:elem_classID, elem_src:img_src});			}
		}
		
		/*fill in the main bottom info*/
		add_elem({elem_type:"a", elem_dad:main_bottom, elem_text:"Web productions", elem_href:"mailto:dopriorat@gmail.com", elem_id:"bottom_webmaster"});
		if(date_modified != null)
		{
			add_elem({elem_type:"span", elem_dad:main_bottom, elem_text:"last modified "+date_modified, elem_id:"bottom_modified"});
		}		
	

		/*Fill all the other main divs*/

		var items_list = lang_data.getElementsByTagName("item");
	
		for(var i=0; i<items_list.length; i++)
		{
			/*paternity check*/
			var dad_id;
			var class_id;
			if(items_list.item(i).parentNode.tagName == "main_left")
			{
				dad_id = main_left;
				class_id = "box_left";
			}
			else if(items_list.item(i).parentNode.tagName == "main_right")
			{
				dad_id = main_right;
				class_id = "box_right";
			}
			else
			{
				dad_id = main_center;
				class_id = "box_center";				
			}
		
		
			/*check all the possibilities I accept*/
			var item_id = items_list.item(i).attributes.getNamedItem("id").nodeValue;
			var item_elem = items_list.item(i);
			
			/*EXTERNAL HTML PAGES*/
			//create and send a request for all external html pages
			if(item_id == "external_html")
			{
				var external_pages = item_elem.getElementsByTagName("info");
				for(var extPage=0; extPage< external_pages.length; extPage++)
				{
					var ext_id = external_pages.item(extPage).attributes.getNamedItem("id").nodeValue;
					var ext_src = external_pages.item(extPage).attributes.getNamedItem("src").nodeValue;
					
					//make the call
					if (window.XMLHttpRequest)
					{
						html_request[ext_id] = new XMLHttpRequest();
					}
					else if(window.ActiveXObject)
					{
						html_request[ext_id] = new ActiveXObject( "Microsoft.XMLHTTP" );	
					}
					html_request[ext_id].onreadystatechange = setMultiDiv;
					html_request[ext_id].open('GET',ext_src,true);
					html_request[ext_id].send(null); /*WARNING: need to put null, or firefox does not work*/
				}
			}
			
			/*CONTACT INFORMATION*/
			if(item_id == "contact_info")
			{
				//empty any preexistance
				var contact_tmp = document.getElementById("contact_info");
				if(contact_tmp){remove_elem(contact_tmp);}				
				//build the block
				var contact_div = add_elem({elem_type:"div", elem_dad:dad_id, elem_id:"contact_info", elem_class:class_id});
				var contact_title = item_elem.attributes.getNamedItem("title").nodeValue;
				add_elem({elem_type:"div", elem_dad:contact_div, elem_class:"title", elem_text:contact_title});
				var address_div = add_elem({elem_type:"div", elem_dad:contact_div, elem_id:"address_div"});
				var contact_list = item_elem.getElementsByTagName("info");			 
				for(var j=0; j< contact_list.length; j++)
				{
					var contact_text = contact_list.item(j).firstChild.nodeValue;
					var contact_id = contact_list.item(j).attributes.getNamedItem("id").nodeValue;
					if(contact_id == "email")
					{
						add_elem({elem_type:"a", elem_dad:address_div, elem_text:contact_text, elem_href:"mailto:"+contact_text});
						add_elem({elem_type:"br", elem_dad:address_div});
					}
					else if(contact_id == "web")
					{
						add_elem({elem_type:"a", elem_dad:address_div, elem_text:contact_text, elem_href:contact_text});
						add_elem({elem_type:"br", elem_dad:address_div});
					}
					else
					{
						add_elem({elem_type:"span", elem_dad:address_div, elem_text:contact_text});
						add_elem({elem_type:"br", elem_dad:address_div});						
					}
				}	
			}
			
			/*LANGUAGE MENU*/
			if(item_id == "lang_menu")
			{
				//empty the preexistant language list
				var tmp = document.getElementById("lang_menu");
				if(tmp){remove_elem(tmp);}
				
				//go through all languages and create the language list
				var lang_menu = add_elem({elem_type:"div", elem_dad:dad_id, elem_id:"lang_menu", elem_class:class_id});
				//check for the menu title
				for(var j=0; j<xml_list.length; j++)
				{
					var lang_alt = xml_list.item(j).attributes.getNamedItem("id").nodeValue;
					if(current_language == lang_alt)
					{
						var lang_title = xml_list.item(j).attributes.getNamedItem("title").nodeValue;
					}
				}
				add_elem({elem_type:"div", elem_dad:lang_menu, elem_class:"title", elem_text:lang_title});
				
				//add all languages								
				var lang_div = add_elem({elem_type:"div", elem_dad:lang_menu, elem_id:"lang_div"});
				for(var j=0; j<xml_list.length; j++)
				{
					var lang_alt = xml_list.item(j).attributes.getNamedItem("id").nodeValue;
					var lang_img = xml_list.item(j).attributes.getNamedItem("image").nodeValue;
					var lang_file = xml_list.item(j).attributes.getNamedItem("file").nodeValue;
					var lang_onclick = "switchMainPage(\""+lang_alt+"\")";
					var href_onclick = "#?lang="+lang_alt;
					/*add_elem({elem_type:"img", elem_dad:lang_div, elem_alt:lang_alt, elem_src:lang_img, elem_onclick:lang_onclick});
					*/
					var tmp_lang = add_elem({elem_type:"a", elem_dad:lang_div, elem_href:href_onclick, elem_onclick:lang_onclick});
					add_elem({elem_type:"img", elem_dad:tmp_lang, elem_alt:lang_alt, elem_src:lang_img, elem_border:"0"});
				}
			}
			
			/*SHOP_LINKS*/
			if(item_id == "shop_links")
			{	
				//empty the preexistant data
				var shop_tmp = document.getElementById("shop_links");
				if(shop_tmp){remove_elem(shop_tmp);}						
				
				//create the box and insert the info				
				var shop_box = add_elem({elem_type:"div", elem_dad:dad_id, elem_id:"shop_links", elem_class:class_id});
				var shop_title = item_elem.attributes.getNamedItem("title").nodeValue;
				add_elem({elem_type:"div", elem_dad:shop_box, elem_class:"title", elem_text:shop_title});
				var shop_div = add_elem({elem_type:"div", elem_dad:shop_box, elem_id:"wine_labels"});
				
				//list all wines
				var wines_list = item_elem.getElementsByTagName("info");
				for(var j=0; j<wines_list.length; j++)
				{				
					var wine_href = null;
					if(wines_list.item(j).attributes)
					{
						/*encapsulate all into a DIV and put the link/img and text*/
						var wine_capsule = add_elem({elem_type:"div", elem_dad:shop_div});
						
						/*the href contains either a direction inside the current page or on another page*/
						wine_href = wines_list.item(j).attributes.getNamedItem("href").nodeValue;
						wine_id = wines_list.item(j).attributes.getNamedItem("id").nodeValue;
						if(wine_id == "internal")
						{
							//internal website change
							var wine_item = add_elem({elem_type:"a", elem_dad:wine_capsule, elem_id:"wine_link", elem_href:"#", elem_onclick:"switchbyname\(\'"+wine_href+"\'\)"});						
						}
						else
						{
							//external website change with optional parameters
							var wine_item = add_elem({elem_type:"a", elem_dad:wine_capsule, elem_id:"wine_link", elem_href:wine_href});
						}
						var wine_alt = wines_list.item(j).attributes.getNamedItem("alt").nodeValue;
						var wine_src = wines_list.item(j).attributes.getNamedItem("src").nodeValue;
						var wine_classID = wines_list.item(j).attributes.getNamedItem("classID").nodeValue;
						add_elem({elem_type:"img", elem_dad:wine_item, elem_alt:wine_alt, elem_src:wine_src, elem_class:wine_classID});

						var wine_text = wines_list.item(j).firstChild.nodeValue;
						add_elem({elem_type:"p", elem_dad:wine_capsule, elem_text:wine_text, elem_width:"100%"})
					}
				}
			}

			/*PRODUCTE DEL DIA*/
			//here I create the container and then fill it up in the dataparser.js
			if(item_id == "featured_product")
			{	
				//empty the preexistant data
				var tmp = document.getElementById("featured_product");
				if(tmp){remove_elem(tmp);}						
				
				//create the box and insert the info				
				var shop_box = add_elem({elem_type:"div", elem_dad:dad_id, elem_id:"featured_product", elem_class:class_id});
				var shop_title = item_elem.attributes.getNamedItem("title").nodeValue;
				add_elem({elem_type:"div", elem_dad:shop_box, elem_class:"title", elem_text:shop_title});
				var shop_div = add_elem({elem_type:"div", elem_dad:shop_box, elem_id:"product_labels"});
			}

			/*Productor del dia*/
			if(item_id == "featured_producer")
			{	
				//empty the preexistant data
				var tmp = document.getElementById("featured_producer");
				if(tmp){remove_elem(tmp);}						
				
				//create the box and insert the info				
				var shop_box = add_elem({elem_type:"div", elem_dad:dad_id, elem_id:"featured_producer", elem_class:class_id});
				var shop_title = item_elem.attributes.getNamedItem("title").nodeValue;
				add_elem({elem_type:"div", elem_dad:shop_box, elem_class:"title", elem_text:shop_title});
				var shop_div = add_elem({elem_type:"div", elem_dad:shop_box, elem_id:"producer_labels"});				
			}

			
			/*TOP MENU*/
			if(item_id == "menu_top")
			{
				var top_xml_list = item_elem.getElementsByTagName("info");
				for(var j=0; j<top_xml_list.length; j++)
				{
					var a_text = top_xml_list.item(j).firstChild.nodeValue;
					var a_href = top_xml_list.item(j).attributes.getNamedItem("href").nodeValue;
					add_elem({elem_type:"a", elem_dad: top_menu, elem_text: a_text, elem_href: a_href});
				}
			}		
					
			/*BACKGROUND CONTENT*/
			//weconsider an image, some text and a full page layout
			if(item_id == "bg_image")
			{
				//remove any preexistence
				var back_doc = document.getElementById("center_bg_image");
				if(back_doc){remove_elem(back_doc);}	
				
				var bg_img_src = item_elem.attributes.getNamedItem("src").nodeValue;
				var bg_img_alt = item_elem.attributes.getNamedItem("alt").nodeValue;
				//add_elem({elem_type:"img", elem_dad:dad_id, elem_id:"center_bg_image", elem_src:bg_img_src, elem_alt:bg_img_alt, elem_class:"fit_v fit_h"});
				add_elem({elem_type:"img", elem_dad:dad_id, elem_id:"center_bg_image", elem_src:bg_img_src, elem_alt:bg_img_alt});
				
			}
			if(item_id == "bg_text")
			{
				//remove any preexistence
				var back_doc = document.getElementById("center_bg_text");
				if(back_doc){remove_elem(back_doc);}
				
				//get the general class attributes
				if(item_elem.attributes.getNamedItem("classID"))
				{
					var info_class = item_elem.attributes.getNamedItem("classID").nodeValue;
				}				
				//create a container for all
				var bg_text_div = add_elem({elem_type:"div", elem_dad: dad_id, elem_id:"center_bg_text", elem_class: info_class});

				
				//parse the data inside
				parse_info(item_elem, bg_text_div);
			}
			if(item_id == "bg_page")
			{
				//remove any preexistence
				var back_doc = document.getElementById("center_bg_page");
				if(back_doc){remove_elem(back_doc);}
					
				//insert the content
				var bg_page = item_elem.firstChild.nodeValue;
				var bg_page_class = item_elem.attributes.getNamedItem("classID").nodeValue;
				add_elem({elem_type:"p", elem_dad: dad_id, elem_id:"center_bg_page", elem_text:bg_page, elem_class:bg_page_class});								
			}
			
			/*MAIN MENU*/
			if(item_id == "main_menu")
			{					
				//remove any preexistence
				var main_doc = document.getElementById("main_menu");
				if(main_doc){remove_elem(main_doc);}	
				//create elements
				var main_menu = add_elem({elem_type:"div", elem_dad: dad_id, elem_id: "main_menu", elem_class:class_id});
				var main_title = item_elem.attributes.getNamedItem("title").nodeValue;
				var main_title = add_elem({elem_type:"div", elem_dad:main_menu, elem_class:"title", elem_text:main_title});
				var main_ul = add_elem({elem_type:"ul", elem_dad: main_menu});
				var main_xml_list = item_elem.getElementsByTagName("info");
				for(var j=0; j<main_xml_list.length; j++)
				{
					var li_text = main_xml_list.item(j).firstChild.nodeValue;
					var li_id = main_xml_list.item(j).attributes.getNamedItem("id").nodeValue;
					/*IE does not like to link from LI, we insert an <a> to do the same job*/
					var li_elem = add_elem({elem_type:"li", elem_dad: main_ul, elem_class:"c_padding unselected_main_item"});
					add_elem({elem_type:"a", elem_dad:li_elem, elem_id:li_id+"_main", elem_href:"#?tab_id="+li_id, elem_onclick: "showMultiDiv('"+li_id+"')", elem_text: li_text});
				}
			}	
			
			/*GENERAL BOX*/
			if(item_id == "general_box")
			{
				//remove any preexistence
				var tmp = document.getElementById("general_box");
				if(tmp){remove_elem(tmp);}	
				//create elements				
				var general_box = add_elem({elem_type:"div", elem_dad: dad_id, elem_id: "general_box", elem_class:class_id});
				var general_title = item_elem.attributes.getNamedItem("title").nodeValue;
				var general_title = add_elem({elem_type:"div", elem_dad:general_box, elem_class:"title", elem_text:general_title});
				var general_content = add_elem({elem_type:"div", elem_dad: general_box, elem_id: "general_content"});				
				
				//parse the inside content
				parse_info(item_elem, general_content);
			}	

			/*SHOP ACCESS*/
			if(item_id == "shop_access")
			{
				//remove any preexistence
				var tmp = document.getElementById("shop_access");
				if(tmp){remove_elem(tmp);}	
				//create elements				
				var shopaccess_div = add_elem({elem_type:"div", elem_dad: dad_id, elem_id: "shop_access", elem_class:class_id});
				var shop_title = item_elem.attributes.getNamedItem("title").nodeValue;
				var shopaccess_title = add_elem({elem_type:"div", elem_dad:shopaccess_div, elem_class:"title", elem_text:shop_title});
				var shopaccess_content = add_elem({elem_type:"div", elem_dad: shopaccess_div, elem_id: "shopaccess_content"});				
				var shopaccess_link = add_elem({elem_type:"a", elem_href:"link a la botiga", elem_dad:shopaccess_content});				
				var shopaccess_img = add_elem({elem_type:"img", elem_dad:shopaccess_link, elem_src:"./images/shoppingcart.png", elem_alt:"shop access"});

			}
		}
	}
}

/*
	Creates embelishment containers
*/
function create_embelishments(dad_div, embelish_id)
{
	var embelish_div = add_elem({elem_type:"div", elem_dad:dad_div, elem_id:embelish_id});
	var embelish_span1 = add_elem({elem_type:"span", elem_dad:embelish_div});
	add_elem({elem_type:"span", elem_dad:embelish_span1});
}

/*
parses info tab elements
These are for the main page data, where the regular functions are not used
*/
function parse_info(item_elem, dad_id)
{
	//create each element in the childNodes
	for(var i=0; i<item_elem.childNodes.length; i++)
	{	
		//plain text
		if(item_elem.childNodes.item(i).nodeType == 3)
		{
			var item_text = item_elem.childNodes.item(i).nodeValue;
			add_elem({elem_type:"span", elem_dad: dad_id, elem_text:item_text});
		}
		else
		{
			var item_id = item_elem.childNodes.item(i).attributes.getNamedItem("id").nodeValue;
			if(item_id == "text")
			{
				var item_class = null;
				if(item_elem.childNodes.item(i).attributes.getNamedItem("classID"))
				{
					item_class = item_elem.childNodes.item(i).attributes.getNamedItem("classID").nodeValue;
				}
				var item_text = item_elem.childNodes.item(i).firstChild.nodeValue;
				add_elem({elem_type:"span", elem_dad: dad_id, elem_text:item_text, elem_class:item_class});			
			}
			if(item_id == "p")
			{
				var item_class = null;
				if(item_elem.childNodes.item(i).attributes.getNamedItem("classID"))
				{
					item_class = item_elem.childNodes.item(i).attributes.getNamedItem("classID").nodeValue;
				}
				var item_text = item_elem.childNodes.item(i).firstChild.nodeValue;
				add_elem({elem_type:"p", elem_dad: dad_id, elem_text:item_text, elem_class:item_class});			
			}
			if(item_id == "br")
			{
				add_elem({elem_type:"br", elem_dad: dad_id});			
			}
			if(item_id == "mail")
			{
				var item_text = item_elem.childNodes.item(i).firstChild.nodeValue;
				add_elem({elem_type:"a", elem_dad: dad_id, elem_href:"mailto:"+item_text, elem_text:item_text});			
			}
		}
	}

}

/*
Function being called upon second XML download completion.
 It fills the information on all divs while hiding them from the user 
 At startup it doesn't expect any divs, first deletes all and remakes all multidivs
Added support for the HTML file
*/
function setMultiDiv()
{
	//check for all elements completion
	if (http_request2.readyState == 4 && http_request2.status == 200)
	{
		var all_done = 1;
		var request;
		for (request in html_request)
		{
			if(html_request[request].readyState != 4 || html_request[request].status != 200)
			{
				all_done = 0;
			}
		}
		if(all_done)
		{		
			//hide the progress clock
			//document.getElementById("progress").style.display="none";
	
			domXML2 = http_request2.responseXML;
			//check for parsing errors
			if(domXML2.firstChild.tagName == "parsererror")
			{
   				alert("Parsing ERROR: " + domXML2.firstChild.firstChild.nodeValue);
	   	  	}	

			//parse the received data into a div
			for (var request_id in html_request)
			{
				divHTML[request_id] = filter_request(html_request[request_id]);
			}
				
			//delete all existing divs with class=multidiv
			//we look for all div tags and find those with this class
			var tmp_multidiv = document.getElementsByTagName("div");
			for(var i=0; i<tmp_multidiv.length; i++)
			{
				//search for the "multidiv" divs
				var info_div = tmp_multidiv.item(i).attributes.getNamedItem("class");
				if(info_div != undefined)
				{
					if(info_div.nodeValue == "multidiv")
					{
						remove_elem(tmp_multidiv.item(i));
						//warning: we need to go back one position as the list just got decimated
						i--;
					}
				}
			}		

			var main_center_div = document.getElementById("main_center");
			//go through all multidivs in the XML2 file and set up the data structures in the html
			var XML_list = domXML2.getElementsByTagName("multidiv");
			for(var i=0; i<XML_list.length; i++)
			{						
				//get the div's ID
				var multidiv_id = XML_list.item(i).attributes.getNamedItem("ID").nodeValue;
				var new_multidiv = add_elem({elem_type:"div", elem_dad:main_center_div, elem_id:multidiv_id, elem_class:"multidiv"});		
				//we initially hide its display
				new_multidiv.style.display = "none"
				
				//set the multidiv content tabs depending on how many there are in the XML2
				var div_list = XML_list.item(i).childNodes; //get the children of this multidiv
				add_MultidivTabs(div_list, new_multidiv, multidiv_id);
			}	
			
			/*
			here go the functions that set initial states or fill the divs with external data
			*/
		
			//tell other functions that I got the data
			XML2_ready = 1;
			//if initial_tab is set, change to this one
			if(initial_tab != null)
			{
				switchbyname(initial_tab);
				initial_tab = null;
			}
			//change if initial_id is set
			if(initial_id != null)
			{
				switchById(initial_id);
				initial_id = null;
			}
			
			//for the dopriorat case, I need to know 
			// when all XML data has been loaded
			if(products_producers != null)
			{
				place_producers();
				place_products();
			}		
		}
		else
		{
			//show the progress clock
			//document.getElementById("").style.display="block";
		}
	}			
}

/*
	Receives the text data, filters out any non desirable info and puts it into a div for further processing 
*/
function filter_request(http_request)
{
	var str = http_request.responseText;
	
	//filter out some stuff
	str = str.replace(/<meta.*\/>/ig, "");
	str = str.replace(/\&nbsp\;/gi, "");

	//solving a bug in parsing in IE
	str = str.replace(/width=\"([^\"]*)\"/ig, "width=&quot;$1&quot;");
	str = str.replace(/width=\"([^\']*)\'/ig, "width=&quot;$1&quot;");
	
	
	var tmp_div = document.createElement(tmp_div);
	//testing
	tmp_div.innerHTML = str;

	return(tmp_div);

}

/* Adds the set of tabs inside a multidiv*/
function add_MultidivTabs(tab_list, multidiv_div, multidiv_id)
{
	/*we do not want to put a tab when there is only one tab. We first count how many there are*/
	var count_tabs = 0;
	for(var j=0; j<tab_list.length; j++)
	{	
		var dad_id = tab_list.item(j).parentNode.attributes.getNamedItem("ID").nodeValue;
		if(dad_id == multidiv_id && tab_list.item(j).tagName == "xdiv")
		{			
			count_tabs++;
		}
	}

	//within each multidiv we create the set of tabs according to the xml file
	if(count_tabs>1)
	{
		var nav_div = add_elem({elem_type:"div", elem_dad:multidiv_div, elem_class:"nav_div"});			
		var nav_ul = add_elem({elem_type:"ul", elem_dad:nav_div});
	}
	var k=0; //number of actual tabs
	for(var j=0; j<tab_list.length; j++)
	{
		/*WARNING: not all elements in the passed in list might pertain to the desired
			level of multidiv Tabs. We need to check for each one wether its dad's ID 
			is the one passed.
			Also, some selected elements might not be divs, we need to check
		*/
		var dad_id = tab_list.item(j).parentNode.attributes.getNamedItem("ID").nodeValue;
		if(dad_id == multidiv_id && tab_list.item(j).tagName == "xdiv")
		{	
			if(!div_is_empty(tab_list.item(j)))
			{		
				//Add the information as a div
				//We add all the info in this div keeping the order it has in the xml			
				var info_div_title = tab_list.item(j).attributes.getNamedItem("title").nodeValue;
				//check if the div has an id to give
				var info_div_id;
				if(tab_list.item(j).attributes.getNamedItem("id"))
				{	
					info_div_id = tab_list.item(j).attributes.getNamedItem("id").nodeValue;
				}
				else
				{
					var info_div_id = "tab-" + tab_number;
				}
				
				var info_div = add_elem({elem_type:"div", elem_dad:multidiv_div, elem_class:"info_div", elem_id:info_div_id});				
				//keep track of the div
				tablist_id[tab_number] = info_div_id;
				tablist_title[tab_number] = info_div_title;
				tablist_multidiv[tab_number] = multidiv_id;
				tab_number++;
				 
				var children = tab_list.item(j).childNodes;
				add_content(children, info_div);
			
				//within the ul we create the different li tabs
				//Create the tab
				//<li><span><span>sub1</span></span></li>
				if(count_tabs>1)
				{			
					var show_class;
					if(k==0)
					{
						info_div.style.display = "block"
						show_class = "selected_tab";
					}
					else
					{
						info_div.style.display = "none"
						show_class = "unselected_tab";
					}			
					var nav_li_onclick = "switchMultidiv('"+multidiv_id+"','"+info_div_id+"')";
					var nav_li = add_elem({elem_type:"li", elem_dad:nav_ul, elem_onclick:nav_li_onclick, elem_class:show_class, elem_id:info_div_id+"_tab"});
					var nav_span1 = add_elem({elem_type:"span", elem_dad:nav_li});
					var nav_span2 = add_elem({elem_type:"span", elem_dad:nav_span1, elem_text:info_div_title});
				}
				k++;
			}
		}									
	}		
}

/*
Checks wether a div is empty or not
It is empty if it only contains comments and text fields
*/
function div_is_empty(test_div)
{
	for(var i=0; i<test_div.childNodes.length; i++)
	{
		if(test_div.childNodes.item(i).nodeType != 3 && test_div.childNodes.item(i).nodeType != 8)
		{
			return false;
		}
	}
	return true;
}

/* Adds the set of tabs inside a SUB*/
function add_SubTabs(tab_list, multidiv_div, multidiv_id)
{
	//within each multidiv we create the set of tabs according to the xml file
	var nav_div = add_elem({elem_type:"div", elem_dad:multidiv_div, elem_class:"nav_div"});			
	var nav_ul = add_elem({elem_type:"ul", elem_dad:nav_div});
	var k=0; //number of actual tabs
	for(var j=0; j<tab_list.length; j++)
	{
		/*WARNING: not all elements in the passed in list might pertain to the desired
			level of multidiv Tabs. We need to check for each one wether its dad's ID 
			is the one passed.
			Also check that the tag name is "div"
		*/
		var dad_id = tab_list.item(j).parentNode.attributes.getNamedItem("ID").nodeValue;
		if(dad_id == multidiv_id && tab_list.item(j).tagName == "xdiv")
		{		
			//Add the information as a div
			//We add all the info in this div keeping the order it has in the xml			
			//var info_div = document.createElement("div");
			var info_div_title = tab_list.item(j).attributes.getNamedItem("title").nodeValue;
			
			//check if the div has an id to give
			var info_div_id;
			if(tab_list.item(j).attributes.getNamedItem("id"))
			{	
				info_div_id = tab_list.item(j).attributes.getNamedItem("id").nodeValue;
			}
			else
			{
				var info_div_id = "tab-" + tab_number;
			}
			tab_number++;
			
			var info_div = add_elem({elem_type:"div", elem_dad:multidiv_div, elem_class:"info_sub", elem_id:info_div_id});				
			
			var children = tab_list.item(j).childNodes;
			add_content(children, info_div);
		
			//within the ul we create the different li tabs
			//Create the tab
			//<li><span><span>sub1</span></span></li>
			var show_class;
			if(k==0)
			{
				info_div.style.display = "block"
				show_class = "selected_tab";
			}
			else
			{
				info_div.style.display = "none"
				show_class = "unselected_tab";
			}
			var nav_li_onclick = "switchSub('"+multidiv_id+"','"+info_div_id+"')";
			var nav_li = add_elem({elem_type:"li", elem_dad:nav_ul, elem_onclick:nav_li_onclick, elem_class:show_class, elem_id:info_div_id+"_tab"});
			var nav_span1 = add_elem({elem_type:"span", elem_dad:nav_li});
			var nav_span2 = add_elem({elem_type:"span", elem_dad:nav_span1, elem_text:info_div_title});
			k++;
		}									
	}		
}


function add_content(children_list, father_div)
{
	for(var k=0; k<children_list.length; k++)
	{
		var regexp = /#BeginSnippet/i; //used to detect snippetmaster snippets

		//for each child see what kind it is and act accordingly			
		if(children_list[k].nodeType == 8 && children_list[k].nodeValue.match(regexp))
		{
			//we have a snippetmaster start tag, we need to follow until its end
			regexp = /#EndSnippet/i;
			k++;
			
			//
			/*
			var ending_match = null
			while(!ending_match  && children_list[k].nodeType != 8 && k<children_list.length)
			{				
				var tmp_node = _importNode(children_list[k], true);
				if(tmp_node)
				{
					father_div.appendChild(tmp_node);
				}
				//dummy thing to solve the event handlers registration in IE
				var tmp_div_id = father_div.attributes.getNamedItem("id").nodeValue;
				document.getElementById(tmp_div_id).innerHTML = document.getElementById(tmp_div_id).innerHTML;
				
				//prepare next element				
				k++;		
				alert(children_list[k].nodeValue);
				if(children_list[k].nodeValue)
				{
					ending_match = children_list[k].nodeValue.match(regexp);
				}	
			}
			*/
						
			//
			var finish_process = null;
			while(!finish_process)
			{
				if(children_list[k].nodeType == 8)
				{
					//we have a comment
					//only check if the comment has content
					if(children_list[k].nodeValue)
					{
						var ending_match = children_list[k].nodeValue.match(regexp);						
						if(ending_match)
						{					
							//we got to the end of the snippet
							finish_process = 1;
						}
					}
				}
				else
				{
					if(k >= children_list.length)
					{
						//check that we do not go overboard
						finish_process = 1;
						alert("Reached end of snippet without snippet end tag");
					}
					else
					{
						if(children_list[k].nodeType != 8)
						{
							//only insert non comment items
							var tmp_node = _importNode(children_list[k], true);
							if(tmp_node)
							{
								father_div.appendChild(tmp_node);
							}
							//dummy thing to solve the event handlers registration in IE
							var tmp_div_id = father_div.attributes.getNamedItem("id").nodeValue;
							document.getElementById(tmp_div_id).innerHTML = document.getElementById(tmp_div_id).innerHTML;							
						}
					}
				}
				k++;
			}
			
			
		}
		if(children_list[k].tagName == "snippet")
		{
			/*this looks into the domHTML and retrieves the DIV whose ID is equal to that passed as argument*/
			var snippet_id = children_list[k].attributes.getNamedItem("id").nodeValue;
			var snippet_href = children_list[k].attributes.getNamedItem("href").nodeValue;
			//document.innerHTML = domHTML;
			var snippet_div = find_snippet(divHTML[snippet_href], snippet_id);
			//alert(snippet_div);
			if(snippet_div == null)
			{
				alert("No data found for div ID: " + snippet_div);
			}			
			else
			{
				//found the div, add all its children to the father div recursively
				for(var i=0; i<snippet_div.childNodes.length; i++)
				{
					if(snippet_div.childNodes.item(i).nodeType != 8)
					{
						//only insert non comment items
						var tmp_node = _importNode(snippet_div.childNodes.item(i), true);
						if(tmp_node)
						{
							father_div.appendChild(tmp_node);
						}
						//dummy thing to solve the event handlers registration in IE
						//var tmp_div_id = father_div.attributes.getNamedItem("id").nodeValue;
						//document.getElementById(tmp_div_id).innerHTML = document.getElementById(tmp_div_id).innerHTML;
						father_div.innerHTML = father_div.innerHTML;							
					}
				}
				
			}
		}
		if(children_list[k].tagName == "xbr")
		{
			add_elem({elem_type:"br", elem_dad:father_div});
		}
		else if(children_list[k].tagName == "xp")
		{
			var p_class = children_list[k].attributes.getNamedItem("classID").nodeValue;
			var p_text = children_list[k].firstChild.nodeValue;
			var p_element = add_elem({elem_type:"p", elem_dad:father_div, elem_class:p_class, elem_text:p_text});
		}
		else if(children_list[k].tagName == "ximg")
		{
			/*I create a div to contain both image and caption
				The DIV's size is the image's, the caption is pushed down and a passing is defined by css*/
			var img_class = children_list[k].attributes.getNamedItem("classID").nodeValue;
			var img_alt = children_list[k].attributes.getNamedItem("alt").nodeValue;
			var img_src = children_list[k].attributes.getNamedItem("src").nodeValue;
			if(children_list[k].firstChild != null)
			{
				var img_text = children_list[k].firstChild.nodeValue;
			}
			else
			{
				var img_text = null;
			}	
			var img_div = add_elem({elem_type:"div", elem_dad:father_div, elem_class:img_class});
			img_div.style.padding = "0% 0% 5% 0%"; /*enforce some padding to include the caption*/
			add_elem({elem_type:"img", elem_dad:img_div, elem_alt:img_alt, elem_src:img_src, elem_width:"100%", elem_height:"100%"});
			if(img_text != null) {add_elem({elem_type:"span", elem_dad:img_div, elem_text:img_text, elem_width:"100%"});}

		}
		else if(children_list[k].tagName == "xa")
		{
			var a_text = children_list[k].firstChild.nodeValue;
			var a_class = children_list[k].attributes.getNamedItem("classID").nodeValue;
			var a_href = children_list[k].attributes.getNamedItem("href").nodeValue;
			var a_element = add_elem({elem_type:"a", elem_dad:father_div, elem_class: a_class, elem_href: a_href, elem_text: a_text});
		}
		else if(children_list[k].tagName == "xsub")
		{
			/*
				A sub is a set of divs positioned as a multidiv, with tabs.
				The sub is given an id and all divs inside it are shown one at a time
				We need to nest 2 divs to have all classes represented
			*/
			
			var sub_class = children_list[k].attributes.getNamedItem("classID").nodeValue;
			var sub_id = children_list[k].attributes.getNamedItem("ID").nodeValue;

			var new_subdiv = add_elem({elem_type:"div", elem_dad:father_div, elem_id:sub_id, elem_class:sub_class + " multisub"});		
			new_subdiv.style.display = "block";
			var div_list = children_list[k].childNodes;
			add_SubTabs(div_list, new_subdiv, sub_id);
						
		}
		else if(children_list[k].tagName == "xdiv")
		{
			/*
				A div contains any of the previous content items.
				it has an id and a class
			*/
			
		}
		else if(children_list[k].tagName == "album")
		{
			/*let's create a pictures album*/
			var pictures_list = children_list[k].getElementsByTagName("foto");
			make_album(pictures_list, father_div);
		}
	}
}

/*
	finds and returns a div with a certain ID
*/
function find_snippet(source_div, snippet_id)
{
	//return source_div.getElementById(snippet_id);

	var div_list = source_div.getElementsByTagName("div");	
	for(var i=0; i<div_list.length; i++)
	{
		if(div_list.item(i).attributes)
		{
			if(div_list.item(i).attributes.getNamedItem("id"))
			{
				var div_id = div_list.item(i).attributes.getNamedItem("id").nodeValue;
				if(div_id == snippet_id)
				{
					return div_list.item(i);
				}
			}
		}	
	}
	return null;

}



/*make any kind of element under a certain element*/
function add_elem(options)
{
	/*check for mandatory parameters*/
	if(!options['elem_dad'])
	{
		alert("Error in add_element: need to define a father node");
	}
	if(!options['elem_type'])
	{
		alert("Error in add_element: need to define an element type");
	}

	/*create the element and properties*/
	var new_elem = document.createElement(options["elem_type"]);
	
	if(options['elem_width'])
	{
		/*IE6 needs this*/
		new_elem.style.width=options['elem_width'];
	}
	if(options['elem_height'])
	{
		/*IE6 needs this*/
		new_elem.style.height=options['elem_height'];
	}
	if(options['elem_margin'])
	{
		/*IE6 needs this*/
		new_elem.style.margin=options['elem_margin'];
	}	
	if(options['elem_text'])
	{
		var new_text = document.createTextNode(options["elem_text"]);
		new_elem.appendChild(new_text);
	}
	if(options['elem_onclick'])
	{
		/*IE6 needs this*/
		new_elem.onclick = function(){eval(options['elem_onclick']);};
	}
	if(options['elem_font_size'])
	{
		var new_attr = document.createAttribute("font-size");
		new_attr.nodeValue = options["elem_id"];
		new_elem.setAttributeNode(new_attr);		
	}
	if(options['elem_id'])
	{
		var new_attr = document.createAttribute("id");
		new_attr.nodeValue = options["elem_id"];
		new_elem.setAttributeNode(new_attr);		
	}
	if(options['elem_class'])
	{
		var new_attr = document.createAttribute("class");
		new_attr.nodeValue = options["elem_class"];
		new_elem.setAttributeNode(new_attr);		
	}
	if(options['elem_href'])
	{
		var new_attr = document.createAttribute("href");
		new_attr.nodeValue = options["elem_href"];
		new_elem.setAttributeNode(new_attr);		
	}
	if(options['elem_src'])
	{
		var new_attr = document.createAttribute("src");
		new_attr.nodeValue = options["elem_src"];
		new_elem.setAttributeNode(new_attr);		
	}
	if(options['elem_alt'])
	{
		var new_attr = document.createAttribute("alt");
		new_attr.nodeValue = options['elem_alt'];
		new_elem.setAttributeNode(new_attr);		
	}
	if(options['elem_border'])
	{
		var new_attr = document.createAttribute("border");
		new_attr.nodeValue = options['elem_border'];
		new_elem.setAttributeNode(new_attr);		
	}	
	if(options['elem_type2'])
	{
		var new_attr = document.createAttribute("type");
		new_attr.nodeValue = options['elem_type2'];
		new_elem.setAttributeNode(new_attr);		
	}	
	
	//append the created element and return it
	options['elem_dad'].appendChild(new_elem);
	return new_elem ;
}

