var agent = navigator.userAgent.toLowerCase();
var is_gecko = agent.indexOf("gecko") != -1; var is_aol = agent.indexOf("aol") != -1;

/////////////////////////////////////////////////////////////////
// Utility functions used by all of our libraries/pages
/////////////////////////////////////////////////////////////////

function getPageName()
{
	var pageName = location.href.substring(location.href.lastIndexOf('/')+1);
	if(pageName.indexOf('?') >= 0)
		pageName = pageName.substring(0, pageName.indexOf('?'));
	return pageName;
}

//////////////////////////////////////////////////////////////////////////
// Creates a popup window.
// url: to be opened
// name: If a new popup is created with the same name, it will replace.
// popW: width in pixels. (must be set to 0 if overridden in features)
// popH: height in pixels. (must be set to 0 if overridden in features)
// features: Optional parameter to allow overriding of defaults.
//			 See Javascript reference for possible settings.
//////////////////////////////////////////////////////////////////////////
function doGenericPopup( url, name, popW, popH, features )
{
	var allFeatures;

	if ( features && features.length > 0 ) allFeatures = features;
	else allFeatures = "menubar=no,scrollbars=yes,resizable=yes,";
	if (allFeatures.charAt(allFeatures.length-1) != ',') allFeatures += ',';
	if ( !name || name.length == 0 ) name = "_blank";

	if ( popW > 0 && popH > 0 )
	{
		// -1 is a flag to indicate we should let the browser determine the position.
		// This value is not changed if the popup height is very close to the availHeight.
		var leftPos = -1;
		var topPos = -1;
		var calcPos = true;

		if ( screen.availWidth && screen.availHeight )
		{
			var maxWidth = screen.availWidth;
			var maxHeight = screen.availHeight - (is_aol ? 150 : 70);

			if (popH > maxHeight)
			{
				// If the height of the popup is larger than the screen height
				// change the height to be slightly less than the available height.
				popH = maxHeight;
				allFeatures = allFeatures.replace(/scrollbars=no/gi, "scrollbars=yes");
				if (allFeatures.indexOf("scrollbars") == -1) allFeatures += "scrollbars=yes,";
				allFeatures = allFeatures.replace(/resizable=no/gi, "resizable=yes");
				if (allFeatures.indexOf("resizable") == -1) allFeatures += "resizable=yes,";
				calcPos = false;
			}
			if (popW > maxWidth)
			{
				// If the width of the popup is larger than the screen width
				// change the width to be slightly less than the available width.
				popW = maxWidth;
				allFeatures = allFeatures.replace(/scrollbars=no/gi, "scrollbars=yes");
				if (allFeatures.indexOf("scrollbars") == -1) allFeatures += "scrollbars=yes,";
				allFeatures = allFeatures.replace(/resizable=no/gi, "resizable=yes");
				if (allFeatures.indexOf("resizable") == -1) allFeatures += "resizable=yes,";
				calcPos = false;
			}

			if (calcPos)
			{
				leftPos = (maxWidth - popW) / 2;
				if (leftPos < 0) leftPos = -1;

				if (!is_aol)
				{
					topPos = (maxHeight - popH) / 2;
					if (topPos < 0) topPos = -1;
				}
			}
		}
		else
		{
			allFeatures = allFeatures.replace(/scrollbars=no/gi, "scrollbars=yes");
			if (allFeatures.indexOf("scrollbars") == -1) allFeatures += "scrollbars=yes,";
			allFeatures = allFeatures.replace(/resizable=no/gi, "resizable=yes");
			if (allFeatures.indexOf("resizable") == -1) allFeatures += "resizable=yes,";
		}

		if (popH > 0 && popW > 0)
		{
			allFeatures += "height="+popH;
			allFeatures += ",width="+popW+',';
		}

		//if (leftPos > 0 && topPos > 0)
		//{
			allFeatures += "left="+leftPos+",screenX="+leftPos;
			allFeatures += ",top="+topPos+",screenY="+topPos;
		//}
	}

	var win = window.open(url, name, allFeatures);
	if (win && !win.closed) win.focus();
	return win;
}

//////////////////////////////////////////////////////////////////////////
// Pops a window and uses the redirect.jsp page to record the URL before
// loading it.
//////////////////////////////////////////////////////////////////////////
function popWithRedirection( url, name, popW, popH, features )
{
	var redirectURL = getRedirectURL(url);
	doGenericPopup(url, name, popW, popH, features);
}

function getRedirectURL(url)
{
	return "redirect.jsp?to="+escape(url);
}

///////////////////////////////////////////////////////////////////////////
// Submit a rating (feedback). Try to auto-submit. If it doesn't work,
// try submiting by reloading
///////////////////////////////////////////////////////////////////////////
function submitRating(src, programid, rating)
{
	// try auto-submit first
	if(!autoSubmitRating(src, programid, rating))
	{
		return reloadAndSubmitRating(src, programid, rating);
	}

	return true;
}

function confirmToPopup(message, url, width, height)
{
	if (confirm(message))
	{
		doGenericPopup(url, "_blank", width, height, "");
	}
}

function openRatingPopup(programID)
{
	doGenericPopup("rateit.jsp?programID=" + programID, "_blank", 328, 380, "menubar=no,scrollbars=no,resizable=yes,"); return false;
}

function getNewMapString(s, key, value)
{
	var keystring = key + "=" + value;

	if (s.length == 0)
		return keystring;

	// Build a regular expression to match the key (with our without ampersand in front)
	// and its current value
	var reg = new RegExp("(^|[&?])" + key + "=([^&]*)", "i");
	if (s.match(reg) == null)
	{
		return s + "&" + keystring;
	}
	else
	{
		return s.replace(reg, "$1" + keystring);
	}
}

function getVariable(urlstring, key)
{
	var reg = new RegExp("([&?]+)" + key + "=([^&]*)", "i");
	var m = urlstring.match(reg);
	if (m)
	{
		return m[2];
	}
	return "";
}

// When the page loads, store the current location.href into a variable that can be updated.
var nextReloadURL = location.href;
// If it doesn't have a ?, pop one in there.
if (nextReloadURL.indexOf("?") == -1)
{
	nextReloadURL += "?";
}
else
{
	// check for any undesired params and remove them.
	nextReloadURL = nextReloadURL.replace(/(forcedrefresh|(ct|partner)code)=[^&]*&?/g, '');
	nextReloadURL = nextReloadURL.replace(/[?&]$/g, '');
}

function forceRefreshMe()
{
	passNewURLValue('forcedrefresh', 1);
}
function updateNextReloadURL(key, value)
{
	nextReloadURL = getNewMapString(nextReloadURL, key, value);
}
function updateNextReloadPassthru(passthru)
{
	if (nextReloadURL.match(/passthru/))
		nextReloadURL = nextReloadURL.replace(/&passthru[^&]+/g, passthru);
	else
		nextReloadURL += passthru;
}

function passNewURLValueWithClickThru(key, value, ctcode, partnercode)
{
	if (ctcode != undefined)
	{
		updateNextReloadURL("ctcode", ctcode);
	}
	else
	{
		updateNextReloadURL("ctcode", key);
	}

	if (partnercode != undefined)
	{
		updateNextReloadURL("partnercode", partnercode);
	}

	passNewURLValue(key, value);
}

function passNewURLValues(keys, values)
{
	for (var i=0; i < keys.length; i++)
	{
		updateNextReloadURL(keys[i], values[i]);
	}
}
function passNewStateValue(key, value)
{
	var value = escape(getNewMapString(unescape(getVariable(nextReloadURL, "cs_viewstate")), key, value));
	passNewURLValue("cs_viewstate", value);
}

// Reloads the current window, passing in one single new value on the url
function passNewURLValue(key, value)
{
	location.href = getNewMapString(nextReloadURL, key, value);
}

function passNewURLValues(keys, values)
{
	for (var i=0; i < keys.length; i++)
	{
		nextReloadURL = getNewMapString(nextReloadURL, keys[i], values[i]);
	}
	location.href = nextReloadURL;
}

function go(url)
{
	window.location.href = url;
}

function cspop(url, width, height, features)
{
	if (width && height)
	{
		if (!features)
		{
			features = "scrollbars=no,resizable=yes";
		}
		doGenericPopup(url, "_blank", width, height, features);
	}
	else
	{
		var win = window.open(url);
		win.focus();
	}
}

function clientlink(url)
{
	if (!is_aol)
	{
		alert("We're Sorry .....\nThe content you requested is only available in the AOL Client; login to the AOL client to view this content.");
		return;
	}

	goparent(url);
}

function myaolpop(url, width, height)
{
	if(!height) height = 465;
	if(!width) width = 553;
	cspop(url, width, height);
}

function mdvpop(url)
{
	cspop(url, 675, 700);
}

function rateitpop(url)
{
	cspop(url, 328, 380, "menubar=no,scrollbars=no,resizable=yes,");
}

function aoltvpop(url)
{
	cspop(url, 640, 450, "menubar=no,scrollbars=no,resizable=yes,");
}

function iframego(url, iframename)
{
	window.document[iframename].location.href = url;
}

function goparent(url)
{
	if (parent)
	{
		parent.location.href = url;
	}
	else
	{
		location.href = url;
	}
}
