var GGD={	STORE_NAME: 'domino-gears-store',	db: null,	gearsReady:false,	localServer: null}GGD.init = function() {	if (!window.google || !google.gears) {		$('status').innerHTML="<a href=\"http://gears.google.com/?action=install\">Get Gears!</a>";		if ( confirm("You need Google Gears to use this demo. Would you like to install it?") ){			location.href="http://gears.google.com/?action=install&message=Domino Loves Gears" +                    "&return="+location.href;		}		return;	}    	GGD.gearsReady = true;	try {		GGD.db =  google.gears.factory.create('beta.database');		GGD.localServer = google.gears.factory.create('beta.localserver');		GGD.db.open('domino');		GGD.db.execute('create table if not exists Test' +     	      ' (docid varchar(32), modified timestamp, aField text, updated timestamp)');			if (!GGD.online){ //Build a "view" from our local database			var rs = GGD.db.execute('select * from Test order by docid desc');			var tmp="<table>";						while (rs.isValidRow()) {				tmp+="<tr id=\"row_"+rs.fieldByName('docid')+"\"><td><input id=\""+rs.fieldByName('docid')+"[aField]\" value=\""+rs.fieldByName('aField')+"\"><td><input type=\"button\" onclick=\"saveRow('"+rs.fieldByName('docid')+"')\" value=\"Update\"></tr>";				rs.next();			}				rs.close();						$("result").innerHTML=tmp+"</table>";		}		//alert('Done setting up!');	} catch (ex) {		alert(ex.message);		return;	}}function saveRow(id){	try {		if (GGD.online){ //Ajax it to the Domino server!			var request = google.gears.factory.create('beta.httprequest');			request.open('POST', GGD.path+( (id.length==32)?"0/"+id+"?Save":"Test?Create" )+'Document&ajax=true&_anticache='+getTimeStamp());			request.onreadystatechange = function() {	  			if (request.readyState == 4) {					alert(((id.length==32)?"Updated":"Saved")+" to server!");   				}	     	}			request.send("Remote_Addr="+GGD.remote_addr+"&aField="+$(id+"[aField]").value);			  			} else { //Save it locally to the Gears database!			if (id.length<32){				var rs = GGD.db.execute('INSERT INTO Test VALUES (?, ?, ?, ?)', [null, 0, $(id+"[aField]").value, getTimeStamp()]);			} else {				var rs = GGD.db.execute("UPDATE Test SET aField=?,updated=? WHERE docid=?;", [$(id+"[aField]").value, getTimeStamp() ,id]);			}			alert((GGD.db.rowsAffected>0)?"Updated locally!":"Update failed!");		}	} catch (ex) {		alert("Error saving row: " + ex.message);	}}function goOffline(){//alert(GGD.gearsReady);if (!GGD.gearsReady){	if ( confirm("You need Google Gears to go offline. Would you like to install it?") ){		location.href="http://gears.google.com/?action=install&message=Domino Loves Gears" +                    "&return="+location.href;	}	return;}try {	// If the store already exists, it will be opened	GGD.localServer = google.gears.factory.create('beta.localserver');	var store = GGD.localServer.createManagedStore(GGD.STORE_NAME);  	store.manifestUrl = GGD.path+'gears_manifest.json';	store.checkForUpdate();	//Flush the table first	GGD.db.execute('DELETE FROM Test;');		//Get a fresh list of all documents in the database and store them locally	var request = google.gears.factory.create('beta.httprequest');	request.open('GET', GGD.path+'gears_download?OpenAgent&remote_addr='+GGD.remote_addr+'&_anticache='+new Date().getTime());		$("result").innerHTML="<p>Downloading data to local DB...</p>";		request.onreadystatechange = function() {  		if (request.readyState == 4) {	     	var view = eval("("+request.responseText+")");	     	var tmp="<table>";			for (var i=0; i< view.documents.length; i++){				if ( view.documents[i].docid){					tmp+="<tr id=\""+view.documents[i].docid+"\"><td><input id=\""+view.documents[i].docid+"[aField]\" value=\""+view.documents[i].aField+"\"><td><input type=\"button\" onclick=\"saveRow('"+view.documents[i].docid+"')\" value=\"Update\"></tr>";					GGD.db.execute('INSERT INTO Test VALUES (?, ?, ?, ?)', [view.documents[i].docid, view.documents[i].modified, view.documents[i].aField, null]);				}			}			$("result").innerHTML = tmp+"</table>";		}	}	request.send();	GGD.online=false;	$("status").className = "offline";	$("status").innerHTML = "You are now offline! <input type=\"button\" value=\"Go Back Online\" onclick=\"goOnline()\">";} catch (ex) {	alert('Error going offline:\n\n' + ex.message);	return;}}function goOnline(){try{	var rs = GGD.db.execute('select * from Test where updated>modified');	var postData="";		while (rs.isValidRow()) {		postData+="aField["+rs.fieldByName('docid')+"]="+rs.fieldByName('aField')+"&";				rs.next();	}	rs.close();		if (postData!=""){		//Send the data to the server!		var request = google.gears.factory.create('beta.httprequest');		request.open('POST', GGD.path+( 'gears_update_rows?OpenAgent&_anticache='+getTimeStamp() ));		request.onreadystatechange = function() {  			if (request.readyState == 4) {  				if (request.status == 200){	  				var msg = eval("("+request.responseText+")");	   								if ( msg.success ) {										alert('Documents synced back to server!\n\n'+msg.message); //request.responseText											toggleStatus('online');										} else {						alert('Sync failed!!\n\n'+msg.message); //request.responseText					}   				} else {					alert('Re-syncing documents failed');				}			}		};				request.send(postData);			} else {		//Nothing to sync back!		toggleStatus('online');	}} catch(ex){	alert(ex.message);}}function toggleStatus(status){	if(status=="online"){		// We call openStore() to test for it's existence prior to removing it		if (GGD.localServer.openManagedStore(GGD.STORE_NAME)) {			GGD.localServer.removeManagedStore(GGD.STORE_NAME);		}							   		GGD.online=true;		$("status").className = "online";		$("status").innerHTML = "You are back online! <input type=\"button\" value=\"Go Offline\" onclick=\"goOffline()\">";	}}function getTimeStamp(){	var d = new Date();	return (d.getTime()-d.getMilliseconds())/1000;}function $(id){	return document.getElementById(id);}Array.prototype.remove=function(s){	var i = this.indexOf(s);	if( i != -1) this.splice(i, 1);}
