// {USD:en,fr,de}
/*==========================================================================
  Name:   scripts_request.js
  Author: dbaron
  Date:   3/00

  This is an include file that define reusable browser side scripts for
  the Request Form.

  Copyright (c) 2000, Peregrine Systems, Inc. 
  All rights reserved.  Use of this file in whole or in part without
  prior written authorization is strictly forbidden.
===========================================================================*/


function CurrencyRecord()
{
  this.strPositivePrefix = CurrencyRecord.arguments[0];
  this.strNegativePrefix = CurrencyRecord.arguments[1];
  this.strPositiveSuffix = CurrencyRecord.arguments[2];
  this.strNegativeSuffix = CurrencyRecord.arguments[3];
  this.strDecimalSeparator = CurrencyRecord.arguments[4];
  this.strGroupingSeparator = CurrencyRecord.arguments[5];
  this.iGroupingSize = CurrencyRecord.arguments[6];
  this.iminDecimalLength = CurrencyRecord.arguments[7];
  this.imaxDecimalLength = CurrencyRecord.arguments[8];
}

var currencies = new Array();
currencies["USD_en_"] = new CurrencyRecord( '$','($','',')','.',',','3','2','2' );
currencies["USD_fr_"] = new CurrencyRecord( '$','($','',')',',',' ','3','2','2' );
currencies["USD_de_"] = new CurrencyRecord( '$','($','',')',',','.','3','2','2' );


/**
 *   Normalize currency to ####0.00 format
 */
function normalizeCurrency(currencyID,userValue)
{
  var key = currencyID+"_"+thisPage.strLanguage+"_"+thisPage.strCountry;
  var currency = currencies[key];
  if( currency == null )
    return userValue;
  var sign = 1;
  var i;
  if( currency.strNegativePrefix.length > 0 && (i = userValue.indexOf(currency.strNegativePrefix)) >= 0 ) {
    sign = -1;
    userValue = userValue.substring(i+currency.strNegativePrefix.length);
  }
  if( currency.strPositivePrefix.length > 0 && (i = userValue.indexOf(currency.strPositivePrefix)) >= 0 )
    userValue = userValue.substring(i+currency.strPositivePrefix.length);

  if( userValue.charAt(0) == '-' ) {
    sign = -1;
    userValue = userValue.substring(1);
  }

  while( currency.strGroupingSeparator.length > 0 && ( i = userValue.indexOf(currency.strGroupingSeparator)) >= 0 )
    userValue = userValue.substring(0,i) + userValue.substring(i+currency.strGroupingSeparator.length);

  if( currency.strDecimalSeparator.length > 0 && (i = userValue.indexOf(currency.strDecimalSeparator)) >= 0 )
    userValue = userValue.substring(0,i)+"."+userValue.substring(i+currency.strDecimalSeparator.length);
  if( currency.strNegativeSuffix.length > 0 && (i = userValue.indexOf(currency.strNegativeSuffix)) >= 0 )
    userValue = userValue.substring(0,i);
  if( currency.strPositiveSuffix.length > 0 && (i = userValue.indexOf(currency.strPositiveSuffix)) >= 0 )
    userValue = userValue.substring(0,i);
  if( sign < 0 )
    userValue = "-"+userValue;
  return userValue;
}

// Format a number as a Money String - $d,ddd.cc
// Default to US Dollar format
function _toMoney( mTotal, currencyType )
{
  var i;
  var strReturn = "";
  var strTotal = mTotal.toString();
  var bIsNegative = ( mTotal < 0 )
  var strPositivePrefix = "$";
  var strNegativePrefix = "($";
  var strPositiveSuffix = "";
  var strNegativeSuffix = ")";
  var strDecimalSeparator = ".";
  var strGroupingSeparator = ",";
  var iGroupingSize = 3;
  var iminDecimalLength = 2;
  var imaxDecimalLength = 2;

  if( currentPage )
  {
    var key = currencyType + "_" + currentPage.strLanguage + "_" + currentPage.strCountry;
    var recCurrency = currencies[key];
    if( recCurrency == null )
    {
      key = currencyType + "_" + currentPage.strLanguage + "_";
      recCurrency = currencies[key];
    }
    if( recCurrency != null )
    {
      strPositivePrefix = recCurrency.strPositivePrefix;
      strNegativePrefix = recCurrency.strNegativePrefix;
      strPositiveSuffix = recCurrency.strPositiveSuffix;
      strNegativeSuffix = recCurrency.strNegativeSuffix;
      strDecimalSeparator = recCurrency.strDecimalSeparator;
      strGroupingSeparator = recCurrency.strGroupingSeparator;
      iGroupingSize = recCurrency.iGroupingSize;
      iminDecimalLength = recCurrency.iminDecimalLength;
      imaxDecimalLength = recCurrency.imaxDecimalLength;
    }
  }

  if( bIsNegative )
    strReturn = strNegativePrefix;
  else
    strReturn = strPositivePrefix;

  i = strTotal.lastIndexOf( "." );
  var integerVal = "";
  var decimalVal = "";
  if( i >= 0 )
  {
    integerVal = strTotal.substring(0,i);
    decimalVal = strTotal.substring(i+1);
  }
  else
    integerVal = strTotal;

  if( iGroupingSize > 0 )
  {
    while( integerVal.length > iGroupingSize )
    {
      var groupLength = integerVal.length % iGroupingSize;
      if( groupLength == 0 )
        groupLength = iGroupingSize;
      strReturn += integerVal.substring(0,groupLength) + strGroupingSeparator;
      integerVal = integerVal.substring(groupLength);
    }
    if( integerVal.length > 0 )
      strReturn += integerVal;
    else
      strReturn += "0";
  }
  else
    strReturn += integerVal;

  if( iminDecimalLength > 0 || decimalVal.length > 0 )
  {
    strReturn += strDecimalSeparator;
    if( decimalVal.length < iminDecimalLength )
    {
      strReturn += decimalVal;
      for( i=decimalVal.length; i < iminDecimalLength; ++i )
        strReturn += "0";
    }
    else if( decimalVal.length> imaxDecimalLength )
      strReturn += decimalVal.substring(0, imaxDecimalLength);
    else
      strReturn += decimalVal;
  }

  if( bIsNegative )
    strReturn += strNegativeSuffix;
  else
    strReturn += strPositiveSuffix;
  return strReturn;
}

// Used to update cart table totals
function updateTotals()
{
  var i;
  var nGrandTotal = 0;
  var currency = "USD";

  // Update each row in the cart
  if ( document.all.Total != null )
  {
    if ( document.all.Total.innerText != null )
    {
        // Deal with a table with a single row
        var strField = "document.all._tf_nCount_ts_" + document.forms[0].nCountItemId.value;
        var countField = eval( strField );
        var nCount = new Number( countField.value );
        var nPrice = new Number( document.all.Price.value );
        nPrice = nPrice * nCount;
        currency = document.all.Price.currency;
        document.all.Total.innerText = _toMoney( nPrice, currency );
        nGrandTotal += nPrice;

    }
    else
    {
      // Deal with a table with multiple rows
      for ( i = 0; i < document.all.Total.length; i++ )
      {
        var strField = "document.all._tf_nCount_ts_" + document.forms[0].nCountItemId[i].value;
        var countField = eval( strField );
        var nCount = new Number( countField.value );
        var nPrice = new Number( document.all.Price[i].value );
        nPrice = nPrice * nCount;
        currency = document.all.Price[i].currency;
        document.all.Total[i].innerText = _toMoney( nPrice, currency );
        nGrandTotal += nPrice;
      }
    }

    // Update grand total
    document.all.mGrandTotal.innerText = _toMoney( nGrandTotal, currency );
  }
}
