
window.onload   = initAll;
window.onunload = unloadStyle;



function initAll() {

    /* **********  Background color selector  ********** */

    addBackgroundColorSelector();

    thisCookie = cookieVal("style");
    var title_onloadStyleSheet = (thisCookie)? thisCookie : "burntorange";
    changeBackgroundColor(title_onloadStyleSheet);

    document.getElementById("selectedbgcolor").onchange = changeBackgroundColor;

    /* **********  Setting up digital clock ********** */

    showTheTime();

}



/******************************************************************/
/******************************************************************/
/* **********  Supporting functions for Digital Clock  ********** */

function showTheTime() {

   var now = new Date();

   document.getElementById("extraDiv1").innerHTML
   =   showTheHours(   now.getHours()   )
     + showZeroFilled( now.getMinutes() )
     + showZeroFilled( now.getSeconds() )
     + ( (now.getHours()<12)? " AM" : " PM" );

   setTimeout( "showTheTime()" , 1000 ); 

}

function showTheHours(theHour) {
   return (theHour>0 && theHour<13)? theHour : (theHour==0)? 12 : theHour-12 ;
}

function showZeroFilled(inValue) { return (inValue>9)? ":"+inValue : ":0"+inValue; }



/******************************************************************************/
/******************************************************************************/
/* **********  Supporting functions for Background Color Selector  ********** */

function addBackgroundColorSelector() {

    var tempString;
    tempString  = "<select id='selectedbgcolor'>";
    tempString += "<option value='NoChange'>Background color</option>";
    tempString += "<option value='burntorange'>Burnt orange</option>";
    tempString += "<option value='gray'>Gray</option>";
    tempString += "<option value='black'>Black</option>";
    tempString += "<option value='olivegreen'>Dark olive green</option>";
    tempString += "<option value='purple'>Purple</option>";
    tempString += "</select>";

    document.getElementById("extraDiv2").innerHTML = tempString;

}


function changeBackgroundColor(inVal) {

    var thisLink;
    var linksFound = document.getElementsByTagName("link");

    var id_StyleSheetToEnable
        = (inVal)? ((typeof inVal=="string")? inVal : inVal.target.options[inVal.target.selectedIndex].value ) :
                   window.event.srcElement.id;

    if ( !(id_StyleSheetToEnable=="NoChange")  ) {
    for (var i=0; i<linksFound.length; i++) {
        thisLink = linksFound[i];
        if (  thisLink.getAttribute("rel").indexOf("style")>-1  && 
              thisLink.getAttribute("title")                        )
           {
              thisLink.disabled = true;
              if (thisLink.getAttribute("title")==id_StyleSheetToEnable) { thisLink.disabled = false; }
           }
        
    }
    }

}


function unloadStyle() {

    var expireDate = new Date();
    expireDate.setYear( 1 + expireDate.getFullYear() );
    document.cookie = "style=" + getActiveStyleSheet() + ";expires=" + expireDate.toGMTString() + ";path=/"; 

}


function getActiveStyleSheet() {

    var thisLink;
    var linksFound = document.getElementsByTagName("link");

    for (var i=0; i<linksFound.length; i++) {
        thisLink = linksFound[i];
        if (  thisLink.getAttribute("rel").indexOf("style")>-1  && 
              thisLink.getAttribute("title")                    &&
              !thisLink.disabled
           )  { return thisLink.getAttribute("title"); }
    }
    return "";

}


function setActiveStyleSheet(inVal) {

    var thisLink;
    var linksFound = document.getElementsByTagName("link");

    var id_StyleSheetToEnable
        = (inVal)? ((typeof inVal=="string")? inVal : inVal.target.id ) : window.event.srcElement.id;

    for (var i=0; i<linksFound.length; i++) {
        thisLink = linksFound[i];
        if (  thisLink.getAttribute("rel").indexOf("style")>-1  && 
              thisLink.getAttribute("title")                        )
           {
              thisLink.disabled = true;
              if (thisLink.getAttribute("title")==id_StyleSheetToEnable) { thisLink.disabled = false; }
           }
        
    }

}


function cookieVal (cookieName) {

    var thisCookie = document.cookie.split("; ");
    for (var i=0; i<thisCookie.length; i++) {
        if (cookieName==thisCookie[i].split("=")[0]) { return thisCookie[i].split("=")[1]; }
    }
    return "";

}

