// JavaScript Document
var blankImage = "images/Browser/General/form_elements/blank.gif"
var modalRequest = ""
var newSetName = ""
var selectedSetID = 0;
var selectedSet = new mySet(0, "", "");
var opSet = new mySet(0, "", "");
var selectedSlide = "";
var selectedSlideUndo = "";
var scrollTopUndo = 0;
var selectedThumb = "";
var selectedPhoto = "";
var slideSetDirty = false;
var mySets = new ActiveXObject("Scripting.Dictionary");
var mySlides = new ActiveXObject("Scripting.Dictionary");
var mySlidesUndo = new ActiveXObject("Scripting.Dictionary");
var myPhotos = new ActiveXObject("Scripting.Dictionary");
var myThumbs = new ActiveXObject("Scripting.Dictionary");

function requestDelete()
{
   newSetName = "";
   modalRequest = "";
   Boxy.ask("Deleting this Slide Set will only delete related slides.\nNo images in database will be erased.\nDelete now?",
   {
      "1" : " Yes ", "2" : "  No  "
   }
   , function(r)
   {
      confirmDeleteSet(r);

   }
   // {behaviours : function(r) { $(r).hover(function() { $(this).css("color", "red"); }, function() { $(this).css("color", "white"); }); }}
   );


}

function confirmDeleteSet(resp)
{
   if (resp == 2) return;
   modalRequest = "DeleteSet";
   processModalRequest();
}

function newModal(mRequest, _default)
{
   var d = document.getElementById("Text1")
   d.value = ""

   modalRequest = mRequest
   if(_default != null)
   d.value = selectedSet.slidesetName;
   else
   d.value = "Untitled"
   newSetName = d.value;

   var el = document.getElementById("divModalContent")

   new Boxy(el.innerHTML,
   {
      title : "Enter name for new Slide Set:", closeable : false, modal : true, behaviours : function(c)
      {

         c.find('input:first').change(function()
         {
            newSetName = trim(this.value);
         }
         );
      }
   }
   );
}

function cancelModalRequest()
{
   modalRequest = "";
   newSetName = "";

}
function processModalRequest()
{
   switch (modalRequest)
   {
      case "NewSet" :
         newSlideSet();
         break;
      case "RenameSet" :
         renameSlideSet();
         break;
      case "DeleteSet" :
         deleteSlideSet();
         break;
      default :
         break;
   }
   modalRequest = "";
   newSetName = "";
}


function newSlideSet()
{
   if(newSetName == "") return;
   opSet = new mySet(0, newSetName, "");
   SaveMainSet(bundleSetInfo());
}

function renameSlideSet()
{
   if(newSetName == "") return;
   opSet = new mySet(selectedSet.slidesetID, newSetName, selectedSet.slidesetDescription);
   SaveMainSet(bundleSetInfo());
}
function deleteSlideSet()
{
   setSlideSetDirty(false);
   opSet = new mySet(selectedSet.slidesetID * - 1, selectedSet.slidesetName, selectedSet.slidesetDescription);
   SaveMainSet(bundleSetInfo());
}

function showGalleryViewer()
{
   var sid = selectedSet.slidesetID;
   if (sid == 0)return;
   var ix = - 1;
   for (i = 0 ; i < mySets.count; i ++  )
   {
      var s = mySets("SET_" + i);
      if (s.slidesetID == sid)
      {
         ix = i;
         break;
      }

   }

   if (ix > - 1)previewGallery(ix);

}

function setSelectedSet(slidesetID)
{
   var done = false;
   for (i = 0 ; i < mySets.count; i ++ )
   {
      var s = mySets("SET_" + i);
      if (s.slidesetID == slidesetID)
      {
         selectedSet = new mySet(s.slidesetID, s.slidesetName, s.slidesetDescription)
         done = true;
         break;
      }

   }
   if ( ! done)selectedSet = new mySet(0, "", "");

   document.getElementById("btnNewSlide").disabled = (selectedSet.slidesetID == 0 ? "disabled" : "");
   document.getElementById("btnRenameSlideSet").disabled = (selectedSet.slidesetID == 0 ? "disabled" : "");
   document.getElementById("btnDeleteSlideSet").disabled = (selectedSet.slidesetID == 0 ? "disabled" : "");
}

function loadSlideSet(el)
{
   if (slideSetDirty)
   {
      var c = confirm("Save changes to slide set:\n" + selectedSet.slidesetName + "?\nIf you click cancel, changes will be lost.");
      if (c == true) SaveSlideSet( bundleSlides(), showIndicator(true));
   }

   // selectedSetID = el.value;
   setSelectedSet(el.value)
   undoAll();
}


function ReceiveAllSets(arg, context)
{
   loadSets(arg);
   showIndicator(false);
   if (selectedSetID == 0)
   {
      if(mySets.count > 0) selectedSetID = mySets("SET_0").slidesetID;
   }
   if (selectedSetID > 0)
   {
      setOption(selectedSetID);
      loadSlideSet(document.getElementById("lstSets"));
   }
   else
   {
      setSelectedSet(0);

      var selectbox = document.getElementById("lstSets");
      removeAllOptions(selectbox);
   }
   showIndicator(false);
}

function ConfirmSaveMainSet(arg, context)
{
   showIndicator(false);
   if (isNaN(parseInt(arg)) == true)Boxy.alert(arg, null,
   {
      title : 'Error!'
   }
   );
   else
   {
      selectedSetID = arg;
      RequestAllSets("REQ_AS", showIndicator(true));

      // opSet = new mySet(selectedSet.slidesetID, selectedSet.slidesetName, selectedSet.slidesetDescription);
   }
}

function ReceiveSlideSet(arg, context)
{
   loadSlides(arg);
   showIndicator(false);
   // document.getElementById("btnSaveChanges").disabled = "disabled";
   enableSaveButton(false);
   document.getElementById("btnUndoAll").disabled = "disabled";

}
function ConfirmSaveSlideSet(arg, context)
{
   showIndicator(false);
   if (arg !== "ACK")alert(arg);
   else
   {
      setSlideSetDirty(false);
      // document.getElementById("btnSaveChanges").disabled = "disabled";
      enableSaveButton(false);
      document.getElementById("btnUndoAll").disabled =  "disabled";
   }
}
function ProcessCallBackError(arg, context)
{
   alert('An unknown error has occurred!');
}
function showIndicator(show)
{

   document.getElementById("imgIndicator").style.display = (show == true ? "" : "none");
}
function initSlidesEdit()
{
   // if(document.title.toLowerCase().indexOf("manage photo gallery slides") == - 1)return;
   RequestAllSets("REQ_AS", showIndicator(true));
   createBlankTd();
   loadPhotos();
   loadThumbs();
   fillThumbs();
   fillPhotos();
}

function loadPhotos()
{

   var d = document.getElementById("divPhotos");
   for(i = 0; i < d.children.length; i ++ )
   {
      var x = d.children[i].value.split("")
      var s = new myPhoto(i, x[0], x[1], x[2], x[3], x[4])
      myPhotos.add("P_" + x[0], s);
   }
}

function loadThumbs()
{

   var d = document.getElementById("divThumbs");
   for(i = 0; i < d.children.length; i ++ )
   {
      var x = d.children[i].value.split("")
      var s = new myThumb(i, x[0], x[1], x[2], x[3])
      myThumbs.add("T_" + x[0], s);
   }
}

function loadSets(arg)
{
   mySets.RemoveAll();
   if (arg == "")return;
   var sep = String.fromCharCode(1);
   var sets = arg.split(sep);

   for (n = 0 ; n < sets.length; n ++ )
   {
      var x = sets[n].split("");
      var s = new mySet(x[0], x[1], x[2]);
      mySets.add("SET_" + n, s);
      // if (n == 0)selectedSet = new mySet(x[0], x[1], x[2])

   }
   // selectedSet = new mySet(opSet.slidesetID, opSet.slidesetName, opSet.slidesetDescription);
   // alert(selectedSet.slidesetID)
   fillSets();

}
function fillSets()
{
   var selectbox = document.getElementById("lstSets");
   removeAllOptions(selectbox);
   for (i = 0 ; i < mySets.count; i ++ )
   {
      var s = mySets("SET_" + i);
      addOption(selectbox, s.slidesetName, s.slidesetID);
   }

}

function addOption(selectbox, otext, ovalue)
{
   var optn = document.createElement("OPTION");
   optn.text = otext;
   optn.value = ovalue;
   // if(ovalue == slidesetID) optn.selected = true;
   selectbox.options.add(optn);
}
function removeAllOptions(selectbox)
{
   for(i = selectbox.options.length - 1; i >= 0;
   i -- )
   {
      selectbox.remove(i);
   }
}

function setOption(slideSetID)
{
   var selectbox = document.getElementById("lstSets");
   for (i = 0; i < selectbox.options.length; i ++ )
   if(selectbox.options[i].value == slideSetID)
   {
      selectbox.options[i].selected = true;
      break;
   }

}


function loadSlides(arg)
{
   mySlides.RemoveAll();
   setSlideSetDirty(false);
   if (arg == "")
   {
      createBlankTd();
      setSlideSetDirty(false);
      return;
   }
   var sep = String.fromCharCode(1);
   var slides = arg.split(sep);

   for (n = 0 ; n < slides.length; n ++ )
   {
      var x = slides[n].split("")
      var s = new mySlide(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9])
      mySlides.add("SL_" + n, s);
   }

   fillSlides(0);
   setSlideSetDirty(false);
}

function myPhoto(pos, photoID, photoWidth, photoHeight, photoDescription, photoURL)
{
   this.pos = pos;
   this.photoID = photoID;
   this.photoWidth = photoWidth;
   this.photoHeight = photoHeight;
   this.photoDescription = photoDescription;
   this.photoURL = photoURL;
}

function myThumb(pos, thumbID, thumbWidth, thumbHeight, thumbURL)
{
   this.pos = pos;
   this.thumbID = thumbID;
   this.thumbWidth = thumbWidth;
   this.thumbHeight = thumbHeight;
   this.thumbURL = thumbURL;
}

function mySet(slidesetID, slidesetName, slidesetDescription)
{
   this.slidesetID = slidesetID;
   this.slidesetName = slidesetName;
   this.slidesetDescription = slidesetDescription;
}


function mySlide(slideID, thumbID, photoID, thumbWidth, thumbHeight, photoWidth, photoHeight, slideText, thumbURL, photoURL)
{
   this.slideID = slideID;
   this.thumbID = thumbID;
   this.photoID = photoID;
   this.thumbWidth = thumbWidth;
   this.thumbHeight = thumbHeight;
   this.photoWidth = photoWidth;
   this.photoHeight = photoHeight;
   this.slideText = slideText;
   this.thumbURL = thumbURL;
   this.photoURL = photoURL;
}

function newSlide()
{
   setSlideSetDirty(true);
   var i = (mySlides.count);
   var s = new mySlide(0, 0, 0, "", "", "", "", "", blankImage, blankImage)
   mySlides.add("SL_" + i, s);
   fillSlides(i);
}

function SelectSlide(slideNum)
{
   var s = mySlides(slideNum);
   if((selectedSlide != "") && (mySlides.Exists(selectedSlide))) document.getElementById ("td_" + selectedSlide.substr(3)).style.border = "3px double #666"
   var imgThumb = document.getElementById ("imgThumb");
   var imgPhoto = document.getElementById ("imgPhoto");
   var txtSlideID = document.getElementById ("txtSlideID");
   var txtDescription = document.getElementById ("txtDescription");
   var _twid = (s.thumbWidth == ""  ? 1 : s.thumbWidth);
   var _tht = (s.thumbHeight == "" ? 1 : s.thumbHeight);
   _twid = (_twid > 80 ? 80 : _twid);
   _tht = (_tht > 80 ? 80 : _tht);
   var _wid = (s.photoWidth == ""  ? 1 : s.photoWidth);
   var _ht = (s.photoHeight == "" ? 1 : s.photoHeight);
   _wid = (_wid > 440 ? 440 : _wid);
   _ht = (_ht > 340 ? 340 : _ht);
   imgThumb.style.width = _twid + "px";
   imgThumb.style.height = _tht + "px";
   imgThumb.src = s.thumbURL;
   imgPhoto.src = s.photoURL;
   imgPhoto.style.width = _wid + "px";
   imgPhoto.style.height = _ht + "px";
   txtDescription.value = s.slideText;
   txtDescription.readOnly = false;
   txtDescription.title = "Try to keep it to 2 lines (3 at most)";
   // txtSlideID.value = slideNum;
   selectedSlide = slideNum;
   document.getElementById ("td_" + slideNum.substr(3)).style.border = "3px solid blue";
   selectThumb((s.thumbID > 0 ? "T_" + s.thumbID : ""), true);
   selectPhoto((s.photoID > 0 ? "P_" + s.photoID : ""), true);
   imgThumb.title = "";
   if((s.thumbWidth != "") && (s.thumbHeight != "")) imgThumb.title = s.thumbURL.substr(s.thumbURL.indexOf("/") + 1 ) + " (" + s.thumbWidth + " x " + s.thumbHeight + ")";
   imgPhoto.title = "";
   if((s.photoWidth != "") && (s.photoHeight != "")) imgPhoto.title = s.photoURL.substr(s.photoURL.indexOf("/") + 1 ) + " (" + s.photoWidth + " x " + s.photoHeight + ")";
   document.getElementById ("tdInfoThumbLabel").innerText = "Thumb:";
   document.getElementById ("tdInfoPhotoLabel").innerText = "Photo:";
   showSlideInfo();
   showSlideOpsButtons(true);

}

function selectThumb(thumbKey, _scroll)
{
   if((selectedThumb != "") && (myThumbs.Exists(selectedThumb)))
   {
      var td = document.getElementById ("t_td_" + selectedThumb.substr(2));
      td.style.border = "3px double #666"
      td.style.background = "#fff url(../Images/Browser/General/form_elements/th_bck.gif) repeat-x 0px -5px";
   }
   if (thumbKey != "")
   {
      try
      {
         selectedThumb = thumbKey;
         var td = document.getElementById ("t_td_" + selectedThumb.substr(2));
         td.style.border = "3px dashed blue";
         td.style.background = "yellow";
         if(_scroll != null) document.getElementById("panelThumbs").scrollLeft = myThumbs(selectedThumb).pos * 90;
      }
      catch (ex)
      {
         selectedThumb = "";
         if(_scroll != null) document.getElementById("panelThumbs").scrollLeft = 0;
      }
   }
   else if(_scroll != null) document.getElementById("panelThumbs").scrollLeft = 0;

}

function selectPhoto(photoKey, _scroll)
{
   if((selectedPhoto != "") && (myPhotos.Exists(selectedPhoto)))
   {
      var td = document.getElementById ("p_td_" + selectedPhoto.substr(2))
      td.style.border = "3px double #666";
      td.style.background = "#fff url(../Images/Browser/General/form_elements/th_bck.gif) repeat-x 0px -5px";
   }
   if (photoKey != "")
   {

      try
      {
         selectedPhoto = photoKey;
         var td = document.getElementById ("p_td_" + selectedPhoto.substr(2));
         td.style.border = "3px dashed blue";
         td.style.background = "yellow";
         if(_scroll != null) document.getElementById("panelPhotos").scrollLeft = myPhotos(selectedPhoto).pos * 90;
      }
      catch (ex)
      {
         selectedPhoto = "";
         if(_scroll != null) document.getElementById("panelPhotos").scrollLeft = 0;
      }

   }
   else if(_scroll != null) document.getElementById("panelPhotos").scrollLeft = 0;

}

function fillSlides(slide, _scrollTop)
{
   clearSlidesTable();
   for (i = 0 ; i < mySlides.count; i ++ )
   {

      var s = mySlides("SL_" + i);
      addSlideThumbs(tblSlides, i, s, "edit_slide_photo_thumbs_img");
   }
   var p = document.getElementById('panelSlides');
   if (_scrollTop != null) p.scrollTop =  _scrollTop;
   else
   {
      var st = p.scrollTop;
      var slideht = 78;
      var panelht = p.clientHeight;
      var slidepos = slide * slideht;
      var sb = (st + panelht);
      //  alert (st);
      //  p.scrollTop = slide * slideht;
      // alert(slidepos);
      if (slidepos > (sb - slideht)) p.scrollTop = slidepos - ((panelht / slideht - 1) * slideht);
      else
      if (slidepos < st)p.scrollTop = slidepos;
   }


   if(mySlides.Exists("SL_" + slide )) SelectSlide("SL_" + slide);
   //   alert($get("p0").getAttribute("click"))
}


function addSlideThumbs(tbl, slideNum, s, classname)
{
   var tit = " title='Click to select for editing' "
   var tr = tbl.insertRow();
   var td = createSlideTd(slideNum)
   var img = document.createElement("IMG");
   img.id = "t" + slideNum;
   td.appendChild(img);
   img.outerHTML = "<IMG id=" + img.id +  tit  +  " class=" + classname + " src='" + s.thumbURL + "' onclick=SelectSlide('SL_" + slideNum + "')>";
   var img = document.createElement("IMG");
   img.id = "p" + slideNum;
   td.appendChild(img);
   img.outerHTML = "<IMG id=" + img.id +  tit  + " class=" + classname + " src='" + s.photoURL + "' onclick=SelectSlide('SL_" + slideNum + "')>";
   tr.appendChild (td);

}

function fillThumbs()
{
   var tbl = document.getElementById ("tblThumbs");
   var tr = tbl.insertRow();
   var a = (new VBArray(myThumbs.Keys())).toArray();
   for (i = 0; i < a.length; i ++ )
   {

      var t = myThumbs(a[i]);
      addThumbs(tr, t, "edit_slide_select_image_img");
   }
}

function addThumbs(tr, t, classname)
{

   var td = document.createElement("TD");
   td.id = "t_td_" + t.thumbID;
   td.className = "edit_slide_select_image_td";
   td.align = "center";
   td.verticalAlign = "middle";
   var img = document.createElement("IMG");
   img.id = "T_" + t.thumbID;
   td.appendChild(img);
   var tit = " title='" + t.thumbURL.substr(t.thumbURL.lastIndexOf("/") + 1 ) + " (" + t.thumbWidth + " x " + t.thumbHeight + ")'"
   var xx = (screen.width - t.thumbWidth) / 2;
   var yy = (screen.height - t.thumbHeight) / 2;
   //   var dbl = " ondblclick=\"popUpWindow('PhotoPopUp.aspx?I=" + t.thumbURL + "', " + xx + ", " + yy + ", " + t.thumbWidth + ", " + (parseInt(t.thumbHeight) + 30)  + ", null, null)\" ";
   var dbl = " ondblclick=\"DisplayNewImageInWindow('" + t.thumbURL + "')\" "
   img.outerHTML = "<IMG id=" + img.id + tit  + " class=" + classname + " src='" + t.thumbURL + "'" + dbl + " onclick=selectThumb(this.id)>";
   tr.appendChild(td);
}

function fillPhotos()
{
   var tbl = document.getElementById ("tblPhotos");
   var tr = tbl.insertRow();
   var a = (new VBArray(myPhotos.Keys())).toArray();
   for (i = 0; i < a.length; i ++ )
   {

      var p = myPhotos(a[i]);
      addPhotos(tr, p, "edit_slide_select_image_img");
   }
}

function addPhotos(tr, p, classname)
{

   var td = document.createElement("TD");
   td.id = "p_td_" + p.photoID;
   td.className = "edit_slide_select_image_td";
   td.align = "center";
   td.verticalAlign = "middle";
   var img = document.createElement("IMG");
   img.id = "P_" + p.photoID;
   td.appendChild(img);
   var tit = " title='" + p.photoURL.substr(p.photoURL.lastIndexOf("/") + 1 ) + " (" + p.photoWidth + " x " + p.photoHeight + ")'";
   //   var dbl = " ondblclick=\"popUpWindow('" + p.photoURL + "', null, null, " + p.photoWidth + ", " + p.photoHeight  + ", null, null)\" ";
   var dbl = " ondblclick=\"DisplayNewImageInWindow('" + p.photoURL + "')\" "
   img.outerHTML = "<IMG id=" + img.id + tit  +  " class=" + classname + " src='" + p.photoURL + "'" + dbl + " onclick=selectPhoto(this.id)>";
   tr.appendChild(td);
}

function moveSlideUp()
{
   var i = parseInt(selectedSlide.substr(3));
   if (i <= 0) return;
   setSlideSetDirty(true);
   var temp = mySlides("SL_" + (i - 1));
   var k = "SL_" + i;
   var k1 = "SL_" + (i - 1);
   mySlides("SL_" + (i - 1)) = mySlides("SL_" + i);
   mySlides("SL_" + i) = temp;
   mySlides("SL_" + i).Key = k1;
   mySlides("SL_" + (i - 1)).Key = k;
   fillSlides(i - 1);

}

function moveSlideDown()
{
   var i = parseInt(selectedSlide.substr(3));
   if (i >= mySlides.count - 1) return;
   setSlideSetDirty(true);
   var temp = mySlides("SL_" + (i + 1));
   var k = "SL_" + i;
   var k1 = "SL_" + (i + 1);
   mySlides("SL_" + (i + 1)) = mySlides("SL_" + i);
   mySlides("SL_" + i) = temp;
   mySlides("SL_" + i).Key = k1;
   mySlides("SL_" + (i + 1)).Key = k;
   fillSlides(i + 1);

}

function removeSlide()
{
   if (selectedSlide == "")return;
   setSlideSetDirty(true);
   var x = parseInt(selectedSlide.substr(3));
   mySlides.Remove(selectedSlide);
   if(mySlides.count > 0)
   {
      for (i = x + 1 ; i <= mySlides.count; i ++ )
      {
         mySlides.Key("SL_" + i) = "SL_" + (i - 1);
      }
      fillSlides((x - 1 < 0 ? 0 : x - 1));
   }
   else createBlankTd();

}

function clearSlidesTable()
{
   var tblSlides = document.getElementById ("tblSlides");
   if (tblSlides != null)
   {
      while (tblSlides.rows.length > 0)
      {
         tblSlides.deleteRow(0);
      }
   }
}

function createSlideTd(slideNum)
{
   var td = document.createElement("td");
   td.id = "td_" + (slideNum == - 1 ? 0 : slideNum);
   td.className = "edit_slide_photo_thumbs_td";
   td.align = "center";
   td.verticalAlign = "middle";
   return (td);
}

function createBlankTd()
{
   clearSlidesTable();
   var td = createSlideTd( - 1);
   td.innerText = "Contains No Slides\n\nClick 'Add New Slide'";
   var tr = document.getElementById ("tblSlides").insertRow();
   tr.appendChild (td);
   selectedSlide = ""
   var imgThumb = document.getElementById ("imgThumb");
   var imgPhoto = document.getElementById ("imgPhoto");
   var txtSlideID = document.getElementById ("txtSlideID");
   var txtDescription = document.getElementById ("txtDescription");
   imgThumb.src = blankImage;
   imgPhoto.src = blankImage;
   txtDescription.value = ""
   txtDescription.readOnly = true;
   txtDescription.title = "";
   document.getElementById ("tdInfoThumbLabel").innerText = "";
   document.getElementById ("tdInfoPhotoLabel").innerText = "";
   showSlideOpsButtons(false);
   document.getElementById("tdInfoThumb").innerText = "";
   document.getElementById("tdInfoPhoto").innerText = "";
   document.getElementById("tdInfoError").innerText = "";

}

function updateSlideText()
{
   if (selectedSlide == "")return;
   mySlides(selectedSlide).slideText = trim(document.getElementById ("txtDescription").value.replace("\"","\'"));
   setSlideSetDirty(true);

}

function changeThumb(thumbID, url, twidth, theight)
{
   if (selectedSlide == "")return;
   if (mySlides(selectedSlide).thumbID != thumbID)
   {

      setSlideSetDirty(true);
      mySlides(selectedSlide).thumbID = thumbID;
      mySlides(selectedSlide).thumbURL = url;
      mySlides(selectedSlide).thumbWidth = twidth;
      mySlides(selectedSlide).thumbHeight = theight;
      fillSlides(selectedSlide.substr(3));
   }
}

function changePhoto(photoID, url, pwidth, pheight, pDesc)
{
   if (selectedSlide == "")return;
   if (mySlides(selectedSlide).photoID != photoID)
   {
      setSlideSetDirty(true);
      mySlides(selectedSlide).photoID = photoID;
      mySlides(selectedSlide).photoURL = url;
      mySlides(selectedSlide).photoWidth = pwidth;
      mySlides(selectedSlide).photoHeight = pheight;
      if (mySlides(selectedSlide).slideText == "")mySlides(selectedSlide).slideText = pDesc;
      fillSlides(selectedSlide.substr(3));

   }
}

function bundleSlides()
{
   var slideSetID = selectedSet.slidesetID;
   var slides = "";
   var sep = String.fromCharCode(1);
   for (i = 0 ; i < mySlides.Count; i ++ )
   {
      var s = mySlides("SL_" + i);
      var a = new Array(s.slideID, slideSetID, s.thumbID, s.photoID, s.slideText, i + 1)
      var b = a.join("");
      slides = slides + (slides == "" ? "" : sep) + b
   }
   return ('SAVE_SS-' + slideSetID + '' + slides);

}

function bundleSetInfo()
{
   var sets = "";
   var sep = String.fromCharCode(1);
   var a = new Array(opSet.slidesetID, opSet.slidesetName, opSet.slidesetDescription);
   var b = a.join(sep);
   sets = b;
   return ('SAVE_SET-' + opSet.slidesetID + '' + sets);

}

function changeThumbImage()
{
   if(selectedThumb)
   {
      var t = myThumbs(selectedThumb)
      changeThumb(t.thumbID, t.thumbURL, t.thumbWidth, t.thumbHeight)
   }
}

function changePhotoImage()
{
   if(selectedPhoto)
   {
      var p = myPhotos(selectedPhoto)
      changePhoto(p.photoID, p.photoURL, p.photoWidth, p.photoHeight, p.photoDescription)
   }
}

function showSlideInfo()
{
   var infoThumb = "";
   var infoPhoto = "";
   var tdInfoThumb = document.getElementById("tdInfoThumb");
   var tdInfoPhoto = document.getElementById("tdInfoPhoto");
   var tdInfoError = document.getElementById("tdInfoError");
   var tblSlideInfo = document.getElementById("tblSlideInfo");
   var error = false;
   var errStr = ""
   tdInfoThumb.style.color = "#363";
   tdInfoPhoto.style.color = "#363";
   if(selectedSlide != "")
   {
      var s = mySlides(selectedSlide);
      if (s.thumbID > 0)
      {
         //         var i = sImageInfo(s.thumbURL, s.thumbWidth, s.thumbHeight);
         //         infoThumb = s.thumbURL.substr(s.thumbURL.indexOf("/") + 1 ) + " (" + s.thumbWidth + " x " + s.thumbHeight + ")";
         var si = new sImageInfo(s.thumbURL, s.thumbWidth, s.thumbHeight);
         infoThumb = si.iFilename.toUpperCase() + " " + si.dimensions
         if(s.thumbURL.toLowerCase().find("images/thumbnails/") == false)
         {
            error = true;
            infoThumb = "(*t) " + infoThumb;
            tdInfoThumb.style.color = "red";
            errStr += (errStr == "" ? "" : "\n") + "(*t) Image in " + si.iFolder.toUpperCase() + " folder instead of THUMBNAILS";
         }
      }
      else infoThumb = "(Thumbnail image not yet assigned)";

      if (s.photoID > 0)
      {
         var si = new sImageInfo(s.photoURL, s.photoWidth, s.photoHeight);
         //         infoPhoto = s.photoURL.substr(s.photoURL.indexOf("/") + 1 ) + " (" + s.photoWidth + " x " + s.photoHeight +
         // ")";
         infoPhoto = si.iFilename.toUpperCase() + " " + si.dimensions
         if(s.photoURL.toLowerCase().find("images/gallery/") == false)
         {
            error = true;
            infoPhoto = "(*p) " + infoPhoto;
            tdInfoPhoto.style.color = "red";
            errStr += (errStr == "" ? "" : "\n") + "(*p) Image in " + si.iFolder.toUpperCase() + " folder instead of GALLERY.";
            tdInfoError.innerText = errStr;
         }
      }

      else infoPhoto = "(Photo image not yet assigned)";
   }
   errStr += "\nThumbnails belong in THUMBNAILS folder, Photos in GALLERY folder";
   errStr += "\nTo show in Thumbs/Photos selectors, images must be in correct folders."
   tdInfoError.innerText = errStr;
   tdInfoThumb.innerText = infoThumb;
   tdInfoPhoto.innerText = infoPhoto;
   if (error)
   {
      tdInfoError.style.display = "";
      tdInfoError.title = "Fix by going to Admin/Manage Picture Files/Manage Images \& changing the appropriate image\'s folder"
      tblSlideInfo.style.marginTop = "";
      tdInfoThumb.style.height = ""
   }
   else
   {
      tdInfoError.style.display = "none";
      tblSlideInfo.style.marginTop = "5px";
      tdInfoThumb.style.height = "45px"
   }
   tdInfoError.style.display = (error == true ? "" : "none");
   tdInfoError.title = "Fix by going to Admin/Manage Picture Files/Manage Images \& changing the appropriate image\'s folder"



}

function sImageInfo(iURL, iWidth, iHeight)
{
   var f = iURL.substr(0, iURL.lastIndexOf("/"));
   f = f.substr(f.lastIndexOf("/") + 1);
   this.iFilename = iURL.substr(iURL.indexOf("/") + 1 );
   this.iFolder = f;
   this.dimensions = "(" + iWidth + " x " + iHeight + ")"
}

function setSlideSetDirty(dirty)
{
   if (dirty)saveToUndo(true);
   slideSetDirty = dirty;
   document.getElementById("btnUndo").disabled = (dirty == true ? "" : "disabled");


}

function enableSaveButton(enable)
{
   document.getElementById("btnSaveChanges").disabled = (enable == true ? "" : "disabled");
   document.getElementById("lstSets").disabled = (enable == false ? "" : "disabled");
   document.getElementById("btnNewSlideset").disabled = (enable == false  ? "" : "disabled");
   document.getElementById("btnRenameSlideset").disabled = (enable == false  ? "" : "disabled");
   document.getElementById("btnDeleteSlideset").disabled = (enable == false  ? "" : "disabled");
   var p = document.getElementById("btnPreview")
   p.disabled = (enable == false  ? "" : "disabled");
   if (p.disabled == "")
   p.disabled = (mySlides.count > 0 ? "" : "disabled");

}


function undoSlideSet()
{
   saveToUndo(false);
   setSlideSetDirty (false);
   fillSlides(selectedSlide.substr(3), scrollTopUndo);
}

function saveToUndo(toUndo)
{
   var from, to, X, key;
   if (toUndo)
   {
      mySlidesUndo.RemoveAll();
      for (i = 0 ; i < mySlides.Count; i ++ )
      {
         key = "SL_" + i;
         X = mySlides(key)
         var s = new mySlide(X.slideID, X.thumbID, X.photoID, X.thumbWidth, X.thumbHeight, X.photoWidth, X.photoHeight, X.slideText, X.thumbURL, X.photoURL)
         mySlidesUndo.add(key, s);
      }
      selectedSlideUndo = selectedSlide;
      scrollTopUndo = document.getElementById('panelSlides').scrollTop;
      // document.getElementById("btnSaveChanges").disabled = "";
      enableSaveButton(true)
      document.getElementById("btnUndoAll").disabled =  "";
   }
   else
   {
      mySlides.RemoveAll();
      for (i = 0 ; i < mySlidesUndo.Count; i ++ )
      {
         key = "SL_" + i;
         X = mySlidesUndo(key)
         var s = new mySlide(X.slideID, X.thumbID, X.photoID, X.thumbWidth, X.thumbHeight, X.photoWidth, X.photoHeight, X.slideText, X.thumbURL, X.photoURL)
         mySlides.add(key, s);
         selectedSlide = selectedSlideUndo;

      }



   }
}
function undoAll()
{
   // var ss = document.getElementById('txtSlideSetID').value
   var ss = selectedSet.slidesetID;
   RequestSlideSet("REQ_SS-" + ss, showIndicator(true));

}

function showSlideOpsButtons(show)
{
   var b;
   b = document.getElementById("btnSlideRemove");
   b.disabled = (show == true ? "" : "disabled");
   b = document.getElementById("btnSlideUp");
   b.disabled = (((show == true) && (mySlides.count > 0)) ? "" : "disabled");
   b = document.getElementById("btnSlideDown");
   b.disabled = (((show == true) && (mySlides.count > 0)) ? "" : "disabled");
   b = document.getElementById("btnChangeThumb");
   b.disabled = (show == true ? "" : "disabled");
   b = document.getElementById("btnChangePhoto");
   b.disabled = (show == true ? "" : "disabled")


}
function trim(value)
{
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp))
   {
      temp = temp.replace(obj, '$2');

   }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ")
   {
      temp = "";

   }
   return temp;
}
