



if (typeof(prependDomain) == "undefined") {
	prependDomain = "";
}

/**
  * Set, Get, and Delete Cookie utils
  *
  */
function GetCookieUtil (name) {
	var result = null;
	var myCookie = " " + document.cookie + ";";
	var searchName = " " + name + "=";
	var startOfCookie = myCookie.indexOf(searchName);
	var endOfCookie;
	if (startOfCookie != -1) {
		startOfCookie += searchName.length;
		endOfCookie = myCookie.indexOf(";", startOfCookie);
		result = unescape(myCookie.substring(startOfCookie, endOfCookie));
	}
	if (result == "") result = null;
	return result;
}

function SetCookieUtil (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "; path=/") +
	((domain) ? "; domain=" + domain : "; domain="+document.location.hostname) +
	((secure) ? "; secure" : "");
}

function DeleteCookieUtil (name,path,domain) {
	if (GetCookieUtil(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
var today = new Date();
var oneDay = new Date(today.getTime() + 24 * 60 *60 * 1000);
var oneWeek = new Date(today.getTime() + 24 * 60 *60 * 1000 * 7);
var oneYear = new Date(today.getTime() + 24 * 60 *60 * 1000 * 365);









<!-- March 9, 2006 - when updating please kill line 51 and uncomment line 52 -->








/***** droplet src="/sitewide/droplets/downloadPop.jhtml" *****/

/***** MEDIAPLAYER LAUNCH SCRIPTS *****/

		






if (typeof additionalVals == "undefined") {
	additionalVals="";
}

function launchVideo(clipy){

prependDomain = "";	

	players = new Array();
	player = "defaultPlayer";
	players['defaultPlayer'] = new Array("mtvmediaplayer","720","540");
	
	clipyNew = clipy
	clipy = clipyNew

	if(clipyNew.indexOf('channelID=')<0 && window.channelID)
	   clipy = clipy + "&" + "channelID=" + channelID
	if(clipyNew.indexOf('orgID=')<0 && window.orgID)
	   clipy = clipy + "&" + "orgID=" + orgID		
	if(clipyNew.indexOf('gateway=')<0 && window.gateway)
	  clipy = clipy + "&" + "gateway=" + gateway 
	if(clipyNew.indexOf('portal=')<0 && window.portal)
	  clipy = clipy + "&" + "portal=" + portal; 
	if(clipyNew.indexOf('paid=')<0 && window.paid)
	  clipy = clipy + "&" + "paid=" + paid
	if(clipyNew.indexOf('seriesID=')<0 && window.seriesID)
	  clipy = clipy + "&" + "seriesID=" + seriesID
	if(clipyNew.indexOf('episodeID=')<0 && window.episodeID)
	  clipy = clipy + "&" + "episodeID=" + episodeID
	if(clipyNew.indexOf('albumid=')<0 && window.albumid)
	  clipy = clipy + "&" + "albumid=" + albumid

	ap = "&adPth=" + escape("");
	ap = ap.substring(0,ap.length-17);
	ap = ap + "&adPN=" + escape("");

	refURL = document.location.pathname;
	pieces = refURL.split("/");
	if (pieces.length == 2) pieces[1] = "home";
	var ss = '';
	for (var i = 1; i < pieces.length; i++) {
		sIndex = i - 1;
		ss = ss + "&section_" + sIndex + "=" + pieces[i];
	}
	ss = ss + "&refURL=" + refURL;
	
	clipy = clipy.replace(/'/g,'\\\'')
	

		attSw = 'no';
	
	


	
	if(document.referrer.indexOf("yahoo")>=0)
			additionalVals += '&partner=yahoo';

	sUrl =  prependDomain + "/sitewide/apps/mediaplayer/index.jhtml?" + clipy + ss + ap + additionalVals;
	
	attrs = 'width=' + players[player][1] + ',';
	attrs += 'height=' + players[player][2] + ',';
	attrs += 'left=25,top=25,';
	attrs += 
		'scrollbars=' + attSw + 
		',resizable=' + attSw + 
		',toolbar=' + attSw + 
		',location=' + attSw + 
		',directories=' + attSw + 
		',status=' + attSw + 
		',menubar=' + attSw;
		
var pWin = false;
pWin = window.open(sUrl, players[player][0], attrs);	
// popCheck(pWin);
// pWin.focus();

}

{
  function launchTrailer(stuff){
    launchVideo(stuff)
  }
}


	

	

/***** URGE LAUNCH SCRIPTS *****/





		
		

var urge_referrer = "[no referrer provided]";

/*************************************************************************
*	URGE Launch: Launch the URGE player
*************************************************************************/

function launchUrge (paramString) {

	var hash = {};
	var pairs = paramString.split("&");
	for (var i=0;i<pairs.length;i++) {
		if(!pairs[i]) continue;
		var nameValue = pairs[i].split("=");
		var name = nameValue[0];
		var value = pairs[i].substring(name.length+1);
		hash[name] = value;
	}

	var url = "http://www.urge.com/launch/?";

	if(typeof hash["referrer"] == "undefined")
		hash["referrer"] = urge_referrer;

	var query = new Array();
	for(var n in hash) {
		if(hash[n])
			query.push(n+"="+hash[n]);
	}

	url += query.join("&");

	window.open(url, "launch_urge", "width=300,height=200,toobar=no,status=no,scrollbars=no,resizable=no,location=no");
}

	
	


/***** OVERDRIVE LAUNCH SCRIPTS *****/

function showAlertsPromo() {}; // overiding shared function that displays OD alert promo
var parentWindowVar		= "parent=";
var detectSuccessUrl 	= "/overdrive/player.jhtml";
var detectFailUrl 		= "/overdrive/index.jhtml";
var detectFailUrlWin	= "/overdrive/player_detect.jhtml";
var winWidth 			= "996";
var winHeight 			= "555";


/**
 * Constructor OSDetector
 */
function OSDetector()
{
  //properties
  this.os                       = navigator.userAgent.toLowerCase();
  this.appVer                   = navigator.appVersion.toLowerCase();
  this.minorVersion             = parseFloat(this.appVer);
  this.majorVersion             = parseInt(this.minorVersion)

  this._isWindows               = false;
  this._isWindows16             = false;
  this._isWindows31             = false;  
  this._isWindows95             = false;
  this._isWindows98             = false;
  this._isWindowsME             = false;
  this._isWindowsNT             = false;
  this._isWindows2K             = false;
  this._isWindowsXP             = false;
  this._isWindows32             = false;         
  this._isWindowsDotNet         = false;
	this._isWindowsVista          = false;
  
  this._isMac                   = false;
  this._isMac68K                = false;
  this._isMacPPC                = false;
  this._isMacOS8                = false;
  this._isMacOS9                = false;
  this._isMacOSX                = false;
             
  //methods
  this.isWindows                = isWindows;
  this.isWindows16              = isWindows16;
  this.isWindows31              = isWindows31;  
  this.isWindows95              = isWindows95;
  this.isWindows98              = isWindows98;
  this.isWindowsME              = isWindowsME;
  this.isWindowsNT              = isWindowsNT;
  this.isWindows2K              = isWindows2K;
  this.isWindowsXP              = isWindowsXP;
  this.isWindows32              = isWindows32;
  this.isWindowsDotNet          = isWindowsDotNet;
  this.isWindowsVista           = isWindowsVista;
  
  this.isMac                    = isMac;
  this.isMac68K                 = isMac68K;
  this.isMacPPC                 = isMacPPC;
  this.isMacOS8                 = isMacOS8;  
  this.isMacOS9                 = isMacOS9;  
  this.isMacOSX                 = isMacOSX;  
  
  this.getOs                    = getOs;  
  this.detect                   = detect;
  this.isPlayerPossible         = isPlayerPossible;
}//end constructor

/**
 * detect
 */
function detect()
{
  //windows os
  this._isWindows               = ((this.getOs().indexOf("win") != -1) || (this.getOs().indexOf("16bit") != -1));
  this._isWindows95             = ((this.getOs().indexOf("win95") != -1) || (this.getOs().indexOf("windows 95") != -1));
  this._isWindows16             = ((this.getOs().indexOf("win16") != -1) || (this.getOs().indexOf("16bit") != -1) || (this.getOs().indexOf("windows 3.1") != -1) || (this.getOs().indexOf("windows 16-bit") != -1) );
  this._isWindows31             = ((this.getOs().indexOf("windows 3.1") != -1) || (this.getOs().indexOf("win16") != -1) || (this.getOs().indexOf("windows 16-bit") != -1));	
  this._isWindowsME             = ((this.getOs().indexOf("win 9x 4.90") != -1));
  this._isWindows98             = ((this.getOs().indexOf("win98") != -1) || (this.getOs().indexOf("windows 98") != -1));
  this._isWindowsNT             = ((this.getOs().indexOf("winnt") != -1));  
  this._isWindows2K             = ((this.getOs().indexOf("windows nt 5.0") != -1) || (this.getOs().indexOf("windows 2000") != -1));
  this._isWindowsXP             = ((this.getOs().indexOf("windows nt 5.1") != -1) || (this.getOs().indexOf("windows xp") != -1));
  this._isWindowsDotNet         = ((this.getOs().indexOf("windows nt 5.2") != -1));
  this._isWindowsVista          = ((this.getOs().indexOf("vista") != -1) || (this.getOs().indexOf("windows nt 6") != -1));
  this._isWindows32             = (this.isWindows95() || this.isWindowsNT() || this.isWindows98() || ((navigator.appVersion >= 4) && (navigator.platform == "Win32")) || (this.os.indexOf("win32") != -1) || (this.os.indexOf("32bit") != -1));

  
  //mac os  
  this._isMac                   = (this.getOs().indexOf("mac") != -1);
  this._isMac68K                = (this.isMac() && ((this.getOs().indexOf("68k") != -1) || (this.getOs().indexOf("68000") != -1)));
  this._isMacPPC                = (this.isMac() && ((this.getOs().indexOf("ppc") != -1) || (this.getOs().indexOf("powerpc") != -1)));
  this._isMacOS8                = (this.isMac() && ((this.getOs().indexOf("os 8") != -1) || (this.getOs().indexOf("os8") != -1)));
  this._isMacOS9                = (this.isMac() && ((this.getOs().indexOf("os 9") != -1) || (this.getOs().indexOf("os9") != -1)));
  this._isMacOSX                = (this.isMac() && ((this.getOs().indexOf("powerpc") != -1) || (this.getOs().indexOf("osx") != -1) || (this.getOs().indexOf("os x") != -1)));    
  
  if (this.isMac()) this._isWindows =! this._isMac;

}//end method

/**
 * getOs - returns the os property
 */
function getOs()
{
  return this.os;
}//end method

/**
 * isWindows - is windows, any version
 */
function isWindows()
{
  return this._isWindows;
}//end method

/**
 * isWindows16 - is windows 16 bit
 */
function isWindows16()
{
  return this._isWindows16;
}//end method

/**
 * isWindows31 - is windows 3.1
 */
function isWindows31()
{
  return this._isWindows31;
}//end method

/**
 * isWindows95 - is windows 95
 */
function isWindows95()
{
  return this._isWindows95;
}//end method

/**
 * isWindows98 - is windows 98
 */
function isWindows98()
{ 
  return this._isWindows98;
}//end method

/**
 * isWindowsME - is windows me
 */
function isWindowsME()
{
  return this._isWindowsME;
}//end method

/**
 * isWindowsNT - is windows NT
 */
function isWindowsNT()
{
  return this._isWindowsNT;
}//end method

/**
 * isWindows2K - is windows 2000
 */
function isWindows2K()
{
  return this._isWindows2K;
}//end method

/**
 * isWindowsXP - is windows xp
 */
function isWindowsXP()
{
  return this._isWindowsXP;
}//end method

/**
 * isWindows32 - is windows 32 bit
 */
function isWindows32()
{
  return this._isWindows32;
}//end method

/**
 * isWindows64 - is windows 64 bit
 */
function isWindows64()
{
  return this._isWindows64;
}//end method

/**
 * isWindowsDotNet - is windows dot net
 */
function isWindowsDotNet()
{
  return this._isWindowsDotNet;
}//end method

/**
 * isWindowsVista - is windows vista
 */
function isWindowsVista()
{
  return this._isWindowsVista;
}//end method


/**
 * isMac - is mac os any version
 */
function isMac()
{
  return this._isMac;
}//end method

/**
 * isMac68K is mac 6800 series
 */
function isMac68K()
{
  return this._isMac68K;
}//end method

/**
 * isMacPPC is mac power pc
 */
function isMacPPC()
{  
  return this._isMacPPC;
}//end method

/**
 * isMacOS8 is mac os 8
 */ 
function isMacOS8()
{
  return this._isMacOS8;
}//end method

/**
 * isMacOS9 is mac os 9
 */
function isMacOS9()
{
  return this._isMacOS9;
}//end method

/**
 * isMacOSX is mac os x
 */
function isMacOSX()
{
  return this._isMacOSX;
}//end method

/**
 * isPlayerPossible
 */
function isPlayerPossible()
{
  //all these platforms cant get wmp 9
  if (this.isWindows95() || 
      this.isWindowsNT() || 
      this.isWindows98() || 
      this.isWindowsME() || 
      this.isMacOS9() || 
      this.isMacOS9() || 
      this.isMacOS8() || 
      this.isMacPPC() || 
      this.isMac68K() )
  {    
    return false;
  }
  return true;
}//end method

os = new OSDetector();
os.detect();


//for wmp detection
/*
if (os.isWindows())
{
  if (isPlayerPossible())
  {
    //player possible, do player detection  
  }
  else
  {
    //no player possible, download vmc
  }
}
*/

		

/*************************************************************************/
//  Requires the following script libraries.
//
//	/global/apps/mediaplayer/scripts/os_detect.js
//	/shared/scripts/flash_detect.js
//	/sitewide/droplets/scripts/detect_overdrive.js
//
/*************************************************************************/
var od_win;
var detectState="";
var fn="";
var ok="";

function launchOverdriveDetect(){

	var resultStatus = getOverdriveState().detectState;

	if (resultStatus == "ok"){
		return true;
	} else{
		window.location.href = detectFailUrlWin + "?error=" + resultStatus;
	}
}

function launchOverdrive(){
	if (getOverdriveState().detectState == "XXXok") {
		var broadband_parent = parentWindowVar;
		var url = detectSuccessUrl;
		var width;
		var height;
		var odscrollbars;
		var odresizable;
		var purlArray = null;

		if (getOverdriveState().dim == "lowres"){
			width = 800;
			height = 600;
			odscrollbars = "yes";
			odresizable = "yes";
		} else {
			width = winWidth;
			height = winHeight;
			odscrollbars = "no";
			odresizable = "no";
		}

		// CONSTRUCT DEEP LINK ARGS

		var qs = "";
		if(arguments.length > 0){
			qs = "?"+arguments[0];
			for(i=1; i<arguments.length; i++){

				if (arguments[i].indexOf(broadband_parent) == -1){
					qs = qs + "&" + arguments[i];
				} else {
					qs = qs + "&" + arguments[i];
					purlArray = arguments[i].split(/=/);
					}
				}
			}

		var docloc = window.location.pathname;
		// for fbml urls
			fullLoc = window.location.toString();
			if ((typeof browserManager != "undefined") && (fullLoc.indexOf('#') > 0)) {
				docloc = fullLoc.substr(fullLoc.indexOf('#') +1, fullLoc.length).split("?")[0];
			}


	      if(qs.length > 0){
	         qs += "&launchedFrom=" + docloc;
	      } else{
	         qs += "?launchedFrom=" + docloc;
	      }

		var path = 	url+qs;

		od_win = window.open(path, "player", "width=" + width + ", height=" + height + ", left=" + (Math.round(window.screen.width/2) - Math.round(width/2)) + ", top=" + (Math.round(window.screen.height/2) - Math.round(height/2)) + ",scrollbars=" + odscrollbars + ", resizable=" + odresizable + ", menubar=no, location=no, toolbar=no, status=no, directories=no" );

		if (purlArray != null) window.location.href = purlArray[1];

		broadbandWindowPopped(od_win, path);

	} else{

	//	window.location.href = detectFailUrl;
			var qs = "";
		if(arguments.length > 0){
			qs = arguments[0];
			for(i=1; i<arguments.length; i++){

				if (arguments[i].indexOf(broadband_parent) == -1){
					qs = qs + "&" + arguments[i];
				} else {
					qs = qs + "&" + arguments[i];
					purlArray = arguments[i].split(/=/);
					}
				}
			}
		launchShell(qs);
	}

}

function launchOverdriveWithReturnValue(){
	if (flashVersion >= 8 && GetCookieUtil("sitePreferenceSet") == "false") {
		var args = "";
		if(arguments.length > 0){
			args = arguments[0];
			for(i=1; i<arguments.length; i++){
				args = args + "," + arguments[i];
			}
		}

		launchOverdrive(args);
		return false;
	}
	else{ 
		return true; 
	}
}


function broadbandWindowPopped(win, path) {}



function launchOverdriveHome(){
	var u = window.location + "";
	var q = u.indexOf('?') + 1;
	var p = u.substring(q,u.length);
	var param = p.split("&");
	var args = "";

	if (q > 0)
	{
		args = param[0];

		if (param.length > 1)
		{
			for (i=1; i<param.length;i++)
			{
				args = args + "&" + param[i];
			}
		}
		launchOverdrive(args);
	}
	else
	{
		launchOverdrive();
	}
}



function getOverdriveState(){
	var dim;

		if( ( !os.isMac()) && (!os.isWindows())){
			detectState =  "os_other";
			fn	= "display_error_os_other";
		}
		else if(os.isMac()){
		   detectState = "macintosh";
		   fn = "display_error_os_mac";
		}
		else if ((!os.isWindowsXP()) && (!os.isWindows2K()) && (!os.isWindowsVista())){
		   detectState = "oldwindows";
		   fn = "display_error_os_old";
		}
		else if (isNetscape) {
			detectState = "netscape";
	  		fn = "display_error_browser";
		}
		else if (isFirefox) {
			detectState = firefoxX().detectState;
	  		fn = firefoxX().fn;
		}
		else if(!hasIE55plus()) {
		   detectState = "oldie";
		   fn = "display_error_browser_oldie";
		}
		else if (flashVersion < 7 ) {
		   detectState = "oldflash";
		   fn = "display_error_flash";
		}
		else if ((hasWMP9() == false ) ) {
		   detectState = "oldwmp";
		   fn = "display_error_wmp";
		}
		else if ((window.screen.width < 1024) || (window.screen.height < 768) ) {
			//detectState = "lowres";
			//fn = "display_error_resolution";
			detectState = "ok";
			dim = "lowres";
			fn = "display_enter";
		} else {
			detectState = "ok";
			fn = "display_enter";
		}

		// DETECT DISPLAY
		var pn = fn + ".js";
		// PAGE REPORTING
		var rpn = fn + ".jhtml";
		// launchOverdrive uses "detectState"

	return {detectState : detectState, pn : pn, rpn : rpn, dim : dim};
}




function firefoxX(){

	if(firefoxXInstall().version == "1.5" && isActiveXInstalled()) {
		detectState = "ok";
    	fn = "display_enter";
	} else if (navigator.appName == "Netscape") {
		detectState 	= firefoxXInstall().detectState;
	  	fn 				= firefoxXInstall().fn;
	}

	return {detectState:detectState, fn:fn};
}

function isActiveXInstalled() {
	// Checks if ActiveX is installed
	var geckoax=new Object();
	geckoax.installed=false;
	var isInstalled=false;

	if (navigator.plugins && navigator.plugins.length)
	{
		for (x=0; x<navigator.plugins.length; x++)
		{
			if (navigator.plugins[x].name.indexOf('ActiveX') != -1 && window.GeckoActiveXObject) {
				geckoax.installed=true;
			}
		}
	}

	if (window.ActiveXObject || geckoax.installed){
		isInstalled=true;
	} else {
		isInstalled=false;
	}

	return isInstalled;
}

function firefoxXInstall() {

	var versionindex 	= 	navigator.userAgent.indexOf("Firefox")+8;
	var version 		= 	navigator.userAgent.substring(versionindex,(versionindex+3));
	var fullversion 	=   navigator.userAgent.substring(versionindex,(versionindex+7))

	fn="display_error_browser";
	detectState="firefox";

	if (version == "1.5") {
 		fn = "display_error_firefox_1.5";
	} else {
		fn = "display_error_firefox";
	}
	return {detectState: detectState, fn : fn, version:version, fullversion:fullversion};
}

function hasWMP9(){

	var wm_player;

	try {
		if (window.ActiveXObject) {
			wm_player = new ActiveXObject("WMPlayer.OCX.7");
		} else if (window.GeckoActiveXObject) {
			wm_player = new GeckoActiveXObject("WMPlayer.OCX.7");
		}
      if (wm_player && parseInt(wm_player.versionInfo) >= 9) {
         return true;
      } else {
         return false;
      }
	} catch(e) { return false; }

}

function hasIE55plus(){

   var version=0;
   if (navigator.appVersion.indexOf("MSIE")!=-1){
      temp=navigator.appVersion.split("MSIE");
      version=parseFloat(temp[1]);
   }
   if (version >= 5.5) {
      return true;
   } else {
      return false;
   }
}

	
function broadbandWindowPopped(win, path) {
	try {
		win.focus();
		showAlertsPromo();
	} catch (e) {
		window.location.replace(path);
	}
}

function launchContentOn(){
	document.getElementById('flaunchContent').style.visibility = "visible"; 
}
	



function launchShell(){

		var hostname = "";
		var hpA = window.location.host.split(".");
		var hp;
		hp = (hpA[0].substr(0,1) == "o" && hpA[0].indexOf("-") == -1) ? hpA[2]: hpA[1].replace("x", "");
		if ( (hp != "mtv") && (hp != "mtv-d") && (hp != "mtv-q"))
			hostname = "http://www.mtv.com";

		var broadband_parent = parentWindowVar;
		var url = hostname + "/overdrive/";
		var width = 1034;
		var height = 768;
		var odscrollbars;
		var odresizable;
		var purlArray = null;


		// CONSTRUCT DEEP LINK ARGS

		var qs = "";
		if(arguments.length > 0){
			qs = "?" +arguments[0];
			for(i=1; i<arguments.length; i++){

				if (arguments[i].indexOf(broadband_parent) == -1){
					qs = qs + "&" + arguments[i];
				} else {
					qs = qs + "&" + arguments[i];
					purlArray = arguments[i].split(/=/);
					}
				}
			}

		var docloc = window.location.pathname;
		// for fbml urls
			fullLoc = window.location.toString();
			if ((typeof browserManager != "undefined") && (fullLoc.indexOf('#') > 0)) {
				docloc = fullLoc.substr(fullLoc.indexOf('#') +1, fullLoc.length).split("?")[0];
			}


		var path = 	url+qs;
			shell_win = window.open(path, "shell", "width=" + width + ", height=" + height + ", left=" + (Math.round(window.screen.width/2) - Math.round(width/2)) + ", top=" + (Math.round(window.screen.height/2) - Math.round(height/2)) + ",scrollbars=yes"  + ", resizable=yes"  + ", menubar=yes, location=yes, toolbar=yes, status=yes, directories=yes" );
			shell_win.focus();
			if (purlArray != null) window.location.href = purlArray[1];
			broadbandWindowPopped(od_win, path);
}

function broadbandWindowPopped(win, path) {}







/***** RADIO LAUNCH SCRIPTS *****/
function depricatedStationAlert() {
	alert('This Station Is No Longer Available');
	return true;
}
function launchRSN(varStationID) {
	var depricatedStation = false;
	switch (parseInt(varStationID)) {
		case 25 : varStationID = 16417;
			break;
		case -238 : varStationID = 16581;
			break;
		case 53 : varStationID = 16416;
			break;
		case -226 : varStationID = 48593;
			break;
		case 142 : varStationID = 16387;
			break;
		case 152 : varStationID = 16414;
			break;
		case 153 : varStationID = 16418;
			break;
		case -195 : varStationID = 49946;
			break;
		case -176 : varStationID = 16527;
			break;
		case -245 : varStationID = 16593;
			break;
		case -202 : varStationID = 16552;
			break;
		case -249 : varStationID = 102130;
			break;
		case 18 : varStationID = 16411;
			break;
		case 171 : depricatedStation = depricatedStationAlert();
			break;
		case -177 : depricatedStation = depricatedStationAlert();
			break;
		case -151 : depricatedStation = depricatedStationAlert();
			break;
		case -230 : depricatedStation = depricatedStationAlert();
			break;
		case -242 : depricatedStation = depricatedStationAlert();
			break;
		case -229 : depricatedStation = depricatedStationAlert();
			break;
		case -224 : depricatedStation = depricatedStationAlert();
			break;
		case -207 : depricatedStation = depricatedStationAlert();
			break;
		case 155 : depricatedStation = depricatedStationAlert();
			break;

	}

  if(!depricatedStation) {
	tuner = window.open('/sitewide/apps/radio/tuner/?stationId='+varStationID,'TUNER','width=702,height=262,toolbar=no,resizable=no,scrollbars=no');
  }
}

/***** FLIPBOOK LAUNCH SCRIPTS *****/
/*
 * @description	Opens the sitewide flipbook player.
 * @param fbKey Flipbook ID or URL.
 * @param obj, Object, used to customize the flipbook skin and values.
 */
function popFlip(fbKey, obj){
	if (flashVersion >= 8 &&
		(siteVersion=="FBML" || (GetCookieUtil("sitePreferenceSet") == "false" && siteVersion == "HTML"))) {
		var fbArgs = "";
		for (var p in obj){
			fbArgs += "&" + p + "=" + obj[p];
		}

		var lfArg;

		// for fbml urls
		fullLoc = window.location.toString();
		if ((typeof browserManager != "undefined") && (fullLoc.indexOf('#') > 0)) {
			lfArg = "&launchedFrom=" + fullLoc.substr(fullLoc.indexOf('#') +1, fullLoc.length).split("?")[0];
		} else {
			lfArg = "&launchedFrom=" + window.location.pathname;
		}

		var pWin = false;
		if (fbKey.indexOf("fid=")>=0) {
			// new flip pop, old w=556, h=486
			pWin = window.open('/sitewide/flipbook/index.jhtml?'+fbKey+fbArgs+lfArg,'flipbook','width=747,height=505,scrollbars=no');
		}
		else {
			var flipLoc  = fbKey.split("?");
			flipLocPath  = flipLoc[0].split("index.jhtml");
			var flipLocFull  =  flipLocPath[0];
			if (flipLoc[1] != "" && flipLoc[1] != null) flipLocFull += "&" + flipLoc[1];
			pWin = window.open('/sitewide/flipbook/index.jhtml?pf='+flipLocFull+fbArgs+lfArg+'','flipbook','width=747,height=505,scrollbars=no');
		}
		// popCheck(pWin);
		return false;
	}
	else{ return true; }

}

function popFlipWithReturnValue(){
	if (flashVersion >= 8 &&
		(siteVersion=="FBML" || (GetCookieUtil("sitePreferenceSet") == "false" && siteVersion == "HTML"))) {
		var args = "";
		if(arguments.length > 0){
			args = arguments[0];
			for(i=1; i<arguments.length; i++){
				args = args + "," + arguments[i];
			}
		}
		popFlip(args);
		return false;
	}
	else{ return true; }
}

function popFlipMedia(fid, mid){
	pWin = window.open('/sitewide/flipbook/index.jhtml?fid=' + fid + '&mid=' + mid,'flipbook' + fid,'width=747,height=505,scrollbars=no');
}

function popFlipById(fid){
	popFlip('fid=' + fid);
}


function submitSearch(formRef){
    var trimed = formRef.searchterm.value.replace(/^\s*|\s(?=\s)|\s*$/g, "").toLowerCase();
    formRef.searchterm.value = trimed;

    if (trimed.length == 0) return;
    else {
		if(formRef.searchtype.selectedIndex===1){
			var msUrl ='http://search.live.com/results.aspx?q=' + trimed + '&amp;mkt=en-us&amp;FORM=VCM004';
			var sWin = window.open(msUrl);
            return false;
		} else formRef.submit();
    }
}

function catchEnter(formRef) {
  if (window.event && window.event.keyCode == 13)
    submitSearch(formRef);
  else
    return true;
}

/***** SEND TO FRIEND LAUNCH SCRIPTS *****/
function sendtofriend(){
	var title = document.getElementsByTagName('h1')[0];

	if (title!=undefined)
	{
        title = stripHtml(title.innerHTML.replace('\u2014', '--'));
		if (title.charCodeAt(title.length-1) < 46) {title = title.substring(0, title.length-1);}
	}
	else title = document.title.replace('\u2014', '--');

	var url = "/sitewide/forms/sendtofriend/form.jhtml?";
	var image = "";

	var metatags = document.getElementsByTagName("meta");
	for (i = 0; i < metatags.length; i++){
		if (metatags[i].getAttribute("name") == "thumbnail"){
			image = metatags[i].getAttribute("content");
		}
	}

	// check if function was called with arguments (ex: audvidId=123)
	// supported types: audvidId (video), contentId (overdrive show)
	if (arguments.length > 0){ url = url + arguments[0]; }
	else{ url = url + "title=" + escape(title) + "&image=" + escape(image) + "&url=" + escape(document.location.toString()); }
	window.open(url, 'sendtofriend', 'width=578,height=485,location=no');
}

function share(){
    var title = stripHtml(document.getElementsByTagName('h1')[0].innerHTML.replace('\u2014', '--'));

    if (title!=undefined)
	{
        title = stripHtml(title.innerHTML.replace('\u2014', '--'));
		if (title.charCodeAt(title.length-1) < 46) {title = title.substring(0, title.length-1);}
	}
	else title = document.title.replace('\u2014', '--');

	var image = "";

	var metatags = document.getElementsByTagName("meta");
	for (i = 0; i < metatags.length; i++){
		if (metatags[i].getAttribute("name") == "thumbnail"){
			image = metatags[i].getAttribute("content");
		}
	}

	var qs = "title=" + escape(title) + "&image=" + escape(image) + "&url=" + escape(document.location.toString());

	var share = new AjaxRequest('/sitewide/forms/share/response.jhtml?' + qs);
	share.sendRequest();
}

function submitShareForm() {
	if (!validateShareForm()) return false;

    var title = document.getElementsByTagName('h1')[0];

	if (title!=undefined)
	{
        title = stripHtml(title.innerHTML.replace('\u2014', '--'));
		if (title.charCodeAt(title.length-1) < 46) {title = title.substring(0, title.length-1);}
	}
	else title = document.title.replace('\u2014', '--');

	var image = "";

	var metatags = document.getElementsByTagName("meta");
	for (i = 0; i < metatags.length; i++){
		if (metatags[i].getAttribute("name") == "thumbnail"){
			image = metatags[i].getAttribute("content");
		}
	}

	document.getElementById("shareUrl").value = document.location.toString();
	document.getElementById("shareTitle").value = escape(title);
	document.getElementById("shareImage").value = image;

	var submit = new AjaxRequest('/sitewide/forms/share/submit.jhtml');
	submit.addNamedFormElements("url", "title", "image", "friendEmail", "senderEmail", "friendNote");
	submit.sendRequest();
	setTimeout("reloadShareForm()", 5000);
}

function reloadShareForm(){
	var reload = new AjaxRequest('/sitewide/forms/share/formReload.jhtml');
	reload.sendRequest();
}

function validateShareForm(){
	var friendEmail = document.getElementById("friendEmail");
	var senderEmail = document.getElementById("senderEmail");

	var friendEmailLabel = document.getElementById("friendEmailLabel");
	var senderEmailLabel = document.getElementById("senderEmailLabel");

	var validFriend = true;

	if(friendEmail.value.indexOf(",") != -1) {
		var emailArray = friendEmail.value.split(",");
		var i;
		var good;

		for (i=0; i < emailArray.length; i++) {
			var email = stripSpace(emailArray[i]);
			good = validateEmail(email);
			if (!good) {
				validFriend = false;
			}
		}
	}
	else {
		var validFriend = validateEmail(friendEmail.value);
	}
	var validSender = validateEmail(senderEmail.value);

	if (validFriend) friendEmailLabel.className= "";
	if (validSender) senderEmailLabel.className= "";

	if (validFriend && validSender)
		return true;
	else{
		//alert ("Please enter a valid email address.");
		if(!validSender){
			senderEmailLabel.className= "error";
			senderEmail.focus();
		}
		if(!validFriend){
			friendEmailLabel.className= "error";
			friendEmail.focus();
		}
		return false;
	}
}

function validateEmail(sVal){
	var regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;

	if (regex.test(sVal)){
		return true;
	}
	else{
		return false;
	}
}

function stripSpace(sVal) {
	return sVal.replace(/^\s*|\s*$/g, "");
}

function stripHtml(sVal) {
    return sVal.replace(/<\/?[^>]+>/gi,"");
}

/***** COMMUNITY LAUNCH SCRIPTS *****/
// Library used for community services
var thisHost = window.location.host;

if (typeof(prependDomain) == "undefined") {
	prependDomain = "";
}

var Prefix = prependDomain;
var SIGNPAGE = Prefix + '/community/account/signin.jhtml';
var REGPAGE =  Prefix + '/community/account/join.jhtml';
var BOARDPAGE = Prefix + '/community/message_boards/adredirect.jhtml';
var MYPROFILEPAGE = Prefix + '/community/profiles/myProfile.jhtml';
var BOARDTHREADPAGE = Prefix + '/community/message_boards/viewthread.jhtml';
var REDFLAGPAGE = Prefix + '/community/message_boards/redflag.jhtml';
var ARTISTBOARDPAGE = Prefix + '/community/message_boards/artistboard.jhtml';
var CHATPAGE = Prefix + '/community/chat/chat.jhtml';
var IMPAGE = Prefix + '/community/im/imlaunch.jhtml';
var IMDOWNLOAD = Prefix + '/community/im/imdownload.jhtml';
var INFOPAGE = Prefix + '/community/account/smryinfo.jhtml';
var mcPAGE = Prefix + '/community/messages/mclaunch.jhtml';
var VMPHONEPAGE = Prefix + '/secure/vmu/editvmu.jhtml?returnPage='+escape('http://'+thisHost+'/community/account/editvmusuccess.jhtml');
var VMPHONETEXT = Prefix + '/secure/vmu/editvmu.jhtml?returnPage='+escape('http://'+thisHost+'/community/account/editvmutext.jhtml?updateVMUser=true');
var winTarget= "im";

 
var openPARAMS = 'resizable=yes,menubar=yes,location=yes,toolbar=yes,status=yes,scrollbars,directories=no,width=805,height=500';
// This is the default for winPARAMS
var winPARAMS = 'resizable=no,menubar=no,location=no,toolbar=no,status=no,scrollbars=no,directories=no,width=260,height=425';
var mcPARAMS = openPARAMS;
var vmPARAMS = 'resizable=no,menubar=no,location=no,toolbar=no,status=no,scrollbars=no,directories=no,width=376,height=260';

var USER_BROWSER = (((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 3 )) || ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 )))

function popMsg(daURL){
	newwin = window.open(daURL,'messages','width=556,height=486,scrollbars,resizable');
}

function signin(){
	signinPop();
}

function signinMsg(mtvparams, service){
	if(arguments.length != 2) {
		mtvparams="";
		service="";
	}
	editwindow = window.open(SIGNPAGE + '?serv=signin&mtvparams=' + mtvparams + '&oparam=' + service + '&target=', 'SignIn', openPARAMS); 
}

function signinPop(){
	editwindow = window.open(SIGNPAGE + '?serv=signin&mtvparams=&target=', 'SignIn', openPARAMS); 
}

function popSignin(){
	editwindow = window.open(SIGNPAGE + '?serv=signin&mtvparams=&target=', 'SignIn', openPARAMS); }

function editProfile(){
	if(USER_BROWSER){
		var editwindow;
		editwindow = window.open(SIGNPAGE + '?serv=profile&mtvparams=&target=&oparam=edit_profile&mesg=', 'SignIn', openPARAMS); 
	}else{	
		editwindow = window.open(SIGNPAGE + '?serv=signin&mtvparams=&oparam=edit_profile&target=', 'mtv', openPARAMS);
		//window.open(SIGNPAGE + '?serv=profile&mtvparams=&target=self&mesg=','EditProfile',IEPARAMS); 
	}
}

function goToMyProfile(){
	var editwindow;
	editwindow = window.open(MYPROFILEPAGE, 'mtv', openPARAMS);
}

function boardPop(bid,tid,mid,offset){
	var newBid;
	var comma = bid.indexOf(',');
	
	if(comma >  -1){
		newBid = bid.substring(comma+1, bid.length);
		bid = bid.substring(0,comma);
	}
	
	if(GetCookieUtil('cCommunityPhase')=='2' && newBid){ 
		community.hostedPageLink('discussion', newBid); 
	}
	else{
		var mbPARAMS = 'resizable=yes,menubar=no,location=no,toolbar,status,scrollbars,directories=no,width=760,height=435';
		if(arguments.length == 2){editwindow = window.open(BOARDPAGE + '?bID=' + bid + '&tID=' + tid, 'Board' + bid, mbPARAMS);}
		else if(arguments.length == 4){editwindow = window.open(BOARDTHREADPAGE + '?bID=' + bid + '&tID=' + tid + '&mID=' + mid + '&offset=' + offset, 'Board' + bid, mbPARAMS);}
		else{editwindow = window.open(BOARDPAGE + '?bID=' + bid, 'Board' + bid, mbPARAMS);}
		editwindow.focus;
		popCheck(editwindow);
	}
}

function popCheck(obj){
	if (!obj) {
		alert("Some content on MTV.com is displayed using pop-ups.  \n\nIf you're using a Pop-up Blocker, you'll need to permit pop-ups \nfrom MTV.com in order to view Videos, photos and other content on our site.");
	}
}

function redFlag(bid,mid) {
	open(REDFLAGPAGE + '?mID=' + mid + '&bID=' + bid, 'RedFlag', 'width=400,height=200');
}

function artistBoardPop(artistid){
var mbPARAMS = 'resizable=yes,menubar=no,location=no,toolbar,status,scrollbars,directories=no,width=600,height=440';
	editwindow = window.open(ARTISTBOARDPAGE + '?artistid=' + artistid, 'Board' + artistid, mbPARAMS); }

function popChat(room){
	var chatPARAMS = 'resizable=yes,menubar=no,location=no,toolbar=no,status=no,scrollbars,directories=no,width=605,height=560';
	editwindow = window.open(CHATPAGE + '?oparam=chat&room=' + room, 'chat' + room,chatPARAMS); }

// legacy chat opener
function launchPopUpApp(p,room){
	l = room.length; var room = room.substring(5,l) 
	var chatPARAMS = 'resizable=yes,menubar=no,location=no,toolbar=no,status=no,scrollbars,directories=no,width=605,height=560';
	editwindow = window.open(CHATPAGE + '?room=' + room, 'chat' + room,chatPARAMS);
		}

function popIm(){
	randid = new String(Math.random());
	var editwindow;
	popimwin = window.open(IMPAGE + '?rnd=' + randid,winTarget,winPARAMS); }

function popImDownload(){
	editwindow = window.open(IMDOWNLOAD, 'imdownload',openPARAMS); 	}

function sendMessage(username){
	randid = new String(Math.random());
	editwindow = window.open(IMPAGE + '?rnd=' + randid + '&sendto=' + username,'im',openPARAMS); }

function popEmailLogin(){
	editwindow = window.open(SIGNPAGE + '?serv=mail&mtvparams=email&target=self&mesg=', 'EmailLogin', openPARAMS); }

function popEmailRegister(){
	editwindow = window.open(REGPAGE + '?serv=mail&mtvparams=email&target=self&mesg=', 'EmailLogin', openPARAMS); }

function popMessageCenter(){
	editwindow = window.open(mcPAGE, 'messages', mcPARAMS); }

function editAccount(){
	editwindow = window.open(INFOPAGE, 'EditAccount', openPARAMS); }

function linkPhone(host){
	window.open(host+VMPHONEPAGE, 'LinkPhone', vmPARAMS); }
	
function linkPhoneForText(host){
	window.location.href = host+VMPHONETEXT; 
}


function popMessageCenterFromNav(page, params)
{
	editwindow = window.open('/community/messages/inbox.jhtml', 'messages', 'width=556,height=486,scrollbars=yes,resizable');
}


function logoutFromNav()
{
    document.logoutform.submit();
}

function signInFromNav()
{
    var thisUrl = document.location;
    signinMsg(thisUrl,'default');
}

function joinFromNav()
{
    var thisUrl = escape(document.location);

    genPop('/community/account/join.jhtml?serv=url&mtvparams=' + thisUrl, 'account', 'width=605,height=535,resizable,scrollbars');
}

function editProfileFromNav()
{
    location.href = '/community/profiles/profile.jhtml?username=' + if_nt_userName;
}

function editProfileWrapper()
{
    editProfileFromNav();
}

function editNewsletters()
{
	genPop('http://ebm.cheetahmail.com/r/regf2?aid=409502673&n=102&a=0', 'newsletters', 'width=690,height=550,resizable,scrollbars');
}

function editProfileWrapper()
{
	editProfile();
}


/***** SHELL SCRIPTS *****/

function toggleFBML() {
	if(document.getElementById('flashObj').style.display == 'none') {
		document.getElementById('noFlash').style.display='none';
		document.getElementById('flashObj').style.display='block';
	} else {
		document.getElementById('noFlash').style.display='block';
		document.getElementById('flashObj').style.display='none';
	}
}


var flip = "false";

function setHTML() {
	var loc = window.location+"";

	if (flip=='true'){
		if ((typeof window.opener != "undefined") && (window.opener != null)){
			window.close();
		}
		else {
			loc = loc.substring(0, loc.indexOf('.com/') + 5);
			if(legacyFBMLPopup=='true')
				window.open(loc);
			else
				window.location = loc;
		}
	}
	else{
		SetCookieUtil("sitePreferenceSet", "true", oneWeek);
		SetCookieUtil("cSiteVersion", "HTML", oneWeek);

		var hash = loc.indexOf('#');
		(hash >= 0) && ((hash +1) < loc.length) ? url = loc.substr(hash + 1): url = window.location.pathname + window.location.search;
		window.location = url;
	}

}

function setFBML() {

	if (flip=='true'){
		var loc = window.location+"";
		(loc.indexOf('?') > 0 ) ? loc = loc + "&siteVersion=FBML" : loc = loc + "?siteVersion=FBML";
		window.location = loc;
	}
	else{
		SetCookieUtil("sitePreferenceSet", "true", oneWeek);
		SetCookieUtil("cSiteVersion", "FBML", oneWeek);
		window.location.reload();
	}

}

function setUpgradeFlash() {
	document.getElementById('flashObj').style.display='none';
	document.getElementById('noFlash').style.display='none';
	if (typeof document.getElementById('upgradeFlash') != "undefined") {
		document.getElementById('upgradeFlashImage').src = "/relaunch/sitewide/images/flashupgrade/promo_02_html.jpg";
		document.getElementById('upgradeFlash').style.display='block';
	}
}


function report() {
	if (typeof REPORTING != "undefined") {

	var reportingObj = new Object();
	reportingObj.tVars  = new Object();
	var pageKeyVals = new Array();
	var communityVars = "";

	if (typeof adCall.pageVals != "undefined")
		pageKeyVals = adCall.pageVals.split(";");
	if (typeof adCall.additionalValues == "undefined")
		adCall.additionalValues = "";
	if (typeof clientEnvironmentVars == "undefined")
		clientEnvironmentVars = "";
	if (typeof personalizationVars == "undefined")
		personalizationVars = "";
	if (typeof community != "undefined" )
		communityVars = community.setReportingVars();

	for (var item = 0; item < pageKeyVals.length; item++) {
		var p = pageKeyVals[item].split("=");
		if (p[0] != null && p[0] != undefined && p[0] !="")
			reportingObj[p[0]] = p[1];
	}
	otherVals = adCall.additionalValues + ";" +  clientEnvironmentVars + ";" + personalizationVars + ";" + communityVars;
	var otherKeyVals = otherVals.split(";");
	for (var item = 0; item < otherKeyVals.length; item++) {
		var p = otherKeyVals[item].split("=");
		if (p[0] != null && p[0] != undefined && p[0] !="")
			reportingObj.tVars[p[0]] = p[1];
	}
	REPORTING.makeCall('fbml_pv', reportingObj);
	}
}

function reportIMX(tagUrl, protocol, params){
	var imxTag = new Image();

	if ((protocol=="flipbook" && !(document.referrer.indexOf("fid="+params) > -1)) || (protocol != 'flipbook')){
		imxTag.src = tagUrl;
	}
}

function disableStyleSheet() {
	var i=0;
	if (document.styleSheets.length > 0) {
		cs=!document.styleSheets[0].disabled;
		for (i=0; i< document.styleSheets.length; i++) document.styleSheets[i].disabled=cs;
	}
	cs=true;
}

function hatTrick(hatNumber, subset) {
	var total = 66;
	var bodyTag = document.getElementsByTagName('body')[0];
	var classList = bodyTag.className;
	if (classList.indexOf("hat") > -1) {
		var tempClasses = new Array();
		var classes = classList.split(" ");
		var j = 0;
		for(i = 0; i < classes.length; i++) {
			if(classes[i].substr(0,3) != "hat") tempClasses[j++] = classes[i];
		}
		classList = tempClasses.join(" ");
	}

	if (!hatNumber) hatNumber = Math.floor(total*Math.random());
	var newHat = " hat" + hatNumber;
	bodyTag.className = classList + newHat + "";
}

function toggleContinousPlay(preference) {
	if(preference == null) {
		preference = new Object();
		if(GetCookieUtil("cContinuousPlay") == null){
			preference.checked = true;
		} else {
			preference.checked = GetCookieUtil("cContinuousPlay");
		}
	}
	document.getElementById('embeddedPlayer').setContinuousPlay(preference.checked);
	isChecked = (preference.checked == false) ? "0" : "1";

	SetCookieUtil("cContinuousPlay", isChecked, oneYear);
	}

function toggleSearchImages(images){
	SetCookieUtil("cSearchImages", images, oneYear);

	var list = document.getElementById('resultListDiv').getElementsByTagName('ol');
	var slats = list[0].getElementsByTagName('li');

	for (var i=0; i < slats.length; i++){
		var thumb = slats[i].getElementsByTagName('div')[0];

		if(thumb != undefined){
			if (images == 'false'){
				thumb.style.display = 'none';
				document.getElementById('images-off').style.display = 'block';
				document.getElementById('images-on').style.display = 'none';
				slats[i].className = "";
			}
			else{
				thumb.style.display = 'block';
				document.getElementById('images-off').style.display = 'none';
				document.getElementById('images-on').style.display = 'block';
				slats[i].className = "img140x105";

			}
		}
	}

}



// tooltips
//****************************************

function Tooltip() {
	var div;
	var posx;
	var posy;
	var eventX;
	var eventY;
	var winWidth;
	var winHeight;
	var width = 200;
	var timeout;

	function setXandY(e) {
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		eventX = e.clientX;
		eventY = e.clientY;
	}

	function position() {
		if (eventX + div.offsetWidth + 5 > winWidth) {
			div.style.left = posx - 7 - div.offsetWidth  + "px";
		} else {
			div.style.left = posx + 12  + "px";
		}

		if (eventY - div.offsetHeight - 5 < 0) {
			div.style.top = (posy + 20)+ "px";
		} else {
			div.style.top = (posy - div.offsetHeight - 5)+ "px";
		}
	}

	function getWindowSize () {
		if (typeof window.innerWidth == "number") {
			winWidth = window.innerWidth;
			winHeight = window.innerHeight;
		} else {
			if (document.documentElement && (document.documentElement.clientWidth)) {
				winWidth = document.documentElement.clientWidth;
				winHeight = document.documentElement.clientHeight;
			} else {
				winWidth = document.body.clientWidth;
				winHeight = document.body.clientHeight;
			}
		}
	}

	function toggle(el, content) {
		div = document.getElementById("tooltip");
		if(content == null) {
			hide = function() {
				document.onmousemove = null;
				div.style.left = "-999px";
			}
			timeout = setTimeout("hide()", 100);

		} else {
			document.onmousemove = move;
			if (content.offsetWidth < width ) div.style.width = content.offsetWidth + 18 + "px"; else div.style.width = width + "px";
			div.getElementsByTagName("span")[0].innerHTML = content.innerHTML;
			position();
			div.style.display="block";
			return false;
		}
	}

	function move(e) {
		if (!e) var e = window.event;
		setXandY(e);
		position();
		return false;
	}

	function deactivate(idContext) {
		anchorTags = idContext == null? document.getElementsByTagName("a"): idContext.getElementsByTagName("a");
		for (i = 0; i < anchorTags.length; i++) {
			if (anchorTags[i].className.indexOf("tooltipControl") != -1 ) {
				anchorTags[i].onmouseover = null;
				anchorTags[i].onmouseout = null;
			}
		}

	}
	function activate (idContext) {
		anchorTags = idContext == null? document.getElementsByTagName("a"): idContext.getElementsByTagName("a");
		for (i = 0; i < anchorTags.length; i++) {
			if (anchorTags[i].className.indexOf("tooltipControl") != -1 ) {
				// disable the alt text on the images so both don't show
				var imgs = anchorTags[i].getElementsByTagName("img");
					for (j = 0; j < imgs.length; j++) {
						imgs[j].alt="";
					}

				var spans = anchorTags[i].getElementsByTagName("span");
				for (j = 0; j < spans.length; j++) {
					if (spans[j].className.indexOf("tooltipContent") != -1 ) {
						anchorTags[i].tooltipContent = spans[j];
						if (anchorTags[i].tooltipContent.innerHTML.length > 0) {
							anchorTags[i].onmouseover = function (e) {
								// handle tooltip display and position
								clearTimeout(timeout);
								setXandY(e)
								el = this;
								toggle(el, this.tooltipContent);
							}
							anchorTags[i].onmouseout = function () {
								el = this;
								toggle(el);
							}
						}

					}
				}
			}
		}
		getWindowSize();
	}
	this.activate = activate;
	this.deactivate = deactivate;
}
var tooltip = new Tooltip();

var Utils = {
	show : function(element){
		if (element!=null && element.className.indexOf('hide') >= 0){
			element.className = element.className.substring(0, element.className.indexOf('hide'));
		}
	},

	hide : function(element){
		if (element!=null && element.className.indexOf('hide') < 0){
			element.className = element.className + " hide";
		}
	},

	// returns the first parent of a node with a given class
	findParent : function(obj, parentClass){
		var parent = obj.parentNode;
		if (parent.className && parent.className.indexOf(parentClass) != -1)
			return parent;
		else return this.findParent(parent, parentClass);
	},

	// returns the first child of a node with a given class
	findChild : function(obj, childClass){
		var match;
		for(var i=0; i<obj.childNodes.length; i++){
			var child = obj.childNodes[i];
			if (child.className && child.className==childClass)
				match = child;
			else if(child.childNodes && child.childNodes.length>0){
				match = this.findChild(child, childClass);
			}
			if(match) return match;
		}
	}
}

var Paginate = {
	getPrev : function(obj){
		this.page(obj, 'prev');
	},

	getNext : function(obj){
		this.page(obj, 'next');
	},

	page : function(obj, direction){
		var mdl = Utils.findParent(obj, 'mdl');

		var pagination = Utils.findChild(mdl, 'pagination');
		var pageCount = Utils.findChild(pagination, 'sm');

		var count = new Array();
		count = pageCount.innerHTML.split(' ');

		var current = count[0]*1;
		var total = count[2]*1;
		var requestPage;

		if(direction=='prev'){
			if(current==1) requestPage = total;
			else requestPage = current - 1;
		}
		else{
			if(current==total) requestPage = 1;
			else requestPage = current + 1;
		}

		var listCount = 0;
		for(var i=0; i<mdl.childNodes.length; i++){
			var child = mdl.childNodes[i];
			if (child.nodeName=='OL' || (child.nodeName=='DIV' && child.className.indexOf('paginationGroup')>-1)){
				listCount++;
				if (listCount!=requestPage) Utils.hide(child);
				else Utils.show(child);
			}
		}

		var newPageCounter = requestPage + " of " + total;
		pageCount.replaceChild(document.createTextNode(newPageCounter), pageCount.childNodes[0]);
	}
}


// WMP Detection
// **************************************
function getWMPVer() {
	var version = "undefined";
	var gkoaxwma = detectMozillaActiveXObject();

	if (window.ActiveXObject || gkoaxwma) {
		try	{
			if (window.ActiveXObject && navigator.userAgent.indexOf('Win') != -1) {
				var axo = new ActiveXObject("WMPlayer.OCX.7");
			} else if (gkoaxwma) {
				var axo = new GeckoActiveXObject("WMPlayer.OCX.7");
			}
			if (axo) {
				parseFloat(axo.versionInfo);

				version=parseFloat(axo.versionInfo);
			}
		}
		catch(e) {}
	}

	return version;
}


function detectWMPVer(reqVer) {
	var version = getWMPVer();

	if (parseInt(version) != NaN &&
		parseInt(version) >= parseFloat(reqVer)) {
		return true;

	} else if (parseInt(version) != NaN &&
		parseInt(version) < parseFloat(reqVer)) {
		return false;

	} else if (navigator.plugins && navigator.plugins.length) {
		for (var i=0; i < navigator.plugins.length; i++) {
			if (navigator.plugins[i].name.indexOf('Windows Media') != -1) {
				// WMP installed but cannot detect version.  hope for the best
				return true;
			}
		}
	} else {
		return false;
	}
}


function detectMozillaActiveXObject() {
	var hasaxo;
	if (navigator.plugins && navigator.plugins.length) {
		var hasaxo = false;
		for (var i=0; i < navigator.plugins.length; i++) {
			if (navigator.plugins[i].name.indexOf('ActiveX') != -1 && window.GeckoActiveXObject) {
				hasaxo = true;
				break;
			}
		}
	}
	return hasaxo;
}



// Flash Detection
// **************************************
function getFlashVer() {

	PlayerVersion = function(arrVersion){
		this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
		this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
		this.revision = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
	}

	var version = new PlayerVersion([0,0,0]);
	if(navigator.plugins && navigator.mimeTypes.length){
		var x = navigator.plugins["Shockwave Flash"];
		if(x && x.description) {
			version = new PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
		}
	}else{
		try{
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		}catch(e){
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				version = new PlayerVersion([6,0,21]);
				axo.AllowScriptAccess = "always";
			} catch(e) {
				if (PlayerVersion.major == 6) {
					return version;
				}
			}
			try {
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			} catch(e) {}
		}
		if (axo != null) {
			version = new PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
		}
	}
	return version;
}


function detectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
	var versionArr = getFlashVer();

	if (versionArr.major > parseFloat(reqMajorVer)) {
		return true;
	} else if (versionArr.major == parseFloat(reqMajorVer)) {
		if (versionArr.minor > parseFloat(reqMinorVer)) {
			return true;
		} else if (versionArr.minor == parseFloat(reqMinorVer)) {
			if (versionArr.revision >= parseFloat(reqRevision)) {
				return true;
			}
		}
	}
	return false;
}

function swapImage (targetImg, srcImg) {
	var swapImg = document.getElementById(targetImg);
	swapImg.src = srcImg.src;
}

navigateTop = function(args) {
	top.location.href = args;
}

/***** SIMPLE WINDOW OPEN AND FOCUS UTILITY *****/
function openAndFocus(url,name,features){
	var o = window.open(url, name, features);
	o.focus();
}

/***** RETURNS THE VALUE OF THE SPECIFIED VAR FROM URL STRING *****/
function getURLVar(urlVarName) {
	//divide the URL in half at the '?'
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;
}


/***** WINDOW OPEN UTILITY *****/
function windowOpener(args){
	var argsArray = args.split(',')

	var url = "";
	var name = "";
	var features = "";
	var replace = "";

	for (i = 0; i < argsArray.length; i++){
		switch(i){
			case 0:
				url = argsArray[i];
				break;
			case 1:
				name = argsArray[i];
				break;
			case 2:
				features = argsArray[i];
				break;
			default:
				if (argsArray[i].indexOf("replace") > -1){
					replace = argsArray[i];
				}
				else {
					features = features + "," + argsArray[i];
				}
				break;
			}
	}

	window.open(url, name, features, replace);
}

function popWin(url,name,w,h,scroll,resize)
{
	scroll = (scroll==null) ? 1:scroll
	resize = (resize==null) ? 1:resize

 	window.open(url,name,'width='+w+',height='+h+',scrollbars='+scroll+',resizable='+resize);
}

// Move to the MTV class.  Just visiting for now.
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

BrowserDetect.init();

/**** WMPBROWSER MEDIA PLAYERS ****/
// WMPBrowser 
function WMPBrowser(targetDiv,playerName) {
	
	//private vars
	var name = "wmplayer"
	var _iframe = null;
	var _target;
	var _currentSrc;
	
	this.updateWindow = function(src,x,y,w,h) {
		
		try {
			
			if(!_iframe)
				_init();
				
			_iframe.style.cssText = 'position:absolute;float:left;padding-left:' + x + 'px;padding-top:'+y + 'px;width:' + w + 'px;height:' + h + 'px;';
			
		}catch(e) {	}

		_setSrc(src);
		
		setPlayerSize(w,h)
	}
	
	this.pause = function() {
		try {
			_getWMPPlayer().controls.pause();
		} catch (e) {}
	};
	
	this.stop = function() {
		try {
			_getWMPPlayer().controls.stop();
		} catch (e) {}
	};
	
	this.play = function() {
		try {
			_getWMPPlayer().controls.play();
		} catch (e) {}
	};
	
	this.onVideoEnd = function() {
		try{
			
			var r = this;
			setTimeout(function () { r.destroyWindow(); }, 50);
			
		}catch(e){
			
			Debug.trace("IframeBrowser :: e " + e);
		}
	}
	
	this.destroyWindow = function(){
		
		try {
			
			if(_iframe){
				_setSrc("about:blank");
				_target.removeChild(_iframe);
				_getFlashContainer().videoPlayStateChange("Media End");
			}
			
		} catch (e) {
			
			Debug.trace("doDestroy: e = " + e);
		}
	}
	
	this.getCurrentPosition = function() {
		
		try{
			return _getWMPPlayer().controls.currentPosition;
		}catch(e){
			return 0;
		}
	}
	
	this.setCurrentPosition = function(val) {
		
		try{
			_getWMPPlayer().controls.currentPosition = val;
		}catch(e){
			return 0;
		}
	}
	
	this.getDuration = function() {
		
		try{
			return _getPlayerContainer().player.currentMedia.duration;
		}catch(e){
			return 0;
		}
	}
	
	this.setPlayerSize = function(width,height){
		
		try {
				
			_iframe.setAttribute("width",width);
			_iframe.setAttribute("height",height);
			
		} catch (e) {
			
			// ping
			var r = this;
			setTimeout(function () { r.setPlayerSize(width,height); }, 100);
		}
	};
	
	this.setPlayerVolume = function(volume) {
		
		if(!_iframe)
			return;
			
		try {
			_getWMPPlayer().settings.volume = volume;
		} catch(e) {}
	};
	
	this.setPlayerPlayState = function(newstate) {
		
		try {
			
			if(newstate == "Media End")
				this.onVideoEnd();
			else
				_getFlashContainer().videoPlayStateChange(newstate);
			
		} catch (e) {
			Debug.trace("setPlayerPlayState e:" + e);
		}
	};
	
	// private methods
	
	var _getFlashContainer = function() {
		return document.getElementById(playerName);
	}
	
	var _getPlayerContainer = function() {
		return window.frames[name];
	};
	
	var _getWMPPlayer = function() {
		return _getPlayerContainer().player;
	};
	
	var _init = function()	{
		
		try {
			
			if(navigator.appName=="Microsoft Internet Explorer"){
				
				_iframe = document.createElement('<iframe name="' + name + '" id="' + name + '" allowtransparency="true" sr' + 'c="" scrolling="no" frameborder="0"></iframe>');
				
			} else {
				_iframe = document.createElement("iframe");
				_iframe.setAttribute("id", name);
				_iframe.setAttribute("name", name);
				_iframe.setAttribute("frameborder", 0);
				_iframe.setAttribute("scrolling", "no");
			}
			
			_iframe.style.cssText = "position:absolute;float:left;";
			
			var existingNode = document.getElementById(name);
			if(existingNode)
				existingNode.parentNode.removeChild(existingNode);
			
			_target = document.getElementById(targetDiv);	
			_target.appendChild(_iframe);
			
		}catch(e) {	}
	}
	
	var _setSrc = function(src) {
		
		if(src && _currentSrc!=src){
			
			try{
				
				if(_iframe.contentWindow)
					_iframe.contentWindow.location.replace(src);
				else
					_iframe.src = src;	
				
				_currentSrc = src;
				
			}catch(e){
				
				_iframe.src = _currentSrc = src;	
			} 
		}
	}
};


/**** PLAYER EMBEDDER/CONTROLLER FOR MULTIPLE MEDIA PLAYERS ****/
/**
 * the id is the string form of the var name
 * it will be called by flash to communicate with the instance
 * var pc = PlayerController("pc");
 */ 
function PlayerController(instanceId) {
	
	// required
	if(!instanceId)
		return;
		
	// public vars
	this.configUrl = "/player/embed/mtv/movieawards/configuration.jhtml"
	this.playerUrl = "";
	
	// private vars
	var _players = new Array();
	var _instanceId = instanceId; 
	var _stopPlayers = function(dontStopId) {
		for(var i in _players) {
			var id = _players[i];
			if(id != dontStopId){
				document.getElementById(id).stopPlayer();
			}
		}
	}
	
	this.write = function(navigateToUrl,target,elementId,width,height,flashVars) {
		
		var jsInstanceId = _instanceId+"."+elementId;
		_players.push(elementId);
		
		var so = new SWFPlayerObject(jsInstanceId,elementId, width ? width : "240", height ? height : "180");
		so.addVariable("playerControllerId",_instanceId);
		so.addVariable("CONFIG_URL",this.configUrl+navigateToUrl);
		so.addParam("wmode","transparent")
		
		if(this.playerUrl)
			so.setPath(this.playerUrl);
		
		//for(var i in flashVars) - doesn't work because community uses prototype
		for (var i=0; i < flashVars.length; i++)
			so.addVariable(i,flashVars[i]);
		
		so.write(target);
	}
		this.onPlay = function (id) {
		
		try {
			
			if(id)
				_stopPlayers(id);
		}catch(e) {}
	}

	this.write2 = function(configUrl,navigateToUrl,target,elementId,width,height,flashVars) {
		
		var jsInstanceId = _instanceId+"."+elementId;
		_players.push(elementId);
		
		var so = new SWFPlayerObject(jsInstanceId,elementId, width ? width : "240", height ? height : "180");
		so.addVariable("playerControllerId",_instanceId);
		so.addVariable("CONFIG_URL",this.configUrl+'?'+escape(navigateToUrl));
		so.addParam("wmode","transparent");
		
		if(this.playerUrl)
			so.setPath(this.playerUrl);
		
		//for(var i in flashVars) - doesn't work because community uses prototype
		for (var i=0; i < flashVars.length; i++)
			so.addVariable(i,flashVars[i]);
		
		so.write(target);
	}
		this.onPlay = function (id) {
		
		try {
			
			if(id)
				_stopPlayers(id);
		}catch(e) {}
	}
};



/**** WRAPPER UTIL CLASS ****/
/**
 * the id is the string form of the var name
 * it will be called by flash to communicate with the instance
 * var sw = SwfPlayerObject("sw");
 */ 
function SWFPlayerObject(instanceId,id,width,height,version,bgcolor) {
	
	// required. for flash to reference the page.
	if(!instanceId)
		return;
		
	this.wmp = null;
	this.coad = null;
	
	version = version ? version : "8";
	width = width ? width : 300;
	height = height ? height : 300;
	bgcolor = !isNaN(bgcolor) ? bgcolor : "#000000";
	
	var so = new SWFObject("/global/apps/fbml/mtvi/flash_browser_fp8.swf", id, width, height, version, bgcolor);
	
	if(location.search.indexOf("jsDebugOn")!=-1)
		so.addVariable("jsDebuggerFunction",instanceId+".log");
	
	if(location.search.indexOf("lcDebugOn=true")!=-1)
		so.addVariable("lcDebugOn","true");
	
	this.setPath = function(path) {
		so.setAttribute('swf', path);
	}
	
	// a javascript reference to this by string.
	so.addVariable("jsInstanceId",instanceId)
	
	// a reference to the embedded plug in
	so.addVariable("elementId",id);
	
	this.addParam = function(name,value) {
		so.addParam(name,value);
		so.addVariable(name,value);
	}
	
	this.addVariable = function(name,value) {
		so.addVariable(name,value);
	}
	
	this.setAttribute = function(name,value) {
		so.setAttribute(name,value);
	}
	
	this.addWMPSupport = function(target) {
		this.wmp = new WMPBrowser(target,id);
	}
	
	this.addCoAd = function(target) {
		this.coad = new CoAdBrowser(target);
	}
	
	this.write = function(target) {
		so.write(target);
	}
	
	this.openAndFocus = function(url,name,features){
		var o = window.open(url, name, features);
		o.focus();
	}
	
	this.log = function(statement){
		
		if(document.getElementById("ta") && statement){
			document.getElementById("ta").value += ('\n'+statement);	
		}else{
			var d = document.createElement('div');
			var width = "100%";
			var height = 200;
			var left = 10;
			var top = screen.availHeight - height - ((window.ActiveXObject) ? 60 : 90);
			var newStyle = 'display:inline;position:absolute;z-index:300;left:' + left + 'px;top:' + top + 'px;width:' + width + 'px;height:' + height + 'px;';	
			d.style.cssText = newStyle;
			document.body.appendChild(d);	
			
			// textarea
			var ta= document.createElement('textarea');
			ta.setAttribute("name","ta");
			ta.setAttribute("id","ta");
			ta.setAttribute("cols","130");
			ta.setAttribute("rows","8");
			ta.setAttribute("wrap","off");
			d.appendChild(ta);	
			var db= document.createElement('button');
			db.setAttribute("name","db");
			db.setAttribute("id","db");
			var buttext = document.createTextNode('clear');
			db.appendChild(buttext);
			db.onclick = function () {
				document.getElementById("ta").value = "";
			};
		
			d.appendChild(db);	
			if(statement){
				ta.value+=statement;
			}
		}
	}
};

/**** COAD FOR EMBEDDED PLAYER ON HTML SITE ****/
function CoAdBrowser(targetDiv,scrolling) {
	
	//private vars
	var inited = false;
	var iframeBrowser;
	var target;
	var currentSrc;
	
	this.updateWindow = function (src,width,height) {

		try {
		
			if(!inited)
				init();
				
			iframeBrowser.style.cssText = 	'width:' + width + 'px;height:' + height + 'px;';	
		}catch(e) {}
		
		setSrc(src);
	}
	
	this.destroyWindow = function() {
		try{
			iframeBrowser.contentWindow.location.replace("about:blank");
		}catch(e){}
		target.removeChild(iframeBrowser);	
	}
	
	// private methods
	var init = function() {
		
		try {
		
			var scrolling = scrolling && scrolling != "null" ? scrolling : "no";
			if(navigator.appName=="Microsoft Internet Explorer"){
				iframeBrowser = document.createElement('<iframe sr' + 'c="" allowtransparency="true" scrolling="' + scrolling + '" frameborder="0"></iframe>');
			}else{
				iframeBrowser = document.createElement('iframe');
				iframeBrowser.setAttribute("frameborder", 0);
				iframeBrowser.setAttribute("scrolling", scrolling);
				iframeBrowser.setAttribute("allowtransparency", "true"); 	
				iframeBrowser.setAttribute("background-color", "transparent"); 	
			}
			iframeBrowser.style.display = 'none';
			
			target = document.getElementById(targetDiv);	
			
			target.appendChild(iframeBrowser);
			
			inited = true;
			
		}catch(e) {}
	}
	
	var setSrc = function(src) {

		if(src && currentSrc!=src){
	
			try{
			
				if(iframeBrowser.contentWindow)
					iframeBrowser.contentWindow.location.replace(src);
				else
					iframeBrowser.src = src;	
				
				currentSrc = src;
			}catch(e){
			
				iframeBrowser.src = src;
				currentSrc = src;
			} 
		}
	}

};


function CoAdPNG(targetDiv) {
	
	//private vars
	var initialized = false;
	var target;
	var container;
	var currentSrc;
	
	this.update = function (src,width,height,clickTag) {		
		try {
			if(!initialized){
				init();
			}
			else if(currentSrc!=src) this.destroy();
			
			if(src && currentSrc!=src){
				container = document.createElement('div');
				container.style.width = width + "px";
				container.style.height = height + "px";
				container.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='image')";
				container.onclick = function(){ window.open(clickTag); }
	
				if(isIE==undefined){
					var img = document.createElement('img');
					img.src = src;
					currentSrc = src;
			
					container.appendChild(img);
				}
				
				target.appendChild(container);
			}				
		}
		catch(e) {}		
	}
	
	this.destroy = function() {
		target.removeChild(container);	
	}
	
	// private methods
	var init = function() {
		try {
			target = document.getElementById(targetDiv);
			initialized = true;
		}
		catch(e) {}
	}

};

/**** TACONITE SCRIPTS ****/
/**
    @fileoverview
    This JavaScript file represents the core browser-side functionality
    supplied by Taconite. In general, the tools in this file wrap an instance
    of XMLHttpRequest object and provide utility methods for gather data from
    form elements to be sent to the server as par of an Ajax request.
*/

/**
    Constructor for the AjaxRequest class. 

    <br><br>
    Example:

    <br><br>
    var ajaxRequest = new AjaxRequest("YOUR_URL");

    @class The AjaxRequest object wraps an instance of XMLHttpRequest and provides 
    facilities for setting functions that are called before a request is made
    and after a request returns. By default, AjaxRequest handles the server
    response by simply calling eval(), passing to it the responseText from 
    the XMLHttpRequestObject, of course assuming that the response was 
    generated by Taconite on the server side and that running eval() will 
    update the web page.<br><br>Example Usage:<br><br>var ajaxRequest = new AjaxRequest("YOUR_URL");
    <br>ajaxRequest.addFormElements("form_element_id_attribute_value");
    <br>ajaxRequest.sendRequest();

    @constructor
    @param {String} a String repesenting the URL to which the Ajax request
    will be sent.
*/
var taconite_client_version=1.6;
function AjaxRequest(url) {
    /** @private */
    var self = this;

    /** @private */
    var xmlHttp = createXMLHttpRequest();
    
    /** @private */
    var queryString = "";

    /** @private */
    var requestURL = url;

    /** @private */
    var method = "GET";

    /** @private */
    var preRequest = null;

    /** @private */
    var postRequest = null;

    /** @private */
    var debugResponse = false;
	
    /** @private */
    var async = true;

    /** @private errorHandler*/ 
    var errorHandler = null;

	/** @private */ 
    var useCacheBust = false;

    /**
        Return the instance of the XMLHttpRequest object wrapped by this object.
        @return XMLHttpRequest
    */
    this.getXMLHttpRequestObject = function() {
        return xmlHttp;
    }

    /**
        Set the pre-request function. This function will be called prior to 
        sending the Ajax request. The pre-request function is passed a reference
        to this object.
        @param {Function} The function to be called prior to sending the Ajax
        request. The function is passed a refernce of this object.
    */
    this.setPreRequest = function(func) {
        preRequest = func;
    }

    /**
        Set the post-request function. This function will be called after the
        response has been received and after eval() has been called using the 
        XMLHttpRequest object's responseText. The post-request function is passed 
        a reference to this object.
        @param {Function} The function to be called after receiving the Ajax
        response. The function is passed a refernce of this object.
    */
    this.setPostRequest = function(func) {
        postRequest = func;
    }
    
    /**
        Return the post request function.
    */
    this.getPostRequest = function() {
        return postRequest;
    }

    /**
        Send the Ajax request using the POST method. Use with caution -- some
        browsers do not support the POST method with the XMLHttpRequest object.
    */
    this.setUsePOST = function() {
        method = "POST";
    }

    /**
        Send the Ajax request using the GET method, where parameters are sent
        as a query string appended to the URL. This is the default behavior.
    */
    this.setUseGET = function() {
        method = "GET";
    }

    /**
        Enable client-side debugging.  The server's response will be written
        to a text area appended to the bottom of the page.  If parsing is
        performed on the client side, then the results of the parsing operations
        are shown in their own text areas.
    */
    this.setEchoDebugInfo = function() {
        debugResponse = true;
    }

    /**
        Indicate if debugging is enabled.
        @return boolean
    */
    this.isEchoDebugInfo = function() {
        return debugResponse;
    }

    /**
        Set the query string that will be sent to the server. For GET
        requests, the query string is appended to the URL. For POST
        requests, the query string is sent in the request body. This 
        method is useful, for example, if you want to send an XML string
        or JSON string to the server.
        @param {String} qa, the new query string value.
    */
    this.setQueryString = function(qs) {
        queryString = qs;
    }

    /**
        Return the query string.
        @return The query string.
    */
    this.getQueryString = function() {
        return queryString;
    }

    /** 
        @param {Boolean} asyncBoolean, set to true if asynchronous request, false synchronous request. 
    */
    this.setAsync = function(asyncBoolean){
            async = asyncBoolean;
    }

    /** 
        @param {Function} Set the error handler function that is called if the 
        server's HTTP response code is something other than 200.
    */	
    this.setErrorHandler = function(func){
        errorHandler = func;
    }
	
	 /**
        Set cache busting on the request. (reifmanm)
    */
    this.setUseCacheBust = function() {
		useCacheBust = true;
    }
	
    /**
        Add all of the form elements under the specified form to the query
        string to be sent to the server as part of the Ajax request. The values
        are automatically encoded.
        @param {String} formID, the value of the id attribute of the form from
        which you wish to accumulate the form values.
    */
    this.addFormElements = function(formID) {
        var formElements = document.getElementById(formID).elements;
        var values = toQueryString(formElements);
        accumulateQueryString(values);
    }

    /** @private */
    function accumulateQueryString(newValues) {
        if(queryString == "") {
            queryString = newValues; 
        }
        else {
            queryString = queryString + "&" +  newValues;
        }
    }

    /**
        Add the name/value pair to the query string.
        @param {String} name
        @param {String} value
    */
    this.addNameValuePair = function(name, value) {
        var nameValuePair = name + "=" + encodeURIComponent(value);
        accumulateQueryString(nameValuePair);
    }

    

   /**
        Same as addNamedFormElements, except it will filter form elements by form's id.
        For example, these are all valid uses:<br>
        <br>ajaxRequest.addNamedFormElements("form-id""element-name-1");
        <br>ajaxRequest.addNamedFormElements("form-id","element-name-1",
        "element-name-2", "element-name-3");
    */
    this.addNamedFormElementsByFormID = function() {
        var elementName = "";
        var namedElements = null;

        for(var i = 1; i < arguments.length; i++) {
            elementName = arguments[i];
            namedElements = document.getElementsByName(elementName);
            var arNamedElements = new Array();
            for(j = 0; j < namedElements.length; j++) {
                if(namedElements[j].form  && namedElements[j].form.getAttribute("id") == arguments[0]){
                    arNamedElements.push(namedElements[j]);				
                }
            }
            if(arNamedElements.length > 0){
                elementValues = toQueryString(arNamedElements);
	        accumulateQueryString(elementValues);
            }
        }
    }








    /**
        Add the values of the named form elements to the query string to be
        sent to the server as part of the Ajax request. This method takes any 
        number of Strings representing the form elements for wish you wish to 
        accumulate the values. The Strings must be the value of the element's 
        name attribute.<br><br>For example, these are all valid uses:<br>
        <br>ajaxRequest.addNamedFormElements("element-name-1");
        <br>ajaxRequest.addNamedFormElements("element-name-1", "element-name-2", "element-name-3");
    */
    this.addNamedFormElements = function() {
        var elementName = "";
        var namedElements = null;

        for(var i = 0; i < arguments.length; i++) {
            elementName = arguments[i];
            namedElements = document.getElementsByName(elementName);

            elementValues = toQueryString(namedElements);

            accumulateQueryString(elementValues);
        }

    }

    /**
        Add the values of the id'd form elements to the query string to be
        sent to the server as part of the Ajax request. This method takes any 
        number of Strings representing the ids of the form elements for wish you wish to 
        accumulate the values. The Strings must be the value of the element's 
        name attribute.<br><br>For example, these are all valid uses:<br>
        <br>ajaxRequest.addFormElementsById("element-id-1");
        <br>ajaxRequest.addFormElementsById("element-id-1", "element-id-2", "element-id-3");
    */
    this.addFormElementsById = function() {
        var id = "";
        var element = null;
        var elements = new Array();

        for(var h = 0; h < arguments.length; h++) {
            element = document.getElementById(arguments[h]);
            if(element != null) {
                elements[h] = element;
            }
        }

        elementValues = toQueryString(elements);
        accumulateQueryString(elementValues);
    }

    /**
        Send the Ajax request.
    */
    this.sendRequest = function() {
        if(preRequest) {
            preRequest(self);
        }

        var obj = this;
		if(async)
	        xmlHttp.onreadystatechange = function () { handleStateChange(self) };

        if(useCacheBust){
			if(requestURL.indexOf("?") > 0) {
	            requestURL = requestURL + "&ts=" + new Date().getTime();
	        }
	        else {
	            requestURL = requestURL + "?ts=" + new Date().getTime();
	        }
		}

        try {
            if(method == "GET") {
                if(queryString.length > 0) {
                    var connector = (requestURL.indexOf("?") > 0) ? "&" : "?";
					requestURL = requestURL + connector + queryString;
                }
                xmlHttp.open(method, requestURL, async);
                xmlHttp.send(null);
            }
            else {
                xmlHttp.open(method, requestURL, async);
                //Fix a bug in Firefox when posting
                try {
                    if (xmlHttp.overrideMimeType) {
                        xmlHttp.setRequestHeader("Connection", "close");//set header after open
                    }			
                }
                catch(e) {
                    // Do nothing
                }
                xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
                xmlHttp.send(queryString);
            }
        }
        catch(exception) {
            if(errorHandler) {
                errorHandler(self, exception);
            }
            else {
                throw exception;
            }
        }

        if(!async) {  //synchronous request, handle the state change
            handleStateChange(self);
        }

        if(self.isEchoDebugInfo()) {
            echoRequestParams();
        }
    }

    handleStateChange = function(ajaxRequest) {
        if(ajaxRequest.getXMLHttpRequestObject().readyState != 4) {
            return;
        }
        try {
            var debug = ajaxRequest.isEchoDebugInfo();
            if(debug) {
                echoResponse(ajaxRequest);
            }

            //handle null responseXML
            var nodes = null;
            if (ajaxRequest.getXMLHttpRequestObject().responseXML != null) {
                nodes = ajaxRequest.getXMLHttpRequestObject().responseXML.documentElement.childNodes;
            }
            else {
                nodes = new Array();
            }

            var parser = new XhtmlToDOMParser();
            var parseInBrowser = "";
            for(var i = 0; i < nodes.length; i++) {
                if(nodes[i].nodeType != 1 || !isTaconiteTag(nodes[i])) {
                    continue;
                }

                parseInBrowser = nodes[i].getAttribute("parseInBrowser");
                if(parseInBrowser == "true") {
                    parser.parseXhtml(nodes[i]);
                    var js = parser.getJavaScript();
                    if(debug) {
                        echoParsedJavaScript(js);
                    }
                }
                else {
                    eval(nodes[i].firstChild.nodeValue);
                }
            }

            if(ajaxRequest.getPostRequest()) {
                var f = ajaxRequest.getPostRequest();
                f(ajaxRequest);
            }
        }
        catch(exception) {
            if(errorHandler) {
                errorHandler(self, exception);
            }
            else {
                throw exception;
            }
        }
    }

    /** @private */
    function isTaconiteTag(node) {
        return node.tagName.substring(0, 9) == "taconite-";
    }

    /** @private */
    function toQueryString(elements) {
        var node = null;
        var qs = "";
        var name = "";

        var tempString = "";
        for(var i = 0; i < elements.length; i++) {
            tempString = "";
            node = elements[i];
            name = node.getAttribute("name");

            //use id if name is null
            if (!name) {
            	name = node.getAttribute("id");
            }

            if(node.tagName.toLowerCase() == "input") {
                if(node.type.toLowerCase() == "radio" || node.type.toLowerCase() == "checkbox") {
                    if(node.checked) {
                        tempString = name + "=" + node.value;
                    }
                }

                if(node.type.toLowerCase() == "text" || node.type.toLowerCase() == "hidden" || node.type.toLowerCase() == "password") {
                    tempString = name + "=" + encodeURIComponent(node.value);
                }
            }
            else if(node.tagName.toLowerCase() == "select") {
                tempString = getSelectedOptions(node);
            }

            else if(node.tagName.toLowerCase() == "textarea") {
                tempString = name + "=" + encodeURIComponent(node.value);
            }

            if(tempString != "") {
                if(qs == "") {
                    qs = tempString;
                }
                else {
                    qs = qs + "&" + tempString;
                }
            }

        }

        return qs;

    }

    /** @private */
    function getSelectedOptions(select) {
        var options = select.options;
        var option = null;
        var qs = "";
        var tempString = "";

        for(var x = 0; x < options.length; x++) {
            tempString = "";
            option = options[x];

            if(option.selected) {
                tempString = select.name + "=" + option.value;
            }

            if(tempString != "") {
                if(qs == "") {
                    qs = tempString;
                }
                else {
                    qs = qs + "&" + tempString;
                }
            }
        }

        return qs;
    }

    /** @private */
    function echoResponse(ajaxRequest) {
        var echoTextArea = document.getElementById("debugResponse");
        if(echoTextArea == null) {
            echoTextArea = createDebugTextArea("Server Response:", "debugResponse");
        }
        var debugText = ajaxRequest.getXMLHttpRequestObject().status 
            + " " + ajaxRequest.getXMLHttpRequestObject().statusText + "\n\n\n";
        echoTextArea.value = debugText + ajaxRequest.getXMLHttpRequestObject().responseText;
    }

    /** @private */
    function echoParsedJavaScript(js) {
        var echoTextArea = document.getElementById("debugParsedJavaScript");
        if(echoTextArea == null) {
            var echoTextArea = createDebugTextArea("Parsed JavaScript (by JavaScript Parser):", "debugParsedJavaScript");
        }
        echoTextArea.value = js;
    }

    /** @private */
    function createDebugTextArea(label, id) {
        echoTextArea = document.createElement("textarea");
        echoTextArea.setAttribute("id", id);
        echoTextArea.setAttribute("rows", "15");
        echoTextArea.setAttribute("style", "width:100%");
        echoTextArea.style.cssText = "width:100%";

        document.getElementsByTagName("body")[0].appendChild(document.createTextNode(label));
        document.getElementsByTagName("body")[0].appendChild(echoTextArea);
        return echoTextArea;
    }


    /** @private */
    function echoRequestParams() {
        var qsTextBox = document.getElementById("qsTextBox");
        if(qsTextBox == null) {
            qsTextBox = createDebugTextBox("Query String:", "qsTextBox");
        }
        qsTextBox.value = queryString;

        var urlTextBox = document.getElementById("urlTextBox");
        if(urlTextBox == null) {
            urlTextBox = createDebugTextBox("URL (Includes query string if GET request):", "urlTextBox");
        }
        urlTextBox.value = requestURL;
    }

    /** @private */
    function createDebugTextBox(label, id) {
        textBox = document.createElement("input");
        textBox.setAttribute("type", "text");
        textBox.setAttribute("id", id);
        textBox.setAttribute("style", "width:100%");
        textBox.style.cssText = "width:100%";

        document.getElementsByTagName("body")[0].appendChild(document.createTextNode(label));
        document.getElementsByTagName("body")[0].appendChild(textBox);
        return textBox;
    }


}

/**
    Create an instance of the XMLHttpRequest object, using the appropriate
    method for the type of browser in which this script is running. For Internet
    Explorer, it's an ActiveX object, for all others it's a native JavaScript
    object.
    @return an instance of the XMLHttpRequest object.
*/
function createXMLHttpRequest() {
    var req = false;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
       	try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
      	}
        catch(e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {
                req = false;
            }
        }
    }
    return req;
}

// JavaScript Document
var taconite_parser_version=1.502;
var isIE=document.uniqueID;
String.prototype.trim = function() {
    //skip leading and trailing whitespace
    //and return everything in between
    var x=this;
    x=x.replace(/^\s*(.*)/, "$1");
    x=x.replace(/(.*?)\s*$/, "$1");
    return x;
};

function requiresContextNode(xmlTagName) {
    return !(xmlTagName == "taconite-execute-javascript" || xmlTagName == "taconite-redirect");
}


function XhtmlToDOMParser(){
    this.parseXhtml = function(xml){
        var xmlTagName=xml.tagName.toLowerCase();
        var contextNode=document.getElementById(xml.getAttribute("contextNodeID"));
        if(contextNode == null && requiresContextNode(xmlTagName)){
            return false;
        }
        switch (xmlTagName) {
            case "taconite-append-as-children":
                getReplaceChildren(contextNode,xml,false);
                break;
            case "taconite-delete":
                getDelete(contextNode,xml);
                break;
            case "taconite-append-as-first-child":
                getAppendAsFirstChild(contextNode,xml);
                break;                         
            case "taconite-insert-after":
                getInsertAfter(contextNode,xml);
                break;
            case "taconite-insert-before":
                getInsertBefore(contextNode,xml);
                break;                         
            case "taconite-replace-children":
                getReplaceChildren(contextNode,xml,true);
                break;
            case "taconite-replace":
                getReplace(contextNode,xml);
                break;                         
            case "taconite-set-attributes":
                xml.removeAttribute("contextNodeID");
                xml.removeAttribute("parseInBrowser");
                handleAttributes(contextNode,xml);
                break;
            case "taconite-redirect":
                handleRedirect(xml);
                break;
            case "taconite-execute-javascript":
                executeJavascript(xml);
                break;
        }  
        return true;
    };
    
    function isInlineMode(node) {
        var attrType;
        if(!node.tagName.toLowerCase() == "input") {
            return false;
        }
        attrType=node.getAttribute("type");
        
        if(attrType=="radio" || attrType=="checkbox") {
            return true;
        }
        return false;
    }  
    this.getJavaScript= function() {
        return "var dummy_taconite_variable=0";
    }; 
    function handleNode(xmlNode){
        var nodeType = xmlNode.nodeType;               
        switch(nodeType) {
            case 1:  //ELEMENT_NODE
                return handleElement(xmlNode);
            case 3:  //TEXT_NODE
            case 4:  //CDATA_SECTION_NODE
                var textNode = document.createTextNode(xmlNode.nodeValue);
                if(isIE) {
                    textNode.nodeValue = textNode.nodeValue.replace(/\n/g, '\r'); 
                }
                return textNode;
        }      
        return null;
    }
    function handleElement(xmlNode){
        var domElemNode=null;
        var xmlNodeTagName=xmlNode.tagName.toLowerCase();
        if(isIE){
            if(isInlineMode(xmlNode)) {
                return document.createElement("<INPUT " + handleAttributes(domElemNode,xmlNode,true) + ">");
            }
            if(xmlNodeTagName == "style"){
                //In internet explorer, we have to use styleSheets array.		
                var text,rulesArray,styleSheetPtr;
                var regExp = /\s+/g;
                text=xmlNode.text.replace(regExp, " ");
                rulesArray=text.split("}");
                
                domElemNode=document.createElement("style");
                styleSheetPtr=document.styleSheets[document.styleSheets.length-1];
                for(var i=0;i<rulesArray.length;i++){
                    rulesArray[i]=rulesArray[i].trim();
                    var rulePart=rulesArray[i].split("{");
                    if(rulePart.length==2) {//Add only if the rule is valid
                        styleSheetPtr.addRule(rulePart[0],rulePart[1],-1);//Append at the end of stylesheet.
                    }
                }							
                return domElemNode;			
            }
            
        }
        if(domElemNode == null){
            if(useIEFormElementCreationStrategy(xmlNodeTagName)) {
                domElemNode = createFormElementsForIEStrategy(xmlNode);
            }
            else {
                domElemNode = document.createElement(xmlNodeTagName);
            }
            
            handleAttributes(domElemNode,xmlNode);
            //Fix for IE Script tag: Unexpected call to method or property access error
            //IE don't allow script tag to have child
            if(isIE && !domElemNode.canHaveChildren){
                if(xmlNode.childNodes.length > 0){
                    domElemNode.text=xmlNode.text;
                }
                
            }                              
            else{
                for(var z = 0; z < xmlNode.childNodes.length; z++) {
                    var domChildNode=handleNode(xmlNode.childNodes[z]);
                    if(domChildNode!=null) {
                        domElemNode.appendChild(domChildNode);
                    }
                }
            }
        }      
        
        return domElemNode;
    }
    
    function useIEFormElementCreationStrategy(xmlNodeTagName) {
        var useIEStrategy = false;
        
        if (isIE && ( xmlNodeTagName.toLowerCase() == "form" ||
              xmlNodeTagName.toLowerCase() == "input" ||
              xmlNodeTagName.toLowerCase() == "textarea" ||
              xmlNodeTagName.toLowerCase() == "select" ||
              xmlNodeTagName.toLowerCase() == "a" ||
              xmlNodeTagName.toLowerCase() == "applet" ||
              xmlNodeTagName.toLowerCase() == "button" ||
              xmlNodeTagName.toLowerCase() == "img" ||
              xmlNodeTagName.toLowerCase() == "link" ||
              xmlNodeTagName.toLowerCase() == "map" ||
              xmlNodeTagName.toLowerCase() == "object")) {
                      
            useIEStrategy = true;
        }
        
        return useIEStrategy;
    }
    
    function createFormElementsForIEStrategy(xmlNode) {
        var attr = null;
        var name = "";
        var value = "";
        for (var x = 0; x < xmlNode.attributes.length; x++) {
            attr = xmlNode.attributes[x];
            name = attr.name.trim();
            if (name == "name") {
                value = attr.value.trim();
            }
        }

        domElemNode = document.createElement("<" + xmlNode.tagName + " name='" + value + "' />"); // e.g. document.createElement("<input name='slot2'>");
        
        return domElemNode;
    }
    
    function handleAttributes(domNode, xmlNode) {
        var attr = null;
        var attrString = "";
        var name = "";
        var value = "";
        var returnAsText = false;
        if(arguments.length == 3) {
            returnAsText = true;
        }
        
        for(var x = 0; x < xmlNode.attributes.length; x++) {
            attr = xmlNode.attributes[x];
            name = cleanAttributeName(attr.name.trim());
            value = attr.value.trim().replace("&#38;", "&"); // added replace() to fix the safari bug that turns & in to numeric entity - gg
            if(!returnAsText){
                if(name == "style") {
                    /* IE workaround */
                    domNode.style.cssText = value;
                    /* Standards compliant */
                    domNode.setAttribute(name, value);
                }
                else if(name.trim().toLowerCase().substring(0, 2) == "on") {
                    /* IE workaround for event handlers */
                    //domNode.setAttribute(name,value);
                    eval("domNode." + name.trim().toLowerCase() + "=function(){" + value + "}");
                }
                else if(name == "value") {
                    /* IE workaround for the value attribute -- makes form elements selectable/editable */
                    domNode.value = value;
                }
                else if(useIEFormElementCreationStrategy(xmlNode.tagName) && name == "name") {
                    //Do nothing, as the "name" attribute was handled in the createFormElementsForIEStrategy function
                    continue;
                }
                else {
                    /* Standards compliant */
                    domNode.setAttribute(name,value);
                }
                /* class attribute workaround for IE */
                if(name == "class") {
                    domNode.setAttribute("className",value);
                }
            }else{
                attrString = attrString + name + "=\"" + value + "\" " ;
            }
        }
        return attrString;
    }
    function getAppendAsFirstChild(domNode,xml){
        var firstNode=null;
        if(domNode.childNodes.length > 0) {
            firstNode=domNode.childNodes[0];
        }
        
        for(var i=0;i<xml.childNodes.length;i++){
            domChildNode=handleNode(xml.childNodes[i]);
            if(domChildNode!=null){
                if(firstNode==null){
                    domNode.appendChild(domChildNode);
                    firstNode=domChildNode;
                }
                else {
                    domNode.insertBefore(domChildNode,firstNode);
                }
                
            }
        }              
    }
    
    function getInsertAfter(domNode,xml){
        var domChildNode=null;
        var nextSibling=domNode.nextSibling;
        for(var i=0;i<xml.childNodes.length;i++){
            domChildNode=handleNode(xml.childNodes[i]);
            if(domChildNode!=null){
                if(nextSibling!=null) {
                    domNode.parentNode.insertBefore(domChildNode,nextSibling);
                }
                else {
                    domNode.parentNode.appendChild(domChildNode);
                }
            }
        }              
    }
    function getInsertBefore(domNode,xml){
        var domChildNode=null;
        for(var i=0;i<xml.childNodes.length;i++){
            domChildNode=handleNode(xml.childNodes[i]);
            if(domChildNode!=null) {
                domNode.parentNode.insertBefore(domChildNode,domNode);
            }
        }              
    }      
    function getReplace(domNode,xml){
        getInsertAfter(domNode,xml);
        domNode.parentNode.removeChild(domNode);
    }
    function getDelete(domNode) {
        domNode.parentNode.removeChild(domNode);
    }
    function getReplaceChildren(domNode,xml,doRemoveChildren) {
        var domChildNode=null;
        if(doRemoveChildren){
            while(domNode.childNodes.length >0){
                domNode.removeChild(domNode.childNodes[0]);
            }      
        }
        for(var i=0;i<xml.childNodes.length;i++){
            domChildNode=handleNode(xml.childNodes[i]);
            if(domChildNode!=null) {
                domNode.appendChild(domChildNode);
            }
        }              
    }
    
    function handleRedirect(xmlNode) {
        var targetUrl = xmlNode.getAttribute("targetUrl");
        window.location.replace(targetUrl);
    }
    
    function executeJavascript(xmlNode) {
        var scripts = xmlNode.getElementsByTagName("script");
        for (var i = 0; i < scripts.length; i++) {
            var script = scripts[i];
            if (script.getAttribute("type") == "text/javascript") {
                var js = script.firstChild.nodeValue;
                eval(js);
            }
        }
    }

    function cleanAttributeName(name) {
        if(isIE == false) {
            return name;  // added "return name;" - previously only worked when isIE was undefined.  MR
        }
        
        // IE workaround to change cellspacing to cellSpacing, etc
        var cleanName = name.toLowerCase();
        if(cleanName == "cellspacing") {
            cleanName = "cellSpacing";
        }
        else if(cleanName == "cellpadding") {
            cleanName = "cellPadding";
        }
        else if(cleanName == "colspan") {
            cleanName = "colSpan";
        }
        else if(cleanName == "tabindex") {
            cleanName = "tabIndex";
        }
        else if(cleanName == "readonly") {
            cleanName = "readOnly";
        }
        return cleanName;
    }
    
}


/***** AdPlacer ******/
var adCall = new Object();
adCall.zone = '';
adCall.zoneOverride = '';  // so Adops can manually set a RequestParamBucket.zoneOverride in adsetup
adCall.type = 'adj';

adCall.pageVals ='';  //this is picked up by reporting

adCall.adPageVals ='';
adCall.additionalValues = '';
adCall.dcopt = 'on';
adCall.tile = 1;
adCall.pos = 'unset';
adCall.mtype = ';mtype=standard;';
adCall.tag = ';tag=' + adCall.type;
adCall.uVals = '';
adCall.ord = '';

adCall.tempPageVals = '';
adCall.tempAdditVals = '';
adCall.begin = '';



adCall.ord = Math.round( Math.random() * 1000000000000000 );

adCall.makeAdVals = function() {

	/*	Note:'adCall.additionalValues' is also being used for reporting calls	*
	*	so any modifications must be in a local variable 	*
	*	unless you intend to modify the reporting values too	*/

	adCall.tempAdPageVals = adCall.adPageVals;
	adCall.tempAdditVals = adCall.additionalValues;



	/* ma08 override */
	adCall.tempAdPageVals = adCall.tempAdPageVals.replace(/movieawards/, 'ma');


	//remove .jhtml from pagename
	adCall.tempAdPageVals = adCall.tempAdPageVals.replace(/pagename=(.*)\..*;/i, 'pagename=$1');
	adCall.tempAdPageVals = adCall.tempAdPageVals.replace(/=onair;|=ontv;/i, '=shows;');

	//create DART Zone here.
	if (adCall.zone.substring(0,3) != 'od/' && adCall.zone.substring(0,3) != 'mv/') {

		if (adCall.zoneOverride == '') {
			var z = adCall.tempAdPageVals.replace(/pagename=/i, '');	//remove pagename=, if there
			z = z.replace(/pg=/i, '');								//remove pg=, if there
			z = z.replace(/index/, '');
			z = z.replace(/section_[0-9]=/g, '');					//remove all section_N, if there
			z = z.replace(/sec[0-9]=/g, '');						//remove all secN, if there
			z = z.replace(/;/g, '/');
			z = z.replace(/onair|ontv/, 'shows');

			if (adCall.tempAdditVals.indexOf("Gracenote") != -1)
				z = 'gn/' + z;

			adCall.zone = z;
		}
		else
			adCall.zone = adCall.zoneOverride;
	}

	if ( adCall.zone.charAt(adCall.zone.length-1) == '/' ) {
		adCall.zone = adCall.zone.slice(0,-1);
	}

	adCall.zone += ';';

	/* ma hack */
	adCall.zone = adCall.zone.replace(/arcade\/game\/the_2008_mtv_movie_awards_pool\/play/, 'ma\/2008');


	//will affect reporting
	adCall.additionalValues = adCall.additionalValues.split('&#8212;').join('_');   //replace long dashes with _

	adCall.tempAdditVals =  encodeURI(adCall.tempAdditVals);
	adCall.tempAdditVals = adCall.tempAdditVals.replace(/\'/g, '');					//remove all single quotes
	adCall.tempAdditVals = adCall.tempAdditVals.replace(/#/g, '');					//remove all pound signs

	adCall.tempAdPageVals = adCall.tempAdPageVals.replace(/\'/g, '');					//remove all single quotes
	adCall.tempAdPageVals = adCall.tempAdPageVals.replace(/#/g, '');					//remove all pound signs



	/* ma08 overrides */
	adCall.zone = adCall.zone.replace(/arcade\/game\/the_2008_mtv_movie_awards_pool\/play/, 'ma\/2008');

	if ( adCall.tempAdditVals.indexOf("gamename=the_2008_mtv_movie_awards_pool") != -1 ) {
		adCall.tempAdPageVals =  adCall.tempAdPageVals.replace(/section_1=arcade/, 'section_1=ma');
		adCall.tempAdPageVals =  adCall.tempAdPageVals.replace(/section_2=game/, 'section_2=2008');
	}

	adCall.beg = '<scr' + 'ipt language="JavaScript1.1" src="http://ad.doubleclick.net/' + adCall.type + '/mtv.mtvi/';

}

adCall.writeIframeSrc = function(url) {
	if (url) {
		var newUrl = url.replace(/\{pageValsParam\}/, adCall.adPageVals);
		newUrl = newUrl.replace(/\{ord\}/, adCall.ord);
		newUrl = newUrl.replace(/\{.*\}/, ''); 		// remove any remaining bracketed params

		var call = '<ifr' + 'ame src="' + newUrl + '?" width="1" height="1"><\/ifr'+'ame>';

		document.write(call);

	}
}

/* most ads are written out with this function */
adCall.writeCall = function(size) {
	if (size) {
		var call = '';
		var dcoptStr = '';

		if (adCall.dcopt == 'on') dcoptStr = ';dcopt=ist;';

		/* get final zone string */
		var zonePlus = '';
		if (adCall.zone.substring(0,3) == 'od/' )
			//per adops, the 'v' is for video
			zonePlus = getPos() + '_' + getTagType()  + '_v/' + adCall.zone;
		else
			//per adops, the 's' is for standard in-page ad
			zonePlus = getPos() + '_' + getTagType() + '_s/' + adCall.zone;


		/* create u-vals */
		//adCall.tempAdditVals.replace(/!.*;/, '');		// regex to remove exclusion cats

		adCall.uVals = adCall.tempAdPageVals + adCall.tag +  adCall.mtype + dcoptStr  + adCall.tempAdditVals + 'sz-' + size + '|tile-' + adCall.tile + '|';

		adCall.uVals = adCall.uVals.replace(/;+/g, '|');		// change ; to |

		adCall.uVals = ';u=|' + adCall.uVals.replace(/=/g, '-');	//change = to -

		call = adCall.beg + zonePlus + adCall.tempAdPageVals + ';pos=' + getPos() + adCall.tag + adCall.mtype + dcoptStr + adCall.tempAdditVals +  ';sz=' + size + ';' + getTileStr() + adCall.uVals + ';ord=' + adCall.ord + '?"><\/scr'+'ipt>'

		call = call.replace(/;+/g, ';' ); //lose extra semi-colons

		//dcopt is on for only the first ad call of page
		if (adCall.dcopt == 'on') adCall.dcopt = 'off';

		document.write(call);
	}
}

getPos = function() {
	var ps = '';
	if (adCall.tile < 3 ) ps = 'atf';  //above the fold
	else ps = 'btf';					//below the fold
	return(ps);
}

getTagType = function() {
	var tt = '';
	if (adCall.type == 'adj') tt = 'j';
	else if  (adCall.type == 'ad') tt= 'a';
	else if  (adCall.type == 'adi') tt= 'i';
	else if  (adCall.type == 'pfadx') tt= 'p';
	else if  (adCall.type == 'adx') tt= 'x';
	return(tt);
}


getUpsellStr = function(sz) {
	var us = '';
	if (sz == '728x90') us = '/sitewide/images/shadow/temp-upsell.gif';
	else if (sz == '300x250') us = '/sitewide/images/shadow/temp-upsell-ad.jpg';
	return(us);
}

getTileStr = function() {
	var ts = '';
	ts = "tile=" + adCall.tile;
	adCall.tile++;
	return(ts);
}




