﻿window.onload = InitializeTimer;
// timer stuff
var secs
var timerID = null
var timerRunning = false
var delay = 1000

function InitializeTimer() {
    // Set the length of the timer, in seconds
    secs = 30;
    StopTheClock();
    StartTheTimer();
    DisplayIcon();
}

function StopTheClock() {
    if (timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer() {
    if (secs == 0) {
        StopTheClock();
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
        DisplayIcon();

    }
    else {
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}

function openSenecaChat() {

    window.open("chatuserlogin.aspx", "SenecaUserChat" + createUUID(), "width=405,height=375, resizable=no,scrollbars=no,toolbar=no,directories=no,status=no,menubar=no,copyhistory=no");
}

function createUUID() {
    // http://www.ietf.org/rfc/rfc4122.txt
    var s = [];
    var hexDigits = "0123456789ABCDEF";
    for (var i = 0; i < 32; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[12] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01

    var uuid = s.join("");
    return uuid;
}

function DisplayIcon() {
    var xmlhttp = false;
    /*@cc_on@*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    @end@*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    var url = "http://printers.senecadata.com/ChatService.asmx/ChatStatus?site=Printers Site";
    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            //alert(xmlhttp.responseText);
            var element = document.getElementById('outputChat');
            if (xmlhttp.responseText.indexOf('Online') != -1) {
                element.innerHTML = "<a href='Javascript:openSenecaChat()' ><img src='images/online.png' alt='Chat with a live rep now!' border=0/></a>";
            }
            else {
                element.innerHTML = " ";
            }

        }
    }
    xmlhttp.send(null)
}
