//
// Javascript library containing the "svUnits" unit conversion widget
// v1.0 - 8/28/09
//
var debug=0;
var SVUNITS_SID = "2_1";
// these variables can be set by user before calling this library
// but if not, here are the defaults:
if (typeof(svUnits_main_bg) == 'undefined') svUnits_main_bg="white";
if (typeof(svUnits_main_text) == 'undefined') svUnits_main_text="navy";
if (typeof(svUnits_main_border) == 'undefined') svUnits_main_border="1px solid blue";
if (typeof(svUnits_top_bg) == 'undefined') svUnits_top_bg="navy";
if (typeof(svUnits_top_text) == 'undefined') svUnits_top_text="white";
if (typeof(svUnits_top_border) == 'undefined') svUnits_top_border="0px";
if (typeof(svUnits_result_bg) == 'undefined') svUnits_result_bg="navy";
if (typeof(svUnits_result_text) == 'undefined') svUnits_result_text="white";
if (typeof(svUnits_result_border) == 'undefined') svUnits_result_border="0px";
if (typeof(svUnits_unit_bg) == 'undefined') svUnits_unit_bg="#eeeeee";
if (typeof(svUnits_unit_text) == 'undefined') svUnits_unit_text="black";
if (typeof(svUnits_unit_border) == 'undefined') svUnits_unit_border="1px solid gray";
//////////////
//---------- HTML/DOC METHODS
//////////////
// quick wrapper for getElementById:
function _obj(id)
{
return document.getElementById(id);
}
// write html to the DOM object given by id
// if dbg=1, add a debug button (if global debug=1)
// if op=+, add to current value
function htmlPut(id, html, op, dbg)
{
var obj = _obj(id);
if (dbg==null) dbg=0;
if (op==null) op="";
if (obj!=null){
if (debug && dbg==1){
// automatically insert a debug button
html += "";
}
if (op=="+"){
obj.innerHTML += html;
} else {
obj.innerHTML = html;
}
}
}
// write debug text to the debug textarea
function debugPut(txt)
{
debugtxt += " "+txt;
if (debug) htmlPut("debug", debugtxt);
}
// called from debug buttons to dump the innerHTML of page sections to debug area
// id = name of an Document object (span) to dump
function debugDump(id)
{
var obj = _obj(id);
if (obj==null) return;
var txt = obj.innerHTML;
debugtxt = "";
htmlPut("debug", debugtxt);
}
// dummy for now
function showStatus(msg)
{
}
//////////////
//---------- XML METHODS
//////////////
//
// this is called when an XML block is done loading
//
function xmlReturn (){
if (req.readyState == 4) { // if req shows "complete"
if (req.status == 200) { // if status="OK"
// ...processing statements go here...
showStatus("Ready"); // update status bar
//
// all responses should have a tag naming a processing function
var target_obj = "scvunitz_"+_xdoctag(req.responseXML.documentElement,"target");
var contents = _xdoctag(req.responseXML.documentElement,"contents");
htmlPut(target_obj, contents);
} else {
alert("There was a problem retrieving the XML data:\n" + req.status + "\n" + req.statusText);
showStatus(req.statusText); // update status bar
}
}
}
// quick wrapper for XML parsing getElementsByTagName:
// doc is an XMLResponse documentElement
function _xdoctag(doc,id)
{
var child=doc.getElementsByTagName(id)[0];
if (child==null){ alert(id+" not found."); return ""; }
child = child.firstChild;
return (child==null) ? "" : child.data;
}
//
// this is called to initiate an XML request
// xtype = type of data to get: Script|Node (must be capitalized, since it's used in an URL)
// parameters are in name=value&name=value format
//
function getXML(xtype,params)
{
var URL = "http://scvs.com/dev/units/get_"+xtype+".php?"+params;
URL += "&host="+window.location.host+"&sid="+SVUNITS_SID;
// alert(URL);
var waitmsg = "Waiting for XML.";
debugtxt = ""; // clear debug buffer
debugPut(URL); // update status bar
if (window.XMLHttpRequest) { // branch for native XMLHttpRequest object
req = new XMLHttpRequest();
} else if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
debugPut("No request object."); // update status bar
return;
}
if (req) {
req.onreadystatechange = xmlReturn;
showStatus(waitmsg); // update status bar
req.open("GET", URL, true);
req.send(null);
}
}
// a category has been selected, call to get the units section:
function getUnits(){
var cat = document.scvunitz_cat_form.category.value;
getXML("units","fmt=xhtml&cat="+cat);
}
// a category has been selected, call to get the units section:
function getFinal(){
var _amt = document.scvunitz_core_form.amount.value;
var _to = document.scvunitz_core_form.to_unit.value;
var _from = document.scvunitz_core_form.from_unit.value;
var cat = document.scvunitz_core_form.category.value;
getXML("convert","fmt=xhtml&cat="+cat+"&amt="+_amt+"&to="+_to+"&from="+_from);
}
var scvUnits_cat_list = "Category: "
+"";
// write all our style stuff here:
document.write("");
var svUnits_about = "About svUnits\n";
var svUnits_head = "
svUnits ... a unit conversion widget
\n";
var svUnits_catform = "\n";
var svUnits_core = "\n";
// write all our startup HTML here:
var svUnits_full = "
\n"
+ svUnits_head
+ "
"
+ "
\n"
+ svUnits_catform
+ "\n"
+ svUnits_core
+ "
\n
\n"
+ "
\n"
+ "\n"
+ ""+svUnits_about
+ "
\n";
document.write("
\n"
+ "
\n"
+ svUnits_full
+ "
\n
\n");
// begin by retrieving the category list:
//getXML("categories","fmt=xhtml");