var doform = false; 

function init() {
       // quit if this function has already been called
       if (arguments.callee.done) return;

       // flag this function so we don't do the same thing twice
       arguments.callee.done = true;

       // create the "page loaded" message
       //var text = document.createTextNode("Page loaded!");
       //var message = document.getElementById("message");
       //message.appendChild(text);
       initPage();
   };

   /* for Mozilla */
   if (document.addEventListener) {
       document.addEventListener("DOMContentLoaded", init, null);
   }

   /* for Internet Explorer */
   /*@cc_on @*/
   /*@if (@_win32)
       document.write("<script defer src=\"/wgbh/rxforsurvival/js/ie_onload.js\"><"+"/script>");
   /*@end @*/

   /* for other browsers */
   window.onload = init;
   
function initPage()
{ initRollovers(); if(doform)
{ initForm();}
  initGlossary();
}
function clearSearchBox()
{ if (document.getElementById('q').value == 'enter search')
{ document.getElementById('q').value = '';}
}
function initRollovers()
{ if (!document.getElementById) return
var aPreLoad = new Array(); var aImages = new Array(); var sTempSrc; var aImages = document.getElementsByTagName("img"); for (var i = 0; i < aImages.length; i++)
{ if (aImages[i].className.match('img-roll'))
{ var src = aImages[i].getAttribute('src'); var ftype = src.substring(src.lastIndexOf('.'), src.length); var hsrc = src.replace(ftype, '-over'+ftype); aImages[i].setAttribute('hsrc', hsrc); aPreLoad[i] = new Image(); aPreLoad[i].src = hsrc; aImages[i].onmouseover = function()
{ sTempSrc = this.getAttribute('src'); this.setAttribute('src', this.getAttribute('hsrc'));}
aImages[i].onmouseout = function()
{ if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_on'+ftype, ftype); this.setAttribute('src', sTempSrc);}
}
}
var aImages = document.getElementsByTagName("input"); for (var i = 0; i < aImages.length; i++)
{ if (aImages[i].className.match('img-roll'))
{ var src = aImages[i].getAttribute('src'); var ftype = src.substring(src.lastIndexOf('.'), src.length); var hsrc = src.replace(ftype, '-over'+ftype); aImages[i].setAttribute('hsrc', hsrc); aPreLoad[i] = new Image(); aPreLoad[i].src = hsrc; aImages[i].onmouseover = function()
{ sTempSrc = this.getAttribute('src'); this.setAttribute('src', this.getAttribute('hsrc'));}
aImages[i].onmouseout = function()
{ if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_on'+ftype, ftype); this.setAttribute('src', sTempSrc);}
}
}
}
function initPopUps()
{ if (!document.getElementById("int-w780")) return; var aHref = document.getElementById("int-w780").getElementsByTagName("a"); var nnn = ""; for (var i = 0; i < aHref.length; i++)
{ if (aHref[i].className.match('pop-up'))
{ alert("match"); aHref[i].setAttribute('title', "launch in new window"); aHref[i].onclick = function()
{ window.open(this.href, 'horiz', 'width=421,height=316,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false;}
}
}
}
function localize_me(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'news', 'width=421,height=446,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false; return false;}
function videoplayer(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'horiz', 'width=431,height=580,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false; return false;}
function newsletter(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'news', 'width=631,height=670,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1'); return false; return false;}
function launchpic(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'piclaunch', 'width=440,height=274,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false; return false;}
function launch_atlas(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'atklast', 'width=740,height=530,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false;}
function launch_chart(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'chart', 'width=740,height=530,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false;}

function send_to_friend(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'sendfriend', 'width=421,height=595,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false;}
function ask_experts(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'experts', 'width=540,height=625,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1'); return false;}
function matters(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'matters', 'width=620,height=585,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false;}
function who(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'who', 'width=580,height=625,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1'); return false;}
function discuss(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'discussion', 'width=550,height=685,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1'); return false;}
function pol_inf(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'politics', 'width=540,height=625,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1'); return false;}
function pol_call(a_tag)
{ if (!a_tag) return true; window.open(a_tag.href, 'politics', 'width=540,height=625,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1'); return false;}
function setCookie(name, value, expires, path, domain, secure)
{ document.cookie= name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "; path=/wgbh/rxforsurvival/") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");}
function getCookie(name)
{ var dc = document.cookie; var prefix = name + "="; var begin = dc.indexOf("; " + prefix); if (begin == -1)
{ begin = dc.indexOf(prefix); if (begin != 0) return false;}
else
{ begin += 2;}
var end = document.cookie.indexOf(";", begin); if (end == -1)
{ end = dc.length;}
return unescape(dc.substring(begin + prefix.length, end));}
function deleteCookie(name, path, domain)
{ if (getCookie(name))
{ document.cookie = name + "=" + ((path) ? "; path=" + path : "; path=/wgbh/rxforsurvival") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";}
}



function initGlossary()
{
	if(!document.getElementById || !document.getElementById('dl-glossary')) return;
	
	var dts = document.getElementById('dl-glossary').getElementsByTagName("dt");
	
	//for nav
	var prevletter = "";
	var curletter  = "";
	var newHTML    = "";
	var divider    = "";
	
	//back to top link
	var dd = document.createElement("dd");
	dd.innerHTML = '<p><a href="#top">back to top</a><br /><br /></p>';	

  
	//loop over images
	for (var i = 0; i < dts.length; i++)
	{	
		curletter = dts[i].innerHTML.substring(0, 1).toLowerCase();
		
		
		//soemtimes a tag at start so we should skip?
		if(curletter == '<') 
		{
			curletter= dts[i].getElementsByTagName("a")[0].innerHTML.substring(0, 1).toLowerCase();
		}
		
		if((curletter != '<') && (curletter != prevletter))
		{
			newHTML += divider + '<a href="#' + curletter + '">' + curletter + '</a>';
			divider = ' | ';
			
			//dts[i].setAttribute('name', curletter);
			dts[i].innerHTML = '<a name="' + curletter + '"></a>' + dts[i].innerHTML;
			
			
			if(prevletter != '')
			{
				//add back to top link with new dd
				var dd2 = dd.cloneNode(true);	
				document.getElementById('dl-glossary').insertBefore(dd2,dts[i]);
			}
		}
		
		prevletter = curletter;
	}
	
	document.getElementById('glossary-nav').innerHTML = '<p>' + newHTML + '<br /><br /></p>';

}



/**
 * initializes video
 **/
function initForm()
{
    //if we don't support getElementById then exit
	if (!document.getElementById) return
	
    if(doform)
    {
        //safari does not support ununload properly
        var bodyElt;
        if (navigator.userAgent.toLowerCase().indexOf('safari')!=-1)
        {
            bodyElt = document.getElementsByTagName("body")[0];
        }

        if (bodyElt)
        {
            bodyElt.addEventListener("unload", save_newsletter, false);
        }
        else
        {
            window.onunload = save_newsletter;
        }

        //write out appropiate things
        write_newsletter();
    }
}


/**
 * writes out controls
 **/
function write_newsletter()
{
    var f_name = document.forms.newsletter.name;
    var f_email = document.forms.newsletter.email;

    //name
    f_name_value = getCookie("rx-name");
    if(f_name_value)
    {
        f_name.value  = f_name_value;
    }

    //email
    f_email_value = getCookie("rx-email");
    if(f_email_value)
    {
        f_email.value = f_email_value;
    }


    //format
    f_format_value = getCookie("rx-format");
    if(f_format_value)
    {
        var f_format = document.forms.newsletter.list_name;
        for (i=0;i<f_format.length;++ i)
        {
            if(f_format_value.match(f_format[i].value))
            {
                f_format[i].checked = true;
            }
            else
            {
                f_format[i].checked = false;
            }
        }
    }


    //opt-in
    var f_optin_value = getCookie("rx-optin");
    if(f_optin_value)
    {
        var f_optin = document.forms.newsletter.optin;
        for (i=0;i<f_optin.length;++ i)
        {
            if(f_optin_value.match(f_optin[i].value))
            {
                f_optin[i].checked = true;
            }
            else
            {
                f_optin[i].checked = false;
            }
        }
    }

}


/**
 * writes out controls
 **/
function save_newsletter()
{
    //name fields
    var f_name = document.forms.newsletter.name.value;
    var f_email = document.forms.newsletter.email.value;

    setCookie("rx-name",f_name);
    setCookie("rx-email",f_email);

    //format
    var f_format_value = "";
    var f_format = document.forms.newsletter.list_name;
    for (i=0;i<f_format.length;++ i)
    {
        if (f_format[i].checked)
        {
            f_format_value =  f_format[i].value;
        }
    }
    setCookie("rx-format",f_format_value);

    //opt-in
    var f_optin_value = "";
    var sep = "";
    var f_optin = document.forms.newsletter.optin;
    for (i=0;i<f_optin.length;++ i)
    {
        if (f_optin[i].checked)
        {
            f_optin_value +=  sep + f_optin[i].value;
            sep = "|";
        }
    }
    setCookie("rx-optin",f_optin_value);
    //alert(f_optin_value + " \n  " + f_format_value);

}