/**
 *  ajax.js
 *
 *  JavaScript functions for AJAX
 *
 *  @version SVN:  $Id: ajax.js 213 2009-06-03 17:46:33Z rchoi $
 */



function updateSubregion()
{

    var xmlHttp;
    
    xmlHttp = getXmlHttpObj();
    
    if (! xmlHttp) {
        return;
    }
    
    xmlHttp.onreadystatechange = function() {

        if (xmlHttp.readyState == 4) {
            
            document.forms.geo.sub_id.options.length = 0;
            document.forms.geo.sub_id.options[0] =
                new Option("whole region", 0, false, true);

            xmlDoc = xmlHttp.responseXML;

            sr = xmlDoc.getElementsByTagName("subregion");
            
            for (i = 0; i < sr.length; i++) {

                id   = sr[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
                name = sr[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
                
                document.forms.geo.sub_id.options[i + 1]
                    = new Option(name, id, false, false);
      
            }
            
        }

    }

    reg_id = document.forms['geo'].reg_id.value;

    xmlHttp.open("GET", "ajax/subregions.php?reg_id=" + reg_id, true);

    xmlHttp.send(null);

}


function updateGenera()
{
    
    var xmlHttp;
    
    xmlHttp = getXmlHttpObj();
    
    if (! xmlHttp) {
        return;
    }
    
    xmlHttp.onreadystatechange = function() {
        
        if (xmlHttp.readyState == 4) {
            
            document.forms.geo.g_id.options.length = 0;
            document.forms.geo.g_id.options[0] =
                new Option("- any -", 0, false, true);
            
            xmlDoc = xmlHttp.responseXML;
            
            g = xmlDoc.getElementsByTagName("genus");
            
            for (i = 0; i < g.length; i++) {
                
                id   = g[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
                name = g[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
                
                document.forms.geo.g_id.options[i + 1]
                    = new Option(name, id, false, false);
                
            }
            
        }
        
    }
    
    t_id = document.forms['geo'].f_id.value;
    
    xmlHttp.open("GET", "ajax/genera.php?t_id=" + t_id, true);
    
    xmlHttp.send(null);
    
}


function getXmlHttpObj ()
{
    try {
        
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
        return xmlHttp;
        
    } catch (e) {
        
        // Internet Explorer
        
        try  {
            
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            return xmlHttp;
            
        } catch (e) {
            
            try {
                
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                return xmlHttp;
                
            } catch (e) {
                
                alert("Your browser does not support AJAX, and can not " +
                      "work with the geographic distribution search form. ");

                return false;
                
            }
            
        }
        
    }
    
}


function resetForm ()
{

    document.forms.geo.sub_id.options.length = 0;
    document.forms.geo.sub_id.options[0] =
        new Option("- select region first -", 0, false, true);


   document.forms.geo.g_id.options.length = 0;
   document.forms.geo.g_id.options[0] =
       new Option("- select tribe first -", 0, false, true);

   return true;
   
}

