<public:component name=odchandler>

<public:method name="maxRowChange"></method>
<public:method name="display"></method>
<public:attach event="ondocumentready" HANDLER="display"/>

<script language="javascript">
var iMAXROWSTOSHOW_c = 1;
var iALL_c = 2;
var iMAXDEFAULTTOTALS_c = 3;
var iNUMROWSDEFAULT_c = 25;

var ptPreview;
var objPT;
var adorsTable;

 
function display() {
  var sHTML, sProgID, sCubeName, sConnectionString, sCommandType, sCommandText;
  var collProgIDTags, collXMLTags, xmldocODC;
  var objHR;


  collProgIDTags = window.document.getElementsByName("ProgID")

  // if there is no progid, bail out.
  if (collProgIDTags.length == 0) { return; }

  sProgID = collProgIDTags[0].content;

  objPT = window.document.createElement("OBJECT")
  objPT.classid = "clsid:0002E55A-0000-0000-C000-000000000046"
  objPT.width = "100%"
  objPT.height = "100%"
  objPT.AutoFit = false;
  objPT.style.margin = "1px"

  objPT.id = "ptPreview";

  xmldocODC = window.document.getElementById("msodc");

  if (xmldocODC == null) { return; }

  element.appendChild(objPT);					// add the PT to the page
  element.style.backgroundColor="buttonface";

  ptPreview = objPT.object; 

  switch (sProgID) {

    case "ODC.Table":
      sCommandType = xmldocODC.documentElement.selectSingleNode("odc:Connection/odc:CommandType").text
      sCommandText = xmldocODC.documentElement.selectSingleNode("odc:Connection/odc:CommandText").text
      sConnectionString = xmldocODC.documentElement.selectSingleNode("odc:Connection/odc:ConnectionString").text

      setTable(sConnectionString, sCommandText, sCommandType); 
      break;
		
    case "ODC.Database":
      sConnectionString = xmldocODC.documentElement.selectSingleNode("odc:Connection/odc:ConnectionString").text
      setDatabase(sConnectionString);
      break;
			
    case "ODC.Cube":
      sCubeName = xmldocODC.documentElement.selectSingleNode("odc:Connection/odc:CommandText").text
      sConnectionString = xmldocODC.documentElement.selectSingleNode("odc:Connection/odc:ConnectionString").text
      setCube(sConnectionString, sCubeName);
      break;
    }

  }

function setTable(sConnectionString, sCommandText, sCommandType) {

  sCommandType = sCommandType.toLowerCase();

  ptPreview.ConnectionString = sConnectionString
  ptPreview.DataSource.MaxRecords = iNUMROWSDEFAULT_c;

  if (sCommandType.indexOf("default") >= 0 || sCommandType.indexOf("sql") >= 0 ) {
    ptPreview.CommandText = sCommandText
    }
  else if (sCommandType.indexOf("table") >= 0) {
    ptPreview.DataMember = scrubTableName(sConnectionString, sCommandText);
    }


  try {
    ptPreview.ActiveView.AutoLayout();
    }
  catch (e) {
    if (e.description.length > 0) {
      alert (e.description);
      }
    }

  // if this is a "table" from an OLAP provider, cubeify.  Otherwise, add the row count dropdown.
  if (sCommandText.indexOf("].[")>=0) {
    showDefaultCubeView();
    }
  else {
    addMaxRowDropdown();
    }

  configurePTPreview();
  }

function scrubTableName(sConnectionString, sDataMember) {
  var sOut;
  var i;
  var sConnectionStringLower = sConnectionString.toLowerCase();

  sOut = sDataMember;

  if (sConnectionStringLower.indexOf("dsn=excel files")>=0 || 
      sConnectionStringLower.indexOf("dsn=ms access database")>=0 ||
      sConnectionStringLower.indexOf("microsoft access")>=0) {
      sOut = replace(sOut, "`", "")  // ODBC providers, like Excel and Access, fail when called via OLE-DB if there 
                                     // are these single quotes.
      }
 
  // customize the connection string for the picky MS ODBC Text Driver
  if (sConnectionStringLower.indexOf("driverid=27")>=0) {

    i = sOut.lastIndexOf("`");

    if (i>=0) {
      i = sOut.lastIndexOf("`", i-1);  

      if (i>=0) {
        sOut = sOut.substring(i, sOut.length);
        sOut = replace(sOut, "`", "\"");
        }
      }
    }

  return sOut;
  }

function setCube(sConnectionString, sCubeName) {
  var i, iNumDefaultTotals

  ptPreview.ActiveView.Titlebar.Visible = false;
 
  ptPreview.ConnectionString = sConnectionString;
  ptPreview.DataMember = sCubeName;

  showDefaultCubeView();
  }

function showDefaultCubeView() {
  ptPreview.DisplayFieldList = true;
  
  // give the PT focus; otherwise, the field list doesn't show until you click on PT.
  objPT.focus();

  with (ptPreview.ActiveView) {
    if (Totals.count < iMAXDEFAULTTOTALS_c) {
      iNumDefaultTotals = Totals.count
      }
    else {
      iNumDefaultTotals = iMAXDEFAULTTOTALS_c
      }

    for (i=0; i<iNumDefaultTotals; i++) {
      DataAxis.InsertTotal(Totals(i))
      }   
    }
  }

function setDatabase(sConnectionString) {
  var adoconn;
  var adors;
 
  configurePTPreview();

  adoconn = new ActiveXObject("ADODB.Connection")

  adoconn.ConnectionString = sConnectionString;
  adoconn.CursorLocation = 3;  // 3 == adUseClient
  adoconn.Properties("Prompt") = 2;

  try {
    adoconn.Open();
    }
  catch (exc) {
    // bail if the user cancels a login
    return; 
    }

  adors = adoconn.OpenSchema(20);

  ptPreview.DataSource = adors;

  with (ptPreview.ActiveView) {
    DataAxis.InsertFieldSet(FieldSets("TABLE_NAME"))
    DataAxis.InsertFieldSet(FieldSets("TABLE_SCHEMA"))
    DataAxis.InsertFieldSet(FieldSets("TABLE_TYPE"))
    DataAxis.InsertFieldSet(FieldSets("DESCRIPTION"))

    // filter to show tables, views, cubes, virtual cubes only
    FieldSets("TABLE_TYPE").AllIncludeExclude = ptPreview.Constants.plAllExclude;
    FieldSets("TABLE_TYPE").Fields(0).IncludedMembers = Array ("TABLE","VIEW","CUBE","VIRTUAL CUBE")
    
    //sort views first to be consistent with Select Tables dialog in XL
    FieldSets("TABLE_TYPE").Fields(0).SortDirection = ptPreview.Constants.plSortDirectionAscending;
    }
  }

function configurePTPreview() {
  with (ptPreview.ActiveView) {
    Titlebar.Visible = false
    RowAxis.Label.Visible = false   
    ColumnAxis.Label.Visible = false   
    FilterAxis.Label.Visible = false
    }
  }

function addMaxRowDropdown() {
  var spanPreDropdown, spanPostDropdown;
  var selDropdown;
  var optTemp;
  var tdDropdownArea;

  // look for the TD that gets inserted via script in the ODC file.
  tdDropdownArea = window.document.getElementById("tdTableDropdown")

  if (tdDropdownArea != null) {

    with (tdDropdownArea.style) {
      fontFamily = "Arial"
      fontSize = "x-small"
      textAlign = "right"
      }
    
    spanPreDropdown = window.document.createElement("SPAN");

    spanPreDropdown.style.fontFamily = getLocalizedFontName();
    spanPreDropdown.style.fontSize = getLocalizedFontSize() + "pt"; 
    spanPreDropdown.innerHTML = replace(getLocalizedString(iMAXROWSTOSHOW_c), " ", "&nbsp;") + "&nbsp;&nbsp;";

    selDropdown = window.document.createElement("SELECT");
  
    // align dropdown with baseline of text
    selDropdown.style.position = "relative"
    selDropdown.style.fontFamily = getLocalizedFontName();
    selDropdown.style.fontSize = getLocalizedFontSize() + "pt"; 
    selDropdown.style.top = "1px"

    tdDropdownArea.appendChild(spanPreDropdown);
    tdDropdownArea.appendChild(selDropdown);

    // add the dropdown items.
    // note: IE5 requires that you add the option obj to a sel obj before you can set innerText.

    optTemp = window.document.createElement("OPTION")
    selDropdown.options.add(optTemp);
    optTemp.innerText = "25";
    optTemp.selected = true

    optTemp = window.document.createElement("OPTION")
    selDropdown.options.add(optTemp);
    optTemp.innerText = "100";

    optTemp = window.document.createElement("OPTION")
    selDropdown.options.add(optTemp);
    optTemp.innerText = "500";

    optTemp = window.document.createElement("OPTION")
    selDropdown.options.add(optTemp);
    optTemp.innerText = "1000";

    optTemp = window.document.createElement("OPTION")
    selDropdown.options.add(optTemp);
    optTemp.innerText = getLocalizedString(iALL_c);

    // trap dropdown change events with this code 
    selDropdown.onchange = maxRowChange;


    }
  }

function maxRowChange() {
  var selDropdown;
  var adors;

  selDropdown = event.srcElement;


  adors = ptPreview.DataSource;
  
  if (selDropdown[0].selected) { adors.MaxRecords = 25; }
  if (selDropdown[1].selected) { adors.MaxRecords = 100; }
  if (selDropdown[2].selected) { adors.MaxRecords = 500; }
  if (selDropdown[3].selected) { adors.MaxRecords = 1000; }

  // set MaxRecords = 0 to show all records
  if (selDropdown[4].selected) { adors.MaxRecords = 0; }

  selDropdown.focus();
  window.document.ptPreview.ActiveData.TopOffset = 0;

  // hack: give IE a timeslice to repaint and get rid of the dropdown area
  window.setTimeout("window.document.ptPreview.Refresh(); ", 400);
  }


function replace(strOrig, strFrom, strTo) {
  var strNew;
  var i;
  var iFromLen;

  strNew = "";

  iFromLen = strFrom.length;

  i = strOrig.indexOf(strFrom)
  j = 0;
  while (i>=0) {
    strNew = strNew + strOrig.substring(j, i) + strTo;
    
    j = i + iFromLen
    i = strOrig.indexOf(strFrom, j)
    }

  i = strOrig.length;
  strNew = strNew + strOrig.substring(j, i);

  return strNew;
  }

function getLocalizedFontSize(iStringIndex) {
  var sLng = window.clientInformation.browserLanguage;

  if (sLng.indexOf("zh") == 0) { return 12; }
  if (sLng.indexOf("ja") == 0 || sLng.indexOf("ko") == 0) { return 11; }

  return 10;
  }


function getLocalizedFontName(iStringIndex) {
  var sLng = window.clientInformation.browserLanguage;

  if (sLng.indexOf("zh-hk") == 0) { return "PMingLiu"; }
  if (sLng.indexOf("zh-tw") == 0) { return "PMingLiu"; }
  if (sLng.indexOf("zh-sg") == 0) { return "PMingLiu"; }
  if (sLng.indexOf("zh") == 0) { return "SimSun"; }

  if (sLng.indexOf("ko") == 0) { return "Dotum"; }
  if (sLng.indexOf("ja") == 0) { return "MS PGothic"; }

  return "Arial Unicode MS";
  }

function getLocalizedString(iStringIndex) {
  var sLng = window.clientInformation.browserLanguage;

  if (iStringIndex == iALL_c) {
    if (sLng.indexOf("ar") == 0) { return "(الكل)"; }
    if (sLng.indexOf("zh-hk") == 0) { return "(全部)"; }
    if (sLng.indexOf("zh-tw") == 0) { return "(全部)"; }
    if (sLng.indexOf("zh-sg") == 0) { return "(全部)"; }
    if (sLng.indexOf("zh") == 0) { return "(全部)"; }
    if (sLng.indexOf("cs") == 0) { return "(Všechny)"; }
    if (sLng.indexOf("da") == 0) { return "(Alle)"; }
    if (sLng.indexOf("nl") == 0) { return "(Alle)"; }
    if (sLng.indexOf("fi") == 0) { return "(Kaikki)"; }
    if (sLng.indexOf("fr") == 0) { return "(Toutes)"; }
    if (sLng.indexOf("de") == 0) { return "(Alle)"; }
    if (sLng.indexOf("el") == 0) { return "(Όλες)"; }
    if (sLng.indexOf("he") == 0) { return "(הכל)"; }
    if (sLng.indexOf("hu") == 0) { return "(Mind)"; }
    if (sLng.indexOf("it") == 0) { return "(Tutte)"; }
    if (sLng.indexOf("ja") == 0) { return "(すべて)"; }
    if (sLng.indexOf("ko") == 0) { return "모두"; }
    if (sLng.indexOf("no") == 0) { return "(Alle)"; }
    if (sLng.indexOf("pl") == 0) { return "(Wszystkie)"; }
    if (sLng.indexOf("pt") == 0) { return "(Todas)"; }
    if (sLng.indexOf("ro") == 0) { return "(Toate)"; }
    if (sLng.indexOf("ru") == 0) { return "Bce"; }
    if (sLng.indexOf("sk") == 0) { return "(Všetky)"; }
    if (sLng.indexOf("sl") == 0) { return "Vse"; }
    if (sLng.indexOf("es") == 0) { return "(Todas)"; }
    if (sLng.indexOf("sv") == 0) { return "(Alla)"; }
    if (sLng.indexOf("th") == 0) { return "ทั้งหมด"; }
    if (sLng.indexOf("tr") == 0) { return "Tümü"; }
    if (sLng.indexOf("hr") == 0) { return "(Svi)"; }

    return "(all)";
    }

  if (iStringIndex == iMAXROWSTOSHOW_c) {  
    if (sLng.indexOf("ar") == 0) { return "العدد الأقصى من الصفوف للإظهار "; }
    if (sLng.indexOf("zh-hk") == 0) { return "可顯示的最多列數"; }
    if (sLng.indexOf("zh-tw") == 0) { return "可顯示的最多列數"; }
    if (sLng.indexOf("zh-sg") == 0) { return "可顯示的最多列數"; }
    if (sLng.indexOf("zh") == 0) { return "可显示的最大行数"; }
    if (sLng.indexOf("cs") == 0) { return "maximální počet řádků k zobrazení"; }
    if (sLng.indexOf("da") == 0) { return "maksimale antal rækker, der skal vises"; }
    if (sLng.indexOf("nl") == 0) { return "Maximum aantal weer te geven rijen"; }
    if (sLng.indexOf("fi") == 0) { return "näytettävien rivien enimmäismäärä"; }
    if (sLng.indexOf("fr") == 0) { return "nombre maximum de lignes à afficher"; }
    if (sLng.indexOf("de") == 0) { return "Maximale Anzahl anzuzeigender Zeilen"; }
    if (sLng.indexOf("el") == 0) { return "Μέγιστο πλήθος σειρών για προβολή"; }
    if (sLng.indexOf("he") == 0) { return "המספר המירבי של שורות להצגה"; }
    if (sLng.indexOf("hu") == 0) { return "mutatandó sorok maximális száma"; }
    if (sLng.indexOf("it") == 0) { return "numero massimo di righe da mostrare"; }
    if (sLng.indexOf("ja") == 0) { return "表示する最大列数"; }
    if (sLng.indexOf("ko") == 0) { return "표시할 최대 행 수"; }
    if (sLng.indexOf("no") == 0) { return "Maksimalt antall rader som skal vises"; }
    if (sLng.indexOf("pl") == 0) { return "Maksymalna ilość wierszy do pokazania"; }
    if (sLng.indexOf("pt") == 0) { return "número máximo de linhas a mostrar"; }
    if (sLng.indexOf("ro") == 0) { return "numărul maxim de rânduri care se afişează"; }
    if (sLng.indexOf("ru") == 0) { return "наибольшее количество отображаемых строк"; }
    if (sLng.indexOf("sk") == 0) { return "Maximálny počet riadkov na zobrazenie"; }
    if (sLng.indexOf("sl") == 0) { return "Največje število vrstic za prikaz"; }
    if (sLng.indexOf("es") == 0) { return "número máximo de filas para mostrar"; }
    if (sLng.indexOf("sv") == 0) { return "maximalt antal rader att visa"; }
    if (sLng.indexOf("th") == 0) { return "จำนวนแถวมากที่สุดที่สามารถแสดงได้"; }
    if (sLng.indexOf("tr") == 0) { return "Gösterilecek maksimum satır sayısı"; }
    if (sLng.indexOf("hr") == 0) { return "maksimalan broj redaka za prikaz"; }

    return "maximum number of rows to show";
    }
  }
</script>

</public:component>