/// Non-Javascript users see old style 'add to list' link
isbn_post_str = '';
function create_add_link(isbn,override,script)
{
	div_id = "add_"+isbn;

	mouseeffect = 'onmouseover="this.src=\'/images/new/add_button_on.jpg\';" onmouseout="this.src=\'/images/new/add_button.jpg\';"';
	//mouseeffect = '';

	if (script == undefined) { script = "combo_pop_queue"; }
	
	if(override !== undefined)
	{
		// 1 is add button
		if(override == 1)
		{
			add_button = '<a style="cursor: pointer" onClick="myRequest.queuePost(\'/cgi-bin/'+script+'.cgi\', \'isbn='+isbn+'\', \'float_div\');"><img ' + mouseeffect + ' src="http://images.booksfree.com/images/new/add_button.jpg" alt="Add" border="0" height="22" width="92" /></a>';
		}
		// 2 is in queue
		else if(override == 2)
		{
			add_button = '<img src="http://images.booksfree.com/images/new/in_queue.jpg" alt="In Queue" border="0" height="22" width="92" />';
		}
	
		if(document.getElementById(div_id)) document.getElementById(div_id).innerHTML = add_button;
	}
	// go to the server to decide which button to display
	else
	{
		isbn_post_str += isbn + "^";
		add_button = '<a style="cursor: pointer" onClick="myRequest.queuePost(\'/cgi-bin/'+script+'.cgi\', \'isbn='+isbn+'\', \'float_div\');"><img  ' + mouseeffect + '  src="http://images.booksfree.com/images/new/add_button.jpg" alt="Add" border="0" height="22" width="92" /></a>';
		if(document.getElementById(div_id)) document.getElementById(div_id).innerHTML = add_button;
		//myRequest.queuePost('/php/cds/check_book', 'is_in_queue='+isbn, div_id);
	}
}

function create_save_link(isbn,override)
{
	div_id = "add_"+isbn;
	
	if(override !== undefined)
	{
		// 1 is add button
		if(override == 1)
		{
			add_button = '<a style="cursor: pointer" onClick="myRequest.queuePost(\'/cgi-bin/combo_pop_queue.cgi\', \'isbn='+isbn+'\', \'float_div\');"><img src="http://images.booksfree.com/images/new/save_button.jpg" alt="Save" border="0" height="22" width="92" /></a>';
		}
		// 2 is in queue
		else if(override == 2)
		{
			add_button = '<img src="http://images.booksfree.com/images/new/in_queue.jpg" alt="In Queue" border="0" height="22" width="92" />';
		}
	
		if(document.getElementById(div_id)) document.getElementById(div_id).innerHTML = add_button;
	}
	// go to the server to decide which button to display
	else
	{
		isbn_post_str += isbn + "^";
		add_button = '<a style="cursor: pointer" onClick="myRequest.queuePost(\'/cgi-bin/combo_pop_queue.cgi\', \'isbn='+isbn+'\', \'float_div\');"><img src="http://images.booksfree.com/images/new/save_button.jpg" alt="Save" border="0" height="22" width="92" /></a>';
		if(document.getElementById(div_id)) document.getElementById(div_id).innerHTML = add_button;
		//myRequest.queuePost('/php/cds/check_book', 'is_in_queue='+isbn, div_id);
	}
}

function create_add_link_rec(isbn,override)
{
	div_id = "add_"+isbn;
	
	if(override !== undefined)
	{
		// 1 is add button
		if(override == 1)
		{
			add_button = '<a style="cursor: pointer;" onClick="myRequest.queuePost(\'/cgi-bin/combo_pop_queue.cgi\', \'isbn='+isbn+'&button=small\', \'float_div\');"><img style="padding-top:4px;" src="http://images.booksfree.com/images/new/add_button_small.jpg" alt="Add" border="0" height="18"  width="56" /></a>';
		}
		// 2 is in queue
		else if(override == 2)
		{
			add_button = '<img style="padding-top:4px;" src="http://images.booksfree.com/images/new/in_q.jpg" alt="In Queue" border="0" height="18" width="56" />';
		}
	
		if(document.getElementById(div_id)) document.getElementById(div_id).innerHTML = add_button;
	}
	// go to the server to decide which button to display
	else
	{
		add_button = '<a style="cursor: pointer;" onClick="myRequest.queuePost(\'/cgi-bin/combo_pop_queue.cgi\', \'isbn='+isbn+'&button=small\', \'float_div\');"><img style="padding-top:4px;" src="http://images.booksfree.com/images/new/add_button_small.jpg" alt="Add" border="0" height="18"  width="56" /></a>';
		if(document.getElementById(div_id)) document.getElementById(div_id).innerHTML = add_button;
		//myRequest.queuePost('/php/cds/check_book', 'small_button=1&is_in_queue='+isbn, div_id);
	}
}

//favorite authors popup
function create_fa_link(author_id, override, atf_num)
{
	div_id = "fa_"+author_id;
	
	if(override !== undefined)
	{
		// 1 is add button
		if(override == 1)
		{
			fa_button = '<a style="margin-left:10px; text-decoration: none;cursor: pointer;" onClick="myRequest.queuePost(\'/cgi-bin/combo_add_fa.cgi\', \'add='+author_id+'\', \'float_div\'); create_fa_link(\''+author_id+'\', \'2\');"><img src="http://images.booksfree.com/images/add_to_favorite_authors.jpg" alt="Add to Favorite Authors" width="154px" height="18px" border="0" align=\"absmiddle\" /></a>';
		}
		// 2 is in favorite authors already
		else if(override == 2)
		{
			fa_button = '<img src="http://images.booksfree.com/images/added_to_favorite_authors.jpg" alt="Added to Favorite Authors" border="0" width="169px" height="18px" align=\"absmiddle\" style="margin-left:10px;" />';
		}
		// 3 is auto_add
		else if(override == 3)
		{
			fa_button = '<img src="http://images.booksfree.com/images/set_to_auto_adding.jpg" alt="Set to Auto Adding" border="0" width=\"160px\" height=\"22px\">';
			div_id = "auto_"+author_id;
		}
		// 4 is for text only, no image
		if(override == 4)
		{
			fa_button = '<a style="font-size: 8pt; text-decoration: underline; color: #003399; cursor: pointer;" onClick="myRequest.queuePost(\'/cgi-bin/combo_add_fa.cgi\', \'add='+author_id+'\', \'float_div\'); create_fa_link(\''+author_id+'\', \'5\', \''+atf_num+'\');">(add to favorites)</a>';
			div_id = "atf_"+atf_num;
		}
		// 5 is in favorite authors already for text only, no image
		else if(override == 5)
		{
			fa_button = '(added to favorites)';
			div_id = "atf_"+atf_num;
		}
		
		if(document.getElementById(div_id)) document.getElementById(div_id).innerHTML = fa_button;
	}
	// go to the server to decide which button to display
	else
	{
		// default to add button
		isbn_post_str += author_id + "^";
		fa_button = '<a style="margin-left:10px; text-decoration: none;cursor: pointer;" onClick="myRequest.queuePost(\'/combo_cgi-bin/add_fa.cgi\', \'add='+author_id+'\', \'float_div\'); create_fa_link(\''+author_id+'\', \'2\');"><img src="http://images.booksfree.com/images/add_to_favorite_authors.jpg" alt="Add to Favorite Authors" width="154px" height="18px" border="0" align=\"absmiddle\" /></a>';
		if(document.getElementById(div_id)) document.getElementById(div_id).innerHTML = fa_button;
		//myRequest.queuePost('/php/check_book', 'is_in_queue='+isbn, div_id);
	}
}

function create_ship_next_link(isbn,image_url)
{
	div_id = "ship_next_"+isbn;
	ship_next = "Ship Next";
	if (image_url == "shipping_next.gif") { ship_next = "Shipping Next"; }
	newLink = '<a style="cursor: pointer" onClick="myRequest.queuePost(\'/cgi-bin/ship_next.cgi\', \'section=combo&isbn='+isbn+'\', \''+div_id+'\');"><img src="http://images.booksfree.com/images/new/'+image_url+'" alt="'+ship_next+'" width="75" height="20" border="0"/></a>';
	document.getElementById(div_id).innerHTML = newLink;
}

//image rollover
function swapimages(which1,what1,which2,what2,which3,what3,which4,what4,which5,what5,which6,what6){
if (document.getElementById(''+which1+'')){
	document.getElementById(''+which1+'').src=''+what1+'';
}
if (document.getElementById(''+which2+'')){
	document.getElementById(''+which2+'').src=''+what2+'';
}
if (document.getElementById(''+which3+'')){
	document.getElementById(''+which3+'').src=''+what3+'';
}
if (document.getElementById(''+which4+'')){
	document.getElementById(''+which4+'').src=''+what4+'';
}
if (document.getElementById(''+which5+'')){
	document.getElementById(''+which5+'').src=''+what5+'';
}
if (document.getElementById(''+which6+'')){
	document.getElementById(''+which6+'').src=''+what6+'';
}
}

// higlight a table row.
function HighlightRowIfChecked(checkBox){
    var bgColor = checkBox.checked ? "#D9DDEF" : "";
    var el = checkBox.parentNode;
    while(el.tagName.toLowerCase() != "tr"){
        el = el.parentNode;
    }
    el.style.backgroundColor = bgColor;
}

//table row toggle
function toggle(id) {	
	if(document.getElementById(id).style.display=='block')
		{
		document.getElementById(id).style.display='none';
		document.getElementById(id).style.visibility='hidden';
		//alert('Its hidden now');
		}
	else 
		{
		document.getElementById(id).style.display='block';
		document.getElementById(id).style.visibility='visible';
		//alert('Its displaying now');
		}
}

// search stuff

function textClear (input) { if ( input.value == input.defaultValue ) { input.value = ""; } }
function textRestore (input) { if ( input.value == "" ) { input.value = input.defaultValue;	} }


function JSFX_FloatTopDiv(div_name)
{
}
	
// Long-in stuff

function textClear (input)
{
	if ( input.value == input.defaultValue ) 
	{ 
		input.value = ""; 
	} 
}
function textRestore (input)
{
	if ( input.value == "" ) 
	{
		input.value = input.defaultValue;	
	} 
}

 function changeBox()
 {
    document.getElementById('div1').style.display='none';
    document.getElementById('div2').style.display='';
    document.getElementById('password').focus();
 }
 function restoreBox()
 {
    if(document.getElementById('password').value=='')
    {
      document.getElementById('div1').style.display='';
      document.getElementById('div2').style.display='none';
    }
 }
 
 
 function getUrl(url) {
 var http = new XMLHttpRequest();
 http.open("GET",url,false);
 http.send("");
 return http.responseText;
 }
 
 dateobj = new Date();
 last_type = dateobj.getTime();
 flat_file = "";
 blockBlur = false;
 
 function filterText(sText) {
 	var reBadWords = /"|'/gi;
 	return sText.replace(reBadWords, function (sMatch) {
 		return sMatch.replace(/./g, "*");
 	});
 }
 
 function show_autosuggest()
 {
 	document.getElementById('autosuggestresults').style.display = 'block';
 }
 
 function hide_autosuggest_maybe()
 {
 	if(!blockBlur)
 	{
 		document.getElementById('autosuggestresults').style.display = 'none'; 		
 	}
 	blockBlur = false;
 }
 
 function hide_autosuggest()
 {
 	autosuggestobj = document.getElementById('autosuggestresults');
 	autosuggestobj.innerHTML = '';
 	autosuggestobj.style.backgroundColor = '';
 	autosuggestobj.style.width = '';
 	autosuggestobj.style.padding = '';
 	autosuggestobj.style.lineHeight = '';
 	autosuggestobj.style.border = '';
 }
 function getautosuggest(value)
 {
 	autosuggestobj = document.getElementById('autosuggestresults');
 	
 	if(value == "")
 	{
 		hide_autosuggest();
 		return;
 	}
 	
 	if(document.getElementById('searchInput').value !== value)
 	{
 		return;
 	}
 
 
 	if(!flat_file)
 	{
 		flat_file = getUrl('/autosuggest_cds.txt');
 		setTimeout("getautosuggest('" + value + "');", 1000);
 		hide_autosuggest();
 		return false;
 	}
 
 	value = filterText(value);
 	var newhtml = "";
 
 	myRE = new RegExp(value, "ig");
 
 	arr_flat = flat_file.split('\n');
 
 	count = 0;
 	for(x=0; x<arr_flat.length; x++)
 	{
 		results = arr_flat[x].match(myRE);
 		if(results)
 		{
 			newhtml = newhtml + "<div style='margin-bottom:8px;'><a onmousedown='blockBlur=true;' class='bluelink' href='http://www.booksfree.com/cgi-bin/combo_search.cgi?searchTypeId=keyword&searchInput=" + escape(arr_flat[x]) + "'>" + arr_flat[x] +  "</a></div>";
 			//newhtml = newhtml + arr_flat[x] + "<br />";
 			count++;
 			if(count > 10)
 			{
 				break;
 			}
 		}
 	}
 
 	if(newhtml == "")
 	{
 		//hide_autosuggest();
 		return;
 	}
 
	autosuggestobj = document.getElementById('autosuggestresults');
	autosuggestobj.innerHTML = newhtml;
	autosuggestobj.style.backgroundColor = '#FFFFFF';
	autosuggestobj.style.width = '249px';
	autosuggestobj.style.padding = '5px';
	autosuggestobj.style.marginLeft = '5px';
	autosuggestobj.style.border = '1px solid #999999';
 }
 
 //dropdown menu form style
function tamingselect()
{
	if(!document.getElementById && !document.createTextNode){return;}
	
// Classes for the link and the visible dropdown
	var ts_selectclass='turnintodropdown'; 	// class to identify selects
	var ts_listclass='turnintoselect';		// class to identify ULs
	var ts_boxclass='dropcontainer'; 		// parent element
	var ts_triggeron='activetrigger'; 		// class for the active trigger link
	var ts_triggeroff='trigger';			// class for the inactive trigger link
	var ts_dropdownclosed='dropdownhidden'; // closed dropdown
	var ts_dropdownopen='dropdownvisible';	// open dropdown
/*
	Turn all selects into DOM dropdowns
*/
	var count=0;
	var toreplace=new Array();
	var sels=document.getElementsByTagName('select');
	for(var i=0;i<sels.length;i++){
		if (ts_check(sels[i],ts_selectclass))
		{
			var hiddenfield=document.createElement('input');
			hiddenfield.name=sels[i].name;
			hiddenfield.type='hidden';
			hiddenfield.id=sels[i].id;
			hiddenfield.value=sels[i].options[0].value;
			sels[i].parentNode.insertBefore(hiddenfield,sels[i])
			var trigger=document.createElement('a');
			ts_addclass(trigger,ts_triggeroff);
			trigger.href='javascript:void(0);';
			trigger.style.color = "#777777";
			trigger.onclick=function(){
				ts_swapclass(this,ts_triggeroff,ts_triggeron)
				ts_swapclass(this.parentNode.getElementsByTagName('ul')[0],ts_dropdownclosed,ts_dropdownopen);
				return false;
			}
			trigger.appendChild(document.createTextNode(sels[i].options[0].text));
			sels[i].parentNode.insertBefore(trigger,sels[i]);
			var replaceUL=document.createElement('ul');
			for(var j=0;j<sels[i].getElementsByTagName('option').length;j++)
			{
				if(sels[i].getElementsByTagName('option')[j].value)
				{
					var newli=document.createElement('li');
					var newa=document.createElement('a');
					newli.v=sels[i].getElementsByTagName('option')[j].value;
					newli.elm=hiddenfield;
					newli.istrigger=trigger;
					newa.href='javascript.void(0);';
					newa.appendChild(document.createTextNode(
					sels[i].getElementsByTagName('option')[j].text));
					newli.onclick=function(){ 
						this.elm.value=this.v;
						ts_swapclass(this.istrigger,ts_triggeron,ts_triggeroff);
						ts_swapclass(this.parentNode,ts_dropdownopen,ts_dropdownclosed)
						this.istrigger.firstChild.nodeValue=this.firstChild.firstChild.nodeValue;
						return false;
					}
					newli.appendChild(newa);
					replaceUL.appendChild(newli);
				}
			}
			ts_addclass(replaceUL,ts_dropdownclosed);
			var div=document.createElement('div');
			div.appendChild(replaceUL);
			ts_addclass(div,ts_boxclass);
			sels[i].parentNode.insertBefore(div,sels[i])
			toreplace[count]=sels[i];
			count++;
		}
	}
	
/*
	Turn all ULs with the class defined above into dropdown navigations
*/	

	var uls=document.getElementsByTagName('ul');
	for(var i=0;i<uls.length;i++)
	{
		if(ts_check(uls[i],ts_listclass))
		{
			var newform=document.createElement('form');
			var newselect=document.createElement('select');
			for(j=0;j<uls[i].getElementsByTagName('a').length;j++)
			{
				var newopt=document.createElement('option');
				newopt.value=uls[i].getElementsByTagName('a')[j].href;	
				newopt.appendChild(document.createTextNode(uls[i].getElementsByTagName('a')[j].innerHTML));	
				newselect.appendChild(newopt);
			}
			newselect.onchange=function()
			{
				window.location=this.options[this.selectedIndex].value;
			}
			newform.appendChild(newselect);
			uls[i].parentNode.insertBefore(newform,uls[i]);
			toreplace[count]=uls[i];
			count++;
		}
	}
	for(i=0;i<count;i++){
		toreplace[i].parentNode.removeChild(toreplace[i]);
	}
	function ts_check(o,c)
	{
	 	return new RegExp('\\b'+c+'\\b').test(o.className);
	}
	function ts_swapclass(o,c1,c2)
	{
		var cn=o.className
		o.className=!ts_check(o,c1)?cn.replace(c2,c1):cn.replace(c1,c2);
	}
	function ts_addclass(o,c)
	{
		if(!ts_check(o,c)){o.className+=o.className==''?c:' '+c;}
	}
}

// DH 2009-07-15 automatically display sign in or out link
function sign_in_out()
{
	if(typeof(document.getElementById('sign_in_out')) != "undefined" && document.getElementById('sign_in_out'))
	{
		sign_in_out = document.getElementById('sign_in_out');
		signed_in = 0;
		// check cookies
		
		var ck_name=new Array(12)
		ck_name[0]="BFmem";
		ck_name[1]="BFcdmem";
		ck_name[2]="BFmp3mem";
		ck_name[3]="BFcombo";
		ck_name[4]="BFkids";
		ck_name[5]="BFmemsession";
		ck_name[6]="BFcdmemsession";
		ck_name[7]="BFmp3memsession";
		ck_name[8]="BFcombosession";
		ck_name[9]="BFkidssession";
		ck_name[10]="BFalacarte";
		ck_name[11]="BFalacartesession";
		
		var x=0;
		for(x=0; x<12; x++)
		{
			if(signed_in != 1) {
				var ck_data = readCookie(ck_name[x]);
				if(ck_data != '000' && ck_data){signed_in = 1;} // they are signed in, or at least are pretending to be
			}
		}
		
		if(signed_in)
		{
		sign_in_out.href= "http://www.booksfree.com/cgi-bin/logout.cgi";
		sign_in_out.innerHTML = "Sign Out";
		}
		else
		{
		sign_in_out.href= "http://www.booksfree.com/mem_login_home.shtml";
		sign_in_out.innerHTML = '<span style="padding-left:5px;">Sign In</span>';		
		}		
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

window.onload=function()
{
	tamingselect();
	sign_in_out();
	// add more functions if necessary
}

