function initPage()
{
  if(typeof(initHomepage) == 'function')
    initHomepage();
  
  var e = document.getElementById('homeenergycheck')
  if(e != null && typeof(e) != 'undefined')
    addClearButton();
  
  useCookie();
}

//Store a value in cookie
function setCookie(title,val)
{
  document.cookie = title+"="+val+";";
}

//Retrieve the cookie value
function useCookie()
{
  if(document.cookie != null)
  {
    if(document.cookie.indexOf("contrast=true") != -1)
    {
      activeStylesheet('contrast');
    }
    
    var t = document.cookie.match(/fontSize=(\d+)_(\d+(?:[.]\d+)?(?:em|px|%))/);
    if( t!= null && t[1] != null && t[2] != null)
    {
      fontSizeChange(t[1], t[2]);
    }
  }
}

function activeStylesheet(titleStyle)
{
  var links = document.getElementsByTagName("link");
  
  for(var i=0; i<links.length;i++)
  {
    var l = links[i];
    
    if(l.getAttribute("rel").indexOf("stylesheet") !=-1 )
    {
      if(l.getAttribute("title") && l.getAttribute("title") == titleStyle)
      {
        setCookie(titleStyle,"true");
        l.disabled = false;
      }
    }
  }
}

function deactiveStylesheet(titleStyle)
{
  var links = document.getElementsByTagName("link");
  
  for(var i=0; i<links.length;i++)
  {
    var l = links[i];
    
    if(l.getAttribute("rel").indexOf("stylesheet") !=-1 )
    {
      if(l.getAttribute("title") && l.getAttribute("title") == titleStyle)
      {
        setCookie(titleStyle,"false");
        l.disabled = true;
      }
    }
  }
}

//Function for change the icon (if the text if small, medium and big and if there is contrast or not
function changeIcon(nbicon)
{
  var s = document.getElementById('smalltext').style;
  var m = document.getElementById('medtext').style;
  var l = document.getElementById('largetext').style;
  var c = document.getElementById('contrast').style;
  var n = document.getElementById('nocontrast').style;
  
  var arr = new Array(s,m,l);
  
  for(var i=0;i<3;i++)
  {
    if(nbicon==i)
    {
      arr[i].fontWeight = 'bold';
    }
    else
    {
      arr[i].fontWeight = 'normal';
    }
  }
  
  if(nbicon==4)
  {
    c.borderColor='#000';
  }
}

//Function who change the size of the text and call the function for change the icon
function fontSizeChange(opt, size)
{
  setCookie("fontSize",""+opt+"_"+size);
  document.body.style.fontSize=size;
  changeIcon(opt);
}

//Erase the input of the search
function eraseSearch(e)
{
  e.value="";
}

/** Standard function from Webcenter, set focus on login **/
function setRegFocus(){
  if( document.login && document.login.username )
  {
    document.login.username.focus();
  }
}

function minimise(id)
{
  var e = document.getElementById(id);
  if(e)
  {
    e.style.display='none';
  }
}

function clearform(id)
{
  var f = document.getElementById(id);
  var inps = f.getElementsByTagName('input');
  var txts = f.getElementsByTagName('textarea');
  
  for(var i=0;i<inps.length;i++)
  {
    var inp = inps[i];
    if(inp.type == 'radio' || inp.type == 'checkbox')
      inp.checked = false;
    else if(inp.type == 'text')
      inp.value = '';
  }
  
  for(var i=0;i<txts.length;i++)
  {
    txts[i].value = '';
  }
}

/* NOTE: Taken from ajaxfuncs.js as the whole file was not needed. */
/*       If included later, delete this function.                  */
/* Function to insert an element after another element, in the DOM */
function insertNodeAfter(newChild, refChild)
{ 
  refChild.parentNode.insertBefore(newChild,refChild.nextSibling); 
}

function addClearButton()
{
  /* <button onclick="clearform('homeenergycheck'); return false;">Clear form</button> */
  var p = document.getElementById('smfcsubmitbutton');
  var e = document.createElement('button');
  
  e.onclick = function(){clearform('homeenergycheck'); return false;}
  e.appendChild(document.createTextNode('Clear form'));
  
  insertNodeAfter(e,p);
}
