// Section TO CREATE XML ELEMENTS.

var blanktext = document.createTextNode('');
var theCurrContainer = '';
var theCurrCat = "";
var theCurrTd = "";
var todayDate = new Date();
var milliseconds = 0;

function LoadXMLContainer(thefile, thecontainer, thetdid)
{
	theCurrContainer = thecontainer;
	theCurrCat = thefile.split(".")[0];
	theCurrTd = thetdid;
		
	document.getElementById('td001').style.borderTopColor = 'Gray';
	document.getElementById('td002').style.borderTopColor = 'Gray';
	document.getElementById('td003').style.borderTopColor = 'Gray';

	document.getElementById('td001').style.borderRightColor = 'Gray';
	document.getElementById('td002').style.borderRightColor = 'Gray';
	document.getElementById('td003').style.borderRightColor = 'Gray';

	document.getElementById('td001').style.borderBottomColor = 'Gray';
	document.getElementById('td002').style.borderBottomColor = 'Gray';
	document.getElementById('td003').style.borderBottomColor = 'Gray';


	document.getElementById(thetdid).style.borderTopColor = 'Red'; 
	document.getElementById(thetdid).style.borderRightColor = 'Red';
	document.getElementById(thetdid).style.borderBottomColor = 'White';

	
	thePathAndFile = "http://kstptv/xmlfiles/" + thefile;
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = createXMLContainer;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) createXMLContainer()
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load(thePathAndFile);
}

function createXMLContainer()
{
	var x = xmlDoc.getElementsByTagName('STORY');
	var wrkTABLE = document.createElement('TABLE');
	wrkTABLE.setAttribute('cellPadding',1);
	wrkTABLE.setAttribute('cellSpacing',3);
	wrkTABLE.setAttribute('border',0);
	wrkTABLE.setAttribute('width','100%');
	var wrkTBODY = document.createElement('TBODY');
	wrkTABLE.appendChild(wrkTBODY);
	var wrkTR = document.createElement('TR');
	wrkTBODY.appendChild(wrkTR);
	for (i=0;i<x.length;i++)
	{
		var wrkTR = document.createElement('TR');
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;
			if (x[i].childNodes[j].nodeName == 'ID')
			{
				var wrkTD = document.createElement('TD');
				wrkTD.setAttribute('className','xmlIntro');
				var ID = x[i].childNodes[j].firstChild.nodeValue;
			}	
			else if (x[i].childNodes[j].nodeName == 'HEADLINE')
			{
				theLink = "/article/stories/S" +ID+ ".html?cat=1";
				// create a new anchor
				wrkA = document.createElement("a");
				// set anchor attributes e.g. href
				wrkA.setAttribute('href',theLink);
				wrkA.setAttribute('className','fullStory');
				// now create clickable text
				clicktext = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
				// add to the anchor
				wrkA.appendChild(clicktext);
				// stick anchor onto an existing object
				wrkTD.appendChild(wrkA);
			 }
			 else if (x[i].childNodes[j].nodeName == 'LASTUPDATED')
			 {
				var theDate = x[i].childNodes[j].firstChild.nodeValue;
				storyYear = theDate.split('-')[0];
				storyMonth = theDate.split('-')[1] - 1;
				storyDay = theDate.split('-')[2];
				storyHours = theDate.split('-')[3];
				storyMinutes = theDate.split('-')[4];
				storySeconds = theDate.split('-')[5];
				var storyDate = new Date(storyYear, storyMonth, storyDay, storyHours, storyMinutes, storySeconds, milliseconds);
				if((todayDate - storyDate) <= 7200000) //Less or Equal to 2 Hours in millisecons.
				{
					wrkIMG = document.createElement("img");
					wrkIMG.setAttribute('src','/kstpimages/new.jpg');
					wrkIMG.setAttribute('hspace',1);
					wrkTD.appendChild(wrkIMG);
				}	
			 }	
			 else if (x[i].childNodes[j].nodeName == 'MEDIA')
			 {
				var theData = x[i].childNodes[j].firstChild.nodeValue;
				if (theData != '--')
				{
					wrkIMG = document.createElement("img");
					wrkIMG.setAttribute('src','/kstpimages/video.jpg');
					wrkIMG.setAttribute('hspace',2);
					wrkTD.appendChild(wrkIMG);
				}	
			 }	
			 else if (x[i].childNodes[j].nodeName == 'SUMMARY')
			 {
				var summid = s + ID;
				var wrkBR = document.createElement('br');
				wrkTD.appendChild(wrkBR);
				var wrkSPAN = document.createElement('span');
				wrkSPAN.setAttribute('id',summid);
				wrkSPAN.innerHTML = RemoveHTML(x[i].childNodes[j].firstChild.nodeValue);
				wrkTD.appendChild(wrkSPAN);
			 }	
		}
		wrkTR.appendChild(wrkTD);
		wrkTBODY.appendChild(wrkTR);
	}

	wrkTR = document.createElement('TR');
	wrkTD = document.createElement('TD');
	wrkTD.setAttribute('align','right');
	theLink = "/article/" + theCurrCat + "/";
	wrkA = document.createElement("a");
	wrkA.setAttribute('href',theLink);
	wrkA.setAttribute('className','fullStory');
	clicktext = document.createTextNode(".... MORE STORIES");
	wrkA.appendChild(clicktext);
	wrkTD.appendChild(wrkA);
	wrkTR.appendChild(wrkTD);
	wrkTBODY.appendChild(wrkTR);

	if (document.getElementById(theCurrContainer).hasChildNodes())
		document.getElementById(theCurrContainer).replaceChild(wrkTABLE, document.getElementById(theCurrContainer).firstChild); 
	else
		document.getElementById(theCurrContainer).appendChild(wrkTABLE);
	
}

function RemoveHTML(Word) 
{
	a = Word.indexOf("<");
	while (a > -1)
	{
		b = Word.indexOf(">");
		len = Word.length;
		c = Word.substring(0, a);
		if(b == -1) b = a;
		d = Word.substring((b + 1), len);
		Word = c + d;
		a = Word.indexOf("<");
	}	
	loop = 1;
	br = 124;
	while (loop == 1)
	{
		br++;
		if (Word.length < br) loop = 0;
		else 
		if (Word.substr(br, 1) == ' ') 
		{
			Word = Word.substr(0, br);
			loop = 0;
		}
	}
	if (Word.length == br) Word = Word + '...';
	return Word;	
}

function LoadProgramContainer(thefile)
{
	theProgramFile = "http://kstptv/xmlfiles/" + thefile;
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = createProgramContainer;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) createProgramContainer()
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load(theProgramFile);
}

function createProgramContainer()
{
	var starloading = 0;
	var wrkbackground = '#191970';
	var x = xmlDoc.getElementsByTagName('PROGRAM');
	var wrkTABLE = document.createElement('TABLE');
	wrkTABLE.setAttribute('cellPadding',2);
	wrkTABLE.setAttribute('cellSpacing',0);
	wrkTABLE.setAttribute('border',0);
	wrkTABLE.setAttribute('width','100%');
	var wrkTBODY = document.createElement('TBODY');
	wrkTABLE.appendChild(wrkTBODY);
	for (i=0;i<x.length;i++)
	{
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;
			if (x[i].childNodes[j].nodeName != 'TIME') continue;
			
			var theDate = x[i].childNodes[j].firstChild.nodeValue;
			storyYear = theDate.split('-')[0];
			storyMonth = theDate.split('-')[1] - 1;
			storyDay = theDate.split('-')[2];
			storyHours = theDate.split('-')[3];
			storyMinutes = theDate.split('-')[4];
			storySeconds = theDate.split('-')[5];
			var storyDate = new Date(storyYear, storyMonth, storyDay, storyHours, storyMinutes, storySeconds, milliseconds);

			if(storyDate <= todayDate) continue;

			starloading = i;
			j = x[i].childNodes.length;
		}
		if (starloading > 0) i = x.length;
	}		
	
	if (starloading > 0) starloading--;
	endloading = starloading + 5;
	
	for (i=starloading;i<endloading;i++)
	{
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;
			if (x[i].childNodes[j].nodeName == 'TIME')
			{
				var theDate = x[i].childNodes[j].firstChild.nodeValue;
				storyYear = theDate.split('-')[0];
				storyMonth = theDate.split('-')[1] - 1;
				storyDay = theDate.split('-')[2];
				storyHours = theDate.split('-')[3];
				storyMinutes = theDate.split('-')[4];
				storySeconds = theDate.split('-')[5];
				if (storyHours < 12)
				{
					if (storyHours == 0) wrkHour = 12;
					else wrkHour = storyHours;
					ampm = 'am';
				}	
				else	
				{
					if (storyHours == 12) wrkHour = storyHours;
					else wrkHour = storyHours - 12;
					ampm = 'pm';
				}	
				if (storyMinutes > 0) 
					thetime = wrkHour + ":" + storyMinutes + ampm; 
				else 
					thetime = wrkHour + ampm;
				
				var wrkTR = document.createElement('TR');
				var wrkTD = document.createElement('TD');
				wrkTD.setAttribute('className','xmlProgram');
				wrkTD.setAttribute('align','right');
				wrkTD.setAttribute('bgColor',wrkbackground);
				timetext = document.createTextNode(thetime);
				wrkTD.appendChild(timetext);
				wrkTR.appendChild(wrkTD);
			}	
			else 
			{
				var wrkTD = document.createElement('TD');
				wrkTD.setAttribute('className','xmlProgram');
				wrkTD.setAttribute('width','100%');
				wrkTD.setAttribute('bgColor',wrkbackground);
				var wrkSPAN = document.createElement('span');
				wrkSPAN.innerHTML = RemoveHTML(x[i].childNodes[j].firstChild.nodeValue);
				wrkTD.appendChild(wrkSPAN);
				wrkTR.appendChild(wrkTD);
			 }	
		}
		wrkTBODY.appendChild(wrkTR);
		if (wrkbackground == 'blue') wrkbackground = '#191970';
		else wrkbackground = 'blue';
	}

	wrkTR = document.createElement('TR');
	wrkTD = document.createElement('TD');
	wrkTD.setAttribute('align','right');
	wrkTD.setAttribute('colSpan',2);
	wrkTD.setAttribute('bgColor','blue');
	theLink = "/article/" + theCurrCat + "/";
	wrkA = document.createElement("a");
	wrkA.setAttribute('href',theLink);
	wrkA.setAttribute('className','fullSchedule');
	clicktext = document.createTextNode(".... FULL SCHEDULE");
	wrkA.appendChild(clicktext);
	wrkTD.appendChild(wrkA);
	wrkTR.appendChild(wrkTD);
	wrkTBODY.appendChild(wrkTR);
	
	if (document.getElementById('ProgramSchedule').hasChildNodes())
		document.getElementById('ProgramSchedule').replaceChild(wrkTABLE, document.getElementById('ProgramSchedule').firstChild); 
	else
		document.getElementById('ProgramSchedule').appendChild(wrkTABLE);

}



