//this function includes all necessary js files for the application
function myjsinclude(file)
{

  var script  = document.createElement('script');
  script.src  = file;
  script.type = 'text/javascript';
  script.defer = true;

  document.getElementsByTagName('head').item(0).appendChild(script);

}

/* include any js files here */
//myjsinclude("/ZAdmin/j/ddmenu.js");
//myjsinclude("/ZAdmin/j/lib/prototype.js");
//myjsinclude("/ZAdmin/j/src/effects.js");
//myjsinclude("/ZAdmin/j/src/dragdrop.js");
//myjsinclude("/ZAdmin/j/src/controls.js");
//myjsinclude("/ZAdmin/j/multiselect.js");
//myjsinclude("/ZAdmin/j/lightbox/lightbox.js");


function updateElement(id, url, highlightid)
  {
    new Ajax.Updater(id, url, {onComplete:function(request){}, evalScripts:true, asynchronous:true});
  }
  
function submitAJAXForm(formid, posturl, updateid, hightlightid)
  {
    new Ajax.Updater(updateid, posturl, {onComplete:function(request){new Effect.Highlight(highlightid);}, parameters:Form.serialize(formid), evalScripts:true, asynchronous:true});
  }
  
function confirmAJAXDelete(updateid, url, highlightid)
  {
    if(confirm("Are you sure you wish to delete this record?"))
      {
        new Ajax.Updater(updateid, url, {onComplete:function(request){new Effect.Highlight(highlightid);}, evalScripts:true, asynchronous:true});
      }
  }

function confirmDelete(url)
  {
    if(confirm("Are you sure you wish to delete this record?"))
      {
        document.location = url;
      }
  }

function addWorkToPage(workid, pagetype, pageid, otheropts)
  {
    var url = '/xml' + pagetype + '/' + pageid + '/addWork?workid=' + workid + otheropts;
    updateElement('thumblist', url, 'list-info');
  }

function donothing()
  {
  }

function removeFromShow(show_id, workid, otheropts)
  {
    var url = '/xmlexhibitions/' + show_id + '/removeWork?workid=' + workid + otheropts;
    updateElement('thumblist', url, 'thumblist');
  }
    
function showArt(id)
  {
    $('featuredart').src = '/images_works/' + id + '_s.jpg';
  }
  
var showartids = Array();
var showcurrentslide = 0;
var executor = null;
var isstopped = true;
var firstrun = true;

function initSlideShow()
  {
    //need to fill in data for all art ids from the thumblist
    var allthumbs = $('thumblist').childElements();
    var t1 = Array();
    var artid = "";
    
    for(var i=0; i < allthumbs.length; i++)
      {
        t1 = allthumbs[i].id.split("_");
        artid = t1[1];
        
        showartids.push(artid);
      }
      
    startSlideShow();
  }
  
function startSlideShow()
  {
    if(isstopped)
      {
        isstopped = false;

        executor = new PeriodicalExecuter(nextSlide, 9);

        if(!firstrun)
          nextSlide();
        else
          firstrun = false;
      }
  }
  
function stopSlideShow()
  {
    isstopped = true;
  }
  
function nextSlide(pe)
  {
    if(isstopped)
      pe.stop();
    else
      {
        if(showcurrentslide != showartids.length - 1)
          showcurrentslide++;
        else
          showcurrentslide = 0;
          
        getArt(showartids[showcurrentslide]);
      }
  }

function sl_next()
  {
    isstopped = true;
    
    if(showcurrentslide != showartids.length - 1)
      showcurrentslide++;
    else
      showcurrentslide = 0;

    getArt(showartids[showcurrentslide]);

  }
  
function sl_prev()
  {
    //if user hits previous button, need to stop the show
    isstopped = true;
    
    if(showcurrentslide > 0)
      showcurrentslide--;
    else
      showcurrentslide = showartids.length - 1;
      
    getArt(showartids[showcurrentslide]);
  }
  
function getArt(artid)
  {
    url = '/xmlworks/' + artid + '/getSlide';
    new Ajax.Request(url, {onComplete: function(response) {
      $('slidec').update(response.responseText);
      initLightbox();
    }});
  }
  
  
  
  function isEmailAddr(email)
  {
      var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      if (filter.test(email)) return true;
      else return false;
  }
  
  function validateForm()
    {
      var name = document.contactform.contact.value;
      var email = document.contactform.email.value;

      var errormsg = "";

      var pass = false;

      if(name == "")
        errormsg += "Please enter your Name in the contact field.\n";
      else
        pass = true;

      if(!isEmailAddr(email))
        {
          errormsg += "Please enter a valid e-mail address.\n"
          pass = false;
        }
      else
        {
          pass = true;
        }

      if(errormsg != "")
        {
          pass = false;

          alert(errormsg);
          return pass;

        }
      else
        return pass;
    }