/**
 * document.getElementsByClassName is deprecated in Prototype 1.6, and seems buggy in ff3
 * This method should do the same job.
 * @param _desiredClass
 * @param ancestorElement
 * @returns Array of descendants with desired class
 */
getDescendantsByClassName = function(_desiredClass, ancestorElement ){
    //REL-706 PGS-244: text field in editlive is not removed on delete due to "nullpointerexception" here when compositionoutcomeforms does not exist.
    //quick-fixing here by catching and swallowing exception when this element is null (kremt)
    var _descendants = [];
    
    if(ancestorElement){
        try {
            var desc_ = $(ancestorElement.id).descendants();
            for (var i = 0; i < desc_.length; i++) {
                var descy = desc_[i];
                if (descy.hasClassName(_desiredClass)) {
                    //alert('getDescendantsByClassName '+descy.className+'_'+descy.id);
                    _descendants[_descendants.length] = descy;
                }
            }
        } catch(ex) {
//            alert('exception getting descendants for '+ancestorElement);

            try{
                getDescendantsByClassNameOldFashion(_desiredClass, ancestorElement, _descendants);
            }catch(err){

            }
        }
    }
    return _descendants;
}

/**
 * SOTI-58
 * Propably slow, but hopefully stable fallback for prototype based getDescendantsByClassName
 * @param _desiredClass
 * @param ancestorElement
 * @param _descendants
 */
getDescendantsByClassNameOldFashion = function(_desiredClass, ancestorElement, _descendants){
    try{

        if(ancestorElement.hasClassName(_desiredClass)){
            _descendants[_descendants.length] = ancestorElement;
            return;
        }
        if(!ancestorElement.hasChildNodes())return;
        var kidz = ancestorElement.childNodes;
//        alert('kidz '+kidz.length);
        for(var i = 0; i < kidz.length;i++){
            getDescendantsByClassNameOldFashion(_desiredClass, kidz[i], _descendants);
        }
    }catch(err){

    }
}


function legalXMLString(str){
//    str = str.replace(/&/g, "&amp;");
    return str.escapeHTML();
}

function legalXMLAttribute(str){
    str = legalXMLString(str);
    str = str.replace(/\"/g, "&quot;");
    return str;
}


