// Global Functions

// set the servername for apps
var secure_servername;
var this_server;
this_server = document.domain;
if ( this_server == "huntingthelightgiveaway.com" ) {
	this_server = "www.huntingthelightgiveaway.com";
}
secure_servername = "https://" + this_server;

// global window popping script: for within site and cross-domain
function winPop(url,width,height)
{
	window.open(
	url,
	"HBOwinpop",
	"screenX=10,screenY=10,left=10,top=10,toolbar=no,location=no,resizable=yes,scrollbars=yes,status=yes,height=" + height + ",width=" + width);
}



// use only for the privacy policy
function WinOpen(height,width,url)
{
	window.open(
	url,
	"RegHelp",
	"toolbar=no,location=no,resizable=yes,scrollbars=yes,status=no,height="+height+",width="+width);
}

// internal flash popping
var extWin;
function openFlashWin(url,w,h){
	var x = (screen.width - w)/2;
	var y = (screen.height - h)/3;
	if(typeof(extWin)=="undefined" || extWin.closed==true){
		extWin = window.open(url,"external","width="+w+",height="+h+",left="+x+",top="+y+",toolbar=no,location=no,menubar=no,status=yes,scrollbars=no,resizable=no");
		if(extWin.opener == null) extWin.opener = window;
		extWin.w = w;
		extWin.h = h;
	} else {
		if(w != extWin.w || h != extWin.h){
			extWin.resizeTo(w,h);
			extWin.moveTo(x,y);
			extWin.w = w;
			extWin.h = h;
		}
		if(extWin.location != url) extWin.location = url;
		extWin.focus();
	}
}

// window redirect
function goTo(link) {
	window.location.href = link;
}

// Newsletter popup
function newsPopup ()
{
	resultWindow = "/newsletter/processing.html";
	window.open(resultWindow,'popup','width=360,height=400,toolbar=no,location=no,resizable=no,scrollbars=no,status=yes,left=10,top=10,screenX=10,screenY=10');
}

// Chat popup
function chatPopup ()
{
	resultWindow = "http://www.hbo.com/corpinfo/processing_login.html";
	window.open(resultWindow,'chat','width=625,height=500,toolbar=no,location=no,resizable=no,scrollbars=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
}

// this is the new login popup. you can use this to either pop the login or registration
function loginPopup (URL)
{
	window.open(URL,'login','width=625,height=500,toolbar=no,location=no,resizable=no,scrollbars=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
}

// game login popup
// DO NOT USE ANYMORE -- see otherLoginPop below
function gamePopup (URL)
{
	window.open(URL,'game','width=800,height=570,toolbar=no,location=no,resizable=no,scrollbars=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
}

// this is the boxing event login popup
function boxingPopup (URL)
{
	window.open(URL,'game','width=600,height=475,toolbar=no,location=no,resizable=no,scrollbars=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
}

// pop Poll window
function popPoll() {
	pollWindow = window.open("","PollResults","width=500,height=500,RESIZABLE=NO,SCROLLBARS=NO");
}

// This pops the Contest window and is called by all Contests
function popContest(contest_id,ro,ows,tws,aid,bgcolor,textcolor,header) {
	var contestURL = secure_servername + '/apps/contest/actions/EnterContest?CONTEST_ID=' + contest_id + '&ro=' + ro + '&ows=' + ows + '&tws=' + tws + '&activity_type_id=' + aid + '&bgcolor=' + bgcolor + '&textcolor=' + textcolor + '&header=' + header;
	//alert( contestURL );
	contestWindow = window.open ("http://www.hbo.com/newsletter/processing.html","ContestResults","width=625,height=500,resizable=yes,scrollbars=yes,toolbar=no,location=no,status=yes,left=10,top=10,screenX=10,screenY=10");
	contestWindow.focus();
	contestWindow.location.href = contestURL;
}

// This pops the slideshow window for legacy slideshows
function popSlideshow(slideshowURL) {
	slideshowWindow = window.open(slideshowURL,'slideshow','width=620,height=550,toolbar=no,location=no,resizable=no,scrollbars=no,status=yes,left=10,top=10,screenX=10,screenY=10');
	slideshowWindow.focus();
}

// This pops the slideshow window for celeb slideshows
function launchSlideshow(slideshowURL) {
	slideshowWindow = window.open(slideshowURL,'slideshow','width=525,height=550,toolbar=no,location=no,resizable=yes,scrollbars=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
}


// This tests url for send-a-friend-a-slide params and pops celeb slideshow window
function loadSlideShow() {
slideshow = queryString('slideshow');
myslide = queryString('myslide');
if (slideshow != "")
{
var target = '/slideshows/'+ slideshow +'/index.html?myslide='+myslide;
celebslideshow = window.open(target,'slideview','width=525, height=550,scrollbars=yes,location=no,resizable=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
}
}

// This tests url for videoplayer links from 3rd party sites
function checkVideo() {
aggregator = queryString('aggregator');
if (aggregator != "")
{
movie = queryString('movie');
section = queryString('section');
num = queryString('num');
title = queryString('title');
tunein = queryString('tunein');
var link = 'http://www.hbo.com/scripts/video/vidplayer.html?movie=' + movie + '+section=' + section + '+num=' + num + '+title=' + title + '+tunein=' + tunein;
player = window.open(link,'vplay','width=700,height=515,status=yes');
}
}


// every window on HBO.com should be named MAIN 
window.name = "MAIN";

// this cookie function is also used with the SURVEY scripts below...
function getCookie (name) {
var dcookie = document.cookie;
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
                if (dcookie.substring(cbegin, vbegin) == cname) {
                var vend = dcookie.indexOf (";", vbegin);
                        if (vend == -1) vend = clen;
                return unescape(dcookie.substring(vbegin, vend));
                }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
                if (cbegin == 0) break;
        }
return null;
}

function checklogon (name) {
	var hbo_username1 = getCookie("logged_in_user");
	var hbo_username2 = getCookie("user_name");
	var hbo_username = null;

	if (hbo_username2 != null) {
		var hbo_username = hbo_username2;
		// alert( "hello 2" );
	}
	if (hbo_username1 != null) { 
		var hbo_username = hbo_username1; 
		// alert( "hello 1" );
	}
	
	if (hbo_username == "") { 
		var hbo_username = null; 
		// alert( "hello 1" );
	}
			
	// alert( "hbo_username:"+hbo_username+":" );
	return hbo_username;
}

var hbo_username = checklogon("logged_in_user");

// these functions are used in the global nav ONLY

function registerPop()
{
	window.open(secure_servername + '/apps/login/UserRegistration.do?ro=103245&ows=103271&tws=103271&activity_type_id=AIMQRYD2&survey_id=XTRAND2&pFlow=1','login','width=625,height=500,toolbar=no,location=no,resizable=no,scrollbars=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
}

function signinPop()
{
	window.open(secure_servername + '/apps/login/secure/LogMeIn.do?ro=103268&ows=103270&tws=103270&activity_type_id=AIMQRYD2&survey_id=XTRAND2&pFlow=2','login','width=625,height=500,toolbar=no,location=no,resizable=no,scrollbars=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
}

function profilePop()
{
	window.open(secure_servername + '/apps/login/secure/ProfileAtAGlance.do?ro=103258&ows=103270&tws=103270&survey_id=XTRAND2&activity_type_id=AIMQRYD2&pFlow=2','login','width=625,height=500,toolbar=no,location=no,resizable=no,scrollbars=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
}

// use this for any of the games that have their own tracking with websources
// place the link starting at slash so that the proper server name can be added on to the beginning

function otherLoginPop(uri,width,height)
{
var finalURL = secure_servername + uri;
window.open(finalURL,'login','width=' + width + ',height=' + height + ',toolbar=no,location=no,resizable=no,scrollbars=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
}

// these functions are for the login

function writeCookie (cookieName, cookieValue, expiry)
{
	var expDate = new Date();

	expDate.setTime (expDate.getTime() + expiry);
	document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString() + "; path=/";
}

function deleteCookie (name) {
	window.open(secure_servername + '/apps/login/Logout','login','width=625,height=500,toolbar=no,location=no,resizable=no,scrollbars=yes,status=yes,left=10,top=10,screenX=10,screenY=10');
	var expireNow = new Date();
	document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
	window.location.reload();
}


//SURVEY POPUP
function insertImg(topCoord,leftCoord,imgVersion) {

document.getElementById('survey').style.left=leftCoord;

document.getElementById('survey').style.top=topCoord;
document.getElementById('survey').innerHTML=imgVersion;


}



function hideImg() {

document.getElementById('survey').style.display='none';

}



function popSurvey(URL){

window.open(URL,"HBO","screenX=10,screenY=10,left=10,top=10,toolbar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,height=600,width=800");

document.getElementById('survey').style.display='none';

}



// SURVEY scripts -- added from home_extras.js
function writeCookieSurvey (cookieName, cookieValue, expiry) {
	var expDays = expiry; // number of days the cookie should last
	var expDate = new Date();
	expDate.setTime (expDate.getTime() + (expDays*24*60*60*1000));
	document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString() + "; path=/;domain=hbo.com";
}

function deleteCookieSurvey (name) {
	var expireNow = new Date();
	// null survey value and set cookie for one tick in the past
	expireNow.setTime(expireNow.getTime() - 1);
	document.cookie = name + "=" + "; expires=" + expireNow.toGMTString + "; path=/";
}


function surveyPopUp(pageIdentifier) {

// change this survey number each time there is a new one

	var survey7 = getCookie("survey7");

// change this frequency number based on the landing pages of the survey
// pageIdentifier number changes with every survey as there are always different pages that the survey goes on
// change the comments designating the page when a new survey is launched
// when setting the frequency, take the number of intercepts ex 1/14... divide it (ex .07) and put the decimal as the freq
// random number is between 0 and 1

	var freq;
	var topCoord;
	var leftCoord;
	var imgVersion;
	
	if ( pageIdentifier == 1 ) {
	// Selected Series Pages
	freq = '0.10';
	topCoord='85';
	leftCoord='232';
	imgVersion='<img src="http://www.hbo.com/img/survey/jan08/help_us_banner.gif" width="300" height="261" border=0 alt="survey" usemap="#map1">';
	} else if ( pageIdentifier == 2 ) {
	// HBO.com homepage
	freq = '0.10';
	topCoord='420';
	leftCoord='229';
	imgVersion='<img src="http://www.hbo.com/img/survey/jan08/help_us_banner.gif" width="300" height="261" border=0 alt="survey" usemap="#map2">';
	} else if ( pageIdentifier == 3 ) {
	// The Wire homepage
	freq = '0.10';
	topCoord='0';
	leftCoord='112';
	imgVersion='<img src="http://www.hbo.com/img/survey/jan08/wire_home_banner.gif" width="552" height="134" border=0 alt="survey" usemap="#map3">';
	} else if ( pageIdentifier == 4 ) {
	// Schedule
	freq = '0.10';
	topCoord='161';
	leftCoord='329';
	imgVersion='<img src="http://www.hbo.com/img/survey/jan08/help_us_banner.gif" width="300" height="261" border=0 alt="survey" usemap="#map1">';
	} else if ( pageIdentifier == 5 ) {
	// Selected Bulletin Board Pages
	freq = '0.10';
	topCoord='129';
	leftCoord='230';
	imgVersion='<img src="http://www.hbo.com/img/survey/jan08/help_us_banner.gif" width="300" height="261" border=0 alt="survey" usemap="#map1">';
	} else {
	// default
	freq = '0.1';
	}

	if (survey7 == "yes") {
		// Survey invite previously given, do nothing
	}
	else {
		// Survey invite has not been previously given, initiate
		rrand = Math.random();
		//window.alert(rrand);
		if (rrand < freq) {
			// random seed below threshhold, set cookie and pop survey
			writeCookieSurvey('survey7', 'yes','1');
			insertImg(topCoord,leftCoord,imgVersion);
			
		}
		else {
			// random seed above threshhold, do nothing
		}
	}
}

// PLEASE DISCONTINUE USING THIS SCRIPT!!
// use only the winPop function

function WinOpenFullAttr(height,width,url,popname,fullattr)
{
	window.open(
	url,
	popname,
	fullattr + "," + "height=" + height + ",width=" + width);
}


// function for handling the episode guide dropdown

function formHandler(form){
var URL = document.form.site.options[document.form.site.selectedIndex].value;
window.location.href = URL;
}

// function for bbs

function talkcity(bbslink) {	
window.location.href = bbslink;
//bbswindow=window.open(bbslink,"","personalbar=no,menubar=yes,toolbar=no,location=no,resizable=yes,scrollbars=yes,status=yes,height=500,width=710")
}

// readparse
function PageQuery(q) {
 if(q.length > 1) this.q = q.substring(1, q.length);
 else this.q = null;
 this.keyValuePairs = new Array();
 if(q) {
  for(var i=0; i < this.q.split("&").length; i++) {
   this.keyValuePairs[i] = this.q.split("&")[i];
  }
 }
 this.getKeyValuePairs = function() { return this.keyValuePairs; }
 this.getValue = function(s) {
  for(var j=0; j < this.keyValuePairs.length; j++) {
   if(this.keyValuePairs[j].split("=")[0] == s)
   return this.keyValuePairs[j].split("=")[1];
  }
  return "";
 }
 this.getParameters = function() {
  var a = new Array(this.getLength());
  for(var j=0; j < this.keyValuePairs.length; j++) {
   a[j] = this.keyValuePairs[j].split("=")[0];
  }
  return a;
 }
 this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString(key){
 var page = new PageQuery(window.location.search); 
 return unescape(page.getValue(key)); 
}

