<!--


var share_content =
function(share_boxID)
{
   this.share_box = null;              // Box that will pop in and out of view

   // The following can be changed after creating the share_content object
   this.tab_id = "share_tabs";         // ID Name given to the tabs object within the share box
   this.content_ext = "content";       // Extension given to content boxes. They should share their
                                       //   link nodes id. For example. share_email would be share_email_content
   this.tab_highlight = "on";          // Classname to give the tab when it is highlighted
   this.bookmark_nosupport = " (CTRL+D)";  // Alternate text used when Javascript cannot bookmark the page

   this.defaultTitle = "";             // It is recommended to change this upon initating the object
   this.shareOptions = "";             // Shared options
   this.pop = true;                    // Specifies if the share_box object will float/pop onto the page
                                       //    or if it is a static object on the page

   this.defaultEmailMessage = "";      // Should be set when this object is created
                                       // Replacement text are: %title% and %linkback%

   // Offsets to move the share box when it pops open.
   //    Left: (-) moves right, (+) moves left
   //    Top: (-) moves down, (+) moves up
   this.offsetLeft = -8;               // Offset the share box to the left when it pops open
   this.offsetTop = 6;                 // Offset the share box to the top when it pops open

   // ID for shared objects
   // Any email fields will have the word "email" appended to the start of the id string
   this.email_refer_id = "refer";      // Refer objects ID (hidden linkback url field)
   this.email_subject_id = "subject";  // Email subject ID (hidden field)
   this.email_message_id = "message";  // Email message id (textarea field)
   this.email_status_id = "status";    // Status message shown after email is sent
   this.linkback_id = "linkback";      // URL Text Linkback ID
   this.bookmark_id = "bookmark";      // Bookmark link ID
   this.print_id = "print";            // Print link ID
   this.title_id = "title";            // ID of Title node the share box displays when opened
   // END EDITABLE VARIABLES

   this.shareXY = null;             // Share_box' X and Y coordinates
   this.lastLink = null;            // Last share link clicked.
   this.lastTabLink = null;         // Last TAB link clicked.

   this.objERefer = null;           // Refer hidden field
   this.objESubject = null;         // Subject hidden field
   this.objEMessage = null;         // Email Message textarea field
   this.objEStatus = null;          // Email status message after sent
   this.objLinkback = null;         // Linkback text object
   this.objBookmark = null;         // Bookmark object (used to bookmark page)
   this.objPrint = null;            // Print object (used to print page)
   this.objTitle = null;            // Title node object for the share_box

   this.linkBackURL = "";           // Link back URL used to email, bookmark, and view URL
   this.pageTitle = "";             // Current share's title - Make sure quotes are escaped > "A quote (\")"

   this.isBoxOpen = false;          // Indicates if the share box is open
   this.setResizeEvent = true;      // Set to indicate whether or not the window's resize event will fire
   this.moveOnResize = true;        // When set to true, moves the share_box on window resize.

   this.sendEmailTimeout = 2000;     // Timeout it takes to re-enable the email send button

   this.initOnLoad =
   function()
   {
      var foo = this;
      addEvent(window, "load", function(){foo.init();}, false);
   };

   this.init =
   function()
   {
      // Make sure that we can find the share_box object
      //    through an external function
      if(getObjByID == null) return false;

      // Initiate the share_box object
      this.share_box = getObjByID(share_boxID);

      // Now make sure it is not null before we continue
      if(this.share_box == null || typeof(this.share_box) == "undefined") return false;

      // Setup each share_box's tab links click events, only if the tab object exists
      var tabNode = getObjByID(this.tab_id);
      if(tabNode == null || typeof(tabNode) == "undefined") return false;
      // Must be anchor link nodes!
      var lNodes = tabNode.getElementsByTagName("a");

      var tmpNode, lNode_id, lNode_child, lClickFunc;
      for (var i=0; i < lNodes.length; i++)
      {
         tmpNode = lNodes[i];
         lNode_id = tmpNode.id;
         lNode_child = (lNode_id != "") ? getObjByID(lNode_id + "_" + this.content_ext) : null;
         // Only add the mouseclick event if the link node's child exists
         if(lNode_child != null)
         {
            // Setup click event;
            tmpNode.show_content = this.show_content;
            // Link this object back to the link node
            tmpNode.owner = this;
            // Save the links old classname
            tmpNode.oldClass = tmpNode.className;
            // Link the child back to the parent
            tmpNode.content_box = lNode_child;
            tmpNode.onclick = function() { this.show_content(this.id); };
            addEvent(tmpNode, "click", killEvent, false);

            // The first tab should be shown by default
            if(i == 0) this.show_content(lNode_id);

            // Find if this is the email box, if so, setup the status div
            if(lNode_id.toLowerCase().indexOf("email") != -1)
               this.setupEmailStatus(lNode_child);
         }
      }

      var shareExt = share_boxID + "_";
      var shareEmailExt = shareExt + "email"

      // Setup shared object variables
      this.objERefer = getObjByID(shareEmailExt + this.email_refer_id);
      this.objESubject = getObjByID(shareEmailExt + this.email_subject_id);
      this.objEMessage = getObjByID(shareEmailExt + this.email_message_id);
      if(this.objEStatus == null)
         this.objEStatus = getObjByID(shareEmailExt + this.email_status_id);
      if(this.objEStatus != null) setDivStyle(this.objEStatus, "display", "none");
      this.objLinkback = getObjByID(shareExt + this.linkback_id);
      this.objBookmark = getObjByID(shareExt + this.bookmark_id);
      this.objPrint = getObjByID(shareExt + this.print_id);
      this.objTitle = getObjByID(shareExt + this.title_id);

      this.setupBookmark();   // Setup bookmark link
      this.setupPrint();      // Setup print link

      var foo = this;
      // Setup highlight referal URL textbox
      if(this.objLinkback != null && typeof(this.objLinkback) != "undefined")
         addEvent(this.objLinkback, "click", function(){foo.highlight_url();}, false);

      // Exit here if pop is not set to true
      if(!this.pop) return;

      // Make sure we hide the share_box if the window is resized.
      if(this.setResizeEvent)
         addEvent(window, "resize", function(){foo.checkResize();}, false);

      // Hide the share_box if the pop property is set to true
      setDivStyle(this.share_box, "display", "none");
   };

   this.setupEmailStatus =
   function(objBox)
   {
      var statID = share_boxID + "_email" + this.email_status_id;
      var objStat = getObjByID(statID);
      // Exit if the object is found.
      if(objStat != null && typeof(share_boxID) != "undefined") return;
      var div = document.createElement("div");
      div.id = statID;
      objBox.appendChild(div);
      this.objEStatus = div;
   };

   // Setup bookmark link to be visible or hidden
   this.setupBookmark =
   function()
   {
      // Bookmark object does not exist. Exit.
      if(this.objBookmark == null || typeof(this.objBookmark) == "undefined") return false;

      var noSupport = ((window.opera && window.print) || 
                       !(window.sidebar || document.all));

      // If bookmarking is not supported then remove the onclick event from the link.
      //    Remove the href value, and set the string to represent this change
      if(!noSupport) return true;
      // If objBookmark is not a link node then find the first linknode within it's content.
      if(!this.objBookmark.href)
      {
         var tmpLinks = this.objBookmark.getElementsByTagName("a");
         if(tmpLinks.length < 0) return true;   // Exit here if no link nodes exist
         // Set this.objBookmark to the first link node object found
         this.objBookmark = tmpLinks[0];
      }
      // Create a span element to replace bookmark link
      var newBookmark = document.createElement("span");
      newBookmark.innerHTML = this.objBookmark.innerHTML.replace(/(\/||)(item)/ig, "") + this.bookmark_nosupport;
      this.objBookmark.parentNode.insertBefore(newBookmark, this.objBookmark);  // Add the new bookmark span element
      this.objBookmark.parentNode.removeChild(this.objBookmark);   // Remove the old bookmark link element
   };

   // Setup print link to be visible or hidden
   this.setupPrint =
   function(show)
   {
      // Print object does not exist. Exit.
      if(this.objPrint == null || typeof(this.objPrint) == "undefined") return false;
      // If show is not set then set it to equal if window.print exists
      //    if show is true, then make sure this object is able to be displayed
      //    by setting it, once again, to if 'window.print' exists
      var show = (typeof(show) != "boolean") ? (window.print != null) :
                  (show) ? (window.print != null) : show;
      var strStyle = (!show) ? "none" : "";
      setDivStyle(this.objPrint, "display", strStyle);
   };

   // Pop/show the share_box content divs
   this.show_content =
   function(e)
   {
      var lNode;

      if(typeof(e) == "string")
         lNode = getObjByID(e);
      else if(getObject(e) == null && typeof(e) == "object")
         lNode = e;
      else
         lNode = getObject(e);

      if(lNode == null || typeof(lNode) == "undefined") return false;

      var owner = lNode.owner;


      // Make sure owner is a valid object reference
      if(owner == null || typeof(owner) == "undefined") return false;

      var lastNode = owner.lastTabLink;

      if(lastNode == lNode) return false;

      var cBox = lNode.content_box;
      setDivStyle(cBox, "display", "block");

      // Hide the last link active, and set this as the last link
      if(lastNode != null)
      {
         setDivStyle(lastNode.content_box, "display", "none");
         // Reset the classname
         lastNode.className = lastNode.oldClass;
      }

      owner.lastTabLink = lNode;    // Set the last TAB link clicked
      lNode.className = owner.tab_highlight;

      return false;
   };

   // Open function to show and setup the share_box object
   this.open =
   function(objLink, strLinkURL, strTitle, strOptions)
   {
      // Make sure it is not null before we continue
      if(this.share_box == null || typeof(this.share_box) == "undefined") return false;

      // Do not go any further if strLinkUrl is null or blank
      // Also exit if objLink is null or not an object
      if(strLinkURL == null || strLinkURL == "" ||
         objLink == null || typeof(objLink) == "undefined") return false;

      // Check to see if any links last opened the share_box object,
      //    if so then make them visible again
      if(this.lastLink != null) setDivStyle(this.lastLink, "visibility", "visible");

      this.lastLink = objLink;

      // Set some defaults
      this.shareOptions = (strOptions == null) ? this.shareOptions : strOptions;
      this.shareOptions.toLowerCase(); // Make it lowercase
      this.pageTitle = (strTitle == null) ? this.defaultTitle : strTitle;

      // Setup some share string variables
      this.linkBackURL = strLinkURL;

      // Setup some values for our share objects
      if(this.objTitle != null) this.objTitle.innerHTML = objLink.innerHTML;
      if(this.objERefer != null) this.objERefer.value = strLinkURL;
      if(this.objESubject != null) this.objESubject.value = this.pageTitle;
      if(this.objEMessage != null)
      {
         var eMessage = this.defaultEmailMessage.replace("%title%", this.pageTitle);
         eMessage = eMessage.replace("%linkback%", strLinkURL);
         this.objEMessage.value = eMessage;
      }
      if(this.objLinkback != null) this.objLinkback.value = strLinkURL;

      // Options:
      if(this.shareOptions.indexOf("noprint") > -1) this.setupPrint(false);
      else  this.setupPrint(true);

      // Exit at this point if the pop variable is not true
      if(!this.pop) return false;

      // Hide the link that opened the share_box object
      //    It will be displayed again when the box is closed.
      setDivStyle(this.lastLink, "visibility", "hidden");

      // Display the sharebox first, but make it invisible
      setDivStyle(this.share_box, "display", "block");
      setDivStyle(this.share_box, "visibility", "hidden");

      this.setBoxPos(true);

      setDivStyle(this.share_box, "visibility", "visible");
      this.isBoxOpen = true;     // Indicate the the share_box is open now

      return killEvent();   // Kill any click events here
   };

   // Close the share_box
   this.close =
   function()
   {
      // Do nothing if this object is a static object on the page
      if(!this.pop) return false;

      // Make sure we show the link that last opened the share_box object
      setDivStyle(this.lastLink, "visibility", "visible");
      // make the last link null
      this.lastLink = null;

      setDivStyle(this.share_box, "display", "none");
      this.isBoxOpen = false;     // Indicate the the share_box is closed now
      return killEvent();   // Kill any click events here
   };

   this.checkResize =
   function()
   {
      if(!this.isBoxOpen) return;
      this.setBoxPos();
   };

   // Sets popup position variables for the share_box object
   this.getBoxPos =
   function()
   {
      // If an error occurs then close the share box.
      try
      {
         var objLink = this.lastLink;

         // Hide the share box and exit if objLink is null
         if(!objLink) { this.close(); return false; }

         var resetXY = (resetXY == null) ? false : true;
         var shareXY;

         // Get the share_box' origin X and Y coords
         // We have to move the share_box to the origin first
         setDivStyle(this.share_box, "left", "0px");
         setDivStyle(this.share_box, "top", "0px");

         shareXY = getObjectXY(this.share_box);

         // Now get the link objects x and y coords
         var loXY = getObjectXY(objLink);
         var linkWidth = objLink.offsetWidth;

         var newX, newY, shareWidth;
         shareWidth = this.share_box.offsetWidth;

         newX = Math.abs((loXY[0] - (shareXY[0] + this.offsetLeft)) - shareWidth + linkWidth);
         newY = Math.abs(loXY[1] - (shareXY[1] + this.offsetTop) );

         objLink.loXY = loXY;    // Set X & Y positions for the link object
         return [newX, newY];
      }
      catch(ex)
      {
         this.close()
         return null;
      }
   };

   // This function finds the offset that the link node has moved on
   //    the X and Y plane and moves the share_box according to the offset
   this.getBoxPos_open =
   function()
   {
      // If an error occurs then close the share box.
      try
      {
         var objLink = this.lastLink;
         var old_loXY = objLink.loXY;
         var new_loXY = getObjectXY(objLink);
         var shareXY = this.shareXY;

         if(old_loXY == new_loXY) return shareXY;

         var newX, newY, offX, offY
         // Get the offset that the link object has moved
         offX = (new_loXY[0] - old_loXY[0]);
         offY = (new_loXY[1] - old_loXY[1]);

         newX = shareXY[0] + offX;
         newY = shareXY[1] + offY;

         objLink.loXY = new_loXY;    // Set X & Y positions for the link object

         return [newX,newY];
      }
      catch(ex)
      {
         this.close()
         return null;
      }
   };

   this.setBoxPos =
   function(resetPos)
   {
      var resetPos = (resetPos == null) ? false : true;
      var newXY;

      if(resetPos)
         newXY = this.getBoxPos();
      else
         newXY = this.getBoxPos_open();

      if(newXY == null || (!this.moveOnResize && !resetPos)) return;

      this.shareXY = newXY;

      setDivStyle(this.share_box, "left", newXY[0] + "px");
      setDivStyle(this.share_box, "top", newXY[1] + "px");
   };

   // Bookmark page function
   this.bookmark =
   function()
   {
      var url = this.linkBackURL;
      var title = this.pageTitle;
      if(url == "") return false;   // Cannot bookmark blank URL

      try
      {
         if (window.sidebar)     // Mozilla support (firefox)
             window.sidebar.addPanel(title, url, "");
         else if(document.all)   // Internet Explorer
             window.external.AddFavorite(url, title);
         else
         {
            // Alert for unsupported browsers
            alert("Individual item bookmarking is not supported by your browser.\n" +
                  "Please press CTRL+D to bookmark this page.");
         }
      }
      catch(err) { /* Error process goes here */ }
      return false;
   };

   // Print page
   this.print = function() { window.print(); };

   // function to highlight the url textbox
   this.highlight_url =
   function()
   {
      try
      {
         this.objLinkback.focus();
         this.objLinkback.select();
      }
      catch (err) { return false; }
   };

   this.email =
   function(fieldsParent, frmPage, frmButton)
   {
      // Wrap everything in a try catch block
      try
      {
         var frm = getObjByID(fieldsParent);
         // If we cannot find the parent form, exit and let the form
         //    process without ajax support
         if(frm == null || typeof(frm) == "undefined") return true;

         // This is used to check if the field of the form should be validated
         var valForm = (typeof(vforms) != "undefined");

         var chld, i, arrChld;
         var formIsValid = true;
         var qStr = ""; // query string to generate

         // Find INPUT tags first
         arrChld = frm.getElementsByTagName("input");
         for(i = 0; i < arrChld.length; i++)
         {
            chld = arrChld[i];

            if(valForm) formIsValid = this.valField(chld);
            if(!formIsValid) break;
            
            // Do not process VIEWSTATE controls (for asp and aspx pages)
            if(chld.name && chld.name.indexOf("VIEWSTATE") > 1) continue;
            

            // Hidden, Text, and password fields
            if(chld.type == "hidden" || chld.type == "text" || chld.type == "password")
               qStr += chld.name + "=" + replaceAll(chld.value, "&", "%26", "&") + "&";

            // Checkboxes
            if(chld.type == "checkbox")
            {
               if(chld.checked)
                  qStr += chld.name + "=" + replaceAll(chld.value, "&", "%26", "&") + "&";  // Checkbox is checked
               else
                  qStr += chld.name + "=&";  // Checkbox is not checked
            }

            // Radio buttons
            if(chld.type == "radio")
               if(chld.checked) qStr += chld.name + "=" + replaceAll(chld.value, "&", "%26", "&") + "&";
         }

         if(formIsValid)
         {
            // Now find SELECT tags
            arrChld = frm.getElementsByTagName("select");
            for(i = 0; i < arrChld.length; i++)
            {
               if(valForm) formIsValid = this.valField(chld);
               if(!formIsValid) break;

               chld = arrChld[i];
               qStr += chld.name + "=" + replaceAll(chld.options[chld.selectedIndex].value, "&", "%26", "&") + "&";
            }

            var tmpStr;
            // Now find TEXTAREA tags
            arrChld = frm.getElementsByTagName("textarea");
            for(i = 0; i < arrChld.length; i++)
            {
               if(valForm) formIsValid = this.valField(chld);
               if(!formIsValid) break;

               chld = arrChld[i];
               tmpStr = replaceAll(chld.value, "\n", "{NL}", "\\n");
               tmpStr = replaceAll(tmpStr, "&", "%26", "&")
               qStr += chld.name + "=" + tmpStr + "&";
            }

            // Process AJAX query here
            if(typeof(loadHREFObj) != "undefined")
            {
               setDivStyle(this.objEStatus, "display", "block");

               var urlSend = frmPage + "?" + qStr;
               var objLoad = new loadHREFObj(urlSend, "share_emailstatus", null, "<!--Start-->", "<!--End-->");
               objLoad.okToReload = true;
               objLoad.setXMLType = true;
               var foo = this;
               objLoad.externalFunction = function(st){foo.postProcessEmail(st, frmButton)};
               objLoad.init();
               // Disable the submit button for now
               frmButton.disabled = true;
            }
         }

         return killEvent();
      }
      catch (e)
      { /* do nothing */ }
   };

   this.postProcessEmail =
   function(stat, frmButton)
   {
      if(!stat) { frmButton.disabled = false; return; }
      // Disable the submit button for now.
      frmButton.disabled = true;

      // Create a function to enable the submit button
      //    and hide the status message
      var foo = this;
      var doEnable =
         function()
         {
            frmButton.disabled = false;
            setDivStyle(foo.objEStatus, "display", "none");
         };

      setTimeout(doEnable, this.sendEmailTimeout);
   };

   // Validates a field when vforms is present
   //    vforms is an external javascript object
   this.valField =
   function(obj)
   {
      // Cannot get the attribute of the object,
      //    return success here
      if(!obj.getAttribute) return true;

      // This field is not required, return success
      if(obj.getAttribute("required") == null) return true;

      return vforms.validateField(obj)
   };
};

// -->
