// System Level JavaScript Functions <sys_util.js>
// Copyright (C) 2005 HighTower Incorporated
// $Id: MAS90/TK/wwwroot/system/sys_util.js 1.9 2007/02/20 12:11:36CST ewojcik SrvcPack  $


// ------------------------------------------------------------------------------------------
// The following variables are used to display the lookup frame.

IFrameWidth = 260
IFrameHeight = 300


// ------------------------------------------------------------------------------------------
// Set the default date.

var todaysDate = new Date();

var monthString = String(parseInt(todaysDate.getMonth()) + 1);
var month;
if (monthString.length == 1) {
  month = "0" + monthString;
} else {
  month = monthString
}

var dayString = String(parseInt(todaysDate.getDate()));
var day;
if (dayString.length == 1) {
  day = "0" + dayString;
} else {
  day = dayString
}

var dfltDate = month + "/" + day + "/" + todaysDate.getFullYear();


// ------------------------------------------------------------------------------------------
// Detect browser type.

var detect = navigator.userAgent.toLowerCase();
var OS,browser;

if (checkBrowser('safari')) browser = "Safari";
else if (checkBrowser('opera')) browser = "Opera";
else if (checkBrowser('msie')) browser = "Internet Explorer";
else if (checkBrowser('firefox')) browser = "Firefox";

if (checkBrowser('mac')) OS = "Mac";

function checkBrowser(string) {
  place = detect.indexOf(string) + 1;
  thestring = string;
  return place;
}


// ------------------------------------------------------------------------------------------
// The following functions are child window specific.

var childWindowHandles = new Array();

// Open a child window.
// Use this function if the child window needs to be closed automatically 
// with its parent window upon a certain condition.
//
function openNewWindow(url, name, params) {
  childWindowHandles[childWindowHandles.length] = window.open(url, name, params);
}


// close all child windows if they are open
//
function closeChildWindows() {
  for (var loop=0; loop<childWindowHandles.length; loop++) {
    if (!childWindowHandles[loop].closed) {
      childWindowHandles[loop].close();
    }
  }
}


// ------------------------------------------------------------------------------------------
// Get an elements' position.
//
function getElementPosition(elemID) {
  var offsetTrail = document.getElementById(elemID);
  var offsetLeft = 0;
  var offsetTop = 0;

  while (offsetTrail) {
    offsetLeft += offsetTrail.offsetLeft;
    offsetTop += offsetTrail.offsetTop;
    offsetTrail = offsetTrail.offsetParent;
  }
  if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") {
    offsetLeft += document.body.leftMargin;
    offsetTop += document.body.topMargin;
  }
  return {left:offsetLeft, top:offsetTop};
}


// ------------------------------------------------------------------------------------------
// The following functions are used to encode and decode a string.

// Replace special characters in a string with expressions.
//
function encodeString(inStr) {
  if (inStr == "") {
    return inStr;
  }
  var newStr = inStr;

  newStr = newStr.replace(/\"/g,"<<quo>>");
  newStr = newStr.replace(/\'/g,"<<aph>>");
  newStr = newStr.replace(/\&/g,"<<amp>>");
  newStr = newStr.replace(/\?/g,"<<que>>");
  newStr = newStr.replace(/\%/g,"<<per>>");
  newStr = newStr.replace(/\#/g,"<<num>>");
  newStr = newStr.replace(/\n/g,"<br>");
  newStr = newStr.replace(/\r/g,"");

  return newStr;
}


// Replace expressions in a string with special characters.
//
function decodeString(inStr) {
  if (inStr == "") {
    return inStr;
  }
  var newStr = inStr;

  newStr = newStr.replace(/\<<quo>>/g,"\""); 
  newStr = newStr.replace(/\<<aph>>/g,"\'"); 
  newStr = newStr.replace(/\<<amp>>/g,"&"); 
  newStr = newStr.replace(/\<<que>>/g,"?"); 
  newStr = newStr.replace(/\<<per>>/g,"%"); 
  newStr = newStr.replace(/\<<num>>/g,"#"); 
  newStr = newStr.replace(/\<br>/g,"\n"); 

  return newStr;
}


// ------------------------------------------------------------------------------------------
// Check if the input is numeric.
//
function isNumber(num) {
  var checknum = parseInt(num);
  return !isNaN(checknum);
}


// ------------------------------------------------------------------------------------------
// Format the input according to a mask.
//
function formatNumeric(t,decimal) {
  decimal = parseInt(decimal);

  // verify the input is numeric
  if (isNaN(t.value)) {
    t.value = "0";
  }
  var nT=Math.pow(10,decimal);
  var sValue = Math.round(t.value * nT) + "";
  if (sValue == 0) {
    sValue = pad(decimal+1,t.value.toString(),'0');    
  }
  a = new Array();
  a = sValue.split(".");

  // there's already a decimal point in the string
  if (a.length == 2) { 
    return;
  }

  var sPart1 = sValue.substr(0, sValue.length-decimal);
  var sPart2 = sValue.substr(sValue.length-decimal);
  var s = sPart1 + "." + sPart2;
  t.value = s;
}


// ------------------------------------------------------------------------------------------
//                             Decimal        Clock
//     Units     Minutes       Format        Format
//
//        90           90         1.50         1:30
//       1.5           90         1.50         1:30
//      1.50           90         1.50         1:30
//       1:5           65         1.08         1:05
//      1:50          110         1.83         1:50
//
// Convert units to minutes
//
function convertUnitsToMinutes(strTime,intBefore,intAfter) {
  strTime = trim(strTime);

  var dblTempHrs = 0;
  var dblTempMin = 0;
  var dblFraction = 0;
  var intDotPos = -1;
  var intColonPos = -1;
  var intMinusPos = -1;
  var intMinusPosTemp = -1;
  intDotPos = InStr(strTime, ".");
  intColonPos = InStr(strTime, ":");
  intMinusPos = InStr(strTime, "-");

  try {
    if ((intDotPos == 0 || intColonPos == 0 || intMinusPos == 0) && strTime.length == 1) {
      // Only a dot or a colon or a minus sign has been entered
      return "0";
    } else {
      if (intDotPos == strTime.length - 1 || intColonPos == strTime.length - 1) {
        // The value entered ends with a dot or a colon
        strTime = strTime + "00";
      }
    }

    if (intMinusPos != -1) {
      // A negative value has been entered
      if (intMinusPos == 0) {
        strTime = strTime.substr(intMinusPos + 1, strTime.length - intMinusPos);
      } else {
        if (intMinusPos == strTime.length) {
          strTime = strTime.substr(0, strTime.length - 1);
        }
      }
      intMinusPosTemp = InStr(strTime, "-");
      if (intMinusPosTemp != -1) {
        // More than one minus sign has been entered
        return "0";
      }
    }

    if (intDotPos == -1 && intColonPos == -1) {
      if (strTime.length > intBefore) {
        return "0";
      }
      dblTempHrs = parseInt(parseFloat(strTime) / 60);
      dblTempMin = parseFloat(strTime) / 60;
      intDotPos = InStr(dblTempMin.toString(), ".");
      if (intDotPos != -1) {
        dblFraction = parseFloat(dblTempMin.toString().substr(intDotPos, dblTempMin.toString().length - intDotPos));
      }
      numTemp = dblFraction * 60
      dblTempMin = Math.round(numTemp.toFixed(2));
    } else {
      if (intDotPos != -1) {
        if (strTime.substr(0, intDotPos).length > intBefore || strTime.substr(intDotPos + 1, strTime.length - intDotPos).length > intAfter) {
          return "0";
        }
        dblTempHrs = parseInt(parseFloat(strTime));
        dblTempMin = parseFloat(strTime);
        intDotPos = InStr(dblTempMin.toString(), ".");
        if (intDotPos != -1) {
          dblFraction = parseFloat(dblTempMin.toString().substr(intDotPos, dblTempMin.toString().length - intDotPos));
        }
        numTemp = dblFraction * 60
        dblTempMin = Math.round(numTemp.toFixed(2));
      } else {
        if (intColonPos != -1) {
          if (intColonPos == 0) {
            dblTempHrs = 0;
          } else {
            if (strTime.substr(0, intColonPos).length > intBefore || strTime.substr(intColonPos + 1, strTime.length - intColonPos).length > intAfter) {
              return "0";
            }
            dblTempHrs = parseInt(strTime.substr(0, intColonPos));
          }
          dblTempMin = parseInt(strTime.substr(intColonPos + 1, strTime.length - intColonPos));
        }
      }
    }
    strTime = dblTempHrs * 60 + parseInt(dblTempMin)
    if (intMinusPos != -1) {
      strTime = "-" + strTime;
    }
   } catch (error) {
    strTime = "0";
  } 

  if (isNaN(strTime)) {
    strTime = "0";
  }

  return strTime;
}

// ------------------------------------------------------------------------------------------
// Convert units (in minutes) using a decimal format
//
function formatUnitsDecimal(strTime) {
  var strTempHrs = 0;
  var strTempMin = 0;
  var strTempTime = 0;
  var numTempTime = 0;
  var intDotPos = -1;
  var intColonPos = -1;
  var intMinusPos = -1;
  var strDfltTime = "0.00";
  intMinusPos = InStr(strTime, "-");
  if (intMinusPos != -1) {
    strTime = strTime.substr(intMinusPos + 1, strTime.length - intMinusPos);
  }

  try {
    numTempTime = parseFloat(strTime) / 60;
    strTempTime = numTempTime.toFixed(2);
    intDotPos = InStr(strTempTime, ".");
    if (intDotPos != -1) {
      strTempHrs = strTempTime.substr(0, intDotPos);
      strTempMin = strTempTime.substr(intDotPos + 1, strTempTime.length - intDotPos);
      if (strTempMin.length != 2) {
        strTempMin = pad(2,strTempMin,'0');    
      }
      strTempTime = strTempHrs + "." + strTempMin;
    } else {
      strTempTime = strTempTime + ".00";
    }
    if (intMinusPos != -1) {
      strTempTime = "-" + strTempTime;
    }
  } catch (error) {
    strTempTime = strDfltTime;
  }

  return strTempTime;
}


// ------------------------------------------------------------------------------------------
// Convert units (in minutes) using a clock format
//
function formatUnitsClock(strTime,intBefore,intAfter) {
  var strTempHrs = 0;
  var dblTempMin = 0;
  var strTempMin = 0;
  var strTempTime = 0;
  var intDotPos = -1;
  var intColonPos = -1;
  var intMinusPos = -1;
  var dblFraction = 0;
  var strDfltTime = "0:00";
  intMinusPos = InStr(strTime, "-");
  if (intMinusPos != -1) {
    strTime = strTime.substr(intMinusPos + 1, strTime.length - intMinusPos);
  }

  try {
    strTempHrs = parseInt(parseFloat(strTime) / 60);
    numTemp = parseFloat(strTime) % 60
    dblTempMin = numTemp.toFixed(2);
    intDotPos = InStr(dblTempMin.toString(), ".");
    if (intDotPos != -1) {
      dblTempMin = dblTempMin.toString().substr(0, intDotPos);
    }
    intDotPos = InStr(strTime, ".");
    intColonPos = InStr(strTime, ":");

    if (intDotPos != -1) {
      if (strTime.substr(0, intDotPos).length > intBefore || strTime.substr(intDotPos + 1, strTime.length - intDotPos).length > intAfter) {
        return strDfltTime;
      }
      strTempHrs = parseInt(parseFloat(strTime));
      dblTempMin = strTime * 60;
      intDotPos = InStr(dblTempMin.toString(), ".");
      if (intDotPos != -1) {
        dblFraction = parseInt(dblTempMin.toString().substr(intDotPos, dblTempMin.toString().length - intDotPos));
      }
      numTemp = dblFraction
      dblTempMin = numTemp.toFixed(2);
    }

    if (intColonPos != -1) {
      if (strTime.substr(0, intDotPos).length > intBefore || strTime.substr(intDotPos + 1, strTime.length - intDotPos).length > intAfter) {
        return strDfltTime;
      }
      strTempHrs = strTime.substr(0, intColonPos);
      dblTempMin = strTime.substr(intColonPos + 1, strTime.length - intColonPos);
    }

    if (dblTempMin.toString().length != 2) {
      strTempMin = "0" + dblTempMin.toString();
    } else {
      strTempMin = pad(2,dblTempMin.toString(),'0');    
    }
    strTempTime = strTempHrs + ":" + strTempMin;
    if (intMinusPos != -1) {
      strTempTime = "-" + strTempTime;
    }
  } catch (error) {
    strTempTime = strDfltTime;
  }

  return strTempTime;
}


// ------------------------------------------------------------------------------------------
// Trim spaces.
//
function trim(string) {
  while (string.substring(0,1) == ' ') {
    string = string.substring(1,string.length);
  }
  while (string.substring(string.length-1,string.length) == ' ') {
    string = string.substring(0,string.length-1);
  }
  return string;
}


// ------------------------------------------------------------------------------------------
// Pad a string.
//
function pad(length,string,filler) {
  if (length <= string.length) {
    return string;
  }
  max = length - string.length;

  for (var i = 0; i < max; i++)  {
    string += filler;
  }
  return string;
}


// ------------------------------------------------------------------------------------------
// Find the position of the specified character in a string.
// Returns the first location a character was found in the string.  
// If the character is not found, -1 is returned.
//
function InStr(string, character) {
  for (i=0; i < string.length; i++) {
    if (character == string.substr(i, 1)) {
        return i;
    }
  }
  return -1;
}


// ------------------------------------------------------------------------------------------
// When ENTER is pressed, move focus to and select the next input field.
// Do not select dropdown boxes and buttons.
// '.value = .value' is used as a means to remove the highlight from the current field.
// Don't do anything if the next field cannot receive focus because it is hidden, locked or disabled.
// If uncertain whether that's the case, DO NOT USE THIS FUNCTION (set focus explicitly using isEnter() function inside the .html or .asp page instead). 
// This function is commonly used in Search Options.
//
function moveFocus(evt, ths) {
  try {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13) {

      for (var i = 0; i < ths.form.elements.length; i++) {
        if (ths == ths.form.elements[i]) {
          ths.form.elements[i].value = ths.form.elements[i].value;
          i = i + 1;
          ths.form.elements[i].focus(); 
          if (ths.form.elements[i].type.substr(0,6) != "select" && ths.form.elements[i].type.substr(0,6) != "button") {
            ths.form.elements[i].select();
          }
          break;  
        }
      }

      return false;
    } else { 
      return true;
    }
  } catch (error) {
  }
}


// ------------------------------------------------------------------------------------------
// The following functions are used to highlight and de-highlight an object, such as a table row. 
// The classes are defined in the .css file.
//
function hl(name) {
  name.className = 'hl';
}

function dl(name) {
  name.className = 'dl';
}


// ------------------------------------------------------------------------------------------
// Display a copyright notice.
// Always use current year.
//
function displayCopyright() {
  var todaysDate = new Date();
  var currentYear = todaysDate.getFullYear()

  document.write ("<table cellpadding=0 cellspacing=0>");
  document.write ("<tr>");
  document.write ("<td class=\"finePrint\">");
  document.write ("©"+currentYear);
  document.write ("<a href=\"http://www.hightowerinc.com\" target=\"_blank\">");
  document.write (" HighTower");
  document.write ("</a>");
  document.write ("</td>");
  document.write ("</tr>");
  document.write ("</table>");
}


// ------------------------------------------------------------------------------------------
// Display the body header.
//
function displayHeader() {
  document.write ("<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/default.css\">");

  document.write ("<body onLoad=\"setDefaults();\" onUnload=\"pageUnload();\" style=\"background-color: #ffffff;\">");

  document.write ("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#F8F8F8\">");
  document.write ("<tr align=\"left\" valign=\"top\">");
  document.write ("<td width=\"1%\" background=\"images/borders/tlrndgry.gif\"><img src=\"images/borders/empty.gif\"></td>");
  document.write ("<td width=\"100%\" background=\"images/borders/mtrndgry.gif\"></td>");
  document.write ("<td width=\"1%\" background=\"images/borders/trrndgry.gif\"><img src=\"images/borders/empty.gif\"></td>");
  document.write ("</tr>");
  document.write ("<tr align=\"left\" valign=\"top\">"); 
  document.write ("<td background=\"images/borders/mlrndgry.gif\"></td>");
  document.write ("<td>"); 
}


// ------------------------------------------------------------------------------------------
// Display the body footer.
//
function displayFooter() {
  document.write ("</td>");
  document.write ("<td background=\"images/borders/mrrndgry.gif\"></td>");
  document.write ("</tr>");
  document.write ("<tr align=\"left\" valign=\"top\">"); 
  document.write ("<td background=\"images/borders/blrndgry.gif\"><img src=\"images/borders/empty.gif\"></td>");
  document.write ("<td background=\"images/borders/mbrndgry.gif\"></td>");
  document.write ("<td background=\"images/borders/brrndgry.gif\"><img src=\"images/borders/empty.gif\"></td>");
  document.write ("</tr>");
  document.write ("</table>");
  document.write ("</body>");
}


// ------------------------------------------------------------------------------------------
// Prevent right-click.
//
document.oncontextmenu = new Function("return false"); 
