/* ENEWSLETTER ARCHIVE scripts */

//for past years -- select / dropdown
function selectIssue(selected_value, year){
	if (selected_value=='') { return; }
	//alert(selected_value + 'selected!' + 'year: '+year);
	var yearMonth = year+selected_value;
	var yearMonth_txt = numToMonth(selected_value) + ' ' + year;
	var fileName = 'eNews/enews'+yearMonth+'.html';
		
	/* for the select which is active, change its class so that its appearance indicates that it is active, and that the other(s) are inactive */
	var archiveOlder=document.getElementById('drawer_older');
	var archiveDrawers = archiveOlder.getElementsByClassName('drawer');  //array
	//alert('there are '+archiveDrawers.length+ ' years in the archive');
	
	//if the current year matches this div id, add class "active"; else remove this class
	for (var y=0; y<archiveDrawers.length; y++){
		$(archiveDrawers[y]).removeClass('active');
		
		if (archiveDrawers[y].id == year.toString()) {
			//alert('this is the current year: '+archiveDrawers[y].id);
			$(archiveDrawers[y]).addClass('active');	
		} else { 
			//for the 'inactive' years get the select and reset the value to ''
			var drawerSelect = archiveDrawers[y].getElementsByTagName('select')[0];
			drawerSelect.value = '';
		}  
	}
	
	loadIssue('divLoadNews', fileName, yearMonth_txt, yearMonth);	

	}  


function numToMonth(num){
	switch(num){
		case '01':
			return 'January';
			break;
		case '02':
			return 'February';
			break;		
		case '03':
			return 'March';
			break;	 
		case '04':
			return 'April';
			break;
		case '05':
			return 'May';
			break;
		case '06':
			return 'June';
			break;
		case '07':
			return 'July';
			break;		
		case '08':
			return 'August';
			break;
		case '09':
			return 'September';
			break;
		case '10':
			return 'October';
			break;
			
		case '11':
			return 'November';
			break;
			
		case '12':
			return 'December';
			break;	
		
		default:
			return 'Month';
			break;
	}
} 

//show the said element
function showIt(showMe) {
	showMe.style.display = 'block';
	showMe.style.visibility = 'visible';
}

//hides the said element
function hideIt(hideMe) {
	hideMe.style.display = 'none';
	hideMe.style.visibility = 'hidden';
}

function showArchive() {
	
	var showlink = document.getElementById('link_showArchive');
	var hidelink = document.getElementById('link_hideArchive');
	var divarchive = document.getElementById('divArchive');
	
	showlink.setAttribute("class", "off"); //For Most Browsers
	showlink.setAttribute("className", "off"); //For IE; harmless to other browsers.
	
	hidelink.setAttribute("class", "on"); 
	hidelink.setAttribute("className", "on"); 
	
	showIt(divarchive);
	
	showIt(hidelink);
	
	hideIt(showlink); 
}

function hideArchive() {
	
	var showlink = document.getElementById('link_showArchive');
	var hidelink = document.getElementById('link_hideArchive');
	var divarchive = document.getElementById('divArchive');
	
	showlink.setAttribute("class", "on"); //For Most Browsers
	showlink.setAttribute("className", "on"); //For IE; harmless to other browsers.
	
	hidelink.setAttribute("class", "off"); 
	hidelink.setAttribute("className", "off");
	
	hideIt(divarchive);
	
	hideIt(hidelink);
	//showLink.className += "on";
	showIt(showlink); 
	
}

//load page -- newsletter -- into div
function loadIssue(id, url, issue, issueNum){
	var xmlHttp;
	try {// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {// Internet Explorer

		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {

			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {

				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = function(){

		if (xmlHttp.readyState == 4) {
			//Get the response from the server and extract the section that comes in the body section of the second html page avoid inserting the header part of the second page in your first page's element
			var respText = xmlHttp.responseText.split('<body link="#308ebf" alink="#415eb8" vlink="#b89c46">');
			//try this
			var news_css = "<style type='text/css'>table td {padding: 0;}</style>";
			elem.innerHTML = news_css+respText[1].split('</body>')[0];
			//turn this issue's link "on"
			newslinklist = document.getElementById("newsLinkList");
			for (i=0; i<newslinklist.childNodes.length; i++) {
				node = newslinklist.childNodes[i];
				if (node.nodeName=="LI") {
					//alert("we are now on: "+issueNum);
					var matchThis = "eNews_"+issueNum;
					if (node.id==matchThis){
						node.setAttribute("class", "on"); 
						node.setAttribute("className", "on");
					} else {
						node.setAttribute("class", "off"); 
						node.setAttribute("className", "off");
					}		
				}
			}
			
			//and update box heading
			info.innerHTML = "<h3>Clinton Foundation News: "+issue+"</h3>";
		}
	}

	var elem = document.getElementById(id);
	if (!elem) {
		alert('The element with the passed ID doesn\'t exists in your page');
		return;
	}

	var info = document.getElementById("info");
	
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}



