//**********************************
//       CLASSE CustomSELECT
//**********************************
function CustomSELECT(nome, testo, valore){    
    this.Select_Name           = nome
    this.Select_Text           = testo
    this.Select_Value          = valore
    
    this.Elem_SelectContainer  = 'SelectContainer_' + this.Select_Name
    this.Elem_SelectInput      = 'SelectInput_'     + this.Select_Name
    this.Elem_SelectMenu       = 'SelectMenu_'      + this.Select_Name    
    
    this.class_SelectContainer = ''
    this.class_SelectInput     = ''
    this.class_SelectMenu      = ''
    
    this.onchange              = ''
    this.margin                = ''
    this.width                 = ''
    
    this.Item_Index            = 1
    this.Elem_Item             = 'SelectMenuItem_' + this.Select_Name + this.Item_Index  
    this.Items                 = ''
                    
    this.AddItem = function (txt, val){
          //  <a href="javascript:CustomSELECT.Valorizza('InputHidden_Name','testo','valore')">Italia</a>                        
          this.Items      += '<a ID=' + this.Elem_Item + ' href="javascript:CustomSELECT.Valorizza(\'' + this.Select_Name + '\',\'' + val.replace("'","\'") + '\',\'' + this.Elem_Item + '\');' + this.onchange + '">' + txt + '</a>'                   
          this.Item_Index += 1
          this.Elem_Item   = 'SelectMenuItem_' + this.Select_Name + this.Item_Index 
    }
            
    this.WriteSELECT = function () {            
          var Style_Container = ''
          var Style_Menu      = ''
          var width           = this.width
          
          width = width.replace('px','')
          
          if (this.width!=''){
              Style_Container = ' STYLE="width:' + width + 'px;'
              width          -= 2
              Style_Menu      = ';width:' + width + 'px'  
          }
          if (this.margin!=''){
              if (Style_Container==''){
                  Style_Container = ' STYLE="'
              }
              Style_Container += ('margin:' + this.margin + ';')
          }    
          
          Style_Container += '"'
            
          document.write ('<input type="hidden" name="' + this.Select_Name + '" value="' + this.Select_Value + '">')
          document.write ('<div id="' + this.Elem_SelectContainer + '" class="' + this.class_SelectContainer + '" '+ Style_Container +'>')
	      document.write ('      <div id="' + this.Elem_SelectInput + '" class="' + this.class_SelectInput + '" onclick="CustomSELECT.ApriMenu(\'' + this.Select_Name + '\');doSomething(event)" >' + this.Select_Text + '</div>')
	      document.write ('      <div id="' + this.Elem_SelectMenu + '"  class="' + this.class_SelectMenu + '" style="visibility:hidden' + Style_Menu + '">')

          document.write (this.Items)

          document.write ('      </div>')
          document.write ('</div>')
    }
}

//--- Dichiarazione metodi statici della classe CustomSELECT 
CustomSELECT.ApriMenu = function(SelectName){
    var Elem_Values = "SelectMenu_" + SelectName  
    var ElemMenu    = document.getElementById(Elem_Values)
    var app_visibility  = ElemMenu.style.visibility
    
    CustomSELECT.NascondiMenu()
    
    ElemMenu.style.visibility = app_visibility
          
    if (ElemMenu.style.visibility == "hidden"){
        ElemMenu.style.visibility = "visible";
    }else{
        ElemMenu.style.visibility = "hidden";
    }
}

CustomSELECT.Valorizza = function(SelectName, val, elemItem){
    var Elem_Values   = "SelectMenu_"  + SelectName  
    var Elem_Selected = "SelectInput_" + SelectName
            
    document.getElementsByName(SelectName)[0].value       = val;
    document.getElementById(Elem_Selected).innerHTML      = document.getElementById(elemItem).innerHTML;
    document.getElementById(Elem_Values).style.visibility = "hidden";    
}

CustomSELECT.NascondiMenu = function(){
    var Elem_SelectMenu = 'SelectMenu_'
    var TagSELECT = document.getElementsByTagName("DIV"); 
    var i, SelectMenu_ID

    for (i=0; i<TagSELECT.length ;i++){
        SelectMenu_ID = TagSELECT[i].getAttribute("ID"); 
        
        if (SelectMenu_ID!=null) {
            if (SelectMenu_ID.indexOf(Elem_SelectMenu)!=-1){
                TagSELECT[i].style.visibility = "hidden";    
            }        
        }    
    }        
}
//**********************************
//     FINE CLASSE CustomSELECT
//**********************************


//--- al click sul documento nasconde tutti i menu a tendina aperti nella pagina
document.onclick = function() 
{     
    CustomSELECT.NascondiMenu()
} 

// Funzione di Stop propagazione evento
function doSomething(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}