// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.4/standard/wwwroot/WEB-INF/config/modules/standard/redline/js/redline.js $ 
// $Date: 25-01-08 12:46 $
// $Revision: 4 $ 
// $Author: Kpo $
// =============================================================== 


var rltPoint = 0;
var rltLine = 1;
var rltPolygon = 2;

function rlHandler(xMap,yMap,xImage,yImage,cbp)
{
    redlineObj.aPRLPoints[redlineObj.nPRLPoints++] = {x:xMap,y:yMap};
    
    var theTable = getElement(redlineObj.tableName);

    xMap=roundToDecs(xMap,0);
    yMap=roundToDecs(yMap,0);

    if (redlineObj.rlType == rltPoint)
        writeTableCellHTML(theTable,1,2,"("+xMap+", "+yMap+")");
    else
        writeTableCellHTML(theTable,1,2,""+cbKortUdstyr.npoints);
    // prompt( "redlineObj.requiredPoints" , redlineObj.requiredPoints ) ;
    // prompt( "cbKortUdstyr.npoints", cbKortUdstyr.npoints ) ;
    // prompt( "redlineObj.okButtonName", redlineObj.okButtonName ) ;
    
    getElement(redlineObj.okButtonName).disabled = (cbKortUdstyr.npoints<redlineObj.requiredPoints);
    var e = getElement("marker");
    if (e)
    {
        setX(e, xImage+cbKort.mapX);
        setY(e, yImage+cbKort.mapY);
//        setX(e, xImage+cbp.mapX-4);
//        setY(e, yImage+cbp.mapY-10);
        showElement(e);
    }
    if(redlineObj.downHandler!=null)
        redlineObj.downHandler();
}

function Redline(title, url, boxName)
{
    this.title = title;
    this.url = url;
    this.nPRLPoints = 0;
    this.aPRLPoints = new Array();
    this.rlType = rltPolygon;
    this.boxName = boxName;
    this.requiredPoints = 3;
    this.windowParams = "width=50,height=50";
    this.window = null;
    this.downHandler = null;
    this.useWKT = false;
}

Redline.prototype.setDownHandler = function(downHandler)
{
    this.downHandler = downHandler;
}

Redline.prototype.setTitle = function(title) 
{
    this.title = title;
}

Redline.prototype.setUrl = function(url)
{
    this.url = url;
}

Redline.prototype.setForm = function(form)
{
    this.form = form;
}

Redline.prototype.setRedlineType = function(rlType)
{
    this.rlType = rlType;
    switch (rlType)
    {
        case rltPoint:
            this.requiredPoints = 1;
        break;
        case rltLine:
            this.requiredPoints = 2;
        break;
        case rltPolygon:
            this.requiredPoints = 3;
        break;
    }
}

Redline.prototype.setWindowParams = function(params)
{
    this.windowParams = params;
}

Redline.prototype.setUseWKT = function(useWkt)
{
    this.useWKT = useWkt;
}

Redline.prototype.activate = function()
{
    cbKortUdstyr.setPointHandler(rlHandler);
    if(this.boxName!="")
    	showElement(getElement(this.boxName));
}

Redline.prototype.deactivate = function()
{
    cbKortUdstyr.setPointHandler(null);
    cbKortUdstyr.reset();
    this.nPRLPoints = 0;
    this.aPRLPoints = new Array();
}

Redline.prototype.close = function()
{
    this.deactivate();
    getElement(this.okButtonName).disabled = true;
    if(this.boxName!="")
        hideElement(getElement(this.boxName));
}

Redline.prototype.OK = function()
{
    switch (this.rlType)
    {
        case rltPolygon:
            cbKortUdstyr.closePolygon();
            var shp = "";
            
            if (this.useWKT)
            {  shp = "POLYGON((";
            }
            for (var i=0;i<this.nPRLPoints-1;i++)
            {
                if (this.useWKT)
                   shp+=this.aPRLPoints[i].x+" "+this.aPRLPoints[i].y+",";
                else
                   shp+=this.aPRLPoints[i].x+"+"+this.aPRLPoints[i].y+"!";                   
            }
            if (this.useWKT)
               shp+= this.aPRLPoints[this.nPRLPoints-1].x+" "+this.aPRLPoints[this.nPRLPoints-1].y + "))";
            else
               shp+=this.aPRLPoints[this.nPRLPoints-1].x+"+"+this.aPRLPoints[this.nPRLPoints-1].y;
        break;
        case rltPoint:
            if (this.useWKT)
               shp= "POINT(" + this.aPRLPoints[this.nPRLPoints-1].x+" "+this.aPRLPoints[this.nPRLPoints-1].y + ")";
            else 
               shp=this.aPRLPoints[this.nPRLPoints-1].x+"+"+this.aPRLPoints[this.nPRLPoints-1].y;
        break;
        case rltLine:
            var shp = "";
            if (this.useWKT)
            {  shp = "LINESTRING(";
            }
            for (var i=0;i<this.nPRLPoints-1;i++)
            {
                if (this.useWKT)
                   shp+=this.aPRLPoints[i].x+" "+this.aPRLPoints[i].y+",";
                else
                   shp+=this.aPRLPoints[i].x+"+"+this.aPRLPoints[i].y+"!";
            }
            if (this.useWKT)
                shp+= this.aPRLPoints[this.nPRLPoints-1].x+" "+this.aPRLPoints[this.nPRLPoints-1].y + ")";
            else    
                shp+=this.aPRLPoints[this.nPRLPoints-1].x+"+"+this.aPRLPoints[this.nPRLPoints-1].y;

        break;
    }
    theForm = getElement( this.form ) ;
    var params = "";
    for (var i=0; i<theForm.elements.length; i++)
    {
       var myElem = theForm.elements[i];
       if (myElem.name&&myElem.name.length>0)
       {
           var val = escape(myElem.value);
           if(val=='')
               val = ' ';
           params+= "&"+myElem.name+"="+val;
       }
    }
    showWaitingBox();
    setHelperframeSrc(this.url+shp+params+'&');
}

Redline.prototype.cancel = function()
{
    this.close();
    if (cbKort.lastMode<0)
        cbKort.setToolMode(tmZoomIn);
    else
        cbKort.setToolMode(cbKort.lastMode); 
}
Redline.prototype.resetTableCell = function()
{
    writeTableCellHTML(getElement(redlineObj.tableName),1,2,"");
}

function InitializeRedline(title, url, posX, posY, noBox)
{
    redlineObj = new Redline(title, url,"rlBox");
}

var redlineObj = null;

