function $(id) {
  return document.getElementById(id);
}

function Start_Page() {
    /* we call this before the page is fully loaded, so any of the
       fields may not exist.  We repeat until they are all filled.
       This prevents things like the intermediate page boxes not
       appearing if Google or Addthis is being a bit slow */
    var todo = [{id:"logon_status",
		 done:false,
		 innerh:function() {Check_Logon_Status();}
	},
	{id:"searchfor",
	 done:false,
	 innerh:function() {  actb($("searchfor"),new Array());}
	},
	{id:"map",
	 depends:"window.Gaz_Map",
	 done:false,
	 innerh:function() {Gaz_Map();}
	},
	{id:"photo_tab",
	 depends:"window.Photo_Tabs",
	 done:false,
	 innerh:function() {if(cp_info.photos > 1) Photo_Tabs();}
	}
	];
    if(typeof(call_at_start)!='undefined' && call_at_start.length) {
	for(i=0;i<call_at_start.length;i++) {
	    var colon = call_at_start[i].indexOf(':');
	    var id = call_at_start[i].substring(0,colon);
	    var fn = call_at_start[i].substring(colon+1);
	    var x = {id:id,depends:"window."+fn,done:false,innerh:new Function(fn+"();")};
	    todo.push(x);
	}
	}    
    Doemall(todo);
}

function Doemall(todo) {
    if(Do_Once(todo) == false) {
	setTimeout(function() {Doemall(todo)},200);   // try again in 2/10 of a second
    } else {
	if(typeof(Set_Content_Width) == 'function')
	    Set_Content_Width();
    }
}

function Do_Once(todo) {
    var alldone = true;
    for(var i in todo) {
	if(todo[i].done == false) {
	    if(todo[i].depends) {          // may not always be there
		if(typeof(eval(todo[i].depends)) == 'undefined') {
		    todo[i].done = true;
		    continue;
		}
	    }
	    var id = document.getElementById(todo[i].id);
	    if(id) {
		todo[i].innerh();
		todo[i].done = true;
	    } else {
		alldone = false;
	    }
	}
    }
    if(alldone) {
	var xyz = Prepare_Editables();
	if(xyz) {
	    Setup_Editables(amiloggedon);
	}
	return xyz;
    }
    return false;
}

var amiloggedon = false;

function Add_Photo(id) {
  if(amiloggedon) {
      var sid = Get_Session_ID();
      document.location = "../cgi-bin/add_photo.cgi?session="+sid+"&id="+id;
  } else {        // false or null
      alert("You must be logged on to add a photograph");
  }
}

function Delete_Place(id) {
  if(cp_info.photos && cp_info.photos > 0) {
    alert("Places with photos cannot be deleted.  Move (or delete) all photos first");
  } else {
    var sid = Get_Session_ID();
    document.location = "../cgi-bin/delete_place.cgi?session="+sid+"&id="+id;
  }
}

