﻿// JScript File
window.onload = HookUpPrint;

function HookUpPrint() {
    var sPath = window.location.pathname;
    //var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    if (sPage == "co2CalcRecommendations.aspx") {
        if (document.getElementById("lnkPrint")) {
            document.getElementById("lnkPrint").style.display = "block";
            if(document.getElementById("lnkPrint").style.styleFloat){
            document.getElementById("lnkPrint").style.styleFloat = "right";
            }
            if (document.getElementById("lnkPrint").style.cssFloat)
            {
            document.getElementById("lnkPrint").style.cssFloat = "right";
            }
            document.getElementById("lnkPrint").onclick = function(evt) {
                window.print();
            };
        }
        else {
            setTimeout(HookUpPrint, 1000);
        }
    }
}

/* Some util functions that can be migrated to a separate utils.js when there are a significant number of utils */

function isUndefined(a) { return typeof a == 'undefined' }


/* Popup link script 
*
* page and target values are derived from a tag values
*
* solution based on ala article http://www.alistapart.com/articles/popuplinks/
*/
var _POPUP_FEATURES = 'location=0,statusbar=0,scrollbars=1,menubar=1,width=630,height=450';

function raw_popup(url, target, features) {
  if (isUndefined(features)) {
    features = _POPUP_FEATURES;
  }
  if (isUndefined(target)) {
    target = '_blank';
  }
  var theWindow = window.open(url, target, features);
  theWindow.focus();
  return theWindow;
}

function link_popup(src, features) {
  return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}


/* Toggle view
*
*/

function toggleView(strClass,strObjType) {
    var arrObj;
    arrObj = getElementsByClass(strClass,strObjType);
    if (arrObj[0].style.display=='none') {
        for (i=0; i<arrObj.length; i++) {
            arrObj[i].style.display='block';
        }
    } else {
        for (i=0; i<arrObj.length; i++) {
            arrObj[i].style.display='none';
        }
    }
}

//

Array.prototype.find = function(value, start) {
    start = start || 0;
    for (var i=start; i<this.length; i++)
        if (this[i]==value)
            return i;
    return -1;
}

Array.prototype.has = function(value) {
    return this.find(value)!==-1;
}

// FUNCTIONAL - based on ala article http://www.alistapart.com/articles/popuplinks/

function map(list, func) {
    var result = [];
    func = func || function(v) {return v};
    for (var i=0; i < list.length; i++) result.push(func(list[i], i, list));
    return result;
}

function filter(list, func) {
    var result = [];
    func = func || function(v) {return v};
    map(list, function(v) { if (func(v)) result.push(v) } );
    return result;
}

// DOM - based on ala article http://www.alistapart.com/articles/popuplinks/

function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}

function hasClass(elem, className) {
    return getElem(elem).className.split(' ').has(className);
}

function getElementsByClass(className, tagName, parentNode) {
    parentNode = !isUndefined(parentNode)? getElem(parentNode) : document;
    if (isUndefined(tagName)) tagName = '*';
    return filter(parentNode.getElementsByTagName(tagName),
        function(elem) { return hasClass(elem, className) });
}

// MISC CLEANING-AFTER-MICROSOFT STUFF - based on ala article http://www.alistapart.com/articles/popuplinks/

function isUndefined(v) {
    var undef;
    return v===undef;
}

