/// Non-Javascript users see old style 'add to list' link
isbn_post_str = '';
function create_add_link(isbn,override)
{
	div_id = "add_"+isbn;
	
	mouseeffect = 'onmouseover="this.src=\'/images/new/add_button_on.jpg\';" onmouseout="this.src=\'/images/new/add_button.jpg\';"';
	
	if(override !== undefined)
	{
		// 1 is add button
		if(override == 1)
		{
			add_button = '<a style="cursor: pointer" onClick="myRequest.queuePost(\'/cgi-bin/mp3_pop_queue.cgi\', \'isbn='+isbn+'\', \'float_div\');"><img src="http://images.booksfree.com/images/new/add_button.jpg" ' + mouseeffect + ' 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/mp3_pop_queue.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/mp3_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/mp3_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/mp3_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/mp3_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/mp3_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/mp3_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(\'/mp3_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=mp3&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_mp3.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/mp3_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';
 }
 
 // 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;
		
		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()
{
	sign_in_out();
	// add more functions if necessary
}