var dynamicOptionListCount=0;var dynamicOptionListObjects = new Array();
function initDynamicOptionLists()
{
    for(var i=0;i<dynamicOptionListObjects.length;i++){
        var dol = dynamicOptionListObjects[i];
        if(dol.formName!=null){
            dol.form = document.forms[dol.formName];
        }
        else if(dol.formIndex!=null){
            dol.form = document.forms[dol.formIndex];
        }else{
            var name = dol.fieldNames[0][0];
            for(var f=0;f<document.forms.length;f++){
                if(typeof(document.forms[f][name])!="undefined"){
                    dol.form = document.forms[f];break;
                }
            }
            if(dol.form==null){
                alert("ERROR: Couldn't find form element "+name+" in any form on the page! Init aborted");return;
            }
        }

        for(var j=0;j<dol.fieldNames.length;j++){
            for(var k=0;k<dol.fieldNames[j].length-1;k++){
                var selObj = dol.form[dol.fieldNames[j][k]];
                if(typeof(selObj)=="undefined"){
                    alert("Select box named "+dol.fieldNames[j][k]+" could not be found in the form. Init aborted");
                    return;
                }
                if(k==0){if(selObj.options!=null){
                        for(l=0;l<selObj.options.length;l++){
                            var sopt = selObj.options[l];
                            var m = dol.findMatchingOptionInArray(dol.options,sopt.text,sopt.value,false);
                            if(m!=null){
                                var reselectForNN6 = sopt.selected;
                                var m2 = new Option(sopt.text, sopt.value, sopt.defaultSelected, sopt.selected);
                                m2.selected = sopt.selected;
                                m2.defaultSelected = sopt.defaultSelected;
                                m2.DOLOption = m;
                                selObj.options[l] = m2;
                                selObj.options[l].selected = reselectForNN6;
                            }
                        }
                    }
                }
                if(selObj.onchange==null){
                    selObj.onchange = new Function("dynamicOptionListObjects["+dol.index+"].change(this)");
                }
            }
        }
    }
    resetDynamicOptionLists();
}


function resetDynamicOptionLists(theform){
    for(var i=0;i<dynamicOptionListObjects.length;i++){
        var dol = dynamicOptionListObjects[i];
        if(typeof(theform)=="undefined" || theform==null || theform==dol.form){
            for(var j=0;j<dol.fieldNames.length;j++){
                dol.change(dol.form[dol.fieldNames[j][0]],true);
            }
        }
    }
}


function DOLOption(text,value,defaultSelected,selected){
    this.text = text;
    this.value = value;
    this.defaultSelected = defaultSelected;
    this.selected = selected;
    this.options = new Array();
    return this;
}


function DynamicOptionList(){
    this.form = null;
    this.options = new Array();
    this.longestString = new Array();
    this.numberOfOptions = new Array();
    this.currentNode = null;this.currentField = null;
    this.currentNodeDepth = 0;
    this.fieldNames = new Array();
    this.formIndex = null;
    this.formName = null;
    this.fieldListIndexes = new Object();
    this.fieldIndexes = new Object();
    this.selectFirstOption = false;
    this.numberOfOptions = new Array();
    this.longestString = new Array();
    this.values = new Object();
    this.forValue = DOL_forValue;
    this.forText = DOL_forText;
    this.forField = DOL_forField;
    this.forX = DOL_forX;
    this.addOptions = DOL_addOptions;
    this.addOptionsTextValue = DOL_addOptionsTextValue;
    this.setDefaultOptions = DOL_setDefaultOptions;
    this.setValues = DOL_setValues;
    this.setValue = DOL_setValues;
    this.setFormIndex = DOL_setFormIndex;
    this.setFormName = DOL_setFormName;
    this.printOptions = DOL_printOptions;
    this.addDependentFields = DOL_addDependentFields;
    this.change = DOL_change;
    this.child = DOL_child;
    this.selectChildOptions = DOL_selectChildOptions;
    this.populateChild = DOL_populateChild;
    this.change = DOL_change;
    this.addNewOptionToList = DOL_addNewOptionToList;
    this.findMatchingOptionInArray = DOL_findMatchingOptionInArray;
    if(arguments.length > 0){
        for(var i=0;i<arguments.length;i++){
            this.fieldListIndexes[arguments[i].toString()] = this.fieldNames.length;
            this.fieldIndexes[arguments[i].toString()] = i;
        }
        this.fieldNames[this.fieldNames.length] = arguments;
    }
    this.index = window.dynamicOptionListCount++;
    window["dynamicOptionListObjects"][this.index] = this;
}


function DOL_findMatchingOptionInArray(a,text,value,exactMatchRequired){
    if(a==null || typeof(a)=="undefined"){
        return null;
    }
    var value_match = null;
    var text_match = null;
    for(var i=0;i<a.length;i++){
        var opt = a[i];if(opt.value==value && opt.text==text){
            return opt;
        }
        if(!exactMatchRequired){if(value_match==null && value!=null && opt.value==value){
                value_match = opt;
            }
            if(text_match==null && text!=null && opt.text==text){
                text_match = opt;
            }
        }
    }
    return(value_match!=null)?value_match:text_match;
}


function DOL_forX(s,type){
    if(this.currentNode==null){
        this.currentNodeDepth=0;
    }
    var useNode =(this.currentNode==null)?this:this.currentNode;
    var o = this.findMatchingOptionInArray(useNode["options"],(type=="text")?s:null,(type=="value")?s:null,false);
    if(o==null){
        o = new DOLOption(null,null,false,false);
        o[type] = s;useNode.options[useNode.options.length] = o;
    }
    this.currentNode = o;this.currentNodeDepth++;
    return this;
}


function DOL_forValue(s){
    s = html_entity_decode(s);
    return this.forX(s,"value");
}


function DOL_forText(s){
    return this.forX(s,"text");
}


function DOL_forField(f){
    this.currentField = f;return this;
}


function DOL_addNewOptionToList(a, text, value, defaultSelected){
    var o = new DOLOption(text,value,defaultSelected,false);
    if(a==null){
        a = new Array();
    }
    for(var i=0;i<a.length;i++){
        if(a[i].text==o.text && a[i].value==o.value){
            if(o.selected){
                a[i].selected=true;
            }
            if(o.defaultSelected){
                a[i].defaultSelected = true;
            }
            return a;
        }
    }
    a[a.length] = o;
}


function DOL_addOptions(){
    if(this.currentNode==null){
        this.currentNode = this;
    }
    if(this.currentNode["options"] == null){
        this.currentNode["options"] = new Array();
    }
    for(var i=0;i<arguments.length;i++){
        var text = arguments[i];
        this.addNewOptionToList(this.currentNode.options,text,text,false);
        if(typeof(this.numberOfOptions[this.currentNodeDepth])=="undefined"){
            this.numberOfOptions[this.currentNodeDepth]=0;
        }
        if(this.currentNode.options.length > this.numberOfOptions[this.currentNodeDepth]){
            this.numberOfOptions[this.currentNodeDepth] = this.currentNode.options.length;
        }
        if(typeof(this.longestString[this.currentNodeDepth])=="undefined" ||(text.length > this.longestString[this.currentNodeDepth].length)){
            this.longestString[this.currentNodeDepth] = text;
        }
    }this.currentNode = null;this.currentNodeDepth = 0;
}


function DOL_addOptionsTextValue(){
    if(this.currentNode==null){
        this.currentNode = this;
    }
    if(this.currentNode["options"] == null){
        this.currentNode["options"] = new Array();
    }
    for(var i=0;i<arguments.length;i++){
        var text = arguments[i++];
        var value = arguments[i];this.addNewOptionToList(this.currentNode.options,text,value,false);
        if(typeof(this.numberOfOptions[this.currentNodeDepth])=="undefined"){
            this.numberOfOptions[this.currentNodeDepth]=0;
        }
        if(this.currentNode.options.length > this.numberOfOptions[this.currentNodeDepth]){
            this.numberOfOptions[this.currentNodeDepth] = this.currentNode.options.length;
        }if(typeof(this.longestString[this.currentNodeDepth])=="undefined" ||(text.length > this.longestString[this.currentNodeDepth].length)){
            this.longestString[this.currentNodeDepth] = text;
        }
    }this.currentNode = null;this.currentNodeDepth = 0;
}


function DOL_child(obj){
    var listIndex = this.fieldListIndexes[obj.name];
    var index = this.fieldIndexes[obj.name];
    if(index <(this.fieldNames[listIndex].length-1)){
        return this.form[this.fieldNames[listIndex][index+1]];
    }return null;
}


function DOL_setDefaultOptions(){
    if(this.currentNode==null){
        this.currentNode = this;
    }
    for(var i=0;i<arguments.length;i++){
        var o = this.findMatchingOptionInArray(this.currentNode.options,null,arguments[i],false);
        if(o!=null){
            o.defaultSelected = true;
        }
    }
    this.currentNode = null;
}


function DOL_setValues(){
    if(this.currentField==null){
        alert("Can't call setValues() without using forField() first!");
        return;
    }
    if(typeof(this.values[this.currentField])=="undefined"){
        this.values[this.currentField] = new Object();
    }
    for(var i=0;i<arguments.length;i++){
        this.values[this.currentField][arguments[i]] = true;
    }
    this.currentField = null;
}


function DOL_setFormIndex(i){
    this.formIndex = i;
}


function DOL_setFormName(n){
    this.formName = n;
}


function DOL_printOptions(name){
    
    
        var index = this.fieldIndexes[name];
        
        var ret = "";
        if(typeof(this.numberOfOptions[index])!="undefined"){
            
            for(var i=0;i<this.numberOfOptions[index];i++){
                ret += "<OPTION>";
            }
        }
        ret += "<OPTION>";

        if(typeof(this.longestString[index])!="undefined"){
            for(i=0;i<this.longestString[index].length;i++){
                ret += "_";
            }
        }
        document.writeln(ret);
    
}


function DOL_addDependentFields(){
    for(var i=0;i<arguments.length;i++){
        this.fieldListIndexes[arguments[i].toString()] = this.fieldNames.length;
        this.fieldIndexes[arguments[i].toString()] = i;
    }
    this.fieldNames[this.fieldNames.length] = arguments;

}


function DOL_change(obj, usePreselected){
    if(usePreselected==null || typeof(usePreselected)=="undefined"){
        usePreselected = false;
    }
    var changedListIndex = this.fieldListIndexes[obj.name];
    var changedIndex = this.fieldIndexes[obj.name];
    var child = this.child(obj);
    if(child == null){
        return;
    }
    if(obj.type == "select-one"){
        if(child.options!=null){
            child.options.length=0;
        }
        if(obj.options!=null && obj.options.length>0 && obj.selectedIndex>=0){
            var o = obj.options[obj.selectedIndex];
            this.populateChild(o.DOLOption,child,usePreselected);
            this.selectChildOptions(child,usePreselected);
        }
    }
    else if(obj.type == "select-multiple"){
        var currentlySelectedOptions = new Array();
        if(!usePreselected){
            for(var i=0;i<child.options.length;i++){
                var co = child.options[i];
                if(co.selected){this.addNewOptionToList(currentlySelectedOptions, co.text, co.value, co.defaultSelected);
                }
            }
        }
        child.options.length=0;
        if(obj.options!=null){
            var obj_o = obj.options;
            for(var i=0;i<obj_o.length;i++){
                if(obj_o[i].selected){
                    this.populateChild(obj_o[i].DOLOption,child,usePreselected);
                }
            }
            var atLeastOneSelected = false;
            if(!usePreselected){
                for(var i=0;i<child.options.length;i++){
                    var m = this.findMatchingOptionInArray(currentlySelectedOptions,child.options[i].text,child.options[i].value,true);
                    if(m!=null){
                        child.options[i].selected = true;atLeastOneSelected = true;
                    }
                }
            }
            if(!atLeastOneSelected){
                this.selectChildOptions(child,usePreselected);
            }
        }
    }
    this.change(child,usePreselected);
}


function DOL_populateChild(dolOption,childSelectObj,usePreselected){
    
    if(dolOption!=null && dolOption.options!=null){
        
        for(var j=0;j<dolOption.options.length;j++){
            var srcOpt = dolOption.options[j];
            
            if(childSelectObj.options==null){
                childSelectObj.options = new Array();
            }
            var duplicate = false;
            var preSelectedExists = false;
            for(var k=0;k<childSelectObj.options.length;k++){
                var csi = childSelectObj.options[k];
                //alert(csi.text);
                if(csi.text==srcOpt.text && csi.value==srcOpt.value){
                    duplicate = true;break;
                }
            }
            if(!duplicate){
                var newopt = new Option(html_entity_decode(srcOpt.text), html_entity_decode(srcOpt.value), false, false);
                newopt.selected = false;
                newopt.defaultSelected = false;
                newopt.DOLOption = srcOpt;childSelectObj.options[childSelectObj.options.length] = newopt;
            }
        }
    }
}


function DOL_selectChildOptions(obj,usePreselected){
    var values = this.values[obj.name];
    var preselectedExists = false;
    if(usePreselected && values!=null && typeof(values)!="undefined"){
        for(var i=0;i<obj.options.length;i++){
            var v = obj.options[i].value;
            if(v!=null && values[v]!=null && typeof(values[v])!="undefined"){
                preselectedExists = true;break;
            }
        }
    }
    var atLeastOneSelected = false;
    for(var i=0;i<obj.options.length;i++){
        var o = obj.options[i];
        if(preselectedExists && o.value!=null && values[o.value]!=null && typeof(values[o.value])!="undefined"){
            o.selected = true;
            atLeastOneSelected = true;
        }
        else if(!preselectedExists && o.DOLOption!=null && o.DOLOption.defaultSelected){
            o.selected = true;
            atLeastOneSelected = true;
        }else{
            o.selected = false;
        }
    }
    if(this.selectFirstOption && !atLeastOneSelected && obj.options.length>0){
        obj.options[0].selected = true;
    }else if(!atLeastOneSelected &&  obj.type=="select-one"){
        obj.selectedIndex = -1;
    }
}

   function html_entity_decode (string, quote_style) {
    // Convert all HTML entities to their applicable characters
    //
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/html_entity_decode    // +   original by: john (http://www.jd-tech.net)
    // +      input by: ger
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman    // +   improved by: marc andreu
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Ratheous
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Nick Kolosov (http://sammy.ru)    // +   bugfixed by: Fox
    // -    depends on: get_html_translation_table
    // *     example 1: html_entity_decode('Kevin &amp; van Zonneveld');
    // *     returns 1: 'Kevin & van Zonneveld'
    // *     example 2: html_entity_decode('&amp;lt;');    // *     returns 2: '&lt;'
    var hash_map = {},
        symbol = '',
        tmp_str = '',
        entity = '';
        if (string != null) tmp_str = string.toString();
        else tmp_str = "";

    if (false === (hash_map = get_html_translation_table('HTML_ENTITIES', quote_style))) {
        return false;
    }

    // fix &amp; problem
    // http://phpjs.org/functions/get_html_translation_table:416#comment_97660
    delete(hash_map['&']);
    hash_map['&'] = '&amp;';
    for (symbol in hash_map) {
        entity = hash_map[symbol];
        tmp_str = tmp_str.split(entity).join(symbol);
    }
    tmp_str = tmp_str.split('&#039;').join("'");

    return tmp_str;
}


function get_html_translation_table (table, quote_style) {
    // Returns the internal translation table used by htmlspecialchars and htmlentities
    //
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/get_html_translation_table    // +   original by: Philip Peterson
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: noname
    // +   bugfixed by: Alex
    // +   bugfixed by: Marco    // +   bugfixed by: madipta
    // +   improved by: KELAN
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Frank Forte    // +   bugfixed by: T.Wild
    // +      input by: Ratheous
    // %          note: It has been decided that we're not going to add global
    // %          note: dependencies to php.js, meaning the constants are not
    // %          note: real constants, but strings instead. Integers are also supported if someone    // %          note: chooses to create the constants themselves.
    // *     example 1: get_html_translation_table('HTML_SPECIALCHARS');
    // *     returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}
    var entities = {},
        hash_map = {},        decimal = 0,
        symbol = '';
    var constMappingTable = {},
        constMappingQuoteStyle = {};
    var useTable = {},        useQuoteStyle = {};

    // Translate arguments
    constMappingTable[0] = 'HTML_SPECIALCHARS';
    constMappingTable[1] = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';

    useTable = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
    useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';


        entities['38'] = '&amp;';
        

            
            entities['160'] = '&nbsp;';
            entities['161'] = '&iexcl;';
            entities['162'] = '&cent;';
            entities['163'] = '&pound;';
            entities['164'] = '&curren;';
            entities['165'] = '&yen;';
            entities['166'] = '&brvbar;';
            entities['167'] = '&sect;';
            entities['168'] = '&uml;';
            entities['169'] = '&copy;';
            entities['170'] = '&ordf;';
            entities['171'] = '&laquo;';
            entities['172'] = '&not;';
            entities['173'] = '&shy;';
            entities['174'] = '&reg;';
            entities['175'] = '&macr;';
            entities['176'] = '&deg;';
            entities['177'] = '&plusmn;';
            entities['178'] = '&sup2;';
            entities['179'] = '&sup3;';
            entities['180'] = '&acute;';
            entities['181'] = '&micro;';
            entities['182'] = '&para;';
            entities['183'] = '&middot;';
            entities['184'] = '&cedil;';
            entities['185'] = '&sup1;';
            entities['186'] = '&ordm;';
            entities['187'] = '&raquo;';
            entities['188'] = '&frac14;';
            entities['189'] = '&frac12;';
            entities['190'] = '&frac34;';
            entities['191'] = '&iquest;';
            entities['192'] = '&Agrave;';
            entities['193'] = '&Aacute;';
            entities['194'] = '&Acirc;';
            entities['195'] = '&Atilde;';
            entities['196'] = '&Auml;';
            entities['197'] = '&Aring;';
            entities['198'] = '&AElig;';
            entities['199'] = '&Ccedil;';
            entities['200'] = '&Egrave;';
            entities['201'] = '&Eacute;';
            entities['202'] = '&Ecirc;';
            entities['203'] = '&Euml;';
            entities['204'] = '&Igrave;';
            entities['205'] = '&Iacute;';
            entities['206'] = '&Icirc;';
            entities['207'] = '&Iuml;';
            entities['208'] = '&ETH;';
            entities['209'] = '&Ntilde;';
            entities['210'] = '&Ograve;';
            entities['211'] = '&Oacute;';
            entities['212'] = '&Ocirc;';
            entities['213'] = '&Otilde;';
            entities['214'] = '&Ouml;';
            entities['215'] = '&times;';
            entities['216'] = '&Oslash;';
            entities['217'] = '&Ugrave;';
            entities['218'] = '&Uacute;';
            entities['219'] = '&Ucirc;';
            entities['220'] = '&Uuml;';
            entities['221'] = '&Yacute;';
            entities['222'] = '&THORN;';
            entities['223'] = '&szlig;';
            entities['224'] = '&agrave;';
            entities['225'] = '&aacute;';
            entities['226'] = '&acirc;';
            entities['227'] = '&atilde;';
            entities['228'] = '&auml;';
            entities['229'] = '&aring;';
            entities['230'] = '&aelig;';
            entities['231'] = '&ccedil;';
            entities['232'] = '&egrave;';
            entities['233'] = '&eacute;';
            entities['234'] = '&ecirc;';
            entities['235'] = '&euml;';
            entities['236'] = '&igrave;';
            entities['237'] = '&iacute;';
            entities['238'] = '&icirc;';
            entities['239'] = '&iuml;';
            entities['240'] = '&eth;';
            entities['241'] = '&ntilde;';
            entities['242'] = '&ograve;';
            entities['243'] = '&oacute;';
            entities['244'] = '&ocirc;';
            entities['245'] = '&otilde;';
            entities['246'] = '&ouml;';
            entities['247'] = '&divide;';
            entities['248'] = '&oslash;';
            entities['249'] = '&ugrave;';
            entities['250'] = '&uacute;';
            entities['251'] = '&ucirc;';
            entities['252'] = '&uuml;';
            entities['253'] = '&yacute;';
            entities['254'] = '&thorn;';
            entities['255'] = '&yuml;';


        if (useQuoteStyle !== 'ENT_NOQUOTES') {
            entities['34'] = '&quot;';
        }
        if (useQuoteStyle === 'ENT_QUOTES') {
            entities['39'] = '&#39;';
        }
        entities['60'] = '&lt;';
        entities['62'] = '&gt;';

        // ascii decimals to real symbols
        for (decimal in entities) {
            symbol = String.fromCharCode(decimal);

            hash_map[symbol] = entities[decimal];
        }
        


    
    return hash_map;
}
