// JavaScript Document



//function changeSheet performs the style swap when given a title



function changeSheet(theSheet) {

  if(document.styleSheets){

    var c = document.styleSheets.length;

     for(var i=0;i<c;i++){

      if(document.styleSheets[i].title!=theSheet){

        document.styleSheets[i].disabled=true;

      }else{

        document.styleSheets[i].disabled=false;

      }

    }

  }

}





//the below functions are used to set up, write to and read the cookie file 

//which contains the title of our preferred style sheet 



function makeCookie(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 pageLoad runs when the document is loaded and 

//queries the cookie file to find the sheet title needed to swap



function pageLoad() {

  var cookie = readCookie("style");

  var title = cookie ? cookie : 'default';

  changeSheet(title);

}



//function pageUnload runs when the document is unloaded and

//creates the cookie file containing the current sheet title 



function applyChange() {	

  var title = getCurrentSheet();

  makeCookie("style", title, 365);

}



//function getCurrentSheet returns the title of the selected sheet



function getCurrentSheet() {

  for(var i=0; document.styleSheets.length; i++) {

	if(!document.styleSheets[i].disabled) {

		return document.styleSheets[i].title;

	}

  }

  return null;

}

//text and html versions
function setType() {	
  makeCookie("type", "text", 365);

}
function setType2() {	
  makeCookie("type", "html", 365);

}
//Admin Panel on/off
function setType3() {	
  makeCookie("debug", "no", 31536000);

}
function setType4() {	
  makeCookie("debug", "yes", 31536000);

}

