// Copyright Queensland Legislative Assembly

var glob_alldb_idx=0; // a flag cum value for 'All Years' tick box

glob_reset_pressed = false;
glob_current_sec = 0;      // 1=ExactPhrase, 2=Menu, 3=Expert

//NOTE: use checkboxes for years ONLY; code presumes that all checkboxes are associated with years

//************ Begin - Year Tick Check stuff ****************


// caveat: forms[0] is hardwired; so dont put a from before the existing one
// When a page is revisited, variable state is lost, so re-establish it

function DoLoad() {

	var element_cnt;	
	var loop_cnt=0;

	element_cnt = document.forms[0].elements.length;
	for (; loop_cnt < element_cnt ; loop_cnt++) {
		if ((document.forms[0].elements[loop_cnt].type == "checkbox") && (document.forms[0].elements[loop_cnt].alt == "alldb"))
			if (document.forms[0].elements[loop_cnt].checked == true)
				glob_alldb_idx = loop_cnt;
	}

	//when returning to this Form, restore state
	if (document.getElementById("ExactPhrase").checked == true) {
		ExactInput();
	} else if (document.getElementById("Menu").checked == true) {
		MenuInput();
	}  else if (document.getElementById("Expert").checked == true) {
		ExpertInput();
	} else {
		ExactInput();
	}

	if (document.getElementById("IW_AUTOCONFLATE").checked)
		document.getElementById("IW_AUTOCONFLATE").value = "1";
	else
		document.getElementById("IW_AUTOCONFLATE").value = "0";	

	ReFocusInput();	
	
}

// User has ticked 'All Years', tick off all the individual years
// and remember what array element 'All Years' tick box is

function AllSelected(ClickState,form) {

	var element_cnt;	
	var loop_cnt=0;
	
	//save the user a mouse click to go back to input field
	ReFocusInput();	

	if (ClickState == true) {
		element_cnt = form.elements.length;
		for (; loop_cnt < element_cnt ; loop_cnt++) {
			if ((form.elements[loop_cnt].type == "checkbox") && (document.forms[0].elements[loop_cnt].name != "IW_AUTOCONFLATE")){
				if (form.elements[loop_cnt].alt != "alldb")
					form.elements[loop_cnt].checked = false;
				else
					glob_alldb_idx = loop_cnt;
			}			
		}
	} else
		glob_alldb_idx = 0;			
}

//on first individual year selection, 'All Years' tick may need extinguishing

function YearSelected(form) {

	//save the user a mouse click to go back to input field
	ReFocusInput();

	if (glob_alldb_idx != 0){
		form.elements[glob_alldb_idx].checked = false;
		glob_alldb_idx = 0;
	}			
}

//************ End - Year Tick Check stuff ****************

//************ Begin - Radio Button manipulation and utilities ******

//Respond to radio button clicks by enabling/disabling fields and field colours
//Rename fields so that Isys engine finds the stuff where it expects
function rad_pick(search_type) {

	if (search_type == "exat") {	
		ExactInput();
	} else if (search_type == "menu") {	
		MenuInput();
	} else if (search_type == "expert") {	
		ExpertInput();
	}
	ReFocusInput();			
}


//Functions to set various input options

function ExpertInput() {
	AllOff();
	document.getElementById("Expert").checked = true;
	glob_current_sec = 3;
	document.getElementById("SUBMIT_STRING").name='IW_FIELD_TEXT';

	document.getElementById("ExpertInput").disabled = false;
	document.getElementById("OKExpert").disabled = false;
	document.getElementById("OKExpert").src =  "/images/search.gif";

	document.getElementById("ExpertText").style.color = 'black';
	document.getElementById("ExpertInput").style.backgroundColor = '';

	//document.getElementById("DivExpert").style.color = 'black';
	//document.getElementById("DivExpert").style.backgroundColor = 'white';
}

function MenuInput() {
	AllOff();
	document.getElementById("Menu").checked = true;
	glob_current_sec = 2;

	document.getElementById("SUBMIT_STRING").name='TEMP_NAME';

	document.getElementById("IW_FIELD_ADVANCE_AND").disabled = false;
	document.getElementById("IW_FIELD_ADVANCE_OR").disabled = false;
	document.getElementById("IW_FIELD_ADVANCE_NOT").disabled = false;
	document.getElementById("OKMenu").disabled = false;
	document.getElementById("OKMenu").src =  "/images/search.gif";

	document.getElementById("MenuText").style.color = 'black';
	document.getElementById("AndText").style.color = 'black';
	document.getElementById("OrText").style.color = 'black';
	document.getElementById("NotText").style.color = 'black';

	document.getElementById("IW_FIELD_ADVANCE_AND").style.backgroundColor = '';
	document.getElementById("IW_FIELD_ADVANCE_OR").style.backgroundColor = '';
	document.getElementById("IW_FIELD_ADVANCE_NOT").style.backgroundColor = '';
			
	//document.getElementById("DivMenu").style.color = 'black';
	//document.getElementById("DivMenu").style.backgroundColor = 'white';
}

function ExactInput() {
	AllOff();
	document.getElementById("ExactPhrase").checked = true;
	glob_current_sec = 1;

	document.getElementById("SUBMIT_STRING").name='IW_FIELD_ADVANCE_PHRASE';


	document.getElementById("ExactPhraseInput").disabled = false;
	document.getElementById("OKExactPhrase").disabled = false;
	document.getElementById("OKExactPhrase").src =  "/images/search.gif";

	document.getElementById("ExactPhraseText").style.color = 'black';

	document.getElementById("ExactPhraseInput").style.backgroundColor = '';		

	//document.getElementById("DivExactPhrase").style.color = 'black';
	//document.getElementById("DivExactPhrase").style.backgroundColor = 'white';
}

//manipulate conflation radio button
function rad_conflate() {

	if (document.getElementById("IW_AUTOCONFLATE").value == "0"){
		document.getElementById("IW_AUTOCONFLATE").checked = true;
		document.getElementById("IW_AUTOCONFLATE").value = "1";
	}else{
		document.getElementById("IW_AUTOCONFLATE").checked = false;
		document.getElementById("IW_AUTOCONFLATE").value = "0";
	}
}

//copy field contents so that Isys engine finds the stuff where it expects
function prepare_field() {

	switch(glob_current_sec) {
	
		case 1: { // Exact Phrase
			document.getElementById("SUBMIT_STRING").value = document.getElementById("ExactPhraseInput").value;
			break;				
		}
		case 2: { // Menu Search
			break;		
		}
		case 3: { // Expert Search
			document.getElementById("SUBMIT_STRING").value = document.getElementById("ExpertInput").value;
			break;	
		}		
		default: {
			document.getElementById("SUBMIT_STRING").value="";			
			break;
		}
	}

}


//utility which marks all fields as disabled
function AllOff() {
	document.getElementById("ExactPhrase").checked = false;
	document.getElementById("Menu").checked = false;
	document.getElementById("Expert").checked = false;

	document.getElementById("ExactPhraseText").style.color = 'black';
	document.getElementById("MenuText").style.color = 'black';
	document.getElementById("ExpertText").style.color = 'black';
	document.getElementById("AndText").style.color = 'silver';
	document.getElementById("OrText").style.color = 'silver';
	document.getElementById("NotText").style.color = 'silver';

	document.getElementById("ExactPhraseInput").disabled = true;
	document.getElementById("IW_FIELD_ADVANCE_AND").disabled = true;
	document.getElementById("IW_FIELD_ADVANCE_OR").disabled = true;
	document.getElementById("IW_FIELD_ADVANCE_NOT").disabled = true;
	document.getElementById("ExpertInput").disabled = true;

	document.getElementById("ExactPhraseInput").style.backgroundColor = 'E8E8E8';
	document.getElementById("IW_FIELD_ADVANCE_AND").style.backgroundColor = 'E8E8E8';
	document.getElementById("IW_FIELD_ADVANCE_OR").style.backgroundColor = 'E8E8E8';
	document.getElementById("IW_FIELD_ADVANCE_NOT").style.backgroundColor = 'E8E8E8';
	document.getElementById("ExpertInput").style.backgroundColor = 'E8E8E8';

	document.getElementById("OKExactPhrase").disabled = true;
	document.getElementById("OKMenu").disabled = true;
	document.getElementById("OKExpert").disabled = true;

	document.getElementById("OKExactPhrase").src = "/images/searchGR.gif";
	document.getElementById("OKMenu").src = "/images/searchGR.gif";
	document.getElementById("OKExpert").src = "/images/searchGR.gif";

	//document.getElementById("DivExactPhrase").style.color = 'gray';
	//document.getElementById("DivMenu").style.color = 'gray';
	//document.getElementById("DivExpert").style.color = 'gray';

	//document.getElementById("DivExactPhrase").style.backgroundColor = 'silver';
	//document.getElementById("DivMenu").style.backgroundColor = 'silver';
	//document.getElementById("DivExpert").style.backgroundColor = 'silver';
}

//utility that restores cursor to current selection
function ReFocusInput() {
	//save the user a mouse click to go back to input field
	switch(glob_current_sec) {
	
		case 1: { // Exact Phrase
			document.forms[0].ExactPhraseInput.focus();
			break;				
		}
		case 2: { // Menu Search
			document.forms[0].IW_FIELD_ADVANCE_AND.focus();
			break;		
		}
		case 3: { // Expert Search
			document.forms[0].ExpertInput.focus();
			break;	
		}		
		default: {			
			break;
		}
	}
}

//************ End - Radio Btton manipulation and utilities ******

//************ Begin - Validation Stuff *******************

//Trap leading spaces
function trapSpacesString(evt,ActiveInputField) {

	evt = (evt) ? evt : (window.event) ? window.event : "";
	var key = (evt.keyCode) ? evt.keyCode : (evt.which) ? evt.which : "";
	
	if (evt) {
		if ((key == 32) && (ActiveInputField.value == ""))
			return false;
	}
	return true;
}	

// Just a check to see that no search is launched without year and input selection
function YearIsSelected() {

	var element_cnt;	
	var loop_cnt=0;

	element_cnt = document.forms[0].elements.length;
	for (; loop_cnt < element_cnt ; loop_cnt++) {
		if ((document.forms[0].elements[loop_cnt].type == "checkbox")
		 && (document.forms[0].elements[loop_cnt].checked == true)
		 && (document.forms[0].elements[loop_cnt].name != "IW_AUTOCONFLATE"))
			return true;
		}
	return false;			
}

// Validate and then submit
function ValidateSearch() {

	//save the user a mouse click to go back to input field
	ReFocusInput();

	//if (glob_reset_pressed) {
	//	glob_reset_pressed = false;
	//	return true;
	//}
	
	if (YearIsSelected()==false){
		alert("At least one year must be selected");
		return false;
	}
	
	if ((glob_current_sec == 1) || (glob_current_sec == 3)){
		if (document.getElementById("SUBMIT_STRING").value == ""){
			alert("No search word or string is entered");
			return false;
		}
	} else if ((document.getElementById("IW_FIELD_ADVANCE_AND").value == "") && (document.getElementById("IW_FIELD_ADVANCE_OR").value == "") && (document.getElementById("IW_FIELD_ADVANCE_NOT").value == "")){
			alert("No search word or string is entered");
			return false;
		}	
				
	return true;	
}

//************ End - Validation Stuff *******************
