// Redirect from the server's 301 hash to an in-progress redirect hash
if (location.href.indexOf('#redirect_notice_display_301') > -1) {
  window.location.href = '#redirect_in_progress';
} // End of if conditional: (location.href.indexOf('#redirect_notice_display_301') > -1)

// Method to add function to onload
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      func();
      if (oldonload) {
        oldonload();
      }
    }
  }
}

// Create blank div to hide page load before notice appears (page load appears
// in faster browsers like Firefox, Chrome, and Safari)
if (location.href.indexOf('#redirect_in_progress') > -1) {  
  // Calculate the page width and height 
  if( document.body && ( document.body.scrollWidth ) ) {
      var pageWidth = document.body.scrollWidth+'px';
  } else if( document.body.offsetWidth ) {
    var pageWidth = document.body.offsetWidth+'px';
  } else {
     var pageWidth='100%';
  }
  var pageWidth = '100%';
  var pageHeight=document.body.offsetHeight+'px';
  
  // Create the div blanker element
  var notice = document.createElement("div");
  
  // Style the div to the full page dimensions with a white opaque background
  notice.style.position='absolute';                 
  notice.style.top='0px';
  notice.style.left='0px';
  notice.style.backgroundColor = '#fff';
  notice.style.opacity=100;
  notice.style.MozOpacity=100;
  notice.style.filter='alpha(opacity=100)';
  notice.style.zIndex=48;
  notice.style.display='block';                      
  notice.style.width= pageWidth;
  notice.style.height= pageHeight;
  notice.id = 'redirect_notice';
    
  // A reference of all the scripts in the DOM
  var scripts = document.getElementsByTagName('script');
  
  // Find the script location in the DOM and insert the notice before it
  for(var i=0; i < scripts.length; i++) {
    if(scripts[i].src.slice(-11) == 'redirect.js') {
      var scr = scripts[i];
      scr.parentNode.insertBefore(notice, scr);
      break;
    } // End of if conditional: (scripts[i].src.slice(-9) == 'script.js')
  } // End of for loop: (var i=0; i < scripts.length; i++) 
  
  
  // On load, reset the dimensions of the notice (IE loading from cache will)
  // cause the notice to cut off at the "BC Home" Header
  addLoadEvent(function() {
    // Get the page dimensions for the browser
    var pageWidth = document.body.clientWidth+'px';
    var pageHeight = document.body.clientHeight+'px';
     
    var notice = document.getElementById('redirect_notice');
    var noticeContent = document.getElementById('redirect_notice_content'); 
    notice.style.width= pageWidth;
    notice.style.height= pageHeight;
    noticeContent.style.width= pageWidth;
    noticeContent.style.height= pageHeight;
  }); // End of addLoadEvent call: function()
  
  
  
  
  // ================================================
  // ================================================
  //
  //         Start of required functions
  //
  // ================================================
  // ================================================
  
  // If correct hash anchor has been set, redirect to unnamed anchor (to remove
  // possibility of bookmarking the notice page) and start the creation of the
  // redirect notice
  function displayRedirectNotice(noticeURL) {
    if (location.href.indexOf('#redirect_in_progress') > -1) {
      window.location.href = '#';
  
      
      var xmlhttp;
      redirectRequest(noticeURL);
    } // End of if conditional: (location.href.indexOf('#redirect_in_progress') > -1)
  } // End of displayRedirectNotice function: (noticeURL)
  
  // Create the XML HTTP Request (AJAX) for the redirect notice
  function redirectRequest(url) {
    xmlhttp = null;
    
    // code for IE7, Firefox, Opera, etc.
    if (window.XMLHttpRequest) {
      xmlhttp=new XMLHttpRequest();
    }
    // code for IE6, IE5
    else if (window.ActiveXObject) {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    // If the xmlhttp object has been created successfully, link the state
    // listener function
    if (xmlhttp!=null) {
      xmlhttp.onreadystatechange=checkState;
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);
    } // End of if conditional: (xmlhttp!=null)
    else
    {
      return false;
    } // End of else conditional: !(xmlhttp!=null)
  } // End of redirectRequest function: (url)
  
  
  // If AJAX status is correct, call method to create the notice
  function checkState() {
    // State of 4 is "loaded"
    if (xmlhttp.readyState==4) {
      if (xmlhttp.status==200)
      {
        noticeLoaded = true;
        createNotice(xmlhttp.responseText);
      } // End of if conditional: (xmlhttp.status==200)
      else
      {
        return false;
      } // End of else conditional: !(xmlhttp.status==200)
    } // End of if conditional: (xmlhttp.readyState==4)
  } // End of checkState function: ()
  
  
  // Generate the html required to display the notice
  function createNotice(content) {
    // Get the page dimensions for the browser
    var pageWidth = document.body.clientWidth+'px';
    var pageHeight = document.body.clientHeight+'px'; 
    
    // Create the div notice element
    var noticeContent = document.createElement("div");
  
    // Style the div to the full page dimensions with a white opaque background
    noticeContent.style.position='absolute';                 
    noticeContent.style.top='0px';
    noticeContent.style.left='0px';
    noticeContent.style.backgroundColor = '#fff';
    noticeContent.style.opacity=100;
    noticeContent.style.MozOpacity=100;
    noticeContent.style.filter='alpha(opacity=100)';
    noticeContent.style.zIndex=49;
    noticeContent.style.display='block';                      
    noticeContent.style.width= pageWidth;
    noticeContent.style.height= pageHeight;
    noticeContent.id = 'redirect_notice_content';
    
    // Insert the content into the noticeContent div
    noticeContent.innerHTML = content;
    
    // Reset notice container dimensions in case the page dimensions have changed
    var notice = document.getElementById('redirect_notice');
    notice.style.width= pageWidth;
    notice.style.height= pageHeight;
    notice.appendChild(noticeContent);
  } // End of createNotice function: (content)
  
  
  
  // Resize the notice div when the window is resized
  function resizeWindow() {
    var notice = document.getElementById('redirect_notice');
  
    if (notice != undefined) {
      var pageWidth = document.body.clientWidth+'px';
      var pageHeight = document.body.clientHeight+'px';
    
      notice.style.width= pageWidth;
      notice.style.height= pageHeight;
    } // End of if conditional: (notice != undefined)
  } // End of function resize()
  
  
  // ================================================
  // ================================================
  //
  //         End of required functions
  //
  // ================================================
  // ================================================  
} // End of if conditional: (location.href.indexOf('#redirect_in_progress') > -1)