// Copyright (C) 2007 by Bricsnet
// 53 Green Street, Portsmouth, New Hampshire 03801, U.S.A.
// All rights reserved.
//
// This software is the confidential and proprietary information
// of Bricsnet. ("Confidential Information").  You shall not
// disclose such Confidential Information and shall use it only
// in accordance with the terms of the license agreement you
// entered into with Bricsnet.

var PCGUI = {
  version: '6.6.0',
  
  // This hash contains any translations that are required by Javascript code.
  translationHolder: {},

  // This hash contains any action urls that are required by Javascript code.
  urlHolder: {},
  
  // This hash contains any miscelaneous data required by Javascript view logic.
  otherDataHolder: {},

  // This hash contains icon/image paths rquired by Javascript view logic.
  imagePathHolder: {},
  
  // Retrieves a translation
  t: function (key) {
    return PCGUI.translationHolder[key] || key;
  },
  
  // Sets a translation
  setTranslation: function (key, value) {
    PCGUI.translationHolder[key] = value;
  },
  
  // Retrieves a url
  url: function (key) {
    return PCGUI.urlHolder[key] || undefined;
  },
  
  // Sets a url
  setUrl: function (key, value) {
    PCGUI.urlHolder[key] = value;
  },
  
  // Retrieves 'other data'
  otherData: function (key) {
    return PCGUI.otherDataHolder[key] || undefined;
  },
  
  // Sets 'other data'
  setOtherData: function (key, value) {
    PCGUI.otherDataHolder[key] = value;
  },
  
  // Retrieves an image path
  imagePath: function (key) {
    return PCGUI.imagePathHolder[key] || '';
  },
  
  // Sets an image path.
  setImagePath: function (key, value) {
    PCGUI.imagePathHolder[key] = value;
  },

  // Navigates the current browser window to the supplied url.
  navigateTo: function (url) {
    window.location.href = url;
  },

  isValueDefined: function (value) {
    return !Ext.isEmpty(value);  
  },

  // Creates an icon button configuration hash for a ExtJS toggle button.
  toggleIconButtonConfig: function (label, handler, icon, pressed) {
    var btn = {
      cls: 'x-btn-text-icon',
      icon: icon,
      text: label,
      enabledToggle: true,
      pressed: pressed,
      handler: handler
    };
    
    return btn;
  },

  // Creates a button configuration hash for a ExtJS toggle button.
  toggleButtonConfig: function (label, handler, pressed) {
    var btn = {
      text: label,
      enabledToggle: true,
      pressed: pressed,
      handler: handler
    };

    return btn;
  },

  // Disabled the designated Ext component if it exists.
  disableCmp: function(cmp_id) {
    var cmp = Ext.getCmp(cmp_id);
    
    if (cmp) {
      cmp.disable();
    }
  },

  // Enables the designated Ext component if it exists.
  enableCmp: function(cmp_id) {
    var cmp = Ext.getCmp(cmp_id);
    
    if (cmp) {
      cmp.enable();
    }
  },
  
  // hide the designated Ext component if it exists.
  hideCmp: function(cmp_id) {
    var cmp = Ext.getCmp(cmp_id);
    
    if (cmp) {
      cmp.hide();
      cmp.disable();
    }
  },

  // Enables the designated Ext component if it exists.
  showCmp: function(cmp_id) {
    var cmp = Ext.getCmp(cmp_id);
    
    if (cmp) {
      cmp.enable();
      cmp.show();
    }
  },

  // Creates a button configuration hash for a ExtJS icon button.
  iconButtonConfig: function (label, handler, icon, options) {
    options = options || {};
    
    var btn = {
      cls: 'x-btn-text-icon', 
      icon: icon,
      text: label,
      disabled: options['disabled'] || false,
      hidden: options['hidden'] || false,
      handler: handler                       
    };

    if (options['id']) {
      btn.id = options['id'];
    }

    return btn;
  },

  // Creates a button configuration hash for a ExtJS icon button.
  iconOnlyButtonConfig: function (handler, icon, options) {
    options = options || {};
    
    var btn = {
      cls: 'x-btn-icon', 
      icon: icon,
      disabled: options['disabled'] || false,
      handler: handler                       
    };

    if (options['id']) {
      btn.id = options['id'];
    }

    return btn;
  },

  textButtonConfig: function (label, handler, options) {
    options = options || {};
    
    var btn = {
      cls: 'x-btn-text', 
      text: label,
      disabled: options['disabled'] || false,
      handler: handler                       
    };

    if (options['id']) {
      btn.id = options['id'];
    }

    return btn;
  },

  previousButton: function(handler, options) {
    return PCGUI.textButtonConfig('&laquo; ' + t('button.previous'), handler, options);
  },

  nextButton: function(handler, options) {
    return PCGUI.textButtonConfig(t('button.next') + ' &raquo;', handler, options);
  },

  // Creates a date field with the given fieldId and renders it to the container element specified by fieldContainerId
  renderDateField: function (fieldId, fieldContainerId, value) {
    var dateField = null;

    if (value == null) {
      dateField = new Ext.form.DateField({id: fieldId});
    } else {
      dateField = new Ext.form.DateField({id: fieldId, value: value});
    }

    dateField.render(fieldContainerId);
  },

  // Appends the specified number of spacers to the supplied toolbar item array.
  // The resulting toolbar item array is returned.
  appendToolbarSpacers: function (toolbarItems, spacerCount) {
    for(var i = 0; i < spacerCount; i++) {
      toolbarItems[toolbarItems.length] = ' ';
    }
    
    return toolbarItems;
  },

  removeAllToolbarItems: function (toolbar) {
    var items = toolbar.items;
    
    if (items != null) {
      items.each (function (item, index, length) {item.destroy();})
    }
  },
  
  // Sets the value of all checkboxes matched by extQuery  to the value
  // of the control checkbox.
  setCheckboxesToCheckboxValue: function(extQuery, controlCheckbox, perCheckBoxEventHandler, postLoopHandler) {
    var items = Ext.query(extQuery);
    
    for (var i = 0; i < items.length; i++) {
      if (!items[i].disabled) {
        items[i].checked = controlCheckbox.checked;
      }
      
      if (perCheckBoxEventHandler != null) {
        perCheckBoxEventHandler(items[i])
      }
    }
    
    if (postLoopHandler !=null) {
      postLoopHandler();
    }
  },

  setControlCheckboxValue: function(extQuery, controlCheckbox) {
    var items = Ext.query(extQuery);
    var checkedCount = 0;
    var itemCount = items.length;
    
    for (var i = 0; i < items.length; i++) {
      if (!items[i].disabled && items[i].checked) {
        checkedCount++;
      }
      
      if (items[i].disabled) {
        itemCount--;
      }
    }
    
    if (itemCount == 0) {
      controlCheckbox.checked = false;
    } else if (checkedCount == itemCount) {
      controlCheckbox.checked = true;
    } else if (checkedCount < itemCount) {
      controlCheckbox.checked = false;
    } 
  },

  checkBoxPoweredButtonEventHandler: function (extQuery, buttonId, minSelected) {
    if (minSelected == null)
      minSelected = 1;
      
    var selectedItems = Ext.query(extQuery);
    
    if (selectedItems.length >= minSelected){
      PCGUI.enableCmp(buttonId);
    } else {
      PCGUI.disableCmp(buttonId);
    }
  },

  addAllItemsToToolbar: function(toolbar, buttonItems) {
    for(var i = 0; i < buttonItems.length; i++) {
      toolbar.add(buttonItems[i]);
    }
  },

  // Determines if paging controls should be displayed.
  showPagingControls: function(sectionId) {
    // Show paging if the paging section element exists in the DOM.
    return (document.getElementById(sectionId) != null);
  },

  // Appends the designated paging section to the toolbarItems array
  // and returns the new array. 
  appendPagingSection: function (toolbarItems, sectionId) {
    if (PCGUI.showPagingControls(sectionId)) {
      var pagingSection = document.getElementById(sectionId);
      pagingSection.style.visibility = 'visible';
      toolbarItems[toolbarItems.length] = '->';
      toolbarItems[toolbarItems.length] = '-';
      PCGUI.appendToolbarSpacers(toolbarItems, 2);
      toolbarItems[toolbarItems.length] = pagingSection;
      PCGUI.appendToolbarSpacers(toolbarItems, 2);
    }    
    return toolbarItems;
  },

  appendManageFolder: function(toolbarItems, canManage, manageFolderHandler) {
    if (canManage == 'true') {
      toolbarItems[toolbarItems.length] = '->';
      toolbarItems[toolbarItems.length] = PCGUI.iconOnlyButtonConfig(manageFolderHandler, ip('icons/toolbox.gif'));
    }
    return toolbarItems;
  },

  confirm: function (animateFromEl, title, message, handler) {
    Ext.MessageBox.buttonText.ok = t('button.ok');
    Ext.MessageBox.buttonText.cancel = t('button.cancel');

    Ext.MessageBox.show({
       title: title,
       msg: message,
       buttons: Ext.MessageBox.OKCANCEL,
       icon: Ext.MessageBox.QUESTION,
       fn: handler
    });
  },

  showMessageWindow: function (message) {
    Ext.MessageBox.buttonText.ok = t('button.ok');
    
    Ext.MessageBox.show({
      msg: message,
      buttons: Ext.MessageBox.OK
    });
  },

  showLoadMask: function (message) {
    if (!PCGUI.loadMask) {
      PCGUI.loadMask = new Ext.LoadMask(Ext.getBody(), {msg: message, removeMask: true});
    }
    
    PCGUI.loadMask.message = message;
    PCGUI.loadMask.enable();
    PCGUI.loadMask.show();
  },

  hideLoadMask: function () {
    if (PCGUI.loadMask) {
      PCGUI.loadMask.disable();
      PCGUI.loadMask.hide();
      PCGUI.loadMask.message = '';
    }
  },

  fieldsHaveValues: function (fieldIds) {
    
    for(var i = 0; i < fieldIds.length; i++) {
      if (!PCGUI.fieldHasValue(fieldIds[i])) {
        return false;
      }
    }
    
    return true;
  },

  fieldHasValue: function (fieldId) {
    var field = document.getElementById(fieldId);
    
    if (field) {
      if (field.value != null && field.value != '') {
        return true;
      }
    } 
    
    return false;
  },

  // Sets the value of the visibility style for the supplied element
  // based on the boolean value of visible.
  setElementVisibility: function (el, visible) {
    if (el) {
      if (visible) {
        el.disabled = false;
        el.style.visibility = '';
      } else {
        el.disabled = true;
        el.style.visibility = 'hidden';
      }
    }
  },

  showHelpGuides: function(url) {
    var guideWindow = new Ext.Window({
      id: 'helpGuideWindow',
      modal: true,
      bodyStyle: 'background-color: #FFFFFF; padding: 15px;',
      width: 450,
      height: 280,
      layout: 'fit',
      maximizable: false,
      title: PCGUI.t('title.pcHelpGuides'),
      autoScroll: true,
      autoLoad: {url: url, scripts: true, single: true},
      buttons: [{
        text: PCGUI.t('button.close'),
        handler: function () {
          guideWindow.close();
          guideWindow.destroy();
        }
      }]
    });
    guideWindow.show(this);
  },

  runGA: function () {
  },

  load: function () {
    if((typeof Ext=='undefined') || parseFloat(Ext.version.split(".")[0] + "." + Ext.version.split(".")[1]) < 3.1)
       throw("PCGUI requires Ext JavaScript framework >= 3.1.0. Ext JavaScript framework version " + Ext.version + " is being used.");
  }
};

PCGUI.load();