//adding a comment for cvs changes
function popUp(page, dimensions)
	{
		window.open(page, '', dimensions);
	}

//setUserVars-- re-added 20030904
function setUserVars()
	{
		if(navigator.appName.indexOf('Internet Explorer') != -1)
			{
				window.isIE = true;
				//alert("isIE = " + window.isIE);
			}
		window.userPlatform = navigator.platform;
		if((window.userPlatform.indexOf("mac") != -1) || (window.userPlatform.indexOf("MAC") != -1) || (window.userPlatform.indexOf("Mac") != -1))
			{
				window.userPlatform = "mac";
				//alert(window.userPlatform);
			}
	}
setUserVars();

//Files for Dynamic Meta File Generator Start Here

function adCompatible(mediaPreference)
{
	
	if( ((window.isIE) && (window.userPlatform != "mac")) && ((mediaPreference == "WM") || (mediaPreference == "RM")) )
	{
		// alert("true");
		// alert(mediaPreference);
		return true;
	}
	else
	{
		// alert("false");
		// alert(mediaPreference);
		return false;
	}
	// return true;
}


function compareDates(date1, date2)
	{
		beforeorafter = "before";

		date1 = parseDate(date1);
		date2 = parseDate(date2);

		day1 = date1["day"];
		day1 = Math.round(day1);
		
		month1 = date1["month"];
		month1 = toNumericMonth(month1);
		year1 = date1["year"];

		day2 = date2["day"];
		day2 = Math.round(day2);
		
		month2 = date2["month"];
		month2 = toNumericMonth(month2);
		year2 = date2["year"];

		if(year2 > year1)
			{
				beforeorafter = "after";
			}
		else if(year2 == year1)
			{
				if(month2 > month1)
					{
						beforeorafter = "after";
					}
				else if(month2 == month1)
					{
						if(day2 >= day1)
							{
								beforeorafter = "after";
							}
					}
			}
		return beforeorafter;
	}

function parseDate(dateToParse)
	{
		dateToParse = dateToParse.split("-");
		var tempArray = new Object();
		tempArray["day"] = dateToParse[0];
		tempArray["month"] = dateToParse[1];
		tempArray["year"] = dateToParse[2];
		return tempArray;
	}

function toThreeLetterMonth(month)
	{
		monthArray = new Array();
		monthArray[0] = "Jan";
		monthArray[1] = "Feb";
		monthArray[2] = "Mar";
		monthArray[3] = "Apr";
		monthArray[4] = "May";
		monthArray[5] = "Jun";
		monthArray[6] = "Jul";
		monthArray[7] = "Aug";
		monthArray[8] = "Sep";
		monthArray[9] = "Oct";
		monthArray[10] = "Nov";
		monthArray[11] = "Dec";
		return monthArray[month - 1];
	}

function toNumericMonth(month)
	{
		var monthArray = new Object();
		monthArray["jan"] = 1;
		monthArray["feb"] = 2;
		monthArray["mar"] = 3;
		monthArray["apr"] = 4;
		monthArray["may"] = 5;
		monthArray["jun"] = 6;
		monthArray["jul"] = 7;
		monthArray["aug"] = 8;
		monthArray["sep"] = 9;
		monthArray["oct"] = 10;
		monthArray["nov"] = 11;
		monthArray["dec"] = 12;
		return monthArray[month.toLowerCase()];
	}
	
function checkMediaPrefs(mediaPrefVals, valToTest)
	{
		match = false;
		for(i = 0; i < mediaPrefVals.length; i++)
			{
				if(mediaPrefVals[i] == valToTest)
					{
						match = true;
					}
			}
		return match;
	}
	
function makePrefArray(mediaPreference)
	{
		temp = new Array();
		temp = mediaPreference.split(",");
		for(i = 0; i < temp.length; i++)
			{
				temp[i] = temp[i].toUpperCase();
			}
		return temp;
	}

function getMedia(prgCode, showDate, segNum, mediaPreference)
	{
		mediaPrefVals = makePrefArray(mediaPreference);

		//alert(checkMediaPrefs(mediaPrefVals, "RM"));

		//sa_onclick("http://www.npr.org/dmg/dmg.php?prgCode=" + prgCode + "&showDate=" + showDate + "&segNum=" + segNum + "&mediaPref=" + mediaPreference);
		
		if((segNum == "all") || (segNum == "ALL"))
			{
				segNum = "";
			}
		if(compareDates("21-Aug-2001", showDate) == "before")
			{
				//alert("date before 21-Aug-2001");
				newURL = "http://www.npr.org/dmg/dmg.php?prgCode=" + prgCode + "&showDate=" + showDate + "&segNum=" + segNum + "&media=old&mediaPref=RM";
				sa_onclick(newURL);
				location.href= newURL;
			}
		else if((checkMediaPrefs(mediaPrefVals, "RM")) && (!checkMediaPrefs(mediaPrefVals, "WM")))
			{
				newURL = "http://www.npr.org/dmg/dmg.php?prgCode=" + prgCode + "&showDate=" + showDate + "&segNum=" + segNum + "&mediaPref=RM";
				sa_onclick(newURL);
				location.href= newURL;
			}
		else if((checkMediaPrefs(mediaPrefVals, "WM")) && (!checkMediaPrefs(mediaPrefVals, "RM")))
			{
				newURL = "http://www.npr.org/dmg/dmg.php?prgCode=" + prgCode + "&showDate=" + showDate + "&segNum=" + segNum + "&mediaPref=WM";
				sa_onclick(newURL);
				location.href= newURL;
			}		
		else if(checkMediaPrefs(mediaPrefVals, "RM") && checkMediaPrefs(mediaPrefVals, "WM"))
			{
				if(compareDates("1-Feb-2003", showDate) == "after")
					{
						//alert("date after 1-Feb-2003");
						if(checkCookie("NPRMediaPref"))
							{
								mediaPreference = checkCookie("NPRMediaPref");
								//alert("mediaPreference = " + mediaPreference);
								newURL = "http://www.npr.org/dmg/dmg.php?prgCode=" + prgCode + "&showDate=" + showDate + "&segNum=" + segNum + "&mediaPref=" + mediaPreference;
								sa_onclick(newURL);
								if(adCompatible(mediaPreference))
								{
									if (mediaPreference == "WM")
									{
										goLightningCast(newURL);
									}
									else
									{
										goLightningCastReal(newURL);
									}
								}
								else
								{
									location.href=newURL;
								}
							}
						else
							{
								newURL = "http://www.npr.org/dmg/audioplayer.php?prgCode=" + prgCode + "&showDate=" + showDate + "&segNum=" + segNum;
								sa_onclick(newURL);
								window.open(newURL, "", "width=540,height=360,toolbar=none,location=none,menubar=none,status=none,resizable=yes");
							}
					}
				else
					{
						//alert("date between 21-Aug-2001 and 1-Feb-2003");
						if((checkCookie("NPROldMediaPref")) || (checkCookie("NPRMediaPref") == "RM"))
							{
								mediaPreference = checkCookie("NPROldMediaPref");
								if(checkCookie("NPRMediaPref") == "RM")
									{
										mediaPreference = "RM";
									}
								newURL = "http://www.npr.org/dmg/dmg.php?prgCode=" + prgCode + "&showDate=" + showDate + "&segNum=" + segNum + "&mediaPref=" + mediaPreference;
								sa_onclick(newURL);
								location.href = newURL;
							}
						else
							{
								newURL = "http://www.npr.org/dmg/oldaudio.php?prgCode=" + prgCode + "&showDate=" + showDate + "&segNum=" + segNum;
								sa_onclick(newURL);
								window.open(newURL, "", "width=540,height=360,toolbar=none,location=none,menubar=none,status=none,resizable=yes");
							}
					}
			}
	}

function getStaticMedia(URL, mediaPreference)
	{
		sauid = checkCookie("sauid");
		referrer = document.referrer;
		
		mediaPrefVals = makePrefArray(mediaPreference);
	
		if(URL.indexOf("http://") != -1)
			{
				location.href=URL;
				sa_onclick(URL);
				//location.href=URL + "?sauid=" + sauid + "&referrer=" + referrer;
			}
		else if((checkMediaPrefs(mediaPrefVals, "WM")) && (checkMediaPrefs(mediaPrefVals, "RM")))
			{ 
				mediaPreference = checkCookie("NPRMediaPref");
				if((mediaPreference == false) || (mediaPreference == "RM"))
					{
						newURL="http://www.npr.org/dmg/dmg.php?mediaURL=" + URL + "&mediaType=RM";
						sa_onclick(URL);
						location.href = newURL;
						//location.href="rtsp://audio.npr.org" + URL + ".rm";
						//location.href="rtsp://audio.npr.org" + URL + ".rm?sauid=" + sauid + "&referrer=" + referrer;
					}
				else if(mediaPreference == "WM")	
					{
						newURL="http://www.npr.org/dmg/dmg.php?mediaURL=" + URL + "&mediaType=WM";
						sa_onclick(URL);
						location.href=newURL;
						//location.href="mms://216.35.221.84" + URL + ".wma";
						//location.href="mms://216.35.221.84" + URL + ".wma?sauid=" + sauid + "&referrer=" + referrer;
					}
			}
		else if((checkMediaPrefs(mediaPrefVals, "RM")) && (!checkMediaPrefs(mediaPrefVals, "WM")))
			{
				newURL="http://www.npr.org/dmg/dmg.php?mediaURL=" + URL + "&mediaType=RM";
				sa_onclick(URL);
				location.href=newURL;
				//location.href="rtsp://audio.npr.org" + URL + ".rm";
				//location.href="rtsp://audio.npr.org" + URL + ".rm?sauid=" + sauid + "&referrer=" + referrer;
			}
		else if((checkMediaPrefs(mediaPrefVals, "WM")) && (!checkMediaPrefs(mediaPrefVals, "RM")))
			{
				newURL="http://www.npr.org/dmg/dmg.php?mediaURL=" + URL + "&mediaType=WM";
				sa_onclick(URL);	
				location.href=newURL;				
				//location.href="mms://216.35.221.84" + URL + ".wma";
				//location.href="mms://216.35.221.84" + URL + ".wma?sauid=" + sauid + "&referrer=" + referrer;
			}

	}
	
function getLatestShow(prgCode)
	{
		if(checkCookie("NPRMediaPref"))
			{
				mediaPreference = checkCookie("NPRMediaPref");
				// alert("mediaPreference = " + mediaPreference);
				newURL = "http://www.npr.org/dmg/dmg.php?getLatestShow=true&prgCode=" + prgCode + "&NPRMediaPref=" + mediaPreference;
				sa_onclick(newURL);
				// alert(newURL);
				location.href=newURL;
			}
		else
			{
				newURL = "http://www.npr.org/dmg/audioplayer.php?getLatestShow=true&prgCode=" + prgCode;
				sa_onclick(newURL);
				window.open(newURL, "", "width=540,height=360,toolbar=none,location=none,menubar=none,status=none,resizable=yes");
			}
	}

function getProgramStream()
	{
		if(checkCookie("NPRMediaPref"))
			{
				mediaPreference = checkCookie("NPRMediaPref");
				// alert("mediaPreference = " + mediaPreference);
				if(mediaPreference == "WM")
					{
						newURL = "http://www.npr.org/dmg/dmg.php?getProgramStream=true&NPRMediaPref=WM";
						// newURL = "http://www.npr.org/windowsmedia/programstream.asx";
					}
				else
					{
						newURL = "http://www.npr.org/dmg/dmg.php?getProgramStream=true&NPRMediaPref=RM";
					}
				sa_onclick(newURL);
				// alert(newURL);
				if(adCompatible(mediaPreference))
				{
					if (mediaPreference == "WM")
					{
						goLightningCast(newURL);
					}
					else
					{
						goLightningCastReal(newURL);
					}
				}
				else
				{
					location.href=newURL;
				}
			}
		else
			{
				newURL = "http://www.npr.org/dmg/audioplayer.php?getProgramStream=true";
				sa_onclick(newURL);
				window.open(newURL, "", "width=540,height=360,toolbar=none,location=none,menubar=none,status=none,resizable=yes");
			}
	}

function getNewsCast()
	{
		if(checkCookie("NPRMediaPref"))
			{
				mediaPreference = checkCookie("NPRMediaPref");
				// alert("mediaPreference = " + mediaPreference);
				if(mediaPreference == "WM")
					{
						newURL = "http://www.npr.org/dmg/dmg.php?getNewsCast=true&NPRMediaPref=WM";
					}
				else
					{
						newURL = "http://www.npr.org/dmg/dmg.php?getNewsCast=true&NPRMediaPref=RM";
						// newURL = "http://www.npr.org/realmedia/newscast.ram";
					}
				sa_onclick(newURL);
				// alert(newURL);
				if(adCompatible(mediaPreference))
				{
					if (mediaPreference == "WM")
					{
						goLightningCast(newURL);
					}
					else
					{
						goLightningCastReal(newURL);
					}
				}
				else
				{
					location.href=newURL;
				}
			}
		else
			{
				newURL = "http://www.npr.org/dmg/audioplayer.php?getNewsCast=true";
				sa_onclick(newURL);
				window.open(newURL, "", "width=540,height=360,toolbar=none,location=none,menubar=none,status=none,resizable=yes");
			}
	}

function goLightningCast(newURL)
	{
		
		newURL += "&getAd=1&ext=.asx";
		// alert("in goLightningCast");
		// alert(newURL);
		window.open("http://www.npr.org/lightningcast/index.html" + "?audioURL=" + newURL, "playerWin", "width=400,height=430,resizable=yes,toolbar=no,scrollbars=no");	
	}

function goLightningCastReal(newURL)
	{
		
		newURL += "&getAd=1&ext=.rm";
		// alert("in goLightningCastReal");
		// alert(newURL);
		window.open("http://www.npr.org/lightningcast/index_real.html" + "?audioURL=" + newURL, "playerWin", "width=400,height=430,resizable=yes,toolbar=no,scrollbars=no");	
	}
	
function getMusicButton(songId, musicAudioFileName, prgCode)
	{
		if(checkCookie("NPRMediaPref"))
			{
				mediaPreference = checkCookie("NPRMediaPref");
				// alert("mediaPreference = " + mediaPreference);
				newURL = "http://www.npr.org/dmg/dmg.php?getMusicButton=true&songId=" + songId + "&musicAudioFileName=" + musicAudioFileName + "&prgCode=" + prgCode + "&NPRMediaPref=" + mediaPreference;
				sa_onclick(newURL);
				// alert(newURL);
				location.href=newURL;
			}
		else
			{
				newURL = "http://www.npr.org/dmg/audioplayer.php?getMusicButton=true&songId=" + songId + "&musicAudioFileName=" + musicAudioFileName;
				sa_onclick(newURL);
				window.open(newURL, "", "width=540,height=360,toolbar=none,location=none,menubar=none,status=none,resizable=yes");
			}
	}
//Useful Stuff Starts Here

//Cookie Functions
	
function setCookie(cookieToSet, daysToExpiration, cookieValue)
	{
		var exp=new Date();
		exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * daysToExpiration));
		document.cookie=cookieToSet + "=" + cookieValue + "; path=/; domain=npr.org; expires=" + exp.toGMTString();
	}

function checkCookie(cookieToCheck)
	{
		var allcookies = document.cookie;
		var pos = allcookies.indexOf(cookieToCheck + "=");
		if(pos != -1) 
			{
				var start = pos + cookieToCheck.length + 1;
				var end = allcookies.indexOf(";", start);
				if(end == -1)
					{
						end = allcookies.length;
					}
				return allcookies.substring(start, end);
			}
		else 
			{
				return false;
			}
	}

function killCookie(cookieToKill)
	{
		setCookie(cookieToKill, -1, '')
	}
	
	

killCookie("nprsurvey");	
killCookie("nprsurvey1");
killCookie("nprsurvey2");
killCookie("nprsurvey3");
killCookie("nprsurvey4");


