AdvancedSearch = {

    xeHTMLForm : null, // XML element representing advanced search HTML form element
    DEBUG : false,

    initPage : function() {
      autoTextInputSize(20);
      this.updateCount()},

  updateCount : function() {

    if ($('resultsCount')) {
      // get value of all form elements
      var formElems = d.advSearchForm.elements;
      // build HTMLForm
      var xeHTMLForm = new XMLElement('HTMLForm');
      for (var i=0; i<formElems.length; i++) {
        var value = formElems[i].value;
        var name = formElems[i].name;
        var isRadio = /^radio_/.test(name);
        if (value && value!='' && name && name!='' && ((isRadio && formElems[i].checked && value!='null') || !isRadio)) {
          var xeHTMLElement = new XMLElement('HTMLElement', xeHTMLForm);
          xeHTMLElement.setAttribute('name',formElems[i].name);
          xeHTMLElement.setAttribute('value',value)}}
      // if different to cached HTMLForm
      if (!xeHTMLForm.equals(this.xeHTMLForm)) {
          // build CollectionQuery
          var xeCollectionQuery = new XMLElement('CollectionQuery');
          xeCollectionQuery.setAttribute('collectionName',COLLECTION_NAME);
          var xeGetResultsCount = new XMLElement('GetResultsCount', xeCollectionQuery);
          xeGetResultsCount.appendChild(xeHTMLForm);
          // send async request
          var xdCollectionQuery = ProgramShop.getQueryXMLDoc(xeCollectionQuery);
            var space = location.pathname.split('/')[2]; // "/ws/[space]"
            HTTP.sendRequest(ProgramShop.getQueryHref('CollectionQuery',space), this.displayCount,
              'POST', HTTP.getXMLContentType('UTF-8'), false,
               xeCollectionQuery.toXML(), this.DEBUG);
          // update this.xeHTMLForm
            this.xeHTMLForm = xeHTMLForm}
        // setTimeout
        if ($('resultsCount')) setTimeout('AdvancedSearch.updateCount()',500);}},

  displayCount : function(xmlCollectionResult) {
    // display count
    var xeResult = new ParsedXMLElement(xmlCollectionResult);
    var xeCount = xeResult.getChild('CollectionResultsCount');
    $('btSubmit').disabled = (xeCount && xeCount.getAttribute('value')=='0');
    $('resultsCount').innerHTML = (xeCount) ? xeCount.getAttribute('label') : '';},

  submit : function() {
    Manager.setAgentFocus();
    if (Manager.isStable()) d.advSearchForm.submit();
    else setTimeout('AdvancedSearch.submit()',500);}
}

Fiche = {

  imgIndex : 0,

  showFullSizeImage : function() {
    var src = IMAGES[this.imgIndex].srcLrg;
    if (src) openPopup(src,null,'scrollbars=0,resizable,width='
      + INIT_POPUP_SIZE.w+',height='+INIT_POPUP_SIZE.h);},

  setFSImgLink : function(imgIndex) {
    var img = IMAGES[imgIndex];
    var oImg = $("mainImg");
    if (img && oImg) {
      oImg.style.cursor = (img.srcLrg) ? "pointer" : "";
      oImg.alt = (img.srcLrg) ? LABEL_ZOOM : "";
      var zoomHTML = "";
      if (img.srcLrg) zoomHTML = '<a href="javascript: Fiche.showFullSizeImage()">'
      + '<img src="/rsrc/img/bt_zoom.gif" class="btZoom" alt="'+LABEL_ZOOM+'"></a>';
      $("mainImgZoom").innerHTML = zoomHTML}},

  showImage : function(imgIndex) {
    var img = IMAGES[imgIndex];
    var oImg = $("mainImg");
    if (img && oImg) {
      oImg.src = img.src;
      this.setFSImgLink(imgIndex);
      var fc = $("mainImgName").firstChild;
      if (fc) fc.data = (img.name) ? img.name : '';
      this.imgIndex = imgIndex}}
}


/**
 * Agent procedure callbacks
 */
AgentPCB = {

  onLogin : function() {
    if (this.isInstanceOf('AlphaListAgent') || this.isInstanceOf('LKTableAgent'))
    Manager.openPanel($(this.id),$('typeName_'+this.fieldId).innerHTML);},

  update : function(xeWidgetResult) {

    if (this.isInstanceOf('TextInputAgent')) {
      $(this.id.replace(/^[^_]+/,'input')).value = this.getResultLabel(xeWidgetResult)}

    else if (this.isInstanceOf('AlphaListAgent') || this.isInstanceOf('LKTableAgent')) {
      if (this.fieldType=='FKey' || this.fieldType=='FKeySet') {
        var fkey = this.getResultValue(xeWidgetResult);
        $('input_'+this.fieldRef).value = (fkey) ? fkey.getAttribute('recId') : '';
        $('label_'+this.fieldRef).value = this.getResultLabel(xeWidgetResult)}
      else $(this.id.replace(/^[^_]+/,'input')).value = this.getResultLabel(xeWidgetResult);
      $(this.id).focus()}},

  updatePanel : function(xeWidgetResult) {
    $(this.id.replace(/^[^_]+/,'input')).value = this.getResultLabel(xeWidgetResult);
    $(this.id).focus()}
}


View = {

  rowToHighlight : null,
  highlightedRow : null,

  gotoItem : function(index) {
    location.href = HREF_GOTO_ITEM + index},

  highlightRow : function(tr) {
    this.rowToHighlight = (tr) ? tr : null;},

  refreshHightlightedRow : function() {
    if (this.highlightedRow!=this.rowToHighlight) {
      if (this.highlightedRow!=null)
        this.highlightedRow.className = this.highlightedRow.className.split('_')[0];
      if (this.rowToHighlight!=null)
        this.rowToHighlight.className += '_mouseOver';
      this.highlightedRow = this.rowToHighlight}
    setTimeout("View.refreshHightlightedRow()",50)}
}


ConfClick = {

  ccDivId : 'conf',
  goDivId : 'greyOut',
  ccWidth : 200, // px
  ccOffset : {left:-2, top:-2}, // px, relative to selected object

  display : function(obj,e,fieldName) {
    var docWidth = document.documentElement.scrollWidth;
    // place grey out div
    var goDiv = $(this.goDivId);
    if (!goDiv) {
      goDiv = d.createElement('DIV');
      goDiv.id = this.goDivId;
      goDiv.className = 'greyOut';
      goDiv.onclick = this.hide;
      goDiv.style.width = docWidth + 'px';
      var pageHeight = d.body.scrollHeight;
      if (d.body.clientHeight > pageHeight) pageHeight = d.body.clientHeight;
      if (d.documentElement && d.documentElement.clientHeight > pageHeight)
        pageHeight = d.documentElement.clientHeight;
      goDiv.style.height = pageHeight + 'px';
      d.body.appendChild(goDiv);
      goDiv.innerHTML = '<img src="/rsrc/img/tx.gif" width="'
      + (docWidth-20) + '" height="1"/>';}
    // place conf click div
    var ccDiv = $(this.ccDivId);
    if (!ccDiv) {
      ccDiv = d.createElement('DIV');
      ccDiv.id = this.ccDivId;
      ccDiv.className = 'conf';
      // set DIV style
      ccDiv.style.top = (getAbsPos(obj,'Top') + this.ccOffset.top) + 'px';
      var x = getAbsPos(obj,'Left') + this.ccOffset.left;
      if (x+this.ccWidth>docWidth-16) x = docWidth - 16 - this.ccWidth;
      if (x<10) x = 10;
      ccDiv.style.left = x + 'px';
      ccDiv.style.width = this.ccWidth + 'px';
      d.body.appendChild(ccDiv);
      // add link to ccDiv
      var ccLink = d.createElement('A');
      ccLink.href = obj.href;
      ccLink.tabIndex = '1';
      ccLink.onclick = this.hide;
      ccDiv.appendChild(ccLink);
      ccLink.innerHTML = LABEL_CONF_CLICK.replace(/#1/g,obj.innerHTML).replace(/#2/g,fieldName)}
    cancelBubble(e)},

  hide : function() {
    var ccDiv = $(ConfClick.ccDivId);
    if (ccDiv) d.body.removeChild(ccDiv);
    var goDiv = $(ConfClick.goDivId);
    if (goDiv) d.body.removeChild(goDiv);}
}



