// Cookie name = 'A5dunstable'

var selectedCSS;

writeSheet();

function writeFormOrNot() {
   if (testForCookies() == true) {
      writeForm();
   }

}

function writeForm(message) {
//Writes the form to allow the user to set a cookie with a new CSS and reload the page
//is passed the text to put before the list box that allows the user to select.

   // URLs of possible CSS
   var cssURLs = new Array (
	"./../A5dunstable/mainstyle.css",
	"../A5dunstable/mainstyle_nocolour.css",
	"../A5dunstable/mainstyle_highcontrast.css"
 );

   // Names to be displayed in the list box:
   var cssNames = new Array (
	"Original",
	"No Colour",
	"High Contrast"
   );

   // Styling drop-down:
   var cssDD_Style = new Array (
	"Original",
	"No_Colour",
	"High_Contrast"
   );

   //Write the top of the form 
  // document.writeln('<form onSubmit="return false">');
   document.writeln(message);
   // document.writeln('<a href="http://www.edochan.com/programming/layout.htm">' + message + '</a> ');
   document.write('<select id=\"changer\" name=\"css\" onChange=\"logcss(this.options[this.selectedIndex].value); blur();\">');

   var i;
   var selectedFlag;

   //Write each CSS in the list box
   for (i=0; i<cssNames.length; i++) {

      //The CSS currently selected needs the word "selected" written in
      if (cssURLs[i] == selectedCSS) {
          selectedFlag = ' selected ';
      } else {
	  selectedFlag = '';
      }

      document.writeln('<option class=\"' + cssDD_Style[i] +'\" value=' + cssURLs[i] + selectedFlag + '>' + cssNames[i]);

   }

//   document.writeln('</select></form>');
   document.writeln('</select>');
}



function logcss(mycss) {
   // var today = new Date()
   // var expires = new Date()
   // expires.setTime(today.getTime() + 1000*60*60*24*7)
   setCookie("A5dunstable", mycss, null, '/')
   if ( document.cookie.length > 0)
      { history.go(0) }
   else 
      { sorryNoCookies(); }
}


function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


function getCookie(Name) 
{   
	var search = Name + "="   
	if (document.cookie.length > 0) { // if there are any cookies      
	offset = document.cookie.indexOf(search)       
	if (offset != -1) { // if cookie exists          
		offset += search.length          
		// set index of beginning of value         
		end = document.cookie.indexOf(";", offset)          
		// set index of end of cookie value         
		if (end == -1)             
			end = document.cookie.length         
		return unescape(document.cookie.substring(offset, end))
		}
	}
}

function sorryNoCookies()
{
//document.writeln("Not Present!");
}


function testForCookies() {
//Try to read a cookie - if there aren't any, try setting one then try reading that.

   if (document.cookie.length > 0) { 
      return true; 
   } else {
      setCookie ("testCookie", "testingForCookies");
         if (document.cookie.length > 0) { 
         return true; 
         } else {
         return false;
      }
   }
}



function writeSheet() {
//Reads the CSS from the cookie if there is one and writes it into the head of the page.

  var cookiecss;

  //Netscape makes a mess of my normal default CSS, so I'll give Netscape users a different one that works OK.
  if (browserIsCrap()) {
     var defaultcss="../A5dunstable/mainstyle.css";
  } else {
     var defaultcss="../A5dunstable/mainstyle.css";
  }

  cookiecss = getCookie("A5dunstable");

  if ( cookiecss != "../A5dunstable/mainstyle.css" ){
	  if ( cookiecss != null ) {
	     selectedCSS = cookiecss;
       	  } else {
	     selectedCSS = defaultcss;
	  }
  }

document.write("<LINK REL=STYLESHEET HREF=" + selectedCSS + " TYPE=text/css>");
   
}


function isNav() {
//Some very simple browser detection...

   var browser = navigator.appName;
   var ver = navigator.appVersion;
   return ((browser == "Netscape") && (ver < 6));

}


function browserIsCrap() {

   var browser = navigator.appName;

   if ((browser == "Netscape") && (navigator.appVersion.substring(0,1) < "5")) {
      //alert(navigator.appVersion);

      return true;
   } else {
      return false;
   }
}


//Just to say this file has finished loading OK. I don't want to call subroutines in it unless it has...
var cssScriptLoadedOK = true;

