var countryParams = 'country=' + countryCode + '&lang=' + languageCode;
var cookieValue = countryCode + '/' + languageCode;
var popuNextLocation;

// Cookies utility functions.
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// Main page functions.

// This function will open the popup window if it has not been disabled,
// else it will redirect the browser to the specified url.
function goTo(url) {
	var nextLocation = url + "?" + countryParams;
	
	popuNextLocation = nextLocation;
	goBackTo(nextLocation);
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}





function goBackTo(nextLocation) {
	if (getCookie('bosePopup') == null) {
		popup = window.open(popupUrl+'?loc='+escape(nextLocation), 'bosePopup', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=650,height=320,left=0,top=0');
		popup.nextLocation = nextLocation;
		popup.popuNextLocation = nextLocation
	} else {
		document.location = nextLocation;
	}
}

// Popup functions.

// Disable the popup by setting a cookie on the browser.
// If the user check the check box, and then uncheck it the cookie
// is removed (popup stays enabled). Use it with the 'onclick' event
// of a check box.
function disablePopup(elt) {
	if (elt.checked) {
		setCookie('bosePopup', 'disabled', null, '/', null, null);
	} else {
		deleteCookie('bosePopup', '/', null);
	}
}
// Redirect the main window to the given url, and then close the popup window.
function go() {
	var parsedLocation =  getQueryVariable("loc")
//	if (parsedLocation != ""){
//		parsedLocation = unescape(parsedLocation);
//	}	
    setCookie('boseTest', cookieValue);
	if (getCookie('boseTest') == null) {
		window.opener.document.location = '/EU/en/error-pages/nocookies.jsp?country=EU&lang=en';
	} else {
		deleteCookie('boseTest', '/', null);
        window.opener.document.location = '/app/redirect?url=' + parsedLocation;
	}
	self.close();
}
