// JavaScript Document

  /** 
   *  Base class containing useful methods
   */     

var AntzBase = {


    /** 
     *  3 dimensional array service
     *  Loop through an array of arrays, looking for matching key=>value pairs
     *  Returns a new array of arrays that match the search request
     */
                        
    findArraysWithThisValue : function(key, value, array){
        var myArray = Array();
        var count = 0;
        //alert('looking for '+key+' = '+value);
        for(var i=0;i<array.length;i++){
        //alert(array[i][key]);
            if(array[i][key]==value){
           // alert('count '+count+': '+array[i]['title']+' '+key+' '+array[i][key]);
                myArray[count]=array[i];
                count++;
            };
        };
        //alert('found: '+myArray.length);
        return myArray;
    },










    /**
     * Creates and returns a compatible Ajax Object depending on the browser    
     * 
     */          

    createAjax : function(){
        if (window.XMLHttpRequest) { 
            return new XMLHttpRequest();
        } else if (window.ActiveXObject) { 
            return new ActiveXObject("Microsoft.XMLHTTP");
        };
    },
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**
     *  Dom style of adding raw text to a node
     */       
       
    addText : function(node, text, overwrite){ 
    
// for testing 
//alert(text);
    
        if (null == node.canHaveChildren || node.canHaveChildren) {
            if(overwrite === true){
                while(node.childNodes.length > 0){
                     node.removeChild(node.firstChild);
                };
            };
            node.appendChild(document.createTextNode(text));
        } else {
            if(overwrite === true){
                node.text = '';
            };
            node.text += text;
        }; 
    },
    
    
    
    
    
    
    
    
    
    
    
    /** 
     *  Sorts and returns a numeric array. Can sort asc or desc
     *  
     */
                   
    sortArrayByKey : function(array, key, direction){
        if(typeof(direction) === 'undefined'){
            direction = 'asc';
        };
        
        if(direction.toLowerCase() == 'asc'){
            direction = 'asc';
        }else if(direction.toLowerCase() == 'desc'){
            direction = 'desc';
        }else{
            direction = 'asc';
        };
      
        if(direction == 'asc'){
            var count = 0;
            var iterator = 0;
            var myArray = Array();
            while(myArray.length < array.length){
                for(var i=0;i<array.length;i++){
                    if(typeof(array[i][key]) === 'undefined' || array[i][key]==iterator || 
                       typeof(array[i][key]) === 'NaN'){
                        myArray[count] = array[i];
                        count++;
                    };
                };
                iterator++;
            };
            
            return myArray;
        
        
        }else if(direction == 'desc'){
            array = this.sortArrayByKey(array, key, 'asc');
            count = array.length-1;
            var myArray = Array();
            
            for (var i=0;i<array.length;i++){
                myArray[count]=array[i];
                count--;                    
            };
            return myArray;
        
        }else{
            return array;
        };
    },
    
    
    
    
    
    
    
    
    
    /**  
     *  Get the current selected KEY of a drop-menu. Attach to the onchange 
     *  event handler, for example
     *  
     */
     
    dom_dropMenuSelectionKey : function (id)
    
    {
        if(!document.getElementById(id)){return false;};
        var x=document.getElementById(id);
        return x.options[x.selectedIndex].value;
    },








    /**  
     *  Get the current selected VALUE of a drop-menu. Attach to the onchange 
     *  event handler, for example
     *  
     */
     
    dom_dropMenuSelectionText : function (id)
    
    {
        if(!document.getElementById(id)){return false;};
        var x=document.getElementById(id);
        return x.options[x.selectedIndex].text;
    },
    
    
    
    
    
    /**
     *  Can use this to disable fields that don't apply in case of a drop menu selection
     *  Can take array or variable values
     *  EG:
     *    
     *    document.getElementById('myDropMenu').onchange=function(){
     *        var sKeys = Array('3', '8');
     *        var dIds = 'Name'; 
     *        AntzBase.dom_toggleDisabledForSelectOnChange('myDropMenu', sKeys, dIds, true);
     *     }
     *    
     *  In the above example, if the key of the selected item == 3 or 8, 
     *  document.getElementById('Name').disabled = disabled;
     *  For any other key, document.getElementById('Name').disabled = null;     
     *  
     */                         
    
    dom_toggleDisabledForSelectOnchange : function (selId, selKeys, disIds, disable)
    {
        if(disable === true || disable === 'disabled'){
            var d1 = 'disabled';
            var d2 = null;
        }else if (disable === false || disable === null){
            var d1 = null;
            var d2 = 'disabled';
        }else{
            var d1 = 'disabled';
            var d2 = null;
        };
        
        var id = AntzBase.dom_dropMenuSelectionKey(selId);
        
        var found = false;
        
        if(typeof(selKeys) == 'Array'){
            for(var i=0;i<selKeys.length;i++){
                if(selKeys[i]===id){
                    found = true;
                };
            };// end for
        }else{
            if(selKeys == id){
                found = true;
            };
        };
    
        if(typeof(disIds) == 'Array'){
            for(var i=0;i<disIds.length;i++){
                if(document.getElementById(disIds[i])){
                     document.getElementById(disIds[i]).disabled = (found) ? d1 : d2;
                };            
                
            };// end for
        }else{
            if(document.getElementById(disIds)){
                 document.getElementById(disIds).disabled = (found) ? d1 : d2;
            };
        };
    }
    
    
    
    
  
}
