/********************************************************

method getElementsBy
ritorna un array con tutti i valori = oggetto HTML
Obj_Att: il nome dell' attributo: width,height ecc..
Par_A  : il valore dell' attributo



method  inArray
ritorna un valore dove:
se s_key non impostato effettua la ricerca nei valori dell' array,e ritorna la chiave della prima occorrenza di s_value, altrimenti false
se s_key impostato effettua la ricerca nelle chiavi dell' array e ritorna true, altrimenti false


// method ListAttribute
// ritorna un array con tutti i valori per quell' attributo
// att: il nome dell' attributo: width,height ecc..
// how: se non impostato ritorna un array con tutti ivalori per l' aattributo richiesto
//      se true ritorna un array con indice= numero del nodo (relativo alla posizione di ricerca, valore= oggetto HTML)


*********************************************************/

// CORE 

/******************************************************************************************************************************************
* Script liberamente utilizzabile, a patto di lasciare inalterato e in cima questo commento
* Libreria realizzata da Mega69 --- http://www.sitomega.net
* Core della libreria ExtendDOM
*****************************************************************************************************************************************/


methods=[]; //Collezione di tutti i metodi creati
_IE___=navigator.appName=="Microsoft Internet Explorer";

if(!_IE___&&!HTMLElement) 
  { //Per Safari
	HTMLElement=function() {}; 
	HTMLElement.prototype = window["[[DOMElement.prototype]]"]||function() {}; 
  }

//EXTENDOM - Estende il DOM creando metodi applicabili a tutti gli oggetti del DOM ------------------------------------------

function ExtendDOM(nmet, met) 
  {
	window[nmet]=met;
	document[nmet]=met;
		if(_IE___) 
			{
		/*Con IE ?ecessario associare manualmente i metodi a tutti gli oggetti, compresi document e window*/
			document.attachEvent("onreadystatechange", function() 
				{
				for(var i=0, l=document.all.length; i!=l; i++)
					{
						document.all[i][nmet]=met;
					}
				extendCreateElement()
				});
			}
		else
			{
			/*Con Firefox, Opera e Safari  basta usare un prototipo*/
			HTMLElement.prototype[nmet]=met;
			}	
  methods[nmet]=met;	 
  }

//Questa funzione fa ereditare i prototipi degli elementi creati con document.createElement anche ad IE.-----------------

function extendCreateElement() 
  {
	if(_IE___) 
    { //(C)webreflection.blogspot.com
		document.createElement = (function(createEl, El)
      {
			return function(node)
        {
				var el, key;
				el = createEl(node)
				for(key in El)
					{
					el[key] = El[key];
					}
  			return el;
		    }
		  })(document.createElement, methods);
    }
  }


//Funzioni per estendere i costruttori nativi e non
function ___extend___(methods) 
  { 
	for(var i in methods) 
		{
		this.prototype[i]=methods[i];
		}
  }

DOM={ '_constructor_':'HTMLElement' };


     DOM.extend		 =		function(methods) { for(var i in methods) { ExtendDOM(i, methods[i]); } };
  String.extend		 =		___extend___;
  Number.extend		 =		___extend___;
   Array.extend		 =		___extend___;
Function.extend		 =		___extend___;
    Date.extend		 =		___extend___;
  RegExp.extend		 =		___extend___;
 // END CORE


  
var ObjJsFunctions= {
                    isElement: function() 
                      {
                      return this && this.nodeType == 1;
                      },
            
                    isBoolean: function()
                      {
                      return typeof this == "boolean";
                      },
                      
                    isTrue: function() 
                      {
                      return (typeof this == 'boolean' && this == true);
                      },
                    
                    isFalse: function()
                      {
                      return (typeof this == 'boolean' && this == false);
                      },
                    
                    isNull: function()
                      {
                      return (this === null);
                      },
            
                    isArray: function() 
                      {
                      return this && this.constructor === Array;
                      },
                  
                    isHash: function() 
                      {
                      return this instanceof Hash;
                      },
                  
                    isFunction: function() 
                      {
                      return typeof this == "function";
                      },
                  
                    isString: function() 
                      {
                      return typeof this == "string";
                      },
                  
                    isNumber: function() 
                      {
                      return typeof this == "number";
                      },
                  
                    isUndefined: function() 
                      {
                      return typeof this == "undefined";
                      },
            
                    inArray: function(s_value,s_key)
                      {
                      if (typeof s_key !='boolean') var s_key=false;
                      
                      if (s_key == false)     // in values
                        {
                        for (x in this)
                          {
                          if (this[x] == s_value) return x;
                          }
                        return false;
                        }
                      else                     // in keys
                        {
                        for (x in this)
                          {
                          if (x == s_value) return true;
                          }
                        return false;
                        }
                      },
                    isEmpty: function()
                      {
                      if (typeof this == 'string' || this.constructor === Array)
                        {
                        return (this.length < 1);
                        }
                      else return null;
                      }
                    };



var HTMLDOMFunctions= {
                      getElementsBy:function (Els)//Obj_Att,Par_A)
                        {
                        if (typeof Els.attribute !='string') return false; 
                      
                        var Out=[];
                      
                        switch (Els.attribute.toLowerCase())
                          {
                          case 'tagname':

                            if (typeof Els.value != 'string' && typeof Els.value != 'number') var Value='*';
                            else                                                              var Value=Els.value;
                      
                            var Out=this.getElementsByTagName(Value);
                            
                          break;
                      
                          default:
                            var ValueSpecified  = false;
                            
                            if (typeof Els.value == 'string' || typeof Els.value == 'number') ValueSpecified=true;
                      
                            var OutOnValue =[];
                          
                            var TheList=this.getElementsByTagName('*');
                            for (var x=0; x < TheList.length; x++)
                              {
                              if (typeof(TheList[x].getAttribute(Els.attribute)) == 'string' && TheList[x].getAttribute(Els.attribute) == Els.value)
                                {
                                if (ValueSpecified == false)  Out.push(TheList[x]);
                                if (ValueSpecified == true  && TheList[x].getAttribute(Els.attribute) == Els.value) OutOnValue.push(TheList[x]); 
                                }
                              }
                          
                            if (ValueSpecified == false)    return Out;
                            else                            return OutOnValue;
                          break;
                          }
                        return Out;
                        },

                      ListAttribute: function(Els)//Att,How)
                        {
                        var Out=[];
                        if (typeof Els.attribute != 'number' || typeof Els.attribute != 'string') return false;
                        
                        if (typeof How !='boolean') var How=false;
                        else                        var How=Els.objects;
                        
                        var TheList=this.getElementsByTagName('*');
                        for (var x=0; x < TheList.length; x++)
                          {
                          if (typeof TheList[x].getAttribute(Els.attribute) == 'string') 
                            {
                                 if( How == false ) Out.push(TheList[x].getAttribute(Att));  
                            else if( How == true  ) Out[x]=TheList[x];  
                            }
                          }
                        return Out;
                        }
                      };

var HTMLSelectFunctions={
                        getSelectedIndex: function()    // OK
                          {
                          return this.selectedIndex;
                          },
                        
                        getListOptions: function()      // OK
                          {
                          var Out=[];
                          for (var i=0; i < this.options.length; i++)
                            {
                            Out[i]= {
                                    index: i,
                                    value: this.options[i].value,
                                    text:  this.options[i].text
                                    };
                            }
                          return Out;
                          },
                        
                        getOptionsBy: function(Els)  // OK
                          {
                          var Att       =(typeof Els.attribute == 'string' )? Els.attribute : (typeof Els.attribute  == 'function')? Els.attribute() : '';
                          var Value     =(typeof Els.value     == 'string' || typeof Els.value     == 'string')? Els.value     : (typeof Els.value      == 'function')? Els.value()     : '';
                          var OnlyOne   =(typeof Els.unique    == 'boolean')? Els.unique    : (typeof Els.unique     == 'function' && Els.unique() == true )? Els.unique() : false;
                          
                          if(OnlyOne == false) var Out=[];
                          
                          for (var i=0; i < this.options.length; i++)
                            {
                            switch (Att.toLowerCase())
                              {
                              case 'index':
                                if (this.options[Value] != null)
                                  {
                                  var Out={
                                          index: Value,
                                          value: this.options[Value].value,
                                          text: this.options[Value].text
                                          };
                                  return Out;
                                  }
                                else return null;
                              break;
                              case 'value':
                              case 'text':
                                var Match=false;
                                eval('if (this.options[i].'+Att+' == Value) Match = true;')

                                if ( Match == true)
                                  {
                                  if (OnlyOne)
                                    {
                                    var Out= {
                                              index: i,
                                              value: this.options[i].value,
                                              text:  this.options[i].text
                                             };
                                    return Out;
                                    }
                                  else
                                    {
                                    var MyOpt={
                                               index: i,
                                               value: this.options[i].value,
                                               text:  this.options[i].text
                                              };
                                    Out.push(MyOpt);
                                    }
                                  } // end match
                              
                              break;
                              } // end switch request

                            } // end for options
                          
                          return Out;
                          },

                        getSelectedOption: function()
                          {
                          var Out=  {
                                    index: this.selectedIndex,
                                    text : this.options[this.selectedIndex].text,
                                    value: this.options[this.selectedIndex].value
                                    }
                          return Out;
                          },
                        
                        appendOption: function(Els)
                          {
                          var OptText    =(typeof Els.text == 'number' || typeof Els.text == 'string')? Els.text : (typeof Els.text == 'function')? Els.text() : '';
                          var OptValue   =(typeof Els.value == 'number' || typeof Els.value == 'string')? Els.value : (typeof Els.value == 'function')? Els.value() : '';

                          this.options[this.options.length]=new Option(OptText,OptValue);
                          },
                        
                        insertOption : function(Els)
                          {                          
                          //alert(typeof Els.index);
                          var Index      =(typeof Els.index == 'number' || typeof Els.index == 'string')? parseInt(Els.index) : (typeof Els.index == 'function')? Els.index() : 0;
                          var OptText    =(typeof Els.text == 'number' || typeof Els.text == 'string')? Els.text : (typeof Els.text == 'function')? Els.text() : '';
                          var OptValue   =(typeof Els.value == 'number' || typeof Els.value == 'string')? Els.value : (typeof Els.value == 'function')? Els.value() : '';

                          var newOptions=[];
                          
                          for (var x=0; x < this.options.length;  x++)
                            {
                            if (x == Index)
                              {
                              newOptions.push(new Option(OptText,OptValue));
                              }
                            newOptions.push(new Option(this.options[x].text,this.options[x].value));
                            }
                          for (var z=0; z < this.options.length;  z++)  this.options[x]=null;
                          //alert(newOptions.length);
                          for (var y=0; y < newOptions.length; y++)
                            {
                            this.options[y]=newOptions[y];
                            }
                          },

                        replaceOption: function(Els)
                          {
                          var Index      =(typeof Els.index == 'number')? Els.index : (typeof Els.index == 'function')? Els.index() : 0;
                          var OptText    =(typeof Els.text == 'number' || typeof Els.text == 'string')? Els.text : (typeof Els.text == 'function')? Els.text() : '';
                          var OptValue   =(typeof Els.value == 'number' || typeof Els.value == 'string')? Els.value : (typeof Els.value == 'function')? Els.value() : '';


                          this.options[Index]=new Option(OptText,OptValue);                             
                          },
                        
                        removeOption: function(Index) // OK
                          {
                          var TheIndex      =(typeof Index == 'number' || typeof Index == 'string')? parseInt(Index) : (typeof Index == 'function')? Index() : false;

                          if (TheIndex === false) return TheIndex;
                          
                          this.options[TheIndex]=null;                             
                          },
                        
                        removeAllOptions: function() // OK
                          {
                          for (var x=this.options.length; x >= 0; x--)
                          this.options[x]=null;
                          }
                        }


DOM.extend(ObjJsFunctions);
DOM.extend(HTMLDOMFunctions);
DOM.extend(HTMLSelectFunctions);


