/*CS Get and write the region media info to the page*/

//Write info to page
function regMedDiv(xmlDoc) {
	var newDiv = document.createElement('newDiv');
	var regionMediaCoverage = xmlDoc.getElementsByTagName('regionMediaCoverage'); 	
	var region = regionMediaCoverage[0].getElementsByTagName("region");	

	var prTable = document.createElement('table');
	prTable.setAttribute('id','statsTable');
	prTable.setAttribute('cellpadding','3');
	prTable.setAttribute('cellspacing','0');
	prTable.setAttribute('width','90%');
	var prTbody = document.createElement('tbody');
	document.getElementById('mediaCoverage').appendChild(newDiv);//append new div to existing div
	newDiv.appendChild(prTable); //append table to div
	prTable.appendChild(prTbody); //append tbody to table
	
	for (j=0; j<region.length; j++) {
		var regionName = region[j].getAttribute("name");		
		if(regionName == regionPage) {
			var newsItem = region[j].getElementsByTagName("newsItem"); 
			for (i=0; i<newsItem.length; i++) {										
				var prRow = document.createElement('tr');
				var prCell = document.createElement('td');
				var para = document.createElement('p');
				var strong = document.createElement('strong');
				var br = document.createElement('br');
				var br2 = document.createElement('br');
							
				//All the elements from XML to write to the page
				var htmlPath= newsItem[i].getAttribute("html"); //Path to html page
				var mediaOutlet = document.createTextNode(newsItem[i].getAttribute("mediaOutlet"));
				var date = document.createTextNode(newsItem[i].getAttribute("date")); //Date
				var headline = newsItem[i].getAttribute("headline").toString();
				
				if(headline.match("}")) {//If there is something to be italicized
					var italEl = document.createElement('i');
		
					//Get index of after{ and before }
					var startX = headline.indexOf("{");
					var endX = headline.lastIndexOf("}");
					
					var hdA = headline.slice(0,startX);
					var hdItal = headline.slice(startX+1,endX); //The text to italize
					var hdC = headline.slice(endX+1,headline.length);
					
					headlineA = document.createTextNode(hdA); //Headlinetext A
					headlineItal = document.createTextNode(hdItal);
					headlineC = document.createTextNode(hdC);
					
					italEl.appendChild(headlineItal);
					
					headline = document.createElement('span');
					headline.appendChild(headlineA);
					headline.appendChild(italEl);
					headline.appendChild(headlineC);					
				}
				else {
					headline = document.createTextNode(newsItem[i].getAttribute("headline")); //Headlinetext
				}		
	
				//Link for headline	
				var headlineLink = document.createElement('a'); 
				headlineLink.setAttribute('href',htmlPath);
				headlineLink.setAttribute('target','_blank');
				headlineLink.appendChild(headline);
				
				prTbody.appendChild(prRow); //append row to tbody
				prRow.appendChild(prCell); //append cell to row
				prCell.appendChild(para);
				para.appendChild(strong);
				strong.appendChild(mediaOutlet);
				para.appendChild(br);
				para.appendChild(headlineLink);
				para.appendChild(br2);
				para.appendChild(date);
			}		
		}
	}
}
