function setCookie(name, value, expire) {
    document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
    return true;
}

function getCookie(Name) {
          var search = Name + "="
          if (document.cookie.length > 0) { // if there are any cookies
                    offset = document.cookie.indexOf(search)
                    if (offset != -1) { // if cookie exists
                              offset += search.length
                              // set index of beginning of value
                              end = document.cookie.indexOf(";", offset)
                              // set index of end of cookie value
                              if (end == -1)
                                        end = document.cookie.length
                              return unescape(document.cookie.substring(offset, end))
                    }
          }
          return null;
}

function j(s) { 
   //open(s,'','resizable=1,scrollbars=1,status=yes,toolbar=no,location=no,menu=no','fullscreen,scrollbars' );
   //open(s,'','resizable=1,scrollbars=1,status=yes,toolbar=yes,location=no,','fullscreen,scrollbars' );
   open(s,'','resizable=1,scrollbars=1,status=yes,titlebar=yes,toolbar=yes,menubar=yes,location=no,','fullscreen,scrollbars' );
}

function k(s) { 
   //open(s,'','resizable=1,scrollbars=1,status=yes,toolbar=no,location=no,menu=no','fullscreen,scrollbars' );
   //open(s,'','resizable=1,scrollbars=1,status=yes,toolbar=yes,location=no,','fullscreen,scrollbars' );
   open(s,'','resizable=1,scrollbars=1,status=yes,titlebar=yes,toolbar=no,menubar=no,location=no,','fullscreen,scrollbars' );
}

function k2(s) { 
   var n = 'newwindow';
   var w = (screen.width * (0.80));
   var l = (screen.width * (0.19));
   var h = (screen.height - 20);
   var winProperty = 'resizable=1,scrollbars=1,titlebar=yes,menubar=yes,toolbar=yes,location=no'
                   + ',width='+w+',top=20,left='+l;
   var newWin = open(s,n,winProperty);
   //open(s,'','resizable=1,scrollbars=1,status=yes,titlebar=yes,toolbar=yes,menubar=yes,location=no,width='+w+',top=20,left='+l);                         
}

function openPopup(s, n, width, height, top, left) {
   var winProperty = 'resizable=yes,scrollbars=no,dependent=1,titlebar=no,menubar=no,toolbar=no,location=no'
                   + ',width='+width+',height='+height+',top='+top+',left='+left;
   var newWin = open(s,n,winProperty);
   newWin.focus();
}
function centerPopup(s, n, width, height) {
   var winl = (screen.width - width) / 2;
   var wint = (screen.height - height) / 2;
   openPopup(s, n, width, height, wint, winl);
}

function popupPage(s, n, width, height, top, left) {
   var count = getCookie(n);
   if (count == null) {
       var today   = new Date();
       var expires = new Date();
       expires.setTime(today.getTime() + 60*60*24*7*1000);
       count = 1;
       setCookie(n, count, expires);
       openPopup(s, n, width, height, top, left);
   }
}

function JumpToIt(list) {
	var listValue = list.options[list.selectedIndex].value;
	var newPage = listValue+"Category.html";
	if (listValue == "" || listValue == "computerbooks3.html") {
		newPage = "computerbooks3.html";
	}
	//if (newPage == "Books.html") newPage = "computerbooks3.html";
    if (newPage != "None")
        window.location.href=newPage
}

function JumpToPage(list) {
    var newPage = list.options[list.selectedIndex].value;
    if (newPage != "None")
        window.location.href=newPage
}

function JumpToBookHome() {
   if (document.referrer.indexOf("boat") == -1 || document.referrer.indexOf("maththinking") == -1) {
       window.top.location.replace("http://www.maththinking.com/boat/booksIndex.html");
   }
}

function JumpToBookHome2() {
   if (document.referrer.indexOf("boat") == -1 || document.referrer.indexOf("maththinking") == -1) {
       window.top.location.replace("http://www.maththinking.com/boat/computerbooks.html");
   }
}

function JumpToHome() {
   if (window.parent == window.self) {
       window.top.location.replace("index.html");
   }
}

function JumpTo(url) {
   if (window.parent == window.self) {
       window.top.location.replace(url);
   }
}

function showImage(imageName, imageFile) {
	document.images[imageName].src = imageFile;
}

var right_msg="How do you do?";

function checkFields() {
   if (document.search.keywords.value == "" || document.search.keywords.value == " " || 
       document.search.keywords.value == "  " || document.search.keywords.value == "   " || 
       document.search.keywords.value == "     ") {
       alert("Please enter a search pattern in the title or author names. \n\nYou may use Regular Expression for patterns");
       document.search.keywords.focus();
       //return false;
   } else {
       document.search.submit();
   }
   //return true;
}

var message="";
function clickIE() {
	if (document.all) {
		(message);
		return false;
	}
}
function clickNS(e) {
	if (document.layers||(document.getElementById&&!document.all)) {
        if (e.which==2||e.which==3) {
			(message);
			return false;
		}
	}
}
if (document.layers) {
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=clickNS;
} else {
	document.onmouseup=clickNS;
	document.oncontextmenu=clickIE;
}
document.oncontextmenu=new Function("return false");

var da = (document.all) ? 1 : 0;
var pr = (window.print) ? 1 : 0;
var mac = (navigator.userAgent.indexOf("Mac") != -1);
 
function printPage() {
  if (pr) // NS4, IE5
    window.print()
  else if (da && !mac) // IE4 (Windows)
    vbPrintPage()
  else // other browsers
    alert("Sorry, your browser doesn't support this feature.\nTo print, from the File Menu or Tool Bar, select Print.");
  //return false;
}
 
if (da && !pr && !mac) with (document) {
  writeln('<OBJECT ID="WB" WIDTH="0" HEIGHT="0" CLASSID="clsid:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>');
  writeln('<' + 'SCRIPT LANGUAGE="VBScript">');
  writeln('Sub window_onunload');
  writeln('  On Error Resume Next');
  writeln('  Set WB = nothing');
  writeln('End Sub');
  writeln('Sub vbPrintPage');
  writeln('  OLECMDID_PRINT = 6');
  writeln('  OLECMDEXECOPT_DONTPROMPTUSER = 2');
  writeln('  OLECMDEXECOPT_PROMPTUSER = 1');
  writeln('  On Error Resume Next');
  writeln('  WB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER');
  writeln('End Sub');
  writeln('<' + '/SCRIPT>');
}

function toggleDisplay(objectID) {
	var object = document.getElementById(objectID);
	var state = object.style.display;
	if (state == '' || state == 'none')
		object.style.display = 'block';
	else if (state != 'none')
		object.style.display = 'none'; 
}

function setDisplay(objectID,state) {
	var object = document.getElementById(objectID);
	object.style.display = state;
}

function imgSwap(oid) {
    var myimage = document.getElementById('img'+oid);
    var currSrc = myimage.src;
    if (currSrc.endsWith("expand.gif")) {
        myimage.src = currSrc.gsub("expand.gif", "collapse.gif");
    } else {
        myimage.src = currSrc.gsub("collapse.gif", "expand.gif");
    }
    return currSrc;
}

function openBlock(oid){
    var myimage = document.getElementById('img'+oid);
    var currSrc = myimage.src;
    if (currSrc.endsWith("expand.gif")) {
        myimage.src = currSrc.gsub("expand.gif", "collapse.gif");
    }
    setDisplay(oid, 'block');         
}

function closeBlock(oid){
    var myimage = document.getElementById('img'+oid);
    var currSrc = myimage.src;
    if (currSrc.endsWith("collapse.gif")) {
        myimage.src = currSrc.gsub("collapse.gif", "expand.gif");
    }
    setDisplay(oid, 'none');         
}

function toggleBlock(oid){
    var imageState = imgSwap(oid);
    toggleDisplay(oid);         
}

function showCatContent(oid) {
	var myAjax = new Ajax.Updater(oid, '/include/cat'+oid+'.html', { method: 'get' });
}
