//PSG Map Scripts
 document.namespaces.add('v', 'urn:schemas-microsoft-com:vml');
 var PSGMapCommand = '' 
 var PSGMapBehavior = '' 
 var PSGStartX = 0 
 var PSGStartY = 0  
 var PSGStopX = 0 
 var PSGStopY = 0  
 var PSGLineArrayX = new Array() 
 var PSGLineArrayY = new Array() 
 var PSGLinePos = 0 
 var PSGTrackStarted = false 
 var PSGCanvas  
 var PSGHiddenTextbox 
 var PSGPoints = '' 
 var PSGPoint1 = ''
 var PSGPoint2 = ''
 var PSGFirstX=0
 var PSGFirstY=0
 var PSGLastX=0
 var PSGLastY=0
 var PSGDone = false 
 var PSGControl 
 var PSGEnableLoadingTag = false 

function PSGOnButton(ButtonName)
{
	//document.Pointer.src = "buttons/Pointer-up.gif";    
	document.ZoomIn.src = "buttons/zoomin-up.gif";
	document.ZoomOut.src = "buttons/zoomout-up.gif";
	document.Pan.src = "buttons/pan-up.gif";
	document.Identify.src = "buttons/identify-up.gif";
	//document.Rectangle.src = "buttons/Rectangle-up.gif";
	//document.Polygon.src = "buttons/Polygon-up.gif";	
	//document.AddPoint.src = "buttons/AddPoint-up.gif";		
    switch (ButtonName){ 
 	case 'Pointer':  
//		document.Pointer.src = "buttons/Pointer-dn.gif";
        PSGMapCommand = 'TrackDefault '; 
        PSGMapBehavior = 'TrackDefault';   
//        MyClearCanvas(PSGCanvas);   		
 		break; 
 	case 'ZoomIn':  
		document.ZoomIn.src = "buttons/ZoomIn-dn.gif";
        PSGMapCommand = 'TrackZoomIn '; 
        PSGMapBehavior = 'TrackWireRectangle'; 
        document.MapForm.Cmd.value = 'ZOOMIN';		
 		break; 
 	case 'ZoomOut':  
		document.ZoomOut.src = "buttons/ZoomOut-dn.gif";
        PSGMapCommand = 'TrackZoomOut '; 
        PSGMapBehavior = 'TrackWireRectangle';  
        document.MapForm.Cmd.value = 'ZOOMOUT';  		
 		break; 
 	case 'Pan':  
		document.Pan.src = "buttons/Pan-dn.gif";
        PSGMapCommand = 'Pan '; 
        PSGMapBehavior = 'TrackPoint';  
        document.MapForm.Cmd.value = 'PAN';    		
 		break; 
 	case 'Identify':  
		document.Identify.src = "buttons/Identify-dn.gif";
        PSGMapCommand = 'TrackIdentify '; 
        PSGMapBehavior = 'TrackPoint';  
        document.MapForm.Cmd.value = 'ID'; 		
 		break; 
 	case 'Rectangle':  
		document.Rectangle.src = "buttons/Rectangle-dn.gif";
        PSGMapCommand = 'TrackRectangle '; 
        PSGMapBehavior = 'TrackRectangle'; 
        MyClearCanvas(PSGCanvas);   
        PSGPoint1='';
        PSGPoint2='';  		
 		break; 
 	case 'Polygon':  
		document.Polygon.src = "buttons/Polygon-dn.gif";
        PSGMapCommand = 'TrackPolygon '; 
        PSGMapBehavior = 'TrackPolygon';  
        MyClearCanvas(PSGCanvas);   		
 		break; 
 	case 'AddPoint':  
		document.AddPoint.src = "buttons/AddPoint-dn.gif";
        PSGMapCommand = 'TrackAddPoint '; 
        PSGMapBehavior = 'TrackAddPoint';  
        MyClearCanvas(PSGCanvas);   		
 		break; 
	default : alert('Unsupported Mode'); 
 	stop 
 	}		
		}	  

 function PSGInit(e) 
 { 
   PSGHiddenTextbox = e.srcElement.children(0); 
   if (PSGMapCommand  != '') {PSGHiddenTextbox.mode = PSGMapBehavior; } 
   PSGCanvas = e.srcElement; 
   PSGControl = PSGCanvas.parentElement;  
 } 


// function PSGSetTrack(Command) //Default, ZoomIn, ZoomOut, Pan, Identify, Rectangle, Point, AddPoint, Line, Polygon
 
function PSGGetCommand(Command)
{
    switch (Command){ 
 	case 'MAP':  
		PSGOnButton('Pointer');   		
 		break; 
 	case 'ZOOMIN':  
		PSGOnButton('ZoomIn');   		
 		break; 
 	case 'ZOOMOUT':  
		PSGOnButton('ZoomOut');   		
 		break; 
 	case 'PAN':  
		PSGOnButton('Pan');   		
 		break; 
 	case 'ID':  
		PSGOnButton('Identify');   		
 		break;  
	default : break;
 	}			
}

 function ThePSGMouseDown(e) 
 { 
   PSGInit(e);   
   switch (PSGHiddenTextbox.mode){ 
 	case 'TrackRectangle':  
 		PSGStartX = e.offsetX ; 
 		PSGStartY = e.offsetY ;
		PSGFirstX = PSGStartX;
		PSGFirstY = PSGStartY;
    	PSGPoint1= (PSGFirstX) + ';' + (PSGFirstY);
		while (g.rowCount>1) {g.delRow(g.rowCount-1, 1);}; 
		g.cells[g.rowCount-1][0].setData(); 
		g.cells[g.rowCount-1][1].setData(); g.paint();
		PSGAddPointToTable(0);			
 		break; 
 	case 'TrackWireRectangle':  
 		PSGStartX = e.offsetX ; 
 		PSGStartY = e.offsetY ; 
 		break; 		
 	case 'Pan':  
 		PSGStartX = e.offsetX ; 
 		PSGStartY = e.offsetY ; 
 		break; 
 	case 'TrackLine': 		 
 		PSGTrackStarted = true; 
 		PSGLineArrayX[PSGLinePos] = e.offsetX ;		 
 		PSGLineArrayY[PSGLinePos] = e.offsetY  ; 
 		PSGLinePos++; 
 		PSGPoints = PSGPoints + (e.offsetX) + ',' + (e.offsetY) + ' ';		 
 		PSGDrawPolyLine(PSGCanvas, PSGPoints, PSGLineArrayX, PSGLineArrayY); 
 		break; 
 	case 'TrackPolygon':  
 		PSGTrackStarted = true; 
 		PSGLineArrayX[PSGLinePos] = e.offsetX ;		 
 		PSGLineArrayY[PSGLinePos] = e.offsetY ; 
 		PSGLinePos++;		 
		if (PSGPoints == '')
		{while (g.rowCount>1) {g.delRow(g.rowCount-1, 1);}; 
		g.cells[g.rowCount-1][0].setData(); 
		g.cells[g.rowCount-1][1].setData(); g.paint();}  
		PSGPoints = PSGPoints + (e.offsetX) + ';' + (e.offsetY) + '\n';				 
 		PSGDrawPolygon(PSGCanvas, PSGPoints, PSGLineArrayX, PSGLineArrayY);	
		PSGAddPointToTable(0);			
 		break; 
 	case 'TrackAddPoint': 
		PSGAddPointToTable(0);			
		break; 
 	case 'TrackPoint': 

		break; 		
 	case 'TrackDefault': 
 		break; 		
 	default : alert('Unsupported Mode'); 
 	} 
 } 
  
function PSGEnableLoading(e)
{ 
  if (PSGEnableLoadingTag==false) {return;}
    e.srcElement.parentElement.children(5).style.display = ''; 
    e.srcElement.parentElement.children(5).style.top = (e.srcElement.parentElement.children(4).offsetTop + 
                 e.srcElement.parentElement.clientHeight /2) - e.srcElement.parentElement.children(5).clientHeight/2 ; 
    e.srcElement.parentElement.children(5).style.left = (e.srcElement.parentElement.children(4).offsetLeft + 
                 e.srcElement.parentElement.clientWidth /2) - e.srcElement.parentElement.children(5).clientWidth/2 ;  
}
function ThePSGMouseUp(e) 
{
   PSGInit(e); 
	switch (PSGHiddenTextbox.mode){ 
 	case 'TrackRectangle':  
 		PSGStopX = e.offsetX; 
 		PSGStopY = e.offsetY; 
		PSGLastX = PSGStopX;
		PSGLastY = PSGStopY;
		PSGPoint2=(PSGLastX) + ';' + (PSGLastY);
        PSGPoints=PSGPoint1+'\n'+PSGPoint2;
 		PSGHiddenTextbox.value = PSGMapCommand + PSGStartX + ',' + PSGStartY +  ' ' + PSGStopX + ',' + PSGStopY ;		 		 
 		PSGStartX = 0 ; PSGPoint1 = ''; PSGPoint2 = ''; PSGFirstX = 0; PSGFirstY = 0; PSGLastX = 0; PSGLastY = 0;
        PSGOnButton('Pointer'); //PSGSetTrackDefault();		
		PSGAddPointToTable(1);			
 		break; 
 	case 'TrackWireRectangle':  
 		PSGStopX = e.offsetX; 
 		PSGStopY = e.offsetY; 
 		PSGHiddenTextbox.value = PSGMapCommand + PSGStartX + ',' + PSGStartY +  ' ' + PSGStopX + ',' + PSGStopY ;		 		 
		document.MapForm.map_x.value = PSGStartX;
		document.MapForm.map_y.value = PSGStartY;
		if (PSGStartX != PSGStopX) {
		document.MapForm.map_x2.value = PSGStopX;
		document.MapForm.map_y2.value = PSGStopY;
		}		
		PSGStartX = 0 ; 
 		PSGEnableLoading(e);
 		document.MapForm.submit();  
 		break; 		
 	case 'Pan':  
 		PSGHiddenTextbox.value =PSGMapCommand + PSGStartX + ',' + PSGStartY ;	
 		PSGStartX = 0 ; 
 		PSGEnableLoading(e);
 		break; 
 	case 'TrackLine': 		 
 		break; 
 	case 'TrackPolygon':  
 		break; 
     case 'TrackPoint': 
 		PSGStartX = e.offsetX; 
 		PSGStartY = e.offsetY;		   
 		PSGHiddenTextbox.value =  PSGMapCommand + PSGStartX + ',' + PSGStartY;		 		 
		document.MapForm.map_x.value = PSGStartX;
		document.MapForm.map_y.value = PSGStartY;		
 		PSGStartX = 0 ; 
 		PSGEnableLoading(e);
 		document.MapForm.submit(); 
 		break;
 	case 'TrackDefault':  
 		break; 	
 	case 'TrackAddPoint': 
 		break;		
 	default : alert('Unsupported Mode'); 
 	stop 
 	} 
} 
  
function ThePSGMouseDoubleClick(e) 
{ 
   PSGInit(e); 
   switch (PSGHiddenTextbox.mode){ 
 	case 'TrackRectangle':	
 		break; 
 	case 'TrackWireRectangle':
 		break; 		
 	case 'Pan':  
 		break; 
 	case 'TrackLine':  
 		PSGLineArrayX[PSGLinePos] = e.offsetX ;		 
 		PSGLineArrayY[PSGLinePos] = e.offsetY ;	 
 		PSGPoints = PSGPoints + (e.offsetX) + ',' + (e.offsetY) + ' ' 
 		PSGHiddenTextbox.value =  PSGMapCommand + PSGPoints;
 		break; 
 	case 'TrackPolygon':  
 		PSGLineArrayX[PSGLinePos] = e.offsetX ;		 
 		PSGLineArrayY[PSGLinePos] = e.offsetY ;	 
 		PSGPoints = PSGPoints ;//+ (e.offsetX) + ';' + (e.offsetY) + '\n'; 
 		PSGHiddenTextbox.value =  PSGMapCommand + PSGPoints; //PSGLineArrayX.valueOf + '|' + PSGLineArrayY.valueOf ;
		PSGOnButton('Pointer');// PSGSetTrackDefault();
		break; 
 	case 'TrackPoint': 
 		break; 
 	case 'TrackDefault':  
 		break; 	
 	case 'TrackAddPoint': 
 		break; 		
 	default : alert('Unsupported Mode'); 
 	} 
} 
 function ThePSGMouseMove(e) 
 {
   if (PSGDone == true){ return; }  
   PSGInit(e) 
   switch (PSGHiddenTextbox.mode){ 
 	case 'TrackRectangle':  
 		if (PSGStartX != 0)  
 			{	
			    if ((PSGStartX < e.offsetX) && (PSGStartY < e.offsetY)){
 			    var CurrentX = e.offsetX - PSGStartX ;
 				var CurrentY = e.offsetY - PSGStartY ;
 				if (CurrentX > 0 && CurrentY > 0) {								 
 				PSGDrawRectangle(PSGCanvas, PSGStartX, PSGStartY, CurrentX, CurrentY);		 
 				}} else {
 			    var CurrentX = PSGStartX - e.offsetX;
 				var CurrentY = PSGStartY - e.offsetY;
 				if (CurrentX > 0 && CurrentY > 0) {								 
 				PSGDrawRectangle(PSGCanvas, e.offsetX, e.offsetY, CurrentX, CurrentY);		 				
				}}
 			}
 		break; 
 	case 'TrackWireRectangle':  
 		if (PSGStartX != 0)  
 			{		 
 			    var CurrentX = e.offsetX - PSGStartX ;
 				var CurrentY = e.offsetY - PSGStartY ;
 				 
 				if (CurrentX > 0 && CurrentY > 0) {								 
 				PSGDrawWireRectangle(PSGCanvas, PSGStartX, PSGStartY, CurrentX, CurrentY);		 
 				} 
 			} 
 		break; 		
 	case 'Pan':  
 		break; 
 	case 'TrackLine':  
 		if (PSGTrackStarted != false)  
 			{ 
 				var TempPoints = PSGPoints + (e.offsetX) + ',' + (e.offsetY) + ' '				 
 				PSGDrawPolyLine(PSGCanvas, TempPoints, PSGLineArrayX, PSGLineArrayY); 
 			}		 
 		break; 
 	case 'TrackPolygon':  
 		if (PSGTrackStarted != false)  
 			{ 
 				var TempPoints = PSGPoints + (e.offsetX) + ',' + (e.offsetY) + ' ' 
 				PSGDrawPolygon(PSGCanvas, TempPoints, PSGLineArrayX, PSGLineArrayY); 
 			}		 
 		break; 
 	case 'TrackPoint': 
 		break; 
 	default : ; 
 	} 
 } 
 function PSGClearCanvas(PSGCanvas) 
 { 
 		var TrackShape = PSGControl.children(3).children['TrackShape']; 
 		if (TrackShape != null) 
 		{ 
 			TrackShape.removeNode(true); 
 		} 
 } 
 function MyClearCanvas(PSGCanvas) 
 { 
        if (PSGCanvas==null){return;}
 		var TrackShape = PSGControl.children(3).children['TrackShape']; 
 		if (TrackShape != null) 
 		{ 
 			TrackShape.removeNode(true); 
			PSGTrackStarted=false;
    		PSGLineArrayX = new Array() ;		 
 	    	PSGLineArrayY = new Array() ; 
 			PSGLinePos=0;		 
	 		PSGPoints = '';		
            PSGHiddenTextbox.value='';
 		} 
 } 
 function PSGDrawRectangle(parentCanvas, left, top, width, height) 
 { 
 	PSGClearCanvas(PSGCanvas); 
 	var Rectangle; 
 	Rectangle = document.createElement('DIV'); 
 	PSGControl.children(3).appendChild(Rectangle); 
 	Rectangle.id = 'TrackShape'; 
 	Rectangle.style.zIndex = 103; 
 	Rectangle.style.mozOpacity = 0.33; 
 	Rectangle.style.filter = 'alpha(opacity=55)'; 
 	Rectangle.style.position = 'absolute'; 
	Rectangle.style.overflow='hidden';	
 	Rectangle.style.backgroundColor = '#00CCFF'; 
 	Rectangle.style.width = width; 
 	Rectangle.style.height = height; 
 	Rectangle.style.top = top;	 
 	Rectangle.style.left = left; 		
 	Rectangle.style.borderRight = 'red 2px solid'; 
 	Rectangle.style.borderLeft = 'red 2px solid'; 
 	Rectangle.style.borderTop = 'red 2px solid'; 
 	Rectangle.style.borderBottom = 'red 2px solid'; 
 } 
 function PSGDrawWireRectangle(parentCanvas, left, top, width, height) 
 { 
 	PSGClearCanvas(PSGCanvas); 
 	var Rectangle; 
 	Rectangle = document.createElement('DIV'); 
 	PSGControl.children(3).appendChild(Rectangle); 
 	Rectangle.id = 'TrackShape'; 
 	Rectangle.style.zIndex = 103; 
 	Rectangle.style.mozOpacity = 0; 
 	Rectangle.style.filter = 'alpha(opacity=99)'; 
 	Rectangle.style.position = 'absolute'; 
	Rectangle.style.overflow='hidden';	
 	Rectangle.style.width = width; 
 	Rectangle.style.height = height; 
 	Rectangle.style.top = top;	 
 	Rectangle.style.left = left; 
 	Rectangle.style.borderRight = 'red 1px solid'; 
 	Rectangle.style.borderLeft = 'red 1px solid'; 
 	Rectangle.style.borderTop = 'red 1px solid'; 
 	Rectangle.style.borderBottom = 'red 1px solid'; 
 } 
 function PSGDrawPolygon(PSGCanvas, pointsStr, xArray, yArray) 
 { 
   if (PSGMapBehavior!='TrackPolygon'){return;}
   PSGClearCanvas(PSGCanvas) 
   if (PSGDone == true){  
   return; } 	 
 	var Polygon; 
 	var endPoint = ''; 
 	if (xArray.length >= 2) 
 	{ 
 		var endPoint = '' + xArray[0] + ',' + yArray[0]; 
 	} 
 	Polygon = document.createElement('v:polyline'); 
 	Polygon.id = 'TrackShape'; 
 	Polygon.stroked = true; 
 	Polygon.strokeColor = '#FF0000'; 
 	Polygon.points = pointsStr + endPoint; 
 	Polygon.strokeWeight = '1px';		 
 	PSGControl.children(3).appendChild(Polygon); 
 	Polygon.style.right = 0; 
 	Polygon.style.left = 0; 
 	Polygon.style.position = 'absolute'; 
 	Polygon.style.zIndex =1001; 
 	Polygon.filled = true ;//false; 
 	Polygon.fillColor = '#00CCFF' 
 	Polygon.fill.opacity = 0.33; 
 } 
 function PSGDrawPolyLine(PSGCanvas, pointsStr, xArray, yArray) 
 { 
 	PSGClearCanvas(PSGCanvas) 
   if (PSGDone == true){  
   return; } 
 	var PolyLine; 
 	var endPoint = '';	 
 	PolyLine = document.createElement('v:polyline'); 
 	PolyLine.id = 'TrackShape'; 
 	PolyLine.stroked = true; 
 	PolyLine.strokeColor = '#FF0000'; 
 	PolyLine.points = pointsStr + endPoint; 
 	PolyLine.strokeWeight = 1;		 
 	PSGControl.children(3).appendChild(PolyLine); 
 	PolyLine.style.right = 0; 
 	PolyLine.style.left = 0; 
 	PolyLine.style.position = 'absolute'; 
 	PolyLine.style.zIndex =1001; 
 	PolyLine.filled = false;//false; 
 	PolyLine.fillColor = '#00CCFF' 
 	PolyLine.fill.opacity = 0.33; 
 } 
  function PSGXConvertToMap(X, Left, Right, Width) 
 { 
 	var MapX;
	MapX=Left+(((Right-Left)/Width))*X;
    return MapX;
 } 
   function PSGYConvertToMap(Y, Top, Bottom, Height) 
 { 
 	var MapY;
	MapY=Top-(((Top-Bottom)/Height))*Y;
    return MapY;
 } 
function PSGAddPointToTable(Rectangle) 
 { 
 		var RoundXCoordinate;
		var RoundYCoordinate;		
		RoundXCoordinate = (Math.round(document.getElementById("XCoordinate").value*10000)/10000);
		RoundYCoordinate = (Math.round(document.getElementById("YCoordinate").value*10000)/10000);	
		
		if (Rectangle == 1){

			g.addRow(g.rowCount);
			g.cells[g.rowCount-1][0].setData(RoundXCoordinate); 
			g.cells[g.rowCount-1][1].setData(g.cells[0][1].getData());
			g.paint();

			g.addRow(g.rowCount);
			g.cells[g.rowCount-1][0].setData(RoundXCoordinate); 
			g.cells[g.rowCount-1][1].setData(RoundYCoordinate);
			g.paint();
			
			g.addRow(g.rowCount);
			g.cells[g.rowCount-1][0].setData(g.cells[0][0].getData()); 
			g.cells[g.rowCount-1][1].setData(RoundYCoordinate);
			g.paint();
			
			}		
		else
		{
		if (g.cells[g.rowCount-1][0].getData() != '<br>') 
		{g.addRow(g.rowCount);}
		g.cells[g.rowCount-1][0].setData(RoundXCoordinate); 
		g.cells[g.rowCount-1][1].setData(RoundYCoordinate);
		g.paint();

		if (Rectangle == 1){
			g.addRow(g.rowCount);
			g.cells[g.rowCount-1][0].setData(g.cells[0][0].getData()); 
			g.cells[g.rowCount-1][1].setData(RoundYCoordinate);
			g.paint();}	
		}
 }

 function DoAddLayer() {
  var formObj =   document.LayerForm;
  //document['LayerForm']['chkBox'];
  var count = 0;
  var MapLayers = '';
  	for (i=0;i<formObj.length; i++)
  	{
  		if(formObj[i].type == 'checkbox' || formObj[i].type == 'radio')
  		if(formObj[i].checked)
		MapLayers = MapLayers +formObj[i].value+'|1]['
  		else
		MapLayers = MapLayers +formObj[i].value+'|0]['		
		count++;
  	}
	document.MapForm.VisibilityMapLayers.value=MapLayers;
	document.MapForm.Cmd.value='AddLayers';
	document.MapForm.submit();  

 }
 
 function SohwElement(itemId, buttomId, src1, src2) 
{
	var currItem;
	currItem = document.getElementById(itemId); 
	currItem2 = document.getElementById(buttomId);
	if(currItem)
	{
		if (currItem.style.display == "inline")
		{
			currItem.style.display = "none";
			currItem2.src = src1;
		}
		else 
		{
			currItem.style.display = "inline";
			currItem2.src = src2;
		}
	}	
}


 function UnSelectElement(formname, itemname) 
{
  var formObj =  document[formname][itemname];
 // document['LayerForm']['chkBox'];
  	for (i=0;i<formObj.length; i++)
  	{
  		//if(formObj[i].type == itemtype && formObj[i].name == itemname)
			formObj[i].checked=false;
  	}
}
