/*
* Home Page Javascript
* Use in conjunction with homepage_search.js
* Date: 10.16.2007 - Edited: 07.29.2008 9:17 am
* Author: Gerard Konars
*/
// rev 004
// Create a cookie to remember chosen login
// setCookie(cheese, 'fresh mozzarella', )
function setCookie(sName, sValue, oExpires) {
	var sCookie = sName + "=" + encodeURIComponent(sValue);
	
	if (oExpires) {
		nowTime = new Date().getTime();
		sCookie += "; expires=" + oExpires.toGMTString() + "; " + nowTime;
	}

	document.cookie = sCookie;
}

function getCookie(sName) {
	
	var sRE = "(?:; )?" + sName + "=([^;]*);?";
	var oRE = new RegExp(sRE);
	if (oRE.test(document.cookie)) {
		return decodeURIComponent(RegExp["$1"]);
	} else {
		return null;
	}
}

function deleteCookie(sName) {
	setCookie(sName, "", new Date(0));
}

// Reload the Flash movie.
function reloadFlash() {
	
	if(thecookie) {
		deleteCookie(thecookie);
	}
	
	var theNewCookie = getCookie('loginChoice');
    var so = new SWFObject("homepage_cookie_alt.swf", "flashcontent", "447", "266", "8", "#FFFFFF");
   	so.addParam("wmode", "transparent");	
	if(thecookie) {
		so.addVariable("in_cookie", theNewCookie);
	} else {
		so.addVariable("in_cookie", "Member");
	}

   so.write("left-main");
}

// Removes 'active' class from all tabs
function clearTab() {
	// locate constituents links and remove class attribute
	var constTabs = document.getElementById("constituents");
	var tabLinks = constTabs.getElementsByTagName("a");
	for (var i=0; i < tabLinks.length; i++) {
		tabLinks[i].className = '';
	}
}

// LOAD IFRAME: uses href attribute for location, sets class active to current link
// SET TABS: sets active class to the link.
function loadIframe(theLink) {
	sChoice = getCookie('loginChoice');
	sURL = getCookie('loginURL');
	
	clearTab();
	
	// set class name of this link to 'active'
	theLink.className = 'active';
	var tabChoice = theLink.firstChild.nodeValue;
	var tabURL = theLink.href;
	
	// set cookies
	if (!sChoice || (sChoice != tabChoice)) {
		setCookie('loginChoice', tabChoice, new Date(Date.parse("Jan 1, 2050")));
		setCookie('loginURL', tabURL, new Date(Date.parse("Jan 1, 2050")));
	}
	
		top.frames['login-frame'].location.replace(tabURL); // does not put iframes into browser history (desired effect)
		reloadFlash();
}

function switchTab(x) {
	clearTab();
	constTabs = parent.document.getElementById("constituents"); //declaring "var constTabs" breaks the functionality.
	var tabLinks = parent.constTabs.getElementsByTagName("a");
	
		for (var j=0; j<tabLinks.length; j++) {
			if (tabLinks[j].firstChild.nodeValue == x) {
					tabLinks[j].className = 'active';	
				}
		}
}


// CLEAR VALUES: clean up GET variables when returning from a search via back button.
var clearValues = function() {
	if (window.location.search !== "") {
		window.location.search = "";
	}
};

function frameCheck() {
	if (top.location != self.location) {
		top.location.href = document.location.href;
	}
}
		
window.onload = function() {
	
	// break out of frames
	frameCheck();
	
	toggleFields();
	clearValues();
	
	// replace specialty options with those in provider_search.js
	switchSpecialties(null);
	
	var sChoice = getCookie('loginChoice');
	var sURL = getCookie('loginURL');
	
	var defaultTab = 'Member';
 
	var defaultURL = '/homepage/logins/member-login.shtml';
	
	loginFrame = parent.document.getElementById("login-frame");
	
	if (sChoice && sURL) {
	//	alert(sChoice + " = " + sURL);
		switchTab(sChoice);
//		loginFrame.setAttribute("src", sURL);
		top.frames['login-frame'].location.replace(sURL);
	} else {
		switchTab(defaultTab);
//		loginFrame.setAttribute("src", defaultURL)
		top.frames['login-frame'].location.replace(defaultURL);
	}

};