
var lastOpenMS = new String(); // storage for the last opened multiselect.  used by multiSelectCloser div if user click off 

function initMultiSelectAR(arrayString){
    var AR = arrayString.split(';');
    var selFound = false;
    for(i=0;i<AR.length;i++){
        AR[i] = AR[i].split('|');
        if(AR[i][2]=='true'){ AR[i][2] = true; selFound = true; }
    }
    if(selFound==false){ AR[0][2] = true; }
    return AR;
}


function writeMultiSelectDD(AR,tMSid){
    var wrapDivStr = new String('<div class="selectwrapper" id="'+tMSid+'">');
    var controlDivStr = new String();
    var optionsDivStr = new String('<div id="options'+tMSid+'" class="options" style="display: none;">');
    var optStyle = new String();
    for(i=0;i<AR.length;i++){
        if(AR[i][2]==true){
            controlDivStr += '<div id="select'+tMSid+'" class="select" onclick="toggleOptions(\''+tMSid+'\');"><div id="selectInner'+tMSid+'" class="selectInner">'+AR[i][1]+'</div></div>';
            optStyle = 'style="background-color:#36c;color:#fff;"'
        } else {
            optStyle = '';
        }
        optionsDivStr += '<div class="option" '+optStyle+' onmouseover="optionHilite(this);" onmouseout="optionDeHilite(this);" onclick="updateMultiSelect(\''+tMSid+'\',ar'+tMSid+','+i+');">'+AR[i][1]+'</div>';
    }
    optionsDivStr += '</div>';
    return wrapDivStr+controlDivStr+optionsDivStr+'</div>';
}


function optionHilite(tDiv){
    tDiv.style.backgroundColor = '#36c';
    tDiv.style.color = '#fff';
}
function optionDeHilite(tDiv){
    tDiv.style.backgroundColor = '#fff';
    tDiv.style.color = '#000';
}

function toggleOptions(tID){

    document.getElementById('selectInner'+tID).style.border = '1px dotted #999';
    var tOpt = document.getElementById('options'+tID);    
    
    if(tOpt.style.display == 'none'){
        tOpt.style.display = 'block';
        var tWidth = tOpt.scrollWidth;
        var origWidth = tWidth;
        var selectWidth = document.getElementById('select'+tID).scrollWidth;
        if(tWidth < selectWidth){ tWidth = selectWidth-2; } //-2 to account for borders
        if(tWidth==origWidth){ tWidth = tWidth+25; }
        tOpt.style.width = tWidth+'px';
        var tHeight = tOpt.scrollHeight;
        if(tHeight > 200){tHeight = 200};
        tOpt.style.height = tHeight+'px';
        tOpt.style.zIndex = '10000';
        lastOpenMS = tID;
        var MScloser = document.getElementById("multiSelectCloser");
        MScloser.style.height = document.documentElement.scrollHeight+50+'px';
        MScloser.style.display = 'block';
    } else {
        tOpt.style.zIndex = '1';
        tOpt.style.display = 'none';
        document.getElementById('multiSelectCloser').style.display = 'none';
        document.getElementById('selectInner'+tID).style.borderColor = '#fff';
    } 
}

function updateMultiSelect(tMSid,tAR,selAddr){
    for(i=0;i<tAR.length;i++){
        tAR[i][2] = false;
    }
    tAR[selAddr][2] = true;
    document.getElementById(tMSid).parentNode.innerHTML = writeMultiSelectDD(tAR,tMSid);
    document.getElementById("multiSelectCloser").style.display = 'none';
    toggleMultiSelectDD(tAR[selAddr][0],tMSid);
}

function disableSelect(tMSid){
    var tSel = document.getElementById('select'+tMSid);
    if(tSel){
        tSel.onclick = null;
        tSel.style.backgroundColor = '#ddd';
        tSel.style.color = "#999";
        document.getElementById('selectInner'+tMSid).style.backgroundImage = 'url(/assets/images/selectArrow-off.gif)';
   }
}


function toggleMultiSelectDD(tCom){
    if(tCom!=''){
        var comAR = tCom.split(':');
        if(comAR[0]=='activate'){
            var tARname = comAR[1];
            var tID = tARname.slice(2,tARname.length);
            document.getElementById('choice2').innerHTML = writeMultiSelectDD(eval(tARname),tID);
        } else if(comAR[0]=='load') {
            document.location = comAR[1];
        } else if(comAR=='disablelev2'){ // equals first option of first select.  reset second select to disabled select.
            document.getElementById('choice2').innerHTML = writeMultiSelectDD(ardefaultlev2,'defaultlev2');
            disableSelect('defaultlev2');
        }
    }
}
