
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 (
	'_scripts/osgreen.css',
	'_scripts/osbrown.css',
	'_scripts/osdblue.css',
	'_scripts/osgray.css',
	'_scripts/osblue.css',
	'_scripts/osorange.css',
	'_scripts/osred.css',
	'_scripts/ospurple.css',
	'_scripts/oschoc.css',
	'_scripts/osblack.css' );

   // Names to be displayed in the list box:
   var cssNames = new Array (
	'Green',
	'Brown',
	'Whale Blue',
	'Gray',
	'Blue',
	'Orange',
	'Red',
	'Purple',
	'Chocolate',
	'Black/White'
   );

   //Write the top of the form 
   document.writeln('<p class="nav"><form class="nav" onSubmit="return false">');
   document.writeln(message);
   // document.writeln('<a href="http://www.oldsherwoodians.com">' + message + '</a> ');
   document.writeln('<select name="css" onChange="logcss(this.options[this.selectedIndex].value);">');

   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 value=' + cssURLs[i] + selectedFlag + '>' + cssNames[i]);
   }

   document.writeln('</select></form></p>');

}



function logcss(mycss) {
   // var today = new Date()
   // var expires = new Date()
   // expires.setTime(today.getTime() + 1000*60*60*24*7)
   setCookie("OSCSSPicker", 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("This doesn\'t work unless you let us use cookies.");
}


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='_scripts/osgreen.css';
  } else {
     var defaultcss='_scripts/osgreen.css';
  }

  cookiecss = getCookie("OSCSSPicker");

  if ( cookiecss != "NOSHEET" ){
	  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 cssScriptLoadedOKh = true;
