


var SUBSTITUTE_VARS = new Array();
var SUBSTITUE_REQUIERES_POINTS = new Array();

SUBSTITUTE_VARS['IMGSIZE'] = function (urlMap,delim) { return urlMap.getMapSize().toString(delim); }
SUBSTITUTE_VARS['IMGSIZE.X'] = function (urlMap) { return urlMap.getMapSize().x; }
SUBSTITUTE_VARS['IMGSIZE.Y'] = function (urlMap) { return urlMap.getMapSize().y; }
SUBSTITUTE_VARS['SIZE'] = SUBSTITUTE_VARS['IMGSIZE'];
SUBSTITUTE_VARS['IMAGESIZE'] = SUBSTITUTE_VARS['IMGSIZE'];
SUBSTITUTE_VARS['WIDTH'] = SUBSTITUTE_VARS['IMGSIZE.X'];
SUBSTITUTE_VARS['HEIGHT'] = SUBSTITUTE_VARS['IMGSIZE.Y'];

SUBSTITUTE_VARS['EXTENT'] = function (urlMap,delim) { return urlMap.getExtent(delim); }
SUBSTITUTE_VARS['EXTENT.MINX'] = function (urlMap) { return urlMap.getExtent(' ').split(' ')[0]; }
SUBSTITUTE_VARS['EXTENT.MINY'] = function (urlMap) { return urlMap.getExtent(' ').split(' ')[1]; }
SUBSTITUTE_VARS['EXTENT.MAXX'] = function (urlMap) { return urlMap.getExtent(' ').split(' ')[2]; }
SUBSTITUTE_VARS['EXTENT.MAXY'] = function (urlMap) { return urlMap.getExtent(' ').split(' ')[3]; }
SUBSTITUTE_VARS['EXTENTS'] = SUBSTITUTE_VARS['EXTENT'];
SUBSTITUTE_VARS['MINX'] = SUBSTITUTE_VARS['EXTENT.MINX'];
SUBSTITUTE_VARS['MINY'] = SUBSTITUTE_VARS['EXTENT.MINY'];
SUBSTITUTE_VARS['MAXX'] = SUBSTITUTE_VARS['EXTENT.MAXX'];
SUBSTITUTE_VARS['MAXY'] = SUBSTITUTE_VARS['EXTENT.MAXY'];

SUBSTITUTE_VARS['CENTERPOINT'] = function(urlMap,delim) { return urlMap.getCenterPoint().toString(delim); }
SUBSTITUTE_VARS['CENTERPOINT.X'] = function (urlMap) { return urlMap.getCenterPoint().x; }
SUBSTITUTE_VARS['CENTERPOINT.Y'] = function (urlMap) { return urlMap.getCenterPoint().y; }

SUBSTITUTE_VARS['SCALE'] = function (urlMap) { return urlMap.getScale(); }

SUBSTITUTE_VARS['POINT'] = function(urlMap,urlPoints,delim) { if(urlPoints && urlPoints.length > 0) { return urlPoints[0].toString(delim); } else { return ''; }}
SUBSTITUTE_VARS['POINT.X'] = function (urlMap,urlPoints) { return urlPoints[0].x; }
SUBSTITUTE_VARS['POINT.Y'] = function (urlMap,urlPoints) { return urlPoints[0].y; }
SUBSTITUE_REQUIERES_POINTS['POINT'] = 1;
SUBSTITUE_REQUIERES_POINTS['POINT.X'] = 1;
SUBSTITUE_REQUIERES_POINTS['POINT.Y'] = 1;

SUBSTITUTE_VARS['LAYERTITLES'] = function (urlMap,delim) {
	var layers = urlMap.getLayerTitles();
	var str = '';
	for(var l = 0; l < layers.length; l++) {
		if(delim == " ") {
			layers[l] = layers[l].replace(/ /g,'');
		}
		str += delim+layers[l];
	}
	str = str.substring(1);
	return str;
}

SUBSTITUTE_VARS['MULTIPOINT'] = function (urlMap,urlPoints,delim, pointdelim) {
	var mPoint = new String();
	mPoint = '';
	for(var p = 0; p < urlPoints.length; p++) {
		mPoint = mPoint + pointdelim + urlPoints[p].toString(delim);
	}
	mPoint = mPoint.substring(1);
	return mPoint;
}

SUBSTITUTE_VARS['MULTIPOINT.X'] = function (urlMap,urlPoints,delim) {
	var mPoint = new String();
	mPoint = '';
	for(var p = 0; p < urlPoints.length; p++) {
		mPoint = mPoint + delim + urlPoints[p].x;
	}
	mPoint = mPoint.substring(1);
	return mPoint;
}

SUBSTITUTE_VARS['MULTIPOINT.Y'] = function (urlMap,urlPoints,delim) {
	var mPoint = new String();
	mPoint = '';
	for(var p = 0; p < urlPoints.length; p++) {
		mPoint = mPoint + delim + urlPoints[p].y;
	}
	mPoint = mPoint.substring(1);
	return mPoint;
}
SUBSTITUE_REQUIERES_POINTS['MULTIPOINT'] = 1;
SUBSTITUE_REQUIERES_POINTS['MULTIPOINT.X'] = 1;
SUBSTITUE_REQUIERES_POINTS['MULTIPOINT.Y'] = 1;


SUBSTITUTE_VARS['MAPBOOK'] = function () { return MAPBOOK_FILE; }

/*
 * Converts a URL to a service XML object
 * Calculated variables (those in SUBSTITUTE_VARS) require a
 * '%' before and after the variable name to be recognized for the substitution,
 * ex. %MAPBOOK% would insert the mapbook.
 */
function ProcessURLString (serviceMap, url, points, addInputs, delim, sep) {
	url = new String(url);
	if(!delim) { delim = URL_DEFAULT_DELIM; }
	if(!sep) { sep = URL_DEFAULT_POINT_DELIM; }
	for(var k in SUBSTITUTE_VARS) {
		var re = new RegExp('%'+k+'%','gi');
		if(url.search(re) > 0) {
			if(SUBSTITUE_REQUIERES_POINTS[k]) {
				url = url.replace(re, SUBSTITUTE_VARS[k](serviceMap, points, delim, sep));
			} else {
				url = url.replace(re, SUBSTITUTE_VARS[k](serviceMap, delim, sep));
			}
		}
	}
	for(var k in addInputs) {
		var re = new RegExp('%'+k+'%','gi');
		url = url.replace(re, addInputs[k]);
	}
	return url;
	
}

function ProcessURL(serviceMap, serviceXML, points, addInputs, returnXML) {
	var url = serviceXML.getElementsByTagName('url')[0].firstChild.nodeValue;
	url  = url + '?';
	/** User Inputs Should Already Have a Value! **/
	var inputs = serviceXML.getElementsByTagName('input');
	/* Pre-built regexs for input types */
	var regExs = new Array();
	for(var k in SUBSTITUTE_VARS) {
		regExs[k] = new RegExp('^'+k,'i');
	}
	for(var i = 0; i < inputs.length; i++) {
		var iName = inputs[i].getAttribute('name');
		var iValue = inputs[i].getAttribute('value');
		var iType = inputs[i].getAttribute('type');
		var iDelim = inputs[i].getAttribute('delim');
		var iSeparator = inputs[i].getAttribute('pointdelim');
		if(!iType.match(/user/i) && !iType.match(/hidden/i)) {
			for(var k in SUBSTITUTE_VARS) {
				if(iType.match(regExs[k])) {
					if(SUBSTITUE_REQUIERES_POINTS[k]) {
						iValue = SUBSTITUTE_VARS[k](serviceMap, points, iDelim, iSeparator);
					} else {
						iValue = SUBSTITUTE_VARS[k](serviceMap, iDelim, iSeparator);
					}
				}

			}
		}
		inputs[i].setAttribute('value',iValue);
		url = url + iName + '=' + (iValue) + '&';
	}
	if(returnXML) {
		return serviceXML;
	}
	for(var k in addInputs) {
		url = url + k + '=' + (addInputs[k]) + '&';
	}
	return url;
}
