/*
customtools.js
Scripts to control general functionality for MI custome map tool
Sept 2009
*/

//----------------------Globals
var http;
var maxNoProducts = 8;					//Total number of map products available for purchase
var extraOS25kPriceperKm = 0.48;		//Extra price/sq km for OS25k
		
//----------------------------------------------------------------LOCATION SEARCHING SCRIPTS	
	
//****************Create request object
function createRequestObject() {
	var ro;		
	if (window.XMLHttpRequest) { 				// Mozilla, Safari,...
		ro = new XMLHttpRequest();
		//if (ro.overrideMimeType) {
		//	ro.overrideMimeType('text/xml');
		//}
	}else if (window.ActiveXObject) { 			// IE
		try {
			ro = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			ro = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	return ro;			
}
//****************Runs database search for entered location	
function runSearch(){	
	if (document.frmSearch1.txtCP.value != ""){
		http = createRequestObject();
		http.open('get', '../maptools/searchLocation.asp?search=' + document.frmSearch1.txtCP.value ); 
		http.onreadystatechange = handleSearchResponse;
		http.send(null);
	}else{
		alert("Please choose a centre point");
	}
}
//****************Handles the responce from running search
function handleSearchResponse() {
	if(http.readyState == 4){
		var response = http.responseText;
		
		var responseArray = response.split("|")
		if (responseArray[0] == "1")	{
			eval(responseArray[1])
			document.getElementById("matches").innerHTML = "&nbsp;"
			document.getElementById("matches").style.display = "none"
		}else {
			document.getElementById("matches").style.display = 'block';
			document.getElementById("matches").innerHTML = responseArray[1];
		}
	}
}
//****************Zooms to specificed location - called when choosing a match from the returned search matches
function showMap(selObj){
	var coordString = selObj.options[selObj.selectedIndex].value;
	var responseArray = coordString.split(",");
	var xLoc = Number(responseArray[0]);
	var yLoc = Number(responseArray[1]);
	var zoomWidth = Number(responseArray[2]);
	zoomToPoint(xLoc,yLoc,zoomWidth);		
}

//----------------------------------------------------------------GENERAL SCRIPTS
//------Set The visibility of Control Layers
function showLayers(theLayers){
    var tabImage = document.getElementById('tabImage');
	tabImage.src = "../images/" + theLayers + ".jpg";
	
	var allLayers = new Array("step0ON","step1ON","step2ON","step3ON"); 
	for(m=0; m < allLayers.length;m++){
		if (theLayers == allLayers[m]){
		  var layerObj = document.getElementById(allLayers[m]);
		  layerObj.className='layerOn';
		}else{
		  var layerObj = document.getElementById(allLayers[m]);
		  layerObj.className='layerOff';
		}
	}
}
//------Store the pre selected map data and scale
function storeMapData(theSelectedMapScale){

	//Check if an extent at scale is already showing - will need resizing
	if(document.frmSearch1.selectedMapScale.value != ""){
		if(vlayer.features.length > 0){
			if(document.frmSearch1.extentType.value == "scaled"){
				resizeExtent(theSelectedMapScale);
			}
		}
	}

	document.frmSearch1.selectedMapScale.value = theSelectedMapScale;
	
	//Remove all unit options
	var noCurrentOptions = eval("document.frmSearch1.txtMapUnits.options.length");
	for(var m=noCurrentOptions; m >= 0; m--){
		eval("document.frmSearch1.txtMapUnits.options[m] = null");
	}
	var noCurrentOptions = eval("document.frmSearch1.txtMapUnits2.options.length");
	for(var m=noCurrentOptions; m >= 0; m--){
		eval("document.frmSearch1.txtMapUnits2.options[m] = null");
	}
	
	//Amend the map units list to show print scales
	if(document.frmSearch1.selectedMapScale.value != ""){
		eval("document.frmSearch1.txtMapUnits.options[0]=new Option('km.','km')");
		eval("document.frmSearch1.txtMapUnits.options[1]=new Option('mi.','mi')");	
		eval("document.frmSearch1.txtMapUnits.options[2]=new Option('mm.','mm')");
		eval("document.frmSearch1.txtMapUnits.options[3]=new Option('cm.','cm')");
		eval("document.frmSearch1.txtMapUnits.options[4]=new Option('in.','in')");		
		eval("document.frmSearch1.txtMapUnits2.options[0]=new Option('km.','km')");
		eval("document.frmSearch1.txtMapUnits2.options[1]=new Option('mi.','mi')");	
		eval("document.frmSearch1.txtMapUnits2.options[2]=new Option('mm.','mm')");
		eval("document.frmSearch1.txtMapUnits2.options[3]=new Option('cm.','cm')");
		eval("document.frmSearch1.txtMapUnits2.options[4]=new Option('in.','in')");	
		//Show the extra extent options if user has chosen a data product 
		document.getElementById('CustomSizeOpts').style.display = 'block';
		document.getElementById('standardPaperOpts').style.display = 'block';
		document.getElementById('CustomSizeOptsMessage').style.display = 'none';
		document.getElementById('standardPaperOptsMessage').style.display = 'none';
			
	}else{
		eval("document.frmSearch1.txtMapUnits.options[0]=new Option('km.','km')");
		eval("document.frmSearch1.txtMapUnits.options[1]=new Option('mi.','mi')");
		eval("document.frmSearch1.txtMapUnits2.options[0]=new Option('km.','km')");
		eval("document.frmSearch1.txtMapUnits2.options[1]=new Option('mi.','mi')");		
		//Hide the extra extent options if user has chosen a data product 
		document.getElementById('CustomSizeOpts').style.display = 'none';
		document.getElementById('standardPaperOpts').style.display = 'none';
		document.getElementById('CustomSizeOptsMessage').style.display = 'block';
		document.getElementById('standardPaperOptsMessage').style.display = 'block';	
	}
	
	updateFixedExtentSizes();
	document.getElementById("gotostep2").style.display = 'block'	
}
//------Updates the list of avaialable map radius sizes to agree with the select unit
function updateFixedExtentSizes(){

	//Remove all radius options
	var noCurrentOptions = eval("document.frmSearch1.txtRadius.options.length");
	for(var m=noCurrentOptions; m >= 0; m--){
		eval("document.frmSearch1.txtRadius.options[m] = null");
	}
	
	//Recreate the list of fixed radius based on selected unit
	if(document.frmSearch1.txtMapUnits.value == "in"){
		eval("document.frmSearch1.txtRadius.options[0]=new Option('Select a Print Area','')");
		eval("document.frmSearch1.txtRadius.options[1]=new Option('30 in','30')");
		eval("document.frmSearch1.txtRadius.options[2]=new Option('50 in','50')");
		eval("document.frmSearch1.txtRadius.options[3]=new Option('100 in','100')");
		eval("document.frmSearch1.txtRadius.options[4]=new Option('150 in','150')");
		eval("document.frmSearch1.txtRadius.options[5]=new Option('200 in','200')");
	}else if(document.frmSearch1.txtMapUnits.value == "cm"){
		eval("document.frmSearch1.txtRadius.options[0]=new Option('Select a Print Area','')");
		eval("document.frmSearch1.txtRadius.options[1]=new Option('30 cm','30')");
		eval("document.frmSearch1.txtRadius.options[2]=new Option('50 cm','50')");
		eval("document.frmSearch1.txtRadius.options[3]=new Option('100 cm','100')");
		eval("document.frmSearch1.txtRadius.options[4]=new Option('150 cm','150')");
		eval("document.frmSearch1.txtRadius.options[5]=new Option('200cm','200')");
	}else if(document.frmSearch1.txtMapUnits.value == "mm"){
		eval("document.frmSearch1.txtRadius.options[0]=new Option('Select a Print Area','')");
		eval("document.frmSearch1.txtRadius.options[1]=new Option('300 mm.','300')");
		eval("document.frmSearch1.txtRadius.options[2]=new Option('500 mm.','500')");
		eval("document.frmSearch1.txtRadius.options[3]=new Option('1000 mm.','1000')");
		eval("document.frmSearch1.txtRadius.options[4]=new Option('1500 mm.','1500')");
		eval("document.frmSearch1.txtRadius.options[5]=new Option('2000 mm.','2000')");
	}else if(document.frmSearch1.txtMapUnits.value == "km"){
		eval("document.frmSearch1.txtRadius.options[0]=new Option('Select a Area','')");
		eval("document.frmSearch1.txtRadius.options[1]=new Option('5 km.','5')");
		eval("document.frmSearch1.txtRadius.options[2]=new Option('10 km.','10')");
		eval("document.frmSearch1.txtRadius.options[3]=new Option('15 km.','15')");
		eval("document.frmSearch1.txtRadius.options[4]=new Option('25 km.','25')");
		eval("document.frmSearch1.txtRadius.options[5]=new Option('50 km.','50')");
		eval("document.frmSearch1.txtRadius.options[6]=new Option('75 km.','75')");
	}else if(document.frmSearch1.txtMapUnits.value == "mi"){
		eval("document.frmSearch1.txtRadius.options[0]=new Option('Select a Area','')");
		eval("document.frmSearch1.txtRadius.options[1]=new Option('5 mi.','5')");
		eval("document.frmSearch1.txtRadius.options[2]=new Option('10 mi.','10')");
		eval("document.frmSearch1.txtRadius.options[3]=new Option('15 mi.','15')");
		eval("document.frmSearch1.txtRadius.options[4]=new Option('25 mi.','25')");
		eval("document.frmSearch1.txtRadius.options[5]=new Option('50 mi.','50')");
		eval("document.frmSearch1.txtRadius.options[6]=new Option('75 mi.','75')");
	}
}

//------Updates the list of avaialable radial sizes to agree with the select unit
function updateRadialSizes(){

	//Remove all radius options
	var noCurrentOptions = eval("document.frmSearch1.txtRadials.options.length");
	for(var m=noCurrentOptions; m >= 0; m--){
		eval("document.frmSearch1.txtRadials.options[m] = null");
	}
	
	//Recreate the list of fixed radius based on selected unit
	if(document.frmSearch1.txtRadialUnits.value == "km"){
		eval("document.frmSearch1.txtRadials.options[0]=new Option('Select a Radial','')");
		eval("document.frmSearch1.txtRadials.options[1]=new Option('Every 1 km.','1')");
		eval("document.frmSearch1.txtRadials.options[2]=new Option('Every 2 km.','2')");
		eval("document.frmSearch1.txtRadials.options[3]=new Option('Every 3 km.','3')");
		eval("document.frmSearch1.txtRadials.options[4]=new Option('Every 5 km.','5')");
		eval("document.frmSearch1.txtRadials.options[5]=new Option('Every 10 km.','10')");
		eval("document.frmSearch1.txtRadials.options[6]=new Option('Every 20 km.','20')");
	}else if(document.frmSearch1.txtRadialUnits.value == "mi"){
		eval("document.frmSearch1.txtRadials.options[0]=new Option('Select a Radial','')");
		eval("document.frmSearch1.txtRadials.options[1]=new Option('Every 1 mi.','1')");
		eval("document.frmSearch1.txtRadials.options[2]=new Option('Every 2 mi.','2')");
		eval("document.frmSearch1.txtRadials.options[3]=new Option('Every 3 mi.','3')");
		eval("document.frmSearch1.txtRadials.options[4]=new Option('Every 5 mi.','5')");
		eval("document.frmSearch1.txtRadials.options[5]=new Option('Every 10 mi.','10')");
		eval("document.frmSearch1.txtRadials.options[6]=new Option('Every 20 mi.','20')");
	}
}
//----------Opens a new window
function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}
//----------Clears the map colour value
function resetColor(){
	document.getElementById('txtColor').value=''
}
//----------Checks the number of boundaries selected does not exceed the limit
function checkboxlimit(checkgroup, limit, type){
	var theType = type;
	var checkgroup=checkgroup;
	var limit=limit;
	for (var i=0; i<checkgroup.length; i++){
		checkgroup[i].onclick=function(){
		var checkedcount=0;
		for (var i=0; i<checkgroup.length; i++)
			checkedcount+=(checkgroup[i].checked)? 1 : 0
			if (checkedcount>limit){
				alert("You can only select a maximum of "+limit+" "+theType);
				this.checked=false;
			}
		}
	}
}
//----------Toggles the displayd map size on confirmation page between mm and "
function displaySize(type,width,height,id){
	
	var theType = type;
	var theID = id;
	var theWidth = width;
	var theHeight = height;
	
	if (theType=='inches'){
		document.getElementById('theWidth'+theID).innerHTML = theWidth;
		document.getElementById('theHeight'+theID).innerHTML = theHeight;
		document.getElementById('inches'+theID).style.display = 'none';
		document.getElementById('cm'+theID).style.display = 'inline';
	}else{
		document.getElementById('theWidth'+theID).innerHTML = theWidth;
		document.getElementById('theHeight'+theID).innerHTML = theHeight;
		document.getElementById('inches'+theID).style.display = 'inline';
		document.getElementById('cm'+theID).style.display = 'none';
	}	
}
//----------Passes selected map info thru to shopping basket
function buyNow(id){
	var theId = id
	if(document.getElementById('buynowlayer'+theId).style.backgroundImage == "url(images/contactUsBack.png)"){
		alert("Please contact the business team on 0845 450 3667 to discuss pricing for this map.");
	}else{
		if(document.getElementById('theFinish'+theId).value==""){
			alert('Please Select a finish before adding to your basket!');
		}else{
			document.getElementById('frmBuy'+theId).action = "http://www.mapsinternational.co.uk/addCustom.asp?id="+theId;
			var theForm = document.getElementById('frmBuy'+theId);
			theForm.submit();
		}
	}
}	
//----------Displays other map options on the confirmation page	
function showAllMaps(totalMaps){
	for(c=0;c<totalMaps;c++){
		var layerName = "container" + (c+1);
		document.getElementById(layerName).style.display = 'block';
	}
	document.getElementById("messagediv").style.display = 'none';			//Hide the message
}	
//******************Runs query to zoom to a county/region
function zoomToCounty(selObject){
	var searchString = selObject.value;			//**Get selected county	
	if(searchString != ""){				
		http = createRequestObject();
		http.open('get', '../maptools/zoomtoarea.asp?search=' + searchString ); 
		http.onreadystatechange = zoomToCountyResponse;
		http.send(null);	
	}else{
		alert("Ensure a county is selected"); 
	}				
}	
function zoomToCountyResponse(){
	if(http.readyState == 4){	
		//Get the returned values
		var response = http.responseText;		
		var tempArray = new Array();
		tempArray = response.split("|"); 	
		if(tempArray[0] == "ok"){					//Zoom to county
			addExtent(tempArray[1], "region");
		}else{										//Show message
			alert("Selected region could not be found");
		}								
	}
}
//----------Shows avaialable map type or goes to next stage
function chooseMapping(theAction){		
	if(theAction == "yes"){
		document.getElementById('alldatasets').style.display = 'block';
	}else if(theAction == "no"){
		storeMapData('','');	
		showLayers('step1ON');
	}
}	
function chooseMappingAfter(theAction){		
	if(theAction == "yes"){
		document.getElementById('alldatasets').style.display = 'block';
	}else if(theAction == "no"){	
		storeMapData('');
		showLayers('step1ON');
		
	}
}	
	
//----------------------------------------------------------------PRICING SCRIPTS


//----------Loop through each map product and work out the price
function getAllPrices(xSize,ySize,theFinish,ID,displayFinish,addOns){
	//Once the selected map item has been updated - change all the rest for comparative purposes
	for(c=1;c <= maxNoProducts; c++){
		if(document.getElementById('thePrice'+String(c))!= null){				//Check the product is showing
			xSize = document.getElementById('mapwidth'+String(c)).value;
			ySize = document.getElementById('mapheight'+String(c)).value;
			mapScale = document.getElementById('mapscale'+String(c)).value;
			getPrice(xSize,ySize,theFinish,c,displayFinish,addOns,mapScale);
		}
	}
}

//----------Work out the map price			
function getPrice(xSize,ySize,theFinish,ID,displayFinish,addOns,mapScale){		
	var theAddOns = addOns;
	var theDisplayFinish = displayFinish;
	var theID = ID;
	var thePrice = 0;
	var extraOS25kPrice = 0;
	
	
	if(theFinish == "floor"){									//Pricing on request for flooring
		document.getElementById('thePrice'+theID).value = "Price on request.";
		document.getElementById('theFinish'+theID).value = "";
		document.getElementById('thePrice'+theID).style.fontSize = '15px';
		document.getElementById('buynowlayer'+theID).style.backgroundImage = "url(images/contactUsBack.png)";	
	
	}else{
	
		if(mapScale == "25,000"){										//Define the value of the xtra price added for OS25k	
			var xGroundSizeKm = (xSize * 25000)/1000;
			var yGroundSizeKm = (ySize * 25000)/1000;
			extraOS25kPrice =  ((xGroundSizeKm) * (yGroundSizeKm)) * extraOS25kPriceperKm;
		}
		
		if((Number(xSize) != 0) && (Number(ySize != 0)) && (theFinish != "")){
			//Set up price arrays and associated size array.
			var customPrices = new Array (7);
			var theSizes = new Array("21x30","30x42","42x60","70x100","88x120","88x130","88x140","88x150","88x160","88x170","88x180","88x190","88x200","88x250","88x300","88x350","88x400","88x450","88x500","100x100","100x110","100x120","100x130","100x140","100x150","100x160","100x170","100x180","100x190","100x200","100x250","100x300","100x350","100x400","100x450","100x500","110x110","110x120","110x130","110x140","110x150","110x160","110x170","110x180","110x190","110x200","110x250","110x300","110x350","110x400","110x450","110x500","120x120","120x130","120x140","120x150","120x160","120x170","120x180","120x190","120x200","120x250","120x300","120x350","120x400","120x450","120x500","130x130","130x140","130x150","130x160","130x170","130x180","130x190","130x200","130x250","130x300","130x350","130x400","130x450","130x500","140x140","140x150","140x160","140x170","140x180","140x190","140x200","140x250","140x300","140x350","140x400","140x450","140x500","150x150","150x160","150x170","150x180","150x190","150x200","150x250"
							,"150x300","150x350","150x400","150x450","150x500","160x160","160x170","160x180","160x190","160x200","160x250","160x300","160x350","160x400","160x450","160x500","170x170","170x180","170x190","170x200","170x250","170x300","170x350","170x400","170x450","170x500","180x180","180x190","180x200","180x250","180x300","180x350","180x400","180x450","180x500","190x190","190x200","190x250","190x300","190x350","190x400","190x450","190x500","200x200","200x250","200x300","200x350","200x400","200x450","200x500","250x250","250x300","250x350","250x400","250x450","250x500","300x300","300x350","300x400","300x450","300x500","350x350","350x400","350x450","350x500","400x400","400x450","400x500","450x450","450x500","500x500");
													
			customPrices[0] = new Array("en",65,70,70,70,70,109,122,129,139,149,159,169,189,214,239,264,289,314,339,129,136,143,150,157,164,171,178,185,192,199,224,298,328,358,388,418,139,146,153,160,167,174,181,188,195,202,227,302,332,362,392,422,149,156,163,170,177,189,194,198,205,230,306,336,366,396,426,159,166,173,180,187,194,201,208,233,309,339,369,399,453,179,189,199,209,219,229,239,264,415,314,406,446,468,238,250,262,274,286,298,328,358,388,418,460,502,262,274,286,298,310,340,370,400,450,474,518,286,298,310,322,352,382,412,462,507,552,310,322,334,364,394,446,474,521,567,334,346,376,406,457,505,554,602,358,394,430,468,518,567,617,478,526,574,622,675,732,567,652,717,782,866,725,817,889,981,916,1016,1116,1123,1211,1365);
			
			customPrices[1] = new Array("enbm",96,111,120,129,151,168,182,202,215,232,241,257,275,325,386,493,590,643,698,181,195,193,199,209,226,239,259,265,277,283,333,475,630,756,822,564,215,213,220,226,238,244,259,266,280,286,336,477,662,786,862,937,215,221,227,243,249,277,272,283,289,348,480,696,829,906,980,237,243,249,264,278,284,297,303,351,492,728,865,946,1029,254,263,281,297,306,321,330,390,626,660,931,1018,1102,417,440,463,487,510,532,624,741,832,975,1071,1156,465,488,513,538,562,658,778,873,1020,1118,1206,512,538,565,590,686,808,903,1060,1156,1261,562,590,619,721,852,956,1111,1222,1315,616,644,751,886,992,1153,1258,1365,673,817,937,1050,1216,1336,1454,1051,1197,1347,1551,1699,1846,1176,1570,1748,1932,2116,1776,1975,1814,1984,1834,4240,2644,2665,2908,3176);
			
			customPrices[2] = new Array("enbmf",107,123,133,143,168,187,202,224,239,258,268,286,305,361,429,547,655,715,776,201,216,215,222,232,251,265,288,295,308,315,370,528,699,840,913,986,239,237,244,251,264,271,288,295,311,318,373,530,735,873,958,1041,239,246,252,270,277,308,302,314,321,387,465,774,920,1005,1089,263,270,277,294,308,315,330,337,390,547,810,961,1051,1143,282,292,312,330,340,357,367,433,694,734,1035,1131,1225,463,489,514,541,566,592,693,817,925,1084,1190,1285,517,542,571,598,626,732,865,970,1134,1242,1340,570,598,627,656,762,898,1004,1178,1285,1401,626,656,688,801,946,1063,1234,1358,1461,685,715,835,985,1102,1281,1399,1516,747,907,1051,1166,1352,1485,1615,1167,1329,1497,1724,1887,2052,1540,1746,1942,2146,2352,1972,2194,2016,2205,2038,2688,2938,2961,3232,3529);
			
			customPrices[3] = new Array("enhb",84,99,119,124,133,155,165,175,185,207,217,227,247,272,309,350,387,424,461,159,170,189,196,203,210,217,236,243,250,257,282,382,432,476,520,565,269,192,199,206,213,220,239,246,253,260,285,386,435,480,524,568,279,202,209,216,223,247,252,256,263,288,390,439,483,528,572,289,212,219,226,245,252,259,266,291,393,442,487,531,576,309,235,245,267,277,287,297,322,522,400,524,568,613,394,306,332,344,356,368,403,447,492,536,580,625,423,344,356,368,380,415,459,504,548,592,637,447,368,380,392,427,471,516,560,604,649,471,392,409,439,483,528,572,616,661,495,421,451,495,540,584,628,673,519,469,519,570,620,670,721,685,551,723,786,848,910,745,747,822,896,970,865,882,797,859,821,1016,1090,1105,1150,1225);
			
			customPrices[4] = new Array("enmbmf",107,123,140,161,189,200,221,243,254,293,304,325,345,413,491,601,719,787,853,217,224,236,242,261,279,286,324,331,348,356,423,624,772,918,1004,1090,248,255,262,278,291,299,327,334,352,359,426,627,816,966,1058,1148,258,266,281,285,303,335,340,355,362,430,606,856,1014,1112,1207,296,303,311,341,361,369,392,399,459,634,900,1060,1164,1267,325,335,361,384,392,421,431,491,808,813,1142,1252,1154,511,541,568,603,626,657,768,913,1027,1201,1314,1428,570,600,632,661,690,808,960,1077,1257,1376,1496,631,662,693,724,849,1004,1129,1314,1436,1561,697,726,760,889,1048,1149,1366,1498,1630,760,793,937,1094,1245,1425,1560,1695,858,1005,1152,1300,1508,1653,1803,1348,1540,1724,1974,2156,2344,1778,2000,2227,2454,2678,2252,2506,2302,2512,2324,3072,3346,3380,3690,4023);
			
			customPrices[5] = new Array("can",229,251,264,304,357,378,400,421,443,487,504,533,560,725,-99,-99,-99,-99,-99,370,389,407,418,444,465,489,532,544,568,662,809,-99,-99,-99,-99,-99,422,435,448,475,501,528,555,582,687,725,879,-99,-99,-99,-99,-99,450,480,509,538,567,673,713,747,781,949,-99,-99,-99,-99,-99,511,543,574,684,727,615,800,837,1026,-99,-99,-99,-99,-99,577,697,736,775,814,854,877,971,-99,-99,-99,-99,-99,790,786,870,865,907,949,1166,-99,-99,-99,-99,-99,1146,1210,1275,1340,1412,1736,-99,-99,-99,-99,-99,1279,1348,1424,1493,1845,-99,-99,-99,-99,-99,1428,1501,1574,1946,-99,-99,-99,-99,-99,1578,1655,2047,-99,-99,-99,-99,-99,1736,2149,-99,-99,-99,-99,-99,2663,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99);
			
			customPrices[6] = new Array("acr",242,265,313,530,582,616,650,684,718,752,787,821,855,1033,1204,1374,1545,1716,1894,553,599,637,676,715,754,793,832,871,909,947,1149,1343,1544,1739,1582,2134,641,684,727,770,812,855,898,940,983,1033,1246,1460,1673,1894,2107,2328,731,777,824,871,917,964,1017,1064,1110,1343,1583,1823,2056,2289,2522,828,878,929,979,1037,1087,1138,1188,1447,1700,1960,2211,2471,2780,933,994,1048,1103,1157,1211,1250,1544,1823,2095,2374,2701,2973,1055,1115,1169,1227,1285,1343,1642,1940,2238,2529,2854,3174,1173,1235,1297,1359,1428,1739,2056,2374,2740,3051,3368,1301,1367,1440,1506,1843,2173,2510,2896,3233,3570,1443,1513,1583,1940,2296,2701,3051,3407,3764,1587,1661,2037,2412,2837,3213,3589,3965,1739,2134,2529,2973,3368,3764,4159,2626,3174,3667,4159,4651,5143,3764,4360,4949,5588,6184,5046,5789,6475,7168,6579,7370,8160,8257,9138,10136);
	
	
	
	
			//Set up size arrays (short side size, valid long side sizes...)
			var sizes = new Array (22);			
			sizes[0] = new Array(21,30);
			sizes[1] = new Array(30,42);
			sizes[2] = new Array(42,60);
			sizes[3] = new Array(70,100);
			sizes[4] = new Array(88,120,130,140,150,160,170,180,190,200,250,300,350,400,450,500);
			sizes[5] = new Array(100,100,110,120,130,140,150,160,170,180,190,200,250,300,350,400,450,500);
			sizes[6] = new Array(110,110,120,130,140,150,160,170,180,190,200,250,300,350,400,450,500);
			sizes[7] = new Array(120,120,130,140,150,160,170,180,190,200,250,300,350,400,450,500);
			sizes[8] = new Array(130,130,140,150,160,170,180,190,200,250,300,350,400,450,500);
			sizes[9] = new Array(140,140,150,160,170,180,190,200,250,300,350,400,450,500);
			sizes[10] = new Array(150,150,160,170,180,190,200,250,300,350,400,450,500);
			sizes[11] = new Array(160,160,170,180,190,200,250,300,350,400,450,500);
			sizes[12] = new Array(170,170,180,190,200,250,300,350,400,450,500);
			sizes[13] = new Array(180,180,190,200,250,300,350,400,450,500);
			sizes[14] = new Array(190,190,200,250,300,350,400,450,500);
			sizes[15] = new Array(200,200,250,300,350,400,450,500);
			sizes[16] = new Array(250,250,300,350,400,450,500);
			sizes[17] = new Array(300,300,350,400,450,500);
			sizes[18] = new Array(350,350,400,450,500);
			sizes[19] = new Array(400,400,450,500);
			sizes[20] = new Array(450,450,500);
			sizes[21] = new Array(500,500);
	
			if((xSize*100) > (ySize*100)){
				var longSide = (xSize*100);
				var shortSide = (ySize*100);
			}
			else{	
				var longSide = (ySize*100);
				var shortSide = (xSize*100);
			}
			//Ensure size does not exceed the max size used for pricing
			if(longSide > 500){
				longSide = 500;
			}
			if(shortSide > 500){
				shortSide = 500;
			}
	
			for(var s=0;s<sizes.length;s++){ 	
				if(sizes[s][0] >= shortSide){
					newIndex = s;
					var arrayLen = sizes[newIndex].length;
					newIndex2 = -1
					for(var s2=1;s2<arrayLen;s2++){
						if(sizes[newIndex][s2] >= longSide){
							newIndex2 = s2;					
						}
						if(newIndex2 != -1){
							break;
						}	
					}
					if(newIndex2 != -1){
						break;
					}					
				}	
			}
	
			var sizeString =  String(sizes[newIndex][0]) + "x" + String(sizes[newIndex][newIndex2]);
	
			//Define the price by matching with pre-defined sizes in the size array.
			var testIndex = -99;
			for(var p=0;p<173;p++){
				if(theSizes[p] == sizeString){
					testIndex = p + 1;
					break;
				}
			}		
			
			if(testIndex != -99){
				for(var p=0;p<7;p++){
					if(customPrices[p][0] == theFinish){
						thePrice = customPrices[p][testIndex];
						thePrice = thePrice + theAddOns;
						break;
					}	
				}
			}
			else{
				thePrice = 0;
			}
		}
		
		if((thePrice == 0) || (thePrice == -99)){
			document.getElementById('thePrice'+theID).value = "Price on request.";
			document.getElementById('theFinish'+theID).value = "";
			document.getElementById('thePrice'+theID).style.fontSize = '15px';
			document.getElementById('buynowlayer'+theID).style.backgroundImage = "url(images/contactUsBack.png)";	
		
		}else{
		
			if(mapScale == "25,000"){										//Add the value of the xtra price added for OS25k
				thePrice += extraOS25kPrice; 
			}
			thePrice = formatPrice(Math.round(thePrice*100)/100);			//Format price to 2 dec places
			document.getElementById('thePrice'+theID).value = "£"+thePrice;
			document.getElementById('theFinish'+theID).value = displayFinish;
			document.getElementById('thePrice'+theID).style.fontSize = '30px';
			document.getElementById('buynowlayer'+theID).style.backgroundImage = "url(../maptools/images/buyNowBack.png)";
		}
		
	}

}
//****************Function to ensure zeros displayed at end of price	
function formatPrice(passedPrice) {   
	inPrice = String(passedPrice);
	var testLen1 = inPrice.length;
	var sep = inPrice.indexOf(".");
	//if(sep == -1){		
	//	inPrice = inPrice + ".00";
	//}else{
		var rest = inPrice.substring(0,(sep+1))  
		var testLen2 = rest.length;
		if(testLen1 == (testLen2 + 1)){
			inPrice = inPrice + "0";
		}
	//}
	return(inPrice);
}

/*Not needed??

function checkboxlimit(checkgroup, limit){
	var checkgroup=checkgroup
	var limit=limit
	for (var i=0; i<checkgroup.length; i++){
		checkgroup[i].onclick=function(){
		var checkedcount=0
		for (var i=0; i<checkgroup.length; i++)
			checkedcount+=(checkgroup[i].checked)? 1 : 0
		if (checkedcount>limit){
			alert("You can select a maximum of 2 boundaries")
			this.checked=false
			}
		}
	}
}

function checkboxlimitPC(checkgroup, limit){
	var checkgroup=checkgroup
	var limit=limit
	for (var i=0; i<checkgroup.length; i++){
		checkgroup[i].onclick=function(){
		var checkedcount=0
		for (var i=0; i<checkgroup.length; i++)
			checkedcount+=(checkgroup[i].checked)? 1 : 0
		if (checkedcount>limit){
			alert("You can select a maximum of 1 postcode")
			this.checked=false
			}
		}
	}
}

	function showLayer(theLayer, chk){		

		if(theLayer == 'vlayer'){
			var layerObj = vlayer	
		}
		else{
			var layerObj = rlayer
		}
				
		if (chk.checked){	
				layerObj.setVisibility(true)
			}
			else{
				layerObj.setVisibility(false)
			}
	}
function setVisibility(id, visibility) {
document.getElementById(id).style.display = visibility;
}
function handleCitySearchResponse() {
	if(http.readyState == 4){
		var theResults = http.responseText;
		document.getElementById("townPlans").innerHTML = theResults	
	}
}
function runAfricaSearch(theCity){			
	var searchTerm = theCity
	http = createRequestObject();
	http.open('get', 'searchTowns.asp?continent=' + searchTerm ); 
	http.onreadystatechange = handleCitySearchResponse;
	http.send(null);	
}
*/
//--------------------------End of Script