//CHECKJS  C:\temp\sudoku\useraction.js 11/6/2005 12:30:11 PM
//useraction.js hansonr@stolaf.edu 7:00 AM 10/28/2005
// handles all user interaction



pageTitle = "Sudoku Analyst"

iReportChains = 0
iShowAnswers = 0
ishowdetail = 0
enableUserEntry = 0

MAXSOLVERSTEPS=1000

function doInit(){
	doInit2()
}

function doInit2(s){
	document.getElementById("info").reset()
	var ii=0
	var st=""
	var S=new Array()
	var docheck = (!!s)
	if (!s)s=document.location.search
	s=unescape(s.substring(1,s.length))
	var is3d=false
	s=s.replace(/MODE\=3D/,"")
        var showonly = 0;
        for (var i = 1; i <= 9; i++)if(s.indexOf("SHOWONLY=" + i)>=0) {
          showonly = i;break;
        }
	var T=new Array()
	if(s.indexOf("COLORONLY")>=0){
		s=s.replace(/COLORONLY/g,"")
	}
	if(s.indexOf("ALSX")>=0){
		s=s.replace(/ALSX/g,"")
		igetalmostlockedx=true
	}
	if(s.indexOf("ALSY")>=0){
		s=s.replace(/ALSY/g,"")
		igetalmostlockedy=true
	}
	if(s.indexOf("ALS")>=0){
		s=s.replace(/ALS/g,"")
		igetalmostlockedx=true
		igetalmostlockedy=true
	}

	if(s.indexOf("YCYCLESONLY")>=0){
		s=s.replace(/YCYCLESONLY/g,"")
	}
	
	if(s.indexOf("CHECK=1")>=0){
		s=s.replace(/CHECK=1/g,"")
		docheck = 1
	}
	if(s.indexOf("DEMO=")>=0){
		showDemo(s.split("DEMO=")[1].split("&")[0])
	}else if(s.indexOf("LOAD=")>=0){
		s=s.split("LOAD=")[1].split("&")[0]
		if(s.indexOf("top")==0 || s.indexOf("impossible")==0){
			s=s.replace(/\./,".htm?")
			loadData(s)
			return
		}
		if(s.indexOf(",")>=0){
			doLoad(s)
		}else{
			doLoadBlock(0,s)
		}
	}else if(s.indexOf("MARKS=")>=0){
		loadMarks(getField(s, "MARKS"))
	}else{
		s=s.replace(/t/g,",,,")
		if(s=="")s=DEFAULTPUZZLE
		if(s.indexOf(",")<0)s=toAssistantMode(s)


		LinearData=s.split(",")
		setData()

		if(LinearData.length>1){
			getNextStep(1,0,1)
		}else{
			doClear()
		}
	}
	if(docheck)doCheck(0)
}window.onload=doInit


function doLearn(ishow){
	if(!ishow){
		document.getElementById("learn").style.display="none"
		document.getElementById("credits").style.display="block"
		document.getElementById("sudoku").style.display="block"
		return
	}
	document.getElementById("numberblock").style.display="none"
	document.getElementById("selectspan").style.display="none"
	document.getElementById("jmol").style.display="none"
	document.getElementById("sudoku").style.display="none"
	document.getElementById("learn").style.display="block"
	document.getElementById("credits").style.display="none"
	document.getElementById("chains").style.display="none"
}

function doLoad(s,docheck){
	if (thisviewmode=="input")docheck=0
	thisanalysis=s
	clearDivs()
	if(docheck)doLearn(0)
	isdemo=0
	isjmolreset=1
	thisslicen=0
	thisr_or_c=0
	if (s.indexOf("MARKS=")==0) {
		loadMarks(s.substring(6))
		
	} else {
		LinearData=s.replace(/t/g,",,,").split(",")
		setData()
	}
	lastdisplay=""
	getNextStep(1,0,1)
	if(docheck && !checkSavedAnalysis("doload" + docheck + "::"+s))doCheck(0)
}

function doLoadBlock(issolution,sblock,docheck){
	if (thisviewmode=="input")docheck=0
	thisanalysis=sblock
	var s=""
	var sout=""
	var isnew=!sblock
	var n = thisslicen
	isjmolreset=1
	thisslicen=0
	thisr_or_c=0
	if(issolution==-2){ //number block
		showDataBlock()
		document.getElementById("numberblock").style.display="block"
		document.getElementById("selectspan").style.display="block"
		document.getElementById("sudoku").style.display="none"
		document.getElementById("jmol").style.display="none"
		document.getElementById("learn").style.display="none"
		document.getElementById("chains").style.display="none"
		thisviewmode="input"
		return
	}else if(issolution==-1){ //standard view
		if(thisviewmode=="sudoku")doShow(0)
		document.getElementById("numberblock").style.display="none"
		document.getElementById("selectspan").style.display="none"
		document.getElementById("jmol").style.display="none"
		document.getElementById("sudoku").style.display="block"
		document.getElementById("learn").style.display="none"
		document.getElementById("chains").style.display="none"
		if(thisviewmode=="jmol" && thisslicen){
			thisviewmode="sudoku"	
			doShow(thisslicen>=0?thisslicen:0)
		}
		thisviewmode="sudoku"
		return
	}else if(issolution==-3){ //chain view
		greenlist="NOSHOW"
		redlist=redlistk=yellowlist=""
		if(thisviewmode=="sudoku"){
			doShow(0)
			if(n>0)doShow(n)
		}else if(thisviewmode=="jmol"){
			thisslicen=0
			thisr_or_c=0
			isjmolreset=0
			dorotatemodel=0
			create3DModel()
			return
		}else if(thisviewmode=="input"){
			doLoadBlock(-1)
		}
		if(ishowchain==0)return
		document.title=s="Chain "+ishowchain+": "+getChainListing(ishowchain)
		logMessage(s+"<br />",0,1)
		return
	}
	if(isnew)prevBlock=sblock=document.getElementById("blockinput").value
	thisanalysis=sblock
	if(sblock.indexOf("[Puzzle]")==0){
		sout=readSDKdata(sblock)
		doLoadBlock(-1)
		showMarks(true)
		loadMarks(sout)
		doLoadBlock(-1)
		if (docheck)doCheck(0)
		return
	}
	if("\n\r".indexOf(sblock.charAt(0))>=0)sblock=sblock.substring(1,sblock.length)
	if(issolution){
		s=sblock.replace(/\D/g,"")
		if(s.length!=81){
			alert(s.length+" is not the 9x9 numbers required for a solution:" +s)
			return
		}
	}
	sout=toAssistantMode(sblock,issolution)
	if(sout.indexOf("[")==0){
		doLoadBlock(-1)
		showMarks(true)
		loadMarks(sout)
	}else{
		doLoad(sout)
	}
	if(isnew)doLoadBlock(-1)
	if(docheck && !checkSavedAnalysis("doloadblock" + issolution + " sblock:"+sblock))doCheck(0)
}

function doCheck(andAnswer){
	ishowdetail = andAnswer
	doSolve()
}

function doStep(iscontinuation,isuserclick){
	lastmsg=""
	if(nsteps==0 && solving && !ihavechecked){
		getNextStep(0,0,1)
		solving=1
	}
	if(FullMsgs.length==0)ref0=getRef(1,0,1)
	nsteps++
	var n=updateAllowed(iscontinuation)
	if(isuserclick){
		isautorun=0
		AutoMsgs = []
		logMessage("",1)
		addAutoMsg("")
	}
	if(n && !solving){
		greenlist=bluelist=yellowlist=redlist=redlistk=""
		createTable(0)
	}else{
		if(!solving && !ihavechecked){
			doHint(1)
		}else{
			//			testchain=ishowchain
			saveRef()
			ishowchain=0
			getNextStep(1,iscontinuation)
		}
	}
	if(isuserclick)document.title=lastmsg
	if(solving)setTimeout("doStep(1)",10)
}

function doClick(i,j,n){
}

function doGoFast() {
	return 1
}

function setSudoku(s) {
	document.getElementById("sudoku").innerHTML=s
}

function getSudokuValue(i, j) {
	return document.getElementById("cell"+i+j).value
}

function setSelectBox(s) {
	document.getElementById("selectspan").innerHTML=s
}

function showDataBlock(){
	document.getElementById("blockinput").value=getRef(1)
}

function showRef(){
	theref=getRef(1,NUMBEST)
	if(theref.replace(/\,/g,"")=="" && theref.indexOf("0")<0){
		theref=""
	}else{
		while(theref.charAt(theref.length-1)==",")theref=theref.substring(0,theref.length-1)
		theref="?"+theref
	}
	theref="index.htm"+theref
	document.getElementById("savelink").href=theref.replace(/,,,/g,"t")
}

function checkOptions(){

	icheckcross=1
	icheckranges=1
	ichecksubs=1

	icheckgrids=1

	icheckmedusa=1
	icheckweaklinks=1
	icheckhinge=0
	ishowtimings=0
	icheckmagic=0


	icheckforwardlogic=1
	icheckreverselogic=0
	if(icheckreverselogic||icheckforwardlogic)icheckweaklinks=1

	isedgesonly=0
	idostronggroups=0
	ijustcolor=0
	isjustycycles=0
	ichecklogicsubsets=1
	ichecklockedcandidates=1
 	idolog=1

	iclearlog=1

}

function isDone(wasSolving) {
 if(wasSolving) {
	clearData()
	if(!ishowdetail){
		document.getElementById("stepselect").selectedIndex=1
		loadSnapShot(1)
	}
 } else {
	 showRef()
 }
}

function getNSelect() {
	var s="<select id='nselect' style='width:80px' onkeyup=\"setTimeout('doShow('+value+')',100)\" onchange=doShow(parseInt(value))><option value='-1'>slices 1-9</option>"
	var n=0
	for(var i=0; i < 9; i++){
		s+="<option value='"+(i+1)+"'>Slice "+(i+1)+"</option>"
	}
	s += "</select> <a href=javascript:doShow(-1)>off</a> "
	return s
}

function clearDivs(){
	thisLogMessage = ""
	ishowchain=0
	ishowchain2=0
	thisslicen=0
	document.getElementById("chains").innerHTML=""
	document.getElementById("chains").style.display="block"
	SnapShot = new Array()
	StrongChains=[]
	AlsChains = []
	showStepSelect("XX")
	showChainSelect()
}

thisanalysis=""

function doShowAnalysis(){
	var s=document.getElementById("logmsg").innerHTML
	if(s.indexOf("||")>=0 || thisanalysis=="" || s=="")return
	s=s.replace(/\n/g,"<br \>")
	s='<div id="'+thisanalysis+'" style="display:none">'
	+s 
	+ "||" + document.getElementById("stepselectspan").innerHTML
	+ "||" + SnapShot.join("/")+"<br \>"
	+"</div>"
	document.getElementById("logmsg").innerHTML=s.replace(/\</g,"&lt;").replace(/selected/g,"xselected")
}

function checkSavedAnalysis(why) {
	var d=document.getElementById(thisanalysis)
	if(!d)return false
	d = d.innerHTML.split("||")
	document.getElementById("logmsg").innerHTML = d[0]
	document.getElementById("stepselectspan").innerHTML = d[1]
	SnapShot=d[2].split("/")
	for (var i=0;i<SnapShot.length;i++){
		var S=SnapShot[i].split(",")
		for(var j=0;j<S.length;j++)if(S[j]!="")S[j]=parseInt(S[j])
		SnapShot[i]=S
	}
	return true
}

