Event.observe(window, 'load', trapCaptionClicks); 

function trapCaptionClicks()
{
   var x = 1; 
   do
   {
      var controlName = 'edit' + x; 
      var control = $(controlName); 
      if (control)
      {
         Event.observe(controlName, 'click', checkPassword); 
      }
      x++; 
   }
   while (control);    
}

var caption = null; 
var theFileName = ""; 

function setFileName(thisFileName)
{
   theFileName = thisFileName; 
}
   
function checkPassword()
{
   caption = this; 
   var thePassword = prompt('Enter password to edit'); 
   new Ajax.Request("http://www.hamletdarcy.com/scripts/validate_password.php", 
      {
         method: "get", 
         parameters: "password=" + thePassword, 
         onSuccess: editText, 
         onFailure: onFailure   
      }
   ); 
}

function badPassword()
{
   alert("Invalid Password");   
}

function editText(request)
{
   var data = request.responseText; 
   if (data)
   {
      if (data == "1")
      {
         var newCaption = prompt('Enter new caption:', caption.innerHTML); 
         new Ajax.Request("http://www.hamletdarcy.com/edit_gallery.php", 
            {
               method: "get", 
               parameters: "filename=" + theFileName + "&caption=" + newCaption + "&captionID=" + caption.id, 
               onSuccess: updateText, 
               onFailure: onFailure   
            }
         ); 
         
         caption.innerHTML = newCaption; 
         
      }  
      else
      {
         badPassword(); 
      }
   }
}

function updateText()
{
   //do nothing
}

function onFailure()
{
   alert("Unknown Failure. I suggest FireBug."); 
}