var popupListDiv = new Array();
var popup_id, popup_rootdir, popup_table, popup_keyfield, popup_displayfield, popup_updatefields;

function clearPopupListControl(p_id) {
  document.getElementById(p_id).value = '';
  popup_control = document.getElementById(p_id + '_display');
  popup_control.value = '';
  if (popup_control.onchange != undefined) popup_control.onchange();
}

function getPopupList(p_id, p_rootdir, p_table, p_keyfield, p_displayfield, p_updatefields) {
  var div1, div2, div3, iframe, button;
  popup_id = p_id;
  popup_rootdir = p_rootdir;
  popup_table = p_table;
  popup_keyfield = p_keyfield;
  popup_displayfield = p_displayfield;
  popup_updatefields = p_updatefields;

  div1 = document.createElement("DIV");
  div1.className = 'popup_list_category';

  div2 = document.createElement("DIV");
  div2.className = 'popup_list';
  div2.id = 'popup_list';
  div2.style.width = '700px';
  div2.style.height = 'auto';
  div2.style.paddingBottom = '20px';
  div2.style.overflowX = 'auto';
  div2.style.overflowY = 'visible';
  div1.appendChild(div2);

  div2 = document.createElement("DIV");
  div2.className = 'popup_list_content';
  div2.appendChild(div1);

  div1 = document.createElement("DIV");
  div1.className = 'popup_right_border';
  div1.appendChild(div2);

  div2 = document.createElement("DIV");
  div2.className = 'popup_buttons';

  button = document.createElement("INPUT");
  button.className = 'button_cancel';
  button.type = 'button';
  button.value = 'Cancel';
  button.onclick = function() { closePopupList() }
  div2.appendChild(button);

  div1.appendChild(div2);

  div2 = document.createElement("DIV");
  div2.className = 'popup_left_border';
  div2.appendChild(div1);

  div1 = document.createElement("DIV");
  div1.className = 'popup_container2';

  div3 = document.createElement("DIV");
  div3.className = 'popup_top_left';
  div1.appendChild(div3);

  div3 = document.createElement("DIV");
  div3.className = 'popup_top_center';
  div1.appendChild(div3);

  div3 = document.createElement("DIV");
  div3.className = 'popup_top_right';
  div1.appendChild(div3);

  div1.appendChild(div2);

  div3 = document.createElement("DIV");
  div3.className = 'popup_list_bottom';

  div2 = document.createElement("DIV");
  div2.className = 'popup_bottom_left';
  div3.appendChild(div2);

  div2 = document.createElement("DIV");
  div2.className = 'popup_bottom_center';
  div3.appendChild(div2);

  div2 = document.createElement("DIV");
  div2.className = 'popup_bottom_right';
  div3.appendChild(div2);

  div1.appendChild(div3);

  var yScroll;

  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
  }

  div2 = document.createElement("DIV");
  div2.className = 'popup_background';
  div2.style.top = yScroll + 'px';
  popupListDiv[0] = document.body.appendChild(div2);

  div2 = document.createElement("DIV");
  div2.className = 'popup_container';
  div2.style.top = yScroll + 'px';
  div2.align = 'center';

  div2.appendChild(div1);

  div1 = document.createElement("DIV");
  //div1.className = 'popup_top';
  div2.insertBefore(div1, div2.firstChild);

  div1 = document.createElement("DIV");
  //div1.className = 'popup_bottom';
  div2.appendChild(div1);

  popupListDiv[1] = document.body.appendChild(div2);

  document.body.style.width = '100%';
  document.body.style.height = '100%';
  document.body.style.overflow = 'hidden';

  searchCombo('hidden');

  loadPopupListFirst('');

}

function loadPopupListFirst(p_cgiparams) {
  var popup_list = document.getElementById('popup_list');
  popup_list.innerHTML = '<div class="popup_loading"><img src="' + popup_rootdir + '_framework/images/popuplist_loading.gif" width="32" height="32" alt="Loading..." border="0" /></div>';
  if (p_cgiparams.length > 0) p_cgiparams = '?' + p_cgiparams
  AjaxRequest.post({
    'url'        : popup_rootdir + '_framework/provider/popuplist/' + p_cgiparams,
    'tablename'  : popup_table,
    'onSuccess'  : function(req) { processXMLList(req); },
    'onError'    : function() {}
  });
}

function loadPopupList() {
  var popup_list = document.getElementById('popup_list');
  popup_list.firstChild.action = popup_rootdir + '_framework/provider/popuplist/?tablename=' + popup_table;
  AjaxRequest.submit(
    popup_list.firstChild,
    {
      'onSuccess'  : function(req) { processXMLList(req); },
      'onError'    : function() {}
    }
  );
  popup_list.innerHTML = '<div class="popup_loading"><img src="' + popup_rootdir + '_framework/images/popuplist_loading.gif" width="32" height="32" alt="Loading..." border="0" /></div>';
}

function popuplistFilterSubmitOnEnter(e,form) {
  var key=e.keyCode || e.which;
  if (key==13){
    form_submit_popuplist(form, 'filter', 1);
    return false;
  } else {
    return true;
  }
}

function form_submit_popuplist(p_this, p_name, p_value) {
  var ha = document.getElementById('popup_list_handleaction');
  ha.name = ha.name.substr(0,ha.name.length-8)+'['+p_name+']';
  ha.value = p_value;
  if (p_name == 'filter') {
    ha = document.getElementById('tableformpager_popup_list_pager');
    if (ha != undefined) ha.value = 1;
  }
  form_submit(p_this);
}

function processXMLList(req) {
  var popup_list = document.getElementById('popup_list');
  if (req.responseText != null) {
    //req.responseText = req.responseText.replace(/<a href="[^"\?]*\?*([^"]*)"[^>]*>/gi, "<a href=\"#\" onclick=\"loadPopupList('$1');return false;\" >");
    popup_list.innerHTML = req.responseText;
  }
  var popup_window = popup_list.parentNode.parentNode.parentNode.parentNode.parentNode;
  popup_window.style.marginTop = ((popup_window.parentNode.clientHeight - popup_window.offsetHeight) / 2) + 'px';
}

function selectPopupList(p_tr) {
  var popup_list = document.getElementById('popup_list');
  var cols = popup_list.getElementsByTagName("COL");
  var tds = p_tr.getElementsByTagName("TD");
  var popup_control, keyfield_idx, displayfield_idx, source, dest, source_idx;
  for (var i=0;i<cols.length;i++) {
    if (cols[i].className == popup_keyfield) keyfield_idx = i;
    if (popup_displayfield != null
        && cols[i].className == popup_displayfield) displayfield_idx = i;
  }
  popup_control = document.getElementById(popup_id);
  popup_control.value = tds[keyfield_idx].innerHTML;
  if (popup_displayfield != null) {
    popup_control = document.getElementById(popup_id + '_display');
    popup_control.value = tds[displayfield_idx].innerHTML;
  }
  if (popup_control.onchange != undefined) popup_control.onchange();
  if (popup_control.onblur != undefined) popup_control.onblur();

  if (popup_updatefields != undefined) {
    for (var j=0;j<popup_updatefields.length;j=j+2) {
      source = popup_updatefields[j];
      dest   = popup_updatefields[j+1];
      for (var i=0;i<cols.length;i++) {
        if (cols[i].className == source) {
          source_idx = i;
          break;
        }
      }
      popup_control = document.getElementById( dest );
      if (popup_control != null) {
        popup_control.value = tds[source_idx].innerHTML;
      }
    }
  }

  closePopupList();
}

function closePopupList() {
  document.body.style.width = 'auto';
  document.body.style.height = 'auto';
  document.body.style.overflow = 'auto';

  searchCombo('visible');

  document.body.removeChild(popupListDiv[0]);
  document.body.removeChild(popupListDiv[1]);
}

function searchCombo(p_visibility) {
  var sAgent = navigator.userAgent.toLowerCase() ;

  if ( /*@cc_on!@*/false && sAgent.indexOf("mac") == -1 )
  {
    var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
    if ( sBrowserVersion  < 7.0 ) {
      var combos = document.getElementsByTagName("SELECT");
      for (var i=0;i<combos.length;i++) {
        combos[i].style.visibility = p_visibility;
      }
    }
  }
}

