
function gatewayCheck() {
  //check the location cookie (language is assumed)
  
  if (document.cookie.indexOf("huskyLocation") == -1) {
    //cookie not set

    //test cookie to see if they are enabled
    document.cookie="cookieTest=1";
    if (document.cookie.indexOf("cookieTest") != -1) {
      //cookies enabled
      displayGateway();
    } else {
//alert('no cookie');
    }
  }
}

function displayGateway() {
  setDropDowns("none");
  //resize and position the gateway elements
  var windowWidth = 0;
  var wind0wHeight = 0;

  //get the window width and height
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    windowWidth = window.innerWidth;
    windowHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }

  
  //set the size of background element

  var gateBG = document.getElementById("gateway_background");
  if (document.body && document.body.scrollHeight > windowHeight) {
   gateBG.style.width = document.body.scrollWidth + 'px';
//   gateBG.style.height = document.body.scrollHeight + 'px';
   gateBG.style.height = getDocHeight() + 'px';
  } else {
    gateBG.style.width = windowWidth + 'px';
//    gateBG.style.height = windowHeight + 'px';
   gateBG.style.height = getDocHeight() + 'px';
  }
  //using -1000px to hide the elements
  gateBG.style.top = '0px';
  gateBG.style.left = '0px';

  //center the gateway select element
  var gateMain = document.getElementById("gateway_select");
  gateMain.style.top = (windowHeight / 2) - (gateMain.offsetHeight / 2) + 'px';
  gateMain.style.left = (windowWidth / 2) - (gateMain.offsetWidth / 2) + 'px';


}

function hideGateway() {
  setDropDowns("block");
  var gateBG = document.getElementById("gateway_background");
  var gateMain = document.getElementById("gateway_select");

  gateBG.width = 0;
  gateBG.height = 0;

  gateBG.style.top = -1000;
  gateBG.style.left = -1000;

  gateMain.style.left = -1000;
  gateMain.style.top = -1000;
}

function doGateway(loc, lang) {
  //set cookies and redirect as necessary

 //set the expiry
 var exdate=new Date();
 exdate.setDate(exdate.getDate()+30);

 //set the location cookie
 var cookieText ='huskyLocation=' + loc;
 cookieText += '&huskyLang=' + lang + ';';

  //set the ecm language cookie
 cookieText +='ecm=user_id=0&site_id=&username=&new_site=/CMS400Example/&unique_id=0&site_preview=0&langvalue=0&DefaultLanguage=1033&NavLanguage=1033&LastValidLanguageID=1033&ContType=&UserCulture=1033&SiteLanguage=' + lang + ';';

 cookieText += 'expires=' + exdate.toUTCString() + ';';
 cookieText += 'path=/;';
 document.cookie = cookieText;

 //do the redirect
 if (loc == 'americas') {
   document.location = '/setlang.aspx?langType=' + lang;
 } else {
   document.location = '/setlang.aspx?langType=' + lang;
 }

}

function setDropDowns(display) {
  var dds = document.getElementsByTagName("SELECT");
  for (var i = 0; i < dds.length; i++) {
    dds[i].style.display = display;
  }
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
