// quickXSLT.js

//Description: Applies an XSLT stylesheet to an XML element, applying parameters.
//Return Type: XMLDOMElement
//Return Value: The transformed element.

function transform(
	sourceXML,	//Argument: String (XMLDOMElement), the XML element to be transformed.
	stylesheetXML,	//Argument: String (XMLDOMElement), the XSLT stylesheet which will be applied to the XML element.
	params		//Argument: Params object, parameters to be applied to the transform.
	) {

	var xmlDOM = newDoc(sourceXML);
	var xslDOM = newDoc(stylesheetXML);
	var resultDOM = newDoc();
	var xslTemplate = new ActiveXObject('MSXML2.XSLTemplate.3.0');
	xslTemplate.stylesheet = xslDOM;
	var xslProcessor = xslTemplate.createProcessor();
	xslProcessor.input = xmlDOM;
	xslProcessor.output = resultDOM

	for (var i in params) {
		if (i.substring(0, 1) != "$") {
			xslProcessor.addParameter(swapStuff(i, "$", "-"), params[i]);
		}
	}

	xslProcessor.transform();
	return resultDOM.documentElement;
}


//Description: Applies an XSLT stylesheet to an XML element, using the properties of a Params object.
//Description: This function is deprecated in favor of the .$run() method of the Params object.
//Description: The .$xml property contains the XML element to be transformed, or is undefined when applying a trivial transform.
//Description: The .$xslt property contains the name of the XSLT stylesheet to be applied to the XML element.
//Description: Additional properties that begin with $ are not used here, but may be used for other purposes.
//Description: All properties that don't begin with $ are parameters to be applied.
//Return Type: XMLDOMElement
//Return Value: The transformed element.

function quickTransform(
	params	//Argument: Params object, contains properties to be used in the transform.
	) {

	var xsltDoc = newDoc();
	while (xsltDoc.xml == '') xsltDoc = loadNewDoc("xslt/" + params.$xslt + ".xsl");
	if (params.$xml == undefined) params.$xml = "<a/>"
	return transform(params.$xml, xsltDoc.xml, params);
}


//Description: The Params object constructor.
//Description: The .$xml and .$xslt properties and the .$run() method are set up in the constructor.
//Description: The .$run() method applies the stylesheet to the XML element.
//Description: Other properties may be added. Those that don't begin with $ are parameters to be applied to the transform.
//Description: Those that begin with $ are not used in the transform, but may be used for other purposes.
//Return Type: XMLDOMElement
//Return Value: The transformed element.

function Params(
	xml,	//Argument: String (XMLDOMElement), the XML element to be transformed.
	xslt	//Argument: String, the name of the XSLT stylesheet to be applied to the XML element.
	) {

	this.$xml = xml;
	this.$xslt = xslt;
	this.$run = function () {return quickTransform(this)}
}


//Description: The TParams object constructor. Inherits from Params.
//Description: This function is deprecated in favor of $XMLCode, which uses XML templates from the XML Template documents.
//Description: This object is used to apply a trivial transform with parameters, in which the input XML isn't used at all.
//Description: The stylesheet has only one template, which consists of XML code with plug-in parameters.
//Description: Other properties may be added. Those that don't begin with $ are parameters to be applied to the transform.
//Description: Those that begin with $ are not used in the transform, but may be used for other purposes.
//Return Type: XMLDOMElement
//Return Value: The transformed element.

function TParams(
	xslt	//Argument: String, the name of the XSLT stylesheet to be applied.
	) {

	return new Params("<a/>", xslt);
}


/*function SampleParams(theField) {
	var self = new TParams("field-sample");
	self.feature$limit = numberDataSamples;
	self.layer = ActiveLayer;
	self.sub$fields = theField;
	return self;
}*/


//Description: The LayerParams object constructor. Inherits from TParams.
//Description: This function is deprecated in favor of ArcXMLQuery, which uses the Query Template from ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform determined by its argument.
//Description: Currently, these are identify-xml.xsl in IdentifyParams, and query-xml.xsl in QueryParams, below.
//Description: The plug-in parameters are: max$returned, layer, subfields, has$limit, and limit$envelope.
//Return Type: LayerParams
//Return Value: The TParams object, with LayerParams defaults added.

function LayerParams(xslt) {
	var self = new TParams(xslt);
	self.max$returned = maxFeaturesReturned;
	self.layer = ActiveLayer;
	self.subfields = selectFields;
	self.has$limit = useLimitExtent;
	var limitParams = new LimitEnvelope();
	self.limit$envelope = limitParams.$run();
	return self;
}


//Description: The IdentifyParams object constructor. Inherits from LayerParams.
//Description: This function is called by aimsAddress.js from the text frame.
//Description: This function is deprecated in favor of ArcXMLIdentify,
//Description: which uses the Identify Template from ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in identify-xml.xsl.
//Description: The plug-in parameters are: max$returned, layer, subfields, envelope, has$limit, and limit$envelope.
//Return Type: QueryParams
//Return Value: The default LayerParams object, with begin$record and query$string defaults added.

function IdentifyParams() {
	var self = new LayerParams("identify-xml");
	var envParams = new Envelope();
	self.envelope = envParams.$run();
	return self;
}


//Description: The QueryParams object constructor. Inherits from LayerParams.
//Description: This function is deprecated in favor of ArcXMLQuery, which uses the Query Template from ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in query-xml.xsl.
//Description: The plug-in parameters are: max$returned, begin$record, layer,
//Description: subfields, query$string, has$limit, and limit$envelope.
//Return Type: QueryParams
//Return Value: The default LayerParams object, with begin$record and query$string defaults added.

function QueryParams(
	queryString	//Argument: String, the query string.
	) {
	var self = new LayerParams("query-xml");
	self.begin$record = queryStartRecord;
	self.query$string = queryString;
	return self;
}

/*function QueryJoin() {
	var self = new QueryParams();
	self.join$tables = "";
	self.from = "";
	self.to = "";
	self.type = "scan";
	self.$xslt = "query-join";
	return self;
}*/


//Description: The Envelope object constructor. Inherits from TParams.
//Description: This function is deprecated in favor of ArcXMLEnvelope,
//Description: which uses the Envelope Template from ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in envelope.xsl.
//Description: The plug-in parameters are: max$x, max$y, min$x, and min$y.
//Return Type: Envelope
//Return Value: The default TParams object, with Envelope defaults added.

function Envelope() {
	var self = new TParams("envelope");
	self.max$x = eRight;
	self.max$y = eTop;
	self.min$x = eLeft;
	self.min$y = eBottom;
	return self;
}


//Description: The LimitEnvelope object constructor. Inherits from Envelope.
//Description: This function is deprecated in favor of ArcXMLEnvelope,
//Description: which uses the Envelope Template from ArcXML Templates.xml.
//Return Type: LimitEnvelope
//Return Value: The default Envelope object, with limit defaults applied instead of envelope defaults.

function LimitEnvelope() {
	var self = new Envelope();
	self.max$x = limitRight;
	self.max$y = limitTop;
	self.min$x = limitLeft;
	self.min$y = limitBottom;
	return self;
}


//Description: The GeocodeLayer object constructor. Inherits from TParams.
//Description: This function is theoretically deprecated in favor of ArcXMLGeocodeLayer,
//Description: but I haven't yet gotten around to writing a template for it in ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in geocode.xsl.
//Description: The plug-in parameters are: name, point$x, delimiter, point$y, point$symbol, and point$label.
//Return Type: GeocodeLayer
//Return Value: The default TParams object, with GeocodeLayer defaults added.

function GeocodeLayer() {
	var self = new TParams("geocode");
	self.name = "GeoCode1";
	self.point$x = geocodeX;
	self.delimiter = coordsDelimiter;
	self.point$y = geocodeY;
	var geoPoint = new GeocodePoint();
	self.point$symbol = geoPoint.$run();
	var pointLabel = new Label();
	var scaleFactor = Math.max((eRight - eLeft) / iWidth, (eTop - eBottom) / iHeight);
	pointLabel.x += scaleFactor * 3;
	pointLabel.y += scaleFactor * 5;
	self.point$label = pointLabel.$run();
	return self;
}


//Description: The Label object constructor. Inherits from TParams.
//Description: This function is theoretically deprecated in favor of ArcXMLLabel,
//Description: but I haven't yet gotten around to writing a template for it in ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in label.xsl.
//Description: The plug-in parameters are: x, delimiter, y, label, color, size, shadow, glowing,
//Description: h$alignment, v$alignment, and overlap.
//Return Type: Label
//Return Value: The default TParams object, with Label defaults added.

function Label() {
	var self = new TParams("label");
	self.x = geocodeX;
	self.delimiter = coordsDelimiter;
	self.y = geocodeY;
	self.label = geocodeLabel;
	self.color = "0,0,0";
//	self.color = geocodePointColor;
	self.size = 13;
//	self.shadow = "64,64,64";
	self.glowing = "255,255,0";
//	self.outline = "255,0,0";
	self.h$alignment = "right";
	self.v$alignment = "top";
	self.overlap = "false";
	return self;
}

// PERTAINS TO HOPS - DO NOT DELETE!
/*function HOPSLabel() {
	var self = new TParams("polygon-label");
	self.field = valuePsfField;
	self.priorities = "1,1,1,1,1,1,1,1";
	self.how$many$labels = "one_label_per_name";
	self.font = "Times New Roman";
	self.font$color = "255, 0, 0";
	self.font$style = "Bold";
	self.font$size = 14;
	self.renderer = "";
	return self;
}*/


//Description: The SelectParams object constructor. Inherits from TParams.
//Description: This function is theoretically deprecated in favor of ArcXMLSelect,
//Description: but I haven't yet gotten around to writing a template for it in ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in select.xsl.
//Description: The plug-in parameters are: from$layer, query$string, selection$mode, envelope, has$limit, limit$envelope,
//Description: buffer$distance, buffer$units, points, renderer.
//Return Type: SelectParams
//Return Value: The default TParams object, with SelectParams defaults added.

function SelectParams() {
	var self = new TParams("select");
	self.name = "Select Features";
	self.from$layer = LayerID[ActiveLayerIndex];
	//self.selection$mode = selectionMode;
	self.selection$mode = 1;
	self.query$string = "";

/*	switch (selectionMode) {
		case 1:
			self.query$string = setQueryString;
			break
		case 2:
			self.envelope = newDoc("<ENVELOPE " + selectEnvelope + " />");
		default:
			switch (clickType) {
				case 1:
					var points = qE("POINT", makePoint(clickPointX[clickCount-1], clickPointY[clickCount-1]));
					break
				case 2:
					var path = qE("PATH");
					var points = qE("POLYLINE", path);
					break
				case 3:
					var path = qE("POLYGON");
					var points = qE("RING", path);
					break
				default:
					var path = qE("MULTIPOINT");
					var points = path;
			}

			if (clickType != 1) {
				for (var i=0;i<clickCount;i++) {
					path.appendChild(makePoint(clickPointX[i], clickPointY[i])); 
				}
			}

			self.points = points;
	}


	if (useLimitExtent) {
		self.useLimitExtent = true;
		var limitParams = new LimitEnvelope();
		self.limit$envelope = limitParams.$run();
	}

	if (shapeBufferDistance > 0) {
		self.buffer$distance = shapeBufferDistance;
		self.buffer$units = ScaleBarUnits;
	}
*/
	var shapeParams =
		(selectType == "point") ? new SelectPoint() : 
		(selectType == "line") ? new SelectLine() : new SelectPolygon();

	self.renderer = shapeParams.$run();
	return self;
}


//Description: Generates an ArcXML point.
//Return Type: XMLDOMElement
//Return Value: An ArcXML POINT element.

function makePoint(
	x,	//Argument: Numeric, x-coordinate.
	y	//Argument: Numeric, y-coordinate.
	) {

	return qE("POINT", "x=" + x, "y=" + y);
}


//Description: The SelectPoint object constructor. Inherits from TParams.
//Description: This function is theoretically deprecated in favor of ArcXMLSelectPoint,
//Description: but I haven't yet gotten around to writing a template for it in ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in marker.xsl.
//Description: The plug-in parameters are: color, type, width, and overlap.
//Return Type: SelectPoint
//Return Value: The default TParams object, with SelectPoint defaults added.

function SelectPoint() {
	var self = new TParams("marker");
	self.color = "255,0,0";
	//self.color = selectColor;
	self.type = "star";
	//self.type = "circle";
	self.width = 22;
	//self.width = 10;
	return self;
}


//Description: The GeocodePoint object constructor. Inherits from SelectPoint.
//Description: This function is theoretically deprecated in favor of ArcXMLGeocodePoint,
//Description: but I haven't yet gotten around to writing a template for it in ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in marker.xsl, using geocode defaults.
//Description: The plug-in parameters are: color, type, width, and overlap.
//Return Type: GeocodePoint
//Return Value: The default SelectPoint object, but with geocoding defaults used.

function GeocodePoint() {
	var self = new SelectPoint();
	self.overlap = "false";
	self.color = geocodePointColor;
	self.width = geocodePointSize;
	self.type = geocodePointType;
	return self;
}


//Description: The SelectLine object constructor. Inherits from TParams.
//Description: This function is theoretically deprecated in favor of ArcXMLSelectLine,
//Description: but I haven't yet gotten around to writing a template for it in ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in line.xsl.
//Description: The plug-in parameters are: color, type, and width.
//Return Type: SelectLine
//Return Value: The default TParams object, with SelectLine defaults added.

function SelectLine() {
	var self = new TParams("line");
	self.color = selectColor;
	self.type = "SOLID";
	self.width = 3;
	return self;
}


//Description: The SelectPolygon object constructor. Inherits from TParams.
//Description: This function is theoretically deprecated in favor of ArcXMLSelectPolygon,
//Description: but I haven't yet gotten around to writing a template for it in ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in polygon.xsl.
//Description: The plug-in parameters are: fill$color, boundary$color, fill$type, boundary$width, and transparency.
//Return Type: SelectLine
//Return Value: The default TParams object, with SelectLine defaults added.

function SelectPolygon() {
	var self = new TParams("polygon");
	self.fill$color = selectColor;
	self.boundary$color = "0,255,255";
	self.boundary$width = 5;
	//self.boundary$width = 1;
	self.fill$type = "solid";
	//self.transparency = transparentLevel;
	self.transparency = 0;
	return self;
}


//Description: The GeocodeParams object constructor. Inherits from TParams.
//Description: This function is called from aimsAddress.js, which runs in the text frame.
//Description: This function is theoretically deprecated in favor of ArcXMLGeocodeParams,
//Description: but I haven't yet gotten around to writing a template for it in ArcXML Templates.xml.
//Description: This object is used to apply the trivial transform in geocode-xml.xsl.
//Description: The plug-in parameters are: max$candidates, max$score, geo$layer, and address.
//Description: This function is called from aimsAddress.js.
//Return Type: GeocodeParams
//Return Value: The default GeocodeParams object.

function GeocodeParams() {
	var self = new TParams("geocode-xml");
	self.max$candidates = maxGeocodeCandidates;
	self.min$score = minGeocodeScore;
	self.geo$layer = GCLayerId[GCActiveLayer];
	self.address = qE("address")

	for (var i=0;i<GCidCount;i++) {
		self.address.appendChild(qE("GCTAG", "id=" + GCid[i], "value=" + GCvalue[i]));
	}

	return self;
}

/*function IdentifyList() {
	var self = new Params(xmlFile.xml, "identify");
	self.images = idImages;
	self.host$name = hostName;
	self.checked = idChecked;
	return self;
}*/


//Description: The GeocodeParams object constructor. Inherits from Params.
//Description: This function is called from identify.htm, which runs in the TOC frame.
//Description: This object is used to apply the transform in identify-doc.xsl to the appropriate table
//Description: in Identify Function.xml to generate the list of radio buttons for the Identify function.
//Return Type: XMLIdentifyList
//Return Value: The Params object, with added properties to allow for creating the Identify function.

function XMLIdentifyList() {
	var self = new Params(xmlIdFile.xml, "identify-doc");
	self.host$name = hostName;
	self.checked = idChecked;
	return self;
}

/*function PrintPageParams() {
	var self = new TParams("print-page");
	self.char$set = charSet;
	self.table$width1 = 656;
	self.table$width2 = 650;
	self.heading$size = 6;
	self.print$title = "";
	self.print$map$url = printMapURL;
	self.print$map$width = 460;
	self.print$map$height = 490;
	self.opacity = 0;
	self.big$print = "";
	self.print$legend$url = printLegURL;
	self.legend$height = 840;
	return self;
}*/


//Description: The MeasureParams object constructor. Inherits from Params.
//Description: This object applies the measure.xsl stylesheet to XML code
//Description: containing the points clicked with the measuring tool.
//Return Type: MeasureParams
//Return Value: The Params object, with added properties to allow for drawing the measuring tool on the map.

function MeasureParams(xml) {
	var self = new Params(xml, "measure");
	self.marker$color = clickMarkerColor;
	self.marker$size = clickMarkerSize;
	return self;
}


//Description: The MouseOverParams object constructor. Inherits from Params.
//Description: This object applies the mouse-poll.xsl stylesheet to ArcXML code containing the polling places in the current
//Description: map extents to generate a div containing invisible images with mouse-over tool tips identifying the polling
//Description: places moused over.
//Return Type: MouseOverParams
//Return Value: The Params object, with added properties to allow for mouse-over tool tips for polling places.

function MouseOverParams(xml) {
	var self = new Params(xml, "mouse-poll");
	self.size = 1;
	self.pixel$x = pixelX;
	self.pixel$y = pixelY;
	self.map$x = eLeft;
	self.map$y = eTop;
	return self;
}