﻿function ShowPopup(url)
{
  popw = window.open(url, null, "height=250, width=400, resizable=no, titlebar=yes, location=no, scrollbars=yes");
  popw.focus();
  return false;
}

Date.prototype.toLocaleShort = function (){
  if (isNaN(this)) {
    return "";
  }
  else {
  var lStr = this.toLocaleString();
  return (this.getMonth() + 1 + '/' + this.getDate() + '/' + this.getFullYear() + ' ' + this.toLocaleTimeString());
  }
}
function setUTC(utcString){
  var uDate = new Date(utcString);
  return new Date(uDate.setUTCMinutes(uDate.getUTCMinutes()-uDate.getTimezoneOffset()));
}

function ShowHelpPage(url)
{
  popw = window.open(url, null, "height=500, width=450, resizable=yes, titlebar=no, location=no, status=no, scrollbars=yes");
  popw.focus();
  return false;
}
function CheckTicketClose(closeCheck, respondToMemberCheck)
{
  closeCheckBox = document.getElementById(closeCheck);
  respondCheckBox = document.getElementById(respondToMemberCheck);
  if (closeCheckBox != null && respondCheckBox != null)
  {
    if (closeCheckBox.checked && !respondCheckBox.checked)
    {
      return 'If you click OK, you will close this request without responding to the member.';
    }
  }
}
function KeyDownHandler(btnID, event) {
	// process only the Enter key
    if (event.keyCode == 13)
    {
      var btn = document.getElementById(btnID);
      // cancel the default submit
      event.returnValue=false;
      event.cancel = true;
      // submit the form by programmatically clicking the specified button
      btn.click();
    }
}
function CheckUserChange()
{
  return 'Changing this member to a non-member will change all staff members associated with this member to non-members and remove the association between staff members and the member.';
}
/* Adapted from http://sstree.tigris.org/nonav/example4.html */

function toggleRows(elm)
{
  if (elm.alt == 'Collapse')
  { 
    // Set my image to expanded
    elm.src = "Images/Forums/ExpandThread.gif";
    elm.alt = "Expand";    
    
    HideChildren(elm);
  }
  else
  { 
    // Set my image to collapsed
    elm.src = "Images/Forums/CollapseThread.gif";
    elm.alt = "Collapse";
    
    ShowChildren(elm);
  }
}

function HideChildren(elm)
{ 
  // Get all the rows
  var rows = document.getElementsByTagName("TR");
  var thisID = elm.parentNode.parentNode.parentNode.parentNode.id + "-";
  for (var i = 0; i < rows.length; i++) 
  {
   var r = rows[i];
   if (matchStart(r.id, thisID))
   {
     var img = GetImageFromRow(r);

     if (img != undefined)
     {
       HideChildren(img);
     }
     r.style.display = "none";     
   }
  }
}

function ShowChildren(elm)
{
  // Get all the rows  
  var rows = document.getElementsByTagName("TR");
  var thisID = elm.parentNode.parentNode.parentNode.parentNode.id + "-";
  
  // Find matching children
  for (var i = 0; i < rows.length; i++) 
  {
   var r = rows[i];
   if (matchStart(r.id, thisID))
   {
     // See if this is a parent node (will have an image with alt set)
     var img = GetImageFromRow(r);
       
     if (img != undefined && img.alt == "Collapse")
     {
       ShowChildren(img);
     }
     
     if (document.all) 
       r.style.display = "block"; //IE4+ specific code
     else 
       r.style.display = "table-row"; //Netscape and Mozilla
   }
  }
}

function GetImageFromRow(r)
{
  // Row is set up like this:
	//<tr>
	//	<td><div><div><img/></div><a>Subject</a></div></td>
	//	<td><a>Author</a></td>
	//	<td>Time</td>
	//	<td>Hits</td>
	//	<td>Attach</td>
	//</tr>

  var cell = r.getElementsByTagName("TD")[0];
  var imgs, img;
  
  if (cell != undefined)
    imgs = cell.getElementsByTagName("IMG");
  else
    return undefined;

  if (imgs != undefined)
  { 
    // There may be 2 images if this is a new item
    if (imgs.length > 1)
     img = imgs[1];
    else
     img = imgs[0];
  }
  else
    return undefined;
    
  return img;
}

function matchStart(target, pattern) 
{
 var pos = target.indexOf(pattern);
 if (pos != 0) 
    return false;
 if (target.slice(pos + pattern.length, target.length).indexOf("-") >= 0) 
    return false;
 return true;
}

function SetThreadView(newView)
{
  if (newView == "")
  {
    var CurrView = Get_Cookie("ThreadExpandState");
    if (CurrView == null || CurrView == "Collapsed")
      newView = "Expanded"
    else
      newView = "Collapsed"
  }
  
  if (newView == "Expanded")
  {
    expandAllRows();
  }
  else
  {
    collapseAllRows();
  }
      
  Set_Cookie('ThreadExpandState', newView, false);
}

function Get_Cookie(name)
{
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) &&
  ( name != document.cookie.substring( 0, name.length ) ) )
  {
    return null;
  }
  if ( start == -1 ) 
    return null;
  var end = document.cookie.indexOf( ";", len );
  if ( end == -1 ) 
    end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}

function Set_Cookie(name, value) 
{
  var expireDate = new Date();
  expireDate.setFullYear(9999,11,31);
  document.cookie = name + "=" + escape( value ) + ";expires=" + expireDate.toGMTString();
}

function collapseAllRows() 
{
  var rows = document.getElementsByTagName("TR");
  for (var j = 0; j < rows.length; j++) 
  {
    var r = rows[j];
    var img = GetImageFromRow(r);
    if (img != undefined && img.alt == "Collapse")
    {
     img.src = "Images/Forums/ExpandThread.gif";
     img.alt = "Expand";
    }  
    if (r.id.indexOf("-") >= 0) 
    {
     r.style.display = "none";
    }
  }
}

function expandAllRows() 
{
 var rows = document.getElementsByTagName("TR");
 for (var j = 0; j < rows.length; j++) 
 {
   var r = rows[j];
   var img = GetImageFromRow(r);
   if (img != undefined && img.alt == "Expand")
   {
    img.src = "Images/Forums/CollapseThread.gif";
    img.alt = "Collapse";
   }

   if (r.id.indexOf("-") >= 0) 
   {
     if (document.all) r.style.display = "block"; //IE4+ specific code
     else r.style.display = "table-row"; //Netscape and Mozilla
     var img = GetImageFromRow(r);
   }
 }
}

function toggleTable(elm)
{ 
  if (elm.alt == 'Collapse')
  { 
    // Set my image to expanded
    elm.src = "Images/Forums/ExpandThread.gif";
    elm.alt = "Expand";    
    
    HideChildTables(elm);
  }
  else
  { 
    // Set my image to collapsed
    elm.src = "Images/Forums/CollapseThread.gif";
    elm.alt = "Collapse";
    
    ShowChildTables(elm);
  }
}

function HideChildTables(elm)
{ 
  // Get all the tables
  var tables = document.getElementsByTagName("TABLE");
  var thisID = elm.parentNode.parentNode.id + "-";

  for (var i = 0; i < tables.length; i++) 
  {
   var t = tables[i];
   if (matchStart(t.id, thisID))
   {
     t.style.display = "none";     
   }
  }
}

function ShowChildTables(elm)
{
  // Get all the rows  
  var tables = document.getElementsByTagName("TABLE");
  var thisID = elm.parentNode.parentNode.id + "-";
  
  // Find matching children
  for (var i = 0; i < tables.length; i++) 
  {
   var t = tables[i];
   
   if (matchStart(t.id, thisID))
   {    
     if (document.all) 
       t.style.display = "block"; //IE4+ specific code
     else 
       t.style.display = "table-row"; //Netscape and Mozilla
   }
  }
}

function collapseAllTables() 
{
  var rows = document.getElementsByTagName("TR");
  for (var j = 0; j < rows.length; j++) 
  {
    var r = rows[j];
    var img = GetImageFromRow(r);
    if (img != undefined && img.alt == "Collapse")
    {
     img.src = "Images/Forums/ExpandThread.gif";
     img.alt = "Expand";
    }  
  }
  
  var tables = document.getElementsByTagName("TABLE");
  for (var j = 0; j < tables.length; j++)
  {
    if (tables[j].id.indexOf("-") >= 0)
    {
      tables[j].style.display = "none";
    }
  }
}

function expandAllTables() 
{
 var rows = document.getElementsByTagName("TR");
 for (var j = 0; j < rows.length; j++) 
 {
   var r = rows[j];
   var img = GetImageFromRow(r);
   if (img != undefined && img.alt == "Expand")
   {
    img.src = "Images/Forums/CollapseThread.gif";
    img.alt = "Collapse";
   }
 }
  var tables = document.getElementsByTagName("TABLE");
  for (var j = 0; j < tables.length; j++)
  {
   if (tables[j].id.indexOf("-") >= 0) 
   {
     if (document.all) tables[j].style.display = "block"; //IE4+ specific code
     else tables[j].style.display = "table-row"; //Netscape and Mozilla
   }
  }
}

function changeNavImage(_id, _name) {
	document.getElementById(_id).src = 'Images/LandingPage/'+_name+'';
}
