var jsDrop_menus = new Array;

document.onclick = function(){
	jsDrop_doc_onclick();
}

function jsDrop_doc_onclick(){
	for(var i in jsDrop_menus){
   	if (jsDrop_menus[i]['visstate'] == 'down' && jsDrop_menus[i]['docclose'] == 'freshly opened'){
      	jsDrop_menus[i]['docclose'] = 'ready';
      }else if (jsDrop_menus[i]['visstate'] == 'down' && jsDrop_menus[i]['docclose'] == 'ready'){
      	jsDrop_menus[i]['docclose'] = 'closed';
         if (jsDrop_menus[i]['oncancel'] != undefined) eval(jsDrop_menus[i]['oncancel']);
			jsDrop_dropmenu(i);
      }
   }
}

function jsDrop_setStyle(element, styleAttr, val){
 // 	if (element type_of )
 	if (typeof(element) == "string") element = document.getElementById(element);
 	element.style[styleAttr] = val;
 	return element;
}

function jsDrop_addClass(element, name){
    if (typeof(element) == "string"){
        theElement = document.getElementById(element);
    }else if (typeof(element) == "object"){
        theElement = element
    }
    if (theElement != null){
        if (theElement.className.indexOf(name)<0)
            theElement.className += " " + name;
    } else {alert("cannot find element to add: "+element); }

//    if (typeof(element) == "object"){
    return element;
    //}
}

function jsDrop_removeClass(element, name){
    if (typeof(element) == "string"){
        theElement = document.getElementById(element);
    }else if (typeof(element) == "object"){
        theElement = element
    }
    if (theElement != null){
        var cnameregex = new RegExp(name, 'gi');
        theElement.className = theElement.className.replace(cnameregex, '');
    }else{ alert("cannot find element to remove: "+element); }
   
    return element;
}

function createCustomDropdown(id, otherOptions){
	if (otherOptions == undefined) otherOptions = new Array;
   if (otherOptions['onclick'] == undefined) otherOptions['onclick'] = '';
   if (otherOptions['onchange'] == undefined) otherOptions['onchange'] = '';
   if (otherOptions['oncancel'] == undefined) otherOptions['oncancel'] = '';
   if (otherOptions['extra'] == undefined) otherOptions['extra'] = '';
   if (otherOptions['class'] == undefined) otherOptions['class'] = 'jsDrop';
   
   jsDrop_menus[id] = new Array;
   if (otherOptions['onchange'] != '') jsDrop_menus[id]['onchange'] = otherOptions['onchange'];
   if (otherOptions['oncancel'] != '') jsDrop_menus[id]['oncancel'] = otherOptions['oncancel'];

   jsDrop_menus[id]['options'] = new Array;
   
   document.write('<div class="'+otherOptions['class']+'"><div class="jsDrop_window" id="'+id+'_window" onclick="jsDrop_dropmenu(\''+id+'\'); if (jsDrop_menus[\''+id+'\'][\'oncancel\'] != undefined) eval(jsDrop_menus[\''+id+'\'][\'oncancel\']); '+otherOptions['onclick']+'" '+otherOptions['extra']+'><input type="hidden" name="'+id+'" id="'+id+'"/><div id="'+id+'_windowLabel" class="jsDrop_windowLabel">&nbsp;</div><div id="'+id+'_but" class="jsDrop_but jsDrop_but_up">&nbsp;</div></div><div id="'+id+'_menuContainer" class="jsDrop_menuContainer"><div id="'+id+'_menu" class="jsDrop_menu" onclick="jsDrop_dropmenu(\''+id+'\'); if (jsDrop_menus[\''+id+'\'][\'oncancel\'] != undefined) eval(jsDrop_menus[\''+id+'\'][\'oncancel\']);">&nbsp;</div></div></div>');
}


function setSelected(obj, val){
	jsDrop_selectItem(obj, val);
}

function removeAllOptions(id){
	document.getElementById(id+"_menu").innerHTML = '';
	document.getElementById(id+"_windowLabel").innerHTML = '';
   jsDrop_menus[id]['current_value'] = false;
   jsDrop_menus[id]['current_label'] = false;
   document.getElementById(id).value = '';
}

function jsDrop_dropmenu(id){
	if (jsDrop_menus[id]['visstate'] == 'down'){
		jsDrop_setStyle(id+"_menu", 'display', 'none');
      jsDrop_menus[id]['visstate'] = 'up';
      jsDrop_removeClass(id+"_but", 'jsDrop_but_down');
      jsDrop_addClass(id+"_but", 'jsDrop_but_up');
   
   }else{
		jsDrop_setStyle(id+"_menu", 'display', 'block');
      jsDrop_menus[id]['visstate'] = 'down';
      jsDrop_menus[id]['docclose'] = 'freshly opened';
      jsDrop_removeClass(id+"_but", 'jsDrop_but_up');
      jsDrop_addClass(id+"_but", 'jsDrop_but_down');
   }
}

function jsDrop_selectItem(drop, value, label){
	if (label == undefined) label = jsDrop_menus[drop]['options'][value];

	jsDrop_menus[drop]['current_value'] = value;
	jsDrop_menus[drop]['current_label'] = label;
   document.getElementById(drop).value = value;
   document.getElementById(drop+"_windowLabel").innerHTML = label;
 
   if (jsDrop_menus[drop]['onchange'] != '' && jsDrop_menus[drop]['onchange'] != undefined){
   	eval(jsDrop_menus[drop]['onchange']);
   }

}

function addOption(obj,text,value,selected,extra) {

   if (extra == undefined) extra = new Array();
   
   if (extra['window_text'] == undefined) extra['window_text'] = text;
   if (extra['extra'] == undefined){ extra['extra'] = ''; }
   if (extra['onclick'] == undefined){ extra['onclick'] = ''; }
   if (extra['onmouseover'] == undefined){ extra['onmouseover'] = ''; }
   if (extra['class'] == undefined){ extra['class'] = 'jsDrop_option'; }
   if (extra['over_class'] == undefined){ extra['over_class'] = 'jsDrop_option_over'; }
   
   
	valueString = new String(value);
	valueString=valueString.replace(new RegExp('"','g'), '\\\"')
	valueString=valueString.replace(new RegExp('\'','g'), '\\\'')
   extraWindow_textString = new String(extra['window_text']);
   extraWindow_textString = extraWindow_textString.replace(new RegExp('"','g'), '\\\"');
   extraWindow_textString = extraWindow_textString.replace(new RegExp("'",'g'), "\\\'");
   extraOnmouseover = new String(extra['onmouseover']);
   extraOnmouseover = extraOnmouseover.replace(new RegExp("'",'g'), "\\\"");
   extraOnclick = new String(extra['onclick']);
   extraOnclick = extraOnclick.replace(new RegExp("'",'g'), "\\\'");
   
	var opt_code;
   
   opt_code = "<div id='"+obj+"-"+value+"' onclick='jsDrop_selectItem(\""+obj+"\", \""+valueString+"\", \""+extraWindow_textString+"\"); "+extraOnclick+"' onmouseover='jsDrop_addClass(this, \""+extra['over_class']+"\");"+extraOnmouseover+"' onmouseout='jsDrop_removeClass(this, \""+extra['over_class']+"\");"+extra['onmouseout']+"' class='"+extra['class']+"'><div class='jsDrop_option_inner'>"+text+"</div></div>";
   
	if (document.getElementById(obj+"_menu") == undefined) alert("canna find "+obj+"+_menu");
   
   if (document.getElementById(obj+"_menu").innerHTML == '' || selected){
   	jsDrop_selectItem(obj, value, extra['window_text']);
   }  
	document.getElementById(obj+"_menu").innerHTML = document.getElementById(obj+"_menu").innerHTML + opt_code;

   jsDrop_menus[obj]['options'][value] = extra['window_text'];
   
   // !!! if selected==true, set as such
}

function addOptgroup(obj, oData, extra){
   if (extra == undefined){ extra = new Array; }
//   if (extra['window_text'] == undefined){ extra['window_text'] = text; }
   if (extra['extra'] == undefined){ extra['extra'] = ''; }
   if (extra['onclick'] == undefined){ extra['onclick'] = ''; }
   if (extra['class'] == undefined){ extra['class'] = 'jsDrop_optgroup'; }
   
	var opt_code;
   
   opt_code = "<div id='"+obj+"-"+oData[0]+"' onclick='if (jsDrop_menus[\""+obj+"\"][\"oncancel\"] != undefined) eval(jsDrop_menus[\""+obj+"\"][\"oncancel\"]);"+extra['onclick']+"' class='"+extra['class']+"'><div class='jsDrop_optgroup_inner'>"+oData[0]+"</div></div>";
    
   document.getElementById(obj+"_menu").innerHTML = document.getElementById(obj+"_menu").innerHTML + opt_code;

   var extra;
   var def;
   for (var i in oData[1]){
   	extra = (oData[1][i][3] == undefined) ? new Array : oData[1][i][3];
   	def = (oData[1][i][2] == undefined) ? false : oData[1][i][2];
		addOption(obj, oData[1][i][0], oData[1][i][1], def, extra);
   }

}



