  /*
  FILE: StoreEngine.js

  PACKAGE: NOF.Ecomm.products
  */
  function NOF_Ecomm_StoreEngine (locale, type) {
    this.__proto__ = NOF_Ecomm_StoreEngine.prototype;
    this.locale = locale;
    this.type = type;
  }

  function NOF_Ecomm_StoreEngine_ProtoBuilder () {
    var member = NOF_Ecomm_StoreEngine.prototype;
    member.settings = null;
    member.resources = null;
    member.validations = null;
    member.settingsPath  = null;
    member.resourcesPath = null;
    member.validationsPath = null;

    member.label  = "";
    member.type   = null;
    member.locale = null;
    member.engineDescription = null;
    member.storeSignupUrl = null;
    member.storeAuthType  = null;
    member.managementUrl  = null;
    member.defaultCurrency = null;
    member.defaultWeightSystem = null;
    member.displayServiceDlgUrl = null;
    member.serviceDialogWidth = "250px";
    member.serviceDialogHeight = "290px";
    member.serviceManager = null;

    var method  = NOF_Ecomm_StoreEngine.prototype;
    method.init = function init () {
      //debugger;
      this.label = this.getResource ('store.Label');
      this.engineDescription = "../../../" + this.getDirectory() + this.getResource ('store.EngineDescription');
      this.displayServiceDlgUrl = "../" + this.getDirectory() + this.getResource ('store.DisplayServiceDlgUrl');
      this.storeSignupUrl = this.getResource ('store.SignupUrl');
      this.storeAuthType = this.getSetting ('store.AuthType');
      this.managementUrl = this.getResource ('store.ManageUrl');

      this.defaultCurrency = this.getResource ('store.currency');
      this.defaultWeightSystem = this.getResource ('store.weightSystem');
      this.defaultCurrencyLabelLocation = this.getResource ('store.currency.defaultLabelLocation');
      this.defaultWeightSystemLabelLocation = this.getResource ('store.weightSystem.defaultLabelLocation');

      this.serviceDialogWidth = this.getSetting ('store.serviceDialogWidth');
      this.serviceDialogHeight = this.getSetting ('store.serviceDialogHeight');
    }

    method.getDirectory = function getDirectory () {
      return "Ecomm/lib/store/engine/" + this.getType() + "/";
    }

    method.getSettingsPath = function getSettingsPath () {
      if (this.settingsPath == null) {
        this.settingsPath = "Ecomm\\lib\\store\\engine\\" + this.getType() + "\\" + "settings";
      }
      return this.settingsPath;
    }

    method.getResourcesPath = function getResourcesPath () {
      if (this.resourcesPath == null) {
        this.resourcesPath = "Ecomm\\lib\\store\\engine\\" + this.getType() + "\\" + "resources";
      }
      return this.resourcesPath;
    }

    method.getSetting = function getSetting (key) {
      try {
        if (this.settings == null) {
          this.settings  = NOF.AppInstance.getResourceFromFile (this.getSettingsPath(), null);
        }
        return this.settings.getProperty(key);
      } catch(e) {}
      return null;
    }

    method.getResource = function getResource (key) {
      try {
        if (this.resources == null) {
          this.resources = NOF.AppInstance.getResourceFromFile (this.getResourcesPath(), this.locale);
        }
        return this.resources.getProperty(key);
      } catch (e) {}
      return null;
    }

    method.getType = function getType () {
      return this.type;
    }

    method.getClassName = function getClassName () {
      return this.getSetting("store.ClassName");
    }

    method.getLabel = function getLabel () {
      return this.label;
    }

    method.getEngineDescription = function getEngineDescription () {
      return this.engineDescription;
    }

    method.getStoreSignupUrl = function getStoreSignupUrl () {
      return this.storeSignupUrl;
    }

    method.getStoreManagementUrl = function getStoreManagementUrl () {
      return this.managementUrl;
    }

    method.getStoreAuthType = function getStoreAuthType () {
      return this.storeAuthType;
    }

    method.getDefaultCurrency = function getDefaultCurrency () {
      return this.defaultCurrency;
    }

    method.getDefaultWeightSystem = function getDefaultWeightSystem () {
      return this.defaultWeightSystem;
    }

    method.getDefaultCurrencyLabelLocation = function getDefaultCurrencyLabelLocation () {
      return this.defaultCurrencyLabelLocation;
    }

    method.getDefaultWeightSystemLabelLocation = function getDefaultWeightSystemLabelLocation () {
      return this.defaultWeightSystemLabelLocation;
    }
    
    method.getCurrencyLabel = function getCurrencyLabel (currency) {      
      var label = this.getSetting ('store.currency.label.'+currency);
      return (label != null) ? label : '';      
    }
    
    method.getWeightSystemLabel = function getWeightSystemLabel (weightSystem) {
      var label = this.getSetting ('store.weightSystem.label.'+weightSystem);      
      return (label != null) ? label : '';
    }
    
    method.getCurrencyCode = function getCurrencyCode (currency) {      
      var label = this.getSetting ('store.currency.code.'+currency);
      return (label != null) ? label : '';      
    }
    
    method.getWeightSystemCode = function getWeightSystemCode (weightSystem) {
      var label = this.getSetting ('store.weightSystem.code.'+weightSystem);      
      return (label != null) ? label : '';
    }

    /* Function setupStore (store)
       Description: setup store from configuration
       Store Attributes:
         store.level        : string
         store.userName     : string
         store.password     : string
         store.email        : string
         store.signupUrl    : string
         store.adminUrl     : string
         store.addToCartUrl : string
         store.viewCartUrl  : string
         store.buyUrl       : string
         store.checkOutUrl  : string
         store.properties   : hashtable
    */
    method.setupStore = function setupStore (store) {
      store.adminUrl = this.getAdminUrl();
      store.buyUrl = this.getBuyUrl();
      store.addToCartUrl= this.getAddToCartUrl ();
      store.checkOutUrl = this.getCheckoutUrl ();
      store.viewCartUrl = this.getViewCartUrl ();
    }

    method.getAdminUrl = function getAdminUrl (product) {
      return this.getSetting ("store.adminUrl");
    }

    method.getAddToCartUrl = function getAddToCartUrl (product) {
      return this.getSetting ("store.addToCartUrl");
    }

    method.getBuyUrl = function getBuyUrl (product) {
      return this.getSetting ("store.buyUrl");
    }

    method.getViewCartUrl = function getViewCartUrl () {
      return this.getSetting ("store.viewCartUrl");
    }

    method.getCheckoutUrl = function getCheckoutUrl () {
      return this.getSetting ("store.checkoutUrl");
    }

    method.isSynchRequired = function isSynchRequired () {
      return (this.getSetting ("store.SynchRequired") == "1") ? true : false;
    }

    method.getValidationsPath = function getValidationsPath () {
      if (this.validationsPath == null) {
        this.validationsPath = "Ecomm\\lib\\store\\engine\\" + this.getType() + "\\" + "validations";
      }
      return this.validationsPath;
    }

    method.getLabelsPath = function getLabelsPath () {
      if (this.labelsPath == null) {
        this.labelsPath = "Ecomm\\lib\\store\\engine\\" + this.getType() + "\\" + "labels";
      }
      return this.labelsPath;
    }

    method.getDisplayServiceDlgUrl = function getDisplayServiceDlgUrl () {
      return this.displayServiceDlgUrl;
    }

    method.getServiceManager = function getServiceManager () {
      return this.serviceManager;
    }

    method.getServiceDialogWidth = function getServiceDialogWidth () {
      return this.serviceDialogWidth;
    }
    method.getServiceDialogHeight = function getServiceDialogHeight () {
      return this.serviceDialogHeight;
    }

    method.showAdmin = function showAdmin( store ){
      var url = this.getStoreManagementUrl();
      if (url != null && url.length > 0) {
        var manageEd = window.open(url, 'ManageStore', 'width=640,height=545,location=1,menubar=1,toolbar=1,resizable=1,scrollbars=1,status=1');
        manageEd.focus();
        return;
      }
    }    
  }

NOF_Ecomm_StoreEngine_ProtoBuilder();

