var ie5 = (document.getElementById && document.all);
var ns6 = (document.getElementById && !document.all);




// **************************************************************************************** //
// ***    Window functions                                                              *** //
// **************************************************************************************** //




function windowSizeX() {
    if(ie5) return document.body.offsetWidth;
    else return innerWidth;
}




function windowSizeY() { 
    if(ie5) return document.body.offsetHeight;
    else return innerHeight;
}




function windowScrollPosY() {

    if (ie5) {

        if (document.documentElement && document.documentElement.scrollTop) {

            // IE 6 in standards compliant mode
            return document.documentElement.scrollTop;

        } else {

            // IE 6 not in standards compliant mode
            return document.body.scrollTop;
        }

    } else if (ns6) {

        // NS
        return self.pageYOffset;

    } else {

        alert("error: could not calculate scrollOffsetY");
    }
}




function windowScrollPosX() {

    if (ie5) {

        if (document.documentElement && document.documentElement.scrollLeft) {

            // IE 6 in standards compliant mode
            return document.documentElement.scrollLeft;

        } else {

            // IE 6 not in standards compliant mode
            return document.body.scrollLeft;
        }

    } else if (ns6) {

        // NS
        return self.pageXOffset;

    } else {

        alert("error: could not calculate scrollOffsetX");
    }
}




function windowMousePosX(e) {
    return (ie5)?event.x:(ns6)?clientX=e.clientX:false;
}




function windowMousePosY(e) {
    return (ie5)?event.y:(ns6)?clientY=e.clientY:false;
}




// **************************************************************************************** //
// ***    Element functions                                                             *** //
// **************************************************************************************** //




function getElementFromDocumentById(document, id) {

    try {
        vElement = document.getElementById(id);
        if (vElement.id != id) alert("getElementFromDocumentById(): Could not find element with id '" + id + "'");
    } catch (e) {
        alert("getElementById(): Could not find element with id '" + id + "'\n" + e.description);
    }
    return vElement;
}




function elementGetChildNode(element, tagName, tagIx) {

    childNodeCnt = element.childNodes.length;

    i = -1;
    while (++i < childNodeCnt) {
        if (element.childNodes[i].tagName == tagName) tagIx--;
        if (tagIx == 0) {
            return element.childNodes[i];
        }
    }
    if (i == childNodeCnt) alert("child node not found");
}




function domCompliantSetInnerText(object, text) {

    if (!object.hasChildNodes()) {
        var newNode = document.createTextNode(text);
        object.appendChild(newNode);
    } else {
        object.childNodes[0].nodeValue = text;
    }
}




// **************************************************************************************** //
// ***    Math functions                                                             *** //
// **************************************************************************************** //




function getRandom(solutionCnt) {

    r1 = Math.random();
    if ((r1 < 0) || (r1 >= 1)) alert("Math.random() returned invalid value: " + r1);
    r2 = r1 * solutionCnt;
    random = Math.round(r2 + 0.5);
    return random;
}







