function setActiveStyleSheet(title){
  var i, a, main;
  var listo = 0;
  var index_pref=0;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
      // Localizar el css por defecto 
      if (a.getAttribute("title")=="2.- Tamanyo de letra mediano") index_pref=i;
      if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("title") != title) {
         a.disabled = true;
         if(a.getAttribute("title") == title){
                listo = 1;
                a.disabled = false;
          }
      }
  }
  
  if(listo == 0) {    
     document.getElementsByTagName("link")[index_pref].disabled = false;
  }
  
  var title_new = getActiveStyleSheet();
  createCookie("style", title_new, 365);
  
}

function setActiveStyleSheet2(title, randomize){ //Main stylesheet switcher function. Second parameter if defined causes a random alternate stylesheet (including none) to be enabled
var i, cacheobj, altsheets=[""];
if(setActiveStyleSheet2.chosen)
try{
document.getElementsByTagName('head')[0].removeChild(setActiveStyleSheet2.chosen);
}catch(e){}
for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")) { //if this is an alternate stylesheet with title
cacheobj.disabled = true
altsheets.push(cacheobj) //store reference to alt stylesheets inside array
if(cacheobj.getAttribute("title") == title){ //enable alternate stylesheet with title that matches parameter
cacheobj.disabled = false //enable chosen style sheet
setActiveStyleSheet2.chosen = document.createElement('link');//cloneNode(false);
setActiveStyleSheet2.chosen.rel = 'stylesheet';
setActiveStyleSheet2.chosen.type = 'text/css';
if(cacheobj.media)
setActiveStyleSheet2.chosen.media = cacheobj.media;
setActiveStyleSheet2.chosen.href = cacheobj.href;
document.getElementsByTagName('head')[0].appendChild(setActiveStyleSheet2.chosen);
}
}
}
if (typeof randomize!="undefined"){ //if second paramter is defined, randomly enable an alt style sheet (includes non)
var randomnumber=Math.floor(Math.random()*altsheets.length)
setActiveStyleSheet2((window.selectedtitle = altsheets[randomnumber].title));
//altsheets[randomnumber].disabled=false
}
return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : "" //if in "random" mode, return "title" of randomly enabled alt 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;
}

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 changeStyle(styleSwap,language) {
	
	estiloActual = getActiveStyleSheet();



	if (!estiloActual) {
		estiloActual = "2.- Tamanyo de letra mediano";
	}

	numEstiloActual = (estiloActual.substring(0,1)*1);
	numEstilo = numEstiloActual + styleSwap;
  if (numEstilo >-1 && numEstilo < 5) {
	if (numEstilo == 0) { 
		setActiveStyleSheet2("0.- Tamanyo de letra pequenyo", 1);
	}
	else if (numEstilo == 1) {		
		setActiveStyleSheet2("1.- Tamanyo de letra estandar", 1);
	}
	else if (numEstilo == 2) {
		setActiveStyleSheet2("2.- Tamanyo de letra mediano", 1);
	}
	else if (numEstilo == 3) {
		setActiveStyleSheet2("3.- Tamanyo de letra grande", 1);
	}
	else if (numEstilo == 4) {
		setActiveStyleSheet2("4.- Tamanyo de letra muy grande", 1);
	}

  }
}

