﻿
(function () {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

function snowplowPageLoaded(sender, args) {
    document.cookie = "JavaScript=1; path=/";
    JSDegrade();
    if (window.navigator.standalone) { iPadApp(); }
}

function snowplowPageRequested(sender, args) {
    //lockButtons();
}

function snowplowEndRequest(sender, args) {
    // if (args.get_error() != undefined) { alert(args.get_error()); /*window.location = "/error";*/args.set_errorHandled(true); }
    if (args.get_error() != undefined) { window.location = "/error"; args.set_errorHandled(true); }
    JSDegrade();
    //unlockButtons();
}

function JSDegrade() {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) { if (inputs[i].className === 'nonJS') { inputs[i].style.display = 'none'; } }
}

function iPadApp() {
    var inputs = document.getElementsByTagName("a");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].href && inputs[i].href.indexOf("javascript:") == -1) {
            inputs[i].onclick = Function("location.href = this.href; return false;");
        } 
    }
}

function lockButtons() {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) { inputs[i].disabled = 'disabled'; }
}

function unlockButtons() {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) { inputs[i].disabled = ''; }
}

function hide(id) {
    try {
        var e = document.getElementById(id);
        e.style.display = 'none'
    } catch (ex) { }
}

function show(id) {
    try {
        var e = document.getElementById(id);
        e.style.display = ''
    } catch (ex) { }
}

function setClass(id, cls) {
    try {
        var e = document.getElementById(id); e.setAttribute('class', cls); e.setAttribute('className', cls); 
    } catch (ex) { } 
 }

function showTab(id) {
    for (i = 0; i <= 9; i++) {
        try { hide('tabcontent' + i) } catch (ex) { }
        try { setClass('tab' + i, '') } catch (ex) { }
    }
    try { show('tabcontent' + id); } catch (ex) { }
    setClass('tab' + id, 'current');
}

function setvisible(id, visible) {
    try {
        var e = document.getElementById(id);
        if (visible) { e.style.display = '' }
        else { e.style.display = 'none' }
    } catch (ex) { }
}

function enable(id) {
    try {
        var e = document.getElementById(id);
        e.disables = false
    } catch (ex) { }
}

function disable(id) {
    try {
        var e = document.getElementById(id);
        e.disabled = true
    } catch (ex) { }
}

function setstate(id, state) {
    try {
        var e = document.getElementById(id);
        e.disabled = !state
    } catch (ex) { }
}

function popupWindow(link, attribs) {
    var popupWindow = null;
    popupWindow = window.open(link, 'winPopup', attribs);
}

function getStyle(e, rule) {
    if (document.defaultView && document.defaultView.getComputedStyle) {
        return document.defaultView.getComputedStyle(e, "").getPropertyValue(rule);
    }
    else if (e.currentStyle) {
        rule = rule.replace(/\-(\w)/g, function (strMatch, p1) { return p1.toUpperCase(); });
        return e.currentStyle[rule];
    }
}

// Snowplow idle refresh timer...
function setRefresh(internval) {
    $(function () {
        var refreshTimer = window.setTimeout("location.reload(true)", (internval * 1000));
        $(this).mousemove(function (e) { window.clearTimeout(refreshTimer); refreshTimer = window.setTimeout("location.reload(true)", (internval * 1000)); });
    });
}

// Barcode scanning code...
var barcodeKeys = ''; var keyTimeout;
function scanHandler(e) {
    if (Object.prototype.toString.call(document.activeElement) == '[object HTMLInputElement]') { return; }
    clearTimeout(keyTimeout);
    if (e.which == 13) {
        barcodeScanned(barcodeKeys);
        barcodeKeys = ''
    } else {
        barcodeKeys += String.fromCharCode(e.which);
        keyTimeout = setTimeout("barcodeKeys='';", 500);
    }
    window.focus();
}
