/*	This Javascript is compatible with only the following browser versions **
** Internet Explorer - Versions 6.0,5.5,5.0
** Netscape navigator - Version 7.0 and later
** Mozilla Firefox - Versions 1.0 and later
**************************************************/
var images;
var defaultImages;

function DisplayPopUp()
{
    document.getElementById('popupControlWindow').style.display = "";
}
function HidePopUp()
{
    document.getElementById('popupControlWindow').style.display = 'none';
}
function AddLabel()
{
    document.getElementById('popupControlWindow').style.display = "";
    document.getElementById('popupContent').innerHTML="<TABLE width=\"95%\" cellpadding=\"2\" cellspacing=\"0\" border=\"0\"><TR><TD></br>Label &nbsp;&nbsp;<input type=text name=txtAddLabel></TD></TR></TABLE>";
    //add Message 
    document.getElementById('popupMessage').innerHTML="Click on map to add label";
     document.getElementById('popupWindowTitle').innerHTML="Add Label";
}

function HandleAddMyPointImages(obj)
{
    images=obj;
    
}

function HandleDefaultImage(obj)
{
    defaultImage=obj;
   HandleImages(); 
}

function HandleImages()
{
    var urls=images;
    var innerHTML="<TABLE width=\"95%\" cellpadding=\"2\" cellspacing=\"0\" border=\"0\"><TR><TD colspan=4></br>Label &nbsp;&nbsp;<input type=text name=txtAddLabel></TD></TR>";
    innerHTML+="<TR><TD colspan=4>Select Icon</TD></TR>";
    var temp=0;
   
    if (urls==null)
    {
        innerHTML="<TR><TD>No images found.</TD></TR></TABLE><BR />";
        document.getElementById('popupMessage').innerHTML=innerHTML;
    }
    else
    {
        for(i=0;i<urls.length;i++)
        {
            if(i%4==0)
            {
                temp=i;
                innerHTML+="<TR>";
            }
            innerHTML+="<td nowrap><input type=radio name=imageselector value='"+urls[i]+"' ";
            if(defaultImage && defaultImage==urls[i])
            {
                innerHTML+=" checked ";
            }
            innerHTML+="><IMG src='"+urls[i]+"'/></td>";
            if(i==(temp+4))
            {
                innerHTML+="</TR>";
            }
        }
    
    
    innerHTML+="</TABLE>";
    document.getElementById('popupContent').innerHTML = innerHTML;
    //add message
    document.getElementById('popupMessage').innerHTML="Click on map to add label and image";
   
   }//End of Else 


}

//////function AddMyPoint()
//////{
//////    
//////    if(!sessionExpired)
//////    {
//////        document.getElementById('popupControlWindow').style.display = "";
//////        document.getElementById('popupWindowTitle').innerHTML="Add My Point";
//////        PageMethods.AddMyPointImages(HandleAddMyPointImages);
//////        PageMethods.AddMyPointDefaultImage(HandleDefaultImage);
//////   }
//////   else
//////	    {
//////	        HandleSessionExpired();
//////	    }
//////}
//////function GetLabel()
//////{   
//////    
//////    if(!sessionExpired)
//////    {
//////        if(document.getElementById('txtAddLabel'))
//////        {
//////   	        PageMethods.AddLabel(mouseX , mouseY, document.getElementById('txtAddLabel').value,SetMapImages);
//////   	        HidePopUp();
//////        }
//////    }
//////    else
//////    {
//////        HandleSessionExpired();
//////    }
//////}
//////function GetAddMyPointAttr()
//////{
//////   
//////    if(!sessionExpired)
//////    {
//////        if(document.getElementById('txtAddLabel'))
//////        {
//////		    var imageURL="-1";
//////		    var x=document.getElementsByName("imageselector");
//////		    for (i=0;i<x.length;i++) 
//////		    {
//////                if (x[i].checked) 
//////                {
//////                    imageURL= x[i].value;
//////                    break;
//////                }
//////            }
//////		    PageMethods.AddMyPoint(mouseX , mouseY, document.getElementById('txtAddLabel').value,imageURL,SetMapImages);
//////		    HidePopUp();
//////        }
//////    }
//////    else
//////    {
//////        HandleSessionExpired();
//////    }
//////}
function SetRedliningMode(mode)
{
    if(window.autoPostBack)
    {   
        document.forms[0].elements['redliningButton'].value=mode;
    }
}


function SetRedliningSymbol()
{
/*var temp="CMD=GET_REDLINING_SYMBOL&drawmode=" + currentDrawMode;
  XMLHttpHandler(postBackUrl,'',temp,SetRedliningDrawProperty);
*/
  return true;
}
function SetRedliningDrawProperty()
{
    if(xmlHttp.readyState==4)
	{
		hideLoading();
		if(xmlHttp.status==200)
		{
			var responseXMLObj = xmlHttp.responseXML;	
			if(responseXMLObj)
			{
			    var lineWidthNode = responseXMLObj.getElementsByTagName("LINEWIDTH");
			    var lineColorNode = responseXMLObj.getElementsByTagName("LINECOLOR");
			    var lineStyleNode = responseXMLObj.getElementsByTagName("LINESTYLE");
			    var fillColorNode = responseXMLObj.getElementsByTagName("FILLCOLOR");
			    var fillOpacityNode = responseXMLObj.getElementsByTagName("FILLOPACITY");
			    var fillTypeNode = responseXMLObj.getElementsByTagName("FILLTYPE");
			    var markerTypeNode = responseXMLObj.getElementsByTagName("MARKERTYPE");
			    var fillcolor;
                if (lineWidthNode != null && lineWidthNode.length > 0)
                {
                    var temp=lineWidthNode.item(0).text;
                    if(temp!=null && temp.length>0)
                        SetLineWidth(temp);
                }
                if (lineColorNode != null && lineColorNode.length > 0)
                {
                    var temp=lineColorNode.item(0).text;
                    if(temp!=null && temp.length>0)
    	                SetLineColor(temp);
                }
                if (lineStyleNode != null && lineStyleNode.length > 0)
                {
	                var temp=lineStyleNode.item(0).text;
                    if(temp!=null && temp.length>0)                    
	                    lineStyle=temp;
                }
                if (fillColorNode != null && fillColorNode.length > 0)
                {
                    fillcolor=fillColorNode.item(0).text;
                    var opacity=0;
                    var fillStyle='Solid';
                    if (fillOpacityNode != null && fillOpacityNode.length > 0)
                    {
                        opacity=fillOpacityNode.item(0).text;
                    }
                    if (fillTypeNode != null && fillTypeNode.length > 0)
                    {
                        fillStyle=fillTypeNode.item(0).text;
                    }
                    if(temp!=null && temp.length>0)
	                    SetFillStyle(true, temp, opacity, fillStyle) // allowFill=true/false, color='#FF0000' or 'Red', style='Solid', opacity=0 to 1.0


                }
               
                if (markerTypeNode != null && markerTypeNode.length > 0)
                {
                    var temp=markerTypeNode.item(0).text;
                    if(temp!=null && temp.length>0)
                    {
                        switch(temp)
                        {
                            case "CIRCLE":
                            case "circle": 
                                SetMarkerStyle(fillcolor , PointType.CIRCLE)
                                break;
                            case "TRIANGLE":
                            case "triangle":  
                                SetMarkerStyle(fillcolor , PointType.TRIANGLE)
                                break;
                            case "STAR":
                            case "star":  
                                SetMarkerStyle(fillcolor , PointType.STAR)
                                break;
                            case "CROSS":
                            case "cross":  
                                SetMarkerStyle(fillcolor , PointType.CROSS)
                                break;
                            case "SQUARE":
                            case "square":  
                                SetMarkerStyle(fillcolor , PointType.SQUARE)
                                break;
                        }                        
                    }
                    else
                    {
                        SetMarkerStyle(fillcolor); 
                    }
                }
            }
         }
    }                    
}


/* function ShowSymbolEditor ( string visibleValue )
   show symbol editor dialog if visible value flag is 'visible', else hide it if 'hidden'
*/
function ShowRedliningEditor(visibleValue)
{
  var elem = null;
  var evnt = null;
  var posX = 0, posY = 0;
  // netscape or firefox
  if (window.Event)
  {
    evnt = window.Event;
    elem = evnt.target;
    posX = evnt.pageX;
    posY = evnt.PageY;
  }
  else
  {
    evnt = event;
    elem = event.srcElement;
    posX = evnt.clientX;
    posY = evnt.clientY;
  }
 
  var redLiningEditorElem = document.getElementById("ctrlRedLiningEditor");
  //var IfrRef = document.getElementById('RedLiningEditorDivShim');
  if (redLiningEditorElem != null)
  {
    if (visibleValue == 'hidden')
    {
      redLiningEditorElem.style.display = 'none';
      redLiningEditorElem.style.visibility = 'hidden';
      //IfrRef.style.visibility = 'hidden';
      //IfrRef.style.display = 'none';
    }
    else
    {
   
      redLiningEditorElem.style.pixelLeft = posX;
      redLiningEditorElem.style.pixelTop = posY;
      redLiningEditorElem.style.visibility = 'visible';
      redLiningEditorElem.style.display = 'block';
      //redLiningEditorElem.style.position = 'absolute';

      //IfrRef.style.width = redLiningEditorElem.offsetWidth;
      //IfrRef.style.height = redLiningEditorElem.offsetHeight;
      //IfrRef.style.top = redLiningEditorElem.style.top;
      //IfrRef.style.left = redLiningEditorElem.style.left;
      //IfrRef.style.visibility = 'visible';
      //IfrRef.style.display = "block";
    }
  }
  
  return false;
}
function RedliningOver()
{
    if (window.autoPostBack && window.m_lastActiveToolBar)
    {   
        window.m_lastActiveToolBar.Click("ApplyAddRedlining");
    }
}

function GetSelectionShape(id)
{

    // To go to server using AJAX - use function XMLHttpHandler
    // On server in JSPostBackHandler.cs, evaluete CMD and get selection object from current maptab's active selectionset 
    // Convert geometry this to image coordinates - Maptab.FromMapPoint
    // stream back ccordinates
    // In call back function create a Shape Object using these coordinates 
    // Add shape to shape collection and call shpaecollection.Refresh  - This should ideally render geometry on map (VML in IE)
    // EValute ShapeCollection.Refresh to see code for movalbe  shape can be integrated for Netscape 
//     var temp="CMD=GET_SELECTION_SHAPE&id=" + id;
//     XMLHttpHandler(postBackUrl,'',temp,DrawSelectionShape);
//     return true;
    
    if(!sessionExpired)
    {
        
         PageMethods.GetObjectGeometry(id, DrawSelectionShape);
    }
    else
    {
        HandleSessionExpired();
    }
}	
	
function DrawSelectionShape(obj)
{
       
	hideLoading();    
     if(obj!=null)
     {
        if(obj.error == null)
        {
            var xmlStr = obj;	  
            
            if(xmlStr!=null && xmlStr.length>0)
            {
                if(isIE){
            	    objXmlDoc = new ActiveXObject("Msxml2.DOMDocument");
            	    objXmlDoc.loadXML(xmlStr);
                }
                else 
                {
                    objXmlDoc = new DOMParser().parseFromString(xmlStr, "text/xml");                    
                }

		        var geometryNode = objXmlDoc.getElementsByTagName("GEOMETRY");
                if (geometryNode != null && geometryNode.length > 0)
                {
            	    DrawShapesFromXml(geometryNode);              
            	    
                    try{
                    if (document.getElementById(_VertexToolbarPan)!=null)
                        {
                           
                        if(window.ShowLayer)
                        {
                         ShowLayer(_VertexToolbarPan, null, null);
                        }
                        else
                        {
                            document.getElementById(_VertexToolbarPan).style.display='block';
                        }
                        }
                       }catch(e){} 
                    
                    HideToolBar('EDITING_VERTEX_TOOLS');
                    
                    ShowToolBar('VERTEX_TOOLS');               	      		                
                }
                else
                {
                    var errorNode = objXmlDoc.getElementsByTagName("ERROR");
                    if (errorNode != null && errorNode.length > 0)
                    {
                        var message = errorNode.item(0).getAttribute("message");
                        if(message!=null && message.length>0)
                        {
                            alert(message);
                        }
                    }                    
                }  
            }          
        }  
        else
        {
            alert("Error from server :\n" + obj.error);
        }
    }
    else
        alert("Server not responding, Please try later");	
}



function ApplyVertexEdit()
{
    
    if(!sessionExpired)
    {
        
        if(currentRedlineShapeId)
        {
    	    var shp = shapesCol.GetLastShape();
    	    var pointsStr="";
		    if(shp != null)
		    {
		        if(shp.type == DrawMode.POLY_LINE || shp.type == DrawMode.POLYGON)
		        {
			        var tpoints = shp.points;
			        if(tpoints)
			        {
				        for(j=0; j<tpoints.length; j++)
				        {
				            if(j>0)
				                pointsStr+=",";
					        pointsStr+=tpoints[j].x+","+tpoints[j].y
				        }
			        }
			    }
			    else if(shp.type == DrawMode.LINE)
			    {
			        pointsStr=shp.xmin + "," + shp.ymin + "," + shp.xmax + "," + shp.ymax;
			    }
			    
			    PageMethods.ApplyAddVertex(pointsStr,currentRedlineShapeId,onAddVertex);
			    
		    }	
	    }
	    else
	    {
	        alert("Selection is null.");
	    }
    }
    else
    {
        HandleSessionExpired();
    }
}

function onAddVertex(resp)
{
	SetMapImages(resp);
	RemoveAllShapes();
	//close apply panel   
	HideLayer(_VertexToolbarPan);
	ClickTool('ZoomIn');
}

function ModeAddRedliningPolygon()
{
    CurrentRedliningMode('AddRedliningPolygon');
}
function ModeAddRedliningPoint()
{
     CurrentRedliningMode('AddRedliningPoint');
}
function ModeAddRedliningLine()
{
     CurrentRedliningMode('AddRedliningLine');
}
function ModeAddRedliningPolyLine()
{
     CurrentRedliningMode('AddRedliningPolyLine');
}

function ModeAddRedliningCircle()
{
     CurrentRedliningMode('AddRedliningCircle');
}
function ModeAddRedliningRectangle()
{
     CurrentRedliningMode('AddRedliningRectangle');
}

function CurrentRedliningMode(mode)
{
    document.forms[0].__EVENTTARGET.value = "tb_REDLININGTOOLS_APPLY";
    document.forms[0].elements['redliningMode'].value="true";
    if(window.autoPostBack)
    {
        document.forms[0].elements['redliningButton'].value=mode;
    }
}
var currentRedlineShapeId;
function EditRedliningVertex(id , itemId)
{
      
    currentRedlineShapeId=itemId;
    SetDrawMode(DrawMode.EDIT);
    
    GetSelectionShape(itemId);
}

function SetRedliningTool(toolName)
{   
	document.forms[0].elements['redliningTool'].value=toolName;    
}
