/**********************************************************************
 *
 * $Id: wfsConnector.js,v 1.5 2006/09/09 12:05:17 lbecchi Exp $
 *
 * purpose: add WFS features to ka-Map (gub 1490)
 *         
 *
 * author: Lorenzo Becchi & Andrea Cappugi
 *
 * TODO:
 *   - 
 * 
 **********************************************************************
 *
 * Copyright (c)  2006, ominiverdi.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 *
 **********************************************************************/

/******************************************************************************
 * _wfsConnector - spiega bene
 *
 * To use wfsConnector:
 * 
 * 1) add a script tag to your page:
 * 
 * <script type="text/javascript" src="wfsConnector.js"></script>
 *
 * 2) create a new instance of _wfsConnector
 *
 * var l = new _wfsConnector( szName, bVisible, opacity, imageformat, bQueryable, 
 *                   server, version, layers, srs);
 *
 * 3) add it to the map
 *
 * ???? myKaMap.addMapLayer( l );
 *
 * For instance, assuming you have a form to input the parameters required:
 *
 * function addWFSData()
 * {
 *     var f = document.forms.wfs;
 *     var szName = f.wfsName.value;
 *     var server = f.wfsServer.value;
 *     var version = "1.0.0";
 *     var layers = f.wfsLayers.value;

 *     var wfsConn = new _wfsConnector( szName, server, version, layers);

 * }
 *
 *****************************************************************************/
 
 function _wfsConnector( oKaMap, szID, server, version,epsg ) // next-arguments: wfsConnectionName, pinLayerName, isUseStandardPinImage
 {
    //_layer.apply(this,[szName,bVisible,opacity,imageformat,bQueryable]);
    // Hacked by D10 - BEGIN
    this.xAdjust = 252;
    this.yAdjust = 95;
    this.isUseMiniFont = "0";
    this.isInitOnlySessId = "0";
    this.isUseBBox = "0";
    this.isUseLongLat = "0";
    this.NextRoutine = "";
    this.isUseDMLayout = "0";
    this.isUseColURLTip = "0";
    this.isUseURLTip = "0";
    this.isUseHTMLTip = "0";
    this.isUseHTMLTipHdrFtr = "0";
    this.BBoxMinX = 0;
    this.BBoxMinY = 0;
    this.BBoxMaxX = 0;
    this.BBoxMaxY = 0;
    this.ColURLTipName = "";
    this.ColImgTipName = "";
    this.URLTip = "";
    this.HTMLTip = "";
    this.HTMLTipHdr = "";
    this.HTMLTipFtr = "";
    this.pleaseEnableChecks = false;
    if (arguments[5]!=undefined) {
	this.wfsConnectionName=arguments[5];
    } else {
        this.wfsConnectionName="wfsConn";
    }
    if (arguments[7]!=undefined) {
	this.isUseStandardPinImage=arguments[7];
    } else {
        this.isUseStandardPinImage=true;
    }
    this.lang = 'en';
    // Hacked by D10 - END
    this.server = server;
    this.kaMap = oKaMap;
    this.wfsObj=this.kaMap.getRawObject(szID);
    this.version = (version && version != '') ? version : '1.0.0';
    this.layers = '';
    this.baseURL = this.server;
    this.sessionId='';
    this.connector='';
    this.epsg= epsg;
  
    this.toolTip = new kaToolTip( this.kaMap );
    //var offsetX=-6;//offset dell'immagine rispetto alle coordinate del punto // Commented by D10 , diganti ke setRedTipImage() !
	//var offsetY=-19;//serve per centrare l'immagine rispetto al punto  // Commented by D10 , diganti ke setRedTipImage() !
	//this.toolTip.setTipImage('images/tip-red.png',offsetX,offsetY);  // Commented by D10 , diganti ke setRedTipImage() !

	this.points;
	this.lines;
	this.polygons;

	this.XMLcode = '';//temporaneo per metterci su il GML della feature
	
	//PG kaXMLOverlay instance

//	this.myDrawingCanvas = new kaXmlOverlay( this.kaMap, 250 );

	var idx = 100;//lo zindex del canvas

    if (arguments[6]!=undefined) {
        pinLayerName=arguments[6];
	    this.canvas = this.kaMap.createDrawingCanvas( idx , pinLayerName);  //add by D10
    } else {
        this.canvas = this.kaMap.createDrawingCanvas( idx );
    }
	
     /*		
     * make sure the server url is terminated with a ? or not a & so we can
     * append the rest of the request without having to worry about a
     * correctly formatted url
     */
    if (this.baseURL.indexOf('?') == -1)
    {
        this.baseURL = this.baseURL + '?';
    }
    else
    {
        if (this.baseURL.charAt( this.baseURL.length - 1 ) == '&')
            this.baseURL = this.baseURL.slice( 0, -1 );
    }

    /*
     * required components of WMS 1.0.0 are:
     * VERSION set to version or 1.1.1 if version is empty
     * REQUEST - set to GetMap if not in the server URL
     * TYPENAME - set to layers if not in the server URL
     * SRS - set to srs if not in the server URL
     * BBOX - ?????
     * OUTPUTFORMAT = GML2
     *
     * Optional components are:
     *
     * TRANSPARENT - set to ON if not in the server URL
     * BGCOLOR - do not add this, it will interfer with transparency
     * EXCEPTIONS - set to in image if not in the server URL
     * TIME - not supported in this code, add to server URL yourself
     * ELEVATION - not supported in this code, add to server URL yourself
     * SLD (SLD only) - not supported in this code, add to server URL yourself
     * WFS (SLD only) - not supported in this code, add to server URL yourself
     */
     
    this.baseURL = this.addRequestParameter(this.baseURL, 'service', "&service=WFS" );
    this.baseURL = this.addRequestParameter(this.baseURL, 'request', "&request=GetCapabilities" );
    this.baseURL = this.addRequestParameter(this.baseURL, 'version', "&version="+this.version );
    //this.addRequestParameter( 'typename', "&typename=" + escape(this.layers) );
/*    this.addRequestParameter( 'srs', "&srs=" + this.srs );
    this.addRequestParameter( 'styles', "&styles=" );
    this.addRequestParameter( 'format', "&format=" + this.imageformat );
    this.addRequestParameter( 'transparent', '&transparent=true' );
    this.addRequestParameter( 'exceptions', '&exceptions=application/vnd.ogc.se-inimage' );*/

    this.connector = 'tools/wfs/wfs_connector.php?';
    var myURL = this.connector + 'wfsURL='+encodeMyHtml(this.baseURL);
    myURL = this.addRequestParameter(myURL, 'com', "&com=getCapabilities" );
    myURL = this.addRequestParameter(myURL, 'xinitonlysessid', "&initonlysessid="+this.isInitOnlySessId );
 //   document.getElementById('toolbar').innerHTML = this.connector;//DEBUG
  
    //call(myURL,this, this.init); // D10 is here ! Pemanggilan WFS getCapabilities 2 kali kadang bikin error !
 	this.kaMap.registerForEvent( KAMAP_MAP_INITIALIZED, this, this.clearPoints );
    this.kaMap.registerForEvent( KAMAP_SCALE_CHANGED, this, this.extentChanged );
  //  this.kaMap.registerForEvent( KAMAP_EXTENTS_CHANGED, this, this.extentChanged );
 
 
 };
 
/** 
 * wmsLayer.addRequestParameter( name, parameter )
 *
 * add a parameter to the baseURL safely by checking to see if the parameter
 * exists already.  This is an internal function not intended to be used
 * by other code.
 */
_wfsConnector.prototype.addRequestParameter = function(url, name, parameter )
{

    if ((url) && url.indexOf( name ) == -1)
    {
        return url + parameter;
    }
};

//add by D10
_wfsConnector.prototype.setXYAdjust = function(xAdj,yAdj)
{
	this.xAdjust = xAdj;	
	this.yAdjust = yAdj;
};
_wfsConnector.prototype.setIsUseMiniFont = function(isUseMiniFont)
{
	this.isUseMiniFont = isUseMiniFont;	
};
_wfsConnector.prototype.setIsInitOnlySessId = function(initonlysessid)
{
	this.isInitOnlySessId = initonlysessid;	
};
_wfsConnector.prototype.setNextRoutine = function(nextroutine)
{
	this.NextRoutine = nextroutine;	
};
_wfsConnector.prototype.setLang = function(lang)
{
	this.lang = lang;	
};
_wfsConnector.prototype.setGreenTipImage = function(greenPinImage)
{
	this.greenPinImage = greenPinImage;	
};
_wfsConnector.prototype.setRedTipImage = function(redPinImage)
{
    var offsetX=-6; // asli
    var offsetY=-19; // asli
    if (arguments[1]!=undefined) {
	    offsetX=arguments[1];
    }
    if (arguments[2]!=undefined) {
	    offsetY=arguments[2];
    }
	this.toolTip.setTipImage(redPinImage,offsetX,offsetY);

};
_wfsConnector.prototype.setIsUseLongLat = function(isUseLongLat){ // isUseLongLat = "0" or "1" :)
	this.isUseLongLat = isUseLongLat;
};
_wfsConnector.prototype.setIsUseBBox = function(isUseBBox){ // isUseBBox = "0" or "1" :)
	this.isUseBBox = isUseBBox;
};
_wfsConnector.prototype.setIsUseDMLayout = function(isUseDMLayout){ // isUseDMLayout = "0" or "1" :)
	this.isUseDMLayout = isUseDMLayout;
};
_wfsConnector.prototype.setIsUseColURLTip = function(isUseColURLTip){ // isUseColURL = "0" or "1" :)
	this.isUseColURLTip = isUseColURLTip;
};
_wfsConnector.prototype.setColImgTipName = function(ColImgTipName) {
	this.ColImgTipName = ColImgTipName;
};
_wfsConnector.prototype.setColURLTipName = function(ColURLTipName) {
	this.ColURLTipName = ColURLTipName;
};
_wfsConnector.prototype.setIsUseURLTip = function(isUseURLTip){ // isUseURLTip = "0" or "1" :)
	this.isUseURLTip = isUseURLTip;
};
_wfsConnector.prototype.setURLTip = function(URLTip) {
	this.URLTip = URLTip;
};
_wfsConnector.prototype.setIsUseHTMLTip = function(isUseHTMLTip){ // isUseHTMLTip = "0" or "1" :)
	this.isUseHTMLTip = isUseHTMLTip;
};
_wfsConnector.prototype.setHTMLTip = function(HTMLTip) {
	this.HTMLTip = HTMLTip;
};
_wfsConnector.prototype.setIsUseHTMLTipHdrFtr = function(isUseHTMLTipHdrFtr){ // isUseHTMLTipHdrFtr = "0" or "1" :)
	this.isUseHTMLTipHdrFtr = isUseHTMLTipHdrFtr;
};
_wfsConnector.prototype.setHTMLTipHdr = function(HTMLTipHdr) {
	this.HTMLTipHdr = HTMLTipHdr;
};
_wfsConnector.prototype.setHTMLTipFtr = function(HTMLTipFtr) {
	this.HTMLTipFtr = HTMLTipFtr;
};
_wfsConnector.prototype.setBBox = function(BBoxMinX,BBoxMinY,BBoxMaxX,BBoxMaxY){
	this.BBoxMinX = BBoxMinX;
	this.BBoxMinY = BBoxMinY;
	this.BBoxMaxX = BBoxMaxX;
	this.BBoxMaxY = BBoxMaxY;
};
//end Add

_wfsConnector.prototype.init = function(szResult)
{

  eval(szResult);
  if (this.lang=='en') {
    getRawObject('wfsMessage').innerHTML = 'now choose a Feature';
  } else if (this.lang=='id') {
    getRawObject('wfsMessage').innerHTML = 'Pilih direktori yang akan ditampilkan';
  }
  
  /* qua dietro ripulire l'interfaccia*/
};

_wfsConnector.prototype.extentChanged = function()
{

  this.toolTip.move();
};

_wfsConnector.prototype.draw = function(szResult)
{
//document.getElementById('toolbar').innerHTML = szResult;//DEBUG
//  alert(szResult);

	var feature='';
	
	eval(szResult);

	if (this.points!=null) {
		if (this.lang=='en') {
		  getRawObject('wfsMessage').innerHTML = 'enjoy! '+this.points.length+' points displayed';
        	} else if (this.lang='id') {
	          getRawObject('wfsMessage').innerHTML = this.points.length+'  lokasi ditampilkan';
        	}
	}
 if(this.points){
	
	
		
	for(var i=0;i<this.points.length;i++){
 		var point = this.points[i];
 		//PG kaXmlOverlay CLASS
 		// Id, x ,y 
 		/*var pm = this.myDrawingCanvas.addNewPoint(point[0], point[1], point[2]);
		pm.div.onmouseover=this.onmouseover;
		//pm.div.onmouseout=this.onmouseout;
		pm.div.style.cursor='pointer';
		pm.feature = feature;
		pm.connector=this;
		var s = new kaXmlSymbol();
		s.size = 10;
		s.color = 'rgb(200,10,50)';
		pm.addGraphic(s);
		var l = new kaXmlLabel();
		l.py = -2;
		l.text = "ID="+point[0];
		l.fsize = "8px";*/
//		pm.addGraphic(l);
		var img = document.createElement('img');
		img.id = feature+'_image['+i+']'; //add id for img pin --h4rdy4ntz
		if (this.isUseStandardPinImage) {
			img.src = 'images/tip-green.png';
		} else {
			img.src = this.greenPinImage;
		}
		img.pid = point[0];
		img.width = 20;
		img.height = 20;
		img.onmouseover=this.onmouseover;
		//img.onmouseout = this.onmouseout;
		img.style.cursor='pointer';
		img.feature = feature;
		img.connector=this;
		var obj = img;
		var lon = point[1];
		var lat = point[2];
		img.lon = lon;
		img.lat = lat;
		this.kaMap.addObjectGeo( this.canvas, lon, lat, obj );
		
 	}

 };
 
 
//  if(this.lines){
//   // TO BE IMPLEMENTED
//   
//  }
//  
//   if(polygons){
//   // TO BE IMPLEMENTED
//   
//  }
 
 
};
_wfsConnector.prototype.onmouseout = function(e)
{
e = (e)?e:((event)?event:null);
/*var msgBox =getRawObject('msgBox');
msgBox.style.top='-300px';
msgBox.style.left='0px';*/
// Hacked by D10 : GAGAL !
/*var conn=this.connector;
conn.toolTip.domObj.style.top='-10000px';
conn.toolTip.domObj.style.left= '-10000px';
alert('Out');
*/
};
_wfsConnector.prototype.onmouseover = function(e)
{
 e = (e)?e:((event)?event:null);
//var msgBox =getRawObject('msgBox');
//msgBox.innerHTML='WAIT A MOMENT :-)';

/*
var y =(e.clientY);
var x =(e.clientX);
conn.toolTip.setText('WAIT A MOMENT... Connecting');

conn.toolTip.move(x,y);
*//*
var a = conn.kaMap.adjustPixPosition( x,y );
var p = conn.kaMap.pixToGeo( a[0],a[1] );
conn.toolTip.setText('WAIT A MOMENT... Connecting');*/

var conn=this.connector;
conn.toolTip.moveGeo(this.lon,this.lat,conn.xAdjust,conn.yAdjust);
var url=conn.kaMap.server+"tools/wfs/wfs_connector.php?";
url=conn.addRequestParameter(url, 'sessionId', "&sessionId="+conn.sessionId );
url=conn.addRequestParameter(url, 'featureId', "&featureId="+this.pid );
url=conn.addRequestParameter(url, 'features', "&features="+this.feature );
url=conn.addRequestParameter(url, 'initonlysessid', "&initonlysessid="+this.isInitOnlySessId );
url=conn.addRequestParameter(url, 'wfsConnectionName', "&wfsConnectionName="+conn.wfsConnectionName );  // Hacked by D10
url=url+"&com=query";
//url=conn.addRequestParameter(url, 'com', "&com=query" );
url=conn.addRequestParameter(url, 'lang', "&lang="+conn.lang );
if (conn.isUseColURLTip=="1") {
    url =   conn.addRequestParameter(url, 'xisUseColURLTip', "&isUseColURLTip=1" );
    url =   conn.addRequestParameter(url, 'xColURLTipName', "&ColURLTipName="+conn.ColURLTipName );
} else if (conn.isUseURLTip=="1") {
    url =   conn.addRequestParameter(url, 'xisUseURLTip', "&isUseURLTip=1" );
    url =   conn.addRequestParameter(url, 'xURLTip', "&URLTip="+conn.URLTip );
} else if (conn.isUseHTMLTip=="1") {
    url=conn.addRequestParameter(url, 'xisUseHTMLTip', "&isUseHTMLTip=1" );
    url=conn.addRequestParameter(url, 'xHTMLTip', "&HTMLTip="+conn.HTMLTip );
} else if (conn.isUseHTMLTipHdrFtr=="1") {
    url =   conn.addRequestParameter(url, 'xisUseHTMLTipHdrFtr', "&isUseHTMLTipHdrFtr=1" );
    url =   conn.addRequestParameter(url, 'xHTMLTipHdr', "&HTMLTipHdr="+conn.HTMLTipHdr );
    url =   conn.addRequestParameter(url, 'xHTMLTipFtr', "&HTMLTipFtr="+conn.HTMLTipFtr );
} else if (conn.isUseDMLayout=="1") {
    url =   conn.addRequestParameter(url, 'xisUseDMLayout', "&isUseDMLayout=1" );
    url =   conn.addRequestParameter(url, 'xisUseLongLat',  "&isUseLongLat="+conn.isUseLongLat );
    url =   conn.addRequestParameter(url, 'xColURLTipName', "&ColURLTipName="+conn.ColURLTipName );
    url =   conn.addRequestParameter(url, 'xColImgTipName', "&ColImgTipName="+conn.ColImgTipName );
}
if (conn.isUseMiniFont=="1") {
   url = conn.addRequestParameter(url, 'xisUseMiniFont', "&isUseMiniFont="+conn.isUseMiniFont );
} 
call(url,conn,conn.getInfo);

};

_wfsConnector.prototype.getInfo = function(szResult)
{
	eval(szResult);
	this.toolTip.recenter(this.toolTip.domObj);
};

_wfsConnector.prototype.clearPoints = function()
{
	//this.myDrawingCanvas.removePoint();
	this.kaMap.removeDrawingCanvas(this.canvas);
};


_wfsConnector.prototype.getFeatures = function(sessionId,features)
{
  /*???*/
  var featURL = this.server;
  featURL = this.addRequestParameter(featURL, 'service', "&service=WFS" );
  featURL =   this.addRequestParameter(featURL, 'request', "&request=GetFeature" );
  featURL =   this.addRequestParameter(featURL, 'version', "&version="+this.version );
  featURL =   this.addRequestParameter(featURL, 'typename', "&typename="+features );
  myURL = this.connector + 'wfsURL='+encodeMyHtml(featURL);
  if (sessionId!=null) {
    myURL =   this.addRequestParameter(myURL, 'sessionId', "&sessionId="+this.sessionId ); // ACCESSING null of this.sessionId will make ERROR !
  }
  myURL =   this.addRequestParameter(myURL, 'com', "&com=getFeature" );
  myURL =   this.addRequestParameter(myURL, 'epsg', "&epsg="+this.epsg );
  myURL =   this.addRequestParameter(myURL, 'lang', "&lang="+this.lang );
  myURL =   this.addRequestParameter(myURL, 'nextroutine', "&nextroutine="+this.NextRoutine );
  if (this.isUseBBox=="1") {
    myURL =   this.addRequestParameter(myURL, 'isUseBBox', "&isUseBBox=1" );
    myURL =   this.addRequestParameter(myURL, 'BBoxMinX', "&BBoxMinX="+this.BBoxMinX );
    myURL =   this.addRequestParameter(myURL, 'BBoxMinY', "&BBoxMinY="+this.BBoxMinY );
    myURL =   this.addRequestParameter(myURL, 'BBoxMaxX', "&BBoxMaxX="+this.BBoxMaxX );
    myURL =   this.addRequestParameter(myURL, 'BBoxMaxY', "&BBoxMaxY="+this.BBoxMaxY );
  }
   //document.getElementById('legend').innerHTML = myURL;//DEBUG
   
  call(myURL,this, this.draw);
  
};



function encodeMyHtml(string) {
  encodedHtml = escape(string);
  encodedHtml = encodedHtml.replace(/\//g,"%2F");
  encodedHtml = encodedHtml.replace(/\?/g,"%3F");
  encodedHtml = encodedHtml.replace(/=/g,"%3D");
  encodedHtml = encodedHtml.replace(/&/g,"%26");
  encodedHtml = encodedHtml.replace(/@/g,"%40");
  return encodedHtml;
  } ;
function urlDecode(sz){

return sz;
return unescape(sz).replace(/\+/g," ");

};
