answerpos=0;
window.selectedLetter="";
window.thecrypt="";
window.thesolution="";
window.thechoices="";
window.usedsource="";
window.useddest="";
window.focusPoint=0;
A='AaZz';
window.asciA=A.charCodeAt(0);
window.ascia=A.charCodeAt(1);
window.asciZ=A.charCodeAt(2);
window.asciz=A.charCodeAt(3);
goodletters='abcdefghijklmnopqrstuvwxyz';
aDest=new Array();
for(i=0;i<26;i++) aDest[i]='';
aTheQuote=new Array();
aTheSolution=new Array();
function hint(){
	answerpos=0;
	setSelectedLetter('');
	setSelectedChoice('');
	var i=document.forms["crypto"].quote.selectedIndex;
  	if(i>=0){
		i=document.forms["crypto"].quote.options[i].value;
		answerSource=aQ[i].quote;
	}
	else answerSource=window.seedcrypt;
	chAnswer=answerSource.charAt(answerpos).toLowerCase();
	while((goodletters.indexOf(chAnswer)==-1  || window.useddest.toLowerCase().indexOf(chAnswer)!=-1) && answerpos <answerSource.length){
		self.status+=chAnswer;
		answerpos++;
		chAnswer=answerSource.charAt(answerpos).toLowerCase();
	}
	if(answerpos==answerSource.length)answerpos=0;			
	removeLetter(aTheSolution[answerpos]);
	chSo=window.orig.charAt(answerpos);
	document.getElementById("quotation").innerHTML=chSo +"="+chAnswer.toUpperCase();
	//alert(chSo +"="+chAnswer);
	setSelectedLetter(chSo.toUpperCase());
	setSelectedChoice(chAnswer.toUpperCase().charAt(0));
	//answerpos++;
	//reBuild();
}
function moveLetter(){
  	if(window.selectedLetter=="") return;
	if(window.selectedChoice=="") return;
	self.status='Replacing ' + window.selectedLetter+ ' with '+window.selectedChoice;
	so=window.selectedLetter;
	window.usedsource+=so;
	de=window.selectedChoice;
	thepos=so.toUpperCase().charCodeAt(0)-window.asciA;
	removeLetter(aDest[thepos]);
	aDest[thepos]=de;
	window.useddest+=de;
	setSelectedLetter('');
	setSelectedChoice('');
//	self.status=window.useddest;
	checkDone();
	reBuild();
	//self.status=outst;
}
function removeLetter(so){
	if(so.length >1 ||so==' ' || so=='&nbsp;'|| so=='')return;
	setSelectedLetter('');
	setSelectedChoice('');
	so=so.toLowerCase();
	q=window.useddest.toLowerCase().indexOf(so);
	window.useddest=window.useddest.substr(0,q)  +window.useddest.substr(q+1);
	self.status='Removing '+so;
	for(i=0;i<aDest.length;i++){
		if(aDest[i].toLowerCase()==so.toLowerCase()){
			aDest[i]=''
			break;
		}
	}
	setSelectedChoice('');
	setSelectedLetter('');
	reBuild();
}
function fixCode(){
	var i;
	i=document.forms["crypto"].quote.selectedIndex;
	if(i>=0)i=document.forms["crypto"].quote.options[i].value;
	j=0;
	answerpos=0;
	window.useddest="";
	window.selectedLetter="";
	window.selectedChoice="";
	aTheSolution=new Array();
	aTheQuote=new Array();
	if(i==-1)	{
		st=window.seedenc;
		stAnswer=window.seedcrypt;
	}
	else {
	if(window.showAuthor){
		st=aQ[i].encquote +'--'+aQ[i].encauthor;
	}
	else{
		st=aQ[i].encquote;
	}
	stAnswer="Click 'Solution' to see answer.";
}
	for(j=0;j<st.length;j++){
		aTheQuote[j]=st.charAt(j);
	 	 aTheSolution[j]='';
	}	

		window.orig=st;
		document.getElementById("quotation").innerHTML=stAnswer;
	
	aDest=new Array();
	for(i=0;i<26;i++) aDest[i]='';
	window.usedsource="";
	window.useddest="";
	//alert(st);
	reBuild();
	}
function giveUp(){
var i;
	i=document.forms["crypto"].quote.selectedIndex;
  if(i>=0){
			i=document.forms["crypto"].quote.options[i].value;
		 document.getElementById("quotation").innerHTML=aQ[i].quote +'--'+aQ[i].author;
	}
	else{
		window.seedcrypt;
		 document.getElementById("quotation").innerHTML=st;
	}
	
	}
function checkDone(){
	var i=document.forms["crypto"].quote.selectedIndex;
	var solString=(aQ[i].quote +'--'+aQ[i].author).toUpperCase();
	var ans_String="";
	var ii=0;
	var correct=true;
	while(correct && ii<aTheQuote.length){
		chcode=aTheQuote[ii].toLowerCase().charCodeAt(0);
    	if(window.ascia <= chcode && chcode <= window.asciz){
			 aTheSolution[ii]=aDest[chcode-window.ascia];
			 
		}
		else {
				aTheSolution[ii]=aTheQuote[ii];
		}
		correct=(aTheSolution[ii].toUpperCase()==solString.substring(ii,ii+1));
		ii++;
	}
		if(correct){
		var holdclass=window.inputClass;
		window.inputClass="greencinput";
		document.getElementById("quotation").innerHTML="Correct. Great job.";
		reBuild();
		window.inputClass=holdclass;
	}
}
function setSelectedLetter(ch){
window.selectedLetter=ch;
document.getElementById("selectedletter").innerHTML=ch;
if(window.selectedLetter==''||window.selectedChoice=='')return;
moveLetter();

}
function setSelectedChoice(ch){
window.selectedChoice=ch;
document.getElementById("selectedchoice").innerHTML=ch;
if(window.selectedchoice==''||window.selectedChoice=='')return;
moveLetter();
}
function handleKeyUp( ob,ov,qv, focuspoint){
	//alert(ob);
	//alert(ov);
	//alert(qv);
	if(ob.value==ov){
		return;
	}
	var chcode=ob.value.toLowerCase().charCodeAt(0);
 	var validLetter=ob.value.length>0 &&  window.ascia <= chcode && chcode <= window.asciz;
	 if(validLetter && (window.useddest.toUpperCase().indexOf(ob.value.toUpperCase()) >= 0)) {// already used
	 	//alert("The letter " + ob.value+ " has already been used");
	 	document.getElementById("quotation").innerHTML="<span class='redc'>The letter " + ob.value+ " has already been used</span>";
	 	ob.value=ov;
	 	ob.focus();
	 	ob.select();
	 	return false;
	 }
	 else{
	 	document.getElementById("quotation").innerHTML="&nbsp;";
	 }
	 for(i=0;i<aTheQuote.length;i++){
	 	if(aTheQuote[i]==qv){
	 		document.getElementById("quote_"+i).value=ob.value.toUpperCase();
	 	};
	 	if(window.usedsource.indexOf(qv)<0) {
	 		window.usedsource+=qv;
	 	}
	 }
	 thepos=qv.toUpperCase().charCodeAt(0)-window.asciA;
	 if((thepos>=0) && (thepos <26)){
		aDest[thepos]=ob.value.toUpperCase();
	}
	if(ov.length>0){
	var pos=window.useddest.toUpperCase().indexOf(ov.toUpperCase());
	var holdstring=window.useddest;
	 if(pos>=0 ){
	 	holdstring=window.useddest.substr(0,pos);
	 	if(pos <window.useddest.length-1){
	   		holdstring+=window.useddest.substr(pos+1);
	  	}
	  window.useddest=holdstring;
	}
	}
	window.useddest+=ob.value.toLowerCase();
	document.getElementById("selectedchoice").innerHTML="";
	document.getElementById("selectedletter").innerHTML="";
	window.selectedLetter="";
	window.selectedChoice="";
	writeChoiceString();	 
	var start=focuspoint +1;
	var foundPoint=false;
	var newFocus=0;
	//get new focus point
	while( start<aTheQuote.length && ! foundPoint) {
		if(document.getElementById("quote_" +start) && document.getElementById("quote_" +start).value==''){
			newFocus=start;
			foundPoint=true;
		}
		start++;
	}
	if(!foundPoint) {    //start at begining
		start=0;
		while( start<focuspoint && ! foundPoint) {
			if(document.getElementById("quote_" +start) && document.getElementById("quote_" +start).value==' '){
				newFocus=start;
				foundPoint=true;
			}
			start++;
		}
	}
	window.focusPoint=newFocus;
	if(document.getElementById("quote_" +window.focusPoint)){
		document.getElementById("quote_" +window.focusPoint).focus();
		document.getElementById("quote_" +window.focusPoint).select();
	}
	//reBuild();
	checkDone();
}

function handleClick( ob,ov,qv, focuspoint){
	//alert(ob);
	//alert(ov);
	//alert(qv);
	if(ov.length<=0){
		return;
	}
 	for(var i=0;i<aTheQuote.length;i++){
 		if(aTheQuote[i].toUpperCase()==qv.toUpperCase()){
 	//		alert('erasing ' +i);
 			document.getElementById("quote_"+i).value='';
 		}
	 }
	if(ov.length>0){
	//	alert(ov);
		var pos=window.useddest.toUpperCase().indexOf(ov.toUpperCase());
		var holdstring=window.useddest;
 		if(pos>=0 ){
 			holdstring=window.useddest.substr(0,pos);
 			if(pos <window.useddest.length-1){
   				holdstring+=window.useddest.substr(pos+1);
  			}
  			window.useddest=holdstring;
		}
		for(i=0;i<aDest.length;i++){
		if(aDest[i].toLowerCase()==ov.toLowerCase()){
			aDest[i]='';
			break;
		}
	}
	}
	document.getElementById("selectedchoice").innerHTML="";
	document.getElementById("selectedletter").innerHTML="";
	window.selectedLetter="";
	window.selectedChoice="";
	writeChoiceString();	 
	checkDone();
}


function frequency(){
var aFreq=new Array();
var aLetter = new Array();
var chcode;
var validLetter;
for(i=0;i<aTheQuote.length;i++){
	if(aLetter[aTheQuote[i]]){
		aFreq[aLetter[aTheQuote[i]]]++;
	}
	else{
		chcode=aTheQuote[i].toLowerCase().charCodeAt(0);
  		validLetter=window.ascia <= chcode && chcode <= window.asciz;
		if (validLetter){
			aLetter[aTheQuote[i]]=aTheQuote[i];
			aFreq[aLetter[aTheQuote[i]]]=1;
		}
	}
}
var st="<table cellpadding=5 cellspacing=5 border=0 ><tr>";
for(i in aLetter){
	st+="<td ><span class='bluec'>" +aLetter[i]+"</span><br><span class='redc'>" +aFreq[aLetter[i]]+"</span></td>";
}
st+="</tr></table>";
document.getElementById("quotation").innerHTML=st;
}	


function setcss(){
	$(".redc,.bluec, .selected,.redcinput,.redcnoboxes ").css("font-size",$("#textSize").val()+"px");		 	
}
function reBuild(){
	
	//alert(window.inputClass);
	if(!window.inputClass){
		window.inputClass="redcinput";
	}
//	alert(window.breakLength);
	window.thecrypt="";
	sCode="<table cellpadding='1' cellspacing='0'>";
	readyToBreak=false;
	sT='<tr>';
	icount=-1;
	for(i=0;i<aTheQuote.length;i++){
		icount++;
		chcode=aTheQuote[i].toLowerCase().charCodeAt(0);
    if(window.ascia <= chcode && chcode <= window.asciz){
		 aTheSolution[i]=aDest[chcode-window.ascia];
			uselink=true;
		}
		else {
			uselink=false;
			aTheSolution[i]=aTheQuote[i];
		}
		//need intelligent wrapping
		if(aTheQuote[i]==" "){
			//we are at a possible breakpoint
			nextspace=findNextSpace(aTheQuote,i+1);
			//alert(nextspace);
			if(nextspace <0){
				nextspace=aTheQuote.length;
			}
			readyToBreak=(icount+(nextspace-i))>window.breakLength;
		//	alert(readyToBreak +" " +nextspace+" " + window.breaklength);
		}
		if(aTheQuote[i]==' ' && readyToBreak){
			//while(aTheQuote[i]==' ' && i < aTheQuote.length-1)i++ ; //skip spaces at start of line
			sT=sT+"</tr>";
			sCode+=sT
			sT='<tr>';
			icount=0;
			readyToBreak=false;
		}
		if(icount==0 && aTheQuote[i]==' '){
			while(aTheQuote[i]==' ' && i < aTheQuote.length-1)i++ ; //skip spaces at start of line
			i--;
		}
		else{
		//	if(uselink){
			//	sT+="<td align='center' valign='bottom' ><a href='#' onmouseover='self.status=";
			//			sT+='"'+ "Click to remove " + aTheSolution[i] + '."';
			//			sT+=";return true;' onmouseout='status="+'"";return true;'+"' onClick='removeLetter(";
			//			sT+='"'+aTheSolution[i]+'");return false;';
			//			sT+="'><span class='redc'>" +aTheSolution[i]+'</span></a><br>';
			//		}
			//else sT+="<td align='center' valign='bottom' ><span class='redc'>"+aTheSolution[i].charAt(0)+ "</span><br>";
			if(uselink){
				sT+='<td class="cryptopadded"  align="center" valign="bottom"><input tabindex="'+(i+1)+'" id="quote_'+i+'" type="text" class="' +window.inputClass+'" size="1" maxlength=1 value="';
				sT+= aTheSolution[i] +'"';
				sT+=" " +'onkeydown="this.select();" onFocus="this.select();oldvalue=this.value;;"' +' ';
				sT+= ' onkeyup="handleKeyUp(this,oldvalue,'+"'" +aTheQuote[i]+"',"+i +');" onclick="handleClick(this,oldvalue,'+"'" +aTheQuote[i]+"',"+i +');"><br>';
			}
			else{
				sT+="<td class='cryptopadded' valign='bottom' align='center' ><span class='redc'>"+aTheSolution[i]+ "</span><br>";
			}
			
		
		
		
			g=aTheQuote[i];
			if(g.charCodeAt(0)==32)	sT+='<span class="bluec">&nbsp;' +'</span></td>';
			else{
				if('a' <= g.toLowerCase() && g.toLowerCase()<='z')uselink=true;
				else uselink=false;
				if(!uselink)sT+='<span class="bluec">'+g +'</span></td>';
				else	sT+='<a tabindex="1000" href="#" onmouseout="self.status='+"'';return true;" +'" onmouseover="self.status=' +"'Click to select " +g +"';return true;" +'" onClick="setSelectedLetter(' +"'" + g + "');return false;"+'"><span class="bluec">'+g +"</span></a></td>";
			}
		}		
	}
	sT+="</tr>";
	sCode+=sT+"</table>";
	
	$('#thecrypt').html(sCode);
	writeChoiceString();
	setcss();
	if(document.getElementById("quote_" +window.focusPoint)){
		document.getElementById("quote_" +window.focusPoint).focus();
		document.getElementById("quote_" +window.focusPoint).select();
	}
}
function findNextSpace(aA,_start){
	var ns=-1;
	for (var _i=_start;_i<aA.length;_i++){
		if(aA[_i]==" "){
			ns=_i;
			break;
		}
	}
	return ns;
}
function writeChoiceString(){
	choicestring="<table><tr>";
	comparestring=window.useddest.toUpperCase();
	for(i=0;i<26;i++){
		ch=String.fromCharCode(window.asciA+i);
		if(comparestring.indexOf(ch.toUpperCase())<0){
			choicestring+="<td><a href='#' onmouseover='self.status="+'"Click to select '+ ch.toUpperCase()+'";return true;'+"' onmouseout='self.status=" +'""'+";return true;' onclick='setSelectedChoice("+'"'+ch+'")'+";return false;'";
			choicestring+="><span class='selected'>"+ch.toUpperCase()+"</span></a></td>";
		}
	}
	choicestring+="</tr></table>";		
	
	$('#mychoices').html(choicestring);
	}		
window.tid=null;
window.elapsedtime=0;
function timerStart()
{
if(window.tid) clearInterval(window.tid);
window.tid=null
window.r=new Date();
window.elapsedtime=0;
window.tid=setInterval("timerUpdate();",1000);
}
function timerResume(){
window.r=new Date();
 clearInterval(window.tid);
window.tid=null;
window.tid=setInterval("timerUpdate();",1000);
}
function timerUpdate(){
d=new Date();
d=d-window.r+window.elapsedtime;
d=Math.floor(d/1000);
m=Math.floor(d/60)+'';
s=(d%60)+'';
while(s.length<2)s='0'+s;
st="<div class='timerstyle'>"+"Elapsed - "+m+":" +s+"</div>";
if(document.all) document.all.clock.innerHTML=st;
else if (document.getElementById)document.getElementById("clock").innerHTML=st;
else if(document.layers){
	document.layers["clockwrapper"].document.layers["clock"].document.open();
	document.layers["clockwrapper"].document.layers["clock"].document.write(st);
	document.layers["clockwrapper"].document.layers["clock"].document.close();
}
}
function timerStop(){
 var d=new Date();
 if(window.tid)window.elapsedtime+=d-window.r;
 clearInterval(window.tid);
window.tid=null;
}

