/***
 * Smart(hostname)
 *
 *  DESCRIPTION
 *    Creates an instance of a SMART object.
 *    This constructor takes a mandatory SMART hostname.
 *
 *  SYNOPSIS:  
 *    Smart([SMART hostname])
 *
 *  EXAMPLE:
 *    var _smart = new Smart("111.111.111.111");
 *
 *    NOTE: there is a global SMART_HOST javascript constant created in begin_nav
 *    which you can use to pass to your SMART object:
 *    
 *      eg. var _smart = new Smart(SMART_HOST);
 *
 * INSTANCE METHODS
 *  getInStoreItem(inStoreItemArr)
 *    returns Item data in XML
 *
 *  getInStoreItemsPriceAndName(upc)
 *    returns Item and Name data in XML
 *
 *  getAssociateData(associate)
 *    returns Associate data in XML (grandfather method)
 *
 *  getAssociateDataByBadgeId( badgeid )
 *    returns new associate logon data in XML
 *
 *  getAssociateDataByUserInfo( userid, password, hiredate )
 *    returns new associate logon data in XML
 *    userid and password should be escaped before being passed
 *    as parameters to this method
 *
 *  getTaxFlags(upc)
 *    returns TaxFlags data in XML
 *
 *  getSmartHost()
 *    returns the SMART host
 *
 *  getBaseSmartUrl()
 *    returns base URL used to construct all the SMART cgi requests
 *
 *  getCostUrl()
 *    returns the url queried to get Item cost XML
 *
 *  getNameAndCostUrl()
 *    returns the url queried to get Item name and cost XML
 *
 *  getAssociateDataUrl()
 *    returns the url queried to get Associate data XML
 *
 *  getTaxFlagsUrl()
 *    returns the url queried to get Tag flag data XML
 *
 *  getClassName()
 *    returns class name
 *
 *  toString()
 *    returns string dump of object
 **/
function Smart (hostname) {
  /* -- Begin get SMART hostname -- */
  Log.logBeginTaskScope( "Getting SMART hostname.", this.URI );
  try 
  {
    if (hostname === undefined) 
    {
      throw new Error("Mandatory SMART hostname argument is undefined.");
    }
  }
  catch(e) 
  {
    Log.logErrorScope( "Hostname is undefined.", this.URI, e );
  }
  Log.logCompletedTaskScope( "Getting SMART hostname.", this.URI );
  this.hostname           = hostname;
  /* -- End get SMART hostname -- */

  this.baseSmartUrl       = "http://" + this.hostname + ":4000/scripts/";
  this.costUrl            = this.baseSmartUrl + "get_cost.cgi?onhand=";
  this.nameAndCostUrl     = this.baseSmartUrl + "sgk_upc.cgi?desc=";
  this.assoDataUrl        = this.baseSmartUrl + "sgk_upc.cgi?desc=";
  this.taxFlagsUrl        = this.baseSmartUrl + "sgk_upc.cgi?ge=";
  this.associateLogonUrl  = this.baseSmartUrl + "sgk_associate_login.cgi";

  /* -- Begin get MS XMLDOM -- */
  Log.logBeginTaskScope( "creating Microsoft.XMLDOM ActiveX for SMART.", this.URI );
  try 
  {
    this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  }
  catch (e)
  {
    Log.logErrorScope( "Microsoft.XMLDOM ActiveX object instantiation error.", this.URI, e );
  }
  Log.logCompletedTaskScope( "creating Microsoft.XMLDOM ActiveX for SMART.", this.URI );
  this.xmlDoc.async = "false"; /* makes sure the XML doc is fully loaded before proceeding */
  /* -- End get MS XMLDOM -- */
}
Smart.prototype.CLASS_NAME = "Smart";
Smart.prototype.URI = "/htdocs/include/ds/js/external/Smart.js";

/***
 * Call SMART system to get Item XML data
 **/
function SMgetInStoreItem (inStoreItemsArr)
{
  var _url = this.costUrl;
  var _xmlDoc = this.xmlDoc;
  for (var i=0; i<inStoreItemsArr.length; i++)
  {
    _url += inStoreItemsArr[i] + "|";
  }
  Log.logBeginTaskScope( "getting XML in getInStoreItem(). URL=" + _url, this.URI );
  try 
  {
    // make HTTP request
    _xmlDoc.load(_url);
  }
  catch (e)
  {
    Log.logErrorScope( "Error geting XML in getInStoreItem(). URL=" + _url, this.URI, e );
  }
  Log.logCompletedTaskScope( "getting XML in getInStoreItem(). XML=" + _xmlDoc.xml, this.URI );
  return _xmlDoc;
}
Smart.prototype.getInStoreItem = SMgetInStoreItem;

/***
 * Call SMART system to get in-store price, and name
 **/
function SMgetInStoreItemsPriceAndName (upc)
{
  var _url = this.nameAndCostUrl;
  var _xmlDoc = this.xmlDoc;
  _url += upc;
  Log.logBeginTaskScope( "getting XML in getInStoreItemsPriceAndName(). URL=" + _url, this.URI );
  try
  {
    _xmlDoc.load(_url);
  }
  catch (e)
  {
    Log.logErrorScope( "Error geting XML in getInStoreItemsPriceAndName(). URL=" + _url, this.URI, e );
  }
  Log.logCompletedTaskScope( "getting XML in getInStoreItemsPriceAndName(). XML=" + _xmlDoc.xml, this.URI );
  return _xmlDoc;
}
Smart.prototype.getInStoreItemsPriceAndName = SMgetInStoreItemsPriceAndName;

/***
 * Call SMART system to get associate data
 *
 * Returns XML from SMART query
 **/
function SMgetAssociateData (associate) 
{
  var _url = this.assocDataUrl;
  var _xmlDoc = this.xmlDoc;
  _url += associate;
  Log.logBeginTaskScope( "getting XML in getAssociateData(). URL=" + _url, this.URI );
  try
  {
    _xmlDoc.load(_url);
  }
  catch (e)
  {
    Log.logErrorScope( "Error geting XML in getAssociateData(). URL=" + _url, this.URI, e );
  }
  Log.logCompletedTaskScope( "getting XML in getAssociateData(). XML=" + _xmlDoc.xml, this.URI );
  return _xmlDoc;
}
Smart.prototype.getAssociateData = SMgetAssociateData;

/***
 * Call SMART system to get associate data for
 * associate alternate logon base on badge id
 *
 * Returns XML from SMART query
 **/
function SMgetAssociateDataByBadgeId( badgeid ) {
  var _url = this.associateLogonUrl;
  var _xmlDoc = this.xmlDoc;
  _url += "?badgeid=" + badgeid;
  Log.logBeginTaskScope( "getting XML in getAssociateDataByBadgeId(). URL=" + _url, this.URI );
  try
  {
    _xmlDoc.load(_url);
  }
  catch (e)
  {
    Log.logErrorScope( "Error geting XML in getAssociateByBadgeId(). URL=" + _url, this.URI, e );
  }
  Log.logCompletedTaskScope( "getting XML in getAssociateByBadgeId(). XML=" + _xmlDoc.xml, this.URI );
  return _xmlDoc;
}
Smart.prototype.getAssociateDataByBadgeId = SMgetAssociateDataByBadgeId;

/***
 * Call SMART system to get associate data for
 * associate alternate logon base on user id,
 * password, and hire date
 *
 * Returns XML from SMART query
 **/
function SMgetAssociateDataByUserInfo( userid, password, hiredate ) {
  var _url = this.associateLogonUrl;
  var _xmlDoc = this.xmlDoc;
  _url += "?userid=" + userid;
  _url += "&pwd=" + password;
  _url += "&hiredate=" + hiredate;

  Log.logBeginTaskScope( "getting XML in getAssociateDataByUserInfo(). URL=" + _url, this.URI );
  try
  {
    _xmlDoc.load(_url);
  }
  catch (e)
  {
    Log.logErrorScope( "Error geting XML in getAssociateDataByUserInfo(). URL=" + _url, this.URI, e );
  }
  Log.logCompletedTaskScope( "getting XML in getAssociateDataByUserInfo(). XML=" + _xmlDoc.xml, this.URI );
  return _xmlDoc;
}
Smart.prototype.getAssociateDataByUserInfo = SMgetAssociateDataByUserInfo;

/***
 * Call SMART system to get tax data
 *
 * Returns XML from SMART query
 **/
function SMgetTaxFlags (upc) 
{
  var _url = this.taxFlagsUrl;
  var _xmlDoc = this.xmlDoc;
  _url += upc;
  Log.logBeginTaskScope( "getting XML in getTaxFlags(). URL=" + _url, this.URI );
  try
  {
    _xmlDoc.load(_url);
  }
  catch (e)
  {
    Log.logErrorScope( "Error geting XML in getTaxFlags(). URL=" + _url, this.URI, e );  
  }
  Log.logCompletedTaskScope( "getting XML in getTaxFlags(). XML=" + _xmlDoc.xml, this.URI );
  return _xmlDoc;
}
Smart.prototype.getTaxFlags = SMgetTaxFlags;

function SMgetSmartHost () {
  return this.hostname;
}
Smart.prototype.getSmartHost = SMgetSmartHost;

function SMgetBaseSmartUrl () {
  return this.baseSmartUrl;
}
Smart.prototype.getBaseSmartUrl = SMgetBaseSmartUrl;

function SMgetCostUrl () {
  return this.costUrl;
}
Smart.prototype.getCostUrl = SMgetCostUrl;

function SMgetNameAndCostUrl () {
  return this.nameAndCostUrl;
}
Smart.prototype.getNameAndCostUrl = SMgetNameAndCostUrl;

function SMgetAssociateDataUrl() {
  return this.assoDataUrl;
}
Smart.prototype.getAssociateDataUrl = SMgetAssociateDataUrl;

function SMgetAssociateLogonUrl() {
  return this.associateLogonUrl;
}
Smart.prototype.getAssociateLogonUrl = SMgetAssociateLogonUrl;

function SMgetTaxFlagsUrl() {
  return this.taxFlagsUrl;
}
Smart.prototype.getTaxFlagsUrl = SMgetTaxFlagsUrl;

function SMgetClassName() {
  return this.CLASS_NAME;
}
Smart.prototype.getClassName = SMgetClassName;

function SMtoString() {
  var _out = this.getClassName() + " = { \n";
  _out += "this.hostname = " + this.getSmartHost() + ", \n";
  _out += "this.baseSmartUrl = " + this.getBaseSmartUrl() + ", \n";
  _out += "this.costUrl = " + this.getCostUrl() + ", \n";
  _out += "this.nameAndCostUrl = " + this.getNameAndCostUrl() + ", \n";
  _out += "this.assoDataUrl = " + this.getAssociateDataUrl() + ", \n";
  _out += "this.associateLogonUrl = " + this.getAssociateLogonUrl() + ", \n";
  _out += "this.taxFlagsUrl = " + this.getTaxFlagsUrl() + ", \n";
  _out += "this.xmlDoc = " + this.xmlDoc.xml + ", \n";
  _out += "this.xmlDoc.async = " + this.xmlDoc.async + " \n";
  _out += " }";
  return _out;
}
Smart.prototype.toString = SMtoString;
