// Copyright © 2002 Microsoft Corporation.  All rights reserved.
var respersistobj         = null;
var respersistid          = null;
var bsavingresults        = false;
var rxmlobj 	          = null;
var rdocument      	  = null;
var userdatasearchresults = "mspclientsearchlastresults";

function setresultsobj( obj ) {

	if ( obj ) {
		if ( respersistid != null )
			respersistobj.removeBehavior(respersistid);

		respersistobj = obj;

		respersistid = respersistobj.addBehavior("#default#userData");
		return respersistobj;
	}
	return null;
}

function getresultsobj() {
	return respersistobj;
}

/*............................................................................................................................................
	loadresultsdata()

	loads saved data - needs work ( verify markup )
............................................................................................................................................*/
function loadresultsdata(){						dout("loadresultsdata");


	var v = null;
	if (( respersistobj ) && ( respersistid )) {
		respersistobj.load(userdatasearchresults);
		v = respersistobj.getAttribute("sPersistAttr");
		if ( v != null ) {
			respersistobj.innerHTML = v;
		}
	}
}

/*............................................................................................................................................
	saveresultsdata( )
	saves data - needs work ( verify amount of data to be saved )
............................................................................................................................................*/

function saveresultsdata(){						dout("saveresultsdata");

	if (( respersistobj ) && ( respersistid )) {

		var dhtmltext = new String( respersistobj.innerHTML );

		respersistobj.setAttribute("sPersistAttr", dhtmltext );

		var utf16_bits     = dhtmltext.length * 16;
		var kbsize          = parseInt( utf16_bits / 8192 );

		try {
			if ( kbsize <= kb_max_cachable_results )
				respersistobj.save(userdatasearchresults);

		} catch( e ) {
			//alert( "search results persist failed" );
			;
		}
	}
	bsavingresults = false;
}

function flushsavedresults(){							dout( "flushsavedresults" );
	var hdiv = window.document.createElement("div");
	if ( hdiv ) {
		hdiv.style.display = "none";
		hdiv = window.document.body.appendChild(hdiv);
		if ( hdiv ) {
			var idpersist = hdiv.addBehavior("#default#userData");
			if ( idpersist ) {
				hdiv.setAttribute("sPersistAttr", " " );
				try {			
					hdiv.save(userdatasearchresults);
				} catch ( e ) {
					alert("un-handled error ( client search - flushsavedresults )");
					;
				}
				hdiv.removeBehavior(idpersist);
				window.document.body.removeChild( hdiv );
			}
		}
	}
}
