﻿// JavaScript for mineral database demo, client side / no server page
// Author: Bob Wardell, Jan 2010, rwardell.com
// persistent global variables:
var minPresent = 0; /* http request has been completed */
var rawMin = '';  /* json string loaded here */
var minDB = {};	/* json object containing the entire string in object notation */
var minArray = {}; /* json object containing only the minerals in object notation */
var minCnt = 0;	/* number of minerals in the database */ 
var targetMin = {}; /* this mineral sliced from the json object */

// ----> Initialization: Load up the mineral database -----------------------------------------------|
function initMinDB () {
	// retrieve mineral dataset from server
	getJsonFile('../../rtw/xml/mindemo_json.txt'); //stuffs it into the rawMin variable if successful
} // suspend pending asynchronous response
function continuePageInit () {	// continue with result received from XMLHttpRequest
	minDB = eval('(' + rawMin + ')');  // transform to object; disambiguate with extra parens
	minArray = minDB.mineral_database.mineral; //only the minerals
	minCnt = minArray.length; //number of minerals
	minPresent = 1; //the mineral database is now in minDB
}
// invoked when a mineral is selected from the list
function showMin (selector) {
var dropDown = document.getElementById(selector);
var minName = dropDown.options[dropDown.selectedIndex].text.toUpperCase();

	for (i=0;i<minCnt;i++) { //search the array for matching mineral
		if (minArray[i].name == minName) {
			targetMin = minArray[i];
			break;
			}
		};
	if (i == minCnt) {	/* invalid mineral name or valid and not found in the demo file*/
		notifyReload(4); 
		return;
		};
	document.getElementById('jName').innerHTML = targetMin.name;
	document.getElementById('jFormula').innerHTML = targetMin.formula;
	chemFormat ('jFormula', 'jFormula'); //reformat flat string to canonical form
	document.getElementById('jXlSys').innerHTML = targetMin.crystal_system;
	document.getElementById('jHardness').innerHTML = targetMin.hardness;
	document.getElementById('jSpecificGravity').innerHTML = targetMin.specific_gravity;
	document.getElementById('jOptical').innerHTML = targetMin.optical_properties;
} // mineral data now visible

// ----> generic error processor
function notifyReload(errType) {
	alert('Unable to run the demo.(error: ' + errType + ')');
}
// ----> XMLHttpRequest code ----------------------------------------------------|
// 		 retrieves a JSON file emitted from MS Excel which contains mineral data
var minXmlHttp; //global
// send the GET request
function getJsonFile(url) {
	minXmlHttp=initXmlHttpObject();
	if (minXmlHttp == null)
	  {
	  alert ('We cannot provide this demo because your browser does not support the required features.');
	  return;
	  }
	minXmlHttp.onreadystatechange=checkDone;	/* state change to 4 resumes mainline */
	minXmlHttp.open('GET',url,true);
	minXmlHttp.setRequestHeader('Content-Type','text/json');
	minXmlHttp.send(null);
}
// initialize the xmlhttp object
function initXmlHttpObject() {
	if (window.XMLHttpRequest) {
	  return new XMLHttpRequest();  // code for IE7+, Firefox, Chrome, Opera, Safari
	  }
	if (window.ActiveXObject) {
	  return new ActiveXObject("Microsoft.XMLHTTP");  // code for IE6, IE5
	  }
	return null;
}
/*	readyState values are:
	0      Uninitated		3      Interactive
	1      Loading			4      Complete
	2      Loaded			*/
// received the requested file? 	
function checkDone() {
	if (minXmlHttp.readyState == 4) {	// complete
		if (minXmlHttp.status == 200) {	// successful
			rawMin = minXmlHttp.responseText;
			continuePageInit();  // pick up with json string in buffer 
			}
		else {
			notifyReload(minXmlHttp.status); /* probable 404, json file not found */
			}
	}
}

