/* Default Physician Directory setting TomTest*/
var specialty=13;

/* Default Newsletter settings */
var moduletype=3;
var newsletter=26;

/* Global variable for pageName to use for menus and next article links*/
var s_pn=location.href.toLowerCase(); var s_pn_clean = s_pn.split('?'); s_pn = s_pn_clean[0]; s_pn_clean = s_pn.split('#'); s_pn = s_pn_clean[0];

/* brings in date for header */
var now = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number)	{
	return (number < 1000) ? number + 1900 : number;
}
wmd_today =  months[now.getMonth()] + " " + date + ", " + (fourdigits(now.getYear()));

/* function to change a class */
function setClass(objectID,newClass) {
	var object = document.getElementById(objectID);
	if (object) {
		object.className = newClass;
	}
}

/* function to toggle visibility */
function toggle( targetId, signId ) {
   if ( document.getElementById ) {
    target = document.getElementById( targetId );
	if (signId != '') {sign = document.getElementById( signId );}
    if ( target.style.display == "none" ) {
     target.style.display = "";
	 if (sign) {sign.src = sign.src.replace('plusSign','minusSign');}
    } else {
     target.style.display = "none";
	 if (sign) {sign.src = sign.src.replace('minusSign','plusSign');}
    }
   }
}

/* function to add an onload event */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  } else 
    {
       window.onload = function() 
    {
      oldonload();
      func();
    }
  }
}	

/* function to wait till DOM ONLY is loaded */
addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;

            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }

        load_events.push(func);
    }
})();
/* function to go back to previous page */
function timedReturn()
	{
	var t=setTimeout("history.go(-1)",60000)
	}

/* function to check e-mail address validity on a form */
 function checkEmailAddr(formId) 
 {
  theForm = document.getElementById('newsletterForm');
  theEmail = document.getElementById('eMail');
  
  if(formId) {
   theForm = document.getElementById('newsletterForm' + formId);
   theEmail = document.getElementById('eMail' + formId);
  }
 
  illegal = /[^\w._\-]/;
  email = theForm.EmailAddr.value;
 
  if (email.length<6 || email.indexOf("@")==-1 || email.indexOf('.')==-1)
  { email=""; }
  else 
  {
   At = email.indexOf("@");
   Period = email.lastIndexOf('.');
   DNS1 = email.substring(0,At);
   if (DNS1.length<1 || DNS1.match(illegal)!=null) email="";
   DNS2 = email.substring(At+1,Period);
   if (DNS2.length<1 || DNS2.match(illegal)!=null) email="";
   DNS3 = email.substring(Period+1,email.length);
   if (DNS3.length<2 || DNS3.match(illegal)!=null) email="";
  }
 
  if (email.length==0) 
  {
   alert("The email address you have entered is invalid. Please re-enter a valid email address.\n");
   theEmail.focus();
   return false;
  }
  
  return true;
 }

 
/* function for omniture tracking */
function sl(url, link_type, bi_tag)
{
	var href = new String(url.href);
	
	// used for Omniture Tracking
	// Figures out if the page has webmd3. For the other external webmd owned assets, they already have biscripts to pull the cookie
	if (href.indexOf("webmd3") == -1) {
		ctrs(bi_tag);
	}
	else {
		wmdPageLink(bi_tag);
	}	

	var status,toolbar,width,height,resizable,scrollbars,menubar;
	
	var left = 0;
	var top = 0;

	if (href.indexOf("/video/") != -1) {
		width=860;
		height=730;
		top=10;
		left=10;
		scrollbars="no";
		resizeable="no";
		status="yes";
		left = 25;
		top = 25;
	}
	else 
		if (href.indexOf("/healthmanager.") != -1) {
			return openAssessPopup(url);			
		}
		else
			if ((href.indexOf("/content/article") != -1 || href.indexOf("/content/pages") != -1 || href.indexOf("/solutions") != -1 || href.indexOf("/content/Article") != -1 || href.indexOf("/content/Pages") != -1) && (link_type != 'sp')) {
				return true; // don't do anything
			}
			else 
				if(link_type == '' || link_type == 'normal' || link_type == 'regular')
				{
					return true; // don't do anything
				}
				else if(link_type == 'sp')
				{
				   width = 380;
				   height = 210;
				   toolbar = 0;
				   resizable = 1;
				   status = 0;
				   scrollbars = 1;
				   left = 25;
				   top = 25;
				}				
				else if(link_type == 'sdp')
				{
				   width = 600;
				   height = 700;
				   scrollbars = 1;
				   toolbar = 0;
				   resizable = 1;
				   status = 0;
				   left = 25;
				   top = 25;
				}				
				else if(link_type == 'scp')
				{
				   width = 530;
				   height = 490;
				   scrollbars = 1;
				   toolbar = 0;
				   resizable = 1;
				   status = 0;
				   left = 25;
				   top = 25;
				}				
				else if(link_type == 'ai')
				{
				   width = 715;
				   height = 600;
				   scrollbars = 1;
				   toolbar = 0;
				   resizable = 1;
				   status = 0;
				   left = 25;
				   top = 25;
				}
				else if(link_type == 'nw')
				{
					// just open a new window
					//window.open(url);
					//return false;
				   width = 1000;
				   height = 600;
				   scrollbars = 1;
				   toolbar = 1;
				   resizable = 1;
				   status = 1;
				   left = 25;
				   top = 25;
				}
				else
				if(link_type == 'hw')
				{
					width = 675;
					height = 626;
					scrollbars = 1;
					toolbar = 1;
					resizable = 1;
					status = 1;
					left = 25;
					top = 25;
				}
				else
				{
					return true;
				}

	//alert(url);

	var newWindow = window.open(url,"","width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",scrollbars=" + scrollbars + ",toolbar=" + toolbar + ",resizable=" + resizable + ",status=" + status + ",menubar=" + menubar);
	
	//if(	moveWindow == true)
	//	newWindow.moveTo(XPosition,YPosition);

	return false;
}


/* For the AB Test */
function writePersistentCookie (CookieName, CookieValue) {
  var expireDate = new Date ();
  
  var year = expireDate.getYear();     
  // Note some browsers give only the years since 1900, and some since 0.
  if (year < 1000) year = year + 1900;  
  
  expireDate.setYear(year + 1);
  
  document.cookie = escape(CookieName ) + "=" + escape(CookieValue) + "; expires=" + expireDate.toGMTString() + "; domain=webmd.com; path=/";
}  

function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
	exp.exec (document.cookie + ";");
	return unescape(RegExp.$1);
  }
  else return false;
}

function chooseSearchVer() {
var randomnumber=Math.floor((Math.random()*9)+1);
if (randomnumber <= 3) {
	writePersistentCookie('whichSearch','a');
	return 'a';
}
if (randomnumber > 3 && randomnumber <=6) {
	writePersistentCookie('whichSearch','b');
	return 'b';
}
writePersistentCookie('whichSearch','c');
return false;
}

//For the search bar
function  DoSearch(srchForm) {
	var query, sourceType, url, srchOmnitureValue;
	if (srchForm == undefined) { srchForm = "topSrchBar";}
	//get the query, set ctrs
	if (srchForm == "topSrchBar") {
		query = escape(document.getElementById("searchQuery_fmt").value);
		srchOmnitureValue = 'srch-bar_submit';
	} else if (srchForm == "botSrchBar") {
		srchOmnitureValue = 'srch-bar-foot_submit';
		query = escape(document.getElementById("searchQueryFoot_fmt").value);
	}
	else {
		query = escape(document.getElementById("searchQuery_fmt").value);
		srchOmnitureValue = 'srch-bar-unknown';
	}
	
	if(query.length > 0) {
		try
		{
			ctrs(srchOmnitureValue);	//omniture function
		}
		catch(err)
		{
		}
		
		/* not sure if we still use sourceType for the chrome search, i believe this is left over from the tab search box - paolo */
		if (document.getElementById("sourceType")) {
			sourceType = document.getElementById("sourceType").value;
		}
		if(sourceType == "web_search") {
			window.location.href = "http://www.webmd.com/search/search_results/web_results.aspx?query=" + query + "&sourceType=" + sourceType;
		} else {
			window.location.href = "http://www.webmd.com/search/search_results/default.aspx?query=" + query + "&sourceType=" + sourceType;
		}
	} else {
		ctrs(srchOmnitureValue);
		window.location.href = "http://www.webmd.com/search";
	}
	return false;
}

function submitSearchBoxLanding(ev, srchForm) {
	if((ev.which && ev.which == 13) || (ev.keyCode && ev.keyCode == 13)) {
		return DoSearch(srchForm);
	} else {
		return true;
	}
}


/* function for bookmarking */
function wmdDoBookmark() {
  /* for omniture tracking */
  wmdPageLink('bookmark');
  /* quick check of the browser and OS */
  var agt=navigator.userAgent.toLowerCase();

  /*** PLATFORM ***/
  var is_win = (agt.indexOf('windows')!=-1);
  var is_mac = (agt.indexOf('mac')!=-1);

  /*** BROWSER ***/
  var is_ie     = (agt.indexOf('msie') != -1);
  var is_firefox = (agt.indexOf('firefox') != -1);
  var is_netscape  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('compatible') == -1));
  var is_safari = ((agt.indexOf('safari')!=-1)&&(is_mac));

  if (is_win) {
    if (is_ie && window.external) {
      window.external.AddFavorite(location.href,document.title);
    }
    else {
      if (is_firefox) {
        alert('Please press CTRL + D to bookmark this page.');
      }
      else {
        if (is_netscape) {
          alert('Please press CTRL + D to bookmark this page.');
        }
        else {
          alert('Please press CTRL + D to bookmark this page.');
        }
      }
    }
  }
  else {
    if (is_mac) {
      if (is_ie && window.external) {
        window.external.AddFavorite(location.href,document.title);
      }
      else {
        if (is_safari) {
          alert('Please press Command (Apple) + D to bookmark this page.');
        }
        else {
          alert('Please press UP arrow + Command (Apple) + D to bookmark this page.');
        }
      }
    }
    else {
      alert('Please press CTRL + D to bookmark this page.');
    }
  }
  return false;
}

/* function for health solutions footer */
function isSensTop (priTop,secTop) {
	var isSensTopBool = false;


	if (priTop.charAt(0) == '7') {
		isSensTopBool = true;
	} else {
		var secTopArray = secTop.split("|");
		var curSecTop = 0;

		for (curSecTop in secTopArray) {
			if (secTopArray[curSecTop].charAt(0) == '7') {
				isSensTopBool = true;
				break;		
			}
		}
	}
	
	return isSensTopBool;
}

/* function for healthmanager portland popup */
function openAssessPopup(theUrl) { 
	var win = window.open(theUrl, 'WebMDHealthManagerTool','resizable,scrollbars,width=705,height=600,left=25,top=25,screenX=25,screenY=25'); 
	if (win) { win.focus(); } 
	return false;
}


/* functions for printer friendly articles - paolo */

/* Used to open a printer friendly window */
function printWindow() { 
var x = 800; var y = 600; 
	var url = location.href;

	if (url.search("[\\#]") != -1) {
		url = url.split("#")[0];
	}

	if (url.search("[\\?]") != -1) {
	url = url + '&print=true';
	} else {
	url = url + '?print=true';
	}

	if (navigator.appVersion.indexOf("Mac") > 0) { 
	openWindow(url, "width=" + x + ",height=" + y + ",toolbar=yes,resizable=yes,scrollbars=yes"); 
	} else { 
	openWindow(url, "width=" + x + ",height=" + y + "toolbar=1,location=1,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,top=10,left=10"); 
	} 
} 

/* opens a window */
function openWindow(url, optionString) { 
	window.open(url, "printFriendly", optionString); 
} 

function getParam(name) {
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

function remove(s, t) {
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + remove(s.substring(i + t.length), t);
  return r;
  }

/* insert/customize printer friendly elements in the top of the page */
function printElements() {
	/* swap with smaller print only logo -- keeping it inline because if its in the css as a background it might not print with user settings  */
	//var logo = document.getElementById('logo_rdr').getElementsByTagName('img')[0];
	
	/* usability logo swap */
	var images = document.getElementsByTagName('img');
	for (var i=0; i<images.length; i++) {
		if (images[i].className == 'logo_rdr') {
			var logo = images[i];
		}
	}
	/* /usability logo swap */	
	
	logo.src = image_server_url + '/webmd/consumer_assets/site_images/layout/shared/logo_webmd_print.gif';
	logo.width = '190';
	logo.height = '43';
	logo.style.visibility = 'visible';
	
	var printDiv = document.createElement('div');
	printDiv.setAttribute('id','global-head-print');
	printDiv.innerHTML = '<a class=\"printWindow-button" href=\"#\" onclick=\"javascript:window.print();return sl(this,\'\',\'prnt\');"\"><img src="'+ image_server_url +'/webmd/consumer_assets/site_images/layout/shared/printWindow_button.gif" width="56" height="21" border="0" alt="Print"/></a><a class=\"printWindow-button" href=\"#\" onClick=\"javascript:window.close();\"><img src="'+ image_server_url +'/webmd/consumer_assets/site_images/layout/shared/closeWindow_button.gif" width="107" height="21" border="0" alt="Close Button"/></a></div>';
	
	if (!document.getElementById("ContentPane2")) return false;
	var insert1 = document.getElementById("ContentPane2");
	
	var urlDiv = articleUrl();

	var notesDiv = document.createElement('div');
	notesDiv.setAttribute('id','print_notes');
	notesDiv.innerHTML = '<div><a href=\"#mynotes\">Type in <strong>"My Notes"</strong> to add personal or doctor discussion notes before printing.</a></div>';
		
	insert1.appendChild(printDiv);
	insert1.appendChild(notesDiv);
	insert1.appendChild(urlDiv);
}

/* get article url */
function articleUrl () {
	/* remove print parameter */
	var url = window.location.href;
	var token = "?print=true";
	
	if (url.indexOf("&print") != -1) {
		token = "&print=true";
	}
	
	var ArticleUrl = remove(url, token);
	
	var urlDiv = document.createElement('div');
	urlDiv.setAttribute('id','url_reference');
	urlDiv.innerHTML = '<div>Article Link:<span class="art_link"> '+ ArticleUrl +'</span></div>';
	return urlDiv;
}

/* execute if printerfriendly */

var pf_param = getParam("print");

/* Makes the nav work in IE */
sfHover = function() {
	if (document.getElementById("nav_fmt")) {
	var sfEls = document.getElementById("nav_fmt").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
/* Slide Switcher for Video Module */
function SwitchSlide(obj, pID){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("video").getElementsByTagName("div");
	var sign = document.getElementById(pID);
	var sign_ar = ['sign0','sign1', 'sign2', 'sign3', 'sign4'];
		if(el.style.display == "none"){
			for (var i=0; i<ar.length; i++){
				ar[i].style.display = "none";
			}
			el.style.display = "block";
			for (var j=0; j<sign_ar.length; j++){
				document.getElementById(sign_ar[j]).src = image_server_url + "/webmd/consumer_assets/site_images/modules/todaysNews_plusSign.gif";
			}
			sign.src = image_server_url + "/webmd/consumer_assets/site_images/modules/todaysNews_minusSign.gif";
		}else{
			el.style.display = "none";
			sign.src = image_server_url + "/webmd/consumer_assets/site_images/modules/todaysNews_plusSign.gif";
		}
	}
}

