/**************************************************************************
**  
**	global_functions.js
**  
**	Global JavaScript functions include file.  Included in any page
**  that wishes to use the global JavaScript functions.
**  
**	Copyright (c) 2003 JIS
**	All Rights Reserved.
**
**	Modified : 24-Nov-2003
**
*/


/* Variable for buttons */
var sOldClassName = null;
var sOldMainClassName = null;

/* Globals */
var dtNotice                  = 0;
var dtError                   = 1;
var dtWarning                 = 2;
var dtConfirmation            = 3;
var dtYesNoCancelConfirmation = 4;
var dtTitles = Array ();
    dtTitles [dtNotice]                  = 'Notice';  
    dtTitles [dtError]                   = 'Error';  
    dtTitles [dtWarning]                 = 'Warning';  
    dtTitles [dtConfirmation]            = 'Confirmation';
    dtTitles [dtYesNoCancelConfirmation] = 'Confirmation';
var drNone                    = -1;    
var drOK                      = 0;
var drCancel                  = 1;
var drYes                     = 2;
var drNo                      = 3;        

var emAdmin              = 0x0001;     // 1
var emEstimator          = 0x0002;     // 2
var emBodyTech           = 0x0003;     // 3
var emPaintTech          = 0x0004;     // 4
var emMechTech           = 0x0005;     // 5
var emFrameTech          = 0x0006;     // 6
var emCSBTech            = 0x0007;     // 7
var emHelper             = 0x0008;     // 8
var emDetailer           = 0x0009;     // 9
var emPartsManager       = 0x000A;     // 10
var emPartsDelay         = 0x000B;     // 11
var emSubletDelay        = 0x000C;     // 12
var emSuppDelay          = 0x000D;     // 13
var emHourlyEmployee     = 0x000E;     // 14
var emNameArray = Array ();
emNameArray [emBodyTech]    = 'Body';
emNameArray [emPaintTech]   = 'Paint';
emNameArray [emMechTech]    = 'Mechanical';
emNameArray [emFrameTech]   = 'Frame';
emNameArray [emCSBTech]     = 'Color Sand/Buff';

var emAbbrevArray = Array ();
emAbbrevArray [emBodyTech]  = 'Body';
emAbbrevArray [emPaintTech] = 'Paint';
emAbbrevArray [emMechTech]  = 'Mech';
emAbbrevArray [emFrameTech] = 'Frame';
emAbbrevArray [emCSBTech]   = 'CS/B';

var emBaseArray = Array ();
emBaseArray [emBodyTech]    = 'body';
emBaseArray [emPaintTech]   = 'paint';
emBaseArray [emMechTech]    = 'mechanical';
emBaseArray [emFrameTech]   = 'frame';
emBaseArray [emCSBTech]     = 'csb';

// Status notification e-mail address source constants
var snFaxNumber      = -2;
var snEmailAddress   = -1;
var snEmployeeList   = 0;
var snInsCoList      = 1;
var snOtherList      = 2;
var snPagerList      = 3;
var snPrinterList    = 3;

var snNameArray = Array ();
snNameArray [snFaxNumber]    = 'Fax Recipient';
snNameArray [snEmailAddress] = 'Email Recipient';

var snLabelArray = Array ();
snLabelArray [snFaxNumber]    = 'Fax Number:';
snLabelArray [snEmailAddress] = 'Email Address:';

// Login Method Constants
var lmAsCurrentUser = 1;
var lmAsTechnician  = 2;
var lmAsEstimator   = 3;
   
// Report "Select" dialog types
var sdTechnician = 1;
var sdEstimator  = 2;
var sdInsCompany = 3;
var sdProdStatus = 4;
var sdDepartment = 5;  
var sdNames = new Array ();
sdNames [sdTechnician] = 'Technicians';
sdNames [sdEstimator]  = 'Estimators';
sdNames [sdInsCompany] = 'Insurance Companies';
sdNames [sdProdStatus] = 'Production Status Codes';
sdNames [sdDepartment] = 'Departments';

    
/*
**  newWindow (URL, ID, width, height, scrollBars, allowResize, showModal)
**
**  Opens a new browser window with specified attributes.
*/
function newWindow (URL, ID, width, height, scrollBars, allowResize, showModal,
                    yOffset, oArguments, bShowModeless) {
  if (yOffset == null)
    yOffset = 0;                   
  var   leftPos = (screen.availWidth - width) / 2;
  var   topPos = ((screen.availHeight - height) / 2) + yOffset;
  if (!showModal) {
    return window.open (URL,
                 ID,
                 'width=' + width + ',' +
                 'height=' + height + ',' +
                 'scrollbars=' + scrollBars + ',' +
                 'menubar=0,toolbar=0,' +
                 'resizable=' + allowResize +
                 ',left=' + leftPos +
                 ',top=' + topPos);
  }
  else {
    var oMyObject = new Object ();
    oMyObject.oParent = self;
    oMyObject.oArguments = oArguments;
    if (bShowModeless)
      return window.showModelessDialog (URL, oMyObject /* was "self" */,
                                        'dialogWidth:' + width + 'px;' +
                                        'dialogHeight:' + height + 'px;' +
                                        'dialogLeft:' + leftPos + 'px;' +
                                        'dialogTop:' + topPos + 'px;' +
                                        'resizable: ' + allowResize + ';' +
                                        'scroll:' + scrollBars + ';' +
                                        'center: yes;' +
                                        'status: no;' +
                                        'help: no;');
    else                                     
      return window.showModalDialog (URL, oMyObject /* was "self" */,
                                     'dialogWidth:' + width + 'px;' +
                                     'dialogHeight:' + height + 'px;' +
                                     'dialogLeft:' + leftPos + 'px;' +
                                     'dialogTop:' + topPos + 'px;' +
                                     'resizable: ' + allowResize + ';' +
                                     'scroll:' + scrollBars + ';' +
                                     'center: yes;' +
                                     'status: no;' +
                                     'help: no;');
  }                                 
}


// Opens a modal, touchscreen-friendly dialog box
function openModalDialogBox (sDialogCaption, iDialogTypeID_int,
                             iDialogWidth_int, iDialogHeight_int,
                             bAdminDialogBox) {
    
  // Initialize
  var oResult = new Object ();
  oResult.iResult_int = drNone;
  sWindowTitle = dtTitles [iDialogTypeID_int];
  sDialogCaption = escape (sDialogCaption);
    
  // Open the window 
  if (bAdminDialogBox == null)
    bAdminDialogBox = false;
  if (bAdminDialogBox) {
    if (iDialogWidth_int == null) {
      if (iDialogTypeID_int == dtYesNoCancelConfirmation)    
        iDialogWidth_int = 320;
      else
        iDialogWidth_int = 290;
    }
    if (iDialogHeight_int == null)
      iDialogHeight_int = 150;
  }
  else {
    if (iDialogWidth_int == null)
      iDialogWidth_int = 330;
    if (iDialogHeight_int == null)
      iDialogHeight_int = 180;
  }
  if (bAdminDialogBox)
    newWindow ('../modal_container.php?page=administrator/dialogbox.php&title=' + sWindowTitle + '&caption=' + sDialogCaption + '&dialogtype_id_int=' + iDialogTypeID_int,
               'MODALCONFIRMBOX', iDialogWidth_int, iDialogHeight_int, 0, 0, 1, 0, oResult);
  else
    newWindow ('../modal_container.php?page=terminal/dialogbox.php&title=' + sWindowTitle + '&caption=' + sDialogCaption + '&dialogtype_id_int=' + iDialogTypeID_int,
               'MODALCONFIRMBOX', iDialogWidth_int, iDialogHeight_int, 0, 0, 1, 0, oResult);
                
  // Return results
  return oResult.iResult_int;
}  


/*
**  Rounds a number to a given number of decimal places.
*/
function round (number, digits) { 
  digits = (!digits ? 2 : digits); 
  return Math.round (number * Math.pow (10, digits)) / Math.pow (10, digits); 
}   

   
/*
**  Formats a number in dollar format (xx.xx)
*/
function cent (amount) {
  amount = round (amount, 2);
  amount -= 0; 
  return (amount == Math.floor (amount)) ? amount + '.00' : ((amount * 10 == Math.floor (amount * 10)) ? amount + '0' : amount); 
} 


/*
**  Formats the specified text field in dollar format (xx.xx)
*/
function formatDollarField (oField) {
  oField.value = cent (oField.value);
}
 
 
/*
**  Formats the specified text field to the specified number of decimal places.
*/
function formatFloatField (oField, iDigits_int) {
  var sDigitStr = '';
  for (var i = 1; i <= iDigits_int; i ++)
    sDigitStr += '0';
  var sDigitStr2 = '';
  for (var i = 1; i < iDigits_int; i ++)
    sDigitStr2 += '0';
  oField.value = parseFloat (oField.value);
  if (!isNaN (oField.value)) {
    amount = round (oField.value, iDigits_int);
  }
  else
    amount = 0;
  amount -= 0; 
  oField.value = (amount == Math.floor (amount)) ? amount + '.' + sDigitStr : ((amount * 10 == Math.floor (amount * 10)) ? amount + sDigitStr2 : amount); 
  //  oField.value = round (oField.value, iDigits_int);
}
 
 
/*
**  Sets the visibility of an object
*/
function setVis (e, state) {
  e.style.visibility = state;  
}


/*
**  This function will not return until (at least)
**  the specified number of milliseconds have passed.
*/
function pause (numberMillis) {
  var now = new Date ();
  var exitTime = now.getTime () + numberMillis;
  while (true) {
    now = new Date ();
    if (now.getTime () > exitTime)
      return;
  }
}


/*
**  Returns 1 or 0 depending on the state of the specified checkbox.
*/
function getCheckBoxIntState (theCB) {
  if (theCB.checked)
    return 1;
  else
    return 0;
}


/*
**  Toggles the state of the specified checkbox.
*/
function toggleCheckBox (theCB, iIndex_int) {
  if (iIndex_int != null) {
    theCB [iIndex_int].click ();
  }
  else  
    theCB.click ();
}


/*
**  Scans through the specified radio button group and returns
**  the value of the currently selected item.
*/
function getSelectedRadioValue (theRBGroup) {
  for (var i = 0; i < theRBGroup.length; i++) {
    if (theRBGroup [i].checked) {
      return theRBGroup [i].value;
      break;
    }
  }
}


function showStr (sTheStr) {
  // Find the first & last printable characters
  for (var lcv = 0; lcv < sTheStr.length; lcv ++)
    alert (sTheStr.charAt (lcv) + ' (' + sTheStr.charCodeAt (lcv) + ')');
}


/*
**  Trims the specified string by removing leading & trailing non-printable characters.
*/
function trim (sTheStr) {
  var iFirstCharIdx_int = 0;
  var iLastCharIdx_int = 0;

  // Find the first & last printable characters
  for (var lcv = 0; lcv < sTheStr.length; lcv ++) {
    if ((sTheStr.charCodeAt (lcv) != 32) && (sTheStr.charCodeAt (lcv) != 160)) {
      iFirstCharIdx_int = lcv;
      break;
    }
  }
  for (var lcv = sTheStr.length - 1; lcv >= 0 ; lcv --) {
    if ((sTheStr.charCodeAt (lcv) != 32) && (sTheStr.charCodeAt (lcv) != 160)) {
      iLastCharIdx_int = lcv + 1;
      break;
    }
  }
  
  // Return the trimmed part of the string
  return (sTheStr.substring (iFirstCharIdx_int, iLastCharIdx_int));
}


/*
**  Returns the # of milliseconds since Jan 01 1970.  Used for UID's
*/
function getCurrentDateTimeMS () {
  var time_ms = new Date();
  return 'NEWWIN_' + time_ms.getTime();
}


/*
**  Opens a user info window
*/
function openUserInfo (sid, iLoginID_int, sEditMode) {
  newWindow ('./userdetails.php?sid=' + sid + '&login_id_int=' + iLoginID_int +
             '&mode=' + sEditMode,
             'USERWIN0001', 520, 340, 0, 0, 0);
}


/*
**  Adds a new method called "trim" the JavaScript's String object.
**  s.trim () will strip the leading and trailing whitespaces from the string.
*/
String.prototype.trim = function () {
  return this.replace (/^\s*(\b.*\b|)\s*$/, "$1");
}


/*
**  Returns TRUE if the specified email address is valid.
*/
function isValidEmail (sEmailAddress, bBlankIsValid) {
  if (bBlankIsValid == null)
    bBlankIsValid = false;
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return (re.test (sEmailAddress)) || (bBlankIsValid && sEmailAddress == '');
}


/*
**  Returns TRUE if the specified phone number is valid.
*/
function isValidPhoneNumber (sPhoneNumber) {
  var re = /(\(?\d{3}\)?)?(\-| )?\d{3}(\-| )?\d{4}/g;
  return re.test (sPhoneNumber);
}


/*
**  Sets the statusbar text at the bottom of the current window.
*/
function setStatus (sStatusValue) {
  window.status = sStatusValue;
  return true;
}


/*
**  Clears the statusbar text at the bottom of the current window.
*/
function clearStatus () {
  window.status = '';
  return true;
}


/*
**  Selects an item in a SELECT object given the OPTION's value.
*/
function selectItemByValue (oSelObj, sValue) {
  for (var i = 0; i < oSelObj.options.length; i ++) {
    if (oSelObj.options [i].value == sValue) {
      oSelObj.selectedIndex = i;
      break;
    }
  }
}


/*
**  Selects an item in a SELECT object given the OPTION's text.
*/
function selectItemByText (oSelObj, sText) {
  for (var i = 0; i < oSelObj.options.length; i ++) {
    if (oSelObj.options [i].text == sText) {
      oSelObj.selectedIndex = i;
      break;
    }
  }
}


/*
**  Returns the # of selected items in a given SELECT object.
*/
function getSelectedCount (oSelObj) {
  iSelCount = 0;
  if (oSelObj != null) {
    for (var i = 0; i < oSelObj.options.length; i ++) {
      if (oSelObj.options [i].selected) {
        iSelCount ++;
      }
    }
  }
  return iSelCount;
}


/*
**  Returns the ID of the currently selected item in a given SELECT object.
*/
function getSelectedItemID (oSelObj) {
  if (oSelObj.selectedIndex > -1)
    return oSelObj.options [oSelObj.selectedIndex].value;
  else
    return -1;
}


/*
**  Returns the Text of the currently selected item in a given SELECT object.
*/
function getSelectedItemText (oSelObj) {
  return oSelObj.options [oSelObj.selectedIndex].text;
}


/*
**  Returns the index of the specified item in a given SELECT object,
**  given its VALUE.
**
**  Returns -1 if an item was not found with the specified value.
*/
function getSelectItemIndexByValue (oSelObj, sItemValue) {

  // Locate the index
  iIndex_int = -1;
  for (var i = 0; i < (oSelObj.options.length); i ++) {
    if (oSelObj.options [i].value == sItemValue) {
      iIndex_int = i;
      break;
    }
  }
  
  // Return results
  return iIndex_int;  
}


/*
**  Deletes all items in the given SELECT object.
*/
function clearSelectObject (oSelObj) {
  while (oSelObj.length > 0)
    oSelObj.options [0] = null;
}


/*
**  Returns TRUE if the specified string is a valid floating point number.
*/
function isValidFloat (sNumber, bMustBePositive) {
  fResult_flt = parseFloat (sNumber);  
  bGood = !isNaN (fResult_flt);
  if (bMustBePositive)
    bGood = ((bGood) && (fResult_flt > -1));
  return bGood;
}


/*
**  Returns TRUE if the specified string is a valid integer number.
*/
function isValidInt (sNumber, bMustBePositive) {
  iResult_int = parseInt (sNumber);  
  bGood = !isNaN (iResult_int);
  if (bMustBePositive)
    bGood = ((bGood) && (iResult_int > -1));
  return bGood;
}


/*
**  Returns TRUE if the specified string is a valid number.
*/
function isValidNumber (sNumber) {
  var bGood = true;
  for (var i = 0; i < sNumber.length; i ++) {
    iTemp_int = sNumber.charCodeAt (i); 
    if ((iTemp_int < 48) || (iTemp_int > 57)) {
      bGood = false;
      break;
    }  
  }
  return bGood;
}


/*
**  Prints out the ASCII values of each character in the specified string.
**  Used for debugging string problems.
*/
function debugString (sStr) {
  var
     sResult = '';
     
  sResult = 'ASCII Values For "' + sStr + '":\n\r\n\r';   
  for (var i = 0; i < sStr.length; i ++)
    sResult += sStr.charAt (i) + '\t' + sStr.charCodeAt (i) + '\n\r';
    
  alert (sResult); 
}


/*
**  Deletes the specified array element.  Moves all elements after the deleted
**  element and sets the last element to null.
*/
function deleteArrayElement (aArray, iDeleteIndex_int) {
  var bMoved = false;
  for (var i = iDeleteIndex_int; i < aArray.length - 1; i ++) {
    aArray [i] = aArray [i + 1];
    bMoved = true; 
  }
  if (bMoved)
    aArray [aArray.length - 1] = null;
  aArray.length --;    
}


/*
**  Searches an array for the specified value.  Returns the index of the value
**  (0-based), or -1 if not found.
**
**  This function _should_ work regardless of the array and value types, as
**  long as they are the same. 
*/
function arraySearch (aArray, oValue, iElementNum_int) {
  
  // Loop through the array
  var iFoundIndex_int = -1;
  for (var lcv = 0; lcv < aArray.length; lcv ++) {
    if (iElementNum_int != null)
      var oArrayValue = aArray [lcv][iElementNum_int];
    else 
      var oArrayValue = aArray [lcv];
    if (oArrayValue == oValue) {
      iFoundIndex_int = lcv;
      break;
    }
  }
  
  // Return results
  return iFoundIndex_int;  
}


/*
**  Pads the specified string with leading '0's
*/
function padIntStr (sStr, iDesiredLen_int) {
  sStr = sStr.toString ();
  while (sStr.length < iDesiredLen_int)
    sStr = '0' + sStr;
  return sStr; 
}


/*
**  Pads the specified number with leading '0's
*/
function make2DigitNumber (iNumber_int) {
  if (iNumber_int < 10)
    sNumber = '0' + iNumber_int;
  else
    sNumber = iNumber_int.toString ();
    
  return sNumber;
}


/*
**  Returns the position of the 'iCharNum_int'th matching character in the 
**  specified string, starting at the beginning (position 0) of the string.
*/
function getCharPos (cChar, iCharNum_int, sStr) {
  var iCharCounter = 0;
  for (var i = 0; i < sStr.length; i ++) {
    if (sStr.charAt (i) == cChar) {
      iCharCounter ++;
      if (iCharCounter == iCharNum_int) {
        return i;
        break;
      }
    }
  }  
}


/*
**  Returns the position of the next matching character in the specified string,
**  starting at 'iCharNum_int' position.
*/
function getCharPos (cChar, iCharNum_int, sStr) {
  var iCharCounter = 0;
  for (var i = 0; i < sStr.length; i ++) {
    if (sStr.charAt (i) == cChar) {
      iCharCounter ++;
      if (iCharCounter == iCharNum_int) {
        return i;
        break;
      }
    }
  }  
} 


/*
**  Returns the position of the next matching character in the specified string,
**  starting at 'iStartPos_int' position.
*/
function getCharNextPos (cChar, iStartPos_int, sStr) {
  for (var i = iStartPos_int; i < sStr.length; i ++) {
    if (sStr.charAt (i) == cChar) {
      return i;
      break;
    }
  }  
} 


/*
**  Returns the number of tokens in the specified token-delimited string.
*/
function getStrTokenCount (sStr, sToken) {
  var iTokenCount_int = 1;
  for (var i = 0; i < sStr.length; i ++) {
    if (sStr.charAt (i) == sToken) {
      iTokenCount_int ++;
    }
  }
  return iTokenCount_int;  
}


/*
**  Returns the specified token in a token-delimited string.
*/
function getStrToken (sStr, sToken, iTokenNum_int) {
  // Return empty string ('') for invalid token index
  if ((iTokenNum_int == 0) || (iTokenNum_int > getStrTokenCount (sStr, sToken)))
    return '';
    
  // Else get the position of the token we're after
  else {    
    // "this,is,a,tokenized,list"
    var sStr2 = sToken + sStr + sToken;
    
    // ",this,is,a,tokenized,list,"
    var sPos = getCharPos (sToken, iTokenNum_int, sStr2) + 1;
    var ePos = getCharNextPos (sToken, sPos, sStr2);
  
    return sStr2.substring (sPos, ePos);
  }
}


/*
**  Sets the state of a "Glyph" button by first setting the image, then
**  setting the 'disabled' property.
*/
function setGlyphButtonState (sButtonBaseName, bEnabled, sFormName) {
  
  // Get the form name
  if (sFormName == null)
    sFormName = "forms [0]";
  
  // Get the button & image objects from the name
  var oButton = eval ('document.' + sFormName + '.' + sButtonBaseName);
  
  // Exit out if the operation is redundant (button already in state needed)
  if ((oButton == null) ||
      ((bEnabled) && (!oButton.disabled)) ||
      ((!bEnabled) && (oButton.disabled)))
    return 0
  
  // Get other objects  
  var oImage = eval ('document.' + sFormName + '.' + sButtonBaseName + 'Image');
  var oText = eval ('window.' + sButtonBaseName + '_text');
  if (oImage != null) {
    var aImageFileNameParts = oImage.src.split ('.');
    var sImageBaseName = '';
    var sEnabledName = '';
    var sDisabledName = '';
    
    // Parse the image filename to find the base name (without '_disabled');
    for (var i = 0; i < (aImageFileNameParts.length - 1); i ++) {
      if (sImageBaseName != '')
        sImageBaseName = sImageBaseName + '.';
      sImageBaseName = sImageBaseName + aImageFileNameParts [i];
    }
    var iDisabledIdx_int = sImageBaseName.indexOf ('_disabled');
    if (iDisabledIdx_int > -1)
      sImageBaseName = sImageBaseName.substring (0, iDisabledIdx_int);
    sEnabledName = sImageBaseName + '.' + aImageFileNameParts [aImageFileNameParts.length - 1];
    sDisabledName = sImageBaseName + '_disabled.' + aImageFileNameParts [aImageFileNameParts.length - 1];
  }
      
  if (bEnabled) {
    if (oImage != null) {
      oImage.src = sEnabledName;
      oImage.style.cursor = 'hand';
    }
    oText.style.cursor = 'hand';
    oButton.style.cursor = 'hand';
  }
  else {
  
    // Before disabling, set the button class back to normal
    oButton.className = oButton.orig_class; 
    
    if (oImage != null) {
      oImage.src = sDisabledName;
      oImage.style.cursor = 'default';
    }
    oText.style.cursor = 'default';
    oButton.style.cursor = 'default';
  }
  oButton.disabled = (!bEnabled);
}


/*
**  Opens a calendar dialog.  The specified field is used to read/write
**  for the initial and selected date/time.  Returns TRUE if a date/time was
**  selected, FALSE otherwise.
*/
function showCalendarPopup (sFormName, sFieldName, bShowTime) {

  // Initialize
  var oResult = new Object ();
  oResult.bResult = false;
  var sInitialValue = eval (sFormName + '.' + sFieldName + '.value');
  if (sInitialValue == 'Immediately')
    sInitialValue = '';
  if (bShowTime == null)
    bShowTime = true;
  if (bShowTime)
    iShowTime_int = 1;
  else
    iShowTime_int = 0;
 
  // Open the popup
  newWindow ('../modal_container.php?page=administrator/calendar_popup.php&title=Calendar&datetime=' +
             sInitialValue +
             '&formname=' + sFormName +
             '&fieldname=' + sFieldName +
             '&showtime=' + iShowTime_int,
             'CALENDARPOPUP', 340, 295 + (60 * iShowTime_int), 0, 0, 1, 0, oResult);
            
  // Return results
  return (oResult.bResult);
}

 
/*
**  Returns the current date/time (now) as a string
*/
function getNow () {
 
  var oNow = new Date ();
 
  var iHour_int = oNow.getHours ();
  var sAMPM = 'AM';
  if (iHour_int == 0)
    iHour_int = 12;
  if (iHour_int >= 12) {
    if (iHour_int > 12) 
      iHour_int -= 12;
    sAMPM = 'PM';
  }
  sMonth = (oNow.getMonth () + 1);        
  if (sMonth < 10)
    sMonth = '0' + sMonth;
  sMinute = oNow.getMinutes ();
  if (sMinute < 10)
    sMinute = '0' + sMinute;

  return (sMonth + '/' +
          oNow.getDate () + '/' +
          oNow.getYear () + ' ' + 
          iHour_int + ':' +
          sMinute + ' ' +
          sAMPM);             
}


/*
**  Sets the state of both a label and a field, at the same time.
*/
function setLabelAndFieldState (oForm, sFieldName, bEnabled, bClearField,
                                iLabelCount_int) {

  if (iLabelCount_int == null) {
    oField = eval ('oForm.' + sFieldName);
    oLabel = eval (sFieldName + '_label');
    bLabelIsLink = (eval ('oLabel.href'));

    if (bClearField)
      oField.value = '';
  
    if (bEnabled) {
      oField.disabled = false;
      oLabel.disabled = false;
      if (bLabelIsLink)
        oLabel.style.cursor = 'hand';
      else
        oLabel.style.cursor = 'default';
    }
    else {
      oField.disabled = true;
      oLabel.disabled = true;
      oLabel.style.cursor = 'default';
    }
  }
  else {
    oField = eval ('oForm.' + sFieldName);
    if (bClearField)
      oField.value = '';
    oField.disabled = (!bEnabled);

    for (var lcv = 1; lcv <= iLabelCount_int; lcv ++) {
      oLabel = eval (sFieldName + '_label' + lcv);
      bLabelIsLink = (eval ('oLabel.href'));
  
      if (bEnabled) {
        oLabel.disabled = false;
        if (bLabelIsLink)
          oLabel.style.cursor = 'hand';
        else
          oLabel.style.cursor = 'default';
      }
      else {
        oLabel.disabled = true;
        oLabel.style.cursor = 'default';
      }
    }
  } 
}


function fixNumericField (oField, iMinValue_int) {

  var bBelowMinValue = true;

  if (isValidNumber (oField.value)) {
    var iTemp_int = parseInt (oField.value);
    if (iTemp_int >= iMinValue_int)
      bBelowMinValue = false;
  }
  
  if (bBelowMinValue)
    oField.value = iMinValue_int;
}


/*
**  Inserts text at the cursor position in a TEXT or TEXTAREA form field.
*/
function insertAtCursor (myField, myValue) {

  // IE support
  if (document.selection) {
    myField.focus ();
   sel = document.selection.createRange();
   sel.text = myValue;
  }
  
  // MOZILLA support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring (0, startPos) + myValue +
                    myField.value.substring (endPos, myField.value.length);
 }
 
 // Other browsers -- simply append the value to the end.
 else
   myField.value += myValue;
}


/*
**  Formats a URL field in the format of "http://website"
*/
function fixURLField (oField) {

  // Initialize
  oField.value = oField.value.trim ();
  sUpperCaseValue = oField.value.toUpperCase ();
 
  if ((sUpperCaseValue.substring (0, 7) != "HTTP://") &&
      (sUpperCaseValue.substring (0, 8) != "HTTPS://"))
    oField.value = 'http://' + oField.value;
    
} 


/*
**  Opens the "Help" dialog.
*/
function doShowHelp (iTopicID_int) {

  // Initialize
  if (iTopicID_int == null)
    iTopicID_int = -1;

  // Open the dialog
  newWindow ('../modal_container.php?page=administrator/help_dialog.php' +
//             '&sid=' + sSID +
             '&title=SmartShop RPS Help System' +
             '&topic_id_int=' + iTopicID_int,
             'HELPDIALOG', 700, 555, 0, 1, 1, 0, null, 1);
}


/*
**  Called when the user hits "F1" anywhere in the app.
*/
function doHelpRequest (iTargetHelpTopicID_int) {
  window.event.returnValue = false;                
  if (window.event.srcElement.helpcontext)
    var iHelpTopicID_int = window.event.srcElement.helpcontext;
  else 
    var iHelpTopicID_int = -1;
  if (iTargetHelpTopicID_int != null)
    iHelpTopicID_int = iTargetHelpTopicID_int;
  doShowHelp (iHelpTopicID_int);
}


/*
**  Opens the "Onscreen Report Viewer" dialog.
*/
/*
function doOpenReportViewer (iReportTypeID_int, iFileCollectionID_int, sSID,
                             oTargetFrame, sAltURL) {

  // Initialize
  if (sAltURL == null)
    sAltURL = '../modal_container.php?page=administrator/reportviewer_dialog.php';
  sURL = sAltURL +
         '&sid=' + sSID +
         '&title=Onscreen Report Viewer' +
         '&reporttype_id_int=' + iReportTypeID_int +
         '&filecollection_id_int=' + iFileCollectionID_int;                             
                             
  // Open the dialog if no target frame specified
  if (oTargetFrame == null)
    newWindow (sURL, 'REPORTVIEWERDIALOG', 850, 644, 0, 1, 1, 0, null, 1);
  else {
    oTargetFrame.location.href = sURL;
  }               
}
*/


/*
**  Opens the "Onscreen Report Viewer" dialog.
*/
function doOpenReportViewerDialog (sFileCollectionGUID, sSID) {

  // Initialize
  var sAltURL = '../modal_container.php?page=administrator/reportviewer_dialog.php';
  var sURL = sAltURL +
             '&sid=' + sSID +
             '&title=Onscreen Report Viewer' +
             '&filecollection_guid=' + sFileCollectionGUID;                             
                             
  // Open the dialog
  newWindow (sURL, 'REPORTVIEWERDIALOG', 850, 644, 0, 1, 1, 0, null, 1);
}


/*
**  Opens the onscreen report generator so the user can view their reports.
*/
function doShowReportViewer (sFileCollectionGUID, sSID) {
                            
  // Open the report viewer to display the report 
  doOpenReportViewerDialog (sFileCollectionGUID, sSID);
                           
}  

