var SAMPLE_POST = HOST_URL + '/search/v1/FUNCTION?key=YOUR_KEY_HERE&callback=renderBasicSearchNarrative';
var searchType = '';


function showBasicSearchURL() {
    var safe = SAMPLE_POST + "&shapePoints=40.099998,-76.305603";
    document.getElementById('divBasicSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','search');
};

function showRadiusSearchURL() {
    var safe = SAMPLE_POST + "&origin=40.099998,-76.305603";
    document.getElementById('divRadiusSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','radius');
};

function showRectangleSearchURL() {
    var safe = SAMPLE_POST + "&boundingBox=40.099998,-77.305603,39.099998,-75.305603";
    document.getElementById('divRectangleSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','rectangle');
};

function showPolygonSearchURL() {
    var safe = SAMPLE_POST + "&polygon=40.078811,-76.730422,41.078811,-74.730422,40.078811,-74.730422,39.961879,-76.730422,39.961879,-76.730422,40.078811,-76.730422";
    document.getElementById('divPolygonSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','polygon');
};

function showCorridorSearchURL() {
    var safe = SAMPLE_POST + "&line=g|jsFjnvpMCo@??wCd@_D\\uHz@kIhAqHt@}Hr@{JhAuEl@sE`@wEl@}Ef@??[eEUaEUgECg@??Sa@MMUG??sFbAo@Da@B{JfAaGd@kKv@}E^??}BPYBaLx@qALyHp@{JbAgD^y@LwALe@FeANsALmIv@gBLeDTiMz@cDT{DZaEFwE?_EOwAK}Dg@}Bc@y@O}Cc@gCg@aBg@oAc@sBy@oBw@aAa@yBkAoGqDkCwAkBaA_@KuAe@yBs@YI{@SoBk@aBc@{@Oq@Cs@Hy@ZcBx@a@RgCtAe@Tg@Vk@Lu@JsAHcDFq@DsGZaFP_ABo@BuAD}HCkA?g@BuGHkAAsDFeFBwTLmCBcGBoKBgNNeDF{EDoCB_AA{@AyCMgFQuCSoBQeBKyAMiDKuFMmCE}Lg@gDI]CqHgAsDg@gEc@uAGuAKuDU_BKiBIs@KwFeAu@OsA[u@AmAFwDt@mCn@C@yBd@iG~AoFvAoObEeGxAgCd@aKxA}Dh@oBNqHDmEB{FDgEDyBJ???W&shapeFormat=cmp";
    document.getElementById('divCorridorSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','corridor');
};

function showBaseSearchURL() {
    var safe = SAMPLE_POST + "&shapePoints=40.099998,-76.305603";
    document.getElementById('divBaseSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','search');
};

function showDriveTimeSearchURL() {
    var safe = SAMPLE_POST + "&shapePoints=40.099998,-76.305603";
	if (document.getElementById('units').value != "") {
		safe += "&units=" + document.getElementById('units').value;
	}
    document.getElementById('divDriveTimeSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','search');
};

function showHostedDataSearchURL() {
    var safe = SAMPLE_POST + "&shapePoints=40.099998,-76.305603&hostedData=MQA.test,city='lancaster',RecordId,Address1,Address2,City,State,Country,Phone";
    document.getElementById('divHostedDataSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','search');
};

function showRecordInfoSearchURL() {
    var safe = SAMPLE_POST + "&ids=280850,280860&hostedData=MQA.test,,RecordId,Address1,Address2,City,State,Country,Phone";
    document.getElementById('divRecordInfoSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','recordinfo');
};

function showMapDataSearchURL() {
    var safe = SAMPLE_POST + "&shapePoints=40.099998,-76.305603&mapData=navt,1049";
    document.getElementById('divMapDataSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','search');
};

function showRemoteDataSearchURL() {
    var safe = SAMPLE_POST + "&shapePoints=40.099998,-76.305603&remoteData=1,point,40.099998,-76.305603";
    document.getElementById('divRemoteDataSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','search');
};

function showAllDataSearchURL() {
    var safe = SAMPLE_POST + "&shapePoints=40.099998,-76.305603&hostedData=MQA.test,city='lancaster',RecordId,Address1,Address2,City,State,Country,Phone&mapData=navt,1049&remoteData=1,point,40.099998,-76.305603&remoteData=2,linestring,40.099998,-76.305603,41.099998,-76.305603,41.099998,-77.305603&remoteData=3,polygon,40.099998,-76.305603,41.099998,-76.305603,41.099998,-77.305603,40.099998,-76.305603";
    document.getElementById('divAllDataSearchUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace('FUNCTION','search');
};

function doBasicSearchClick() {
	searchType = "helloworld";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','search') + "&shapePoints=40.099998,-76.305603";
	var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doRadiusSearchClick() {
	searchType = "radius";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','radius') + "&origin=40.099998,-76.305603";
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doRectangleSearchClick() {
	searchType = "rectangle";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','rectangle') + "&boundingBox=40.099998,-77.305603,39.099998,-75.305603";
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doPolygonSearchClick() {
	searchType = "polygon";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','polygon') + "&polygon=40.078811,-76.730422,41.078811,-74.730422,40.078811,-74.730422,39.961879,-76.730422,39.961879,-76.730422,40.078811,-76.730422";
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doCorridorSearchClick() {
	searchType = "corridor";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','corridor') + "&line=g|jsFjnvpMCo@??wCd@_D\\uHz@kIhAqHt@}Hr@{JhAuEl@sE`@wEl@}Ef@??[eEUaEUgECg@??Sa@MMUG??sFbAo@Da@B{JfAaGd@kKv@}E^??}BPYBaLx@qALyHp@{JbAgD^y@LwALe@FeANsALmIv@gBLeDTiMz@cDT{DZaEFwE?_EOwAK}Dg@}Bc@y@O}Cc@gCg@aBg@oAc@sBy@oBw@aAa@yBkAoGqDkCwAkBaA_@KuAe@yBs@YI{@SoBk@aBc@{@Oq@Cs@Hy@ZcBx@a@RgCtAe@Tg@Vk@Lu@JsAHcDFq@DsGZaFP_ABo@BuAD}HCkA?g@BuGHkAAsDFeFBwTLmCBcGBoKBgNNeDF{EDoCB_AA{@AyCMgFQuCSoBQeBKyAMiDKuFMmCE}Lg@gDI]CqHgAsDg@gEc@uAGuAKuDU_BKiBIs@KwFeAu@OsA[u@AmAFwDt@mCn@C@yBd@iG~AoFvAoObEeGxAgCd@aKxA}Dh@oBNqHDmEB{FDgEDyBJ???W&shapeFormat=cmp";
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doBaseSearchClick() {
	searchType = "base";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','search') + "&shapePoints=40.099998,-76.305603";
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doDriveTimeSearchClick() {
	searchType = "drivetime";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','search') + "&shapePoints=40.099998,-76.305603";
    if (document.getElementById('units').value != "") {
		newURL += "&units=" + document.getElementById('units').value;
	}
	var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doHostedDataSearchClick() {
	searchType = "hosteddata";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','search') + "&shapePoints=40.099998,-76.305603&hostedData=MQA.test,city='lancaster',RecordId,Address1,Address2,City,State,Country,Phone";
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doRecordInfoSearchClick() {
	searchType = "recordinfo";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','recordinfo') + "&ids=280850,280860&hostedData=MQA.test,,RecordId,Address1,Address2,City,State,Country,Phone";
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doMapDataSearchClick() {
	searchType = "mapdata";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','search') + "&shapePoints=40.099998,-76.305603&mapData=navt,1049";
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doRemoteDataSearchClick() {
	searchType = "remotedata";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','search') + "&shapePoints=40.099998,-76.305603&remoteData=1,point,40.099998,-76.305603";
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function doAllDataSearchClick() {
	searchType = "alldata";
    var newURL = SAMPLE_POST.replace('YOUR_KEY_HERE', APP_KEY).replace('FUNCTION','search') + "&shapePoints=40.099998,-76.305603&hostedData=MQA.test,city='lancaster',RecordId,Address1,Address2,City,State,Country,Phone&mapData=navt,1049&remoteData=1,point,40.099998,-76.305603&remoteData=2,linestring,40.099998,-76.305603,41.099998,-76.305603,41.099998,-77.305603&remoteData=3,polygon,40.099998,-76.305603,41.099998,-76.305603,41.099998,-77.305603,40.099998,-76.305603";
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = newURL;
    document.body.appendChild(script);
};

function renderBasicSearchNarrative(response) {
     var html = '';
    var i = 0;
    var j = 0;

    if (response.info.statuscode && (response.info.statuscode != 0)) {
        var text = "Whoops!  There was an error during the request";
		text += " [" + response.info.statuscode + "]:\n";
        if (response.info.messages) {
            for (i = 0; i < response.info.messages.length; i++) {
                text += response.info.messages[i] + "\n";
            }
        }
        alert(text);
    }
    
    if (response.collections) { // Location ambiguities!
        html = "<p>Whoops!  Ambiguous addresses found in request:</p><ol>";
        for (i = 0; i < response.collections.length; i++) {
            var collection = response.collections[i];
            for (j = 0; j < collection.length; j++) {
                html += '<li>';
                html += ' ' + (collection[j].adminArea5 || ' ');  
                html += ' ' + (collection[j].adminArea4 || ' ');  
                html += ' ' + (collection[j].adminArea3 || ' ');  
                html += ' ' + (collection[j].adminArea2 || ' ');  
                html += ' ' + (collection[j].adminArea1 || ' ');  
                html += '</li>';
            }
        }
        html += '</ol>';
        document.getElementById('advancedRadiusNarrative').innerHTML = html;
        return;        
    }
	
	if(response.searchResults){
		doPaging(response, "renderAdvancedRadiusResults");	
		html += '<table><tr><th colspan="5">Search Results</th></tr>'
		html += '<tr><td><b>#</b></td><td><b>key/name</b></td><td><b>fields</b></td><td><b>distance</b></td><td><b>shapes</b></td></tr>';
		html += '<tbody>'
		for(var i =0; i < response.searchResults.length; i++){
			var result = response.searchResults[i];			
			
			html += "<tr valign=\"top\">";
			html += "<td>" + result.resultNumber + "</td>";
			html += "<td>key: " + result.key + "<br/>";
			html += "name: " + result.name + "<br/>";
			html += "</td>";
			html += "<td>";
			if(result.fields){
				for (var obj in result.fields){
					var f = result.fields[obj];
					html += obj + ": " + f + "<br/>";					
				}
			}
			html += "</td>";
			html += "<td>distance:" + result.distance + " " + result.distanceUnit + "<br/>";
			html += "source:" + result.sourceName + "</td>";
			html += "<td>";
			if(result.shapePoints){
				if(result.shapePoints instanceof Array){
					for(var j=0; j < result.shapePoints.length; j+=2){
						html += result.shapePoints[j];
						html += ", ";
						html += result.shapePoints[j + 1];
						html += "<br/>";
					}
				}
				else{
					html += result.shapePoints
				}
			}
			html += "</td>";
			html += "</tr>";
		}		
		html += '</tbody></table>';
	}
	
	
    
    switch (searchType) {
		case "helloworld":
			var divBasicMap = document.getElementById("divBasicMap");
			if(divBasicMap){
				if(!window.mapBasic){
					window.mapBasic = new MQA.TileMap(divBasicMap);
				}
				processResults(response, window.mapBasic);
				divBasicMap.style.display = "";
			}
			div = document.getElementById("divBasicMap");
			document.getElementById('divBasicSearchResults').style.display = "";
			document.getElementById('divBasicSearchResults').innerHTML = html;
			break;
		case "radius":
			var divRadiusMap = document.getElementById("divRadiusMap");
			if(divRadiusMap){
				if(!window.mapRadius){
					window.mapRadius = new MQA.TileMap(divRadiusMap);
				}
				processResults(response, window.mapRadius);
				divRadiusMap.style.display = "";
			}
			document.getElementById('divRadiusSearchResults').style.display = "";
			document.getElementById('divRadiusSearchResults').innerHTML = html;
			break;
		case "rectangle":
			var divRectangleMap = document.getElementById("divRectangleMap");
			if(divRectangleMap){
				if(!window.mapRectangle){
					window.mapRectangle = new MQA.TileMap(divRectangleMap);
				}
				processResults(response, window.mapRectangle);
				divRectangleMap.style.display = "";
			}
			document.getElementById('divRectangleSearchResults').style.display = "";
			document.getElementById('divRectangleSearchResults').innerHTML = html;
			break;
		case "polygon":
			var divPolygonMap = document.getElementById("divPolygonMap");
			if(divPolygonMap){
				if(!window.mapPolygon){
					window.mapPolygon = new MQA.TileMap(divPolygonMap);
				}
				processResults(response, window.mapPolygon);
				divPolygonMap.style.display = "";
			}
			document.getElementById('divPolygonSearchResults').style.display = "";
			document.getElementById('divPolygonSearchResults').innerHTML = html;
			break;
		case "corridor":
			var divCorridorMap = document.getElementById("divCorridorMap");
			if(divCorridorMap){
				if(!window.mapCorridor){
					window.mapCorridor = new MQA.TileMap(divCorridorMap);
				}
				processResults(response, window.mapCorridor);
				divCorridorMap.style.display = "";
			}
			document.getElementById('divCorridorSearchResults').style.display = "";
			document.getElementById('divCorridorSearchResults').innerHTML = html;
			break;
		case "base":
			var divBaseMap = document.getElementById("divBaseMap");
			if(divBaseMap){
				if(!window.mapBase){
					window.mapBase = new MQA.TileMap(divBaseMap);
				}
				processResults(response, window.mapBase);
				divBaseMap.style.display = "";
			}
			document.getElementById('divBaseSearchResults').style.display = "";
			document.getElementById('divBaseSearchResults').innerHTML = html;
			break;
		case "drivetime":
			var divDriveTimeMap = document.getElementById("divDriveTimeMap");
			if(divDriveTimeMap){
				if(!window.mapDriveTime){
					window.mapDriveTime = new MQA.TileMap(divDriveTimeMap);
				}
				processResults(response, window.mapDriveTime);
				divDriveTimeMap.style.display = "";
			}
			document.getElementById('divDriveTimeSearchResults').style.display = "";
			document.getElementById('divDriveTimeSearchResults').innerHTML = html;
			break;
		case "hosteddata":
			var divHostedDataMap = document.getElementById("divHostedDataMap");
			if(divHostedDataMap){
				if(!window.mapHostedData){
					window.mapHostedData = new MQA.TileMap(divHostedDataMap);
				}
				processResults(response, window.mapHostedData);
				divHostedDataMap.style.display = "";
			}
			document.getElementById('divHostedDataSearchResults').style.display = "";
			document.getElementById('divHostedDataSearchResults').innerHTML = html;
			break;
		case "recordinfo":
			/*
			var divRecordInfoMap = document.getElementById("divRecordInfoMap");
			if(divRecordInfoMap){
				if(!window.mapRecordInfo){
					window.mapRecordInfo = new MQA.TileMap(divRecordInfoMap);
				}
				processResults(response, window.mapRecordInfo);
				divRecordInfoMap.style.display = "";
			}
			*/
			document.getElementById('divRecordInfoSearchResults').style.display = "";
			document.getElementById('divRecordInfoSearchResults').innerHTML = html;
			break;
		case "mapdata":
			var divMapDataMap = document.getElementById("divMapDataMap");
			if(divMapDataMap){
				if(!window.mapMapData){
					window.mapMapData = new MQA.TileMap(divMapDataMap);
				}
				processResults(response, window.mapMapData);
				divMapDataMap.style.display = "";
			}
			document.getElementById('divMapDataSearchResults').style.display = "";
			document.getElementById('divMapDataSearchResults').innerHTML = html;
			break;
		case "remotedata":
			var divRemoteDataMap = document.getElementById("divRemoteDataMap");
			if(divRemoteDataMap){
				if(!window.mapRemoteData){
					window.mapRemoteData = new MQA.TileMap(divRemoteDataMap);
				}
				processResults(response, window.mapRemoteData);
				divRemoteDataMap.style.display = "";
			}
			document.getElementById('divRemoteDataSearchResults').style.display = "";
			document.getElementById('divRemoteDataSearchResults').innerHTML = html;
			break;
		case "alldata":
			var divAllDataMap = document.getElementById("divAllDataMap");
			if(divAllDataMap){
				if(!window.mapAllData){
					window.mapAllData = new MQA.TileMap(divAllDataMap);
				}
				processResults(response, window.mapAllData);
				divAllDataMap.style.display = "";
			}
			document.getElementById('divAllDataSearchResults').style.display = "";
			document.getElementById('divAllDataSearchResults').innerHTML = html;
			break;
	}
}

function collapseResults(type) {
	switch(type) {
		case "helloworld":
			var divBasicMap = document.getElementById("divBasicMap");
			if(divBasicMap){
				divBasicMap.style.display = "none";
			}
			document.getElementById('divBasicSearchResults').style.display = "none";
			break;
		case "radius":
			var divRadiusMap = document.getElementById("divRadiusMap");
			if(divRadiusMap){
				divRadiusMap.style.display = "none";
			}
			document.getElementById('divRadiusSearchResults').style.display = "none";
			break;
		case "rectangle":
			var divRectangleMap = document.getElementById("divRectangleMap");
			if(divRectangleMap){
				divRectangleMap.style.display = "none";
			}
			document.getElementById('divRectangleSearchResults').style.display = "none";
			break;
		case "polygon":
			var divPolygonMap = document.getElementById("divPolygonMap");
			if(divPolygonMap){
				divPolygonMap.style.display = "none";
			}
			document.getElementById('divPolygonSearchResults').style.display = "none";
			break;
		case "corridor":
			var divCorridorMap = document.getElementById("divCorridorMap");
			if(divCorridorMap){
				divCorridorMap.style.display = "none";
			}
			document.getElementById('divCorridorSearchResults').style.display = "none";
			break;
		case "base":
			var divBaseMap = document.getElementById("divBaseMap");
			if(divBaseMap){
				divBaseMap.style.display = "none";
			}
			document.getElementById('divBaseSearchResults').style.display = "none";
			break;
		case "drivetime":
			var divDriveTimeMap = document.getElementById("divDriveTimeMap");
			if(divDriveTimeMap){
				divDriveTimeMap.style.display = "none";
			}
			document.getElementById('divDriveTimeSearchResults').style.display = "none";
			break;
		case "hosteddata":
			var divHostedDataMap = document.getElementById("divHostedDataMap");
			if(divHostedDataMap){
				divHostedDataMap.style.display = "none";
			}
			document.getElementById('divHostedDataSearchResults').style.display = "none";
			break;
		case "recordinfo":
			var divRecordInfoMap = document.getElementById("divRecordInfoMap");
			if(divRecordInfoMap){
				divRecordInfoMap.style.display = "none";
			}
			document.getElementById('divRecordInfoSearchResults').style.display = "none";
			break;
		case "mapdata":
			var divMapDataMap = document.getElementById("divMapDataMap");
			if(divMapDataMap){
				divMapDataMap.style.display = "none";
			}
			document.getElementById('divMapDataSearchResults').style.display = "none";
			break;
		case "remotedata":
			var divRemoteDataMap = document.getElementById("divRemoteDataMap");
			if(divRemoteDataMap){
				divRemoteDataMap.style.display = "none";
			}
			document.getElementById('divRemoteDataSearchResults').style.display = "none";
			break;
		case "alldata":
			var divAllDataMap = document.getElementById("divAllDataMap");
			if(divAllDataMap){
				divAllDataMap.style.display = "none";
			}
			document.getElementById('divAllDataSearchResults').style.display = "none";
			break;
	}
}

