/* fonctions Java-script */
var undefined; /* make suree Java knows the undefined value*/
/* shut error reporting to users (return true)*/
onerror=function(){return true;} 
var newWindow, newWindowSound, albumWindow; //references to pop-ups

   function getObj(elemRef) {
    //convert elemRef into a valid object reference
    if (typeof elemRef == "string") {
     if (document.getElementById) {
      return document.getElementById(elemRef);
     } else {
      if (document.all) {
       return document.all[elemRef];
      } else
       return null;
     }
    } else
     return elemRef;
   } //
	
	function checkCR (evt){
		// disable carriage return (in input text but not textarea) to avoid submitting a form without clicking the submit button
		 var evt = evt || window.event;
		 var code = evt.charCode || evt.keyCode;
		 var node = evt.target || evt.srcElement;
		 if (code == 13 && node.type == "text") { return false; }
		} 


function setSelectionRange(event) {
	//The caret should be at the start of a textarea in a form. 
	
var e = event || window.event;
var input = e.target || e.srcElement;

if (input.createTextRange) {
//IE case
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', 0);
range.moveStart('character', 0);
range.select();
}
else  {
//FireFox and Opera case
input.focus();
input.selectionStart = 0;
input.selectionEnd = 0;
}
}

function registerFocusTextarea(){
//Register the set cursor position handler on textarea elements in an unobstrusive way. Setting directly the onfocus attribute  doesn't work in Opera but this unobstrusive solution works.

	var textareaElts = document.getElementsByTagName("textarea");
	for (var i=0; i<textareaElts.length; i++){
		textareaElts[i].onfocus= setSelectionRange;
	}
}
 
function hideh(){//stub
return true;}

//for vertical menu
function hideall() {
  for (var i = 1; i<=10; i++) {
		if (getObj('smenuv'+i)) {
		getObj('smenuv'+i).style.display='none';
		getObj('icon'+i).innerHTML = "<img src='imagenes/plusDirectoryGreen.png' />";
		}
	}
}

//toogle : hide and show of a vertical submenu

function showhide(i) {
//careful to the interference  with onfocus and on click
 var el  = getObj('smenuv'+i);
 var il = getObj('icon'+i);
 
 if (el){
 if (el.style.display == "none"){
	   el.style.display = 'block';
  	 il.innerHTML="<img src='imagenes/minusDirectoryGreen.png' />";
 } else  	{
     el.style.display = 'none'; 
		 il.innerHTML="<img src='imagenes/plusDirectoryGreen.png' />";
 }
 }
return false;
}


/*style switcher, there are three types of style sheets:
 -permanent. These have no titles and are always loaded, they are not of concern to us here.
 -preferred. These have a title and are enabled (active) by default when the page is loaded. There may be many sheets with a same title in which case they are treated as a group. (Here, we do not use preferred stylesheet(s).)
 -alternate. These have a title as well and the alternate attribute. They can be enabled (made active) on user demand and are a substitute for the preferred stylesheet(s).
*/
function setActiveStyleSheet(title) {
try{
/* if title is not empty disable all the sheets but the one with  the given title , otherwise disable all the sheets*/
  var i, a;
	//disable on IE (pb on some versions of IE 7?
	// if (navigator.appName.indexOf ("Microsoft")!= -1) return; 
    for(i=0; (i!= document.getElementsByTagName("link").length); i++) {
    a = document.getElementsByTagName("link")[i];
    if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true; //doen't work on Opera, use stylesheets instead!
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}
catch(ex) {return;}
}

function getActiveStyleSheet() {
try{
  var i, a;
    for(i=0; (i!= document.getElementsByTagName("link").length); i++) {
    a = document.getElementsByTagName("link")[i];
    if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) { return a.getAttribute("title");}
  }
  return '';
}
catch(ex) {return '';}
}

function createCookie(name,value) {
  /* cookies have no expire attribute and by default will expire at the end of the user session*/
  document.cookie = name+"="+value+"; path=/";
}

function readCookie(name) {
try{
  var nameEQ = name + "=";
  var ca = document.cookie;
	if (!ca) return '';
	ca = ca.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 '';
}
catch (ex) {return '';}
}

function writeCookies(){
  var title = getActiveStyleSheet();
	createCookie("style", title);
}

//when loading a page: read cookies, hide all submenus but the submenu of the page
function loadWindow(){
function loadingCookies(){
var title = readCookie("style");
if (title) setActiveStyleSheet(title);
}
var p= document.getElementsByTagName("body")[0];
var l;
hideall(); // hide vertical menu
// show submenu of the page assuming the last character of pageid is the number of the vertical submenu
l= p.id.charAt(p.id.length - 1);
showhide (l); 
loadingCookies();
}

function addOnLoadEvent(func) {
var oldQueue = (window.onload) ? window.onload : function( ) {};
window.onload = function( ) {
func( );
oldQueue( );
}
}//addOnLoadEvent(func)

function toggleToc(){
	// toggle the table of contents: show and hide
	var tocElt = getObj("tocContents");
	var tocButton = getObj("tocButton");
	if (tocElt.style.display == 'none'){
	tocElt.style.display = 'block';
	tocButton.innerHTML = 'esconder';
	}
	else{
	tocElt.style.display = 'none';
	tocButton.innerHTML = 'mostrar';
	}
}// toogleToc

/* functions for enlarging pictures at the bottom of the current window. This assumes the stactic markup has been introduced at the bottom of the page*/

function onLoadImage() {
	var eltImage = getObj("divImage");
	eltImage.style.display = "block";
	window.location.hash= "#enlarge";
}
function enlarge(picture, caption){
	if (window.opera) return true; /*Opera has a bug on returning from a name anchor*/
	var imagElt = getObj("bigImage");
	var descElt = getObj("caption");
	descElt.innerHTML = caption;
	imagElt.onload = onLoadImage;
	imagElt.src = picture;
	return false;
}
function backEnlarge(){
	var eltImage = getObj("divImage");
	eltImage.style.display = "none";
	history.back();
}

addOnLoadEvent(loadWindow);
