
function setActiveStyleSheet(title, cat) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("cat")==cat && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("cat")==cat &&  a.getAttribute("title") == title) a.disabled = false;
    }
  }

  //check img tag whether it needs to be changed

  var imgsrc;
  
  if (cat == "color") {
      for (i = 0; (a = document.getElementsByTagName("img")[i]); i++) {
          if (a.getAttribute("src")) {
              if (title == "color_bl") {
                  imgsrc = a.getAttribute("src").replace("bl_", "bl_");
                  a.setAttribute("src", imgsrc);

                  imgsrc = a.getAttribute("src").replace("br_", "bl_");
                  a.setAttribute("src", imgsrc);

                  imgsrc = a.getAttribute("src").replace("g_", "bl_");
                  a.setAttribute("src", imgsrc);
              }
              if (title == "color_br") {
                  imgsrc = a.getAttribute("src").replace("bl_", "br_");
                  a.setAttribute("src", imgsrc);

                  imgsrc = a.getAttribute("src").replace("br_", "br_");
                  a.setAttribute("src", imgsrc);

                  imgsrc = a.getAttribute("src").replace("g_", "br_");
                  a.setAttribute("src", imgsrc);
              }
              if (title == "color_g") {
                  imgsrc = a.getAttribute("src").replace("bl_", "g_");
                  a.setAttribute("src", imgsrc);

                  imgsrc = a.getAttribute("src").replace("br_", "g_");
                  a.setAttribute("src", imgsrc);

                  imgsrc = a.getAttribute("src").replace("g_", "g_");
                  a.setAttribute("src", imgsrc);
              }
          }
      }
      
      for (i = 0; (a = document.getElementsByTagName("table")[i]); i++) {
          if (a.getAttribute("background")) {
              if (title == "color_bl") {
                  imgsrc = a.getAttribute("background").replace("bl_", "bl_");
                  a.setAttribute("background", imgsrc);

                  imgsrc = a.getAttribute("background").replace("br_", "bl_");
                  a.setAttribute("background", imgsrc);

                  imgsrc = a.getAttribute("background").replace("g_", "bl_");
                  a.setAttribute("background", imgsrc);
              }
              if (title == "color_br") {
                  imgsrc = a.getAttribute("background").replace("bl_", "br_");
                  a.setAttribute("background", imgsrc);

                  imgsrc = a.getAttribute("background").replace("br_", "br_");
                  a.setAttribute("background", imgsrc);

                  imgsrc = a.getAttribute("background").replace("g_", "br_");
                  a.setAttribute("background", imgsrc);
              }
              if (title == "color_g") {
                  imgsrc = a.getAttribute("background").replace("bl_", "g_");
                  a.setAttribute("background", imgsrc);

                  imgsrc = a.getAttribute("background").replace("br_", "g_");
                  a.setAttribute("background", imgsrc);

                  imgsrc = a.getAttribute("background").replace("g_", "g_");
                  a.setAttribute("background", imgsrc);
              }
          }
      }

  } 


//  setActiveStyle(title, cat);
}

/*
function setActiveStyle(title, cat) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("a")[i]); i++) {
    if(a.getAttribute("cat")==cat && a.getAttribute("id")) {
      if(a.getAttribute("id") == title) {
	    a.setAttribute("class", "here"); 
	//	a.disabled = false;
	  }
	  else {
	    a.setAttribute("class", ""); 
    //    a.disabled = true;
	  }
    }
  }
  for(i=0; (a = document.getElementsByTagName("select")[i]); i++) {
    if(a.getAttribute("cat")==cat) {
		var options = a.options

	    for (i=0; i < options.length; i++) {
	        if (options[i].value==title)
				options[i].selected = true;
	    }
    }
  }
}
*/

function getActiveStyleSheet(cat) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("cat")==cat && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet(cat) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("cat")==cat && 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 setAllStyleCat() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("styleItem")[i]); i++) {
		if(a.getAttribute("cat")) {
			var cookie = readCookie(a.getAttribute("cat"));
			var title = cookie ? cookie : getPreferredStyleSheet(a.getAttribute("cat"));
			setActiveStyleSheet(title, a.getAttribute("cat"));
		}
	}
}

function createAllStyleCatCookie() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("styleItem")[i]); i++) {
		if(a.getAttribute("cat")) {
			var title = getActiveStyleSheet(a.getAttribute("cat"));
			createCookie(a.getAttribute("cat"), title, 365);
		}
	}
}

//window.onload = function(e) {
//	setAllStyleCat();
  //var cookie = readCookie("style");
  //var title = cookie ? cookie : getPreferredStyleSheet();
  //setActiveStyleSheet(title);
//}

window.onunload = function(e) {
	createAllStyleCatCookie();
  //var title = getActiveStyleSheet();
  //createCookie("style", title, 365);
}
setAllStyleCat();
//var cookie = readCookie("style");
//var title = cookie ? cookie : getPreferredStyleSheet();
//setActiveStyleSheet(title);

