// Spell checker stuff

var spell_current_word_index=0;
var cur_suggestion=0;
var spell_control_mqid=0;
var spell_use_custom_word=false;

function spell_submit(mqid, qnnr_id, list_module_question_id) {
	var x = get_object("spellcheck_"+mqid);
	x.value="on";
	if(list_module_question_id) {
		var list_action = get_object("module_question_list_action_"+list_module_question_id);
		list_action.value = "edit";
	}
	document.forms['qnnr'+qnnr_id].submit();
	
}

function spell_close() {
	var qb_spell_mainwin = get_object("qb_spell_mainwin");
	if(confirm("Are you sure you wish to cancel the spell check?")) {
		qb_spell_mainwin.style.display="none";	
		if(browser_type=="ie" && browser_ver!=5) {
			var qb_spell_clip_layer = get_object('qb_spell_clip_layer');
			qb_spell_clip_layer.style.display="none";
		}
	}
}

function spell_cancel() {
	var qb_spell_mainwin = get_object("qb_spell_mainwin");
	if(qb_spell_mainwin.style.display!="none") {
		if(confirm("Doing this will cancel the spell check. Are you sure?")) {
			qb_spell_mainwin.style.display="none";	
		if(browser_type=="ie" && browser_ver!=5) {
				var qb_spell_clip_layer = get_object('qb_spell_clip_layer');
				qb_spell_clip_layer.style.display="none";
			}
			return true;
		} else {
			return false;
		}
	}
}

function spell_update_lengths(offsetfrom, difference) {
	for(var i=offsetfrom;i<spell_word.length;i++) {
		spell_word_pos[i]=spell_word_pos[i]-difference;
	}
}

function spell_reselect() {
	var text_object = spell_get_object(spell_control_mqid);
	var qb_spell_word_custom = get_object("qb_spell_word_custom");
	var x = (document.activeElement) ? (document.activeElement.id=="qb_spell_word_custom") : false;
	if(x==false) {
		select_text(text_object, spell_word_pos[spell_current_word_index], spell_word[spell_current_word_index].length);
		text_object.focus();
	}
}

function spell_change_ie(ind, theObj) { // Select an item in the list (IE version to handle invocation stuff)
	this.ind=ind;
	this.theObj=theObj;
	var me=this;
	this.invoke=function () {
		spell_change()
	}
	
}
function spell_get_object(mqid) {
	var obj = get_object("module_question_text_"+mqid);
	if(!obj) {
		obj = get_object("module_question_list_entry_"+mqid);
	}
	return obj;
}
function spell_change() {
	if(cur_suggestion!=0 || spell_use_custom_word==true) {
		var text_object = spell_get_object(spell_control_mqid);
		var qb_spell_word_custom = get_object("qb_spell_word_custom");
		var replace_word = (spell_use_custom_word==true) ? qb_spell_word_custom.value : spell_word_suggestions[spell_current_word_index][cur_suggestion];
		select_text(text_object, spell_word_pos[spell_current_word_index], spell_word[spell_current_word_index].length, replace_word);
		var difference = spell_word[spell_current_word_index].length - replace_word.length;
		spell_word[spell_current_word_index]=replace_word;
		spell_update_lengths(spell_current_word_index+1, difference);
		spell_next();
	} else {
		alert("Please select a suggestion or type your own\ncorrection in before you change this word.");
	}
}

function spell_do_sel(theObj) { // Select the item in the list presented by theObj (contaner element)
	var x = theObj.parentNode;
	for(var j=0;j<x.childNodes.length;j++) if(x.childNodes[j].hasChildNodes()) x.childNodes[j].className='qb_spell_item';
	theObj.className='qb_spell_item_sel';
}

function spell_sel(ind, theObj) { // Select an item in the list
	cur_suggestion=ind;
	var qb_spell_word_custom = get_object("qb_spell_word_custom");
	qb_spell_word_custom.value = spell_word_suggestions[spell_current_word_index][cur_suggestion];
	spell_use_custom_word = false;
	spell_do_sel(theObj);
}

function spell_sel_ie(ind, theObj) { // Select an item in the list (IE version to handle invocation stuff)
	this.ind=ind;
	this.theObj=theObj;
	var me=this;
	this.invoke=function () {
		spell_sel(me.ind, me.theObj)
	}	
}

function spell_next() {
	var qb_spell_mainwin = get_object("qb_spell_mainwin");
	var qb_spell_clip_layer = get_object('qb_spell_clip_layer');
	if(spell_current_word_index<spell_word.length-1) {
		spell_current_word_index++;
		spell_check(spell_control_mqid);
	} else {
		alert("Spell check complete!");
		qb_spell_mainwin.style.display="none";
		if(qb_spell_clip_layer) qb_spell_clip_layer.style.display="none";
	}
}

function spell_check(module_question_id) {
	if(spell_word.length==0) {
		alert("No spelling errors found - spell check complete!");
		return true;
	}
	spell_control_mqid=module_question_id;
	cur_suggestion=0;
	spell_use_custom_word = false;
	var qb_spell_word_custom = get_object("qb_spell_word_custom");
	qb_spell_word_custom.value = "";
	var i=0;
	var qb_spell_mainwin = get_object("qb_spell_mainwin");
	var qb_spell_word = get_object("qb_spell_word");
	var qb_spell_suggestions = get_object("qb_spell_suggestions");

	var text_object = spell_get_object(spell_control_mqid);
	
	qb_spell_mainwin.style.zOrder="0";	
	qb_spell_mainwin.style.display="";	
	qb_spell_word.value=spell_word[spell_current_word_index];

	if(browser_type=="ie" && browser_ver!=5) {
		var qb_spell_clip_layer = get_object('qb_spell_clip_layer');
		qb_spell_clip_layer.style.zOrder="1";
		qb_spell_clip_layer.style.display="";
	}

	for(var j=qb_spell_suggestions.childNodes.length-1;j>=0;j--) {
		qb_spell_suggestions.removeChild(qb_spell_suggestions.childNodes[j]);
	}

	select_text(text_object, spell_word_pos[spell_current_word_index], spell_word[spell_current_word_index].length);

	for(i=1;i<spell_word_suggestions[spell_current_word_index].length;i++) {
		e = document.createElement('A');
		e.className="qb_spell_item";
		e.href="javascript:void(0);";
		e.style.display="block";
		atr = document.createTextNode(spell_word_suggestions[spell_current_word_index][i]);
		e.appendChild(atr);
		e.setAttribute("id", document.uniqueID);
		if(browser_type=="ie") {
			fu = new spell_sel_ie(i,e);					
			dbl_fu = new spell_change_ie(i, e);
			e.attachEvent('onclick', fu.invoke);
			e.attachEvent('ondblclick', dbl_fu.invoke);
		} else {
			e.setAttribute("onclick", "spell_sel("+i+", this);");
			e.setAttribute("ondblclick", "spell_change();");
		}
		qb_spell_suggestions.appendChild(e);
	}
}