function standardTrim(value) {
// original trim function, does not work on Safari
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = /  /g;
   while (temp.match(obj)) { temp = temp.replace(obj, " "); }
   return temp;
}

function LTrim(str)
// part of Safari trim
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)
// part of Safari trim
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

function safariTrim(str)
// simpler trim needed because Safari is buggy
{
   return RTrim(LTrim(str));
}

function trim(value) {
  var agt=navigator.userAgent.toLowerCase();
  if(agt.indexOf("safari")!=-1)
    return safariTrim(value)
  else
    return standardTrim(value);
}

function NoFraming() {
  if (top.frames.length != 0) {
    top.location = location.href
  }
}

function getCookie1(Name) {   
  var search = Name + "=";
	if (document.cookie.length > 0) { //if there are any cookies      
    offset = document.cookie.indexOf(search);     
    if (offset != -1) { // if cookie exists          
      offset += search.length;    //set index of beginning of value         
      end = document.cookie.indexOf(";", offset);  // set index of end of cookie value         
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(offset,end))
    }
  }
}

var mname=getCookie1('UName');
var varietypopad = getCookie1('varietypopad');

/* this doesn't do anything, so I am commenting it out ART 4/19/04
//if logged in or if the popup ad has been seen, show eyeblaster 
if ( (mname!=null && mname!='') || (varietypopad!=null && varietypopad!='') ) {
  //document.write('<SCR'+'IPT language="JavaScript" SRC="http://ad.doubleclick.net/adj/variety.dart/eyeblaster_oceans;sz=1x1;tile=0;ord=55?">');
  //document.write('<' + '/SCRIPT>');
} else //openPopAd();
*/

function openPopAd() {
  var arg = "varietypopad=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  var cookiethere = "no";
  var expdate = new Date();
  expdate.setTime (expdate.getTime() + (1 * 60 * 60 * 1000));
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i,j) == arg) {
      cookiethere = "yes";
      //alert("There is a cookie")
      break;
    }
    i = document.cookie.indexOf(" ",i) + 1;
    if (i==0) break;
  }
  if (cookiethere == "no") {
    document.cookie = "varietypopad=been_there;expires=" + expdate.toGMTString()
    // the following should be uncommented if you want the cookie to expire after the session and not after 60 minutes
    //document.cookie = "varietypopad=been_there;"
    popupWin =window.open('http://www.variety.com/index.asp?layout=front_page_popup','remote','width=200,height=300,left=50,top=250,resizable=0')
  }
}

//this is from module welcome
function go_login(layout) {
  m_layout=layout;
  m_page=window.location.href;
  //-- AH 11/18/03 - added site_url
  if (m_layout=='login_link') m_page=site_url+'index.asp?layout=front_page';
  m_page = "&returnURL=" + escape(m_page)
  //-- AH 11/18/03 - added site_url
  window.location.href = site_url+"subscribe.asp?screen=li1"+m_page;
}

//this is from v_cookiecrumbs module
function setCookie(name, value, expire) {   
  document.cookie = name + "=" + escape(value)   + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}

function getCookie(Name) {   
  return getCookie1(Name);
}

// from module v_Stock_top_losers_late
function ConvertDow( strName ) {
  if (strName == "DJ US M") document.write("MEDIA");
  else {
	  if (strName == "DOW JON") document.write("DJIA");
	  else document.write(strName);
  }
}

//from module right_boxoffice_weekend
function decimal(weekbox) {
  var number = weekbox;
  if (number.indexOf('.')==-1) number = number + '.0';
  else number=number;
  document.write('$' + number);
}

//21 modules have this function
function dateBuster(date) {
  var stock_date=date
  m_month=stock_date.substring(0,stock_date.indexOf('/'));
  m_day=stock_date.substring(stock_date.indexOf('/')+1, stock_date.indexOf('/',stock_date.indexOf('/')+1));
  m_year=stock_date.substring(stock_date.indexOf('/',stock_date.indexOf('/')+1)+1);
  var bits = m_year.split(' ');
  m_year = bits[0];
  //strips time formatting of leading zeros
  m_time = m_year.substring(3,8)
  if (m_time.charAt(0)==0) m_time = m_time.substring(m_time.indexOf(m_time.charAt(1)),5);
  //strips time formatting from year
  if (m_year.length > 2) m_year = m_year.substring(2,4);
  else m_year = m_year.substring(0,2);
  //strips out leading zeros
  if (m_day.charAt(0)==0) m_day = m_day.charAt(1);
  //strips out leading zeros
  if (m_month.charAt(0)==0) m_month = m_month.charAt(1);
  document.write(m_month + '/' + m_day + '/' + m_year)
}

//modifies time by taking of the leading 0 in hh input: 03:25 pm output: 3:25pm 
function timeBuster(time) {
  var th = time.split(':')
  var t;
  if (th[0].charAt(0)==0) t = th[0].charAt(1);
  else t = th[0];
  t = t + ':' + th[1].replace(' ','');
  document.write(' '+t);
}

//from layout advanced_result
function writeError(section,stories) {
	var layout = "advanced_result"
	if (layout!="advanced_result") {section="";}
	document.write("<a name="+ section +" class=contentSubhed><b>" + section + "</b></a><br><span class=content>Sorry, your search for " + stories);
	if (userQuery != "" && author != "") {
		document.write(" by <b>"+ author +"</b> containing <b>" + userQuery );
	} else if (userQuery == "" && author != "") {
		document.write(" by  <b>"+ author);
	} else if (userQuery != "" && author == "") {
		document.write(" containing <b>" + userQuery);
	}	
	document.write("</b> returned 0 matches<br>Please <a href=javascript:history.go(-1)>go back</a> to refine your search.<br>Or see ");	
	function openWindow(){
	//-- AH 11/18/03 - added site_url
	sameWindow = window.open(site_url+'index.asp?layout=search_help','help',',scrollbars,status,menubar,resizable,width=400,height=300');
	sameWindow.focus();	
	}
  //-- AH 11/18/03 - added site_url
	document.write('<a href='+site_url+'index.asp?layout=search_help onClick="openWindow();return false;" class=contentSmall>Search Help Here.</a><br>&nbsp;<br></span>');
}


//from module v_dept_head
function openPopTVAd() {
  var arg = "varietypopad=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  var cookiethere = "no";
  var expdate = new Date();
  var dept = "%dept%";

  expdate.setTime (expdate.getTime() + (1 * 60 * 60 * 1000));

  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i,j) == arg) {
      cookiethere = "yes";
      break;
    }
    i = document.cookie.indexOf(" ",i) + 1;
    if (i==0) break;
  }
		
  if (cookiethere == "no") {
    if (dept == "TV") {
      document.cookie = "varietypopad=been_there; expires=" + expdate.toGMTString()
		//-- AH 11/18/03 - added site_url
      popupWin = window.open(site_url + 'index.asp?layout=popup','remote','width=220,height=320,left=50,top=250,resizable=0');
    }
  }
}

function stripQts2(name) {
  var newName = "";
  var test = name.split(unescape("%27%27")); //split the field on ascii code for double ''
  for (i=0;i<test.length;i++) {
    if (i != (test.length - 1) ) newName = newName + test[i] + "'";  // put it back together with an "'"
    else newName = newName + test[i]; // make sure end of string has no "'"
  }
  userQuery = newName;
}

function ConvertDow(strName) {
  if (strName == "DJ US M") document.write("MEDIA");
  else {
	  if (strName == "DOW JON") document.write("DJIA");
	  else document.write(strName);
  }
}

function decimal(weekbox) {
  var number = weekbox;
  if (number.indexOf('.')==-1) number = number + '.0';
	else number=number;
	document.write('$' + number);
}

function dateDisplay(m){
	var dtm = ""+new Date()+"";
	var d = new Date(m);
	var dts = d.toLocaleString(d.toGMTString());
	var dtz = dtm.split(' ');
	dtm = dts.split(' ');
	if (dtm[2].substr(0,1) == 0) dtm[2] = dtm[2].substr(1,2);
	dts = dtm[4].split(':');
	document.write(dtm[1].substr(0,3) + ". " + dtm[2] + " " + dtm[3] + ", " + dts[0] + ":" + dts[1] + "" + dtm[5].toLowerCase() + " " + dtz[4]);
	
}

//for review trailers
function openPlayerWindow(filename){
	var newWindow;
	newWindow = window.open("http:/" + "/www.variety.com/media/player.asp?filename=" + filename,"player","width=468,height=450,left=50,top=10");
}

function MakeCookieCrumb(layout,dept,type,charttype,deck) {
  var homeLink = "Home";
  if (navigator.userAgent.indexOf("Mac") == -1)
    if (document.all)
      homeLink = "Make Variety.com your <a HREF onClick=" + 'this.style.behavior=\'url(#default#homepage)\';this.setHomePage(\'http://www.variety.com\');' +" style='cursor:hand;text-decoration:underline;' class='breadcrumb'>Home Page</a>";

	
  var m_layout = layout;
  var m_dept = dept;
  var m_type = type;
  var m_charttype = charttype;
  var m_deck = deck;

	m_layout=m_layout.toLowerCase();
	var flag = 0;
	//-- get display value of breadcrumb from the title
	var mtitle=document.title;
	var offset=mtitle.indexOf('Variety.com - ');
	var x=14;
	if (offset==-1) {offset=mtitle.indexOf('Variety.com -');x=13;}
	if (offset==-1) {offset=mtitle.indexOf('Variety.com-');x=12;}
	if (offset==-1) {offset=mtitle.indexOf('Variety.com');x=11;}
	if (offset!=-1) {mtitle=mtitle.substr(offset+x);}
	if (m_layout=='front_page' || m_layout=='' || m_layout=='null') {
		//-- reset cookies if on the home page
		var mCrumb='&nbsp;' + homeLink;
		setCookie('bread1','');
		setCookie('bread2','');
		setCookie('bread3','');
		flag=1;
	}
	else { 
		//-- AH 11/18/03 - added site_url
		var mCrumb='&nbsp;<a href='+site_url+'index.asp?layout=front_page class=breadcrumb>Home</a>';
		var mlink='<a href='+window.location.href+' class=breadcrumb>'+mtitle+'</a>';
	}
	
	//first level, for most layouts, reset to bread1 and clear the rest cookies. new additions to layout arrays must be lowercase
	var m_layout2 = new Array('news_front','columns_front','reviews_front','reviews_layout','people_home','vpage','animation2000','about_special_c','advanced_search','charts_layout','credits_reviews','calendar','weblinks_front','ticker','handheld_access','slanguage','la411_layout','classified_layout','hot_topics_main','google_results','chart_tv_prod','ticker','afm','b_o_weekend','b_o_weekend_all','b_o_layout','b_o_chart_all','b_o_foreign','b_o_foreign_all','v_eletter_jump','big_talk_discussion','ev_billing_step2','ev_billing_3','ev_billing_step1','ev_billing_2','variety_gen_f');
	for (i=0;i<m_layout2.length;i++) {
		if (m_layout==m_layout2[i]) {
			setCookie('bread1',mlink);
			setCookie('bread2','');
			setCookie('bread3','');
			mCrumb = mCrumb+'&nbsp;&gt;&nbsp;' + mtitle;
			flag = 1;
		}
	}

	if (flag==0) {
		var crumb1=getCookie('bread1')
		if (crumb1!="") {
			//second level, for topnav layouts and other must be second level layouts, reset to bread2 and clear lowerlevel cookies
			var m_layout3 = new Array('news_layout','columns_category','film_categories','vpage_category','calendar_result_1','calendar_result_2','calendar_result_3','adv_search_result','reviews_layout','dept_beyond_variety','variety_gen_f');
			for (i=0;i<m_layout3.length;i++) {
				if (m_layout==m_layout3[i]) {
					if (m_layout3[i] == 'adv_search_result') {mtitle = 'More Search Results';}
					setCookie('bread2',mlink);
					setCookie('bread3','');
					//mCrumb=mCrumb+'&nbsp;&gt;&nbsp;' + crumb1 + '&nbsp;&gt;&nbsp;' + mtitle;
					mCrumb=mCrumb+'&nbsp;&gt;&nbsp;' + crumb1 + '&nbsp;&gt;&nbsp;' + mtitle;
					flag = 1;
				}
			}
		}
	}

	if (m_layout == 'ticker_quote') {
		setCookie('bread1',mlink);
		setCookie('bread2','');
		setCookie('bread3','');
		mCrumb=mCrumb+'&nbsp;&gt;&nbsp;'+ mtitle;
		flag = 1;
	}


	if (m_layout == 'dept_main') {
		var m_dept2 = new Array('Film','TV','World','Business','Music','Legit','More');
		for (j=0;j<m_dept2.length;j++) {
			if (m_dept==m_dept2[j]) {
				//-- AH 11/18/03 - added site_url
				var crumb1link='<a class=breadcrumb href='+site_url+'index.asp?layout=dept_main&dept=' + m_dept2[j] +'>' + mtitle+ '</a>';
				setCookie('bread1',crumb1link);
				setCookie('bread2','');
				setCookie('bread3','');
				if (mtitle != '') {mCrumb=mCrumb+'&nbsp;&gt;&nbsp;'+mtitle;}

				flag = 1;
			}
		}

	}

	if (m_layout == 'reports') {
		var m_type2 = new Array('AWARDS','FESTIVALS','MARKETS','MORE');
		var mtitle3 = new Array('Awards','Festivals','Markets','Special Reports');

		for (k=0;k<mtitle3.length;k++) {
			if (m_type == m_type2[k]) {
				//-- AH 11/18/03 - added site_url
				var crumb1link = '<a class=breadcrumb href='+site_url+'index.asp?layout=reports&type=' + m_type2[k] + '>' + mtitle3[k] + '</a>';
				setCookie('bread1',crumb1link);
				setCookie('bread2','');
				setCookie('bread3','');
				mCrumb = mCrumb+'&nbsp;&gt;&nbsp;'+mtitle3[k];
				flag = 1;
			}
		}
	}

	if (m_layout == 'chart_pass') {
		var m_charttype2 = new Array('prodcharts','hwoodfilmfest');
		var mtitle4 = new Array('Production Charts','5th Annual Hollywood Film Festival and Hollywood Movie Awards','');
		for (k=0;k<mtitle4.length;k++) {
			if (m_charttype == m_charttype2[k]) {
				//-- AH 11/18/03 - added site_url
				var crumb1link = '<a class=breadcrumb href='+site_url+'index.asp?layout=reports&type=' + m_charttype2[k] + '>' + mtitle4[k] + '</a>';
				setCookie('bread1',crumb1link);
				setCookie('bread2','');
				setCookie('bread3','');
				mCrumb=mCrumb+'&nbsp;&gt;&nbsp;'+mtitle4[k];
				flag = 1;
			}
		}
	}

	if (flag==0) {
		if (m_layout == 'weblinks') {
			var crumb1=getCookie('bread1')
			var name = m_deck
			var nameSplit = name.split(unescape('+'));
			var newName="";
			for (i=0;i<nameSplit.length;i++) {
				if (i!=(nameSplit.length - 1)) {newName = newName + nameSplit[i] + " "} // put it back together with an " " 
				else {newName = newName + nameSplit[i]} // make sure end of string has no " "
			}
			var mtitle='Web Links:' + ' ' + newName;
			setCookie('bread2',mlink);
			setCookie('bread3','');
			mCrumb=mCrumb+ '&nbsp;&gt;&nbsp;' + crumb1 + '&nbsp;&gt;&nbsp;' + mtitle;
			flag = 1;
		}
	}

	if (flag == 0 || m_layout=='null') {
		var mstop=0;
		var crumb1=getCookie('bread1');
		if ( (crumb1!=null) && (crumb1!='') ) {
			if (crumb1==mlink || mtitle==document.title) { //goes back
				mstop=1;
				setCookie('bread2','');
				setCookie('bread3','');
			}
			else {mCrumb=mCrumb+'&nbsp;&gt;&nbsp;'+crumb1;}
		}
 
 		else {
			if ((m_layout!='login_page')&&(m_layout!='logout')&&(m_layout!='login_link')&&(m_layout!='online_login')&&(m_layout!='email_login')&&(m_layout!='print_login')&&(m_layout!='slideshow')) {setCookie('bread1',mlink);mstop=1;}
		}

		if (mstop==0) {
			var crumb2=getCookie('bread2');
			if ( (crumb2!=null) && (crumb2!='') ) { //goes back
				if (crumb2==mlink || document.title=='Variety.com - Search Results') { //goes back
					mstop=1;
					setCookie('bread3','');
				}
				else {mCrumb=mCrumb+'&nbsp;&gt;&nbsp;'+crumb2;}
			} 

			else {
				if ((m_layout!='login_page')&&(m_layout!='logout')&&(m_layout!='slideshow')) {setCookie('bread2',mlink);mstop=1;}
			}

			var crumb3=getCookie('bread3');
			if ( (crumb3!=null) && (crumb3!='') ) { //goes back
				if (crumb3==mlink) {mstop=1;} //goes back
				else {mCrumb=mCrumb+'&nbsp;&gt;&nbsp;'+crumb3;}
			} 
			else {
				if ((m_layout!='login_page')&&(m_layout!='logout')&&(m_layout!='story')&&(m_layout!='review')&&(m_layout!='slideshow')) {setCookie('bread3',mlink);}
			}
		}
		
		if (mtitle != '') {mCrumb=mCrumb+'&nbsp;&gt;&nbsp;'+mtitle;}
	}

	document.write(mCrumb);
}

