// Global Functions

// set the servername for apps
if ( document.domain.match("dev") ) { secure_servername = "https://www.aztv.az"; }
else if ( document.domain.match("webbeta") ) { secure_servername = "https://www.aztv.az"; }
else if ( document.domain.match("62.212.234.214") ) { secure_servername = "https://62.212.234.214"; }
else { secure_servername = "https://www.aztv.az"; }

// global window popping script: for within site and cross-domain
function winPop(url,width,height)
{
	window.open(
	url,
	"aztvwinpop",
	"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();
	}
}

// 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 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');
}

// 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 scripts -- added from home_extras.js
function writeCookieSurvey (cookieName, cookieValue, expiry) {
	var expDays = 60; // 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=/";
}

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;

	if ( pageIdentifier == 1 ) {
	// Homepage
	freq = '0.0001';
	} else if ( pageIdentifier == 2 ) {
	// SATC
	freq = '0.1';
	} else if ( pageIdentifier == 3 ) {
	// Deadwood
	freq = '0.0001';
	} else {
	// default
	freq = '0.0001';
	}

	if (survey7 == "yes") {
		// Survey invite previously given, do nothing
	}
	else {
		// Survey invite has not been previously given, initiate
		rrand = Math.random();
		if (rrand < freq) {
			// random seed below threshhold, set cookie and pop survey
			writeCookieSurvey('survey7', 'yes');
			window.open("/survey/pop-up_survey.html?" + pageIdentifier, "surveyInvitation", "top=5,left=5,height=400,width=500,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0");
		}
		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)); 
}

