<% /*
     $Revision$
     $Id$

     File:      FcltJquery.js
     Note:      JSONP compatible
*/ %>
<%
var maxNormal = 10;
var maxFull = S("qp_maxrows_suggest");
// sql
// bAll: haal alle records op (met nog steeds een maximum volgens setting qp_maxrows_suggest (default 1500)
// bStoreDesc: Hoewel we zoeken op fldName slaan we uiteindelijk fldDesc op in het suggest veld
//             (vooral gebruikt voor zoeken op telefoonnummer)

var callback = getQParam("callback", null);
if ( callback && !S("fac_allow_jsonp") )
    _AiAi("JSONP not allowed");

// Deprecated version
function WriteResult(sql, bAll, fldName, fldKey, fldDesc, fldExtraParam, fldTitle, bStoreDesc, checkExist)
{
    return WriteResult2(sql, { bAll:          bAll,
                               fldName:       fldName,
                               fldKey:        fldKey,
                               fldDesc:       fldDesc,
                               fldExtraParam: fldExtraParam,
                               fldTitle:      fldTitle,
                               bStoreDesc:    bStoreDesc,
                               checkExist:    checkExist
                             });
}

// Params bAll, fldName, fldKey, fldDesc, fldExtraParam, fldTitle, bStoreDesc, checkExist of similarSQL
//        foto
function WriteResult2(sql, params)
{
  var jsonResult = { bForcedAll: params.bForcedAll } ;
  params.bAll |= params.bForcedAll;

  if ("preferredKeys" in params && params.preferredKeys.length)
  {
    sql = "SELECT * FROM (" + sql + ")";
    sql += " ORDER BY DECODE(" + params.fldKey
    for (var i = 0; i < params.preferredKeys.length; i++)
    {
      sql += ", " + params.preferredKeys[i] + ", " + i;
    }
    sql += ", " + i + ")";
  }

  if (!params.bAll)
  	sql = "SELECT * FROM (" + sql + ") WHERE ROWNUM < " + (maxNormal + 1);
  else
  	sql = "SELECT * FROM (" + sql + ") WHERE ROWNUM < " + (maxFull + 1);

  var oRs = Oracle.Execute(sql);
  if (oRs.eof && params.SimilarSql)
  {
    oRs.Close();
    __Log("Niets gevonden, ik probeer het nog eens met SimilarSql");
    oRs = Oracle.Execute(params.SimilarSql);
    jsonResult.similar = true;
  }
  var cnt = 1;
  var lijst = [];
  if (params.firstOption)
    lijst.push(params.firstOption);
  while (!oRs.eof && cnt < (params.bAll? maxFull : maxNormal))
  {
    cnt++;
    // JSON Syntax
    var result = { txt: oRs(params.fldName).Value,
                   key: oRs(params.fldKey).Value,
                   desc: oRs(params.fldDesc).Value || "" // Indien null dan lege string weergeven
                 };
    if (params.fldExtraParam && oRs(params.fldExtraParam).Value)
        result.extra = oRs(params.fldExtraParam).Value;
    if (params.fldTitle && oRs(params.fldTitle).Value)
        result.title = oRs(params.fldTitle).Value;
    if (params.fldExtraDesc && oRs(params.fldExtraDesc).Value)
        result.txtextra = oRs(params.fldExtraDesc).Value;
    // extra attributes for the result are put in object result.extraAttr
    if (params.extraParamValue)
    {
        var index;
        var arrAttr = params.extraParamValue.split(",");
        result.extraParamValue = {};
        for (index = 0; index < arrAttr.length; index++)
        {
            var arrAttrString = String(arrAttr[index]).replace(/^\s+|\s+$/g, ""); // Replace is for trim spaces.
            result.extraParamValue[arrAttrString] = oRs(arrAttrString).Value != null? oRs(arrAttrString).Value : "";
        }
    }
    if (params.fnFoto)
    {
        var foto = params.fnFoto(oRs);
        if (foto)
        {
            result.foto = foto;
            jsonResult.anyfoto = true;
        }
    }

    lijst.push(result);
    oRs.MoveNext();
  }
  jsonResult.result = lijst;
  if (!oRs.eof)
  {
      jsonResult.hasMore = true;
  }
  oRs.close();

  if (params.bAll)
     jsonResult.isAll = true;
  if (params.bStoreDesc)
    jsonResult.storeDesc = true;
  if (params.checkExist)
    jsonResult.checkExist = true;

  if (callback)
  {
      Response.ContentType = "application/javascript";
      Response.Write(callback + "(" + JSON.stringify(jsonResult) + ")");
  }
  else
      Response.Write(JSON.stringify(jsonResult));
}
%>