function counter(page) {
 var pg, ref, i, uid, ip;

 if (document.images)
 {
  if (typeof dwPageID == 'undefined') {dwPageID = ''}

  if (dwPageID.substr(0,1) == 'b') {
    var bsarr = dwPageID.split('_');
    bid = bsarr[1];
    bs = getcookie('ffbs');
    if (bs == '') {
      setcookie('ffbs', bid);
    } else {
      if (bs.indexOf(bid) == -1) {
        bs = bs + '|' + bid;
        setcookie('ffbs', bs);
      }
    }
  } else if (dwPageID.substr(0,1) == 'a') {
    aid = dwPageID.substr(1);
    as = getcookie('ffas');
    if (as == '') {
      setcookie('ffas', aid);
    } else {
      if (as.indexOf(aid) == -1) {
        as = as + '|' + aid;
        setcookie('ffas', as);
      }
    }
  }

  uid = getcookie('ffuid');
  if (uid == '') {
    dt = new Date();
    uid = Math.round(dt.getTime()/1000) + '_' + Math.round(Math.random()*2147483647);
    setcookie('ffuid', uid );
    uid = getcookie('ffuid');
  }
  if (page && page!="") {
   pg = escape(page);
   ref = dwPageID;
   dwPageID = escape(page);
  } else {
   pg = escape(document.location);
   ref = escape(document.referrer);
  }

  var nav_lng = "";
  if (navigator.language) nav_lng = navigator.language; 
  if (navigator.browserLanguage) nav_lng = navigator.userLanguage; 
  if (navigator.systemLanguage) nav_lng = navigator.systemLanguage; 
  if (navigator.browserLanguage) nav_lng = navigator.browserLanguage; 
  if (navigator.userLanguage) nav_lng = navigator.userLanguage; 
  if (window.navigator.language) nav_lng = window.navigator.language; 

  i = new Image(1,1); 
  i.src = "http://cgi.fantasticfiction.co.uk/cgi-bin/tracker.fcgi?pg=" + pg + "&ref=" + ref + "&uid=" + uid + "&pageid=" + dwPageID + "&bl=" + nav_lng;
  i.onload = function() {return;}
 }
}

function ct(shop) {
  if (document.images){ 
    shop = shop.toLowerCase();
    shop = shop.replace(/\s/g, '');
    if (typeof dwPageID == 'undefined') {dwPageID = ''}
    (new Image()).src = "http://cgi.fantasticfiction.co.uk/cgi-bin/click.cgi?shop=" + shop + "&from=" + dwPageID;
  } 
  return true;
}

function checker() {
 j = new Image(1,1); 
 j.src = "http://www.ffbooks.co.uk/newbooks/checker.cgi?r=" + dwR + "&fn=" + escape(dwFN) + "&sn=" + escape(dwSN);
 j.onload = function() {return;}
}

function setcookie(NameOfCookie, value){
 cookiestring= NameOfCookie + "=" + escape(value) + "; path=/; domain=.fantasticfiction.co.uk;";
 document.cookie=cookiestring;
}

function setpermcookie(NameOfCookie, value){
 expire = new Date();
 today = new Date();
 expire.setTime(today.getTime() + 3600000*24*90);
 cookiestring = NameOfCookie + "=" + escape(value) + "; expires=" + expire.toGMTString() + "; path=/; domain=.fantasticfiction.co.uk;";
 document.cookie=cookiestring;
}

function getcookie(NameOfCookie) {
if (document.cookie.length > 0) { 
  begin = document.cookie.indexOf(NameOfCookie+"="); 
  if (begin != -1) { 
    begin += NameOfCookie.length+1; 
    end = document.cookie.indexOf(";", begin);
    if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end)); 
  } 
}
return ''; 
}

function delcookie(NameOfCookie) {
 expire = new Date();
 today = new Date();
 expire.setTime(today.getTime() - 1800000);
 cookiestring = NameOfCookie + "=x" + "; expires=" + expire.toGMTString() + "; domain=.fantasticfiction.co.uk;";
 document.cookie=cookiestring;
}

function goList(let) {
self.location = "http://www.fantasticfiction.co.uk/" + let.toLowerCase() + "/index" + document.menuForm.genreSelect.options[document.menuForm.genreSelect.selectedIndex].value + ".html";
}

function checkisbn(isbn) {
  var isbnw = isbn.replace(/-/g, '');
  isbnw = isbnw.replace(/ /g, '');

  isbnw = isbnw.toUpperCase();

  if (isbnw.length == 13) {
    return checkisbn13(isbn);
  }

  if (isbnw.length != 10) {
    window.alert('The ISBN number you entered is not valid');
    return false;
  }

  var validChars = '0123456789';
  var validCharsX = '0123456789X';

  for (i = 0; i < 9; i++) {
    strChar = isbnw.charAt(i);
    if (validChars.indexOf(strChar) == -1) {
      window.alert('The ISBN number you entered is not valid');
      return false;
    }
  }
  if (validCharsX.indexOf(isbnw.charAt(9)) == -1) {
    window.alert('The ISBN number you entered is not valid');
    return false;
  }

  var isbn1 = isbnw.charAt(0);
  var isbn2 = isbnw.charAt(1);
  var isbn3 = isbnw.charAt(2);
  var isbn4 = isbnw.charAt(3);
  var isbn5 = isbnw.charAt(4);
  var isbn6 = isbnw.charAt(5);
  var isbn7 = isbnw.charAt(6);
  var isbn8 = isbnw.charAt(7);
  var isbn9 = isbnw.charAt(8);

  var val = eval(isbn1) * 10;
  val = val + eval(isbn2) * 9;
  val = val + eval(isbn3) * 8;
  val = val + eval(isbn4) * 7;
  val = val + eval(isbn5) * 6;
  val = val + eval(isbn6) * 5;
  val = val + eval(isbn7) * 4;
  val = val + eval(isbn8) * 3;
  val = val + eval(isbn9) * 2;

  decimalval = Math.floor(val % 11);
  digitval = 11 - decimalval; 

  if (digitval == 10) {
    digitval = 'X';
  } else if (digitval == 11) {
    digitval = '0';
  } else {
    digitval = digitval.toString();
  }

  var digit = isbnw.charAt(9);

  if (digit == digitval) {
    return true;
  } else {
    alert('The ISBN number you entered is not valid');
  }
  return false;
}

function checkisbn13(isbn) {
  var isbnw = isbn.replace(/-/g, '');
  isbnw = isbnw.replace(/ /g, '');

  if (isbnw.length != 13) {
    window.alert('The ISBN-13 number you entered is not valid :1');
    return false;
  }

  var validChars = '0123456789';

  for (i = 0; i < 9; i++) {
    strChar = isbnw.charAt(i);
    if (validChars.indexOf(strChar) == -1) {
      window.alert('The ISBN-13 number you entered is not valid :2');
      return false;
    }
  }

  var isbn1 = isbnw.charAt(0);
  var isbn2 = isbnw.charAt(1);
  var isbn3 = isbnw.charAt(2);
  var isbn4 = isbnw.charAt(3);
  var isbn5 = isbnw.charAt(4);
  var isbn6 = isbnw.charAt(5);
  var isbn7 = isbnw.charAt(6);
  var isbn8 = isbnw.charAt(7);
  var isbn9 = isbnw.charAt(8);
  var isbn10 = isbnw.charAt(9);
  var isbn11 = isbnw.charAt(10);
  var isbn12 = isbnw.charAt(11);

  var odd = eval(isbn1) + eval(isbn3) + eval(isbn5) + eval(isbn7) + eval(isbn9) + eval(isbn11);
  var even = eval(isbn2) + eval(isbn4) + eval(isbn6) + eval(isbn8) + eval(isbn10) + eval(isbn12);

  var digitval = odd + (even * 3);

  decimalval = Math.floor(digitval % 10);
  digitval = 10 - decimalval; 
  digitval = digitval.toString();

  if (digitval == 10) {
    digitval = '0';
  }

  var digit = isbnw.charAt(12);

  if (digit == digitval) {
    return true;
  } else {
    alert('The ISBN-13 number you entered is not valid - check digit should be '+digitval);
  }
  return false;
}

function right(str, n) {
if (n <= 0)
  return "";
else if (n > String(str).length)
  return str;
else { 
  var iLen = String(str).length;
  return String(str).substring(iLen, iLen - n);
}
}

function writePageupdated() {
var dt=new Date(Date.parse(document.lastModified));
var m = new Array;
m[0]="Jan";m[1]="Feb";m[2]="Mar";m[3]="Apr";m[4]="May";m[5]="Jun";m[6]="Jul";m[7]="Aug";m[8]="Sep";m[9]="Oct";m[10]="Nov";m[11]="Dec";
document.write(dt.getDate()+'-'+m[dt.getMonth()]+'-20'+right(dt.getYear(),2));
}

function writeBookshopsuk() {
if (typeof dwAuthorName == 'undefined') {dwAuthorName = ''}
if (typeof dwBookTitle == 'undefined') {dwBookTitle = ''}
document.write('<table>');
document.write('<tr align="center"><td>');
document.write('<form name=bookshopukform target="_top" method="get" action="http://cgi.fantasticfiction.co.uk/cgi-bin/bookshops.cgi" onsubmit="if (document.bookshopukform.shop.options[document.bookshopukform.shop.selectedIndex].value==\'amazonuk\') {return counter(\'/outgoing/amazonuk\')} else if(document.bookshopukform.shop.options[document.bookshopukform.shop.selectedIndex].value==\'abeuk\') {return counter(\'/outgoing/abeuk\')} else {return true}">');
document.write('<input type=hidden name=from value="page">');
document.write('<input type=hidden name=author value="');
document.write(dwAuthorName);
document.write('">');
document.write('<input type=hidden name=book value="');
if (dwBookTitle != "") {
document.write(dwBookTitle); } else {
document.write(''); }
document.write('">');
document.write('<table class="box">');
document.write('<tr><th bgcolor="darkblue">');
document.write('<font size="+1">Search the<br>UK bookshops</font>');
document.write('</th></tr>');
document.write('<tr><td align="center">');
document.write('<select size=10 name="shop">');
document.write('<option value="abeuk">Abebooks UK&nbsp');
document.write('<option selected value="amazonuk">Amazon UK&nbsp');
document.write('<option value="blackwell">Blackwell&nbsp');
document.write('<option value="bookfellas">BookFellas&nbsp');
document.write('<option value="ebayuk">eBay UK&nbsp');
document.write('<option value="foyles">Foyles');
document.write('<option value="pickabook">Pickabook&nbsp');
document.write('<option value="play">Play.com');
//document.write('<option value="waterstones">Waterstones&nbsp');
//document.write('<option value="whsmith">WHSmith');
document.write('</select>');
document.write('</td></tr>');
document.write('<tr><td align="center">');
document.write('<input type="text" name="keywords" SIZE=16 VALUE="');
document.write(dwAuthorName);
if (dwBookTitle != "") {
document.write(' ');
document.write(dwBookTitle);
}
document.write('"></td></tr><tr><td align="center"><input type="submit" border=0 value="Search" name="Search"></td></tr></table></form></td></tr></table>');
}

function writeBookshopsus() {
if (typeof dwAuthorName == 'undefined') {dwAuthorName = ''}
if (typeof dwBookTitle == 'undefined') {dwBookTitle = ''}
document.write('<table><tr align="center"><td><form name=bookshopform target="_top" method="get" action="http://cgi.fantasticfiction.co.uk/cgi-bin/bookshops.cgi" onsubmit="if (document.bookshopform.shop.options[document.bookshopform.shop.selectedIndex].value==\'amazon\') {return counter(\'/outgoing/amazon\')} else if(document.bookshopform.shop.options[document.bookshopform.shop.selectedIndex].value==\'abe\') {return counter(\'/outgoing/abe\')} else {return true}"><input type=hidden name=author value="');
document.write(dwAuthorName);
document.write('"><input type=hidden name=book value="');
if (dwBookTitle != "") {
  document.write(dwBookTitle);
} else {
  document.write('');
}
document.write('"><table class="box"><tr><th bgcolor="darkblue"><font size="+1">Search the<br>US bookshops</font></th></tr><tr><td align="center"><select size=10 name="shop"><option value="abe">Abebooks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<option  value="alibris">Alibris<option selected value="amazon">Amazon<option value="bamm">BAMM<option value="biblio">Biblio<option value="buy">buy.com<option value="ebayus">eBay US<option value="half">Half.com<option value="powells">Powells</select></td></tr><tr><td align="center"><input type="text" name="keywords" SIZE=16 VALUE="');
document.write(dwAuthorName);
if (dwBookTitle != "") {
  document.write(' ');
  document.write(dwBookTitle);
}
document.write('"></td></tr><tr><td align="center"><input type="submit" border=0 value="Search" name="Search"></td></tr></table></form></td></tr></table>');
}

function writeebayheader(a, b) {
if (typeof country == 'undefined') {country = 'US'};

sc = getcookie('ffsc');
if (sc != '') {
 country = sc;
} 

var t = a + "'s " + b;

if (country == 'GB') {
  document.write('Current eBay UK auctions for ');
} else if (country == 'CA') {
  document.write('Current eBay CA auctions for ');
} else if (country == 'AU') {
  document.write('Current eBay AU auctions for ');
} else if (country == 'FR') {
  document.write('Current eBay FR auctions for ');
} else {
  document.write('Current eBay US auctions for ');
}
document.write(t);
}

function writeebay(a, b) {
if (typeof country == 'undefined') {country = 'US'};

sc = getcookie('ffsc');
if (sc != '') {
 country = sc;
} 

var s = a + ' ' + b;
s.replace(/ /g, '+');

if (country == 'GB') {
  document.write('<scr' + 'ipt language="JavaScript" src="http://ilapi.ebay.com/ws/eBayISAPI.dll?EKServer&ai=fyvlyklq%7B%7Eq%7Blqwv%28%29%21%21&bdrcolor=000000&cid=0&eksize=1&encode=UTF-8&endcolor=FF0000&endtime=y&fbgcolor=00008B&fntcolor=FFFFFF&fs=0&hdrcolor=000000&hdrimage=1&hdrsrch=y&img=y&lnkcolor=00FFFF&logo=3&num=6&numbid=y&paypal=n&popup=n&prvd=9&query=' + s + '&r0=1&shipcost=n&siteid=3&sort=MetaLowestPriceSort&sortby=price&sortdir=asc&srchdesc=n&tbgcolor=00008B&title=&tlecolor=000000&tlefs=0&tlfcolor=FFFFFF&toolid=10004&track=5335903573&width=740"></scr' + 'ipt>');

} else if (country == 'CA') {
  document.write('<scr' + 'ipt language="JavaScript" src="http://ilapi.ebay.com/ws/eBayISAPI.dll?EKServer&ai=fyvlyklq%7B%7Eq%7Blqwv%28%29%21%21&bdrcolor=000000&cid=0&eksize=1&encode=UTF-8&endcolor=FF0000&endtime=y&fbgcolor=00008B&fntcolor=FFFFFF&fs=0&hdrcolor=000000&hdrimage=1&hdrsrch=n&img=y&lnkcolor=00FFFF&logo=3&num=6&numbid=y&paypal=n&popup=n&prvd=9&query=' + s + '&r0=1&shipcost=n&siteid=2&sort=MetaLowestPriceSort&sortby=price&sortdir=asc&srchdesc=n&tbgcolor=00008B&title=&tlecolor=000000&tlefs=0&tlfcolor=FFFFFF&toolid=10004&track=5335903573&width=740"></scr' + 'ipt>');

} else if (country == 'AU') {
  document.write('<scr' + 'ipt language="JavaScript" src="http://ilapi.ebay.com/ws/eBayISAPI.dll?EKServer&ai=fyvlyklq%7B%7Eq%7Blqwv%28%29%21%21&bdrcolor=000000&cid=0&eksize=1&encode=UTF-8&endcolor=FF0000&endtime=y&fbgcolor=00008B&fntcolor=FFFFFF&fs=0&hdrcolor=000000&hdrimage=1&hdrsrch=n&img=y&lnkcolor=00FFFF&logo=3&num=6&numbid=y&paypal=n&popup=n&prvd=9&query=' + s + '&r0=1&shipcost=n&siteid=15&sort=MetaLowestPriceSort&sortby=price&sortdir=asc&srchdesc=n&tbgcolor=00008B&title=&tlecolor=000000&tlefs=0&tlfcolor=FFFFFF&toolid=10004&track=5335903573&width=740"></scr' + 'ipt>');

} else if (country == 'FR') {
  document.write('<scr' + 'ipt language="JavaScript" src="http://ilapi.ebay.com/ws/eBayISAPI.dll?EKServer&ai=fyvlyklq%7B%7Eq%7Blqwv%28%29%21%21&bdrcolor=000000&cid=0&eksize=1&encode=UTF-8&endcolor=FF0000&endtime=y&fbgcolor=00008B&fntcolor=FFFFFF&fs=0&hdrcolor=000000&hdrimage=1&hdrsrch=n&img=y&lnkcolor=00FFFF&logo=3&num=6&numbid=y&paypal=n&popup=n&prvd=9&query=' + s + '&r0=1&shipcost=n&siteid=71&sort=MetaLowestPriceSort&sortby=price&sortdir=asc&srchdesc=n&tbgcolor=00008B&title=&tlecolor=000000&tlefs=0&tlfcolor=FFFFFF&toolid=10004&track=5335903573&width=740"></scr' + 'ipt>');

} else {
  document.write('<scr' + 'ipt language="JavaScript" src="http://lapi.ebay.com/ws/eBayISAPI.dll?EKServer&ai=dm%7Bt%7Ei%2B%28%29&bdrcolor=000000&cid=0&eksize=1&encode=UTF-8&endcolor=FF0000&endtime=y&fbgcolor=00008B&fntcolor=FFFFFF&fs=0&hdrcolor=000000&hdrimage=1&hdrsrch=y&img=y&lnkcolor=00FFFF&logo=3&num=6&numbid=y&paypal=n&popup=n&prvd=9&query=' + s + '&r0=1&shipcost=n&siteid=0&sort=MetaLowestPriceSort&sortby=price&sortdir=asc&srchdesc=n&tbgcolor=00008B&title=&tlecolor=000000&tlefs=0&tlfcolor=FFFFFF&toolid=10004&track=5335903573&width=740"></scr' + 'ipt>');
}
}

function showprefs() {
  if (ffsid != '') {
    document.write('<strong>Logged in as:</strong>'); 
  } else {
//    document.write('<a href="/login.htm">Login</a><strong> / </strong><a href="/register.htm">Register</a>');
    document.write('<a href="/preferences.htm">Preferences</a>');
  }
}

function showmainframe() {
  var mainframe = document.getElementById('mainframe');
  if (mainframe) {
    if (ffsid != '') {
      mainframe.style.width = '950px';
    }
    sp = getcookie('ffsp');
    if (sp != '') {
      arr = sp.split('|');
      mainframe.style.fontSize = arr[0] + '%';
    }
  }
}

function showsideframe() {
  if (ffsid != '') {
    var sideframe = document.getElementById('sideframe');
    if (sideframe) {
      sideframe.style.visibility = "visible";
      if (ffuser.isCoverEditor == true) {
        document.write('<scr' + 'ipt src="/usr/ffcovereditor.js" type="text/javascript"></scr' + 'ipt>');
      } else if (ffuser.isEditor) {
        document.write('<scr' + 'ipt src="/usr/ffeditor.js" type="text/javascript"></scr' + 'ipt>');
      } else {
        document.write('<scr' + 'ipt src="/usr/ffuser.js" type="text/javascript"></scr' + 'ipt>');
      }
    }
  }
}

function logincheck() {
  if (typeof country == 'undefined') {country = 'US'};
  sc = getcookie('ffsc');
  if (sc != '') {
    country = sc;
  } 
  if (ffsid != '') {
    if (autologin == 'Y') {
      setcookie('ffprefs', ffprefs);
    } else if (autologin == 'F') {
      window.location = 'http://www.fantasticfiction.co.uk/logout.htm';
    }
    ffuser = new userobject();
  }
}


function userobject() {
  this.username = '';
  this.prefs = '';
  this.url = '';
  this.svr = '';
  this.sid = '';

  this.isCookied = false;
  this.isLoggedOn = false;
  this.isCoverEditor = false;
  this.isEditor = false;

  var ffsid = getcookie('ffsid');
  var ffprefs = getcookie('ffprefs');

  if (ffsid != '') {
    this.isCookied = true;
    this.isLoggedOn = true;
    this.sid = ffsid;
    arr = ffprefs.split('|');
    this.username = arr[0];

    if (arr[1] == '200') {

      this.isCoverEditor = true;
      this.url = '/covereditors/' + this.username;
    } else if (arr[1] == '100') {
      this.isEditor = true;
      this.url = '/editors/' + this.username;
    } else {
      this.url = '/users/' + this.username;
    }
    this.prefs = arr[2];

    arr = ffsid.split(',');
    if (arr[0] == 1) {
      this.svr = 'ffmon';
    }
  }
}

function resizeiframe(f) {
  f.style.height = (f.contentWindow.document.body.scrollHeight+20) + "px";
}

var ffuser = new userobject();

var ffsid = getcookie('ffsid');
var ffprefs = getcookie('ffprefs');
var autologin = 'N';
if (ffsid != '') {
  if (ffprefs == '') {
    var dataurl = 'http://cgi.fantasticfiction.co.uk/cgi-bin/users/autologin.cgi?sid=' + ffsid;
    document.write('<scr' + 'ipt src="' + dataurl + '" type="text/javascript"></scr' + 'ipt>');
  }
}