// total number of fortunes
var fortunesnum = 8493;
// fortunes array
var fa = new Array();
var fidx;


function getValue(varname) {
  var url = window.location.href;
  var qparts = url.split("?");
  if (qparts.length < 2) {
    return "";
  }

  var query = qparts[1];
  var vars = query.split("&");
  var value = "";

  for (i=0;i<vars.length;i++) {
    var parts = vars[i].split("=");

    if (parts[0] == varname) {
      value = parts[1];
      break;
    }
  }

  value = unescape(value);
  value.replace(/\+/g," ");

  return value;
} // end function getValue


function showev () {
	var evobj = document.getElementById('evdiv');

	if (evobj.style.display == 'none') {
		evobj.style.display='block';
	} else {
		evobj.style.display='none';
	}

} // end function showev


function parseIt(fnum) { // replace bar (|) with <br>
	var fstr = fa[fnum];
	var re = /\|/g;

	var str = fstr.replace(re, "<br>");
	var cstr = fstr.replace(re, " ");

	document.all.fortnum.value = fnum;

	document.all.fortdiv.innerHTML = str;

	if (parseInt(fnum) == 1) {
		document.fortform.lastfort.disabled = 1;
	} else {
		document.fortform.lastfort.disabled = 0;
	}

	if (parseInt(fnum) == (fortunesnum)) {
		document.fortform.nextfort.disabled = 1;
	} else {
		document.fortform.nextfort.disabled = 0;
	}

	// setBgImg(cstr);
	setBgImg(fnum);
} // end function parseIt


function setBgImg(fnum) {
	document.body.style.backgroundImage = 'url("http://www.kurtmyles.com/bgimg.php?fortnum=' + fnum + '")';
} // end function setBgImg


function makeRandomNum(size) { // generates psuedo random number
    var myrand = Math.random();
    var mynum = Math.round(myrand*size);

	callFortune(mynum);
};

function showFortuneNumber(fnum, n) { // show specific fortune
	if (!e) var e = window.event;
	// alert("ctrl:" + e.ctrlKey);
	if (e.ctrlKey) {
		// alert("used control");
		// status="used control";
		// document.all.evdiv.style.visibility="visible";
		showev();
	}
	num = parseInt(fnum);
	num += parseInt(n);

	// lets stay above zero
	if (! num) {
		num = 1;
	}

	// but not above the total number of fortunes
	if (num > fortunesnum) {
		num = fortunesnum;
	}

	callFortune(num);
};

function checkthekey (fnum) {
	//alert("key was " + event.keyCode);
	if (event.keyCode == 13) {
		showFortuneNumber(fnum, 0);
		event.keyCode = null;
	}
};

function callFortune (fnum) {
	var fortpre = "f_000";
	// see if we already have this fortune
	if (typeof fa[fnum] == 'undefined') {
		// sus filename by number
		var catnum = String(parseInt((fnum - 1)/ 100));
		var catname = fortpre.substr(0, fortpre.length - catnum.length) + catnum;
		// load in appropriate fortunes file
		getMoreFortunes(fnum, catname);
	} else {
		// just show the fortune
		parseIt(fnum);
	}

} // end function callFortune

function getMoreFortunes(fnum, catname) {
  var srcfile = "fortunes/" + catname;
  // Attempt to create an AJAX object
  var xmlHttp;
  try{xmlHttp=new XMLHttpRequest();}
  catch(e){
    try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
    catch(e){
      try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
      catch(e){
        // Ajax not supported, use DOM instead.
        alert('No AJAX capability!\n\nNO fortune for YOU!!!');
      }
    }
  }

  if(xmlHttp){
	// document.all.fortdiv.innerHTML = '<font color=\"green\">Loading...' + catname + '</font>';
	document.all.fortdiv.innerHTML = '<font color=\"green\">Loading Your Fortune... </font>';
    xmlHttp.open("GET",srcfile,true);
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4){
        // load more fortunes into the array
        eval(xmlHttp.responseText);
        // show the fortune
        parseIt(fnum);
      }
    }
    xmlHttp.send(null);
  }
} // end function getMoreFortunes


// makeRandomNum(fortunesnum);


