////////////////////////////////////////////////////////////////////////////////////////////////////
// utils.js
//
// Javascript utilities for fun and profit
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// Breakpoint for debugging.  
////////////////////////////////////////////////////////////////////////////////////////////////////

function Breakpoint()
{
  // Add "Breakpoint();" func call to js code where 
  //   you need to break on loading

  // Set breakpoint below on curly brace (end of func)
  ;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Make sure platform is windows and browser is ie.  
////////////////////////////////////////////////////////////////////////////////////////////////////

function CheckForWinIe()
{
  // convert all characters to lowercase to simplify testing
  var agt = navigator.userAgent.toLowerCase();
  var appVer = navigator.appVersion.toLowerCase();

  // *** BROWSER VERSION ETC ***

  var is_minor = parseFloat(appVer);
  var is_major = parseInt(is_minor);
  var is_opera = (agt.indexOf("opera") != -1);
  
  // Note: On IE, start of appVersion return 3 or 4
  // which supposedly is the version of Netscape it is compatible with.
  // So we look for the real version further on in the string
  // And on Mac IE5+, we look for is_minor in the ua; since 
  // it appears to be more accurate than appVersion - 06/17/2004

  var is_mac = (agt.indexOf("mac") != -1);
  var iePos  = appVer.indexOf('msie');
  if (iePos !=-1) 
  {
    if (is_mac) 
    {
      var iePos = agt.indexOf('msie');
      is_minor = parseFloat(agt.substring(iePos+5,agt.indexOf(';',iePos)));
    }
    else 
    {
      is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));
    }
    is_major = parseInt(is_minor);
  }
  var is_konq = false;
  var kqPos = agt.indexOf('konqueror') != -1 ? true : false;
  var is_safari = ((agt.indexOf('safari') != -1) && (agt.indexOf('mac') != -1)) ? true : false;
  var is_khtml = (is_safari || is_konq);  
  var is_ie = ((iePos != -1) && (! is_opera) && (! is_khtml));
  var is_ie5_5up =(is_ie && is_minor >= 5.5);
  var is_ie6   = (is_ie && is_major == 6);
  var is_ie6up = (is_ie && is_minor >= 6);

  // Determine what to do...
  var msg = '';

  if (is_mac) 
  {
    msg = 'This application is designed to run on the Windows operating system.  ';
  }
  if (! is_ie6up)
  {
    msg += 'This application will not work properly using this browser.  ';
    msg += 'Microsoft Internet Explorer version 6.0 or better is required for this application.  ';
  }
  if (msg.length > 0)
  {
    alert(msg);
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Display an hourglass cursor
//
// Use on postback...
// <body onbeforeunload="CursorHourglass();" onunload="CursorHourglass();">
////////////////////////////////////////////////////////////////////////////////////////////////////

function CursorHourglass()
{
  //document.body.style.cursor = 'wait';
}

function CursorDefault()
{
  document.body.style.cursor = 'default';
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Clear on first focus
//
// Use on postback...
// <body onbeforeunload="CursorHourglass();" onunload="CursorHourglass();">
////////////////////////////////////////////////////////////////////////////////////////////////////

function ClearOnFirstCall(element, text)
{
  if (text == element.innerHTML)
  {
    element.innerHTML = "";
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// NoFrames pops your page out of a frame to take whole window
////////////////////////////////////////////////////////////////////////////////////////////////////

function NoFrames ()
{
  // Keep login page out of frames
  if (top.location != self.location)
  {
    top.location.replace(self.location);
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// String trim funcs (don't work)
////////////////////////////////////////////////////////////////////////////////////////////////////

function Trim (TRIM_VALUE)
{
  if (TRIM_VALUE.length < 1)
  {
    return "";
  }

  TRIM_VALUE = RTrim(TRIM_VALUE);
  TRIM_VALUE = LTrim(TRIM_VALUE);

  if (TRIM_VALUE == "")
  {
    return "";
  }
  else
  {
    return TRIM_VALUE;
  }
} //End Function

function RTrim (VALUE)
{
  var w_space = String.fromCharCode(32);
  var v_length = VALUE.length;
  var strTemp = "";

  if (v_length < 0)
  {
    return "";
  }

  var iTemp = v_length - 1;

  while (iTemp > -1)
  {
    if (VALUE.charAt(iTemp) == w_space)
    {
    }

    else
    {
      strTemp = VALUE.substring(0, iTemp + 1);
      break;
    }

    iTemp = iTemp - 1;
  } //End While

  return strTemp;
}   //End Function


function LTrim (VALUE)
{
  var w_space = String.fromCharCode(32);

  if (v_length < 1)
  {
    return "";
  }

  var v_length = VALUE.length;
  var strTemp = "";
  var iTemp = 0;

  while (iTemp < v_length)
  {
    if (VALUE.charAt(iTemp) == w_space)
    {
    }

    else
    {
      strTemp = VALUE.substring(iTemp, v_length);
      break;
    }

    iTemp = iTemp + 1;
  } //End While

  return strTemp;
}   //End Function


/*

function Trim(s)
{
  s1 = LTrim(s);
  s2 = RTrim(s1);
  return s2;
}

function LTrim(s)
{
  var subject = new String();
  //var result = subject.replace(/\A[\s]+/g, "");
  var chomping = true;
  
  for (var i = 0; i < subject.length; i++)
  {
    if (chomping == false || subject.[i] != " ")
    {
      result += s[i];
    }
  }
  return result;
}

function RTrim(s)
{
  var subject = new String(s);
  var result = subject.replace(/[\s]+\z/g, "");
  return result;
}
*/
////////////////////////////////////////////////////////////////////////////////////////////////////
// Safe email script defeats spam crawler
////////////////////////////////////////////////////////////////////////////////////////////////////

function SafeEmail (user, domain, suffix, display, subject)
{
  // use SafeEmail function inline like so...
  //
  //Example1: <script type="text/javascript">SafeEmail('Brad','rpi','edu','Brad');</script>
  //Example2: <script type="text/javascript">SafeEmail('Brad','rpi','edu','Brad','Help me');</script>
  //Example3: <script type="text/javascript">SafeEmail('Brad','rpi','edu','','Help me');</script>
  //Example4: <script type="text/javascript">SafeEmail('Brad','rpi','edu');</script>

  var addr = new String();
  addr = user + '@' + domain + '.' + suffix;

  var disp = new String();

  if (display != null && display != '')
  {
    disp = display;
  }
  else
  {
    disp = addr;
  }

  var subj = new String();

  if (subject != null && subject != '')
  {
    subj = new String('?subject=') + subject;
  }
  else
  {
    subj = '';
  }

  var anchor = new String('<a href=\"m') + 'ailto:' + addr + subj + '\">' + disp + '</a>';
  document.write(anchor);
}


////////////////////////////////////////////////////////////////////////////////////////////////////
// Maximize this browser window
////////////////////////////////////////////////////////////////////////////////////////////////////

function MaximizeWindow ()
{
  window.moveTo(0, 0);

  if (document.all)
  {
    top.window.resizeTo(screen.availWidth, screen.availHeight);
  }
  else if (document.layers || document.getElementById)
  {
    if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth)
    {
      top.window.outerHeight = screen.availHeight;
      top.window.outerWidth = screen.availWidth;
    }
  }
}


////////////////////////////////////////////////////////////////////////////////////////////////////
// Query string processing
////////////////////////////////////////////////////////////////////////////////////////////////////

function getQueryString (queryString, parameterName)
{
  // Add "=" to the parameter name (i.e. parameterName=value)
  var parameterName = parameterName.toLowerCase() + "=";

  if (queryString.length > 0)
  {
    // Find the beginning of the string (Case insensitive)
    qs = queryString.toLowerCase()
    begin = qs.indexOf(parameterName);

    // If the parameter name is not found, skip it, otherwise return the value
    if (begin != -1)
    {
      // Add the length (integer) to the beginning
      begin += parameterName.length;
      // Multiple parameters are separated by the "&" sign
      end = queryString.indexOf("&", begin);

      if (end == -1)
      {
        end = queryString.length
      }

      // Return the string
      return unescape(queryString.substring(begin, end));
    }

    // Return "null" if no parameter has been found
    return null;
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Cookie processing (with encryption)
////////////////////////////////////////////////////////////////////////////////////////////////////

function SetCookie (name, value, expires, path, domain, secure)
{
  // name - name of the cookie
  // value - value of the cookie
  // [expires] - expiration date of the cookie
  //    (defaults to end of current session)
  // [path] - path for which the cookie is valid
  //    (defaults to path of calling document)
  // [domain] - domain for which the cookie is valid
  //    (defaults to domain of calling document)
  // [secure] - Boolean value indicating if the cookie transmission requires
  //    a secure transmission
  //  * an argument defaults when it is assigned null as a placeholder
  //  * a null placeholder is not required for trailing omitted arguments

  var myValue = value;
  var myExpires = (expires) ? "; expires=" + expires.toGMTString() : "";
  var myPath = (path) ? "; path=" + path : "";
  var myDomain = (domain) ? "; domain=" + domain : "";
  var mySecure = (secure) ? "; secure" : "";
  var myCookie = name + "=" + myValue + myExpires + myPath + myDomain + mySecure;
  document.cookie = myCookie;
}

function GetCookie (name)
{
  // name - name of the desired cookie
  // return string containing value of specified cookie or null
  // if cookie does not exist
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);

  if (begin == -1)
  {
    begin = dc.indexOf(prefix);

    if (begin != 0)
      return null;
  }
  else
  {
    begin += 2;
  }

  var end = document.cookie.indexOf(";", begin);

  if (end == -1)
  {
    end = dc.length;
  }

  var cookieValue = unescape(dc.substring(begin + prefix.length, end));

  return cookieValue;
}

function DeleteCookie (name, path, domain)
{
  // name - name of the cookie
  // [path] - path of the cookie (must be same as path used to create cookie)
  // [domain] - domain of the cookie (must be same as domain used to
  //    create cookie)
  // path and domain default if assigned null or omitted if no explicit
  //    argument proceeds
  if (GetCookie(name))
  {
    var myPath = (path) ? "; path=" + path : "";
    var myDomain = (domain) ? "; domain=" + domain : "";
    document.cookie = name + "=" + myPath + myDomain + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function FixDate (date)
{
  // date - any instance of the Date object
  // * hand all instances of the Date object to this function for "repairs"
  var base = new Date(0);
  var skew = base.getTime();

  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
