
function addEvent(oTarget, sType, fpDest) {
  sType = 'on'+sType;
  var oOldEvent = oTarget[sType];
  if (typeof oOldEvent != "function") {
    oTarget[sType] = fpDest;
  } else {
    oTarget[sType] = function(e) {
      oOldEvent(e);
      fpDest(e);
    }
  }
}
 
  toplist = new Object;
  loadtype = '';
  xmlhttp = null;
var junk = ( navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Mac') != -1 );

setLoadType();

search_init_run = false;

addEvent( window, 'load', search_init );
addEvent( document, 'load', search_init );

function search_init()
{
   if ( search_init_run) return;

   search_init_run = true;

   var target = document.getElementById('Location');

   if ( target != null && !junk )
   {
       addEvent( target, 'change', loadData );

       if ( target.selectedIndex != 0 )
       {
          loadData(target);
       }

   }
}

function clean( target )
{
   if ( junk ) 
   {
      target.innerHTML = '';
   }
   else
   {
      while ( target.childNodes.length > 0 )
      {
         target.removeChild(target.firstChild)
      }
   }

}



  function setLoadType()
  {

     try {
	if ( window.XMLHttpRequest )
        { loadType = 'saf'; }
     }
     catch(e){1}
     try{ 
        if (document.implementation && document.implementation.createDocument)
	{ loadtype = 'moz';}
     }
     catch(e){1}
     try{
	if (window.ActiveXObject){loadtype = 'ie';}
     }
     catch(e){1}

     if ( ! loadtype && ! junk ) { loadtype = 'opera'; }	
    
     if ( ! loadtype ) { loadtype = 'junk' }
     
  
  }

  function operaLoad( val ){document.getElementById('loader').src = "/location/loc_"+val+".js";} 

  function mozLoad( val )
  { 
     xmlhttp = document.implementation.createDocument("", "", null); 
     xmlhttp.onload = fill_list;
     genLoad( val );
  }

  function IELoad( val )
  { 
    xmlhttp = new ActiveXObject("Microsoft.XMLDOM");
    xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4) fill_list(); };
    genLoad( val );
  }

  function SafLoad( val )
  {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4) fill_list(); };
    
    var url = "/locations/loc_"+val+".xml";
    xmlhttp.open("GET", url, true);
    xmlhttp.send();    
  }


  function genLoad( val )
  {
     var url = "/locations/loc_"+val+".xml";
     xmlhttp.load( url);
  }

  function fill_list()
  {
      var doc = xmlhttp;
     if ( !doc ) return false;
     var locs = doc.getElementsByTagName('l');

     var list = [];
     for ( i =0; i < locs.length; i++)
     {
	
        var name = locs[i].childNodes[0].nodeValue || ''; 
	var id = locs[i].getAttribute('id') || 0;
	
        list[list.length] = [0,id,name];
     }
     toplist[val] = list;
     update_location();

  }


function loadData(e)
  { 

    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    else targ = e; // let us pass the script the select box for manual update
    if (targ.nodeType == 3) // defeat Safari bug
	targ = targ.parentNode;

    val = targ.options[targ.selectedIndex].value;
    if ( val == "" || val == "any") { update_location(); return; }

    if ( !loadtype ) { setLoadType() }

    if ( loadtype == 'opera' && ! toplist[val] )
    {   
	// hide_location();
        // operaLoad(val); 
    }
    else if ( loadtype == 'moz' )
    {
	hide_location();
	if ( ! toplist[val] )
	{
          mozLoad(val);
	}
        else
	{
	  update_location( );
	}
    }
    else if ( loadtype ==  'ie' )
    {
	hide_location();
	if ( ! toplist[val] )
	{
           IELoad(val);
	}
	else
	{
	   update_location();
	}
    }
    else if ( loadtype ==  'saf' )
    {
        hide_location();
        if ( ! toplist[val] )
        {
           SafLoad(val);
        }
        else
        {
           update_location();
        }
    }
    else if ( loadtype == 'junk' )
    {
  	// hide_location();
	// update_location();
    }

  }

function hide_location()
{
   var target = document.getElementById('moreLocalSelector');
   target.style.display = 'none';
   return target;
}

function update_location()
{
   var target = hide_location();

   var source = document.getElementById('Location');

   clean(target);

   if ( resetLocationBox( source, target ) )
   {
     target.style.display = 'block';
   }

}
function resetLocationBox( source, target )
  { 
    var list = [];
    var val = source.options[source.selectedIndex].value;
	
    if ( val == "" || val == "any" ) 
    {
      return false;
    }
    if ( ! toplist[val] )
    {
      toplist[val] = addToList(list);
    }
    list = toplist[val];

    if ( list == null || list == [] )
    {
       return false;
    }

    var span = document.createElement('span');
target.appendChild(span);    
var tmp = document.createElement('input');
    tmp.type = 'checkbox';
    tmp.name = 'moreLocal';
    tmp.value = '';
    tmp.id = 'vl';

    span.appendChild(tmp);

    tmp = document.createElement('strong');
    span.appendChild(tmp);
    var label = document.createElement('label');
    label.setAttribute('for','vl');
    tmp.appendChild(label);

    label.appendChild(document.createTextNode('All'));
    span.appendChild(tmp);
    
    target.appendChild(span);
    var tmp = document.createElement('br');
    target.appendChild(tmp);

    for ( i=0; i < list.length; i++){
       span = document.createElement('span');
       target.appendChild(span);     
       tmp = document.createElement('input');

       tmp.type = 'checkbox';
       tmp.name = 'moreLocal';

       tmp.value = String(list[i][1]);
       tmp.id = 'vl'+String(list[i][1]);  

       span.appendChild(tmp);

       tmp = document.createElement('label');
       span.appendChild(tmp);
       tmp.setAttribute('for','vl'+String(list[i][1]));
       tmp.appendChild(document.createTextNode(list[i][2]));
       var tmp = document.createElement('br');
       target.appendChild(tmp);
    }

    setup(target);

    return true;


}

function setup(target)
{
   var list = target.getElementsByTagName('label');
   
   var check = new Object;
   if ( veryLocal != null ) { check = veryLocal; veryLocal = null }

   var clean = true;
   var poss = null;
   for ( i =0; i < list.length; i++)
   {
     var label = list[i];
     tmp = document.getElementById( label.getAttribute('for') );
	
     if ( tmp.name == 'moreLocal' )
     { 
       if ( tmp.value != '' )
       {
	  if ( check[tmp.value] )
	  {
	     tmp.checked = true;
	     clean = false;
	  }
	  else 
	  {
             tmp.checked = false;
        
          }
	     addEvent( tmp, 'click', selectBox );
	     
	     if (! tmp.addEventListener) addEvent( label, 'click', checkBox );
       }
       else 
       {
	  poss = tmp;
	  addEvent( tmp, 'click', selectAll );
          if (! tmp.addEventListener) addEvent( label, 'click' , checkBox );
       }

     }
   }
   if ( clean && poss != null )	
   {
     poss.checked = true;
   }
   else
   {
     poss.checked = false;
   }

   return true;
 
}

function checkBox(e)
{
var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    document.getElementById( targ.getAttribute('for') ).click();


}

function selectAll(e)
{
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    if ( targ.nodeName == 'LABEL' )
    {
       targ = document.getElementById( targ.getAttribute('for') );
    }


    if ( targ.checked == true )
    {	
       var name = targ.name;
       var list = document.getElementsByTagName('input');
       for ( i =0; i < list.length; i++)
       {
	  var tmp = list[i];
	 
          if ( tmp.name == name && tmp.value != '' )
	  {
	    tmp.checked = false;
	  }
       }
    }

    return true;

}



function selectBox(e) 
{
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;


    if ( targ.nodeName == 'LABEL' )
    {
       targ = document.getElementById( targ.getAttribute('for') );
    }


    var name = targ.name;
    var list = document.getElementsByTagName('input');
    for ( i =0; i < list.length; i++)
    {
      var tmp = list[i];

      if ( tmp.name == name && tmp.value == '' )
      {
         tmp.checked = false;
      }
    }


   return true;

}
