// JavaScript Document// JavaScript Document

var leftd, rightd, isIE, state, cx;

var m_aoi = "conus";  //set aoi value to conus as default
var m_earliest = new Date(2000, 0, 1);  //set the earliest available dm archives which is not used for anything

var m_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var m_name;
var m_cd, m_pd;
var m_xml;

var m_isIE = true;
var m_load = true;

//sets the conus radio and sets up the page for the current conus archive (archive.html)
function archiveInit() {
	try {
		document.getElementById('conusRadio').checked = true;
		setCompare();
		m_load = false;
	} catch (e) {
		window.location.href = '../error.htm';
	}
}

//sets up the two columns with images, downloads, and the table for comparison (archive.html)
function setCompare() {
	try {
		var isIE = false;
		if (navigator.appName.indexOf("Microsoft") != -1) 
			isIE = true;
			
		m_isIE = isIE;
	
		//get the aoi name
		var name;
		if (m_aoi == "conus") {
			name = m_aoi;
		} else if (m_aoi == "region") {
			name = document.getElementById('selRegion').value;
		} else if (m_aoi == "state") {
			name = document.getElementById('selState').value;
		}
		
		m_name = name;
		
		//and set up both columns based on the existence of the archive file as before
		var date = new Date();
		//date = new Date(2008, 11, 9);
		
		
		
		var dt, cd;
		var xmlFile, xml, xslFile, xsl;
		if ((date.getDay() == 4) || (date.getDay() == 3)) {
			dt = ReturnTuesday(date);
			cd = returnShortDate(dt);
			var n = "20";
			if (cd.substr(0,2) == "99") 
				n = "19";
				
			if (name == "conus") {
				xmlFile = 'archive/' + n + cd + '/tabular/' + name.toLowerCase() +'_archive_' + cd + '.xml';
			} else {
				xmlFile = 'archive/' + n + cd + '/tabular/' + name.toLowerCase() +'_' + cd + '.xml';
			}	
			
			//xslFile = 'scripts/dm_dropdown.xsl';
			xslFile = 'scripts/dm_select.xsl';
			try {
				/*if (isIE) {
					xml = loadDocIE(xmlFile);
					//xsl = loadXslIE(xslFile);
					
					//ie is stupid
					
					var xsl2 = loadXslIE(xslFile);
			
					var s = BuildTable(cd, cd, xml, xsl2);
				} else {*/
					xml = loadFromUrl(xmlFile);
					
					var xsl2 = loadFromUrl(xslFile);
					
					var s = BuildTable(cd, cd, xml, xsl2);
					
					if (s == "") {
						throw 'stupid javascript';
					}
					//xsl = loadFromUrl(xslFile);
				//}
				
				
			} catch (e) {
				//reset the date to the previous thursday
				dt = ReturnThursday(dt);
			}
	
		} else {
			dt = ReturnThursday(date);		
		}
		
		//now get the tuesday for that week 
		var dtTues = ReturnTuesday(dt);
		cd = returnShortDate(dtTues);
	
		theDate = date;
		
		var pd, previousDate;
		
		pd = ReturnThursday(dtTues);
		pd = ReturnTuesday(pd);
		previousDate = returnShortDate(pd);
		
		leftd = previousDate;
		rightd = cd;
		
		m_cd = cd;
		m_pd = previousDate;
		
		var n = "20";
		if (cd.substr(0,2) == "99") 
			n = "19";
			
		var np = "20";
		if (previousDate.substr(0,2) == "99") 
			np = "19";
		
		if (name =="conus") {
			xmlFile = 'archive/' + n + cd + '/tabular/' + name.toLowerCase() +'_archive'+'_' + cd + '.xml';
		} else {
			xmlFile = 'archive/' + n + cd + '/tabular/' + name.toLowerCase() +'_' + cd + '.xml';
		}
		
		xslFile = 'scripts/dm_dropdown.xsl';
		
		//try {
			/*if (isIE) {
				xml = loadDocIE(xmlFile);
				xsl = loadXslIE(xslFile);
			} else {*/
				xml = loadFromUrl(xmlFile);
				xsl = loadFromUrl(xslFile);
			//}
			
			m_xml = xml;
			
			//set the date combo with the long list, otherwise it won't automatically update with choice
			document.getElementById('prevDate').innerHTML = BuildCombo(cd, xml, xsl, 'updateLeft', previousDate, 'prevDateList');
			document.getElementById('currDate').innerHTML = BuildCombo(cd, xml, xsl, 'updateRight', cd, 'currDateList');
			
			//set the images
			document.getElementById('prevImage').innerHTML = '<IMG SRC="' + 'archive/' + np + previousDate + '/pics/' + name.toLowerCase() + '_dm_'+ previousDate +'.jpg" ' + ' width="' + 460 + '" height="' + 417 + '"/>';
			document.getElementById('currentImage').innerHTML = '<IMG SRC="' + 'archive/' + n + cd + '/pics/' + name.toLowerCase() + '_dm_'+ cd +'.jpg" ' + ' width="' + 460 + '" height="' + 417 + '"/>';
			
			
			//and the links
			var names = name;
			
			url = 'archive/' + np + previousDate + '/pics/' + names.toLowerCase() + '_dm_'+ previousDate +'.png';
			var currurl = 'archive/' + n + cd + '/pics/' + names.toLowerCase() + '_dm_'+ cd +'.png';
			if (names.toLowerCase() == 'conus') {
				names = 'total';
				url = 'archive/' + np + previousDate.substring(0,2) + '/drmon' + previousDate.substring(2,6) +'.gif';
				currurl = 'archive/' + n + cd.substring(0,2) + '/drmon' + cd.substring(2,6) +'.gif';
			}
			
			pdf = 'archive/' + np + previousDate + '/pdfs/'+names.toLowerCase()+'_dm_'+previousDate+'.pdf';
			document.getElementById('prevDld').innerHTML = 'Download <a href="' + url + '">image</a> or <a href="' + pdf + '">PDF</a><br>Read the <a href="javascript:openSummary(' + np + previousDate +');">summary</a>';
		

			pdf = 'archive/' + n + cd + '/pdfs/'+names.toLowerCase()+'_dm_'+cd+'.pdf';
			document.getElementById('currDld').innerHTML = 'Download <a href="' + currurl + '">image</a> or <a href="' + pdf + '">PDF</a><br>Read the <a href="javascript:openSummary(' + n + cd +');">summary</a>';
			
			//and finally the table
			if (!m_load) {
				xslFile = 'scripts/dm_select.xsl';
				var xsl2;
				/*if (isIE) {
					xsl2 = loadXslIE(xslFile);
				} else {*/
					xsl2 = loadFromUrl(xslFile);
				//}
				document.getElementById('data').innerHTML = BuildTable(cd, previousDate, xml, xsl2);
			}
			
			
			
		//} catch (e) {
			//document.getElementById('prevImage').innerHTML = '<h4>Invalid Data Request.</h4>' + '<p>' + e + '</p>';
		//}
	} catch (e) {
		throw "setCompare error";
	}
	

}

function openSummary(lngDate) {
	window.open('archive/' + lngDate + '/nar' + lngDate + '.xml', 'archNarr', 'width=600,height=700,scrollbars=yes,resizable=yes');
	//window.location = 'archive/' + lngDate + '/nar' + lngDate + '.xml';
}

//updates the aoi value
function setAoi(combo) {
	m_aoi = combo.value;
}

//checks the radio button if the region/state combo is modified (archive.html, dmtabs_archive.htm)
function setRadio(combo) {
	if (combo.id == "selState") {
		document.getElementById('stRadio').checked = true;
		m_aoi = "state";
	} else if (combo.id == "selRegion") {
		document.getElementById('rgnRadio').checked = true;
		m_aoi = "region";
	}
}

/* 	builds the comboboxes based on the dates available in the xml file (archive.html)
*/
function BuildCombo(cd, xml, xsl, ca, sd, name) {
	
	try { 
		var theHtml = '<form name="dateForm" method="post" action="">';
	
		var str = '';
		/*if (m_isIE) {
			var cache = new ActiveXObject("MSXML2.XSLTemplate.4.0");
			cache.stylesheet = xsl;
			var processor = cache.createProcessor();
			processor.input = xml;
			processor.addParameter("currDate", cd);
			processor.addParameter("comboAction", ca);
			processor.addParameter("selDate", sd);
			processor.transform();
			str = processor.output;
			
			
		} else {*/
			var processor = new XSLTProcessor();
			processor.importStylesheet(xsl);
			processor.setParameter("", "currDate", cd);
			processor.setParameter("", "comboAction", ca);
			processor.setParameter("", "selDate", sd);
			processor.setParameter("", "cbName", name);
			var outDoc = processor.transformToDocument(xml);  
			
			//i have absolutely no idea why this fixes the problem for firefox 3. 
			/*outDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
			outDoc.setProperty("SelectionLanguage", "XPath");
			
			var nodelist = outDoc.selectNodes('transformiix:result');*/
			
			var xmlSerializer = new XMLSerializer();  
			var xmlString = xmlSerializer.serializeToString(outDoc);
			str = xmlString ;
			
		//}
	
		return theHtml + str + '</form>';
	} catch (e) {
		throw "buildCombo error";
	}
	
}

/*	builds a table with two records, one for each date sent in
	(archive.html)
*/
function BuildTable(cd, pd, xml, xsl) {
	var str
	
	try {
		/*if (m_isIE) {
			var cache = new ActiveXObject("MSXML2.XSLTemplate.4.0");
			cache.stylesheet = xsl;
	
			var processor = cache.createProcessor();
			processor.input = xml;
	
			processor.addParameter("currDate", cd);
			processor.addParameter("prevDate", pd);
			processor.transform();
			str = processor.output;
	
		} else {*/
			var processor = new XSLTProcessor();
			processor.importStylesheet(xsl);
			processor.setParameter("", "currDate", cd);
			processor.setParameter("", "prevDate", pd);
			var outDoc = processor.transformToDocument(xml); 
			
			//i have absolutely no idea why this fixes the problem for firefox 3. 
			outDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
			outDoc.setProperty("SelectionLanguage", "XPath");
			
			var nodelist = outDoc.selectNodes('transformiix:result');
			
			var xmlSerializer = new XMLSerializer();  
			str = xmlSerializer.serializeToString(outDoc);
		//}
		
		return str;
	} catch (e) {
		throw "buildTable error";
	}
}

//resets the leftside image, downloads, table based on the selected date from the combobox (archive.html)
function updateLeft(combo) {
	
	try {
		var pd = combo.value;
		
		var ad;
		if (pd.substr(0,2) == "99") {
			ad = "19" + pd;
		} else {
			ad = "20" + pd;
		}
		
		//update the image
		var url = 'archive/' + ad + '/pics/' + m_name.toLowerCase() + '_dm_'+pd+'.jpg';
		document.getElementById('prevImage').innerHTML = '<IMG SRC="' + url + '" ' + ' width="' + 460 + '" height="' + 417 + '">';
			
		//update the pdf link
		url = 'archive/' + ad + '/pics/' + m_name.toLowerCase() + '_dm_'+ pd +'.png';
		var names = m_name;
			if (names.toLowerCase() == 'conus') {
				names = 'total';
				url = 'archive/' + ad.substring(0,4) + '/drmon' + ad.substring(4,8) +'.gif';
			}
			
		pdf = 'archive/' + ad + '/pdfs/'+names.toLowerCase()+'_dm_'+pd+'.pdf';
		document.getElementById('prevDld').innerHTML = 'Download <a href="' + url + '">image</a> or <a href="' + pdf + '">PDF</a><br>Read the <a href="javascript:openSummary(' + ad +');">summary</a>';
		
		//and the table
		var xslFile = 'scripts/dm_select.xsl';
		var xsl2;
		/*if (m_isIE) {
			xsl2 = loadXslIE(xslFile);
		} else {*/
			xsl2 = loadFromUrl(xslFile);
		//}
		
		document.getElementById('data').innerHTML = BuildTable(m_cd, pd, m_xml, xsl2);
		
		m_pd = pd;
	} catch (e) {
		throw "updateLeft error " + e;
	}
	
}

//resets the rightside image, downloads, table based on the selected date from the combobox (archive.html)
function updateRight(combo) {
	
	try {
		var cd = combo.value;
		
		var ad;
		if (cd.substr(0,2) == "99") {
			ad = "19" + cd;
		} else {
			ad = "20" + cd;
		}
		
		//update the image
		var url = 'archive/' + ad + '/pics/' + m_name.toLowerCase() + '_dm_'+cd+'.jpg';
		document.getElementById('currentImage').innerHTML = '<IMG SRC="' + url + '" ' + ' width="' + 460 + '" height="' + 417 + '">';
			
		//update the pdf link
		
		var names = m_name;
		
		url = 'archive/' + ad + '/pics/' + names.toLowerCase() + '_dm_'+ cd +'.png';
			if (names.toLowerCase() == 'conus') {
				names = 'total';
				url = 'archive/' + ad.substring(0,4) + '/drmon' + ad.substring(4,8) +'.gif';
			}
		
		
		pdf = 'archive/' + ad + '/pdfs/'+names.toLowerCase()+'_dm_'+cd+'.pdf';
		document.getElementById('currDld').innerHTML = 'Download <a href="' + url + '">image</a> or <a href="' + pdf + '">PDF</a><br>Read the <a href="javascript:openSummary(' + ad +');">summary</a>';
		
		//and the table
		var xslFile = 'scripts/dm_select.xsl';
		var xsl2;
		/*if (m_isIE) {
			xsl2 = loadXslIE(xslFile);
		} else {*/
			xsl2 = loadFromUrl(xslFile);
		//}
		
		document.getElementById('data').innerHTML = BuildTable(cd, m_pd, m_xml, xsl2);
		
		m_cd = cd;
	} catch (e) {
		throw "updateRight error";
	}

}

function moveLeft(dir) {
	var combo = document.getElementById('prevDateList');
	//sort out which date to go to
	var pd = combo.value;
	var m =pd.substring(2, 4) - 1;
	var day = pd.substring(4);
	
	var ad;
		if (pd.substr(0,2) == "99") {
			ad = "19" + pd.substr(0,2);
		} else {
			ad = "20" + pd.substr(0,2);
		}

	var nd = new Date(ad, m, day);
	if(dir=='prev') {
		nd = addDays(nd, -7);
	} else {
		nd = addDays(nd, 7);
	}
	
	//set the combo selection to the new date
	var nds = returnShortDate(nd);
	
	if (nd >= m_earliest && nd <= new Date()) {
		combo.value = nds;
		//update the map, etc
		updateLeft(combo);
	}
}

function moveRight(dir) {
	var combo = document.getElementById('currDateList');
	//sort out which date to go to
	var pd = combo.value;
	var m =pd.substring(2, 4) - 1;
	var day = pd.substring(4);
	
	var ad;
		if (pd.substr(0,2) == "99") {
			ad = "19" + pd.substr(0,2);
		} else {
			ad = "20" + pd.substr(0,2);
		}

	var nd = new Date(ad, m, day);
	if(dir=='prev') {
		nd = addDays(nd, -7);
	} else {
		nd = addDays(nd, 7);
	}
	
	//set the combo selection to the new date
	var nds = returnShortDate(nd);
	
	if (nd >= m_earliest && nd <= new Date()) {
		combo.value = nds;
		//update the map, etc
		updateRight(combo);
	}
}


//loads the current conus xml file and sets the radio for the table archive page onload event (dmtabs_archive.htm)
function tableInit() {
	try {
		document.getElementById('conusRadio').checked = true;
		ArchiveTable();
	} catch (e) {
		window.location.href = '../error.htm';
	}
}

/*	updates the table for the current date and selected aoi
	always uses the current date because all records will be added
	(dmtabs_archive.htm)
*/
function ArchiveTable() {
	try {  
		var isIE = false;
		if (navigator.appName.indexOf("Microsoft") != -1) 
			isIE = true;
			
		m_isIE = isIE;
		
		//get the aoi value
		var aoi = m_aoi;	
		var name = '';
		if ((aoi == "conus") || (aoi == "total")) {
			name = aoi + '_archive';
		} else if (aoi == "region") {
			name = document.getElementById('selRegion').value;
		} else if (aoi == "state") {
			name = document.getElementById('selState').value;
		}
		
		var date = new Date();
		//date = new Date(2007, 3, 5);
		
		var dt, cd;
		var xmlFile, xml, xslFile, xsl;
		if ((date.getDay() == 4) || (date.getDay() == 3)) {
			dt = ReturnTuesday(date);
			cd = returnShortDate(dt);
			var n = "20";
			if (cd.substr(0,2) == "99") 
				n = "19";
			xmlFile = 'archive/' + n + cd + '/tabular/' + name.toLowerCase() + '_' + cd + '.xml';
			xslFile = 'scripts/dm_dropdown.xsl';
			try {
				/*if (m_isIE) {
					xml = loadDocIE(xmlFile);
					xsl = loadXslIE(xslFile);
					
					//ie is stupid
				} else {*/
					xml = loadFromUrl(xmlFile);
					xsl = loadFromUrl(xslFile);
				//}
				
				var s = BuildTable(cd, cd, xml, xsl);
				
					if (s == "") {
						throw 'stupid javascript';
					}
			} catch (e) {
				//reset the date to the previous thursday
				dt = ReturnThursday(dt);
			}
	
		} else {
			dt = ReturnThursday(date);		
		}
		
		//now get the tuesday for that week 
		var dtTues = ReturnTuesday(dt);
		cd = returnShortDate(dtTues);
		
		var n = "20";
		if (cd.substr(0,2) == "99") 
			n = "19";
		
		//get the xml file 
		var xmlFile = 'archive/' + n + cd + '/tabular/' + name.toLowerCase() +'_' + cd + '.xml';
		
		//get the xsl file
		xslFile = 'scripts/ndmcdmstates.xsl';
		
		var str = '';
		/*if (isIE) {
			xml = loadDocIE(xmlFile);
			xsl = loadXslIE(xslFile);
			
			//and just set the table here
			var cache = new ActiveXObject("MSXML2.XSLTemplate.4.0");
			cache.stylesheet = xsl;
	
			var processor = cache.createProcessor();
			processor.input = xml;
			processor.transform();
			str = processor.output;
		} else {*/
			//uses sarissa which is supposed to work with some non IE browsers
			xml = loadFromUrl(xmlFile);
			xsl = loadFromUrl(xslFile);
			
			//and just set the table here
			var processor = new XSLTProcessor();
			processor.importStylesheet(xsl);
			var outDoc = processor.transformToDocument(xml);  
			var xmlSerializer = new XMLSerializer();  
			str = xmlSerializer.serializeToString(outDoc);
		//}
		
		document.getElementById('tab').innerHTML = str;
	} catch (e) {
		window.location.href = '../error.htm';
	}
}


