/*
** This file was created by Cuesta Systems Inc, Sept 2004
** It contains functions used for Selection and Notification tool
** Created By: Aida Dodo, Cuesta Systems
** On: Sept 24, 2004
*/


//****************************************************************************
//** This function send the shape points to php, to do the selection query by shape or line
//** Created By: Aida Dodo, Sept 24, 2004
//** Modified By: 
//*****************************************************************************
function sendshape(n,arr,arrindex)
{	
	var theForm = top.PostFrame.document.forms[0];
	var clickCount = arrindex;
	var sendshape = true;

	if (n==2)
	{
		// line
		if (clickCount>=n)
		{
			arr[arrindex] = "end";theForm.selectline.value = arr;
			//AD Oct 8, 2004
			completePointLayer(selectionType);
			emptyArray(selectionLineStr);
		}
		else
		{
			alert("A minimum of two points are required.");
			sendshape = false;
		}

	}
	else if (n==3)
	{
		// polygon
		if (clickCount>=n)
		{
			arr[arrindex] = "end";theForm.selectshape.value = arr;
			//AD Oct 8, 2004
			completePointLayer(selectionType);			
			emptyArray(selectionShapeStr);		
		}
		else
		{
			alert("A minimum of three points are required.");
			sendshape = false;
		}
	}

	if (sendshape==true) {
		//AD Oct 12, 2004
		showRetrieveMap();
		if (isAddSelection == true) {
			theForm.CMD.value = "ADDSELECTION";
			theForm.selectShpResults.value = sqryShpResults;}
		else if (isDelSelection == true) {
			theForm.CMD.value = "DELSELECTION";
			theForm.selectShpResults.value = sqryShpResults;
			}
		
		theForm.inputType.value = "Y"; //set to simulate a 'map click' when sending data to php.
		theForm.redraw.value = "Y";
		theForm.submit();
	}

}
//*****************************************************************************

//*****************************************************************************
//** This function deletes the last point in the polygon shape for selection
//** Created By: Aida Dodo, Sept 24, 2004
//** Modified By: 
//*****************************************************************************
function deleteClick()
{
	var theForm = top.PostFrame.document.forms[0];

	if (selectionType=="Select by Polygon" || selectionType=="Select by Shape")
	{
		selectionShapeStr.splice(selectionShapeStr.length-1,1);
		selshapeCount = selectionShapeStr.length;
		theForm.selectshape.value = selectionShapeStr;
	}
	else if (selectionType=="Select by Line")
	{
		selectionLineStr.splice(selectionLineStr.length-1,1);
		sellineCount = selectionLineStr.length;
		theForm.selectline.value = selectionLineStr;
	}

	//AD Oct 12, do not submit form now, only work on DHML layer

	//theForm.redraw.value = "Y";
	//theForm.submit();
	csy_redrawPointLayer(selectionType);
}
//*****************************************************************************

//*****************************************************************************
//** This function deletes all point clicks, to restart selection, in the polygon shape for selection
//** Created By: Aida Dodo, Sept 24, 2004
//** Modified By: 
//*****************************************************************************
function resetClick()
{
	var theForm = top.PostFrame.document.forms[0];

	if (selectionType=="Select by Polygon" ||selectionType=="Select by Shape")
	{
		emptyArray(selectionShapeStr);
		
		theForm.selectshape.value = selectionShapeStr;
	}
	else if (selectionType==="Select by Line")
	{
		emptyArray(selectionLineStr);
		theForm.selectline.value = selectionLineStr
	}

	isDrawPointLayer = false;
			
	//AD Oct 12, do not submit form now

	//theForm.redraw.value = "Y";
	//theForm.submit();
	
	csy_redrawPointLayer(selectionType);
}
//*****************************************************************************		
		
//*****************************************************************************
//** This function refreshes the tool mode, based on the selection type
//** Created By: Aida Dodo, Sept 24, 2004
//** Modified By: 
//*****************************************************************************
function refreshToolMode(mode)
{ 	toolMode=mode;csy_setMode(toolMode);writeModeFrame();
}
//*****************************************************************************


//*****************************************************************************
//** Complete pointLayer DHTML on shape/line completion
//** Created By: Aida Dodo, October, 2004
//** Modified By: 
//*
function completePointLayer(selectionType){
	destroyElement("pointLayer");
	if (selectionType == "Select by Shape" && selshapeCount>=1) {
		var numpts = selshapeCount;
		for(var i=0;i<numpts-1;i++) {
			var tmp_bi1 = selectionShapeStr[i].split("|");		
			var x1 = MapToPixCoord(tmp_bi1[0],0);
			var y1 = MapToPixCoord(tmp_bi1[1],1);
			var tmp_bi2 = selectionShapeStr[i+1].split("|");
			var x2 = MapToPixCoord(tmp_bi2[0],0);
			var y2 = MapToPixCoord(tmp_bi2[1],1);
			drawLine(x1,y1,x2,y2,"2px","pointLayer");
			drawPoint(x1,y1,"4px","pointLayer");		
		}
		tmp_bi1 = selectionShapeStr[numpts-1].split("|");	
		x1 = MapToPixCoord(tmp_bi1[0],0);
		y1 = MapToPixCoord(tmp_bi1[1],1);	
		tmp_bi2 = selectionShapeStr[0].split("|");
		x2 = MapToPixCoord(tmp_bi2[0],0);
		y2 = MapToPixCoord(tmp_bi2[1],1);
		drawPoint(x1,y1,"4px","pointLayer");
		drawDotLine(x1,y1,x2,y2,"2px","pointLayer");
	}
	else if (selectionType == "Select by Line" && sellineCount>=1) {
		var numpts = sellineCount;
		for(var i=0;i<numpts-1;i++) {
			var tmp_bi1 = selectionLineStr[i].split("|");		
			var x1 = MapToPixCoord(tmp_bi1[0],0);
			var y1 = MapToPixCoord(tmp_bi1[1],1);
			var tmp_bi2 = selectionLineStr[i+1].split("|");
			var x2 = MapToPixCoord(tmp_bi2[0],0);
			var y2 = MapToPixCoord(tmp_bi2[1],1);
			drawLine(x1,y1,x2,y2,"2px","pointLayer");
			drawPoint(x1,y1,"4px","pointLayer");		
		}
		tmp_bi1 = selectionLineStr[numpts-1].split("|");	
		x1 = MapToPixCoord(tmp_bi1[0],0);
		y1 = MapToPixCoord(tmp_bi1[1],1);	
		tmp_bi2 = selectionLineStr[0].split("|");
		x2 = MapToPixCoord(tmp_bi2[0],0);
		y2 = MapToPixCoord(tmp_bi2[1],1);
		drawPoint(x1,y1,"4px","pointLayer");
		drawDotLine(x1,y1,x2,y2,"2px","pointLayer");	
	}

}
//************************************************************************
