// Copyright 2008 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.

/*
JavaScipt for handling results from AJAX processing.
*/
function sendAjaxRequest(searchTheme, searchMethod, dataString, searchIndex, searchLimit, selectNum, treeNode, username, password, browser) {
    if (logLevel >= 3)
        console.log("function sendAjaxRequest\nsearchTheme: " + searchTheme +"\nsearchMethod: "+ searchMethod +"\ndataString: "+ dataString +"\nsearchIndex: "+ searchIndex +"\nsearchLimit: "+ searchLimit +"\nselectNum: "+ selectNum +"\ntreeNode: "+ treeNode +"\nusername: "+ username +"\npassword: "+ password +"\nbrowser: "+ browser);

    OrkaInit.OrkaSearch(searchTheme, searchMethod, dataString, searchIndex, searchLimit, selectNum, treeNode, username, password, browser, updatePage, onTimeOut, onError);
}

/*
Update form area of page to reflect contents of results.
*/
function updatePage(results) {
    if (logLevel >= 3)
        console.log("updatePage Results:\n" + results);

    var dCount = parseRecord(results, "dspCount");
    dspCount = Number(dCount);
    var tCount = parseRecord(results, "totCount");
    totCount = Number(tCount);

	var src = parseRecord(results, "searchSource");
	
	var pid1 = parseRecord(results, "pid1");
	
	var searchResults = parseRecord(results, "searchResults");
    //alert(searchResults);
    
    //console.log(src.toUpperCase());
    
    switch (src.toUpperCase()) {
    case "ORKA_SEARCH":
        displaySearchResults(searchResults, pid1, dspCount, totCount);
        break;
        
    case "BUILDING_SKETCH_THEME":
    	//alert(searchResults);
    	displaySketchResults(searchResults, pid1, dspCount, totCount);
    	break;

	case "OWNER_NAME_FORM":
	case "ADDRESS_FORM":
	case "LEGAL_FORM":
	case "SALES_FORM":
	case "PERSONAL_PROPERTY_FORM":
	case "PARCEL_ID_FORM":
		var themeResultsStatus = '';
		if (document.getElementById("SalesSearch") != null)
			themeResultsStatus = 'hidden';

		togglePage('', themeResultsStatus, '', '', 'visible', '', '', '', '', 'hidden', searchResults, src);

		break;
	
	default:
    	//alert("displayThemeResults");
        displayThemeResults(searchResults, pid1, src);
        break;
	}
}

function parseRecord(results, searchTag) {
    var finalVal = "";
    var startpos = 0;
	var pos = results.indexOf("<" + searchTag, startpos);
    //alert(searchTag.length);
	if (pos != -1) {
		startpos = pos + (searchTag.length + 2);
		xmlEndPos = results.indexOf("</" + searchTag, startpos);
		finalVal = results.substring(startpos, xmlEndPos);
	}

    return finalVal;

}

function onTimeOut() {
    alert("Time Out!");
}

function onError() {
    alert("Error!");
}

