/****************************************************************************
Validate browser
****************************************************************************/
if (document.layers) 
{
	//NN4
	document.location = "Unsupported.aspx";
}

if (!document.all && !document.getElementById)
{
	//Older browsers
	document.location = "Unsupported.aspx";
}

/****************************************************************************
Set the active styleSheet
****************************************************************************/
function setActiveStyleSheet(title) 
{
  if (title != 'null')
  {
      var i, a, main;
      for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
      {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
        {
          a.disabled = true;
          if(a.getAttribute("title") == title) 
          {
            a.disabled = false;
          }
        }
      }
   }
}

/****************************************************************************
Get the active styleSheet
****************************************************************************/
function getActiveStyleSheet() 
{
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
  {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) 
    {
        return a.getAttribute("title");
    }
  }
  return null;
}

/****************************************************************************
Get the preferred styleSheet
****************************************************************************/
function getPreferredStyleSheet() 
{
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
  {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}


/****************************************************************************
Toggle the style
****************************************************************************/
function toggleStyle()
{
	if (getActiveStyleSheet() == 'default')
	{
	    setActiveStyleSheet('large');
	}
	else
	{
	    setActiveStyleSheet('default');
	}
}

/****************************************************************************
Create a cookie
****************************************************************************/
function createCookie(name,value,days) 
{
  if (days) 
  {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

/****************************************************************************
read a cookie
****************************************************************************/
function readCookie(name) 
{
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) 
  {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

/****************************************************************************
Search 
****************************************************************************/
function TrimString(str) 
{
  str = str.replace( /^\s+/g, "" ); 
  return str.replace( /\s+$/g, "" );
}

function RunSearch(terms, NavID, CultureCode)
{		
	terms = TrimString(terms);
	terms = terms.replace(/\s\s/gi," ");

	if (terms.length > 0)
	{
		terms = terms.replace(/>/gi, '');
		terms = terms.replace(/</gi, '');
		terms = terms.replace(/&/gi, '');
		terms = terms.replace(/#/gi, '');
		terms = terms.replace(/=/gi, '');
		terms = terms.replace(/\\/gi, '');
		terms = terms.replace(/\//gi, '');
		terms = terms.replace(/\`/gi, '');
		terms = terms.replace(/[ ][ ]+/gi, ' ');
		document.location = 'Search.aspx?NavID=' + NavID + "&CultureCode=" + CultureCode + "&terms=" + terms;
	}
	else
	{
		if (CultureCode == 'F')
			alert('Veuillez entrer un mot cl\xE9')
		else
			alert('Please, enter a keyword')
	}
}

/****************************************************************************
Change Language 
****************************************************************************/
function changeLang(strLang)
{
    var strLocationSearch = location.search;
    if (strLocationSearch.toLowerCase().indexOf('culturecode=') >= 0)
    {
        strLocationSearch = strLocationSearch.replace(/CultureCode=...../i, 'CultureCode=' + strLang);
    }
    else if (strLocationSearch.length > 0)
    {
        strLocationSearch += '&CultureCode=' + strLang;
    }
    else
    {
        strLocationSearch = '?CultureCode=' + strLang;
    }
    location.href = location.protocol + '//' + location.host + location.pathname + strLocationSearch + location.hash;
}

/****************************************************************************
Generate a number between 0 and range
****************************************************************************/
function get_random(range)
{
	return Math.round(Math.random() * range);
}

/****************************************************************************
Dynamic Footer Posotionning
****************************************************************************/
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function setFooter() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var contentHeight = document.getElementById('wrapper').offsetHeight;
			var footerElement = document.getElementById('footer');
			var footerHeight  = footerElement.offsetHeight;
			if (windowHeight - (contentHeight + footerHeight) >= 0) {
				footerElement.style.position = 'relative';
				footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
			}
			else {
				footerElement.style.position = 'static';
			}
		}
	}
}

/****************************************************************************
Called on ClientScript
****************************************************************************/
function setStyle()
{
	// Set the stylesheet
	if (typeof(readCookie) == "function" && typeof(getPreferredStyleSheet) == "function" && typeof(setActiveStyleSheet) == "function")
	{
		var cookie = readCookie("style");
		var title = (cookie!=null && cookie!='') ? cookie : getPreferredStyleSheet();
		setActiveStyleSheet(title);
		setFooter();
	}
}

/****************************************************************************
Called on Page Reseize Event
****************************************************************************/
function resize()
{
	setFooter();
}

/****************************************************************************
Called on Page Load Event
****************************************************************************/
function load()
{

}

/****************************************************************************
Called on Page UnLoad Event
****************************************************************************/
function unload()
{
	if (typeof(getActiveStyleSheet) == "function" && typeof(createCookie) == "function")
	{
		var title = getActiveStyleSheet();
		createCookie("style", title, 365);
	}
}

/****************************************************************************
Handle window object Events
****************************************************************************/
window.onload = load;
window.onunload = unload;
window.onresize = resize;
