﻿function getWinDims() {
    if (self.innerWidth) {
        _ac_winWidth = self.innerWidth;
        _ac_winHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
        _ac_winWidth = document.documentElement.clientWidth;
        _ac_winHeight = document.documentElement.clientHeight;
    }
    else if (document.body) {
        _ac_winWidth = document.body.clientWidth;
        _ac_winHeight = document.body.clientHeight;
    }
    return [_ac_winWidth, _ac_winHeight];
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft, curtop];
}

function checkReal(evt) {
    var target = '';
    if (!evt) {
        evt = window.event;
        target = evt.srcElement;
    } else {
        target = evt.target;
    }
    var charCode = (evt.which) ? evt.which : event.keyCode;
    var ch = String.fromCharCode(charCode);
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;

}

var _atiTextObjectCollection = [];

function atiTextBox(txtId, checkId, toolId, errId, isRequired, isDouble, minLen) {
    this.domText = document.getElementById(txtId);
    this.domError = document.getElementById(errId);
    this.toolTipId = toolId;    
    this.isRequired = isRequired;
    this.isDouble = isDouble;
    this.minLength = minLen;
    this.domCheckImg = document.getElementById(checkId);
    this.errorMsg = '';
    this.regEx = '';
    this.regExErrStr = '';
    if (this.domText) {
        if (isDouble) {
            this.domText.onkeypress = function(evt) {
                var target = '';
                if (!evt) {
                    evt = window.event;
                    target = evt.srcElement;
                } else {
                    target = evt.target;
                }
                var charCode = (evt.which) ? evt.which : event.keyCode;
                var ch = String.fromCharCode(charCode);
                if ((ch == '.')) {
                    return true;
                }
                if (charCode > 31 && (charCode < 48 || charCode > 57))
                    return false;
                return true;

            }
        }
        this.domText.onchange = function(event) {
            var target = '';
            if (!event) {
                event = window.event;
                target = event.srcElement;
            } else {
                target = event.target;
            }
            var atiTxt = _atiTextObjectCollection[target.id];
            if (atiTxt.domCheckImg) atiTxt.domCheckImg.style.visibility = 'hidden';
            var valid = atiTxt.validate();
            if (valid) {
                if (atiTxt.domCheckImg) atiTxt.domCheckImg.style.visibility = 'visible';
            }
        }

    }
};

atiTextBox.prototype = {
    value: function() {
        return this.domText.value;
    },

    setRegEx: function(regEx, errStr) {
        this.regExErrStr = errStr;
        this.regEx = regEx;
    },

    validate: function() {
        var toolTip = $find(this.toolTipId);
        toolTip.hide();
        if (this.isRequired) {
            if (this.value() == '') {
                this.errorMsg = "required field.";
                this.domError.innerHTML = this.errorMsg;
                toolTip.show();
                return false;
            }
        }
        if (this.isDouble) {
            if (!parseFloat(this.value())) {
                this.errorMsg = "must be a Real Number.";
                this.domError.innerHTML = this.errorMsg;
                toolTip.show();
                return false;
            }
        }
        if (this.minLength > 0) {
            if (this.value().length < this.minLength) {
                this.errorMsg = "must be at least " + this.minLength + " characters.";
                this.domError.innerHTML = this.errorMsg;
                toolTip.show();
                return false;
            }
        }
        if (this.regEx != '') {
            var re = new RegExp(this.regEx);
            var m = re.exec(this.value());
            if (m == null) {
                this.errorMsg = this.regExErrStr;
                this.domError.innerHTML = this.errorMsg;
                toolTip.show();
                return false;
            }
        }
        return true;
    }
};


function AtiValidationGroup(valArray) {
    this.valArray = valArray;
}
AtiValidationGroup.prototype.validate = function() {
    for (var i = 0; i < this.valArray.length; i++) {
        var v = this.valArray[i].validate();
        if (!v) return false;
    }
    return true;
};


function unitTest() {
    alert(atiHeight);
    atiHeight.validate();
}