// JavaScript Document

// Created By Alan Eicker - 3/26/2009 - Last Modified 12/8/2009
// This script controls content for the live events pages.
var browser = navigator.appName;
var xmlhttp;

function loadXMLDoc(sSession) {
	xmlhttp=null;
	// Get current time (will be added to url to prevent browser from returning cached XML)
	var d = new Date();
	var time = d.getTime();
	
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest(); // code for IE7, Firefox, Mozilla, etc.
	} 
	else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE5, IE6
	}
	if (xmlhttp!=null) {
	  xmlhttp.onreadystatechange=onResponse;
	  xmlhttp.open("GET","/education_center/xml/calendar_of_events.xml?var=" + time + "",true);
	  xmlhttp.send(null);
	}
	else {
		alert("Your current browser does not support this application.");
	}
}

function onResponse()
{
	var f = getFirmType();
	var sID = GetSessionID();
	
	if(xmlhttp.readyState!=4) return;
	if(xmlhttp.status!=200) {
		alert("Problem retrieving XML data");
		return;
	}

	//Finds current date
	var date = new Date();
	var m = date.getMonth();
	var d = date.getDate();
	var y = date.getFullYear();
	
	var months = [1,2,3,4,5,6,7,8,9,10,11,12];
	
	var currMonth = months[m];
	
	//Create the output html
	txt="<div>";
	
	var eventName;
	if (currTab == "Webinars") {
		x=xmlhttp.responseXML.documentElement.getElementsByTagName("webinar"); 
	} else {
		x=xmlhttp.responseXML.documentElement.getElementsByTagName("workshop");
	} 
	for (i=0;i<x.length;i++) {
		var nodeId; var linkID; var tbdClass; var imgId;
		
		if (currTab == "Webinars") {
			nodeId = "webinarNode" + i;
			linkID = "webinarDetailsLink" + i;
			imgId = "webinarShowIcon" + i;
			document.getElementById('maplink').style.display = "none";
		}
		
		if (currTab == "Live Events") {
			nodeId = "liveEventNode" + i;
			linkID = "liveEventDetailsLink" + i;
			imgId = "liveEventShowIcon" + i;
			document.getElementById('maplink').style.display = "block";
		}
		
		// Get event month,dat and year from XML
		var xx=x[i].getElementsByTagName("month");
		var xmlMonth = xx[0].firstChild.nodeValue;
		
		var xx=x[i].getElementsByTagName("day");
		var xmlDay = xx[0].firstChild.nodeValue;
		
		var xx=x[i].getElementsByTagName("year");
		var xmlYear = xx[0].firstChild.nodeValue;
		
		// Get specific firm to show
		var xx=x[i].getElementsByTagName("show");
		var xmlShow = xx[0].firstChild.nodeValue;
		
		//if xml node value for date elemnt is past the current date - hide output
		if (xmlShow == "All" || xmlShow == f) {
			if (xmlYear < y || xmlYear == y && xmlMonth < currMonth || xmlYear == y && xmlMonth == currMonth && xmlDay < d) {
				var classname = 'expired';
			} else {
				var classname = 'open';
			}
		}
		if (xmlShow != "All" && xmlShow != f) {
			var classname = 'expired';	
		}
				
		//create content for eventListLeft div
		txt=txt + "<div class='" + classname + "'>";
			txt=txt + "<div class='webinarEventsContainer'>";
				txt=txt + "<div class='newEvent'>";
					txt=txt + "<div class='dateTimeTitle'>";
						if (xmlMonth == "--" && xmlDay == "--") {
							xx=x[i].getElementsByTagName("date_series");
							try {
								txt=txt + xx[0].firstChild.nodeValue;
							} 
							catch (er) {
								txt=txt + "Date &amp; Time TBD";
							}
						} else {
							// Get event date and time
							xx=x[i].getElementsByTagName("time");
							try {
							  txt=txt + xmlMonth + "/" + xmlDay + "/" + xmlYear  + "&nbsp;-&nbsp;" + xx[0].firstChild.nodeValue;
							} 
							catch (er) {
							  txt=txt + "Date &amp; Time TBD";
							}
						}
						// title node to display event title 
						xx=x[i].getElementsByTagName("title");
						try {
							txt=txt + "<br /><strong><a class='eventTitle' title='Event Details' href=\"javascript: showMore('" + nodeId + "','" + imgId + "')\"><img id='" + imgId + "' class='openIcon' src='/images/education_center/plus_icon.gif' alt='Event Details' /> &nbsp;" + xx[0].firstChild.nodeValue + "</a></strong>";
						}
						catch (er) {
							txt=txt + "";
						}
						txt=txt + "</div>";
						// link node to display event registration button 
						xx=x[i].getElementsByTagName("link");
						try {
							if (currTab == "Live Events" && browser != "Microsoft Internet Explorer") { 
								txt=txt + "<a id='" + linkID + "' href=\"javascript: showRegistration('" + xx[0].firstChild.nodeValue  + "&SessionID=" + sID +"')\"><img src='/images/education_center/event_register_btn.gif' alt='Register' class='linkRight' /></a>";
							} else {
								txt=txt + "<a href ='" + xx[0].firstChild.nodeValue + "&SessionID=" + sID + "' target='_blank'><img src='/images/education_center/event_register_btn.gif' alt='Register' class='linkRight' /></a>";
							}
						}
						catch (er) {s
							txt=txt + "";
						}
						//optionlink node to display ODD diclaimer for option Webinars
						xx=x[i].getElementsByTagName("optionlink");
						try {
							txt=txt + "<a href = javascript:OpenODD('" + xx[0].firstChild.nodeValue + "','Agree');><img src='/images/education_center/event_register_btn.gif' alt='Register' class='linkRight' /></a>";					  
						}
						catch (er) {
							txt=txt + "";
						}
						// description node to display event description 
						xx=x[i].getElementsByTagName("description");
						try {
							txt=txt + "<div class='clear'></div><div class='hide' id='" + nodeId + "'>" + xx[0].firstChild.nodeValue + "<br />"; 
						}
						catch (er) {
							txt=txt + "";
						}
						// description node to display futures disclosure 
						xx=x[i].getElementsByTagName("disclosure");
						try {
							if (xx[0].firstChild.nodeValue) {
								txt=txt + "<p class='futures_disclosure'>Futures involve substantial risk and are not appropriate for all investors. Please read \"Risk Disclosure Statement for Futures and Options\" prior to applying for an account. <a href='http://images.optionsxpress.com/static/pdf/security_futures_risks.pdf' target='_blank'>More Information <img src='/images/education_center/pdf_icon.png' width='11' height='12' alt='pdf icon' /></a></p>";
							}
						} 
						catch (er) {
							txt=txt + "";
						}
					txt=txt + "</div>";
				txt=txt + "</div>";
			txt=txt + "</div>";
		txt=txt + "</div>";
	}
	txt=txt + "</div>";
	if (currTab == "Webinars") {
		document.getElementById('currentEventsWebinars').innerHTML=txt;
	} else {
		document.getElementById('currentEventsLiveEvents').innerHTML=txt;
	}
}

// Displays current active tab
var currTab = "Webinars";
function showNewEventTab(newTab,tabNum, sSession) {
	document.getElementById(currTab).style.display = "none";
	document.getElementById(newTab).style.display = "block";
	document.body.className = tabNum;
	currTab = newTab;
	var heading = document.getElementById('eventType');
	heading.innerHTML = "Free " + currTab;
	loadXMLDoc(sSession);
}

// Shows event description and swaps out "show/hide details" text
function showMore(currNode,icon) {
	if (document.getElementById(currNode).className == "hide") {
		document.getElementById(currNode).className = "show";
		document.getElementById(icon).src = "/images/education_center/minus_icon.gif";
	} else {
		document.getElementById(currNode).className = "hide";
		document.getElementById(icon).src = "/images/education_center/plus_icon.gif";
	}
}

// Function to display Google Map for Live Events
function showMap(theMap) {
	var mapdiv = document.getElementById(theMap);
	if (mapdiv.style.display == 'none') {
		mapdiv.style.display = 'block';
		mapdiv.innerHTML = '<div id="mapModule"><div class="spacer"><div class="frameContainer"><iframe class="mapFrame" scrolling="no" frameborder="0" width="1150" height="900" src="http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&msid=115585022624401693375.000465dfca233e5cfa12e&ll=30.501722,-95.835937&spn=69.743654,225&z=4"></iframe></div><div class="titleDiv"><strong>optionsXpress Event Locations</strong></div><div class="closeDiv"><a href="javascript: showMap(\'mapWrapper\');"><strong>Close</strong></a></div><div class="clear"></div></div></div><div class="mapShadow"></div>';
	} else {
		mapdiv.style.display = "none";
		mapdiv.innerHTML = '';
	}
}

// Function to how registration page
function showRegistration(regLink) {
	if (document.getElementById('registrationShadow').style.display == 'none') {
		document.getElementById('registrationShadow').style.display = 'block';
		document.getElementById('registrationShadow').innerHTML = '<div id="registrationWrapper"><div class="registrationModule"><iframe id="regFrame" class="regFrame" scrolling="yes" frameborder="0" width="850" height="650" src=""></iframe></div><p class="close"><a href="javascript: showRegistration();">Close</a></p></div>';
		document.getElementById('regFrame').src = regLink;
	} else {
		document.getElementById('registrationShadow').style.display = 'none';	
		document.getElementById('registrationShadow').innerHTML =  '';
	}
}

// shows ODD disclaimer popup window
function OpenODD(sLink,sName) {
	OpenChildWindowSizeable('/odd_popup.aspx?site=' + sLink + '&name=' + sName,'','690','420');
}