/*****************************************************************
* Function:    Ltrim()
* Description: Trims the blanks off the left side of the
*              passed string.
* Parameters:  strImg - The string to trim
* Returns:     strImg - The trimmed string
*****************************************************************/
function Ltrim(strImg)
{
   strChar = strImg.substring(0, 1)
   while (strChar == ' ')
   {
      strImg  = strImg.substring( 1, strImg.length );
      strChar = strImg.substring(0, 1);
   }

   return(strImg);
}

/*****************************************************************
* Function:    Rtrim()
* Description: Trims the blanks off the right side of the
*              passed string.
* Parameters:  strImg - The string to trim
* Returns:     strImg - The trimmed string
*****************************************************************/
function Rtrim(strImg)
{
   strChar = strImg.substring(strImg.length -1, 1);
   while(strChar == ' ')
   {
      strImg  = strImg.substring( 0, strImg.length - 1 );
      strChar = strImg.substring( strImg.length -1, 1 );
   }

   return(strImg);
}

/*****************************************************************
* Function:    Trim()
* Description: Trims the blanks off both sides of the passed
*              field.
* Parameters:  strImg - The string to trim
* Returns:     strImg - The trimmed string
*****************************************************************/
function Trim(strImg)
{
   strImg = Ltrim(Rtrim(strImg));
   return(strImg);
}

/*****************************************************************
* Function:    IsEmpty()
* Description: Deterines if the passed text field is empty.
* Parameters:  txtField - The field to check
*              strFieldName - A string used in the error message.
*                 If left blank.  No error message is displayed
* Returns:     true  - if the field has content in it
*              false - if the field is empty.
*****************************************************************/
function IsEmpty(txtField, strFieldName)
{
   txtField.value = Trim(txtField.value);
   strField = txtField.value;

   if (strField.length == 0)
   {
      if (strFieldName.length > 0)
      {
         alert("Please provide " + strFieldName + ".");
         txtField.focus();
      }

      return (true);
   }
   else
   {
      return (false);
   }
}

/*****************************************************************
* Function:    BrowserVer()
* Description: Returns the browser major version number.
* Parameters:  None
* Returns:     First character of the browser version number.
*****************************************************************/
function BrowserVer()
{
   strVersion = navigator.appVersion;
   return(strVersion.charAt(0));
}

/*****************************************************************
* Function:    IsNumber()
* Description: Deterines if the passed text field is a valid number.
* Parameters:  txtField - The field to check
*              strFieldName - A string used in the error message.
*                 If left blank.  No error message is displayed
* Returns:     true  - if the field is a valid number.
*              false - if the field is not a valid number.
*****************************************************************/
function IsNumber(txtField, strFieldName) 
{
   var GoodChars = "0123456789.-"
   var bDotFound = false;
   var valid     = true;
   var i;

   txtField.value = Trim(txtField.value);
   strField = txtField.value;

   if (strField.length == 0) 
      valid = false;

   if (strField.length == 1)
      if (strField.charAt(0) == '.' || strField.charAt(0) == '-')
         valid = false;
   
   if (strField.length == 2)
      if (strField.charAt(0) == '-' && strField.charAt(1) == '.')
         valid = false;
   
   for (i =0; i <= strField.length -1; i++) 
   {
      if (GoodChars.indexOf(strField.charAt(i)) == -1) 
         valid = false;
      
      if (strField.charAt(i) == '-' && i > 0)
         valid = false;
         
      if (strField.charAt(i) == '.')
      {
         if (bDotFound == true)
            valid = false;
         else
            bDotFound = true;
      }
   }

   if (valid == false)
   {
      if (strFieldName.length > 0)
      {
         alert(strFieldName + " must be a valid number.");
         txtField.focus();
      }
      
      return(false);
   }
   else   
      return(true);
}

/*****************************************************************
* Function:    IsMoney()
* Description: Deterines if the passed text field is a dollar amount.
* Parameters:  txtField - The field to check
*              strFieldName - A string used in the error message.
*                 If left blank.  No error message is displayed
* Returns:     true  - if the field is a valid dollar amound.
*              false - if the field is not a valid dollar amount.
*****************************************************************/
function IsMoney(txtField, strFieldName) 
{
   var GoodChars = "0123456789.-$,"
   var bDotFound = false;
   var valid     = true;
   var i;

   txtField.value = Trim(txtField.value);
   strField = txtField.value;

   if (strField.length == 0) 
      valid = false;

   if (strField.length == 1)
      if (strField.charAt(0) == '.' || strField.charAt(0) == '-')
         valid = false;
   
   if (strField.length == 2)
      if (strField.charAt(0) == '-' && strField.charAt(1) == '.')
         valid = false;
   
   for (i =0; i <= strField.length -1; i++) 
   {
      if (GoodChars.indexOf(strField.charAt(i)) == -1) 
         valid = false;
      
      if (strField.charAt(i) == "$" && i > 0)
         valid = false;

      if (strField.charAt(i) == '-' && i > 0)
         valid = false;
         
      if (strField.charAt(i) == '.')
      {
         if (bDotFound == true)
            valid = false;
         else
            bDotFound = true;
      }
   }

   if (valid == false)
   {
      if (strFieldName.length > 0)
      {
         alert(strFieldName + " must be a dollar amount.");
         txtField.focus();
      }
      
      return(false);
   }
   else   
      return(true);
}

/*****************************************************************
* Function:    IsMoneyPrecise()
* Description: Deterines if the passed text field is a dollar amount,
*				up to specified decimal places.
* Parameters:  txtField - The field to check
*              strFieldName - A string used in the error message.
*                 If left blank.  No error message is displayed
*			   intMaxCents - The number of decimal places allowed
* Returns:     true  - if the field is a valid dollar amount.
*              false - if the field is not a valid dollar amount.
*****************************************************************/
function IsMoneyPrecise(txtField, strFieldName, intMaxCents) 
{
   var GoodChars = "0123456789.-$,"
   var bDotFound = false;
   var valid     = true;
   var i;
   var intDecPlaces = 0;

   txtField.value = Trim(txtField.value);
   strField = txtField.value;

   if (strField.length == 0) 
      valid = false;

   if (strField.length == 1)
      if (strField.charAt(0) == '.' || strField.charAt(0) == '-')
         valid = false;
   
   if (strField.length == 2)
      if (strField.charAt(0) == '-' && strField.charAt(1) == '.')
         valid = false;
   
   for (i =0; i <= strField.length -1; i++) 
   {
      if (GoodChars.indexOf(strField.charAt(i)) == -1) 
         valid = false;
      
      if (strField.charAt(i) == "$" && i > 0)
         valid = false;

      if (strField.charAt(i) == '-' && i > 0)
         valid = false;
         
      if (strField.charAt(i) == '.')
      {
         if (bDotFound == true)
            valid = false;
         else
            bDotFound = true;
      }
      else if (bDotFound == true)
      {
		 intDecPlaces++;
		 if (intDecPlaces > intMaxCents)
			valid = false;
      }

      if (valid == false)
		 break;
   }

   if (valid == false)
   {
      if (strFieldName.length > 0)
      {
         alert(strFieldName + " must be a dollar amount.");
         txtField.focus();
      }
      
      return(false);
   }
   else   
      return(true);
}

/*****************************************************************
* Function:    Round()
* Description: Rounds a number to a set number of decimal places.
* Parameters:  numValue - The number to round
*              numDecPlaces - The number of places to round to.
* Returns:     strRndVal - The rounded number.
*****************************************************************/
function Round(numValue, numDecPlaces)
{
  var bDotFound = false;
  var iDecCnt   = 0;
  var strRndVal = "";
  var strRndPos = "0.";
  var strValue  = "";

  for (i=1 ; i <= numDecPlaces ; i++)
     strRndPos = strRndPos + "0";

  strRndPos = strRndPos + "5";
  numValue = numValue + parseFloat(strRndPos);
  
  strValue = new String(numValue);
  
  for (i =0; i <= strValue.length -1; i++) 
  {
     if (strValue.charAt(i) == '.')
        bDotFound = true;

     if (bDotFound)
     {
        if (iDecCnt <= numDecPlaces)
           iDecCnt++;
        else
           return(parseFloat(strRndVal));
     }

     strRndVal = strRndVal + strValue.charAt(i);
   }
   
   return(parseFloat(strRndVal));
}

/*****************************************************************
* Function:    PowerOf()
* Description: Multiplies one number to the power of another.
* Parameters:  numValue - The number to multiply
*              numPower - The power to multiply by.
* Returns:     strRetVal - The multiplied number.
*****************************************************************/
function PowerOf( numValue, numPower )
{
  numRetVal = numValue;

  for( i=1 ; i < numPower ; i++ )
     numRetVal = numRetVal * numValue;

  return(numRetVal);
}

/*****************************************************************
* Function:    NumToMoney()
* Description: Formats a number as a dollar amount
* Parameters:  numValue - The number to format
* Returns:     strDollars - The formatted amount
*****************************************************************/
function NumToMoney(numValue)
{
   var bDotFound  = false;
   var iCharCnt   = 0;
   var strDollars = "";
   var strValue = "";

   if (Round(numValue,2) == 0 && Round(numValue,2) != numValue) numValue = 0.01;
   numValue = Round(numValue,2);
   
   strValue = new String(numValue);

   for (i =0; i <= strValue.length -1; i++) 
     if (strValue.charAt(i) == '.')
        bDotFound = true;

   if (bDotFound == false)
      strValue  = strValue + ".00"
   else
      strValue  = strValue

   bDotFound = false;

   for( i=strValue.length-1 ; i>=0 ; i--)
   {
      if(bDotFound)
      {
         if (iCharCnt > 2)
         {
            iCharCnt = 1;
            strDollars = "," + strDollars
         }
         else
            iCharCnt++;
      }

      strDollars = strValue.charAt(i) + strDollars;
   
      if (strValue.charAt(i) == '.')
      {
         if (i==strValue.length-2) 
            strDollars = strDollars + "0"
            
         bDotFound = true;
      }
   }
   
   return("$" + strDollars)
}

/*****************************************************************
* Function:    MoneyToNum()
* Description: Converts a dollar amount to an ordinary number.
* Parameters:  numValue - The number to convert
* Returns:     strDollars - The converted amount
*****************************************************************/
function MoneyToNum( strField )
{
   var GoodChars = "0123456789.-";
   var strOutVal = "";

   for (i =0; i <= strField.length -1; i++) 
   {
      if (GoodChars.indexOf(strField.charAt(i)) > -1) 
         strOutVal = strOutVal + strField.charAt(i);
   }
   
   return (strOutVal);
}

/*****************************************************************
* Function:    PopWindow()
* Description: Brings a window to the front of the stack.
* Parameters:  None
* Returns:     None
*****************************************************************/
function PopWindow()
{
   if (navigator.appName == "Microsoft Internet Explorer" &&
       BrowserVer() <= '3') return;

   self.focus(); 
   return;
}
