var xOp7Up,xOp6Dn,xIE4Up,xIE4,xIE5,xNN4,xUA=navigator.userAgent.toLowerCase();
if(window.opera){
  var i=xUA.indexOf('opera');
  if(i!=-1){
    var v=parseInt(xUA.charAt(i+6));
    xOp7Up=v>=7;
    xOp6Dn=v<7;
  }
}
else if(navigator.vendor!='KDE' && document.all && xUA.indexOf('msie')!=-1){
  xIE4Up=parseFloat(navigator.appVersion)>=4;
  xIE4=xUA.indexOf('msie 4')!=-1;
  xIE5=xUA.indexOf('msie 5')!=-1;
}
else if(document.layers){xNN4=true;}
xMac=xUA.indexOf('mac')!=-1;

function xAddEventListener(e,eT,eL,cap)
{
  if(!(e=xGetElementById(e))) return;
  eT=eT.toLowerCase();
  var eh='e.on'+eT+'=eL';
  if(e.addEventListener) e.addEventListener(eT,eL,cap);
  else if(e.attachEvent) e.attachEvent('on'+eT,eL);
  else eval(eh);
}

function xGetElementById(e)
{
  if(typeof(e)!='string') return e;
  if(document.getElementById) e=document.getElementById(e);
  else if(document.all) e=document.all[e];
  else e=null;
  return e;
}

function xGetElementsByTagName(t,p) 
{
  var list = null;
  t = t || '*';
  p = p || document;
  if (xIE4 || xIE5) {
    if (t == '*') list = p.all;
    else list = p.all.tags(t);
  }
  else if (p.getElementsByTagName) list = p.getElementsByTagName(t);
  return list || new Array();
}

function addClass_o() 
{
  var allTags, thisTag;
  
  allTags = xGetElementsByTagName ('INPUT','');
  for (var i = 0; i < allTags.length; i++) {
    thisTag = allTags[i];
    if ((thisTag.type.toLowerCase() == 'submit') || (thisTag.type.toLowerCase() == 'button')) {
      thisTag.className += (!thisTag.className) ? 'btn' : ' btn';
    }
  }
}

// version for zelph_onDOMload
/*function addClass() 
{
  if ((theTarget.type.toLowerCase() == 'submit') || (theTarget.type.toLowerCase() == 'button')) {
    theTarget.className += (!theTarget.className) ? 'btn' : ' btn';
  }
}*/
function addClass() 
{
  switch (theTarget.type.toLowerCase()) {
    case 'submit':
    case 'button':
    case 'reset':
      theTarget.className += (!theTarget.className) ? 'btn' : ' btn';
      break;
    /* case 'checkbox':
      theTarget.className += (!theTarget.className) ? 'check' : ' check';
      break;
    case 'radio':
      theTarget.className += (!theTarget.className) ? 'radio' : ' radio';
      break; */
    case 'text':
      theTarget.className += (!theTarget.className) ? 'text' : ' text';
      break;
  }
}

/*************************************
	onDOMload
	v1.1
	last revision: 03.31.2005
	aaron@zelph.com
	get more info: http://www.zelph.com/releases/ondomload/
	
	zelph_ prefix is just so function names don't interfere with potentially similarly named functions.

	Vars passed to the function
	dtw = Do To What -  the id/class/tagname that the code should be run on
	ctr = Code To Run -  is the actual code to run on the element/id.  use "theTarget" to reference the element you want to work with in the code you pass
	
	Other vars used in the script
	ndc = Number Done Count - Array that will hold the number that have been done for each request. Uses assoicative array,
	kg = Keep Going - as long as this is true the loop continues. Stops after "onload" event is fired
	es = ElementS (e and s) - tht elements returned by the getElementsBySelector
	nes = Number ElementS - the number of elements returned above
	coc = Copy Of Code - copy of the code we are running, so it can be run one last time when the onload fires to stop the loops
*************************************/
d = document;
ndc = new Array();
coc = new Array();
kg = true;
function zelph_onDOMload(dtw,ctr){
	es = zelph_getElementsBySelector(dtw); // get all elements that match what we are looking for
//	alert(es.length);
	if(ndc[dtw] == null){
		ndc[dtw] = 0; // initialize the number we have done for this tag type
		coc[coc.length] = "zelph_onDOMload('" + dtw + "', '" + ctr + "')"; // copy off the code
	}
	if(es.length > ndc[dtw]){ // if available elments are greater then the number of elements already done, then lets do them
		nes = es.length;
		for(var x=ndc[dtw]; x<nes; x++){ // loop through all elements that have not been done yet
			// do what we need to do
			theTarget = es[x];
			eval(ctr);
		}
		ndc[dtw] = nes;
	}
	
	if(kg == true){
		// play it again sam
		setTimeout("zelph_onDOMload('" + dtw + "', '" + ctr + "')", 100);
	}
	return true;
}

// taken from http://pro.html.it/esempio/nifty/niftyCodice.html
// modified to return false in a few situations since that wasn't an option in the original script
// changed to use the xFunctions
function zelph_getElementsBySelector(selector){
	var i;
	var s=[];
	var selid="";
	var selclass="";
	var tag=selector;
	var objlist=[];
  var theID;
	if(selector.indexOf(" ")>0){  //descendant selector like "tag#id tag"
		s=selector.split(" ");
		var fs=s[0].split("#");
		if(fs.length==1) return(objlist);
    theID = xGetElementById(fs[1]);
		if(theID){
      return(xGetElementsByTagName(s[1],fs[1]));
		}
		return false;
	}
	if(selector.indexOf("#")>0){ //id selector like "tag#id"
		s=selector.split("#");
		tag=s[0];
		selid=s[1];
	}
	if(selid!=""){
    theID = xGetElementById(selid);
		if(theID){
			objlist.push(theID);
			return(objlist);
		}
		return false;
	}
	if(selector.indexOf(".")>0){  //class selector like "tag.class"
		s=selector.split(".");
		tag=s[0];
		selclass=s[1];
	}
	var v=xGetElementsByTagName(tag,'');  // tag selector like "tag"
	if(selclass=="")return(v);
	for(i=0;i<v.length;i++){
		curClass = " "+v[i].className+" ";
		if(curClass.indexOf(" "+selclass+" ") != "-1"){
			objlist.push(v[i]);
		}
	}
	return(objlist);
}

function zelph_stopIt(){
	kg = ""; // stop the flash loop
	for(x=0; x<coc.length; x++){ // one last check of each script
		eval(coc[x]);
	}
}

// duplicate the Moodle function setfocus () here to show it to the eventhandler function for MSIE
function setfocus() { if(document.login) document.login.password.focus(); }

// not needed if you already have your own onload function or if you just want to use onload.  Just call the "zelph_stopIt" function however you want to.

xAddEventListener(window, 'load', zelph_stopIt);

