var testmt = cgi.testmt;
var book = cgi.bk;
var yaku = cgi.yaku;

//  FUNCTIONS

// SET DISABLE BOOKS WHEN LOADING

/*
window.onload = function(){
	for (var i = 0; i < (document.forms.findout.bk || document.forms.namae2.bk).options.length; i++) {
		(document.forms.findout.bk || document.forms.namae2.bk).options[i].disabled = !cpt[yaku][i - -cgi.testmt];
	}
	
}
*/

//  GET FORM INPUTS OF BIBLE SEARCH

var loading;	//NOW FILE LOADING (NEW LOADING OR PUSHED A SUBMIT BUTTON)

//  TESTAMENT OR YAKU CHANGED, SET BOOK

function tstmChange (theForm, tt, yk) {		//TESTMT MUST BE 0 OR 39
	if (tt != '') testmt = tt - 0;
	if (tt == '') yaku = yk - 0;
	var theLength = bk[yaku][testmt].length;
	
	setOptionLength (theForm.bk, theLength);
	
	for (var i = 0; i < theLength; i++) {
		theForm.bk.options[i].text = bk[yaku][testmt][i];
		theForm.bk.options[i].value = i - -testmt;
		theForm.bk.options[i].disabled = !cpt[yaku][i - -testmt];
	}
	
	selectOption (theForm.bk, (loading) ? cgi['bk'] % 39 : 0);
	bkChange (theForm.bk);
}


//  BOOK CHANGED --- SET CHAPTERS

function bkChange (theInput) {
	book = theInput.selectedIndex - -testmt;
	var chap;
	var toSet = 0;
	var setTo = 0;
	theInput.form.prgSubmit.value = submValue[!cpt[yaku][book] - 0];
	if (!cpt[yaku][book]) {
		setOptionLength (theInput.form.cpt, 1);
		setOptionLength (theInput.form.beginsAt, 1);
		setOptionLength (theInput.form.endsAt, 1);
		theInput.form.cpt.options[0].text = '---';
		theInput.form.beginsAt.options[0].text = '---';
		theInput.form.endsAt.options[0].text = '---';
		return;
	}
	
	var theLength = (cpt[yaku][book]) ? cpt[yaku][book].length / 3 : 0;	//NUMBER OF OPTIONS
	setOptionLength (theInput.form.cpt, theLength);
	for (var i = 0; i < theLength; i++) {
		chap = cpt[yaku][book].substring(i * 3, i * 3 + 3) - 0;
		theInput.form.cpt.options[i].text = (chap > 0) ? chap : '';
	}
	selectOption (theInput.form.cpt, 0);
	
	cptChange (theInput.form.cpt);
}

//	CHAPTER CHANGED --- SET PARAGRAPHS
function cptChange (theInput) {
	
	var cpt = theInput[theInput.selectedIndex].text - 0;
	var bkcpt = book * 1000 - -cpt;
	var parag;

	// SET BEGINING
	var toSet = 0;
	var setTo = 0;
	var theLength = bgP[yaku][bkcpt].length / 3;	//NUMBER OF OPTIONS
	setOptionLength (theInput.form.beginsAt, theLength);
	for (var i = 0; i < theLength; i++) {
		theInput.form.beginsAt.options[i].text = parag = bgP[yaku][bkcpt].substring(i * 3, i * 3 + 3) - 0;
	}
	selectOption (theInput.form.beginsAt, 0);


	// SET END
	toSet = 0;
	setTo = 0;
	theLength = edP[yaku][bkcpt].length / 3;	//NUMBER OF OPTIONS
	setOptionLength (theInput.form.endsAt, theLength);
	for (i = 0; i < theLength; i++) {
		theInput.form.endsAt.options[i].text = parag = edP[yaku][bkcpt].substring(i * 3, i * 3 + 3) - 0;
	}
	selectOption (theInput.form.endsAt, 0);

	
}

// BEGINING CHANGED -- SET END
function bgChange (theInput) {
	var bgValue = theInput[theInput.selectedIndex].text - 0;
	var target = theInput.form.endsAt;
	for (var i = target.selectedIndex; target[i].text - 0 < bgValue; i++) {
		;
	}
	selectOption (target, i);
}

// END CHANGED -- SET BEGINING
function edChange (theInput) {
	var edValue = theInput[theInput.selectedIndex].text - 0;
	var target = theInput.form.beginsAt;
	for (var i = target.selectedIndex; target[i].text - 0 > edValue; i--) {
		;
	}
	selectOption (target, i);

}

//  REMOVE ALL OPTIONS
function setOptionLength (sel, lengthToSet) {
	sel.options.length = 0;
	sel.options.length = lengthToSet;
}

//  SELECT OPTION

function selectOption (sel, opt) {
	if (window.opera) {
		sel.options[opt].focus();
		sel.options[opt].selected = true;
	}
	else {
		sel.selectedIndex = opt;
	}
}
