<!------- hide the script from old browsers
// new image - photo array using image_names for onclick ref (image to load)
// captions - caption array for images
// credits - photographer names array for images
//photos = new Array ();
captions = new Array (); 
credits = new Array ();

W3C = (document.getElementById) ? 1 : 0; 
IE4 = (document.all) ? 1 : 0; 
NS4 = (document.layers) ? 1 : 0; 

function dhtml_check() { // browser specific items
  if (W3C) { // netscape 6 (gecko) or greater - DOM specific object variables
    layerRef = "document.getElementById('";
    layerEnd = "')";
    layerStyle = "').style";
    layerText = "').innerHTML";
    visible = '.visibility = "visible"';
    hidden = '.visibility = "hidden"';
    zlevel = '.zindex';
    pxunits = 'px';
    okMenu = true;
  }
  else if (NS4) { // netscape 4 - DOM specific object variables
    window.onResize = reloadIt;// patch for netscape window resizing problem
    layerRef = "document.layers['";
    layerEnd = "']";
    layerStyle = "']";
    visible = '.visibility = "show"';
    hidden = '.visibility = "hide"';
    zlevel = '.zIndex';
    pxunits = 'px';
    okMenu = true;
  }
  else if (IE4) { // msie 4 or greater - DOM specific object variables
    layerRef = "document.all['";
    layerEnd = "']";
    layerStyle = "'].style";
    //layerText = "'].innerHTML";
    layerText = "').innerText";
    visible = '.visibility = "visible"';
    hidden = '.visibility = "hidden"';
    zlevel = '.zindex';
    pxunits = 'px';
    okMenu = true;
  }
  else { okMenu = false; }
}
dhtml_check();

function reloadIt() { document.location = document.location;}

function define_gallery(id,title) {
  var winopt,url;
  var height = 475; 
  var width = 490; 

  if (NS4) { height= 100; url = "gallery_problem.html"; }
  else { url = "gallery.shtml?l=popup;q=" + id +";t=" + title; }
  winopt = "toolbar=no,location=no,directories=no,menubar=no,resizable=no,";
  winopt += "scrollbars=yes,width=" + width + ",height=" + height;
  newWin=window.open(url,"dummy",winopt);
  return
}

this_set = 1;
function n_gallery(ref,nn) {
  if (nn <= max_set[this_set] && nn > max_set[this_set - 1]) {
    // no need to change the list of photo numbers
    return;
  }
  if (ref == "next") {
    this_set += 1;
    if (this_set > max_sets) {
      this_set = 1;
    }
  }
  if (ref == "back") { 
    this_set -= 1;
    if (this_set < 1) {
      this_set = max_sets;
    }
  } 
  ChangeText("myGallery",gtxt[this_set]);
}

last_img = 1;
function gallery(ref,imgID,txtID,byID) {
  if (!imgID) { imgID = "photo"; }
  if (!txtID) { txtID = "caption"; }
  if (!byID) { byID = "credit"; }
  // subtract 1 from photos.length since photos array starts at 1 (not 0)
  if (ref == "next") { 
    ref = last_img + 1; 
    if (ref > (photos.length - 1)) {
      ref = 1; 
    }
    n_gallery('next',ref);
  }
  if (ref == "back") { 
    ref = last_img*1 - 1; 
    if (ref < 1) {
      ref = photos.length - 1;
    }
    n_gallery('back',ref);
  }
  last_img = ref * 1; 
  eval('document.' + imgID).src = myPhotos[ref].src; 
  eval('document.' + imgID).height = heights[ref]; 
  eval('document.' + imgID).width = widths[ref];  
  for (var loop in photos) { // reset css class of all numbers
    eval(layerRef + "P" + loop + layerEnd).className = "ITEMS";
  }
  // set css class for the chosen number (ref)
  eval(layerRef + "P" + ref + layerEnd).className = "ITEMS_ON";
  ChangeText(txtID,captions[ref]);
  img = "Credit: " + credits[ref];
  ChangeText(byID,img);
  // hide previous link if the 1st photo im the gallery (otherwise show it)
  if (ref == 1) { ChangeText("previous_link",previous_photo1); } 
  else { ChangeText("previous_link",previous_photo); }
  // change next link text if last photo in the gallery
  if (ref == (photos.length - 1)) { ChangeText("next_link",next_photo1); }
  else { ChangeText("next_link",next_photo); }
}

function ChangeText(txt_tag,txt) {
  if (W3C) { eval(layerRef + txt_tag + "')").innerHTML  = txt; }
  else if (IE4) { eval(layerRef + txt_tag + "')").innerText  = txt; }
  else if (NS4) { 
    var txt_tag2 = txt_tag + '_a';  
    document.eval(txt_tag).document.eval(txt_tag2).document.open();
    document.eval(txt_tag).document.eval(txt_tag2).document.write(txt); 
    document.eval(txt_tag).document.eval(txt_tag2).document.close();
  }
  else { alert("Don't recognize this browser"); }
}

function noCR(thisEvent) {
  var key = window.event ? thisEvent.keyCode : thisEvent.which;
  var keychar = String.fromCharCode(key);
  var reg = /\r/; // key = 13 
  // reg.test is true if \r and false otherwise 
  // return the opposite of the reg.test value (true --> false, false --> true)
  return !reg.test(keychar);
}


// admin functions

function select_gallery(gallery) {
  if (!gallery) { gallery = "new"; }
  if (gallery == "new") { document.admin.gallery.value = ""; }
  else { document.admin.gallery.value = gallery; }
  document.admin.entry_no.value = entry_no[gallery]; 
  document.admin.date.value = date[gallery]; 
  document.admin.title.value = title[gallery];
  document.admin.caption.value = caption[gallery];
}

function check_gallery(which) {
  var ok = required_fields(which);
  if (ok) { ok = check_gallery_name(); }
  if (ok) { ok = check_delete(); }
  return ok;
}

function check_gallery_name(gallery) {
  if (!gallery) { gallery = document.admin.gallery.value; }
  var flag = 0;
  var selected = document.admin.mygallery.value;
  for (var loop in title) {
    if (loop == selected) { continue; }
    if (loop == gallery) { flag = 1; }
  }
  if (flag) { 
    alert('This gallery name (' + gallery + ') already exists.');
    document.admin.gallery.value = "";
    return false;
  }
  else { // check if follows VxxNx format
    flag = gallery.match(/^V(\d{2})N(\d{1})$/);
    if (!flag) { // allow for multiple galleries for an issue of CQ
      flag = gallery.match(/^V(\d{2})N(\d{1})-(\d{1})$/);
    }
    if (!flag) { 
      alert('This gallery name (' + gallery + ') is invalid. The pattern must be VxxNx or VxxNx-x where x is an interger (0-9).');
      return false;
    }
    return true;
  }
}

function check_delete() { 
  var ok = false;
  if (document.admin.db[3].checked) {
    if (document.admin.mygallery.selectedIndex < 1) { 
      alert("You cannot delete a new entry.");
      ok = false;
    }
    else { 
      ok = confirm("Do you really want to delete this entry, its photographs and other  information?\nOK - yes, Cancel - No"); 
    }
    if (!ok) { 
      document.admin.db[0].checked = true;
    }
  }
  else { ok = true; }
  return ok;
}

function check_order(myVar,myVal) {
  for (var item in myOrder) {
    if (myOrder[item] == myVal) {
      eval("document.admin." + item).value = myOrder[myVar];
      myOrder[item] = myOrder[myVar]; // myVar old value
      myOrder[myVar] = myVal; // myVar new value
    }
  }
}

function check_filetype(form,myVar,myVal) {
  var types = new Array('.jpg', '.gif', '.pgn');
  var n = types.length - 1;
  var ok = true;
  var msg = "";  
  var temp = myVal.split('/');
  myVal = temp.pop();
  myVal = myVal.toLowerCase();
  var flag = 0;  
  for (var loop in myPhoto) {
    var thisVal = myPhoto[loop].toLowerCase();
    if (thisVal == myVal) { flag = loop; }
  }
  if (flag) { 
    ok = confirm('This photograph already exists (photo ' + flag + ').\nDo you want to replace it?\nOK - yes, Cancel - No');
    if (!ok) { 
      eval('document.' + form + '.' + myVar).value = "";
      return;
    }
    ok = true;
  }
  ok = false;  
  for (var ext in types) {
    if (myVal.indexOf(types[ext]) > 0) { ok = true; }  
    if (ext == n) { msg += 'or ' + types[ext]; }
    else { msg += types[ext] + ', '; }
  }
  if (!ok) {  
    alert('The file must end in ' + msg + ' (case insensitive).');
    eval('document.' + form + '.' + myVar).value = "";
  }
}

function show_img(form,image,width,height,myTitle) { 
  if (!myTitle) { myTitle = "Photo Gallery"; }
  var myVal = "<img src=\"" + image + "\">\n";
  width = width*1 + 15;
  height = height*1 + 15;
  var html = "<HTML>\n<HEAD>\n";
  html += "<TITLE>" + myTitle + "</TITLE>\n";
  html += "<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" HREF=\"gallery.css\">\n";
  html += "</HEAD>\n<BODY style=\"margin:5px;background-color:#000000;\" ";
  html += "onBlur=\"window.close();\">\n<div align=\"center\"";
  html += myVal + "</div>\n</BODY style=\"margin:0px\">\n</HTML>\n";
  var winopt = "toolbar=no,location=no,directories=no,menubar=no,";
  winopt += "resizable=no,scrollbars=yes,width=" + width + ",height=" + height;
  newWIn=open_window('','blurb',winopt);  
  newWin.document.write(html);
  newWin.document.close();
}

function show_text(form,myVar,myTitle) { 
  var height = 300;
  var width = 260; 
  if (eval('document.' + form + '.pick').value == "issue") {
    height = 100;
    width = 600;
  }
  if (!myTitle) { myTitle = "Text Display"; }
  var myVal = eval('document.' + form + '.' + myVar).value;
  myVal = myVal.replace(/>\r/g,'>'); /* protect html command at end of line */
  myVal = myVal.replace(/>\n/g,'>'); /* protect html command at end of line */
  myVal = myVal.replace(/\r/g,'<br>');
  myVal = myVal.replace(/\n/g,'<br>');
  var html = "<HTML>\n<HEAD>\n";
  html += "<TITLE>" + myTitle + "</TITLE>\n";
  html += "<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" HREF=\"gallery.css\">\n";
  html += "</HEAD>\n<BODY CLASS=\"BACK\" onBlur=\"window.close();\">\n";
  if (myVar == "title") { 
    html += "<H2 ALIGN=\"CENTER\" CLASS=\"TITLE\">" + myVal + "</H2>";
  }
  else {
    html += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"" + width + "\">\n";
    html += "<tr>\n<td align=\"left\" valign=\"top\" width=\"" + width + "\">\n";
    html += "<div CLASS=\"TEXT\">" + myVal + "</div>";
    html += "</td>\n</tr>\n</table>\n";
  } 
  html += "</BODY>\n</HTML>\n";
  width += 20;
  var winopt = "toolbar=no,location=no,directories=no,menubar=no,";
  winopt += "scrollbars=yes,width="+width+",height="+height+"\"";
  newWIn=open_window('','blurb',winopt);  
  newWin.document.write(html);
  newWin.document.close();
}


function required_fields(which) {
  // checks if all required fields are filled in
  // do not change the rest of this function
  // adapted from script by wsabstract.com posted at 
  // The JavaScript Source!! http://javascript.internet.com

  var pass = true;
  var msg = "Please make sure that entries for \n";
  var num = 0; 

  if (document.images) {
    for (i=0; i<which.length; i++) {
      var tempobj=which.elements[i]; 
      for (j = 0; j < required.length; j++) { 
        if (tempobj.name==required[j]) { 
          if (((tempobj.type=="password" ||
            tempobj.type=="text" ||
            tempobj.type=="file" ||
            tempobj.type=="hidden" ||
            tempobj.type=="textarea") && 
            tempobj.value=='') ||
            (tempobj.type.toString().charAt(0)=="s" && 
             tempobj.selectedIndex==0)) {
            pass=false;
            num += 1;
            msg += "  * " + required_text[j] + "\n";
          }
        } 
      }
    }
  }  
 
  if (!pass) {
    if (num > 1) { msg += "are entered correctly.\n"; }
    else { msg += "is entered correctly.\n"; }
    alert(msg);
    return false;
  }
  else { return true; }
}

function open_window (file,width,height,toolbar) {
  // note: toolbar = yes to allow printing of pop-up window
  if (!toolbar) { toolbar = "yes"; }  
  winopt = "toolbar=" + toolbar + ",location=no,directories=no,menubar=yes,";
  winopt += "resizable=no,scrollbars=yes";
  if (width && height) { winopt += ",width=" + width + ",height=" + height; }
  newWin=window.open(file,"dummy",winopt);
}

//close off comment to hide this script from old browsers----------->
