var ThisPage = {};
var p$ = {};

function getCommandLinkSubmitter() {
    var frm = document.forms['form1'];
    if (!frm)
        return null;
    var hidden = frm['com_sun_rave_web_ui_appbase_renderer_CommandLinkRendererer'];
    if (!hidden)
        return null;
    return hidden.value;
}

Enumerable.byId = function(id) {
    return this.find(function (t) {
        if (t.id==id)
            return t;
    });
};

Enumerable.byKey = function(key, value) {
    return this.find(function (t) {
        if (t[key]==value)
            return t;
        if (!!t.tagName && !!t.getAttribute && t.getAttribute(key)==value) {
            return t;
        }
    });
};

Enumerable.indexOfId = function(id) {
    var i = 0;
    return this.find(function (t) {
        if (t.id==id)
            return i;
        i++;
    });
};

Enumerable.indexOfKey = function(key, value) {
    var i = 0;
    return this.find(function (t) {
        if (t[key]==value)
            return i;
        if (!!t.tagName && !!t.getAttribute && t.getAttribute(key)==value) {
            return i;
        }
        i++;
    });
};

Object.extend(Array.prototype, {
    byId:     Enumerable.byId,
    byKey:     Enumerable.byKey,
    indexOfId:     Enumerable.indexOfId,
    indexOfKey:     Enumerable.indexOfKey
});

/**
* Obtém um sub elemento procurando dentre todos os subelementos por uma
* propriedade específica e coincidente com um valor especificado.<br>
* Eg.:<br>
* <pre>
*    var option = getSubByKeyValue( p$.cmbShipmentUF.getSelectElement().options, 'label', 'Minas Gerais') );
*    if (option!=null)
*       option.selected=true;
* </pre>
* @param collection O elemento principal onde estão contidos os subelementos a serem pesquisados.
* @param key A propriedade a ser procurada em cada um dos subelmentos de <code>collection</code>.
* @param value Para que um subelemento seja retornado, além dele ter uma
* propriedade que coincida com a especificada por <code>key</code>, o valor da
* chave também deve ser coincidente com <code>value</code>.
* @return Quando encontra, retorna o sub-elemento. Senão encontrado, retorna
* <code>null</code>.
* @author Marcio Wesley Borges<marciowb@gmail.com>
 */
function getSubByKeyValue(collection, key, value) {
    var valueIsUndefined = Object.isUndefined(value);
    var valueIsNull = !valueIsUndefined && (value==null);
    for (var n in collection) {
        var o = collection[n];
        if (!o)
            continue;
        var v = o[key];

        if (valueIsUndefined) {
            if (Object.isUndefined(v))
                return o;
            continue;
        }
        if (valueIsNull) {
            if (v==null && !Object.isUndefined(v))
                return o;
            continue;
        }

        if (v==value)
            return o;

        if (!!o.tagName && !!o.getAttribute && o.getAttribute(key)==value) {
            return o;
        }
    }
    return null;
}
