/* Applies to the whole document - Firefox/Mozilla knows nothing about CTRL key outside of inputs and object events, all that is, except the document... */
var isCtrlKey = false; 
var isEntKey = false;
if (!document.all) {
	document.onkeyup = function(e) { if(e.which == 17) isCtrlKey = false; if(e.which == 13) isEntKey = false; } 
	document.onkeydown = function(e) { if(e.which == 17) { isCtrlKey = true; } if(e.which == 13) { 	isEntKey = true; } }
}
/* */

function showJumper() {

	var trigger = document.getElementById("trigger");
	var jumper = document.getElementById("jumper");
	var items = null;

	if (document.getElementById("content") != null) {

		items =	document.getElementById("content").getElementsByTagName("select");
	
		for(var i = 0; i < items.length; i++) {
			items[i].style.zIndex = 1;
		}
		
		if (trigger) {
			
			trigger.onclick = function() {

				if(jumper.style.visibility == 'visible') {

					jumper.style.visibility = "hidden";
					jumper.style.display = "none";

				} else {

					jumper.style.visibility = "visible";
					jumper.style.display = "block";

				}
			}
		}
	}
}

function toggleJumper()
{
	var jumper = document.getElementById("jumper");
	var items = null;

	if (document.getElementById("content") != null) {

		items = document.getElementById("content").getElementsByTagName("select");
		
		for(var i = 0; i < items.length; i++)
		{
			items[i].style.zIndex = 1;
		}
		
		if(jumper.style.visibility == 'visible')
		{
			jumper.style.visibility = "hidden";
			jumper.style.display = "none";
		}
		else
		{
			jumper.style.visibility = "visible";
			jumper.style.display = "block";
		}
	}

}


function showHideFilter() {
	var thetext = document.getElementById("filterlinktext");
    var filtercontent = document.getElementById("filtercontent");
	
	if(filtercontent.style.visibility == 'visible') {
		filtercontent.style.visibility = "hidden";
		filtercontent.style.display = "none";
		filterVis = 0;
        thetext.innerHTML = "Show filters";
	}
	else {
		filtercontent.style.visibility = "visible";
		filtercontent.style.display = "block";
		filterVis = 1;
        thetext.innerHTML = "Hide filters";
	}
}

// below not used, if I can avoid it...
function showFilter()
{
	var trigger = document.getElementById("filterlink");
    var filtercontent = document.getElementById("filtercontent");
	
	if (trigger) {
		trigger.onclick = function()
		{
			if(filtercontent.style.visibility == 'visible')
			{
				filtercontent.style.visibility = "hidden";
				filtercontent.style.display = "none";
				filterVis = 0;
			}
			else
			{
				filtercontent.style.visibility = "visible";
				filtercontent.style.display = "block";
				filterVis = 1;
			}
		}
	}
}

function getItem(id)
{
    var itm = false;
    if(document.getElementById)
        itm = document.getElementById(id);
    else if(document.all)
        itm = document.all[id];
    else if(document.layers)
        itm = document.layers[id];

    return itm;
}

function toggleItem(id)
{
    itm = getItem(id);

    if(!itm)
        return false;

    if(itm.style.display == 'none')
        itm.style.display = '';
    else
        itm.style.display = 'none';

    return false;
}



function changeTab(thisName, idx)
{
	toggleAllTabs(thisName, idx);
	return false;
}

/**
*	Overcomes IE's inability to select all objects by name
*	populates an array containing all objects with name="ARG1_i=0; i<ARG2"
*   to arbitrary max of 10 items [ debug ]
*/
function getObjectsByName(name)
{
	var objs = new Array();
    var i = 0;

    var obj = eval("document.all." + name + "_" + i);

	while (obj != null && i < 10) {
        objs[i] = obj;
        i++;
        obj = eval("document.all." + name + "_" + i);
    }
	return objs;
}

function toggleAllTabs(thisName, idx)
{	
	var tabs = (document.all) ? getObjectsByName(thisName) : document.getElementsByName(thisName);

	/* disable all except idx - //= bb.getAlibrisTabIdx() */
	for (var i=0; i<tabs.length; i++)
	{
		var tab = tabs[i];
		/* selected this tab */
		tab.style.display = "none";
        tab.style.visibility = "hidden";
	}

	var tab = (document.all) ? document.all[thisName + "_" + idx] : document.getElementById(thisName + "_" + idx);

	/* reset the previously highlighted tab */
	resetHighlightedTab(thisName, tabs, idx);

	/* select tab to change */

 	var container = (document.all) ? document.all["li_"+thisName+"_" + idx] : document.getElementById("li_"+thisName+"_" + idx);

 	container.className = "active";
	tab.style.display = "block";
    tab.style.visibility = "visible";

}

/*
	resets all highlighted tabs, except at idx, 
	since will be highlighting currently selected anyway
*/
function resetHighlightedTab(thisName, tabs, idx)
{	
    var containers = (document.all) ? getObjectsByName("li_"+thisName) : document.getElementsByName("li_"+thisName);

	for (var i=0; i<containers.length; i++)
	{
		var container = containers[i];
        container.className = "";
	}
}

function openInfoWindow(url)
{
    var opts = "toolbar=no,menubar=no,location=no,directories=no,status=no,scrollbars=no,scrolling=no,resizable=yes,copyhistory=no,width=410,height=500,top=100,left=200";
    var infoWindow = window.open(url,'info',opts);
}

// Predictive search stuff
function PredictiveSearch(override)
{
	document.search.override.value=override;
	document.search.submit();
}
function StyleSwitch(styleSwitch)
{
	document.search.styleSwitch.value=styleSwitch;
	document.search.submit();
}

/** Blackwell Shops Google Map */
function createBlackwellMap(ggmap) {
	var geocodes = new Array(ggmap.geocode.split(",")[0], ggmap.geocode.split(",")[1]);
//alert("createBlackwellMap: ggmap.geocode[0]: [" + geocodes[0] + "] geocodes[1]: [" + geocodes[1] + "] - ggmap.title: [" + ggmap.title + "]");
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(geocodes[0], geocodes[1]), 15);
		map.addControl(new GSmallMapControl());
		//map.addControl(new GScaleControl());
		map.addControl(new GMapTypeControl());
		map.addOverlay(new GMarker(new GLatLng(geocodes[0], geocodes[1])));
		//map.openInfoWindow(map.getCenter(), ggmap.title);
	}
}

// Some values for showing the Carousel - these get set if needed
// in buy.jsp or editorial/index.jsp
var promoVer  = 1;
var promoDis  = "";
var promoSite = "";
var promoCnt  = "";
var promoCat  = "";
var promoTyp  = "";
var promoDis  = "";

/** Sets the mainContent div with the passed flash XML file */
function setSWF(num)
{
   // Get a random number and the date/time so the XML does not gt cached by the flash
   var randomnumber=Math.floor(Math.random()*10);
   var currentTime = new Date();
   var gt = currentTime.getTime();

   var filename = "/assets/xml/" + promoSite + "/" + promoCnt + "_" + promoCat + "_" + promoTyp + "_" + num + ".xml?cb=" + gt + "." + randomnumber;

   // Version 2 has files somewhere else and also includes discount
   if(promoVer == 2) {
      // No discount is 0 discount
      if(promoDis == "")
         promoDis = "0";
      filename = "/assets/xml2/" + promoSite + "/" + promoCnt + "_" + promoCat + "_" + promoTyp + "_" + num + "_" + promoDis + ".xml?cb=" + gt + "." + randomnumber;
   }

// alert("Filename = "+filename);

    var flashvars = {xmlConfig: filename};
//window.alert(filename);
    var params = {};
    var attributes = {};
    params.bgcolor="#ffffff"
    if (num == 5)
        swfobject.embedSWF("/assets/swf/Main_600x205.swf", "mainContent", "600", "205", "8.0.0", "expressInstall.swf",flashvars,params,attributes);
    else if (num == 3)
        swfobject.embedSWF("/assets/swf/Main_493x227.swf", "mainContent", "493", "227", "8.0.0", "expressInstall.swf",flashvars,params,attributes);
}

/** Called when one of the 3 Bookseller, New or Soon links are hit
  * and updates the mainContent div with the new flash XML file */
function setSWF1(file, num)
{
    // Need to remove the underline of the clicked items
    // and add it to all of the others
    var link1 = document.getElementById("flashselectbookseller");
    var link2 = document.getElementById("flashselectnew");
//    var link3 = document.getElementById("flashselectsoon");

    if(link1 != null) {
        if(file == 'bookseller')
            link1.style.textDecoration='none';
        else
            link1.style.textDecoration='underline';
    }

    if(link2 != null) {
        if(file == 'new')
            link2.style.textDecoration='none';
        else
            link2.style.textDecoration='underline';
    }

//if(link3 != null) {
//    if(file == 'soon')
//        link3.style.textDecoration='none';
//    else
//        link3.style.textDecoration='underline';
//}

    // Store the passed file and set the flash xml
    promoTyp = file;
    setSWF(num);
}

/** Called when one of the category selection changes
  * and updates the mainContent div with the new flash XML file */
function setSWF2(num)
{
    // Get & store the passed file and set the flash xml
    var select = document.getElementById("flashselect");
    promoCat = select.value;
    setSWF(num);
}

/** Given a base URL sends the browser off to a page to view the entire promo
  */
function setSWF3(url)
{
    url = url + "?cnt=" + promoCnt + "&cat=" + promoCat + "&typ=" + promoTyp;

    window.location=url;
}

/** Sets the 'jacket' image to the new passed image
  * and resets all of the links
  */
function showNewJacket(isbn, id, src, num) {

    for(var i = 0; i <= num; i++) {
        if(i == id)
            document.getElementById("viewImage" + i).style.textDecoration='underline';
        else
            document.getElementById("viewImage" + i).style.textDecoration='none';
    }
    if(src != '') {

        //document.getElementById("jacket").src = src;
        document.getElementsByName("jacket")[0].src = src;
        //document.getElementById("jacket").parentNode.href = ('/jsp/display_large_jacket.jsp?isbn='+isbn+'_'+id);
        document.getElementsByName("jacket")[0].parentNode.href = ('/jsp/display_large_jacket.jsp?isbn='+isbn+'_'+id);
        document.getElementById("zoom").href = ('/jsp/display_large_jacket.jsp?isbn='+isbn+'_'+id);
        // document.getElementById("jacket").src = src;
        // document.getElementById("jacket").parentNode.href = ('/jsp/display_large_jacket.jsp?isbn=5029378000653_'+id);
        // document.getElementById("zoom").href = ('/jsp/display_large_jacket.jsp?isbn=5029378000653_'+id);
    }
}

/** Restricts keys used on an input matching the passed by returning false if 
*	entered keyCode does not match regexpr
*	regular expression.
*	<p>
*		Examples: 
*			onkeypress="return restrictKeys(event, '[0-9]');" 		// restrict input to numeric
*			onkeypress="return restrictKeys(event, '[A-z]|[0-9]');" // restrict to alphanumeric
*	</p>
*
*	@param e		- event
*	@param pattern 	- regexpr
*/
function restrictKeys(e, pattern)
{
    // Allow all key presses for now
    return 1;

	var key = 0;
	var charKey = "";
	
	if(window.event) 
		key = "" + e.keyCode; 
	else if(e.which) // Netscape/Firefox/Opera
		key = "" + e.which;
	if (key == 8 || key == 9 || key == 46 || (key == 118 && isCtrlKey)) pattern = "";
	charKey = String.fromCharCode(key);
	
	return (key == 0) || (charKey.match(pattern) != null);
}

function openHelpWindow(url)
{
    var opts = "toolbar=no,menubar=no,location=no,directories=no,status=no,scrollbars=yes,scrolling=yes,resizable=yes,copyhistory=no,width=410,height=450,top=100,left=200";
    var isbnWindow = window.open(url,'isbn13',opts);
}

function openHelpWindowSize(url, width, height)
{
    var opts = "toolbar=no,menubar=no,location=no,directories=no,status=no,scrollbars=yes,scrolling=yes,resizable=yes,copyhistory=no,width=" + width + ",height=" + height + ",top=100,left=200";
    var isbnWindow = window.open(url,'isbn13',opts);
}

function requestReward(fld) {
				
	if (fld && fld.checked) {
		if (fld.form.givexNumber) {
			fld.form.givexNumber.value = "";
		}
	}
}

