/*CHAPTER 4 CODE BEGINS HERE*/

/* Browser Detection Script begins here. */
/* The variable theDOM1 will be true for modern browsers. */
var theDOM1 = (document.getElementById) ? true : false;

/* theApp will contain the browser name */
var theApp = navigator.appName.toLowerCase();

/* UA (user agent) contains detailed browser info. For example,
UA for Internet Explorer on Mac would be 'mozilla/4.0
(compatible; msie 5.0; mac_powerpc)' */
var UA = navigator.userAgent.toLowerCase();

/* variables for the two major browsers in existence today. */
var isIE = (UA.indexOf('msie') >= 0) ? true : false;
var isNS = (UA.indexOf('mozilla') >= 0) ? true : false;

/* 'compatible' text string is only in non-Netscape browsers */
if (UA.indexOf('compatible')>0){
    isNS = false;
}

/* platform */
var thePlatform = navigator.platform.toLowerCase();
var isMAC = (UA.indexOf('mac') >= 0) ? true : false;
var isWIN = (UA.indexOf('win') >= 0) ? true : false;

/* Most UNIX users use X-Windows so this detects UNIX most of
the time.*/
var isUNIX = (UA.indexOf('x11') >= 0) ? true : false;

/* browser version */
var version = navigator.appVersion;
var isMajor = parseInt( version );

/* Internet Explorer version 5 on the Mac reports itself as
version 4. This code corrects the problem. */
if(isIE && isMAC) {
    if(UA.indexOf("msie 5")) {
        isMajor = 5;
        var stringLoc = UA.indexOf("msie 5");
        version = UA.substring(stringLoc + 5, stringLoc + 8);
    }
}
/* Internet Explorer version 6 on Windows reports itself as
version 4. This code corrects the problem. */
if(isIE && isWIN) {
    if(UA.indexOf("msie 6")) {
        isMajor = 6;
        var stringLoc = UA.indexOf("msie 6");
        version = UA.substring(stringLoc + 5, stringLoc + 8);
    }
    if(UA.indexOf("msie 5.5")) {
        isMajor = 5;
        var stringLoc = UA.indexOf("msie 5.5");
        version = UA.substring(stringLoc + 5, stringLoc + 8);
    }
}

/* Netscape 6 reports itself as version 5 on all platforms.
This code corrects the problem. */
if(isNS && isMajor>4) {
    if(UA.indexOf("netscape6")) {
        isMajor = 6;
        var stringLoc = UA.indexOf("netscape6");
        version = UA.substring(stringLoc + 10, stringLoc + 14);
    }
}

var isMinor = parseFloat( version );

/* a function to report browser info */
function getBrowserInfo(){
    var temp="<p>";
    temp += "User Agent: " + UA + "<br>";
    temp += "Platform: " + thePlatform + "<br>";
    temp += "Macintosh: " + isMAC + "<br>";
    temp += "Windows: " + isWIN + "<br>";
    temp += "Application: " + theApp + "<br>";
    temp += "Version: " + version + "<br>";
    temp += "Netscape: " + isNS + "<br>";
    temp += "Internet Explorer: " + isIE + "<br>";
    temp += "Major Version: " + isMajor + "<br>";
    temp += "Full Version: " + isMinor + "<br>";
    temp += "<br>";

    if (theDOM1){
        temp += "You appear to have a modern browser.<br>";
        temp += "Netscape 6, IE 6, or IE5Mac are recommended.";
    }else{
        temp += "Alert! Your browser is obsolete.<br>";
        temp += "You may enjoy the Web more if you upgrade."; 
    }
    temp +="<\/p>";
    return temp;
}/* End of browser detection code */

/* Convert object name string or object reference
into a valid object reference */
function getObj(elementID){
    if (typeof elementID == "string") {
        return document.getElementById(elementID);
    }else{
        return elementID;
    }
}/* Object Motion and Position Scripts */

/*This function places a positionable object (obj) in 
three dimensions (x,y, and z).*/
function shiftTo(obj,x,y,z){
    var newObj = getObj(obj)

    newObj.style.left = x + "px";
    newObj.style.top = y + "px";
    newObj.style.zIndex = z;
}

/*This function gets the x coordinate of a positionable object.*/
function getObjX(obj){
    return parseInt(getObj(obj).style.left);
}

/*This function gets the y coordinate of a positionable object.*/
function getObjY(obj){
    return parseInt(getObj(obj).style.top);
}

/*This function gets the z-index of a positionable object.*/
function getObjZ(obj){
    return parseInt(getObj(obj).style.zIndex);
}

/*The emptyNode() function loops through the array of child
nodes and removes each one.*/
function emptyNode(elementID){
    var theNode = getObj(elementID);
    for (i=0;i<theNode.childNodes.length;i++){
        theNode.removeChild(theNode.childNodes[i]);
    }
}

/*This function gets the available width of the window.*/
function getAvailableWidth(){
    var theWidth=null;
    /*Netscape uses window.innerWidth */
    if (window.innerWidth) {
        theWidth = window.innerWidth;
    }/*IE uses document.body.clientWidth */
    if (document.body.clientWidth) {
        theWidth = document.body.clientWidth;
    }
    
    return theWidth;
}

/*This function gets the available height of the window.
IE6.0 on Windows has a bug and returns null.*/
function getAvailableHeight(){
    var theHeight = null;
    /*Netscape uses window.innerHeight */
    if (window.innerHeight) {
        theHeight = window.innerHeight;
    }
    /*IE uses document.body.clientHeight */

    if (document.body.clientHeight) {
        theHeight = document.body.clientHeight;
    }
    return theHeight;
}

/*This function sets the total height and width of the window.*/
function setWindowSize(w,h){
    window.resizeTo(w,h);
}

/*This function sets the size of the window to cover all of the screen.*/
function maximizeWindow(){
    window.moveTo(0,0);
    window.resizeTo(screen.availWidth,screen.availHeight);
}

/*END OF CHAPTER 4 CODE*/

/*CHAPTER 5 CODE BEGINS HERE.*/

/* Redirects visitors who are using outdated browsers.*/
function checkDOM(newlocation){
    if (!theDOM1){
        window.location.replace(newlocation);
    }
}

/* This function changes the cursor.  The second argument is optional. */
function setCursor(cursortype,thisobj){
    if (UA.indexOf("msie 5")>=0){
        if (cursortype == 'pointer') { cursortype='hand'; }
    } 
    if (thisobj==null){
        document.body.style.cursor = cursortype;
    }else{
        getObj(thisobj).style.cursor = cursortype;
    }
}

/*Set the background color of an object*/
function setBackground(thisobj, color){
    getObj(thisobj).style.background = color;
}

/*Set the text color of an object*/
function setColor(thisobj, color){
    getObj(thisobj).style.color = color;
}

/*Setting the visibility of an object*/
function setVisibility(obj,vis){
    var theObj = getObj(obj);
    if (vis == true || vis=='visible' || vis=='y'){
        theObj.style.visibility = "visible";
    }else{
        theObj.style.visibility = "hidden";
    }
}

/*Getting the visibility of an object*/
function isVisible(obj) {
    var theObj = getObj(obj);
    return theObj.style.visibility;
}

/*Setting the display of an object*/
function setDisplay(obj,dis){
    var theObj = getObj(obj);
    if (dis==true || dis=='block' || dis=='y'){
        theObj.style.display = "block";
    }else{
        if (dis==false || dis=='n'){
            theObj.style.display = "none";
        }else{
            theObj.style.display = dis;
        }
    }
}

/*Getting the display of an object*/
function isDisplayed(obj) {
    var theObj = getObj(obj);
    return theObj.style.display;
}

/*Set the clip region of an object*/
function setClip(obj,top,right,bottom,left){
    var theObj = getObj(obj);
    var r = 'rect('+top+'px,'+right+'px,'+bottom+'px,'+left+'px)';
    theObj.style.clip = r;
}

/*END OF CHAPTER 5 CODE*/


/*CHAPTER 6 CODE BEGINS HERE*/

function showxy(evt){
    if (window.event){ evt = window.event; }
    if (evt){
        var pos = evt.clientX + ", " + evt.clientY;
        window.status=pos;
    }
}

function checkModel(evt){
    if (window.event){
        alert("This browser uses the IE4+ event model.");
    }else{
        if (evt){ 
            alert("This browser uses the W3C/Netscape6 event model.");
        }
    }
}

/*Getting the width of an object*/
function getWidth(obj){
    var theObj = getObj(obj);

    if (theObj.clientWidth){
        return parseInt(theObj.clientWidth);
    }
    if (theObj.offsetWidth){
        return parseInt(theObj.offsetWidth);
    }
}

/*Getting the Height of an object*/
function getHeight(obj){
    var theObj = getObj(obj);

    if (theObj.clientHeight){
        return parseInt(theObj.clientHeight);
    }
    
    if (theObj.offsetHeight){
        return parseInt(theObj.offsetHeight);
    }
}

/*Drag and Drop Script for positioned divs and spans*/
/*Place this in your codelibrary.js file for future use.*/
var maxZdrag = document.getElementsByTagName("div").length;
maxZdrag += document.getElementsByTagName("span").length;

var dragElem=null;
var dragging=false;

function setDrag(evt,elementID){
    dragElem=getObj(elementID);
    startDrag();
}

function startDrag() {
    dragging=true;
    document.onmousemove=dragObj;
    document.onmouseup=dropObj;
}

function dragObj(evt) {
    if (window.event){ evt = window.event; }
    if (evt){
        var x = evt.clientX - parseInt(getWidth(dragElem)/2);
        var y = evt.clientY - parseInt(getHeight(dragElem)/2);
        var z = maxZdrag++;
        shiftTo(dragElem,x,y,z);
        var pos = "drag coordinates are: " + x + ", " + y + ", " + z;
        window.status=pos;
    }
    
    return false;
}

function dropObj() {
    dragging=false;
    document.onmousemove=null;
    document.onmouseup=null;
    window.status="";
}
/*End Drag and Drop Script*/

/*End of Chapter 6 Code*/

/*Chapter 7 code*/
/*Utility function to convert decimal to hexadecimal*/
function getHex(base10){
    if (base10>255){base10=255;}
    var hexSet = "0123456789abcdef";
    var theMod = base10 % 16; 
    var theRemainder = (base10 - theMod)/16;
    var hexNum = hexSet.charAt(theMod) + "";

    hexNum += hexSet.charAt(theRemainder);

    return hexNum;
}
/*End of Chapter 7 code*/

/* EXTRA FUNCTIONS YOU MAY FIND USEFUL IN YOUR WORK.*/
/* Many of these functions are from The Web Wizard's Guide to JavaScript.*/
/*Setting the width of an object*/
function setWidth(obj,w){
    var theObj = getObj(obj);
    theObj.style.width = w + "px";
}

/*Setting the height of an object*/
function setHeight(obj,h){
    var theObj = getObj(obj);
    theObj.style.height = h + "px";
}

/* Code to manipulate values from form objects */
function getRadioValue(formname,radioname){
    var theRadioButtons = document[formname][radioname];
    for (i=0;i<theRadioButtons.length;i++){
        if (theRadioButtons[i].checked){
            return theRadioButtons[i].value;
        }
    }
}

function showRadioValue(formname,radioname,thevalue){
    var theRadioButtons = document[formname][radioname];
    for (i=0;i<theRadioButtons.length;i++){
        var temp = theRadioButtons[i].value;
        if (temp == thevalue){
            theRadioButtons[i].checked = true;
        }
    } 
}

function getSelectValue(formname,selectname){
    var theMenu = document[formname][selectname];
    var selecteditem = theMenu.selectedIndex;
    return theMenu.options[selecteditem].value;
}

function showSelectValue(formname,selectname,thevalue){
    var theMenu = document[formname][selectname];
    for (i=0;i<theMenu.options.length;i++){
        var temp = theMenu.options[i].value;
        if (temp == thevalue){
            theMenu.selectedIndex = i;
        }
    } 
}
/* End of code to manipulate values from form objects */

/* Essential Arrays for pages with calendars */
var theDays = new Array();
theDays[0]="Sunday";
theDays[1]="Monday";
theDays[2]="Tuesday";
theDays[3]="Wednesday";
theDays[4]="Thursday";
theDays[5]="Friday";
theDays[6]="Saturday";

var theMonths = new Array();
theMonths[0]="January";
theMonths[1]="February";
theMonths[2]="March";
theMonths[3]="April";
theMonths[4]="May";
theMonths[5]="June";
theMonths[6]="July";
theMonths[7]="August";
theMonths[8]="September";
theMonths[9]="October";
theMonths[10]="November";
theMonths[11]="December";
/* End of date code */

/* Beginning of Cookie Code Based on code by Bill Dortch
of hidaho designs who has generously placed it in the public 
domain.*/
function SetCookie(name,value,expires,path,domain,secure){
    var temp = name + "=" + escape(value);
    if (expires){
        temp += "; expires=" + expires.toGMTString();
    }
    if (path){
        temp += "; path=" + path;
    }
    if (domain){
        temp += "; domain=" + domain;
    }
    if (secure){
        temp += "; secure";
    }
    document.cookie = temp;
}

function GetCookie(name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i,j) == arg){
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    
    return null;
}

function getCookieVal(offset){
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1){
        endstr = document.cookie.length;
    }
    
    return unescape(document.cookie.substring(offset,endstr));
}

function DeleteCookie (name,path,domain) {
    if (GetCookie(name)) {
        var temp = name + "=";
        temp += ((path) ? "; path=" + path : "");
        temp += ((domain) ? "; domain=" + domain : "");
        temp += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        document.cookie = temp;
    }
}
/* End of Cookie Code */

/* converts days to milliseconds */
function daysToMS(days){
    return days * 24 * 60 * 60 * 1000;
}

/* converts weeks to milliseconds */
function weeksToMS(weeks){
    return weeks * 7 * 24 * 60 * 60 * 1000;
}

/* converts years to milliseconds */
function yearsToMS(years){
    return years * 365.25 * 24 * 60 * 60 * 1000;
}

/* code to open a new window with various features */
var myWindow = null;
function openWin(url,targetname,W,H,L,T,thefeatures) {
    var params = "";
    var nofeatures = "toolbar=0,location=0,directories=0,status=0,";
    nofeatures += "menubar=0,scrollbars=0,resizable=0,copyhistory=0";
    var basicfeatures = "scrollbars=1,resizable=1,menubar=1 ";
    var morefeatures = "toolbar=1,location=1,directories=1,";
    morefeatures += "status=1,copyhistory=1";
    var dimensions = "width=" + W + ",height=" + H;

    /* The placement variable contains values for left/top and
        screenX/screenY. Use both to ensure compatibility with all
        browsers. */
    var placement = "left=" + L + ",top=" + T;
    placement += ",screenX=" + L + ",screenY=" + T;
    /* The switch control structure makes decisions that
        depend on the value of a variable. In this case the value of the
        parameter variable thefeatures determines the value of the
        variable params. */
    switch (thefeatures){
        case "none":
            params += nofeatures;
            break;
        case "basic":
            params += basicfeatures;
            break;
        case "full":
            params += basicfeatures + "," + morefeatures;
            break;
        default:
            params += thefeatures;
    }
    
    /* Adds the dimensions and placement info to the params variable. */
    params += "," + dimensions + "," + placement;
    /* The window.open() method creates myWindow. */
    myWindow = window.open(url,targetname,params);
}

function closeWin(){
    if (myWindow != null){
        myWindow.close();
        myWindow = null; 
    }
}
/* End of openWin() and closeWin() functions */

/* End of codelibrary.js */

