//__________________________________________________________________________
// Declare global variables
var datafile = "/data/pp_catalog.xml"; // xml data file
var table = 0; // table of xml data
var col_list = new Array(); // array of column data types

//__________________________________________________________________________
// Initialize global variables
function globalInit(){
    table = get_table(datafile);
    col_list = get_col_list();
    return 0;
}

//__________________________________________________________________________
// Make xmlHttpRequest object
function loadxml(xmlfile){
    var xmldoc;
    if(window.XMLHttpRequest){
	xmldoc = new window.XMLHttpRequest();
    }
    else if(window.ActiveXObject){
	xmldoc = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else
	xmldoc = false;
    if (xmldoc){
	xmldoc.open("GET",xmlfile,false);
	xmldoc.send(null);
	return xmldoc.responseXML;    
    }
    else{
	alert("XML loading not supported.");
	return null;
    }   
}
http://www.google.com/
//__________________________________________________________________________
// Get global table element
function get_table(xmlfile){
    var xmldocument = loadxml(xmlfile);
    var a_table = xmldocument.getElementsByTagName("Table")[0];
    return a_table;
}

//__________________________________________________________________________
// Get global array of column data types
function get_col_list(){
    var row0 = table.getElementsByTagName("Row")[0];
    var cells = new Array();
    cells = row0.getElementsByTagName("Cell");
    var a_col_list = new Array(cells.length);
    for (var i=0;i<cells.length;i++){
	if (cells[i].getElementsByTagName("Data").length>0){
	    var data = cells[i].getElementsByTagName("Data")[0].childNodes[0];
	    a_col_list[i] = data.nodeValue;
	}
	else
	    a_col_list[i] = '';
    }
    return a_col_list;
}

//__________________________________________________________________________
// Get column index of a particular data type (e.g. 'title')
function get_col_ind(data_type){
    var ind = -1;
    for (var i=0;i<col_list.length;i++){
	if (col_list[i]==data_type){
	    ind = i;
	    break;
	}
    }
    return ind;
}

//__________________________________________________________________________
// Get full column of a particular data type (e.g. 'title')
function get_full_col(data_type){
    var col = get_col_ind(data_type);
    var rows = new Array();
    rows = table.getElementsByTagName("Row");
    var full_col = new Array(rows.length);
    for (var i=0;i<rows.length;i++){
	var cell = rows[i].getElementsByTagName("Cell")[col];	
	if (cell.getElementsByTagName("Data").length>0){
	    var data = cell.getElementsByTagName("Data")[0].childNodes[0];
	    full_col[i] = data.nodeValue;
	}
	else
	    full_col[i] = '';
    }
    return full_col;
}

//__________________________________________________________________________
// Get row index of a particular title
function find_title(title){
    var row_ind = 0;
    var all_titles = new Array();
    all_titles = get_full_col('title');
    for (var i=0;i<all_titles.length;i++){
	var this_title = simplify_string(all_titles[i]);
	if (this_title == title)
	    row_ind = i;
    }
    return row_ind;
}

//__________________________________________________________________________
// Get a single column's data for a particular row
function get_data(row_ind,data_type){
    var data = '';
    var row = table.getElementsByTagName("Row")[row_ind];
    var cell = row.getElementsByTagName("Cell")[get_col_ind(data_type)];
    if (cell.getElementsByTagName("Data").length>0)
	data = cell.getElementsByTagName("Data")[0].childNodes[0].nodeValue;
    return data;
}

//__________________________________________________________________________
// Fill item values
function fill_item(row_ind){
    var item = {
	sku        : get_data(row_ind,'SKU'),
	title      : get_data(row_ind,'title'),
	author     : get_data(row_ind,'author'),
	style      : get_data(row_ind,'style'),
	status     : get_data(row_ind,'status'),
	short_desc : get_data(row_ind,'short description'),
	price      : get_data(row_ind,'price'),
	weight     : get_data(row_ind,'ounces')/16.0,
	full_desc  : get_data(row_ind,'description'),
	guts       : get_data(row_ind,'spilling its guts'),
	said       : get_data(row_ind,'he said, she said'),
	date       : get_data(row_ind,'release date'),
	publisher  : get_data(row_ind,'Publisher'),
	paper      : get_data(row_ind,'Paper/binding'),
	dimensions : get_data(row_ind,'Dimensions'),
	pages      : get_data(row_ind,'Pages'),
	language   : get_data(row_ind,'Language'),
	isbn       : get_data(row_ind,'ISBN'),
	copyright  : get_data(row_ind,'Copyright Ownership'),
	barcode    : get_data(row_ind,'Barcode'),
	outofprint : get_data(row_ind,'Out Of Print'),
	also_avail : get_data(row_ind,'also available at:'),
	as_seen_in : get_data(row_ind,'as seen in:'),
	sneak_peek : get_data(row_ind,'sneak peek:')
    }
    return item;
}

//__________________________________________________________________________
// Fill in the purchase button information
function fill_button(item){
    var button_title = getElementsByClassName("button_title",'input',document)[0];
    button_title.value = item['title']+" by "+item['author'];
    var button_sku = getElementsByClassName("button_sku",'input',document)[0];
    button_sku.value = item['sku'];
    var button_price = getElementsByClassName("button_price",'input',document)[0];
    button_price.value = parseFloat(item['price']).toFixed(2);
    var button_weight = getElementsByClassName("button_weight",'input',document)[0];
    button_weight.value = parseFloat(item['weight']).toFixed(2);
    return 0;
}

//__________________________________________________________________________
// Print a nicely formatted date
function get_nice_date(ugly_date){
    var month=new Array(12);
    month[0]="January";
    month[1]="February";
    month[2]="March";
    month[3]="April";
    month[4]="May";
    month[5]="June";
    month[6]="July";
    month[7]="August";
    month[8]="September";
    month[9]="October";
    month[10]="November";
    month[11]="December";
    var tmp = ugly_date.split('T');
    var tmp2 = tmp[0].split('-');
    var nice_date = month[tmp2[1]-1]+" "+tmp2[0];
    return nice_date;
}

//__________________________________________________________________________
// Simplify a string (e.g. a title)
function simplify_string(orig_string){
    return orig_string.toLowerCase().replace(/ /g,"_").replace(/\'/g,"").replace("...","").replace(/\,/g,"").replace(/\:/g,"").replace(/\[/g,"").replace(/\]/g,"").replace(/\(/g,"").replace(/\)/g,"").replace("#","issue_").replace("&","and").replace(/\//g,"_").replace(/\./g,"");
}

//__________________________________________________________________________
// Fill in the page
function fill_page(row_ind){
    var item = fill_item(row_ind);
    // title
    document.getElementById('title').innerHTML = item['title'];
    // author
    var auth1 = item['author'].split(';');
    for (var j=0;j<auth1.length;j++){
	if (j>0){
	    if (j==auth1.length-1)
		document.getElementById('author').innerHTML += " and ";
	    else
		document.getElementById('author').innerHTML += ", ";
	}
	var auth2 = auth1[j].split(',');
	for (var k=auth2.length-1;k>=0;k--)
	    document.getElementById('author').innerHTML += auth2[k]+" ";
    }
    // price/out of print
    if (item['outofprint'] != 'Yes' && item['price'] != '')
	document.getElementById('price').innerHTML = "$"+parseFloat(item['price']).toFixed(2);    
    else if (!(item['outofprint'] != 'Yes' && item['price'] != ''))
	document.getElementById('price').innerHTML = "Out Of Print";
    // detailed information
    document.getElementById('full_desc').innerHTML = item['full_desc'];
    document.getElementById('guts').innerHTML    = item['guts'];
    document.getElementById('said').innerHTML    = item['said'];
    document.getElementById('also').innerHTML    = item['also_avail'];
    document.getElementById('seen').innerHTML    = item['as_seen_in'];
    document.getElementById('peek').innerHTML    = item['sneak_peek'];
    document.getElementById('style').innerHTML   = item['style'];
    document.getElementById('sku').innerHTML     = "Item #"+item['sku'];
    document.getElementById('status').innerHTML  = item['status'];
    // date
    document.getElementById('date').innerHTML    = 
	"Release Date: <br />" + get_nice_date(item['date']);
    // technical information
    document.getElementById('publisher').innerHTML  = 
	"Published by <br />" + item['publisher'] + "<br /><br />";
    document.getElementById('paper').innerHTML      = 
	item['paper'] + "<br /><br />";
    document.getElementById('dimensions').innerHTML = 
	"Dim.: " + item['dimensions'] + "<br />";
    document.getElementById('pages').innerHTML      = 
	item['pages'] + " pages" + "<br />";
    document.getElementById('language').innerHTML   = 
	"Lang.: " + item['language'] + "<br /><br />";
    document.getElementById('isbn').innerHTML       = 
	"ISBN: " + item['isbn'] + "<br /><br />";
    document.getElementById('copyright').innerHTML  = 
	"Copyright " + item['copyright'] + "<br />";
    // fill the purchase button information
    fill_button(item);
    // display the picture
    var picture = simplify_string(item['title']) + ".jpg";
    document.getElementById('pic').setAttribute("src","/images/book_meds/med_" + picture);
    return 0;
}

//__________________________________________________________________________
// Display the page based on the url hash info
function initDisplay(pagehash){
    globalInit();
    var title = pagehash.replace("#","");
    var row_ind = find_title(title);
    fill_page(row_ind);
    return 0;
}

//__________________________________________________________________________
// Popup large picture of item
function picpop(pagehash){
    var pic_url = "http://alt-current.com/pp/pp_pic_frame.html" + pagehash;
    window.open(pic_url);
}
