/* The dynamic AJAX-style selector for the new gazetter - Nov 2006 */

var selector_whatfor = "";

function Selector_Start(forwhat) {
  Populate_Selector("sel_0","");
  Selector_Clear('sel_1');
  Selector_Clear('sel_2');
  Selector_Clear('sel_3');
  Selector_Clear('sel_4');
  selector_whatfor = forwhat;
}

function Populate_Selector(id,sofar) {
  var selbox = document.getElementById(id);
  // first clear our selection box - in reverse order, as null removes them
  for(var count = selbox.options.length - 1; count >= 0; count--) {
    selbox.options[count] = null;
  }
  // add a place marker while we download the options
  selbox.options[0] = new Option(i18n.select_pending,"HEADER");
  // read contents
  Download_Candidates(sofar,selbox);
}

function Download_Candidates(t,selid) {
  var xmlhttp=null
  // first for all but IE, else for IE
  if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest()
  } else if (window.ActiveXObject) {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  if (xmlhttp == null) {
    alert(i18n.browserfail);
    return false;
  }
  xmlhttp.onreadystatechange=function() {
      // if xmlhttp shows "loaded"
      if (xmlhttp.readyState==4) {
	  // if "OK"
	  if (xmlhttp.status==200) {
	      var response = xmlhttp.responseText;
	      var items = eval("("+response+")");
	      var idx = 1;
	      var lastones = false;
	      selid.options[0].text = i18n.select_empty;
	      for (var i in items) {
		  if(items[i].complete && items[i].complete == selector_whatfor) {
		      selid.options[idx++] = new Option(items[i].name,'!'+items[i].id);
		  } else {
		      selid.options[idx++] = new Option(items[i].name,items[i].id);
		  }
	      }
	  }  
      }
  };
  xmlhttp.open("GET","../cgi-bin/selector.cgi?"+t,true);
  xmlhttp.send(null);
}

function Selector_Show(s) {
  var nx = document.getElementById(s+'_b');
  nx.style.display = 'block';
}

function Selector_Clear(s) {
  var nx = document.getElementById(s+'_b');
  nx.style.display = 'none';
}

/* This could be done in a loop, but would probably be harder to read */
function Selector_Changed(sel) {
  var nx = document.getElementById(sel);
  var n = nx.options[nx.selectedIndex].value;
  if(n.substring(0,1) == '!') {
      return Place_Selected({id:n.substring(1),name:nx.options[nx.selectedIndex].text});
  }
  switch(sel) {
    case "sel_0":
      Selector_Clear('sel_2');
      Selector_Clear('sel_3');
      Selector_Clear('sel_4');
      if(n == 'HEADER') {
        Selector_Clear('sel_1');
        return false;
      }
      var n = nx.options[nx.selectedIndex].value;
      Populate_Selector('sel_1',n);
      Selector_Show('sel_1');
    break;
    case "sel_1":
      Selector_Clear('sel_3');
      Selector_Clear('sel_4');
      if(n == 'HEADER') {
        Selector_Clear('sel_2');
        return false;
      }
      Populate_Selector('sel_2',n);
      Selector_Show('sel_2');
    break;
    case "sel_2":
      Selector_Clear('sel_4');
      if(n == 'HEADER') {
        Selector_Clear('sel_3');
        return false;
      }
      Populate_Selector('sel_3',n);
      Selector_Show('sel_3');
    break;
    case "sel_3":
      if(n == 'HEADER') {
        Selector_Clear('sel_4');
        return false;
      }
      Populate_Selector('sel_4',n);
      Selector_Show('sel_4');
    break;
  }
}

function Place_Selected(pn) {
    if(typeof(Place_Selected_Callback)=='function') {
	Place_Selected_Callback(pn);
	return;
    }
    var tocall = pn.id;
    var sid = Get_Session_ID();
    if(sid)
	tocall += '&session='+sid;
    if(selector_whatfor == "place")
	window.location = 'gazetteer.cgi?id='+tocall;
    else
	window.location = 'waterway.cgi?id='+tocall;
}

// allow to be used as a pop-up
function place_selector(where,what) {
    // hack to get ID of input box - Oct 2009: WHAT DID THIS DO?
    //    var iboxid = where;
    //    if(iboxid.indexOf("_")==-1) {   // not start or end
    //	iboxid = iboxid.replace(/place([0-9]+)/,'placebox$1');
    //    }
    if(typeof(KillMenu)=='function')
	KillMenu();
    Popup('selector_popup',where);
    Place_Selected_Callback = function(pn) {
	$(where).value = pn[what];
	Kill_Popup('any');
    };
    Selector_Start('place');
}

function Selector_Mode(mode) {
    var sels = document.getElementById('selectors');
    var names = document.getElementById('selnames');
    if(mode == '0') {
	sels.style.display = 'block';
	names.style.display = 'none';
	return;
    }
    if(mode == '1') {
	sels.style.display = 'none';
	names.style.display = 'block';
	return;
    }
}

function Update_Inc_Selector(el) {
    //console.log(el.value);
}