﻿/**********************************************************************************/
/*********** FUNCTIONS TO DETERMINE VIEWPORT DIMENSIONS ***************************/
/**********************************************************************************/

getViewportWidth = function() {
    var width = 0;
    if (document.documentElement && document.documentElement.clientWidth) {
        width = document.documentElement.clientWidth;
    }
    else if (document.body && document.body.clientWidth) {
        width = document.body.clientWidth;
    }
    else if (window.innerWidth) {
        width = window.innerWidth - 18;
    }
    return width;
};

getViewportHeight = function() {
    var height = 0;
    if (document.documentElement && document.documentElement.clientHeight) {
        height = document.documentElement.clientHeight;
    }
    else if (document.body && document.body.clientHeight) {
        height = document.body.clientHeight;
    }
    else if (window.innerHeight) {
        height = window.innerHeight - 18;
    }
    return height;
};

getViewportScrollX = function() {
    var scrollX = 0;
    if (document.documentElement && document.documentElement.scrollLeft) {
        scrollX = document.documentElement.scrollLeft;
    }
    else if (document.body && document.body.scrollLeft) {
        scrollX = document.body.scrollLeft;
    }
    else if (window.pageXOffset) {
        scrollX = window.pageXOffset;
    }
    else if (window.scrollX) {
        scrollX = window.scrollX;
    }
    return scrollX;
};

getViewportScrollY = function() {
    var scrollY = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
        scrollY = document.documentElement.scrollTop;
    }
    else if (document.body && document.body.scrollTop) {
        scrollY = document.body.scrollTop;
    }
    else if (window.pageYOffset) {
        scrollY = window.pageYOffset;
    }
    else if (window.scrollY) {
        scrollY = window.scrollY;
    }
    return scrollY;
};

function findPos(obj) {
    var curleft = 0, curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } 
    }
    return [curleft, curtop]
}


/**********************************************************************************/
/*********** FUNCTIONS TO DETERMINE IF ELEMENT IS IN CURRENT VIEWPORT *************/
/**********************************************************************************/

IsInViewPort = function(div) {
    if (document.getElementById) {
        var setX = 195;
        var setY = 150;

        var divWidth = div.offsetWidth ? div.offsetWidth : div.style.width ? parseInt(div.style.width) : 0;
        var divHeight = div.offsetHeight ? div.offsetHeight : div.style.height ? parseInt(div.style.height) : 0;

        var objCoords = findPos(div);
        objCoords.x = objCoords[0];
        objCoords.y = objCoords[1];

        viewportscrollX = getViewportScrollX();
        viewportscrollY = getViewportScrollY();

        //alert(viewportscrollY + ' > ' + objCoords.y + ' + ' + divHeight);

        if (viewportscrollY > (objCoords.y)) {
            return false;
        }

        if (viewportscrollX > (objCoords.x)) {
            return false;
        }

        return true;
    }
};

/**********************************************************************************/
/****************** FUNCTION TO SUBMIT FORM ON ENTER BUTTON ***********************/
/**********************************************************************************/

function submitForm(myfield,e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    if (keycode == 13) {
        myfield.form.submit();
        return false;
    }
    else
        return true;    
}

function isEnterKey(myfield, e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return false;
    if (keycode == 13) {
        return true;
    }
    else
        return false;
}

/**********************************************************************************/
/*********** ARRAY INDEXOF PROTOTYPE IF BROWSER DOES NOT SUPPOR IT (IE) ***********/
/**********************************************************************************/

if (!Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}

/**********************************************************************************/
/**********************************************************************************/
/**********************************************************************************/