/* Function to get the RefUrl parameter for the link to HTML gateway
 */
function getRefUrl() {
	var refUrl = '/NoFlash/Gatewaypage.htm?RefUrl=http%3a%2f%2f';
	var constructedDomain = '';
	var domain = document.domain;
	var domainParts = domain.split('.');
	var root = domainParts[0];
	switch (root) {
		case 'testinggw':
			domainParts[0] = 'testing3';
			break;
		case 'staginggw':
			domainParts[0] = 'staging3';
			break;
		case 'landingpage2':
			domainParts[0] = 'www3';
			break;
	}
	for (var i=0; i<domainParts.length; i++) {
		constructedDomain += domainParts[i];
		if (i != (domainParts.length-1)) {
			constructedDomain += '.';
		}
	}
	refUrl += constructedDomain;
	refUrl += '%2fen-us%2fhome%2fhtmlhome';
	return refUrl;
}
/** Function intended to open a popup window based on language and country
   * popupName - name of the popup window to be opened
   * countryCode - language and country information
   * userId - identifies the user for the opening a manage account page
   **/
function smirnoffPopupCc(popupName, countryCode, userId) {
  smirnoffPopup(popupName,userId);
}
/** Function intended to open a popup window based on language and country
   * popupName - name of the popup window to be opened
   * countryCode - language and country information
   * userId - identifies the user for the opening a manage account page
   **/
function smirnoffPopupCcNoBlock(popupName, countryCode, userId) {
  smirnoffPopupNoBlock(popupName,userId);
}
/** Helper function to call smirnoffPopupOver with the popup blocker check toggled
  * on
  * popupName - name of the popup window
  * userId - identifies the user for the opening a manage account page
  **/
function smirnoffPopup(popupName,userId) {
  smirnoffPopupOver(popupName,'1',userId);
}
/** Helper function to call smirnoffPopupOver with the popup blocker check toggled
  * off
  * popupName - name of the popup window
  * userId - identifies the user for the opening a manage account page
  **/
function smirnoffPopupNoBlock(popupName,userId) {
  smirnoffPopupOver(popupName,'0',userId);
}
/** Opens a popup window with the appropriate attributes and positioning based on name
   * After telling the popup to open it waits two seconds and calls the function to test if the
   * popup was blocked
   * popupName - name of the popup window
   * check - toggles whether or not to check for a blocked popup
   * userId - identifies the user for the opening a manage account page
   **/
function smirnoffPopupOver(popupName,check,userId) {
  var swidth = screen.width;
  var sheight = screen.height;
  var width; var height; var popupWindow;
  var checkBlocked;
  if (check == '1') { checkBlocked = true; }
  else { checkBlocked = false; }
  switch(popupName)
  {
    case 'terms_of_use':
      width = 508; height = 500;
      popupWindow = window.open('/en-us/terms.html',popupName,'resizable=no,scrollbars=yes,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
    case 'privacy_statement':
      width = 508; height = 500;
      popupWindow = window.open('/en-us/privacy.html',popupName,'resizable=no,scrollbars=yes,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
    case 'accessibility_statement':
      width = 508; height = 500;
      popupWindow = window.open('/en-us/accessibility.html',popupName,'resizable=no,scrollbars=yes,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
    case 'contactus':
      width = 336; height = 460;
      popupWindow = window.open('/en-us/Utilities/ContactUs.htm',popupName,'resizable=no,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
	case 'sxregistration':
		width = 330; height = 625;
		url = '/en-us/Secure/experience/ExperienceRegistration.htm';
		popupWindow = window.open(url,popupName,'resizable=no,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
			break;
	case 'sxregistrationTerms':
		width = 330; height = 625;
		url = '/en-us/Secure/experience/terms.htm';
		window.open(url,popupName,'resizable=yes,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
			break;
	case 'diamondcode':
		width = 650; height = 690;
		var diamondcode = document.getElementById('codebox').value;
		url = 'http://diamonds.smirnoff.com/?diamondcode='+diamondcode;
		popupWindow = window.open(url,popupName,'resizable=no,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
			break;
	case 'diamondemailcode':
		width = 650; height = 690;
		url = 'http://diamonds.smirnoff.com/RequestCode.aspx';
		popupWindow = window.open(url,popupName,'resizable=no,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
			break;
	case 'diamondofficialrules':
		width = 650; height = 690;
		url = 'http://diamonds.smirnoff.com/Rules.aspx';
		popupWindow = window.open(url,popupName,'resizable=no,scrollbars=yes,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
			break;
    case 'registration_1':
      width = 660; height = 572;
      popupWindow = window.open('/en-us/Secure/Registration/USRegistration.htm',popupName,'resizable=yes,scrollbars=yes,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
    case 'registration_2':
      width = 640; height = 572;
      popupWindow = window.open('/en-us/Secure/Registration/registration_2.htm',popupName,'resizable=yes,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
    case 'registration_3':
      width = 640; height = 572;
      popupWindow = window.open('/en-us/Secure/Registration/registration_thanks2.htm',popupName,'resizable=no,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
		case 'electric_cabaret':
			width = 565; height = 600;
			popupWindow = window.open('/en-us/CabaretForm.htm',popupName,'resizable=no,scrollbars=yes,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
			break;
    case 'login':
      width = 420; height = 210; 
      popupWindow = window.open('/en-us/Secure/Login/Login.htm',popupName,'resizable=no,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
    case 'forgot_password':
      width = 420; height = 200;
      popupWindow = window.open('/en-us/Secure/ForgotPW/forgot_password.htm',popupName,'resizable=no,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
    case 'sent_password':
      width = 420; height = 200;
      popupWindow = window.open('/en-us/Utilities/Secure/ForgotPW/sent_password.htm',popupName,'resizable=no,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
    case 'unsubscribe':
      width = 336; height = 355;
      popupWindow = window.open('/en-us/Utilities/Unsubscribe/Unsubscribe.htm',popupName,'resizable=no,scrollbars=no,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
    case 'manage_account':
      width = 660; height = 572;
      popupWindow = window.open('/en-us/Secure/ManageAccount/manage_account.htm?userid='+userId,popupName,'resizable=no,resizable=no,scrollbars=yes,width=' + width + ',height=' + height + ',top=' + calcTop(sheight,height) + ',left=' + calcLeft(swidth,width));
      break;
    case 'smirnoff.com':
      window.open('http://www.smirnoff.com',popupName,'resizable=yes');
      checkBlocked = false;
      break;
    case 'drink_responsibly':
      window.open('http://www.diageo.com/en-row/CorporateCitizenship/PromotingResponsibledrinking/',popupName,'resizable=yes');
      checkBlocked = false;
      break;
	case 'the_bar':
      window.open('http://www.thebar.com?promo=WBE0009A11',popupName,'resizable=yes');
      checkBlocked = false;
      break;
	case 'ICAP':
	  window.open('http://www.icap.org/',popupName,'resizable=yes');
      checkBlocked = false;
      break;
    case 'know_your_drink':
      window.open('http://www.drinkIQ.com',popupName,'resizable=yes');
      checkBlocked = false;
      break;
		case 'about_cookies':
			window.open('http://www.aboutcookies.org',popupName,'resizable=yes');
      checkBlocked = false;
			break;
		case 'diageo_brands':
			window.open('http://www.diageobrands.com/',popupName,'resizable=yes');
      checkBlocked = false;
			break;
    case 'html_site':
      checkBlocked = false;
			window.location = 'http://'+document.domain+'/en-us/Home/HTMLHome.htm';
      break;
		case 'flash_site':
			checkBlocked = false;
			window.location = 'http://'+document.domain+'/en-us/Home/home.htm';
			break;
		case 'html_site_pop':
			checkBlocked = false;
			window.opener.location='http://'+document.domain+'/en-us/Home/HTMLHome.htm';
			break;
		case 'flash_site_pop':
			window.opener.location='http://'+document.domain+'/en-us/Home/home.htm';
			break;
		case 'html_gate':
			checkBlocked = false;
			var sOpLoc = new String(window.location);
			var refUrlLoc = sOpLoc.indexOf('RefUrl');
			var refUrlAmp = sOpLoc.indexOf('%26',refUrlLoc);
			var refUrl = sOpLoc.substring(refUrlLoc,refUrlAmp);
			window.location = 'http://'+document.domain+'/noflash?'+refUrl;
			break;
		case 'flash_gate':
			checkBlocked = false;
			var sOpLoc = new String(window.location);
			var refUrlLoc = sOpLoc.indexOf('RefUrl');
			var refUrlAmp = sOpLoc.indexOf('%26',refUrlLoc);
			var refUrl = sOpLoc.substring(refUrlLoc,refUrlAmp);
			window.location = 'http://'+document.domain+'/flash?'+refUrl;
			break;
		case 'html_gate_pop':
			checkBlocked = false;
			var sOpLoc = new String(window.opener.location);
			var refUrlLoc = sOpLoc.indexOf('RefUrl');
			var refUrlAmp = sOpLoc.indexOf('%26',refUrlLoc);
			var refUrl = sOpLoc.substring(refUrlLoc,refUrlAmp);
			window.opener.location = 'http://'+window.opener.document.domain+'/noflash?'+refUrl;
			break;
		case 'flash_gate_pop':
			checkBlocked = false;
			var sOpLoc = new String(window.opener.location);
			var refUrlLoc = sOpLoc.indexOf('RefUrl');
			var refUrlAmp = sOpLoc.indexOf('%26',refUrlLoc);
			var refUrl = sOpLoc.substring(refUrlLoc,refUrlAmp);
			window.opener.location = 'http://'+window.opener.document.domain+'/flash?'+refUrl;
			break;
    case 'logout':
      //session.invalidate();
      checkBlocked = false;
      break;
  }
  if (checkBlocked && popupWindow == null) { setTimeout(function(){smirnoffPopupBlocked(popupName);},5000); }
	return popupWindow;
}
/** Helper function to calculate where to position a popup so it is centered vertically
   * sheight - height of the screen display
   * height - height of the popup window
   **/
function calcTop(sheight, height) {
  return sheight/2 - height/2;
}
/** Helper function to calculate where to position a popup so it is centered horizontally
   * swidth - width of the screen display
   * width - width of the popup window
   **/
function calcLeft(swidth, width) {
  return swidth/2 - width/2;
}

// -- Popup blocker detection

/** Function to set a value in a hidden field on the parent document of the popup window
   * parentDoc - document of the parent windo
   * popupName - name of the window to open
   **/
function smirnoffPopupOnLoad(win) {
  var popupName = win.name;
  if (win.opener) {
    var parentDoc = win.opener.document;
    if (parentDoc) {
      var popupBlockDetect = parentDoc.getElementById('popupBlockDetect');
      if (popupBlockDetect) { popupBlockDetect.value = popupName; }
    }
  }
}
/** Function to check if the popup has been blocked and if it has to alert the user
   * of the need for them to allow popups
   * doc - document of popup parent window
   * popupName - name of the popup window which may have been blocked
   **/
function smirnoffPopupBlocked(popupName) {
  // should be called after a couple second delay from the browser
  var popupBlockDetect = document.getElementById('popupBlockDetect');
  var blocked = popupBlockDetect.value != popupName;
  var swf = document.getElementById('home');
  if (swf && blocked) { swf.popupBlockerEnabled(); }
  else if (blocked) { // no swf found so it is an html page
    // get the div with the popup has been blocked copy and make it visible
    var popupBlocked = document.getElementById('popupBlocked');
    popupBlocked.style['display'] = 'block';
  }
}

// -- end popup blocker detection