//********************************************************************
//* JavaScript support for Tiger Heron Bookstore
//********************************************************************

var browser = new Object();		// browser type
var isLowRes = false;			// Determine screen resolution

// Get IE version

function getIEVersion()
{
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    if (MSIEOffset == -1) return 0;
    return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
}

// Initialize our Javascript settings

function initBookstore()
{
    // Browser major version

    browser.version = parseFloat(navigator.appVersion);

    // Browser type

    browser.isIE = false;
    browser.isOpera = false;
    browser.isGecko = navigator.userAgent.indexOf("Gecko") != -1;

    if (navigator.userAgent.indexOf("Opera") != -1) {
	browser.isOpera = true;
    }
    else if (navigator.appName.indexOf("Microsoft") != -1) {
	browser.isIE = true;
    }

    if (browser.isIE) browser.version = getIEVersion();

    // Support for CSS2?

    browser.isCSS2 = false;
    if (document.implementation &&
	document.implementation.hasFeature) {
	browser.isCSS2 = document.implementation.hasFeature("CSS2", "2.0");
    }
    if (!browser.isCSS2 &&
	(browser.isGecko ||
	 browser.isIE && browser.version >= 5.5 ||
	 browser.isOpera && browser.version >= 6.0)) {
	browser.isCSS2 = true;
    }

    // Find out where we are

    var path = window.location.pathname;
    path = path.replace(/^\/sff\//, "/");
    path = path.replace(/\/[^\/]*$/, "/");
    path = path.replace(/[^\/]+\//g, "../");
    path = path.replace(/^\//, "");

    // Set up the font to use based on the screen resolution

    if (screen.width) {
	isLowRes = screen.width <= 800;

	if (!isLowRes) {
	    document.write(
		"<link href=\"" + path +
		"support/largefont.css\" rel=\"stylesheet\" type=\"text/css\" />\n");
	}
	else{
	    document.write(
		"<link href=\"" + path +
		"support/mediumfont.css\" rel=\"stylesheet\" type=\"text/css\" />\n");
	}
    }
    else {
	document.write(
	    "<link href=\"" + path +
	    "support/largefont.css\" rel=\"stylesheet\" type=\"text/css\" />\n");
    }
}

// Add a field to a form

function addField(form, fieldType, fieldName, fieldValue)
{
    var input;
    if (document.all) {
	// Check to see if the element exists

	for (i = 0; i < form.elements.length; i++) {
	    if (form.elements[i].name == fieldName) {
		if (form.elements[i].type != fieldType) {
		    form.elements[i] = document.createElement('INPUT');
		    form.elements[i].type = fieldType;
		}
		form.elements[i].name = fieldName;
		form.elements[i].value = fieldValue;
		return;
	    }
	}

	// If it doesn't exist, create it and add it to the form

	input = document.createElement('INPUT');
	input.type = fieldType;
	input.name = fieldName;
	input.value = fieldValue;
    }
    else if (document.getElementById) {	// NN6 workaround
	// Check to see if the element exists

	for (i = 0; i < form.elements.length; i++) {
	    if (form.elements[i].name == fieldName) {
		if (form.elements[i].type != fieldType) {
		    form.elements[i] = document.createElement('INPUT');
		    form.elements[i].setAttribute('type', fieldType);
		}
		form.elements[i].setAttribute('name', fieldName);
		form.elements[i].setAttribute('value', fieldValue);
		return;
	    }
	}

	// If it doesn't exist, create it and add it to the form

	input = document.createElement('INPUT');
	input.setAttribute('type', fieldType);
	input.setAttribute('name', fieldName);
	input.setAttribute('value', fieldValue);
    }
    form.appendChild(input);
}

// Modify a search form to search only for Science Fiction and Fantasy

function searchSFF()
{
    // Set up the right BrowseNode to search

    if (document.sffsearch.SearchIndex.options[0].selected) {
	addField(document.sffsearch, "hidden", "BrowseNode", "25");
    }
    else if (document.sffsearch.SearchIndex.options[1].selected) {
	addField(document.sffsearch, "hidden", "BrowseNode", "17466");
    }
    else if (document.sffsearch.SearchIndex.options[2].selected) {
	addField(document.sffsearch, "hidden", "BrowseNode", "163431");
    }
    else if (document.sffsearch.SearchIndex.options[3].selected) {
	addField(document.sffsearch, "hidden", "BrowseNode", "144");
    }

    // If there are no keywords, direct them to the right storefront

    if (document.sffsearch.Keywords.value == "") {

	// If this is not one of the SF&F stores, then don't do anything
	if (document.sffsearch.SearchIndex.options[4].selected ||
	    document.sffsearch.SearchIndex.options[5].selected ||
	    document.sffsearch.SearchIndex.options[6].selected ||
	    document.sffsearch.SearchIndex.options[7].selected ||
	    document.sffsearch.SearchIndex.options[8].selected) {

	    return false;
        }

	// Otherwise, go the the storefront

	addField(document.sffsearch, "hidden", "templates", "th1");
	addField(document.sffsearch, "hidden", "link_templates", "th");
	addField(document.sffsearch, "hidden", "Sort", "salesrank");
	if (document.sffsearch.SearchIndex.value == "Books") {
	    addField(document.sffsearch, "hidden", "Power", "pubdate: after 2004");
	}
    }

    return true;
}

function insureThWindow()
{
    var amazonWindow =
	window.open(
	    "about:blank",
	    "th_ext");
    amazonWindow.focus();
    return true;
}
