var _bcfui = {

    validators: [],
    ctlToFocus: null,
    windowTargets: {},
    onUnloadFunctions: [],
    
    setControlToFocus: function (ctl)
    {
        if (_bcfui.ctlToFocus) {
            return;
        }
        else {
            _bcfui.ctlToFocus = ctl;
        }
    },
    
    getControlToFocus: function ()
    {
        return _bcfui.ctlToFocus;
    },
    
    focusCtl: function (fctl)
    {
    	try {
            if (fctl.type) {
                fctl.focus();
            }
            else if (fctl.length && fctl.length > 0) {
                var t = fctl[0].type;
                if (t == "radio" || t == "checkbox") {
                    var selIndex = Number(_bcfui.booleanListValue(fctl));
                    if (selIndex > -1) {
                        fctl[Number(selIndex)].focus();
                        return;
                    }
                }
                fctl[0].focus();
    	    }
    	}
    	catch (e) {
    	}
    },
    
    setElementText: function (elm, text)
    {
    	if (elm) {
    		elm.innerHTML = text;
    	}
    },
    
    getElement: function (id)
    {
    	try {
    		return document.getElementById(id);
    	}
    	catch (e) {
    	}
    },
    
    getFormElement: function (id)
    {
        return document.pageForm.elements[id];
    },
    
    booleanListValue: function (fctl)
    {
        for (var i=0; i<fctl.length; i++) {
            if (fctl[i].checked) {
                return fctl[i].value;
                //return "" + i;
            }
        }
        return "-1";
    },
    
    ctlValue: function (fctl)
    {
    	try {
            if (fctl.type) {
            	var t = fctl.type;
                if ((t == "select-one") || (t == "select-multiple")) {
                    return fctl.options[fctl.selectedIndex].value;
                }
                else if (t == "radio" || t == "checkbox") {
                	return fctl.checked;
                }
                return fctl.value;
            }
            else if (fctl.length) {
                if (fctl.length > 0) {
                    var t = fctl[0].type;
                    if (t == "radio" || t == "checkbox") {
                        return _bcfui.booleanListValue(fctl);
                    }
                }
            }
    	}
    	catch (e) {
    	}
    },
    
    disableClientSideValidation: function (vid)
    {
        for (var i=0; i < _bcfui.validators.length; i++) {
            var vdata = _bcfui.validators[i];
            if (vdata.vid == vid) {
                vdata.enabled = false;
                return;
            }
        }
    },

    enableClientSideValidation: function (vid)
    {
        for (var i=0; i < _bcfui.validators.length; i++) {
            var vdata = _bcfui.validators[i];
            if (vdata.vid == vid) {
                vdata.enabled = true;
                return;
            }
        }
    },
    
    pageIsValid: function ()
    {
        _bcfui.ctlToFocus = null;
    	var retValue = true;
    	var valid;
        for (var i=0; i < _bcfui.validators.length; i++) {
            var vdata = _bcfui.validators[i];
            if (vdata.enabled == false) {
                continue;
            }
            valid = window[vdata.proc](vdata);
            retValue = (retValue && valid);
        }
        
        if (typeof(window["Page_Validate"]) == "function") {
        	valid = window["Page_Validate"]();
        	retValue = (retValue && valid);
        }
        
        if (_bcfui.ctlToFocus) {
        	_bcfui.focusCtl(_bcfui.ctlToFocus);
        }
        return retValue;
    },
    
    doSubmit: function (f, target)
    {
        if (f.onsubmit) {
        	var retVal = f.onsubmit.call(f);
            if (retVal == false) {
                return false;
            }
        }
        if (target == null) {
            f.target = "";
            _bcfui.isSubmitting = true;
            f.submit();
        }
        else {
            f.target = target;
            _bcfui.openWT(target,true);
            f.submit();
        }
        return true;
    },
    
    pb: function (evSrc, evArg, validate, target)
    {
    	if (_bcfui.isSubmitting) return false;
        var f = document.pageForm;
        f._EVENTSRC.value = evSrc;
        if (evArg) {
            f._EVENTARG.value = evArg;
        }
        if ((validate==false) || _bcfui.pageIsValid()) {
            return _bcfui.doSubmit(f, target);
        }
        return false;
    },
    
    pb2: function (funcName, evSrc, evArg, validate, target)
    {
    	if (_bcfui.isSubmitting) return false;
        var retValue = true;
        try {
            retValue = window[funcName](evSrc, evArg, validate);
        }
        catch (e) {
        }
        if (retValue != false) {
            return _bcfui.pb(evSrc, evArg, validate, target);
        }
        return false;
    },
    
    addV: function (vdata)
    {
        _bcfui.validators.push(vdata);
    },
    
    _registerOnUnload: function ()
    {
    	var f = window.onunload;
    	if (f) {
    		if (f != _bcfui.onUnload) {
    			_bcfui.onUnloadFunctions.push(f);
    		}
    	}
   		window.onunload = _bcfui.onUnload;
    },
    
    addWT: function (name, features, url, replace, autoClose, onOpen, onAutoClose)
    {
        _bcfui.windowTargets[name] = {
            features: features,
            url: url,
            replace: replace,
            autoClose: autoClose,
            onOpen: onOpen,
            onAutoClose: onAutoClose
        };
        if (autoClose) {
        	_bcfui._registerOnUnload();
        }
    },
    
    openWT: function (name, focused)
    {
        var wt = _bcfui.windowTargets[name];
        if (wt) {
            var w = window.open(wt.url, name, wt.features, wt.replace);
			if (w) {
                wt.handle = w;
                if (wt.onOpen != "") {
                    window[wt.onOpen](w);
                }
                if (focused) {
                    window.setTimeout("_bcfui.focusWT('" + name + "')", 250);
                }
                return w;
            }
        }
    },
    
    focusWT: function (name)
    {
        var wt = _bcfui.windowTargets[name];
        if (wt && wt.handle && !wt.handle.closed) {
            wt.handle.focus();
        }
    },
    
    closeWT: function (windowTarget)
    {
        var wt;
        if (typeof(windowTarget) == "string") {
            wt = _bcfui.windowTargets[name];
        }
        else {
            wt = windowTarget;
        }
        if (wt && wt.handle && !wt.handle.closed) {
            if (wt.onAutoClose) {
                if (window[wt.onAutoClose](wt.handle) == false) {
                    return;
                }
            }
            wt.handle.close();
        }
    },
    
    _autoCloseWT: function ()
    {
    	for (var name in _bcfui.windowTargets) {
    		var wt = _bcfui.windowTargets[name];
    		if (wt.autoClose) {
    			_bcfui.closeWT(wt);
    		}
    	}
    },
    
    onUnload: function ()
    {
    	// Invoca las funciones onunload previas
    	try {
    		for (var i=0; i<_bcfui.onUnloadFunctions.length; i++) {
    			_bcfui.onUnloadFunctions[i]();
    		}
    	}
    	catch (e) {
    	}
    	// Cierra los window targets con autoClose
    	_bcfui._autoCloseWT();
    }

};

function _bcfui_CV(vdata)
{
	try {
		var ctl = _bcfui.getElement(vdata.vid);
	    var ctv = _bcfui.getFormElement(vdata.ctv);
	    var value = _bcfui.ctlValue(ctv);
	    var f = window[vdata.fn];
	    var valid = vdata.valid = f(value, vdata.msg, ctl, ctv, vdata);
	    if (valid) {
	        _bcfui.setElementText(ctl, "");
	    }
	    else {
	        _bcfui.setControlToFocus(ctv);
	        _bcfui.setElementText(ctl, vdata.msg);
	    }
	    return valid;
	}
	catch (e) {
		throw e;
	}
	return false;
}

function _bcfui_RV(vdata)
{
	try {
		var ctl = _bcfui.getElement(vdata.vid);
	    var ctv = _bcfui.getFormElement(vdata.ctv);
	    var value = _bcfui.ctlValue(ctv);
	    var iv = vdata.iv;
	    var ivd = vdata.ivd;
	    var valid = true;
	    if (value == null) {
	        valid = false;
	    }
	    else if (value == iv || value == ivd) {
	        valid = false;
	    }
	    if (valid) {
	        _bcfui.setElementText(ctl, "");
	    }
	    else {
	        _bcfui.setControlToFocus(ctv);
	        _bcfui.setElementText(ctl, vdata.msg);
	    }
	    return valid;
	}
	catch (e) {
		throw e;
	}
	return false;
}

