/************************************************************
* Geo Web Solutions (C) 2006 Bentley Systems Netherlands BV *
* -------------------------------------------------------   *
* This script page contains a number of generic functions   *
* needed for this website.                                  *
************************************************************/


/*****************VARIABLES AND CONSTANTS******************/

var _mapWidth=520;                        // width of the flashobject in the website
var _mapHeight=510;                       // height of the flashobject in the website
var _serviceName = 'Milieu';         // Name of the Geo Web Solutions service to use
//var _searchName = 'Adres';                // Name of the Search to present to the user
//var _questionOne = 'Straatnaam';          // Name of the first Question (column) for this search
//var _questionTwo = 'Hnr';          // Name of the second Question (column) for this search

var _searchName = 'BAG_Adres';        // Name of the Search to present to the user
var _questionOne = 'STRAAT';          // Name of the first Question (column) for this search
var _questionTwo = 'HNR';             // Name of the second Question (column) for this search

var _searchNameBp = 'Bestemmingsplan';     // Name of the Search to present to the user
var _questionBp = 'Bestemmingsplan';      // Name of the first Question (column) for this search

var isIE = (document.all) ? true : false;
var sVersion = "1.5.0";
var xmlns = "http://www.bentley.com/FlexiMap"
var xmlns_xsi = "http://www.w3.org/2001/XMLSchema-instance";
var xsi_schemaLocation = "http://www.bentley.com/FlexiMap FMRequest.xsd";
var host = window.location.hostname;

var _answerOne='';
var _answerTwo='';

var _answerBp='';

var _Report_Url='';

//show the pdf in a new window
function getPDF(doc)
{
  var url= 'http://' + window.location.hostname + '/geowebsolutions/veh/showdoc.asp?' + doc;
  win = window.open(url,'pdf');
}

function getReport()
{
  if(_Report_Url != '')
  {
     parent.fraReport.location.href = _Report_Url;
     //parent.fraReport.location.href = _Report_Url;

    _Report_Url='';
    
    //'<A href="http://www.cbpweb.nl" target=_blank></A>';
  }
  else
  {
    parent.fraReport.location.href = 'Pagina_Rapportage.html';
  }
  
}

function letterSearch(sChar)
{
  //clear divtags
  document.getElementById("divAnswer1").innerHTML = '<font class="pluswit">Een ogenblik aub...</font>';
  document.getElementById("divAnswer2").innerHTML = '';
  setTimeout('loadFirstQuestion("'+ sChar +'")', 100)
}


function retrieveXML(sendXML)
{
  var xmlObj;
  if(isIE)
  {
    xmlObj = new ActiveXObject("MSXML2.XMLHTTP");
  }
  else
  {
    xmlObj = new XMLHttpRequest();
  }

  xmlObj.open("POST","http://" + window.location.hostname + "/scripts/fleximap.dll", false);
  xmlObj.setRequestHeader("Content-Length", sendXML.length);
  xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
  xmlObj.send(sendXML);

  return xmlObj.responseXML;
}

function loadFirstQuestion(sValue)
{

  var frag;

  if(isIE)
  {
    frag = new ActiveXObject("Microsoft.XMLDOM");
  }
  else
  {
    frag = document.implementation.createDocument("", "", null);
  }

  var fragRoot = frag.createElement("fragroot");
  frag.appendChild(fragRoot);

  var fragRequest = frag.createElement("FMGetList");
  fragRoot.appendChild(fragRequest);
  fragRequest.setAttribute("version", sVersion);
  fragRequest.setAttribute("xmlns", xmlns);
  fragRequest.setAttribute("xmlns:xsi", xmlns_xsi);
  fragRequest.setAttribute("xsi:schemaLocation", xsi_schemaLocation);

  var fragService = frag.createElement("Service");
  var fragServiceText = frag.createTextNode(_serviceName);
  fragService.appendChild(fragServiceText);
  fragRequest.appendChild(fragService);

  var fragSQ = frag.createElement("SearchQuery");
  fragSQ.setAttribute("name", _searchName);
  fragRequest.appendChild(fragSQ);
  var fragSQCol = frag.createElement("Column");
  fragSQCol.setAttribute("name", _questionOne);
  if (sValue!="")
  {
    fragSQCol.appendChild(frag.createTextNode(sValue));
  }
  fragSQ.appendChild(fragSQCol);

  var sendXML;
  if(isIE)
  {
    sendXML = fragRequest.xml;
  }
  else
  {
    var ser = new XMLSerializer();
    sendXML = ser.serializeToString(fragRequest);
  }

  var responseXML = retrieveXML(sendXML);
  var responseTag = responseXML.documentElement.tagName;

  var elColumn = responseXML.documentElement.getElementsByTagName("Column")[0];

  //alert(sHtml);
  var sHtml =  '<option>- Selecteer -</option>';
  //alert(sHtml);

  //var sHtml = '';
  for(idx = 0; idx < elColumn.childNodes.length; idx++)
  {
    elItem = elColumn.childNodes[idx];
    if(elItem.childNodes.length > 0)
    {
      sHtml += '<option value="' + elItem.childNodes[0].nodeValue + '">' + elItem.childNodes[0].nodeValue + '</option>';
    }
  }
  if(sHtml=='')
  {
    sHtml = '<font class="plus"> - geen resultaten - </font>';
  }
  else
  {
    sHtml = '<select size="1" style="background-color:FFFFFF" style="color:000000" width="150em" name="listbox1" id="listbox1" onClick="loadSecondQuestion(listbox1.value)">' + sHtml + '</select>';
  }
  //sHtml='<font class="pluswit">Adres:</font>' + sHtml;

  //alert(sHtml);
  document.getElementById("divAnswer1").innerHTML = sHtml;

  return true;
}


function loadSecondQuestion(sValue)
{

  //reset second answer if available
  _answerTwo = '';
  _answerOne = sValue;
  updateChoices();


  if (sValue =="")
  {
    return false;
  }

  var frag;
  if(isIE)
  {
    frag = new ActiveXObject("Microsoft.XMLDOM");
  }
  else
  {
    frag = document.implementation.createDocument("", "", null);
  }

  var fragRoot = frag.createElement("fragroot");
  frag.appendChild(fragRoot);

  var fragRequest = frag.createElement("FMGetList");
  fragRoot.appendChild(fragRequest);
  fragRequest.setAttribute("version", sVersion);
  fragRequest.setAttribute("xmlns", xmlns);
  fragRequest.setAttribute("xmlns:xsi", xmlns_xsi);
  fragRequest.setAttribute("xsi:schemaLocation", xsi_schemaLocation);

  var fragService = frag.createElement("Service");
  var fragServiceText = frag.createTextNode(_serviceName);
  fragService.appendChild(fragServiceText);
  fragRequest.appendChild(fragService);

  var fragSQ = frag.createElement("SearchQuery");
  fragSQ.setAttribute("name", _searchName);
  fragRequest.appendChild(fragSQ);
  var fragSQCol = frag.createElement("Column");
  fragSQCol.setAttribute("name", _questionOne);
  fragSQCol.appendChild(frag.createTextNode(sValue));
  fragSQ.appendChild(fragSQCol);

  fragSQCol = frag.createElement("Column");
  fragSQCol.setAttribute("name", _questionTwo);
  fragSQ.appendChild(fragSQCol);

  var sendXML;
  if(isIE)
  {
    sendXML = fragRequest.xml;
  }
  else
  {
    var ser = new XMLSerializer();
    sendXML = ser.serializeToString(fragRequest);
  }

  var responseXML = retrieveXML(sendXML);
  var responseTag = responseXML.documentElement.tagName;

  var elColumn = responseXML.documentElement.getElementsByTagName("Column")[0];

  var sHtml =  '<option>- -</option>';
  for(idx = 0; idx < elColumn.childNodes.length; idx++)
  {
    elItem = elColumn.childNodes[idx];


    if(elItem.childNodes.length > 0)
    {

      sHtml += '<option value="' + elItem.childNodes[0].nodeValue + '">' + elItem.childNodes[0].nodeValue + '</option>';
    }
  }
  if(sHtml=='')
  {
    sHtml = '<font class="plus"> - geen resultaten - </font>';
  }
  else
  {
    sHtml = '<select size="1" style="background-color:FFFFFF" style="color:000000" width="130em" name="listbox2" id="listbox2" onChange="questionsDone(listbox2.value)">' + sHtml + '</select></div>';
  }
  //sHtml='<font class="pluswit">Huisnummer:<br></font>' + sHtml;

  document.getElementById("divAnswer2").innerHTML = sHtml;
}


function updateChoices()
{
  var sStr='' +  _answerOne + ' ' + _answerTwo + '';

  //alert( sStr );


  //document.getElementById("answers").innerHTML = sStr;
  document.getElementById("answers").innerHTML = ' ';
}

function questionsDone(sValue)
{
  _answerTwo = sValue;
  updateChoices();
  buildStartParams();
}

/*---------------------------------------------------------------------*
 * buildStartParams:                                                   *
 * Initialize Flash with parameters provided in the searchquery        *
 *---------------------------------------------------------------------*/
function buildStartParams()
{
  var movieVars = "http://"+ host + "/GeoWebSolutions/fmClient.swf?fm=http://"+ host + "/Scripts/FlexiMap.dll";
  movieVars += "&commurl=report.asp&commtarget=fraReport";
  movieVars += "&service=" + _serviceName;

  var sSQ='';
  // check if a searchquery has been executed, if so add to startup string
  if (_answerOne != '')
  {
    sSQ='&sq='+_searchName + '&' + _questionOne + '=' + _answerOne;
    if (_answerTwo != '')
    {
      sSQ +='&' + _questionTwo + '=' + _answerTwo;
    }
  }

  movieVars += sSQ;
  //call function that (re)initializes Flash

  startFlash(movieVars);

}

/*---------------------------------------------------------------------*
 * BP query                                                            *
 *---------------------------------------------------------------------*/

function loadQuestionBp(sValue)
{

  var frag;

  if(isIE)
  {
    frag = new ActiveXObject("Microsoft.XMLDOM");
  }
  else
  {
    frag = document.implementation.createDocument("", "", null);
  }

  var fragRoot = frag.createElement("fragroot");
  frag.appendChild(fragRoot);

  var fragRequest = frag.createElement("FMGetList");
  fragRoot.appendChild(fragRequest);
  fragRequest.setAttribute("version", sVersion);
  fragRequest.setAttribute("xmlns", xmlns);
  fragRequest.setAttribute("xmlns:xsi", xmlns_xsi);
  fragRequest.setAttribute("xsi:schemaLocation", xsi_schemaLocation);

  var fragService = frag.createElement("Service");
  var fragServiceText = frag.createTextNode(_serviceName);
  fragService.appendChild(fragServiceText);
  fragRequest.appendChild(fragService);

  var fragSQ = frag.createElement("SearchQuery");
  fragSQ.setAttribute("name", _searchNameBp);
  fragRequest.appendChild(fragSQ);
  var fragSQCol = frag.createElement("Column");
  fragSQCol.setAttribute("name", _questionBp);
  if (sValue!="")
  {
    fragSQCol.appendChild(frag.createTextNode(sValue));
  }
  fragSQ.appendChild(fragSQCol);

  var sendXML;
  if(isIE)
  {
    sendXML = fragRequest.xml;
  }
  else
  {
    var ser = new XMLSerializer();
    sendXML = ser.serializeToString(fragRequest);
  }

  var responseXML = retrieveXML(sendXML);
  var responseTag = responseXML.documentElement.tagName;

  var elColumn = responseXML.documentElement.getElementsByTagName("Column")[0];

  var sHtml =  '<option>- Selecteer -</option>';

  //var sHtml = '';
  for(idx = 0; idx < elColumn.childNodes.length; idx++)
  {
    elItem = elColumn.childNodes[idx];
    if(elItem.childNodes.length > 0)
    {
      sHtml += '<option value="' + elItem.childNodes[0].nodeValue + '">' + elItem.childNodes[0].nodeValue + '</option>';
    }
  }
  if(sHtml=='')
  {
    sHtml = '<font class="plus"> - geen resultaten - </font>';
  }
  else
  {
    sHtml = '<select size="1" style="background-color:FFFFFF" style="color:000000" width="150em" name="listbox3" id="listbox3" onChange="questionsDoneBp(listbox3.value)">' + sHtml + '</select></div>';
  }
  //sHtml='<font class="pluswit">Bestemmingsplan:</font>' + sHtml;

  //alert(sHtml);
  document.getElementById("divAnswer3").innerHTML = sHtml;

  return true;
}

function updateChoicesBp()
{
  var sStr='' +  _answerBp + '';

  //alert( sStr );
  //document.getElementById("answers").innerHTML = sStr;
  document.getElementById("answers").innerHTML = ' ';
}

function questionsDoneBp(sValue)
{
  _answerBp = sValue;
  updateChoicesBp();
  buildStartParamsBp();
}

/*---------------------------------------------------------------------*
 * buildStartParams:                                                   *
 * Initialize Flash with parameters provided in the searchquery        *
 *---------------------------------------------------------------------*/
function buildStartParamsBp()
{
  var movieVars = "http://"+ host + "/GeoWebSolutions/fmClient.swf?fm=http://"+ host + "/Scripts/FlexiMap.dll";
  movieVars += "&commurl=report.asp&commtarget=fraReport";
  movieVars += "&service=" + _serviceName;

  var sSQ='';
  // check if a searchquery has been executed, if so add to startup string
  if (_answerBp != '')
  {
    sSQ='&sq='+_searchNameBp + '&' + _questionBp + '=' + _answerBp;
  }

  movieVars += sSQ;
  // call function that (re)initializes Flash

  startFlash(movieVars);

}

/*----------------------------------------------------------------*
 * startFlash:                                                    *
 * Initialize Flash with or without specific startparameters      *
 *----------------------------------------------------------------*/
function startFlash(MovieandVars)
{

  var flashHtml = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'";
  flashHtml += "codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0'";
  flashHtml += "WIDTH='"+ _mapWidth + "' HEIGHT='"+ _mapHeight + "' id='fmClient' ALIGN='middle'>";
  flashHtml += "<param name=movie value='"+ MovieandVars + "'>";
  flashHtml += "<param name=allowScriptAccess value='sameDomain'>";
  flashHtml += "<param name=quality value='high'>";
  flashHtml += "<param name=bgcolor value='#'>";
  flashHtml += "<param name=scale value='noscale'>";
  flashHtml += "<embed src='"+ MovieandVars + "' quality=high bgcolor=# WIDTH='"+ _mapWidth + "' HEIGHT='"+ _mapHeight + "' scale='noscale' NAME='fmClient'";
  flashHtml += "align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed>";
  flashHtml += "</object>";

  //alert(flashHtml);
  var divFlash = document.getElementById("flashMap");
  divFlash.innerHTML = flashHtml;

  loadFirstQuestion();
  document.getElementById("divAnswer2").innerHTML = '';
  //loadQuestionBp();

}

/************************ICON BOTTEM FUNCTIONS********************
/**************************************************************/

function showLegend()
        {
          //var legendURL = parent.legendURL;
          alert (parent.legendURL);

          if(legendURL != "") {
            parent.fraReport.location.href = legendURL;
          } else {
            parent.fraReport.location.href = "Er is nog geen bestemmingsplan actief. " +
              "U dient eerst in de kaart een plan actief te maken.";
          }

          return true;
        }

function goHome()
        {
          var Filename = "http://www.bentley.com/";
          document.location.replace(Filename);
        }

function showHelp()
        {
          //parent.fraReport.location.href = "http://www.soest.nl/smartsite13315.dws";
          parent.fraReport.location.href = "http://milieu.soest.nl/Milieu/Pagina_Help.htm";
          return true;
        }

function showRights()
        {
          parent.fraReport.location.href = "http://www.soest.nl/ecache/def/2/671.html";
          return true;
        }

/************************DIV NONE OR BLOCK********************
/**************************************************************/

var open=true;

function toggleDisplay(obj,display1,display2){

        obj=document.getElementById(obj);
          if(obj.style.display == display1)
            obj.style.display=display2;
          else
            obj.style.display=display1;
        if(open){
                  open = false;
                }
        else{

            window.scrollBy(0,200);
            window.scrollBy(0,-200);
            open=true;
            }

}

/************************EXPOSING FUNCTIONS********************
/**************************************************************/
var exposedParams;

/*------------------------------------------------------------*
 * addParams (used starting at version 2.0)                   *
 * This function will be used by Flash to expose parameters   *
 * Function must be named "addParams" and accept 1 parameter  *
 * ==============Content must NOT be changed================= *
 * If string from Flash is too long, this function will be    *
 * called until entire string is passed                       *
 *------------------------------------------------------------*/
function addParams(params)
{
  // DO NOT PUT ALERT BOXES IN THIS FUNCTION
  // FUNCTION IS CALLED WITH AN INTERVAL
  exposedParams += params;
}
/*------------------------------------------------------------*
 * submitParams (used starting at version 2.0)                *
 * This function will be called from Flash when the entire    *
 * string is transferred to addParams                         *
 * Now a function can be called to process the exposed params *
 *------------------------------------------------------------*/
function submitParams()
{
  //now call the function fleximap, to be backward compatible
  fleximap(exposedParams);
}

function fleximap(params)
  {

    //alert (params);
    
    if (params == undefined)
    {
      return;
    }

    var arrAllParams = params.split('|');
    _Report_Url='';

    for (var i = 0;i< arrAllParams.length;i++)
    {
      var tmp = arrAllParams[i];
      var arrOneParam = arrAllParams[i].split('=');
      var param = arrOneParam[0].toLowerCase();
      var val= arrOneParam[1];

      switch (param) {
      case 'undefined':
        break;
      case 'fm':
        fm=val;
        //alert (params);
        break;
      case 'servicekey':
        servicekey=val;
        break;
      case 'service':
        service=val;
        break;
      case 'mapwidth':
        mapwidth=val;
        break;
      case 'mapheight':
        mapheight=val;
        break;
      case 'mapresolution':
        mapresolution=val;
        break;
      case 'xcoord':
        xcoord=val;
        break;
      case 'ycoord':
        ycoord=val;
        break;
      case 'maplayers':
        maplayers=val;
        break;
      case 'redline':

       document.getElementById("answers").innerHTML = ' ';    

       if(val == 'CLEAR' || val == '')
       {
         _Report_Url = '';
       }
       else
       {
         var val_01 = val.substring(9,val.length-2);
         
        //alert(val_01)

        /*
         * for (ii = 0;val_01.match(","); ii++)
         * {
         *   var val_01 = val_01.replace(","," ");
         * }
         *
         */

         var filename = "http://milieu.soest.nl:90/makelaarsmodule/makelaarsmodule.aspx?wkt=POLYGON(( " + val_01 + " ))&ReportType=1&ReportName=Rapport&SelectionType=3&x=1&y=1";
         _Report_Url = filename;

          //alert(_Report_Url);

        }
        break;
      case 'info':
        //infopoint.addItem(val);
        searchCoords=val;
        break;
      }
    }
    //now clear the variables that have been exposed
    exposedParams='';
    return;
}

/*----------------------------------------------------------------*
 * Noenter:                                                    *
 *----------------------------------------------------------------*/
function noenter()
{
  //alert(window.event.keyCode);

  if ( window.event && window.event.keyCode == 13 )
  {
    letterSearch(document.info.txtChar.value);
    return false;
  }
  else
  {
    return true;
  }
}


