//----------------------------------------------------------
//              _   _        _
//  __ ___  ___| |_(_)___   (_)___
// / _/ _ \/ _ \ / / / -_)_ | (_-<
// \__\___/\___/_\_\_\___(_)/ /__/
//                        |__/
//
//----------------------------------------------------------
// File      : cookie.js
// Author    : Richard Lewis
// Project   : /mnt/web/localhost/htdocs/js/cookies/
// Syntax    : javascript
// Date      : Fri 18 Jan 2008
// Copyright : Richard Lewis 2008
//----------------------------------------------------------
// cookie.js - cookie functions
//----------------------------------------------------------

function goCookie() {
    // onload

    lastModified();
    setCookie();
    setCookieInterface();
    readCookies();
}
//----------------------------------------------------------

function setCookie() {
    // set cookies

    var exp = new Date();

    exp.setDate(exp.getDate() + 2);

    var cookie1 =
        "cookie1=" +
            escape("test1 cookie")     + "; " + // data
        "expires=" + exp.toGMTString() + "; " + // expires
        "path=/cookies";                        // path

    document.cookie = cookie1;

    var cookie2 =
        "cookie2=" +
            escape("test2 cookie")     + "; " + // data
        "expires=" + exp.toGMTString() + "; " + // expires
        "path=/cookies";                        // path

    document.cookie = cookie2;
}
//----------------------------------------------------------

function cookieSet(
    name, value, lifespan, path, domain, secure) {
    // set an arbitary cookie

    if (name == null || name == "")
        return;

    if (value == null || value == "")
        return;

    if (lifespan == null || isNaN(parseInt(lifespan)))
        lifespan = "";

    var cookieStr = name + "=" + escape(value) + "; ";

    if (lifespan != "") {
        var exp = new Date();

        exp.setDate(exp.getDate() + lifespan);

        cookieStr +=
            "expires=" + exp.toGMTString() + "; ";
    }

    if (path)
        cookieStr += "path=" + escape(path) + "; ";

    if (domain)
        cookieStr += "domain=" + escape(domain) + "; ";

    if (secure)
        cookieStr += "secure";

    document.cookie = cookieStr;
}
//----------------------------------------------------------

function formCookie() {
    // read form cookie information

    var form = document.getElementById('setCookieForm');

    var name     = form.name.value;
    var path     = form.path.value;
    var value    = form.value.value;
    var domain   = form.domain.value;
    var lifespan = form.lifespan.value;

    var secure = false;

    if (form.secure.checked)
        secure = true;

    cookieSet(name, value, lifespan, path, domain, secure);

    readCookies();

    return false;
}
//----------------------------------------------------------

function setCookieInterface() {
    // display a set cookie interface

    var out  = document.getElementById('setcookieOut');
    var form = document.createElement('form');

    form.id  = "setCookieForm";

    form.onsubmit = formCookie;

    var nameTxt     = document.createTextNode('name');
    var pathTxt     = document.createTextNode('path');
    var valueTxt    = document.createTextNode('value');
    var domainTxt   = document.createTextNode('domain');
    var secureTxt   = document.createTextNode('secure');
    var lifespanTxt = document.createTextNode('lifespan');

    var nameInp     = document.createElement('input');
    var pathInp     = document.createElement('input');
    var valueInp    = document.createElement('input');
    var resetInp    = document.createElement('input');
    var domainInp   = document.createElement('input');
    var submitInp   = document.createElement('input');
    var secureInp   = document.createElement('input');
    var lifespanInp = document.createElement('input');

    nameInp.name     = "name";
    pathInp.name     = "path";
    valueInp.name    = "value";
    domainInp.name   = "domain";
    secureInp.name   = "secure";
    lifespanInp.name = "lifespan";

    resetInp.type  = "reset";
    submitInp.type = "submit";
    secureInp.type = "checkbox";

    submitInp.style.cursor = "pointer";
    resetInp.style.cursor  = "pointer";

    submitInp.value = "Set Cookie"

    form.appendChild(nameTxt);
    form.appendChild(br());
    form.appendChild(nameInp);
    form.appendChild(br());
    form.appendChild(nbsp(1));
    form.appendChild(br());

    form.appendChild(valueTxt);
    form.appendChild(br());
    form.appendChild(valueInp);
    form.appendChild(br());
    form.appendChild(nbsp(1));
    form.appendChild(br());

    form.appendChild(lifespanTxt);
    form.appendChild(br());
    form.appendChild(lifespanInp);
    form.appendChild(br());
    form.appendChild(nbsp(1));
    form.appendChild(br());

    form.appendChild(pathTxt);
    form.appendChild(br());
    form.appendChild(pathInp);
    form.appendChild(br());
    form.appendChild(nbsp(1));
    form.appendChild(br());

    form.appendChild(domainTxt);
    form.appendChild(br());
    form.appendChild(domainInp);
    form.appendChild(br());
    form.appendChild(nbsp(1));
    form.appendChild(br());

    form.appendChild(secureTxt);
    form.appendChild(br());
    form.appendChild(secureInp);
    form.appendChild(br());
    form.appendChild(nbsp(1));
    form.appendChild(br());

    form.appendChild(resetInp);
    form.appendChild(br());
    form.appendChild(nbsp(1));
    form.appendChild(br());

    form.appendChild(submitInp);
    form.appendChild(br());
    form.appendChild(nbsp(1));
    form.appendChild(br());

    out.appendChild(form);
}
//----------------------------------------------------------

function readCookies() {
    // read and display browser cookies

    var out = document.getElementById('cookiesOut');
    var p   = document.createElement('p');

    var txt1 = document.createTextNode(document.cookie);

    p.appendChild(txt1);
    p.appendChild(document.createElement('br'));

    var cookies = new Array();
    cookies     = document.cookie.toString().split('; ');

    for (var k =0; k < cookies.length; ++k) {

        var pair = new Array();
        pair     = cookies[k].split("=");

        var txt = document.createTextNode(
            pair[0] + " : " + unescape(pair[1]));

        p.appendChild(txt);
        p.appendChild(document.createElement('br'));
    }

    clrEle(out);

    out.onclick      = readCookies;

    out.style.cursor = "pointer";

    out.appendChild(document.createTextNode(
        "Click to Re-read Cookies"));
    out.appendChild(br());

    out.appendChild(p);
}
//----------------------------------------------------------

function lastModified() {
    // add last modified date to page

    var modified = getEle('lastmodified');

    var now = new Date();
    var mod = new Date(document.lastModified);
    var dif = Math.floor(
        (now.getTime()  -
         mod.getTime()) /
        (1000 * 60 * 60 * 24));

    if (dif < 0) dif = 0;

    var day   = getDay(mod.getDay());
    var mday  = mod.getDate();
    var mord  = ord(mday);
    var month = getMonth(mod.getMonth());
    var year  = getYear(mod.getYear());

    var lastMod1 =
        "Last Modified : " +
        day  + ", "        +
        mday;
    modified.appendChild(crtTxt(lastMod1));

    var sup = crtEle('sup');
    sup.appendChild(crtTxt(mord));
    modified.appendChild(sup);

    var lastMod2 = " "  + month + " " + year;
    modified.appendChild(crtTxt(lastMod2));

    var lastMod3 = " (" + dif + " days ago).";
    modified.appendChild(crtTxt(lastMod3));
}
//----------------------------------------------------------

function clrEle(ele) {
    // clear element

    while(ele.firstChild)
        ele.removeChild(ele.firstChild);
}
//----------------------------------------------------------

function getDay(day) {
    // get day str

    var dayA = new Array(
        'Sun', 'Mon', 'Tue',
        'Wed', 'Thu', 'Fri', 'Sat');

    return dayA[day];
}
//----------------------------------------------------------

function getMonth(month) {
    // get month str

    var monthA = new Array(
        'Jan', 'Feb', 'Mar',
        'Apr', 'May', 'Jun',
        'Jul', 'Aug', 'Sep',
        'Oct', 'Nov', 'Dec');

    return monthA[month];
}
//----------------------------------------------------------

function getYear(year) {
    // return full year

    if (year < 2000)
        return year + 1900;

    return year;
}
//----------------------------------------------------------

function getEle(id) {
    // get document element by id

    if (document.getElementById(id))
        return document.getElementById(id);
    return false;
}
//----------------------------------------------------------

function crtEle(tag) {
    // create a document element from a tag

    if (document.createElement(tag))
        return document.createElement(tag);
    return false;
}
//----------------------------------------------------------

function crtTxt(txt) {
    // create textnode

    if (document.createTextNode)
        return document.createTextNode(txt);
    return false;
}
//----------------------------------------------------------

function nbsp(count) {
    // non breaking space

    var spc = "\u00a0";
    var spaces = new Array();

    for (var k = 0; k < count; ++k)
        spaces.push(spc);

    return crtTxt(spaces.join(""));
}
//----------------------------------------------------------

function br() {
    // return a line break

    var lb = crtEle('br');

    return lb;
}
//----------------------------------------------------------

function hr() {
    // return a hr

    var div = crtEle('div');

    div.className = "hr";

    return div
}
//----------------------------------------------------------

function ord(num) {
    // return ordinal of number

    var ordA = new Array('th', 'st', 'nd', 'rd');

    if (num >= 10 && num <= 20) i = 0;
    else i = num % 10;

    if (i > 3) i = 0;

    return ordA[i];
}
//----------------------------------------------------------

if (window.addEventListener)
    window.addEventListener("load", goCookie, false);

else if (window.attachEvent)
    window.attachEvent("onload", goCookie);
//----------------------------------------------------------

