var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;
var algor = 0;
var func="";
var markupObjects = new Object();
var currentMarkUpObject = null;
var dragobj = null;
var editobj = null;
var path="";
var firstclick=true;
var layvls=null;
var im = null;
var coloropener="";
var secx=0;
var secy=0;
var lay=null;
var tst=null; 
var userid =null;
var isAuthenticated = false;


//____Added for layout tools___________________________________________________________
    //
    var layout={};
    var layoutIsDirty = false;
    var layoutLoaded = false;
    var currentLayoutId ="";
    var currentLayoutName ="";
    var currentUserID = null;
    var layoutOwnerID =null;
    var reopen = true;
//_______________________________________________________________   
 
 
 
 
//init function sets all the event handlers for the individual div's
function layoutinit()
{
tst= document.getElementById('test');

spans = document.getElementsByTagName('label');
for(i=0;i<spans.length;i++){
     spans[i].elementType="label";
     spans[i].onmousedown = editObject;
     spans[i].style.cursor = "hand";
    }  
  im = document.getElementById('mapImage')
  lay = document.getElementById('layouts')
}

function checkMarkups(vertMap, markupObjects,offset)
{
	
	// This function runs after every map refresh to determin if layouts should be added to the mapframe
		offset = offset*1;
		clearMarkups();
		if(lay!=null)
		{
		lay.style.top = 0;
		lay.style.left = 0;
		}
		
		for(i in markupObjects)
		{
		
		 if(markupObjects[i]!=null && markupObjects[i].objtype!=null)
		 {
			
			
		  if(markupObjects[i].objtype=="callout")
		  {	  
		 	 var vc = new symbol.vCallout();
		 	 vc = markupObjects[i];
			 var pt = vertMap.toViewPoint(vc.insertx,vc.inserty)	
         	 layvls = new symbol.callout(vc.txt,vc.bgColor,vc.fontColor,vc.fontSize,vc.fontBold,pt.X,pt.Y,vertMap,false);
        	 lay.appendChild(layvls.group,im);
        	 layvls.group.id = vc.id;
        	 layvls.build(vc.offsets);
		  }
		  
		  else if(markupObjects[i].objtype=="shape" && markupObjects[i].elementType=="rectangle")
		  {
			 //alert() 
			 var vc = new symbol.vRectangle();
			 vc = markupObjects[i];
			 var pt = vertMap.toViewPoint(vc.insertx,vc.inserty)		
        	 layvls = new symbol.LayoutRectangle(vc.strokeWeight,vc.strokeColor,vc.fillColor,vc.fillOpacity);
			 var pt1 = vertMap.toViewPoint(vc.Ax,vc.Ay);
			 var pt2 = vertMap.toViewPoint(vc.Bx,vc.By);
			 layvls.shape.path = "m" +  pt1.X+","+ (pt1.Y+offset)+" l"+  pt1.X +","+(pt2.Y+offset)+" "+pt2.X+","+(pt2.Y+offset)+" "+pt2.X+","+(pt1.Y+offset)+"xe";
			 layvls.shape.id = vc.id;
        	 lay.appendChild(layvls.shape);
        	
		  }
		  else if(markupObjects[i].objtype=="label" && markupObjects[i].elementType=="label")
		  {		
			 var vc = new symbol.vLabel();
			 vc = markupObjects[i];
			 var pt = vertMap.toViewPoint(vc.Ax,vc.Ay)	 
		  	 var lbl = new symbol.LayoutLabel(vc.fontSize,vc.fontColor,vc.txt,vc.bgColor,vc.width,vc.fontBold,pt.X,(pt.Y+offset)); 
		  	 
		  	 lbl.txt.id = vc.id;
			 lay.appendChild(lbl.txt);  
			  
		  }
		  
		 else if(markupObjects[i].objtype=="shape" && markupObjects[i].elementType=="circle")
		  {
			  
			 var vc = new symbol.vCircle();
			 vc = markupObjects[i];	
        	 layvls = new symbol.LayoutCircle(vc.strokeWeight,vc.strokeColor,vc.fillColor,vc.fillOpacity);
			 var pt1 = vertMap.toViewPoint(vc.Ax,vc.Ay);
			  layvls.circle.style.top = (pt1.Y+offset);
              layvls.circle.style.left= pt1.X;
			  layvls.circle.style.width = vertMap.toViewLength(vc.width);
			  layvls.circle.style.height = vertMap.toViewLength(vc.height);
			  layvls.circle.id = vc.id;
        	 lay.appendChild(layvls.circle);
        	
		  }
		 else if(markupObjects[i].objtype=="shape" && markupObjects[i].elementType=="line")
		  {
			  
			// var vc = new symbol.vLine();
			var vc = markupObjects[i];	
        	 layvls = new symbol.LayoutLine(vc.strokeWeight,vc.strokeColor);
			 var pt1 = vertMap.toViewPoint(vc.points[0].X,vc.points[0].Y);
			 layvls.line.path = "m" + pt1.X+","+(pt1.Y+offset) +" l";
			 for(var i = 1;i<vc.points.length;i++)
			 {	
				 var pt = vertMap.toViewPoint(vc.points[i].X, vc.points[i].Y);
				 layvls.line.path += pt.X+","+(pt.Y+offset);
				 if(i<vc.points.length-1)
				 {
					 layvls.line.path+=",";
				 }
			 }
			 layvls.line.path +=" e";
			 layvls.line.id = vc.id;
        	 lay.appendChild(layvls.line);
        	
		  }
		  
		 else  if(markupObjects[i].objtype=="shape" && markupObjects[i].elementType=="polygon")
		  {
			  
			var vc = new symbol.vPolygon();
			vc = markupObjects[i];	
        	 layvls = new symbol.LayoutPolygon(vc.strokeWeight,vc.strokeColor,vc.fillColor,vc.fillOpacity);
			 var pt1 = vertMap.toViewPoint(vc.points[0].X,vc.points[0].Y);
			 layvls.polygon.path = "m" + pt1.X+","+(pt1.Y+offset) +" l";
			 for(var i = 1;i<vc.points.length;i++)
			 {	
				 var pt = vertMap.toViewPoint(vc.points[i].X, vc.points[i].Y);
				 layvls.polygon.path += pt.X+","+(pt.Y+offset);
				 if(i<vc.points.length-1)
				 {
					 layvls.polygon.path+=",";
				 }
			 }
			 layvls.polygon.path +=" xe";
			 layvls.polygon.id = vc.id;
        	 lay.appendChild(layvls.polygon);
        	
		  }
		  }
		}
	//}
	//top.dataframe.resetTools();
         func="";
}

//function the remove all markup objects from the screen
function clearMarkups()
{
	var map = document.getElementById("layouts");
	var elements = map.childNodes;
	var num =elements.length; 
	
	for(var i=num-1;i>=0;i--)
	{		
		map.removeChild(elements[i]);
	}
	
	
}

function falsefunc() { return false; } // used to block cascading events



function layout_getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
     
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft
      mousey = e.clientY + document.body.scrollTop;
      
     
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }  
  }
}

//Grab an element to move it-----------------------------------------
function layout_grab()
{
    if(parent.currenttool == "markup"&& func=="select")
	  {	
        layout_getMouseXY();
         document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
         dragobj = this;
         //check to see if the dragobject is the map image
         
         if(dragobj==im)
         {   
             editobj=null;
         }
        else //if not then set the current object equal to the drag object
         {
             top.dataframe.clearProps();
             currentMarkUpObject = dragobj.id;
             layout_editObject(dragobj); 
         }

        dragobj.style.zIndex = 10; // move it to the top
        document.onmousemove = layout_drag;
        document.onmouseup = layout_drdrop;
        grabx = mousex;
        graby = mousey;
        
        if(dragobj.objtype!="shape"||dragobj.elementType=="label")
        {
            elex = orix = dragobj.offsetLeft;
           eley = oriy = dragobj.offsetTop;
         }
         else if(dragobj.elementType=="circle")
         {
            orix=dragobj.style.left.substring(0,dragobj.style.left.indexOf("px"));
            oriy=dragobj.style.top.substring(0,dragobj.style.top.indexOf("px"));
         }
         else
         {
             orix=dragobj.coordorigin.x;
             oriy=dragobj.coordorigin.y;
         } 
  
	}
	
}
//Drag the selected element------------------------------------------
function layout_drag(e) // parameter passing is important for NS family 
{

	
  if (dragobj)
  {
    if(dragobj.objtype!="shape"){
	
        if(dragobj.objtype=="callout")
		{
            var anchor =false;
              if(anchorclick==1)
			  {
                 anchor=true;
              }
           layout_getMouseXY(e)
            eley =mousey-(graby-oriy);
            elex=mousex-(grabx-orix);
           elex = parseInt(orix) + (mousex-grabx)-5;
           eley = parseInt(oriy) + (mousey-graby);
           dragobj.style.position = "absolute";    
           dragobj.drag(elex,eley,anchor);
        }
		else      
        {
         layout_getMouseXY(e)
         elex = orix + (mousex-grabx);
         eley = oriy + (mousey-graby);
         dragobj.style.position = "absolute";
         dragobj.style.left = (elex).toString(10) + 'px';
         dragobj.style.top  = (eley).toString(10) + 'px';
         }
	}
	else
	{
	   
	    
	    layout_getMouseXY(e)
        elex = orix - (mousex-grabx);
        eley = oriy - (mousey-graby);
        
        if(dragobj.elementType=="circle")
	         {
	       
	          dragobj.style.top =mousey-(graby-oriy);
              dragobj.style.left= mousex-(grabx-orix);
              }
       else
             {
       
             dragobj.coordorigin=elex+","+eley
              }

	    }
  }
	
  return false; 
} 


//Drop the element being moved---------------------------------------
function layout_drdrop(e)
{
        
        
  if (dragobj)
  {
  
  
        layout_getMouseXY(e)
        var markup = markupObjects[dragobj.id];
        if(markup.objtype=="shape")
          {
                  elex = orix - (mousex-grabx);
                  eley = oriy - (mousey-graby);
                  var wx = vMap.toWorldLength(elex);
                  var wy = vMap.toWorldLength(eley);
        
        
                 if(markup.elementType=="circle")
                     {
                      var pt = new geo.vPoint();
                      pt = vMap.toWorldPoint(dragobj.style.left.substring(0,dragobj.style.left.indexOf("px")),dragobj.style.top.substring(0,dragobj.style.top.indexOf("px")))
                        markup.Ax = pt.X;//markup.Ax - (wx);
                        markup.Ay = pt.Y;//markup.Ay + (wy);
                     }
                else if(markup.elementType=="rectangle")
                     { 
                        
                        markup.Ax = markup.Ax*1 - (wx*1);
                        markup.Ay = markup.Ay*1 + (wy*1);
                        markup.Bx = markup.Bx*1 - (wx*1);
                        markup.By = markup.By*1+ (wy*1);
                        
                    }
                    
                 else if(markup.elementType=="line")
                    { 
                      for(var i =0;i<markup.points.length;i++)
                        {
                           markup.points[i].X = markup.points[i].X*1 - wx*1
                           markup.points[i].Y = markup.points[i].Y*1 + wy*1
                        }
                     }
              
                  else if(markup.elementType=="polygon")
                     { 
                         for(var i =0;i<markup.points.length;i++)
                           {
                            markup.points[i].X = markup.points[i].X*1 - wx*1
                            markup.points[i].Y = markup.points[i].Y*1 + wy*1
                           }
                     }
              
              
            }
         else if(markup.objtype=="label"&& markup.elementType=="label")
              { 
                 elex = orix + (mousex-grabx);
                  eley = oriy + (mousey-graby);
                var pt = vMap.toWorldPoint(elex,eley-45);
                   markup.Ax = pt.X;
                   markup.Ay = pt.Y;
                 //alert(markup.Ax)
                 }
              
     if(markup.objtype=="callout")
	 {
        //dragobj.updatePosition();
       
     }
	 
    dragobj.style.zIndex = 25;
	//updateMarkUpObject();
    dragobj = null;
	
    
  }


  document.onmouseup = null;
  document.onmousedown = null;   // re-enables text selection on NS
 
  if(layoutLoaded ==true)
        { 
            layoutIsDirty = true;
          showUpdate();
        }
  
}

//Object Editor---------------------------------------
function layout_editObject(obj)
{//alert("edit Object called")
  layout_getMouseXY()
  
 if(editobj && editobj.style){
  editobj.style.border ="none"
  
     if(editobj.polygon){
      editobj.polygon.style.border ="none"
      }
     if(editobj.insertpoint){
     editobj.insertpoint.style.display="none";
    }
     
  }
 
  if(!obj){
  editobj=this;
  }else{
  editobj = obj;
  }

   
    if(editobj.elementType=="label"){
    top.dataframe.arrProps['label'].style.display="block";
     var tbox = top.dataframe.document.getElementById('txtedit');
     editobj.style.border ="dotted 1px red"
     tbox.value = editobj.innerHTML;
    }
    
     if(editobj.elementType=="callout"){
      top.dataframe.arrProps['callout'].style.display="block";
     var tbox = top.dataframe.document.getElementById('txtcalltext');
     editobj.polygon.style.border ="dotted 1px red"
     tbox.value = editobj.txt.innerText;
     editobj.insertpoint.style.display="block";
    }
    
    if(editobj.elementType=="line"){
     top.dataframe.arrProps['line'].style.display="block";
    editobj.style.border ="dotted 1px red"
    }
   if(editobj.elementType=="polygon"){
    //alert(editobj.elementType)
      top.dataframe.arrProps['polygon'].style.display="block";
    editobj.style.border ="dotted 1px red"
    }
    if(editobj.elementType=="rectangle"){
    //alert(editobj.elementType)
      top.dataframe.arrProps['rectangle'].style.display="block";
    editobj.style.border ="dotted 1px red"
    }
    if(editobj.elementType=="circle"){
    //alert(editobj.elementType)
      top.dataframe.arrProps['circle'].style.display="block";
    editobj.style.border ="dotted 1px red"
    }
    
    if(layoutLoaded ==true)
        {
        layoutIsDirty = true;
        showUpdate();
        }
}




  
  //This is the mousedown event handler.------------------
  function layout_checkButton(e)
  {
	  
 	 if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
	 if (e.target) targ = e.target;
	 else if (e.srcElement) targ = e.srcElement;
  	if (e)
 	 { 	 	
  			if(targ.id==im.id ||targ.id=="mapImage")//check to see if we clicked on the image
			{
				
 			  if(editobj && editobj.style)//check to see if there is a current editobject and that it has a style proporty
			  	{	 
					
					//turn off the dashed border around the object.
  					 editobj.style.border ="none";
  					 if(editobj.polygon)
					 {
      					editobj.polygon.style.border ="none"
     				 }
    				 if(editobj.insertpoint)
					 {
     					editobj.insertpoint.style.display="none";
    				 }  
  					
  				}
 		 	}


    layout_getMouseXY()
	
     //------------------------------------------------------------------        
        if(func=='addtext'||func=='addcallout')//check if the current function is is either adding text or a callout
		{
            layout_addObject(e)//if so call the add object function
        }
               
        //------------------------------------------------------------------        
        if(func=='addline')//if the function is a line call then we need to start drawing the line
		{
                
              
                if(firstclick)//check to see if it is the first point of the line
				{ 
                    layout_addObject();//if so then call the addobject function to create a new line object
             	    path +="m "+(mousex)+","+(mousey) +" l";//start the vml path object
		      	    firstclick=false;			  //reset the firstclick
		      	    im.onmousemove = layout_drawElement; //set the mousemove eventhandler to the layout_drawElement function
		      	    layvls.line.path = path;
		      	  
	            }
	            else
	            {
                     path += " "+mousex+","+mousey;
	                 layvls.line.path = path;
	            }

	     }
	     
	     //-----------------------------------------------------------------  
	   if(func=='addpolygon')
	   {
   
                if(firstclick)
                {
                layout_addObject()
                layvls.draghandlex = mousex;
                layvls.draghandley = mousey;
                path +="m "+mousex+","+mousey +" l";
		        firstclick=false;
		        document.getElementById('mapImage').onmousemove = layout_drawElement;
		        layvls.polygon.path = path;
	            }
	            else
	            {
                path += " "+mousex+","+mousey;
	            layvls.polygon.path = path;
                }

	    }
	        
	         //-----------------------------------------------------------------  
	        
	   
	     if(func=='addrectangle')
	     {
	      
             if(firstclick)
             { 
               layout_addObject()
	             secx = mousex;
                 secy = mousey;
                 firstclick=false;
                document.getElementById('mapImage').onmousemove = layout_drawElement;        
	            }
	         else
			 {
				 
	               
	                this.onmousemove = null; 
                    layvls.shape.path = "m" +  secx+","+ secy+" l"+  secx +","+mousey+" "+mousex+","+mousey+" "+mousex+","+secy+"xe";
                    func="";
                    layvls.shape.style.cursor="hand";
                    im.style.cursor="none";
                    //top.dataframe.resetTools();
                    firstclick=true;
                    
                    //create the vertual object----------------------------------------
                   var vrect = new symbol.vRectangle();
				   vrect.fillColor = String(layvls.fill.color);
				   vrect.fillOpacity = layvls.fill.opacity;
				   vrect.strokeColor = String(layvls.shape.strokecolor);
				   vrect.strokeWeight = layvls.shape.strokeweight;  
				   var pt1 = vMap.toWorldPoint(secx,(secy-45));
				   vrect.Ax = pt1.X;
				   vrect.Ay = pt1.Y;
				   var pt2 = vMap.toWorldPoint(mousex,(mousey-45));
				   vrect.Bx = pt2.X;
				   vrect.By = pt2.Y;
				   vrect.id = "rectangle"+ vrect.Ax + vrect.Ay;
				   layvls.shape.id = "rectangle"+ vrect.Ax + vrect.Ay;
				   markupObjects[vrect.id] = vrect;
				      
     
	             }
	        }
	           
	           
	          //-----------------------------------------------------------------  
	        if(func=='addcircle')
	        {
	      
             if(firstclick)
             { 
                 layout_addObject()
	             grabx = mousex;
                 graby = mousey;
                 firstclick=false;
                 document.getElementById('mapImage').onmousemove = layout_drawElement;
                 layvls.circle.style.top = graby;
                 layvls.circle.style.left= grabx;
               
	            }
	         else
	         {     
	                this.onmousemove = null; 
                    layvls.circle.style.height =Math.abs(graby - mousey);
                    layvls.circle.style.width=Math.abs(grabx- mousex) ;
                    func="";
                    layvls.circle.style.cursor="hand";
                    im.style.cursor="none";
                    firstclick=true;
                    //top.dataframe.resetTools(); 
                    
                    
					//Create the vertual element--------------------------
					var vc = new symbol.vCircle();
					var pt = vMap.toWorldPoint(grabx,(graby-45))
					 vc.id = "circle"+pt.X+pt.Y;
					 layvls.circle.id = "circle"+pt.X+pt.Y;
					 vc.fillColor = String(layvls.fill.color);
					 vc.fillOpacity = layvls.fill.opacity;
	 				 vc.strokeColor = String(layvls.circle.strokecolor);
	 				 vc.strokeWeight = layvls.circle.strokeweight;
	 				 vc.Ax = pt.X;
	 				 vc.Ay = pt.Y;
					 var pt2 = vMap.toWorldPoint(mousex,(mousey-45))
	 				 vc.width = Math.abs((pt.X - pt2.X));
					 vc.height = Math.abs((pt.Y - pt2.Y));
					 markupObjects[vc.id] = vc;
				}
	        }
	         
	        

	    }
    
   
        if(layoutLoaded ==true)
        {
        layoutIsDirty = true;
        showUpdate();
        }

  }
  
 
  
  
  
  //function to add new elements to the layout-----------------------------------------------
  function layout_addObject(){
 //var im = document.getElementById('image');
   layout_getMouseXY()
  
   //Add a label object-------------------------------------------------
   if(func=='addtext'){
          //var mapImage = obj;
		    var txtsize = top.dataframe.document.getElementById('seltextsize').value;
     		var txtcolor = top.dataframe.document.getElementById('seltextcolor').style.backgroundColor;
     		var labelText = top.dataframe.document.getElementById('txtedit').value;
    		var txtbgcolor = top.dataframe.document.getElementById('selbgcolor').style.backgroundColor;
    		var txtwidth = top.dataframe.document.getElementById('selwidth').value;
			var txtbold;
          if(top.dataframe.document.getElementById('chktextbold').checked ==true)
		  {
         	 txtbold = "bold";
          }
          else
		  {
			  txtbold = "normal";
          }
          var obj = document.getElementById('mapImage');
          var lbl = new symbol.LayoutLabel(txtsize,txtcolor,labelText,txtbgcolor,txtwidth,txtbold,mousex,mousey);
		  
		 lay.appendChild(lbl.txt);  
     	 //top.dataframe.resetTools()
         //func="";
		 //build the vertual label--------------------------
		 var vc = new symbol.vLabel();
		 var pt = vMap.toWorldPoint(mousex,(mousey-45));
		 vc.id = "label"+pt.X+pt.Y;
		 vc.txt = labelText;
		 vc.bgColor = txtbgcolor;
		 vc.fontColor = txtcolor;
		 vc.fontSize = txtsize;
		 vc.fontBold = txtbold;
		 vc.width = txtwidth;
		 vc.Ax = pt.X;
		 vc.Ay = pt.Y;
		 lbl.txt.id = vc.id
	 	 markupObjects[vc.id] = vc;
	 	 
           

        }
        
        //Add a callout object-------------------------------------------------
        if(func=='addcallout'){
        var vText = top.dataframe.document.getElementById('txtcalltext').value;
        var vbgcolor = top.dataframe.document.getElementById('txtcallbackcolor').style.backgroundColor;
        var vfontcolor = top.dataframe.document.getElementById('txtcallfontcolor').style.backgroundColor;
        var vfontsize = top.dataframe.document.getElementById('selcallfontsize').value;
        var vfontbold = top.dataframe.document.getElementById('Chkcallbold').value;      
        layvls = new symbol.callout(vText,vbgcolor,vfontcolor,vfontsize,vfontbold,mousex,mousey,vMap,true);
        lay.appendChild(layvls.group,im);
        layvls.build();
        //top.dataframe.resetTools();
        func="";
		//layvls.updateSize();
		  

     }  
        
         //Add a Line object-------------------------------------------------
        if(func=='addline')
		{
       		 if(layvls)//if we are here it is because we wanr to create a new line,so we need to clear the temp path
			 {
       			 layvls=null;
       		 }
			var wt = top.dataframe.document.getElementById('sellineweight').value;
       		var clr = top.dataframe.document.getElementById('sellinecolor').style.backgroundColor;
      		layvls = new symbol.LayoutLine(wt,clr);

      		lay.appendChild(layvls.line,im);//insert the line object into the map.
               

        }
         //Add a Rectangle object-------------------------------------------------
        if(func=='addrectangle'){
         if(layvls)
		 	{
               layvls=null;
            }
			var wt = top.dataframe.document.getElementById('seloutlineweight').value;
      		 var clr = top.dataframe.document.getElementById('seloutlinecolor').style.backgroundColor;
      		 var fclr = top.dataframe.document.getElementById('selfillcolor').style.backgroundColor;
      		 var ftrans = top.dataframe.document.getElementById('selfilltrans').value; 
       		layvls = new symbol.LayoutRectangle(wt,clr,fclr,ftrans);
			var pt1 = vMap.toWorldPoint(secx,(secy-45));
			layvls.shape.id = "rectangle"+ pt1.X +","+pt1.Y;
     		layvls.shape.onclick = layout_checkButton;	   
            lay.appendChild(layvls.shape,im);
              

        }
        
        //Add a Polygon object-------------------------------------------------
        if(func=='addpolygon'){
        if(layvls){
        layvls=null;
        }
		var wt = top.dataframe.document.getElementById('seloutlineweight').value;
       var clr = top.dataframe.document.getElementById('seloutlinecolor').style.backgroundColor;
       var fclr = top.dataframe.document.getElementById('selfillcolor').style.backgroundColor;
       var ftrans = top.dataframe.document.getElementById('selfilltrans').value;
       layvls = new symbol.LayoutPolygon(wt,clr,fclr,ftrans );
       lay.appendChild(layvls.polygon,im);
           

        }
        
         //Add a Circle object-------------------------------------------------
        if(func=='addcircle'){
        if(layvls){
        layvls=null;
        }
		var wt = top.dataframe.document.getElementById('seloutlineweight').value;
       var clr = top.dataframe.document.getElementById('seloutlinecolor').style.backgroundColor;
       var fclr = top.dataframe.document.getElementById('selfillcolor').style.backgroundColor;
       var ftrans = top.dataframe.document.getElementById('selfilltrans').value;
       layvls = new symbol.LayoutCircle(wt,clr,fclr,ftrans);
       lay.appendChild(layvls.circle,im);

        }
        
         if(layoutLoaded ==true)
        {
        layoutIsDirty = true;
        showUpdate();
        }
  } 
 
   

  
   //Event handler for the mouse move event - Draw the Elements--------------------------------------------
   function layout_drawElement(){
	   
     layout_getMouseXY()
     
     if(func=='addline'){
         if(firstclick==false){
         layvls.line.path = path + " "+mousex+","+mousey;
        }
     }
     
     if(func=='addrectangle'){
     if(firstclick==false){
    
    layvls.shape.path = "m" +  secx+","+ secy+" l"+  secx +","+mousey+" "+mousex+","+mousey+" "+mousex+","+secy+"xe";
        
     }
    }
    
    if(func=='addpolygon'){
         if(firstclick==false){
         layvls.polygon.path = path + " "+mousex+","+mousey;
        }
     }
     if(func=='addcircle'){
         if(firstclick==false){
         layvls.circle.style.height =Math.abs(graby - mousey);
         layvls.circle.style.width=Math.abs(grabx- mousex) ;
         
        }
     }
     
  }
  //function to handle the double click event----------------------------------
  
  function layout_doubleClick(e){
  
  
     if(func=='addline')
     {
        func=""
         layvls.line.style.cursor="hand";
            im.style.cursor="none";
        firstclick=true;
       	var vc = new symbol.vLine();
		vc.strokeColor = String(layvls.line.strokecolor);
		vc.strokeWeight = String(layvls.line.strokeweight);
		vc.points = symbol.parsePath(layvls.line.path,'line');		
		vc.id = "line"+vc.points[0].X+vc.points[0].Y;
		layvls.line.id = vc.id;
		markupObjects[vc.id] = vc;
       
    }
    
     if(func=='addpolygon')
     {
        func=""
        layvls.polygon.style.cursor="hand";
        im.style.cursor="none";
        firstclick=true;
		var vc = new symbol.vPolygon();
		vc.strokeColor = String(layvls.polygon.strokecolor);
		vc.strokeWeight = layvls.polygon.strokeweight;
		vc.fillColor= String(layvls.polygon.fillColor);
		vc.fillOpacity = layvls.polygon.fillOpacity;
		vc.points = symbol.parsePath(layvls.polygon.path,'polygon');
		vc.id = "polygon"+vc.points[0].X+vc.points[0].Y;
		layvls.polygon.id = vc.id;
		markupObjects[vc.id] = vc;
		layvls.polygon.path = path + " xe";
       
    }
   
 }
       
       //change object proporties---------------------------------
function changeproporties(obj)
{
   
    if(editobj){
    
    
        if(obj.id =="txtedit"){
         editobj.innerHTML = obj.value;
         //editobj.style.width = obj.value.length
         markupObjects[editobj.id].txt = obj.value;

        }
           
       if(obj.id =="seltextsize"){
         editobj.style.fontSize = obj.value;
          markupObjects[editobj.id].fontSize = obj.value;
        }
        if(obj.id =="seltextcolor"){
         editobj.style.color = obj.style.backgroundColor;
         markupObjects[editobj.id].fontColor = obj.style.backgroundColor;
        }
        if(obj.id =="selwidth"){
         editobj.style.width = obj.value;
        }
         if(obj.id =="selbgcolor"){
         editobj.style.backgroundColor = obj.style.backgroundColor;
         markupObjects[editobj.id].bgColor = obj.style.backgroundColor;

        }
        if(obj.id =="chktextbold"){
            if(obj.checked==true){
                 editobj.style.fontWeight = "bold";
                 markupObjects[editobj.id].fontBold = true;

              }
             else{
                editobj.style.fontWeight = "normal";
                markupObjects[editobj.id].fontBold = false;

               }
          }
          
         if(obj.id =="txtcalltext"){
            editobj.txt.font.innerText= obj.value;
            
            editobj.txtwidth= editobj.txt.font.offsetWidth;
            editobj.txtheight= editobj.txt.font.offsetHeight;
            editobj.updateSize();
            markupObjects[editobj.id].txt = obj.value;

           }
           
       if(obj.id =="selcallfontsize"){
         editobj.txt.font.style.fontSize = obj.value;
         editobj.txtwidth= editobj.txt.offsetWidth;
            editobj.txtheight= editobj.txt.offsetHeight;
            editobj.updateSize();
            markupObjects[editobj.id].fontSize = obj.value;

        
        }
        if(obj.id =="txtcallfontcolor"){
         editobj.txt.font.style.color = obj.style.backgroundColor;
        markupObjects[editobj.id].fontColor = obj.style.backgroundColor;

        }
        
         if(obj.id =="txtcallbackcolor"){
         editobj.fill.color = obj.style.backgroundColor;
        markupObjects[editobj.id].bgColor = obj.style.backgroundColor;

        }
        if(obj.id =="Chkcallbold"){
            if(obj.checked==true){
         
                 editobj.txt.font.style.fontWeight = "bold";
                  markupObjects[editobj.id].fontBold = true;
              }
             else{
                editobj.txt.font.style.fontWeight = "normal";
                 markupObjects[editobj.id].strokeWeight = false;
               }
          }
          
          
     if(obj.id =="sellineweight"){
        editobj.strokeweight = obj.value;
         markupObjects[editobj.id].strokeWeight = obj.value
        }
     if(obj.id =="sellinecolor"){
           
         editobj.strokecolor = obj.style.backgroundColor;
          markupObjects[editobj.id].strokeColor = obj.style.backgroundColor;
        }
            
     if(obj.id =="seloutlineweight"){
        editobj.strokeweight = obj.value;
       markupObjects[editobj.id].strokeWeight = obj.value; 

        }
     if(obj.id =="seloutlinecolor"){
         editobj.strokecolor = obj.style.backgroundColor;
         markupObjects[editobj.id].strokeColor = obj.style.backgroundColor; 
        }
      if(obj.id =="selfillcolor"){
         editobj.fill.color = obj.style.backgroundColor;
         markupObjects[editobj.id].fillColor = obj.style.backgroundColor;
        }
       if(obj.id =="selfilltrans"){
         editobj.fill.opacity = obj.value;
         markupObjects[editobj.id].fillOpacity = obj.value;
        }    
    
       } 
       
       if(layoutLoaded ==true)
        {
        layoutIsDirty = true;
        showUpdate();
        }
    }    
        
   // Function to rebuild the edited markupobject
  
  function showUpdate()
  {
   //top.dataframe.document.getElementById('btnupdatelayout').style.display = "block";
   }
   
   
   function deleteObject()
   { 
 
     if(editobj)
      {
       
	     if(editobj.elementType=="label"||editobj.elementType=="line"||editobj.elementType=="rectangle"||editobj.elementType=="polygon"||editobj.elementType=="circle"||editobj.elementType=="callout")
	          {
	          editobj.parentNode.removeChild(editobj)
	          markupObjects[editobj.id]=null;

	         }
	  }
   }
