function setActiveStyleSheet(title) {
  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;
    }
  }
  createCookie("style", title, 365);
}

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;
}

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;
}

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=/";
}

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;
}

function loadStyleSheet(e) {
  var cookie = readCookie("style");
  var title = (cookie ? cookie : (getPreferredStyleSheet() != null ? getPreferredStyleSheet() : "default"));
  setActiveStyleSheet(title);
}


function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function'){ 
		window.onload = func
	} else { 
		window.onload = function() {
			oldonload();
			func();
		}
	}
}



function getFirstElementHref (el) {
	var els = el.getElementsByTagName('a');
	for (var i = 0; i < els.length; i++) {
		var el = els[i];
		if (el.className == "element_href") return (el);
		break;
	}
	return (false);
}




function getHash() {
	var s = location.hash.substr(1);
	return (s);
}


function checkLocationHash () {
	// check to see if we have a DOM compliant browser
	if (document.getElementById) {
		// get location hash
		var hash = getHash();

		switch (hash) {
			case "textonly":
				setActiveStyleSheet("textonly");
				break;
			case "defaultstylesheet":
				setActiveStyleSheet("default");
				break;
			default:
				break;
		}
	}
}


function alternateRowStyles(el){
	var nodes = $A(el.getElementsByTagName('tr'));
	
	nodes.each(
		function(node, index){
			Element.addClassName(node, (index%2 ? "" : "alt"));
		}
	);
}

function cleanString(id){
	obj = document.getElementById(id);
	str = obj.value;
	str = str.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-|\//g,"");    
	obj.value = str;
}


if(document.all){
	document.execCommand("BackgroundImageCache",false,true);
}

//limits the textarea entry to 500 characters
function textarealimit(){
	// the phrase in french and english
	var message = (language == "fr" ? "Cliquez ici pour ajouter votre message. Votre message peut contenir jusqu'à 500 caractères." : "Click here to add your message. You may enter a maximum of 500 characters of text.");
	
	$$("textarea").each(function(s){
	    if (s.value == "") s.value = message; 
	    Event.observe(s, 'focus', function() { if (s.value == message) { s.value = ""; } });
		Event.observe(s, 'blur', function(){if(s.value == ""){s.value = message;}});
		Event.observe(s, 'keydown', function(){if(s.value.length > 500){s.value = s.value.substring(0,500);}});
		Event.observe(s, 'keyup', function(){if(s.value.length > 500){s.value = s.value.substring(0,500);}});
		//clear the value before it gets submitted if it's the default.
		Event.observe($$("form.webform")[0], 'submit', function(){if(s.value == message){s.value = "";}});
	});
		
}

addLoadEvent(textarealimit);
addLoadEvent(loadStyleSheet);
addLoadEvent(checkLocationHash);
