/*
* 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 "";
	}
}

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", "560", "253", "0", "#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(num, obj) {
	sNum = getCookie('loginURL');
	if(num > -1 && num < 4) {
		setCookie('loginChoice', obj.firstChild.nodeValue, new Date(Date.parse("Jan 1, 2050")));
		setCookie('loginURL', num, new Date(Date.parse("Jan 1, 2050")));
	}

	clearTab();
	obj.className = 'active';
	
	var tabURL = translateURL(num);

	
	// set cookies
	if (sNum != num) {
		setCookie('loginURL', num, 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 translateURL(num) {
	switch(num) {
		case "1":
			return "/homepage/logins/agent-login.shtml";
			break;
		case "2":
			return "/homepage/logins/group-login.shtml";
			break;
		case "3":
			return "/homepage/logins/provider-login.shtml";
			break;
		default:
			return "/homepage/logins/member-login.shtml";
			break;
	}
}
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() {
	var noflash = document.getElementById("no-flash");
	if(noflash) {
		noflash.style.display = "block";
	}
	// break out of frames
	frameCheck();
	
	if(document.getElementById("findA"))
		toggleFields(); 

    clearValues();
	
	// replace specialty options with those in provider_search.js
    if(document.getElementById("findA"))
		switchSpecialties(null);
	
	var sChoice = getCookie('loginChoice');
	var sURL = getCookie('loginURL');
	
	/* catch legacy cookies */
	if(sURL.match(/member-login.shtml$/)) {
		sURL = 0;
		setCookie('loginURL', 0, new Date(Date.parse("Jan 1, 2050")));
	}
	else if(sURL.match(/agent-login.shtml$/)) {
		sURL = 1;
		setCookie('loginURL', 1, new Date(Date.parse("Jan 1, 2050")));
	}
	else if(sURL.match(/group-login.shtml$/)) {
		sURL = 2;
		setCookie('loginURL', 2, new Date(Date.parse("Jan 1, 2050")));
	}
	else if(sURL.match(/provider-login.shtml$/)) {
		sURL = 3;
		setCookie('loginURL', 3, new Date(Date.parse("Jan 1, 2050")));
	}
	var defaultTab = 'Member';
 	
	loginFrame = parent.document.getElementById("login-frame");
	
	if(sChoice) {
		switchTab(sChoice);
	}
	else {
		switchTab(defaultTab);
	}
	top.frames['login-frame'].location.replace(translateURL(sURL));
//	document.getElementById("currentYear").innerHTML = new Date().getFullYear();
};
