// JavaScript Document


function getAuthorizedUsers(widgetID, format) {
	dojo.xhrGet( { // 
				
	// The following URL must match that used to test the server.
	url: "http://www.stanford.edu/~dianan/cgi-bin/getAuthUsers.php" , 
	handleAs: "text",

	timeout: 5000, // Time in milliseconds

	// The LOAD function will be called on a successful response.
	load: function(response, ioArgs) { // 
	  dojo.byId(widgetID).innerHTML = response; 
	  return response; // 
	},

	// The ERROR function will be called in an error case.
	error: function(response, ioArgs) { // 
	  if(xhr.status != 200) {
		  console.error("XHR status code: ", ioArgs.xhr.status); // 
	   }
	  return response; // 
	  }
	});

}

function fillOpenHouses(widgetID, format) {
	dojo.xhrGet( { // 
				
	// The following URL must match that used to test the server.
	url: "http://www.stanford.edu/~dianan/cgi-bin/getOpenHouse.php?mode=individual&format="+format , 
	handleAs: "text",

	timeout: 5000, // Time in milliseconds

	// The LOAD function will be called on a successful response.
	load: function(response, ioArgs) { // 
	 	dojo.byId(widgetID).innerHTML = response; 
	    return response; // 
	},

	// The ERROR function will be called in an error case.
	error: function(response, ioArgs) { // 
	  if(xhr.status != 200) {
		  console.error("XHR status code: ", ioArgs.xhr.status); // 
	   }
	  return response; // 
	  }
	});

}



function populate_map(mapID) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(mapID));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        geocoder = new GClientGeocoder();
		dojo.xhrGet( { // 
			// The following URL must match that used to test the server.
			url: "http://www.stanford.edu/~dianan/cgi-bin/sqlGeneric.php?table=property_listing&field=address_full&id=session", 
			handleAs: "text",
		
			timeout: 5000, // Time in milliseconds
		
			// The LOAD function will be called on a successful response.
			load: function(response, ioArgs) { // 
				  showAddress(response+", Stanford, CA");
				  
				  },
			// The ERROR function will be called in an error case.
			error: function(response, ioArgs) { // 
			    if(xhr.status != 200) {
		  			console.error("XHR status code: ", ioArgs.xhr.status); // 
	   			} 
			  return response; // 
			  }
		});
    };//browser compatibility check
} //function initialize_map

function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
			  map.setZoom(15);
			  map.addControl(new GSmallMapControl());
			 
			  marker.openInfoWindowHtml("<strong>Address:</strong><br>"+address+"<br><a href=''>Street View</a>");
            }
          }
        );
      }
    }



function populate_street_view(mapID) {
      if (GBrowserIsCompatible()) {
		  
        
        geocoder = new GClientGeocoder();
		dojo.xhrGet( { // 
			// The following URL must match that used to test the server.
			url: "http://www.stanford.edu/~dianan/cgi-bin/sqlGeneric.php?table=property_listing&field=address_full&id=session", 
			handleAs: "text",
		
			timeout: 5000, // Time in milliseconds
		
			// The LOAD function will be called on a successful response.
			load: function(response, ioArgs) { // 
				  showStreetView(response+", Stanford, CA");
				  
				  },
			// The ERROR function will be called in an error case.
			error: function(response, ioArgs) { // 
			    if(xhr.status != 200) {
					  console.error("XHR status code: ", ioArgs.xhr.status); // 
				}
			  return response; // 
			  }
		});
    };//browser compatibility check
} //function initialize_map

function showStreetView(address) {
      if (geocoder) {
		  
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
          
			 console.log("myPoint is ", point.lat(), "long is ", point.lng());
			 var palo_alto = new GLatLng(point.lat(), point.lng());
		  	 panoramaOptions = { latlng:palo_alto };
             var myStreetView = new GStreetviewPanorama(document.getElementById("map_street_view"), panoramaOptions);
	
            }
          }
        );
      }
    }










function formatCurrency(inDatum){
	return isNaN(inDatum) ? '...' : dojo.currency.format(inDatum, { currency: 'USD' , places:0} );
}

function fillHTML(tableName, fieldName, recordID, widgetID, format) {
	
	dojo.xhrGet( { // 
	// The following URL must match that used to test the server.
	url: "http://www.stanford.edu/~dianan/cgi-bin/sqlGeneric.php?table="+tableName+"&field="+fieldName+"&id="+recordID, 
	handleAs: "text",

	timeout: 5000, // Time in milliseconds

	// The LOAD function will be called on a successful response.
	load: function(response, ioArgs) { // 
	  if (format == "money") {
		dojo.byId(widgetID).innerHTML = formatCurrency(response);
	  } else if (format=="link") {
		  dojo.byId(widgetID).innerHTML = "<a href='http://" + response + "'>" + response + "</a>";
	  } else {
	  	dojo.byId(widgetID).innerHTML = response; 
	  }
	  return response; // 
	},

	// The ERROR function will be called in an error case.
	error: function(response, ioArgs) { // 
	   if(xhr.status != 200) {
		  console.error("XHR status code: ", ioArgs.xhr.status); // 
	   }
	  return response; // 
	  }
	});
	
}

function fillHTMLSpecific(valueName, recordID, widgetID, format) {
	dojo.xhrGet( { // 
	// The following URL must match that used to test the server.
	url: "http://www.stanford.edu/~dianan/cgi-bin/sqlSpecific.php?value="+valueName+"&id="+recordID, 
	handleAs: "text",

	timeout: 5000, // Time in milliseconds

	// The LOAD function will be called on a successful response.
	load: function(response, ioArgs) { // 
	  dojo.byId(widgetID).innerHTML = response; 
	  return response; // 
	},

	// The ERROR function will be called in an error case.
	error: function(response, ioArgs) { // 
	  if(xhr.status != 200) {
		  console.error("XHR status code: ", ioArgs.xhr.status); // 
	   }
	  return response; // 
	  }
	});

}

function fillWidget(tableName, fieldName, recordID, widgetID, format) {
	dojo.xhrGet( { // 
	// The following URL must match that used to test the server.
	url: "http://www.stanford.edu/~dianan/cgi-bin/sqlGeneric.php?table="+tableName+"&field="+fieldName+"&id="+recordID, 
	handleAs: "text",

	timeout: 5000, // Time in milliseconds

	// The LOAD function will be called on a successful response.
	load: function(response, ioArgs) { // 
	    if (format == "money") {
		   response = formatCurrency(response);
		};
		dijit.byId(widgetID).setValue(response); //  
	    return response; // 
	},

	// The ERROR function will be called in an error case.
	error: function(response, ioArgs) { // 
	  if(xhr.status != 200) {
		  console.error("XHR status code: ", ioArgs.xhr.status); // 
	   }
	  return response; // 
	  }
	});

}

function fillDateWidget(tableName, fieldName, recordID, widgetID) {
	dojo.xhrGet( { // 
	// The following URL must match that used to test the server.
	url: "http://www.stanford.edu/~dianan/cgi-bin/sqlGeneric.php?table="+tableName+"&field="+fieldName+"&id="+recordID,
	handleAs: "text",

	timeout: 5000, // Time in milliseconds

	// The LOAD function will be called on a successful response.
	load: function(response, ioArgs) { // 
	dijit.byId(widgetID).setDisplayedValue(response.substring(5,7)+ "/"+response.substring(8,10)+ "/"+ response.substring(0,4)); //  
	return response; // 
	},

	// The ERROR function will be called in an error case.
	error: function(response, ioArgs) { // 
	  if(xhr.status != 200) {
		  console.error("XHR status code: ", ioArgs.xhr.status); // 
	   }
	  return response; // 
	  }
	});

}	