/*--------------------------------------------------------------
suppliers.js
For CentreMapsLive Ver. 2
Code for retrieving and outputing data supplier info
Lovell Jonhs Ltd.
01/01/10
--------------------------------------------------------------*/

//----------------------------------------------------------GLOBALS

var supplierNames = new Array('Cities Revealed','Getmapping','Blom','Infoterra','The Envrionment Agency','bluesky');
var supplierCodes = new Array('CR','GM','BLOM','IT','EA','BLUESKY');
var getSuppliersReq;						//Request object for retrieving suppliers for defined area			
var getOverviewHeightSuppliersReq;				//Request object for retrieving suppliers for map centre point
var getOverviewAerialSuppliersReq;
var getHeightSuppliersReq;					//Request object for retrieving suppliers for the defined area
var getAerialSuppliersReq;
var toCheckDateAsDate = null;

//-------------------------------------------------------------------------GETTING SUPPLIER INFO
function getOverlaySupplierInfo(theType,showArea){
	if(showArea){
		var outputHTMLString = "<p align='right' style='margin-right:10px;'><a href='javascript:closeSupplierListing();'>Close [x]</a></p>"					
		//document.getElementById("productsAvailable").innerHTML = outputHTMLString;
		//if(document.varform.data.value == "NULL_AERIAL"){
			outputHTMLString += "<div id='supplierinfoAerial_layer'></div>";		
		//}else if(document.varform.data.value == "NULL_HEIGHT"){
			outputHTMLString += "<div id='supplierinfoHeight_layer'></div>";
		//}		
		document.getElementById("productsAvailable").innerHTML = outputHTMLString;	
	}
	if(theType == "aerial"){		
		if(showArea == false){
			document.getElementById("supplierinfoAerial_layer").innerHTML = "<p style='color:#24318D;'>The following tables will show the suppliers of aerial imagery and height data in this area.<br/>These can be selected as natural additions to the OS MasterMap product.<br/><br/><img src='images/loader.gif' alt='Retrieving suppliers...'>   Please wait while retrieving aerial suppliers ........Alternatively cancel this search <a href='javascript:cancelSupplierReq2(\"aerial\");'>here</a></p>";   			
		}else{
			document.getElementById("supplierinfoAerial_layer").innerHTML = "<p style='color:#24318D;'>The following tables will show the suppliers of aerial imagery and height data in this area.<br/>These can be selected as natural additions to the OS MasterMap product.<br/><img src='images/loader.gif' alt='Retrieving suppliers...'>   Please wait while retrieving aerial suppliers ........Alternatively cancel this search <a href='javascript:cancelSupplierReq2(\"aerial\");'>here</a></p>";			
		}			
	}else if(theType == "height"){
		if(showArea == false){
			document.getElementById("supplierinfoHeight_layer").innerHTML = "<p style='color:#24318D;'><img src='images/loader.gif' alt='Retrieving suppliers...'>   Please wait while retrieving height suppliers ........Alternatively cancel this search <a href='javascript:cancelSupplierReq2(\"height\");'>here</a></p>";   			
		}else{
			document.getElementById("supplierinfoHeight_layer").innerHTML = "<p style='color:#24318D;'><img src='images/loader.gif' alt='Retrieving suppliers...'>   Please wait while retrieving height suppliers ........Alternatively cancel this search <a href='javascript:cancelSupplierReq2(\"height\");'>here</a></p>";			
		}
	}
	if(showArea){
		var theSelectedProductIndex = getSelectedProductIndex();
		var theExtentID = document.getElementById("extent" + theSelectedProductIndex).value;	
		if(theExtentID != ""){											//Get products that intesect map extent (Full info)	
			var extentGeom = getExtentGeomByID(theExtentID);
			var geomString = String(extentGeom).substring(9,(String(extentGeom).length -2))	
			aDate = new Date();	
			if(theType == "aerial"){			
				var qstring = "&data=NULL_AERIAL&type=overlay&xtent=" + geomString + "&thecountry=" + document.varform.region.value.toUpperCase() + "&thetime=" + String(aDate.getTime());
				getSuppliersReq = new ajaxObject('getSupplierInfo.php', handleGetSupplierInfo);
				getSuppliersReq.update(qstring,'POST');
			}else if(theType == "height"){
				var qstring = "&data=NULL_HEIGHT&type=overlay&xtent=" + geomString + "&thecountry=" + document.varform.region.value.toUpperCase() + "&thetime=" + String(aDate.getTime());
				clearTimeout(timerId);
				getSuppliersReq = new ajaxObject('getSupplierInfo.php', handleGetSupplierInfo);
				getSuppliersReq.update(qstring,'POST');			
			}
		}else{															//Get products that centre of the view (Reduced info)	
			aDate = new Date();
			if(theType == "aerial"){
				var qstring = "&data=NULL_AERIAL&type=overlay&thecountry=" + document.varform.region.value.toUpperCase() + "&thetime=" + String(aDate.getTime()) + "&centrex=" + map.getCenter().lon + "&centrey=" + map.getCenter().lat;
				if(getOverviewAerialSuppliersReq != null){
					getOverviewAerialSuppliersReq.abort();
				}
				getOverviewAerialSuppliersReq = new ajaxObject('getOverviewSupplierInfo.php', handleGetSupplierInfo);
				getOverviewAerialSuppliersReq.update(qstring,'POST');
			}else if(theType == "height"){
				var qstring = "&data=NULL_HEIGHT&type=overlay&thecountry=" + document.varform.region.value.toUpperCase() + "&thetime=" + String(aDate.getTime()) + "&centrex=" + map.getCenter().lon + "&centrey=" + map.getCenter().lat;
				clearTimeout(timerId);
				if(getOverviewHeightSuppliersReq != null){
					getOverviewHeightSuppliersReq.abort();
				}
				getOverviewHeightSuppliersReq = new ajaxObject('getOverviewSupplierInfo.php', handleGetSupplierInfo);
				getOverviewHeightSuppliersReq.update(qstring,'POST');
			}		
		}
	}else{	
		//Get suppliers that intersect the centre point of the map (Reduced info) - Called when clicking the "Height" or "Aerial" product category	
		aDate = new Date();
		if(theType == "aerial"){
			var qstring = "&data=NULL_AERIAL&type=overlay&thecountry=" + document.varform.region.value.toUpperCase() + "&thetime=" + String(aDate.getTime()) + "&centrex=" + map.getCenter().lon + "&centrey=" + map.getCenter().lat;
			if(getOverviewAerialSuppliersReq != null){
				getOverviewAerialSuppliersReq.abort();
			}
			getOverviewAerialSuppliersReq = new ajaxObject('getOverviewSupplierInfo.php', handleGetSupplierInfo);
			getOverviewAerialSuppliersReq.update(qstring,'POST');
		}else if(theType == "height"){
			var qstring = "&data=NULL_HEIGHT&type=overlay&thecountry=" + document.varform.region.value.toUpperCase() + "&thetime=" + String(aDate.getTime()) + "&centrex=" + map.getCenter().lon + "&centrey=" + map.getCenter().lat;
			clearTimeout(timerId);
			if(getOverviewHeightSuppliersReq != null){
					getOverviewHeightSuppliersReq.abort();
				}
			getOverviewHeightSuppliersReq = new ajaxObject('getOverviewSupplierInfo.php', handleGetSupplierInfo);
			getOverviewHeightSuppliersReq.update(qstring,'POST');
		}
	}
}
//****************Get supplier info for a specific product - called when product selected
function getSupplierInfo(showArea){
	//Ensure area for supplier info is visible
	if(showArea){
		var outputHTMLString = "<p align='right' style='margin-right:10px;'><a href='javascript:closeSupplierListing();'>Close [x]</a></p>"			
		if(document.varform.data.value == "NULL_AERIAL"){
			outputHTMLString += "<div id='supplierinfoAerial_layer'></div>";		
		}else if(document.varform.data.value == "NULL_HEIGHT"){
			outputHTMLString += "<div id='supplierinfoHeight_layer'></div>";
		}		
		document.getElementById("productsAvailable").innerHTML = outputHTMLString;
		zchm.slideIn();
	}		
	if((document.getElementById("supplierinfoAerial_layer") != null) || (document.getElementById("supplierinfoHeight_layer") != null)){		
		if(document.varform.data.value == "NULL_AERIAL"){
			if(showArea == false){
				document.getElementById("supplierinfoAerial_layer").innerHTML = "<p style='color:#24318D;'>The following table will show the suppliers of aerial imagery in this area.<br/><img src='images/loader.gif' alt='Retrieving suppliers...'>   Please wait while retrieving aerial suppliers ........Alternatively cancel this search <a href='javascript:cancelSupplierReq2(\"aerial\");'>here</a></p>";   			
			}else{
				document.getElementById("supplierinfoAerial_layer").innerHTML = "<p style='color:#24318D;'><br/>The following table will show the suppliers of aerial imagery for your chosen data area. Please choose a supplier from this table.<br/><img src='images/loader.gif' alt='Retrieving suppliers...'>   Please wait while retrieving aerial suppliers ........Alternatively cancel this search <a href='javascript:cancelSupplierReq2(\"aerial\");'>here</a></p>";			
			}
		}else if(document.varform.data.value == "NULL_HEIGHT"){
			if(showArea == false){
				document.getElementById("supplierinfoHeight_layer").innerHTML = "<p style='color:#24318D;'>The following table will show the suppliers of height data in this area.<br/><img src='images/loader.gif' alt='Retrieving suppliers...'>   Please wait while retrieving height suppliers ........Alternatively cancel this search <a href='javascript:cancelSupplierReq2(\"height\");'>here</a></p>";   			
			}else{
				document.getElementById("supplierinfoHeight_layer").innerHTML = "<p style='color:#24318D;'><br/>The following table will show the suppliers of height data for your chosen data area. Please choose a supplier from this table.<br/><img src='images/loader.gif' alt='Retrieving suppliers...'>   Please wait while retrieving height suppliers ........Alternatively cancel this search <a href='javascript:cancelSupplierReq2(\"height\");'>here</a></p>";			
			}
		}
		if(showArea){
			var theSelectedProductIndex = getSelectedProductIndex();
			var theExtentID = document.getElementById("extent" + theSelectedProductIndex).value;
			if(theExtentID != ""){											//Get products that intesect map extent (Full info)	
				var extentGeom = getExtentGeomByID(theExtentID);
				var geomString = String(extentGeom).substring(9,(String(extentGeom).length -2))	
				aDate = new Date();
				var qstring = "&data=" + document.varform.data.value + "&type=product&xtent=" + geomString + "&thecountry=" + document.varform.region.value.toUpperCase() + "&thetime=" + String(aDate.getTime());
				if(document.varform.data.value == "NULL_AERIAL"){
					getAerialSuppliersReq = new ajaxObject('getSupplierInfo.php', handleGetSupplierInfo);
					getAerialSuppliersReq.update(qstring,'POST');
				}else if(document.varform.data.value == "NULL_HEIGHT"){		
					getHeightSuppliersReq = new ajaxObject('getSupplierInfo.php', handleGetSupplierInfo);
					getHeightSuppliersReq.update(qstring,'POST');
				}
			}else{															//Get products that centre of the view (Reduced info)	
				aDate = new Date();
				var qstring = "&data=" + document.varform.data.value + "&type=product&thecountry=" + document.varform.region.value.toUpperCase() + "&thetime=" + String(aDate.getTime()) + "&centrex=" + map.getCenter().lon + "&centrey=" + map.getCenter().lat;
				if(document.varform.data.value == "NULL_AERIAL"){
					getOverviewAerialSuppliersReq = new ajaxObject('getOverviewSupplierInfo.php', handleGetSupplierInfo);
					getOverviewAerialSuppliersReq.update(qstring,'POST');
				}else if(document.varform.data.value == "NULL_HEIGHT"){				
					getOverviewHeightSuppliersReq = new ajaxObject('getOverviewSupplierInfo.php', handleGetSupplierInfo);
					getOverviewHeightSuppliersReq.update(qstring,'POST');
				}	
			}
		}else{	
			//Get suppliers that intersect the centre point of the map (Reduced info) - Called when clicking the "Height" or "Aerial" product category	
			aDate = new Date();
			var qstring = "&data=" + document.varform.data.value + "&type=product&thecountry=" + document.varform.region.value.toUpperCase() + "&thetime=" + String(aDate.getTime()) + "&centrex=" + map.getCenter().lon + "&centrey=" + map.getCenter().lat;
			if(document.varform.data.value == "NULL_AERIAL"){
				getOverviewAerialSuppliersReq = new ajaxObject('getOverviewSupplierInfo.php', handleGetSupplierInfo);
				getOverviewAerialSuppliersReq.update(qstring,'POST');
			}else if(document.varform.data.value == "NULL_HEIGHT"){				
				getOverviewHeightSuppliersReq = new ajaxObject('getOverviewSupplierInfo.php', handleGetSupplierInfo);
				getOverviewHeightSuppliersReq.update(qstring,'POST');
			}
		}
	}
}

//****************Handles the response from getting supplier info
function handleGetSupplierInfo(responseText, responseStatus){
	if(responseStatus == 200){
		if(responseText == ""){
			var supplierinfoHtml = "<p class='error'><br/>Due to the size of area selected or another issue, we have been unable to produce a listing.<br/><a href='javascript:getSupplierInfo(false);'>Click here to try again</a> or alternatively <a href='javascript:sendQuoteReq();'>request a quote here.</a></p>";				
			if(document.getElementById("supplierinfoAerial_layer") != null){
				document.getElementById("supplierinfoAerial_layer").innerHTML = supplierinfoHtml;
			}else if(document.getElementById("supplierinfoHeight_layer") != null){
				document.getElementById("supplierinfoHeight_layer").innerHTML = supplierinfoHtml;
			}else{			
				showDialog('Warning','Due to the size of area selected or another issue, we have been unable to produce a listing.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');
			}
		}else{
			//Store info for aerial and height info in arrays 
			var theSupplierOptions = new Array();
			var theAerialInfo = new Array();
			var theHeightInfo = new Array();
			var aerialCnt = 0;
			var heightCnt = 0;		
			var tempArray = responseText.split("~");			
			var theOutputType = tempArray[0];
			var theOutputType2 = tempArray[1];
			theSupplierOptions = tempArray[2].split("|");
			for(s=0;s<theSupplierOptions.length;s++){
				var theSupplierInfo = new Array();
				var theSupplierInfo = theSupplierOptions[s].split(",");
			
				if(theSupplierInfo[1] == "AERIAL"){
					theAerialInfo[aerialCnt] = theSupplierInfo;
					aerialCnt += 1;	
				}else if(theSupplierInfo[1] == "HEIGHT"){
					theHeightInfo[heightCnt] = theSupplierInfo;
					heightCnt += 1;
				}
			}	
			//--Reformat info into HTML
			var supplierinfoHtml = "";			
			//************************************************Show Aerial suppliers
			if(theAerialInfo.length > 0){
				if(theOutputType == "full"){
					var thecolspan = "8";
					var theTableWidth = "700px";
				}else{
					var thecolspan = "4";
					var theTableWidth = "500px";
				}
				supplierinfoHtml += "<table width='" + theTableWidth + "' cellpadding='0' cellspacing='0'>";
				if(theOutputType == "full"){
					//if(theOutputType2 == "overlay"){
						supplierinfoHtml += "<tr valign='top'><td colspan='" + thecolspan + "'><h2>Aerial Data</h2></td></tr>";
					//}			
					supplierinfoHtml += "<tr valign='top'><td colspan='" + thecolspan + "'><p style='color:#24318D;'>The following suppliers have been found in this area. Please select one or more options from the available datasets listed.</p><br/></td></tr>";							
				}else{
					if(theOutputType2 == "overlay"){
						//supplierinfoHtml += "<tr valign='top'><td colspan='" + thecolspan + "'><p>The following suppliers have been found in this area. To purchase choose the product above and define your data area.<br/>You will then be able to choose the specific data supplier.</p><br/></td></tr>";			
					}else{
						//supplierinfoHtml += "<tr valign='top'><td colspan='" + thecolspan + "'><p>The following suppliers have been found in this area. To purchase choose the product above and define your data area.<br/>You will then be able to choose the specific data supplier.</p><br/></td></tr>";								
						//document.getElementById("supplierMessages").innerHTML = "<p>The following is available for this location.<br/>To purchase choose the product to the left and define your data area.<br/>You will then be able to choose the specific data supplier for price and availability.";			
					}
					//document.getElementById("supplierMessages").innerHTML = "<p>The following is available for this location.<br/>To purchase choose the product to the left and define your data area.<br/>You will then be able to choose the specific data supplier for price and availability.";			

					document.getElementById("supplierMessages").innerHTML = "<p><img src='images/select.jpg' alt='Select Now!' onmouseover='this.src=\"images/selectON.jpg\"' onmouseout='this.src=\"images/select.jpg\"' onclick='getProductInfo(\"NULL_AERIAL\",\"new\");'/><br/>(After selection define your data area.<br/>You will then be able to choose the specific data supplier for price and availability.)</p>";			
					//supplierinfoHtml += "<tr valign='top'><td colspan='" + thecolspan + "'><h2>Aerial Data</h2></td></tr>";
				}
					
				if(theOutputType != "full"){	
					supplierinfoHtml += "<tr valign='top'><td colspan='3'><p>The following are available for this location.</p></td></tr>";	
				}	
				//Write header
				supplierinfoHtml += "<tr valign='top'>";	
				supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Res.<br/>(cm)</strong></p></td>";
				supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Dates</strong></p></td>";
				supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Supplier</strong></p></td>";
				supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Preview</strong></p></td>";				
				
				if(theOutputType == "full"){
					supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>% Coverage</strong></p></td>";
					supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Supply<br/>Res.(cm)</strong></p></td>";						
					supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Price<br/>(excl. VAT)</strong></p></td>";
					supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Add?</strong></p></td>";					
				}
				supplierinfoHtml += "</tr>";								
				//Re-replace back in " " and "," in the date entries
				for(c=0;c<theAerialInfo.length;c++){
					theAerialInfo[c][3] = theAerialInfo[c][3].replace(/-space-/g, " ");
					theAerialInfo[c][3] = theAerialInfo[c][3].replace(/-comma-/g, ",");
				}					
				//Sort the array by date.
				//Supplier info array = [ProductCode,Category,PixelSize,Date,Supplier,Cost,PreviewURL]
				for(c=0;c<theAerialInfo.length;c++){
					for(c2=c;c2<theAerialInfo.length;c2++){				
						//Get the year (get start date where range specified)
						/*var toCheckDate1 = getCheckDate(theAerialInfo[c][3]);
						var toCheckDateAsDate1 = toCheckDateAsDate;
						var toCheckDate2 = getCheckDate(theAerialInfo[c2][3]);
						var toCheckDateAsDate2 = toCheckDateAsDate;*/
						
						
						var toCheckDateAsDate1 = getCheckDate(theAerialInfo[c][3]);
						//var toCheckDateAsDate1 = toCheckDateAsDate;
						var toCheckDateAsDate2 = getCheckDate(theAerialInfo[c2][3]);
						//var toCheckDateAsDate2 = toCheckDateAsDate;
						
						
						var reOrder = false;
						if((toCheckDateAsDate1 != null) && (toCheckDateAsDate2 != null)){		
							if(toCheckDateAsDate1 < toCheckDateAsDate2){
								reOrder = true;
							}
						//}else{
						//	if(toCheckDate1 < toCheckDate2){
						//		reOrder = true;
						//	}
						}
						
												
						//if(  (toCheckDate1 < toCheckDate2) || ((toCheckDateAsDate1 < toCheckDateAsDate2) &&  ((toCheckDateAsDate1 != null) && (toCheckDateAsDate2 != null)))){		//Need to check actual date in the format dd/mm/yyyy as well
						if(reOrder){
							holding = theAerialInfo[c2][0];
							theAerialInfo[c2][0] = theAerialInfo[c][0];
							theAerialInfo[c][0] = holding; 
							holding = theAerialInfo[c2][1];
							theAerialInfo[c2][1] = theAerialInfo[c][1];
							theAerialInfo[c][1] = holding; 
							holding = theAerialInfo[c2][2];
							theAerialInfo[c2][2] = theAerialInfo[c][2];
							theAerialInfo[c][2] = holding;  
							holding = theAerialInfo[c2][3];
							theAerialInfo[c2][3] = theAerialInfo[c][3];
							theAerialInfo[c][3] = holding;  						
							holding = theAerialInfo[c2][4];
							theAerialInfo[c2][4] = theAerialInfo[c][4];
							theAerialInfo[c][4] = holding; 
							holding = theAerialInfo[c2][5];
							theAerialInfo[c2][5] = theAerialInfo[c][5];
							theAerialInfo[c][5] = holding;
							holding = theAerialInfo[c2][6];
							theAerialInfo[c2][6] = theAerialInfo[c][6];
							theAerialInfo[c][6] = holding;
							holding = theAerialInfo[c2][7];
							theAerialInfo[c2][7] = theAerialInfo[c][7];
							theAerialInfo[c][7] = holding;
						}
					}
				}
				
				/*testing = "";
				for(c=0;c<theAerialInfo.length;c++){
					var toCheckDate1 = getCheckDate(theAerialInfo[c][3]);
					alert(toCheckDateAsDate);
					if(c==0){
						testing = theAerialInfo[c][3]						
					}else{
						testing += ", " + theAerialInfo[c][3]
					}
				
				}
				alert(testing)*/
				
				
				//---Sort the array by productcode.
				for(c=0;c<theAerialInfo.length;c++){
					for(c2=c;c2<theAerialInfo.length;c2++){
						//Get the year (get start date where range specified)
						//var toCheckDate1 = getCheckDate(theAerialInfo[c][3]);
						//var toCheckDateAsDate1 = toCheckDateAsDate;
						//var toCheckDate2 = getCheckDate(theAerialInfo[c2][3]);							
						//var toCheckDateAsDate2 = toCheckDateAsDate;
						var isSameDate = false;
						//if((toCheckDateAsDate1 != null) && (toCheckDateAsDate2 != null)){		
					//		if(toCheckDateAsDate1 == toCheckDateAsDate2){
						//		isSameDate = true;
						//	}
						//}else{
							if(theAerialInfo[c][3] == theAerialInfo[c2][3]){
							//if(toCheckDate1 == toCheckDate2){
								isSameDate = true;
							}
						//}
						if((theAerialInfo[c][0] > theAerialInfo[c2][0]) && (isSameDate)){   //(toCheckDate1 == toCheckDate2)){ 
							holding = theAerialInfo[c2][0];
							theAerialInfo[c2][0] = theAerialInfo[c][0];
							theAerialInfo[c][0] = holding; 
							holding = theAerialInfo[c2][1];
							theAerialInfo[c2][1] = theAerialInfo[c][1];
							theAerialInfo[c][1] = holding; 
							holding = theAerialInfo[c2][2];
							theAerialInfo[c2][2] = theAerialInfo[c][2];
							theAerialInfo[c][2] = holding;  
							holding = theAerialInfo[c2][3];
							theAerialInfo[c2][3] = theAerialInfo[c][3];
							theAerialInfo[c][3] = holding;  						
							holding = theAerialInfo[c2][4];
							theAerialInfo[c2][4] = theAerialInfo[c][4];
							theAerialInfo[c][4] = holding; 
							holding = theAerialInfo[c2][5];
							theAerialInfo[c2][5] = theAerialInfo[c][5];
							theAerialInfo[c][5] = holding;
							holding = theAerialInfo[c2][6];
							theAerialInfo[c2][6] = theAerialInfo[c][6];
							theAerialInfo[c][6] = holding;
							holding = theAerialInfo[c2][7];
							theAerialInfo[c2][7] = theAerialInfo[c][7];
							theAerialInfo[c][7] = holding;	
						} 	
					}
				}
				
				/*testing = "";
				for(c=0;c<theAerialInfo.length;c++){
					if(c==0){
						testing = theAerialInfo[c][2]
					}else{
						testing += ", " + theAerialInfo[c][2]
					}
				
				}
				alert(testing)*/
	
				//Sort the array by resolution.
				for(c=0;c<theAerialInfo.length;c++){
					for(c2=c;c2<theAerialInfo.length;c2++){
						//Get the year (get start date where range specified)
						//var toCheckDate1 = getCheckDate(theAerialInfo[c][3]);
						//var toCheckDateAsDate1 = toCheckDateAsDate;
						//var toCheckDate2 = getCheckDate(theAerialInfo[c2][3]);
						//var toCheckDateAsDate2 = toCheckDateAsDate;					
						var isSameDate = false;
						//if((toCheckDateAsDate1 != null) && (toCheckDateAsDate2 != null)){		
						//	if(toCheckDateAsDate1 == toCheckDateAsDate2){
						//		isSameDate = true;
						//	}
						//}else{
						
							if(theAerialInfo[c][3] == theAerialInfo[c2][3]){
							//if(toCheckDate1 == toCheckDate2){
								isSameDate = true;
							}
						//}
						

						//alert(theAerialInfo[c][2] + "=" + theAerialInfo[c2][2] + "|" + theAerialInfo[c][0] + "=" + theAerialInfo[c2][0] + "|" + String(isSameDate) + "|" + theAerialInfo[c][4] + "=" + theAerialInfo[c2][4])    //(toCheckDate1 == toCheckDate2)

						if((Number(theAerialInfo[c][2]) > Number(theAerialInfo[c2][2])) && (theAerialInfo[c][0] == theAerialInfo[c2][0]) && (isSameDate) && (theAerialInfo[c][4] == theAerialInfo[c2][4])){    //(toCheckDate1 == toCheckDate2)
							holding = theAerialInfo[c2][0];
							theAerialInfo[c2][0] = theAerialInfo[c][0];
							theAerialInfo[c][0] = holding; 
							holding = theAerialInfo[c2][1];
							theAerialInfo[c2][1] = theAerialInfo[c][1];
							theAerialInfo[c][1] = holding; 
							holding = theAerialInfo[c2][2];
							theAerialInfo[c2][2] = theAerialInfo[c][2];
							theAerialInfo[c][2] = holding;  
							holding = theAerialInfo[c2][3];
							theAerialInfo[c2][3] = theAerialInfo[c][3];
							theAerialInfo[c][3] = holding;  						
							holding = theAerialInfo[c2][4];
							theAerialInfo[c2][4] = theAerialInfo[c][4];
							theAerialInfo[c][4] = holding; 
							holding = theAerialInfo[c2][5];
							theAerialInfo[c2][5] = theAerialInfo[c][5];
							theAerialInfo[c][5] = holding;
							holding = theAerialInfo[c2][6];
							theAerialInfo[c2][6] = theAerialInfo[c][6];
							theAerialInfo[c][6] = holding;
							holding = theAerialInfo[c2][7];
							theAerialInfo[c2][7] = theAerialInfo[c][7];
							theAerialInfo[c][7] = holding;	
						} 	
					}
				}
								
				var nofSuppliers = theAerialInfo.length;
				a= 0;
				while(a<nofSuppliers){
					var baseDate = theAerialInfo[a][3];
					var baseCode = theAerialInfo[a][0];
					uptoIndex = -1;
					for(a1=(a+1);a1<theAerialInfo.length;a1++){
						var checkDate = theAerialInfo[a1][3];
						var checkCode = theAerialInfo[a1][0];
						if((baseDate == checkDate) && (baseCode == checkCode)){
						}else{
							uptoIndex = a1;
							break
						}
					}
					
					if(uptoIndex == -1){		//Nothing with the same date (Print as is)
						//Get Supplier name
						var supplierName = "Not Defined";
						for(s=0;s<supplierCodes.length;s++){
							if(theAerialInfo[a][4] == supplierCodes[s]){
								supplierName = supplierNames[s];
								break;
							}
						}
						var theResolution = String(Number(theAerialInfo[a][2]) * 100);
						if(theResolution.indexOf(".") != -1){
							theResolution = Math.round(Number(theResolution)*10)/10;
						}
						supplierinfoHtml += "<tr align='center' valign='top'>";						
						supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + theResolution + "</td>";
						supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + theAerialInfo[a][3] + "</td>";						
						supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + supplierName + ": " + theAerialInfo[a][0] + "</td>";
						
						if(theAerialInfo[a][6] != ""){
							supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'><img src='images/photo.jpg' witdh='22' height='18' style='cursor:pointer;' alt='Click for sample' border='0' onclick='showSupplierEg(\"" + theAerialInfo[a][6] + "\");' /></td>";
						}else{
							supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>&nbsp;</td>";
						}
						
						if(theOutputType == "full"){
						
							if(theAerialInfo[a][7] == ""){
								theAerialInfo[a][7] = "100";
							}
							if(Number(theAerialInfo[a][7]) == 100){
								outputCoverage = "<p style='color:#00FF00;font-weight:bold;'>";
							}else if(Number(theAerialInfo[a][7]) < 90){
								outputCoverage = "<p style='color:#FF0000;font-weight:bold;'>";
							}else{
								outputCoverage = "<p style='color:#FF9900;font-weight:bold;'>";
							}
							outputCoverage += String(Math.round(Number(theAerialInfo[a][7])*10)/10) + "</p>"
							//supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + String(Math.round(Number(theAerialInfo[a][7])*10)/10) + "</td>";
							supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + outputCoverage + "</td>";							
							supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + theResolution + "</td>";											
							supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>+&pound;" + formatPrice(theAerialInfo[a][5]) + "</td>"											
							//Define if this need to be selected ie. showing a selected product need to list the suppliers chosen	
							var selectedProductIndex = getSelectedProductIndex();
							var isChecked = false;
							if(document.getElementById("productLayers" + selectedProductIndex).value != ""){
								var selectedSupplierInfoArray = document.getElementById("productLayers" + selectedProductIndex).value.split(",");
								for(s=0;s<selectedSupplierInfoArray.length;s++){
									var storedSupplierInfo = selectedSupplierInfoArray[s];
									var retrievedSupplierInfo = theAerialInfo[a][0] + "|" + theAerialInfo[a][3] + "|"  + theAerialInfo[a][2] + "|" + supplierName + "|"  + String(Math.round(Number(theAerialInfo[a][7])*10)/10);
									retrievedSupplierInfo = retrievedSupplierInfo.replace(/,/g,"-com-");	//Adjustments to make agree with what been stored/
									if(retrievedSupplierInfo.indexOf("*") == 0){			
										retrievedSupplierInfo = retrievedSupplierInfo.substring(1,retrievedSupplierInfo.length);
									}
									if(storedSupplierInfo == retrievedSupplierInfo){
										isChecked = true
										break;
									}
								}
							}
							supplierinfoHtml += "<td class='supplierinfo'><input type='checkbox' name='supp1' onclick='setSupplier(this,\"" + theAerialInfo[a][0] + "|" + theAerialInfo[a][3] + "|"  + theAerialInfo[a][2] + "|" + supplierName + "\",\""  + theAerialInfo[a][5] + "\",\""  + String(Math.round(Number(theAerialInfo[a][7])*10)/10) + "\",\"aerial\");'";
							if(isChecked){
								supplierinfoHtml += " checked ";
							}
							supplierinfoHtml += "></td>";									
						}
						supplierinfoHtml += "</tr>";
						a++;
					}else{
						var lineCnt = 0;
						for(p=a;p<uptoIndex;p++){
							lineCnt++;
							//Get Supplier name
							var supplierName = "Not Defined";
							for(s=0;s<supplierCodes.length;s++){
								if(theAerialInfo[p][4] == supplierCodes[s]){
									supplierName = supplierNames[s];
									break;
								}
							}		
							supplierinfoHtml += "<tr align='center' valign='top'>";
							if(lineCnt ==1){			//Define class to disply table cells with
								var classToUse = "supplierinfoNoBot";
							}else if(lineCnt == (uptoIndex-a)){			
								var classToUse = "supplierinfoNoTop";
							}else{
								var classToUse = "supplierinfoNoTopBot";
							}
							
							var theResolution = String(Number(theAerialInfo[p][2]) * 100);
							if(theResolution.indexOf(".") != -1){
								theResolution = Math.round(Number(theResolution)*10)/10;
							}
	
							if(lineCnt ==1){
								supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>" + theResolution + "</td>";
								supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>" + theAerialInfo[p][3] + "</td>";
								supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>" + supplierName + ": " + theAerialInfo[p][0] + "</td>";
	
								if(theAerialInfo[p][6] != ""){
									supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'><img src='images/photo.jpg' witdh='22' height='18' alt='Click for sample' border='0' onclick='showSupplierEg(\"" + theAerialInfo[a][6] + "\");' /></td>";
								}else{
									supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>&nbsp;</td>";
								}
								
								if(theOutputType == "full"){	
									if(theAerialInfo[a][7] == ""){
										theAerialInfo[a][7] = "100";
									}
									if(Number(theAerialInfo[a][7]) == 100){
										outputCoverage = "<p style='color:#00FF00;font-weight:bold;'>";
									}else if(Number(theAerialInfo[a][7])  < 90){
										outputCoverage = "<p style='color:#FF0000;font-weight:bold;'>";
									}else{
										outputCoverage = "<p style='color:#FF9900;font-weight:bold;'>";
									}
									outputCoverage += String(Math.round(Number(theAerialInfo[a][7])*10)/10) + "</p>"
									supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>" + outputCoverage + "</td>";
								}	
							}else{
								supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>&nbsp;</td>";
								supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>&nbsp;</td>";
								supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>&nbsp;</td>";
								supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>&nbsp;</td>";
								
								if(theOutputType == "full"){
									supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>&nbsp;</td>";
								}
								
							}
							if(theOutputType == "full"){								
								//if(lineCnt ==1){
								//	supplierinfoHtml = supplierinfoHtml + "<td class='" + classToUse + "'>&nbsp;</td>";											
								//}else{
		
									/*if(theAerialInfo[a][7] == ""){
										theAerialInfo[a][7] = "100";
									}
									if(Number(theAerialInfo[a][7]) == 100){
										outputCoverage = "<p style='color:#00FF00;font-weight:bold;'>";
									}else if(Number(theAerialInfo[a][7])  < 90){
										outputCoverage = "<p style='color:#FF0000;font-weight:bold;'>";
									}else{
										outputCoverage = "<p style='color:#FF9900;font-weight:bold;'>";
									}
									outputCoverage += String(Math.round(Number(theAerialInfo[a][7])*10)/10) + "</p>"
									supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + outputCoverage + "</td>";*/

									//supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + String(Math.round(Number(theAerialInfo[p][7])*10)/10) + "</td>";
								//}
								supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + theResolution + "</td>";
								supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>+&pound;" + formatPrice(theAerialInfo[p][5]) + "</td>";
								
								//Define if this need to be selected ie. showing a selcted product need to list the suppliers chosen	
								var selectedProductIndex = getSelectedProductIndex();
								var isChecked = false;
								if(document.getElementById("productLayers" + selectedProductIndex).value != ""){
									var selectedSupplierInfoArray = document.getElementById("productLayers" + selectedProductIndex).value.split(",");
									for(s=0;s<selectedSupplierInfoArray.length;s++){
										var storedSupplierInfo = selectedSupplierInfoArray[s];
										var retrievedSupplierInfo = theAerialInfo[p][0] + "|" + theAerialInfo[p][3] + "|"  + theAerialInfo[p][2] + "|" + supplierName + "|"  + String(Math.round(Number(theAerialInfo[p][7])*10)/10);
										retrievedSupplierInfo = retrievedSupplierInfo.replace(/,/g,"-com-");	//Adjustments to make agree with what been stored/
										if(retrievedSupplierInfo.indexOf("*") == 0){			
											retrievedSupplierInfo = retrievedSupplierInfo.substring(1,retrievedSupplierInfo.length);
										}
										if(storedSupplierInfo == retrievedSupplierInfo){
											isChecked = true
											break;
										}
									}
								}
								supplierinfoHtml += "<td class='supplierinfo'><input type='checkbox' name='supp1' onclick='setSupplier(this,\"" + theAerialInfo[p][0] + "|" + theAerialInfo[p][3] + "|" + theAerialInfo[p][2] + "|" + supplierName + "\",\""  + theAerialInfo[p][5] + "\",\""  + String(Math.round(Number(theAerialInfo[p][7])*10)/10) + "\",\"aerial\");'";
								if(isChecked){
									supplierinfoHtml += " checked ";
								}
								supplierinfoHtml += "></td>";			
							}
							supplierinfoHtml += "</tr>";		
						}
						a = uptoIndex;
					}
				}
				supplierinfoHtml += "</table>";
			}			
			//************************************************Show Height suppliers
			if(theHeightInfo.length > 0){
				
				if(theOutputType == "full"){
					var thecolspan = "7";
					var theTableWidth = "700px";
				}else{
					var thecolspan = "5";
					var theTableWidth = "500px";
				}
				supplierinfoHtml += "<table width='" + theTableWidth + "' cellpadding='0' cellspacing='0'>";
				if(theOutputType == "full"){
					supplierinfoHtml += "<tr valign='top'><td colspan='" + thecolspan + "'><h2>Height Data</h2></td></tr>";				
					supplierinfoHtml += "<tr valign='top'><td colspan='" + thecolspan + "'><p style='color:#24318D;'>The following suppliers have been found in this area. Please select one or more options from the available datasets listed.</p><br/></td></tr>";								
				}else{
				
					if(theOutputType2 != "overlay"){
					//	supplierinfoHtml += "<tr valign='top'><td colspan='" + thecolspan + "'>The following suppliers have been found in this area. To purchase choose the product above and define your data area.<br/>You will then be able to choose the specific data supplier.</p><br/></td></tr>";			
					}
					//supplierinfoHtml += "<tr valign='top'><td colspan='" + thecolspan + "'><h2>Height Data</h2></td></tr>";
					//document.getElementById("supplierMessages").innerHTML = "<p>The following is available for this location.<br/>To purchase choose the product to the left and define your data area.<br/>You will then be able to choose the specific data supplier for price and availability.";			
					document.getElementById("supplierMessages").innerHTML = "<p><img src='images/select.jpg' alt='Select Now!' onmouseover='this.src=\"images/selectON.jpg\"' onmouseout='this.src=\"images/select.jpg\"' onclick='getProductInfo(\"NULL_HEIGHT\",\"new\");'/><br/>(After selection define your data area.<br/>You will then be able to choose the specific data supplier for price and availability.)</p>";			
				}
				if(theOutputType != "full"){	
					supplierinfoHtml += "<tr valign='top'><td colspan='3'><p>The following are available for this location.</p></td></tr>";	
				}
				//Write header
				supplierinfoHtml += "<tr valign='top'>";
				supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Name</strong></p></td>"	
				supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Res.(cm)</strong></p></td>";
				supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Dates</strong></p></td>";	
				supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Preview</strong></p></td>";		
				if(theOutputType == "full"){
					supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>% Coverage</strong></p></td>";
					supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Price<br/>(excl. VAT)</strong></p></td>";
					supplierinfoHtml += "<td class='supplierinfoheader'><p><strong>Add?</strong></p></td>";					
				}				
				supplierinfoHtml += "</tr>";							
				//Re-replace back in " " and "," in the date entries
				for(c=0;c<theHeightInfo.length;c++){
					theHeightInfo[c][3] = theHeightInfo[c][3].replace(/-space-/g, " ");
					theHeightInfo[c][3] = theHeightInfo[c][3].replace(/-comma-/g, ",");
					theHeightInfo[c][3] = theHeightInfo[c][3].replace(/'/g, "");
				}	
				for(h=0;h<theHeightInfo.length;h++){
					supplierinfoHtml += "<tr valign='top'>";	
					supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + theHeightInfo[h][0] + "</td>";
					supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + Number(theHeightInfo[h][2]) * 100 + " cm.</td>";
					supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + theHeightInfo[h][3] + "</td>";
					///Show preview link if covereage less than 100%
					if(Number(theHeightInfo[h][7]) < 100){
						if(theHeightInfo[h][0].substring(0,1) == "*"){
							var theCode = theHeightInfo[h][0].substring(1,theHeightInfo[h][0].length);
						}else{					
							var theCode = theHeightInfo[h][0];
						}					
						var theHeightExtent = getHeightExtent();
						supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'><img src='images/photo.jpg' witdh='22' height='18' style='cursor:pointer;' alt='Click for preview of coverage' border='0' onclick='showHeightCoverage(\"" + theCode + "\",\"" + theHeightExtent + "\");' /></td>";			
					}else{
						supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>&nbsp;</td>";
					}	
					if(theOutputType == "full"){
						if(theHeightInfo[h][7] == ""){
							theHeightInfo[h][7] = "100";
						}
						if(Number(theHeightInfo[h][7]) == 100){
							outputCoverage = "<p style='color:#00FF00;font-weight:bold;'>";
						}else if(Number(theHeightInfo[h][7])  < 90){
							outputCoverage = "<p style='color:#FF0000;font-weight:bold;'>";
						}else{
							outputCoverage = "<p style='color:#FF9900;font-weight:bold;'>";
						}
						outputCoverage += String(Math.round(Number(theHeightInfo[h][7])*10)/10) + "</p>"
						supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>" + outputCoverage + "</td>";
						supplierinfoHtml = supplierinfoHtml + "<td class='supplierinfo'>+&pound;" + formatPrice(theHeightInfo[h][5]) + "</td>";													
						//Define if this need to be selected ie. showing a selcted product need to list the suppliers chosen	
						var selectedProductIndex = getSelectedProductIndex();
						var isChecked = false;
						if(document.getElementById("productLayers" + selectedProductIndex).value != ""){
							var selectedSupplierInfoArray = document.getElementById("productLayers" + selectedProductIndex).value.split(",");
							for(s=0;s<selectedSupplierInfoArray.length;s++){
								var storedSupplierInfo = selectedSupplierInfoArray[s];
								var retrievedSupplierInfo = theHeightInfo[h][0] + "|" + theHeightInfo[h][3] + "|"  + String(Math.round(Number(theHeightInfo[h][7])*10)/10);
								retrievedSupplierInfo = retrievedSupplierInfo.replace(/,/g,"-com-");	//Adjustments to make agree with what been stored/
								if(retrievedSupplierInfo.indexOf("*") == 0){			
									retrievedSupplierInfo = retrievedSupplierInfo.substring(1,retrievedSupplierInfo.length);
								}
								if(storedSupplierInfo == retrievedSupplierInfo){
									isChecked = true
									break;
								}
							}
						}		
					}	
					if(theOutputType == "full"){
						supplierinfoHtml +=  "<td class='supplierinfo'><input type='checkbox' name='supp1' onclick='setSupplier(this,\"" + theHeightInfo[h][0] + "|" + theHeightInfo[h][3] + "\",\""  + theHeightInfo[h][5]  + "\",\""  + String(Math.round(Number(theHeightInfo[h][7])*10)/10) + "\",\"height\");'"; 				
						if(isChecked){
							supplierinfoHtml += " checked ";
						}else{
						supplierinfoHtml += ">";
						}	
					}
					supplierinfoHtml += "</td>";
					supplierinfoHtml += "</tr>";		
				}
				supplierinfoHtml += "</table>";
			}					
			//Show footer info
			if(((aerialCnt != 0 ) || (heightCnt =! 0)) && (theOutputType == "full")){
				supplierinfoHtml += "<table width='" + theTableWidth + "' cellpadding='0' cellspacing='0'>";
				if(heightCnt != 0){
					supplierinfoHtml += "<tr valign='top'>";	
					supplierinfoHtml += "<td colspan='" + thecolspan + "'><br/><p>DTM - Digital Terrain Model (bare earth)<br/>";
					supplierinfoHtml += "DSM - Digital Surface Model (vegetation & buildings included)</p></td>";
					supplierinfoHtml += "</tr>";					
					supplierinfoHtml += "<tr  valign='top'>";	
					supplierinfoHtml += "<td colspan='" + thecolspan + "'><p>Contour format available in shopping basket</p></td>";
					supplierinfoHtml += "</tr>";
				}				
				supplierinfoHtml += "<tr  valign='top'>";	
				supplierinfoHtml += "<td colspan='" + thecolspan + "'><p>*Please allow up to 1 business day<br/>";
				supplierinfoHtml += "<a href='#'>Click here</a> for information on the details shown.</p></td>";
				supplierinfoHtml += "</tr>";
			}
			if(theOutputType == "full"){
				if(((theOutputType2 == "overlay") && (theSupplierInfo[1] == "HEIGHT")) || (theOutputType2 != "overlay")) {
					supplierinfoHtml += "<tr valign='top'><td colspan='" + thecolspan + "'>&nbsp;</td></tr>";
					supplierinfoHtml += "<tr valign='top'>";	
					supplierinfoHtml += "<td colspan='" + thecolspan + "'><p align='right'><img src='images/ok.jpg' alt='ok' onmouseover='this.src=\"images/okON.jpg\"' onmouseout='this.src=\"images/ok.jpg\"' onclick='closeSupplierListing();' /></p></td>";
					supplierinfoHtml += "</tr>";
				}
			}	
			supplierinfoHtml += "</table>";								
			//Show supplier data 
			if(theSupplierInfo[1] == "AERIAL"){
				document.getElementById("supplierinfoAerial_layer").innerHTML = supplierinfoHtml; 			
			}else if(theSupplierInfo[1] == "HEIGHT"){
				document.getElementById("supplierinfoHeight_layer").innerHTML = supplierinfoHtml; 	
			}else{
			//	document.getElementById("supplierinfo_layer").innerHTML = supplierinfoHtml; 
			}
			//Ensure the content to show the info is visible
			zchm.slideIn();	
		}
		callCufon();			
	}
}
//****************Get the year to use when checking dates for grouping the returned product layers (get start date where range specified)
function getCheckDate(theDataString){
	//Get rid of "s" eg in 1970s
	if(theDataString.indexOf("s") == (theDataString.length-1)){
		theDataString = theDataString.substring(0,(theDataString.length-1));
	}
	//Swap date string to dd/mm/yyyy format if necessary
	theDataString = theDataString.replace(/ January/g,"/01/");
	theDataString = theDataString.replace(/ February/g,"/02/");
	theDataString = theDataString.replace(/ March/g,"/03/");
	theDataString = theDataString.replace(/ April/g,"/04/");
	theDataString = theDataString.replace(/ May/g,"/05/");
	theDataString = theDataString.replace(/ June/g,"/06/");
	theDataString = theDataString.replace(/ July/g,"/07/");
	theDataString = theDataString.replace(/ August/g,"/08/");
	theDataString = theDataString.replace(/ September/g,"/09/");
	theDataString = theDataString.replace(/ October/g,"/10/");
	theDataString = theDataString.replace(/ November/g,"/11/");
	theDataString = theDataString.replace(/ December/g,"/12");
	theDataString = theDataString.replace(/st/g,"");
	theDataString = theDataString.replace(/nd/g,"");
	theDataString = theDataString.replace(/rd/g,"");
	theDataString = theDataString.replace(/th/g,"");	
	var sep = theDataString.indexOf("-");
	var sep1 = theDataString.indexOf("to");
	var sep2 = theDataString.indexOf(",");
	if(sep != -1){
		var toCheckDate1 = theDataString.substring((sep+1),theDataString.length);
	}else if(sep1 != -1){	
		var toCheckDate1 = theDataString.substring((sep1+2),theDataString.length);
	}else if(sep2 != -1){
		var toCheckDate1 = theDataString.substring((sep2+1),theDataString.length);		
	}else{
		var toCheckDate1 = theDataString;
	}
	toCheckDateAsDate = null;				
	//Remove specific dates to get the year only
	var sep3 = toCheckDate1.indexOf("/");
	if(sep3 != -1){
		tempArray = toCheckDate1.split("/");
		if(tempArray.length == 3){								//Full date defined
		   	usFormatCheckDate = tempArray[1] + "/" + tempArray[0] + "/" + tempArray[2];
			toCheckDateAsDate = new Date(usFormatCheckDate);
		}else{													//Only year defined create a date object for 1st jan
			for(s=0;s < toCheckDate1.length;s++){
				if(toCheckDate1.substring(s,s+1) == "/"){
					splitAt = s;
				}
			}
			toCheckDate1 = toCheckDate1.substring((splitAt+1),toCheckDate1.length);
			usFormatCheckDate = "01/01/" + toCheckDate1;
			toCheckDateAsDate = new Date(usFormatCheckDate);
		}
		
		/*for(s=0;s < toCheckDate1.length;s++){
			if(toCheckDate1.substring(s,s+1) == "/"){
				splitAt = s;
			}
		}
		toCheckDate1 = toCheckDate1.substring((splitAt+1),toCheckDate1.length);
		*/
		
		
		
	}else{																//Only year defined create a date object for 1st jan
		usFormatCheckDate = "01/01/" +toCheckDate1;
		toCheckDateAsDate = new Date(usFormatCheckDate);
	}
	
	return(toCheckDateAsDate);
	//return(Number(toCheckDate1));
}
//****************Display the example of the data in new window
function showSupplierEg(passedQstring){
	if(passedQstring != ""){
		winName =  "suppliereg";  
		features = "menubar=no,toolbar=no,scrollbars=yes,resizable=no,width=290,height=290";		
		theURL = "supplier_sample.htm?" + passedQstring;
		window.open(theURL,winName,features);
	}
}
//****************Display the % coverage of height data in new window
function showHeightCoverage(passedCode,passedCoords){
	if((passedCode != "") && (passedCoords != "")){
		winName =  "previewcov";  
		features = "menubar=no,toolbar=no,scrollbars=yes,resizable=no,width=250,height=250";
		theURL = "heightCoverage.php?code=" + passedCode + "&coords=" + passedCoords;		
		window.open(theURL,winName,features);
	}
}
//****************Store the choosen supplier (or choose this product if the supplier choosen does not relate to the selected product)
function setSupplier(theCheckBox,theLayerCode,thePriceToAdd,theCovPercent,supplierType){
	if((document.varform.suppliers.value != "no") && (document.varform.suppliers.value != "")){				//Check it allows suppliers
		//Get the selected product
		var theSelectedProductIndex = getSelectedProductIndex();
		if(theSelectedProductIndex == -1){																	//No product selected - add as new product
			addAsNewProduct();
		}else{
			if((document.getElementById("data" + theSelectedProductIndex).value == "OSMM") && (document.getElementById("extent" + theSelectedProductIndex).value == "")){
				//Do nothing
			}else if((document.getElementById("data" + theSelectedProductIndex).value != document.varform.data.value) && (document.getElementById("data" + theSelectedProductIndex).value != "OSMM")){			//Supplier not for the seelcted product - add as new product
				addAsNewProduct();
			}else{
				if(document.getElementById("extent" + theSelectedProductIndex).value == ""){							//Extent not for the seelcted product - add as new product
					addAsNewProduct();
				}else{																									//The choosen supplier layer is the same as the product - supplier to the product order
					//Replace comma in passed layer code (commas are used for seperating each choosen product layer)
					theLayerCode = theLayerCode.replace(/,/g,"-com-");	
					if(theLayerCode.indexOf("*") == 0){			//Strip off * to indicate 1 extra day to supply
						theLayerCode = theLayerCode.substring(1,theLayerCode.length);
					}
					var currentLayers = document.getElementById("productLayers" + theSelectedProductIndex).value;
					var currentLayersPrice =  Number(document.getElementById("layersPrice" + theSelectedProductIndex).value);
					var currentOverlays = document.getElementById("layeroverlays" + theSelectedProductIndex).value;
					var currentPrices = document.getElementById("layersPrices" + theSelectedProductIndex).value;					
					newLayersPrice = 0;	
					//Add product code to currently selected data layers and price to add
					if(theCheckBox.checked){
						//Store layers and type of overlay (aerial or height)
						if(currentLayers == ""){
							document.getElementById("productLayers" + theSelectedProductIndex).value = theLayerCode + "|" + theCovPercent;							
						}else{
							document.getElementById("productLayers" + theSelectedProductIndex).value = currentLayers + "," + theLayerCode + "|" + theCovPercent;				
						}
						newLayersPrice = currentLayersPrice + Number(thePriceToAdd);
						if(currentOverlays == ""){
							document.getElementById("layeroverlays" + theSelectedProductIndex).value = supplierType;							
						}else{
							document.getElementById("layeroverlays" + theSelectedProductIndex).value = currentOverlays + "," + supplierType;				
						}
						if(currentPrices == ""){
							document.getElementById("layersPrices" + theSelectedProductIndex).value = thePriceToAdd;							
						}else{
							document.getElementById("layersPrices" + theSelectedProductIndex).value = currentPrices + "," + thePriceToAdd;				
						}						
						//Show breakdown of selected in user selcted area
						var overlaysToTest = document.getElementById("layeroverlays" + theSelectedProductIndex).value;
						var overlaysToTestArray = overlaysToTest.split(",");
						var selectedHeightCnt = 0;
						var selectedAerialCnt = 0;
						for(c=0;c < overlaysToTestArray.length;c++){
							if(overlaysToTestArray[c] == "height"){
								selectedHeightCnt++;
							}else if(overlaysToTestArray[c] == "aerial"){
								selectedAerialCnt++;
							}
						}
						if((selectedAerialCnt == 0) && (selectedHeightCnt == 0)){
							if((document.varform.data.value == "NULL_AERIAL") || (document.varform.data.value == "NULL_HEIGHT")){
								var outputBreakdown = "<a href='javascript:showFurtherInfo(\"suppliers\"," + theSelectedProductIndex + ");'>Click to show data suppliers</a>";	
							}else{
								var outputBreakdown = "<a href='javascript:showFurtherInfo(\"mastermap\"," + theSelectedProductIndex + ");'>Add Aerial/Height?</a>";	
							}
						}else{
							if((document.varform.data.value == "NULL_AERIAL") || (document.varform.data.value == "NULL_HEIGHT")){
								var infoType = "suppliers";
							}else{
								var infoType = "mastermap";
							}
							if(document.varform.data.value == "NULL_AERIAL"){
								var outputBreakdown = "<p style='color:#24318D;'>+Aerial (" + String(selectedAerialCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "\"," + theSelectedProductIndex + ");'>&nbsp;&nbsp;&nbsp;&nbsp;Add/Edit</a></p>";	
							}else if(document.varform.data.value == "NULL_HEIGHT"){
								var outputBreakdown = "<p style='color:#24318D;'>+Height (" + String(selectedHeightCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "\"," + theSelectedProductIndex + ");'>&nbsp;&nbsp;&nbsp;&nbsp;Add/Edit</a></p>";		
							}else{    //MasterMap product
								var outputBreakdown = "<p style='color:#24318D;'>+Aerial (" + String(selectedAerialCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "aerial\"," + theSelectedProductIndex + ");'>&nbsp;&nbsp;&nbsp;&nbsp;Add/Edit</a></p>";	
								outputBreakdown += "<p style='color:#24318D;'>+Height (" + String(selectedHeightCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "height\"," + theSelectedProductIndex + ");'>&nbsp;&nbsp;&nbsp;&nbsp;Add/Edit</a></p>";		
							}
							//var outputBreakdown = "";
							//if(selectedAerialCnt > 0){
								//var outputBreakdown = "<p style='color:#24318D;'>+Aerial (" + String(selectedAerialCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "\"," + theSelectedProductIndex + ");'>Edit</a></p>";	
							//}
							//if(selectedHeightCnt > 0){
								//if(outputBreakdown == ""){
								//	outputBreakdown = "<p style='color:#24318D;'>+Height (" + String(selectedHeightCnt) + ")   <a href='javascript:showFurtherInfo(\"suppliers\"," + theSelectedProductIndex + ");'>Edit</a></p>";	
								//}else{
								//	outputBreakdown += "<p style='color:#24318D;'>+Height (" + String(selectedHeightCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "\"," + theSelectedProductIndex + ");'>Edit</a></p>";		
								//}
							//}
						}
						document.getElementById("furtherInfo" + theSelectedProductIndex).innerHTML = outputBreakdown;
						document.getElementById("furtherInfo" + theSelectedProductIndex).style.display = "block";				
					}else{				//Remove product code to currently selected data layers
						var newLayers = "";
						var newCoverage = "";
						var newOverlays = "";
						var newPrices = "";
						if(currentLayers.indexOf(",") == -1){			
						}else{
							var tempArray = new Array();
							tempArray = currentLayers.split(",");
							var tempArray2 = new Array();
							tempArray2 = currentOverlays.split(",");
							var tempArray3 = new Array();
							tempArray3 = currentPrices.split(",");
							for(t=0;t<tempArray.length;t++){
								if((theLayerCode + "|" + theCovPercent) == tempArray[t]){
									//ignore
									newLayersPrice = currentLayersPrice - Number(thePriceToAdd);
								}else{
									newLayers = newLayers + "," + tempArray[t];
									newOverlays = newOverlays + "," + tempArray2[t];
									newPrices = newPrices + "," + tempArray3[t];
								}
							}
							newLayers = newLayers.substring(1,newLayers.length);
							newOverlays = newOverlays.substring(1,newOverlays.length);
							newPrices = newPrices.substring(1,newPrices.length);
						}
						document.getElementById("productLayers" + theSelectedProductIndex).value = newLayers;
						document.getElementById("layeroverlays" + theSelectedProductIndex).value = newOverlays;
						document.getElementById("layersPrices" + theSelectedProductIndex).value = newPrices;
						
						//Update the product description that displays to the user
						var currentDisplay = document.getElementById("product" + theSelectedProductIndex + "content").innerHTML;
						if(currentDisplay.indexOf("<p>+") != -1){
							currentDisplay = currentDisplay.substring(0,(currentDisplay.indexOf("<p>+") - 1));
						}
		
						var overlaysToTest = document.getElementById("layeroverlays" + theSelectedProductIndex).value;
						var overlaysToTestArray = overlaysToTest.split(",");
						var selectedHeightCnt = 0;
						var selectedAerialCnt = 0;
						for(c=0;c < overlaysToTestArray.length;c++){
							if(overlaysToTestArray[c] == "height"){
								selectedHeightCnt++;
							}else if(overlaysToTestArray[c] == "aerial"){
								selectedAerialCnt++;
							}
						}
						if((selectedAerialCnt == 0) && (selectedHeightCnt == 0)){
							if((document.varform.data.value == "NULL_AERIAL") || (document.varform.data.value == "NULL_HEIGHT")){
								var outputBreakdown = "<a href='javascript:showFurtherInfo(\"suppliers\"," + theSelectedProductIndex + ");'>Click to show data suppliers</a>";	
							}else{
								var outputBreakdown = "<a href='javascript:showFurtherInfo(\"mastermap\"," + theSelectedProductIndex + ");'>Add Aerial/Height?</a>";	
							}
						}else{
							if((document.varform.data.value == "NULL_AERIAL") || (document.varform.data.value == "NULL_HEIGHT")){
								var infoType = "suppliers";
							}else{
								var infoType = "mastermap";
							}	
							if(document.varform.data.value == "NULL_AERIAL"){
								var outputBreakdown = "<p style='color:#24318D;'>+Aerial (" + String(selectedAerialCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "\"," + theSelectedProductIndex + ");'>&nbsp;&nbsp;&nbsp;&nbsp;Add/Edit</a></p>";	
							}else if(document.varform.data.value == "NULL_HEIGHT"){
								var outputBreakdown = "<p style='color:#24318D;'>+Height (" + String(selectedHeightCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "\"," + theSelectedProductIndex + ");'>A&nbsp;&nbsp;&nbsp;&nbsp;dd/Edit</a></p>";		
							}else{    //MasterMap product
								var outputBreakdown = "<p style='color:#24318D;'>+Aerial (" + String(selectedAerialCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "aerial\"," + theSelectedProductIndex + ");'>&nbsp;&nbsp;&nbsp;&nbsp;Add/Edit</a></p>";	
								outputBreakdown += "<p style='color:#24318D;'>+Height (" + String(selectedHeightCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "height\"," + theSelectedProductIndex + ");'>&nbsp;&nbsp;&nbsp;&nbsp;Add/Edit</a></p>";		
							}
							
							//var outputBreakdown = "";
							//if(selectedAerialCnt > 0){
							//	var outputBreakdown = "<p style='color:#24318D;'>+Aerial (" + String(selectedAerialCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "\"," + theSelectedProductIndex + ");'>Edit</a></p>";	
							//}
							//if(selectedHeightCnt > 0){
							//	if(outputBreakdown == ""){
							//		outputBreakdown = "<p style='color:#24318D;'>+Height (" + String(selectedHeightCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "\"," + theSelectedProductIndex + ");'>Edit</a></p>";	
							//	}else{
								//	outputBreakdown += "<p style='color:#24318D;'>+Height (" + String(selectedHeightCnt) + ")   <a href='javascript:showFurtherInfo(\"" + infoType + "\"," + theSelectedProductIndex + ");'>Edit</a></p>";		
							//	}
							//}
						}
						document.getElementById("furtherInfo" + theSelectedProductIndex).innerHTML = outputBreakdown;
						document.getElementById("furtherInfo" + selectedIndex).style.display = "block";	
					}	
					//Update the price
					document.getElementById("layersPrice" + theSelectedProductIndex).value = newLayersPrice;		//Store layers price	
					updatePrice(theSelectedProductIndex);
				}
			}
		}		
	}
}
//****************Selects a product (Aerial or Height) - called when set supplier called but no approproate selected product to add the supplier to
function addAsNewProduct(){			
	var totalProducts = Number(document.getElementById("nofProducts").value);
	var selectedIndex = -1;
	var addingNew = false;
	if(totalProducts > 0){					//Check if a product is currently selected							
		selectedIndex = getSelectedProductIndex();
		if(selectedIndex != -1){			
			var answer = confirm("Do you want to replace the currently selected product with this one?");			
			if(answer == false){								
				selectedIndex = getNextEmptyIndex();							//Add as new product
				addingNew = true;
			}
		}else{
			selectedIndex = getNextEmptyIndex();								//Add as new product
			addingNew = true;
		}
	}else{																	//Define the selected index as the next available 
		selectedIndex = getNextEmptyIndex();
		addingNew = true;
	}					
	var tooManyProducts = false;				
	if((addingNew) && ((totalProducts+1) > maxProducts)){					//Is a new product added to the selected list - Ensure it does not exceed the max no. (5)
		showDialog('Warning','Maximum number of selected products has been reached.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');	
		tooManyProducts = true;
	}		
	if(tooManyProducts == false){												//Add a new product or replace an existing one		
		//Adding a new product - update the total number selected
		if(addingNew){		
			document.getElementById("nofProducts").value = Number(document.getElementById("nofProducts").value) + 1;
			hideShowProductMessage();
			var theAction = "new";											//Is a new product added to the list
		}else{
			var theAction = "change";										//Is a change to an existing selected product
		}
		//If changing the product check if the extent is valid for the new product
		if(theAction == "change"){
			var theExtentType = "";
			var theCurrentExtentID = document.getElementById("extent" + String(selectedIndex)).value;
			for(p=0; p < extentLayer.features.length;p++){
				if(extentLayer.features[p].attributes.id == theCurrentExtentID){
					if(String(extentLayer.features[p].geometry).indexOf("POINT") == 0){
						theExtentType = "point";
					}else{
						theExtentType = "poly";
					}
				}	
			}
			if(theExtentType != ""){
				var isOK = checkProductExtentAgree(document.varform.data.value,"no",theExtentType,theCurrentExtentID);
			}else{
				var isOK = true;
			}
			if(isOK == false){													//Cannot replace - add as a new item if possible
				if((totalProducts+1) > maxProducts){			
					showDialog('Warning','Cannot replace the current selected product as current extent type is incompatible.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');	
				}else{
					showDialog('Warning','Cannot replace the current selected product as current extent type is incompatible. This product will be added as a new item.<br/><br/><input type="button" value="Close" id="closeButton" onclick="hideDialog();" >','warning');	
					selectedIndex = getNextEmptyIndex();
					document.getElementById("nofProducts").value = Number(document.getElementById("nofProducts").value) + 1; //Update the total number of products accordingly.
					hideShowProductMessage();
					var theAction = "new";
				}		
			}
		}
	}		
	if(tooManyProducts == false){					
		//Add a new product or replace an existing one
		//Show the selected product in list of selcted products
		document.getElementById("Product " + selectedIndex + "container").style.display = "block";		//Show layer to display selected product info
		if(document.varform.dataDescript.value != document.varform.fullDataName.value){
			var descToPrint = document.varform.dataDescript.value + ": " + document.varform.fullDataName.value;
		}else{
			var descToPrint = document.varform.dataDescript.value;
		}	
		document.getElementById("furtherInfo" + selectedIndex).innerHTML = "<a href='javascript:getSupplierInfo(true);'><img src='images/info.jpg' alt='further info' /></a>";											
		document.getElementById("product" + selectedIndex + "content").innerHTML = "<h5>" + descToPrint + "</h5>";
		document.getElementById("data" + selectedIndex).value = document.varform.data.value;
		document.getElementById("datadesc" + selectedIndex).value = document.varform.dataDescript.value;
		document.getElementById("scale" + selectedIndex).value = document.varform.scale.value;
		document.getElementById("maxsize" + selectedIndex).value = document.varform.maxsize.value;
		document.getElementById("fulldataname" + selectedIndex).value = descToPrint;
		document.getElementById("hassuppliers" + selectedIndex).value = "yes";
		//Ensure this radio button is selected
		for (var i=0; i < document.frmSelected.selectedproducts.length; i++){	
			if(Number(document.frmSelected.selectedproducts[i].value) == selectedIndex){
				document.frmSelected.selectedproducts[i].checked = true;		
				break;	
			}
		}	
		//If selected product is changed need to update the price (providing an extent is associated)
		if(theAction == "change"){
			if(document.getElementById("extent" + selectedIndex).value != ""){
				updatePrice(selectedIndex);
			}
		}		
	}
	//Show the define extent area and hide product options
	zchm.slideOut();
	displayTools("Define");
	showTools("Define");
	toolbox.slideIn();
	callCufon();
}
//****************Define the extent of the order area and minxy-maxxy
function getHeightExtent(){
	var geomString = "";
	var theSelectedProductIndex = getSelectedProductIndex();
	var theExtentID = document.getElementById("extent" + theSelectedProductIndex).value;	
	if(theExtentID != ""){	
		var extentGeom = getExtentGeomByID(theExtentID);
		var geomString = String(extentGeom).substring(9,(String(extentGeom).length -2));
		var newGeomString = "";
		tempArray = geomString.split(",");
		for(c=0;c < tempArray.length; c++){
			var tempArray2 = tempArray[c].split(" ");
			if(c==0){
				newGeomString = String(Math.round(Number(tempArray2[0]))) + "," + String(Math.round(Number(tempArray2[1])))
			}else{
				newGeomString += "," + String(Math.round(Number(tempArray2[0]))) + "," + String(Math.round(Number(tempArray2[1])));
			}
		}
	}			
	return newGeomString;
}
//****************Cancels a supplier request
function cancelSupplierReq(theType){
	if(theType == "height"){	
		if(getOverviewHeightSuppliersReq != null){
			getOverviewHeightSuppliersReq.abort();
		}
		if(getHeightSuppliersReq != null){
			getHeightSuppliersReq.abort();
		}
	}else if(theType == "aerial"){
		if(getOverviewAerialSuppliersReq != null){
			getOverviewAerialSuppliersReq.abort();
		}
		if(getAerialSuppliersReq != null){
			getAerialSuppliersReq.abort();
		}
	}
}
//****************Cancels a supplier request and closes the slide out
function cancelSupplierReq2(theType){
	if(theType == "height"){	
		if(getOverviewHeightSuppliersReq != null){
			getOverviewHeightSuppliersReq.abort();
			zchm.slideOut();
		}
		if(getHeightSuppliersReq != null){
			getHeightSuppliersReq.abort();
			zchm.slideOut();
		}
	}else if(theType == "aerial"){
		if(getOverviewAerialSuppliersReq != null){
			getOverviewAerialSuppliersReq.abort();
			zchm.slideOut();
		}
		if(getAerialSuppliersReq != null){
			getAerialSuppliersReq.abort();
			zchm.slideOut();
		}
	}
}
//****************Closes supplier listing (when pressing "ok")
function closeSupplierListing(){
	zchm.slideOut();
	//kill any height-aerial lookups
	cancelSupplierReq("aerial");
	cancelSupplierReq("height");
}
//---------------------------------------End of file
