//http://wiki.script.aculo.us/scriptaculous/show/Prototype
//http://www.sergiopereira.com/articles/prototype.js.html
//http://prototypejs.org/api

function showHint(id) {
	Element.hide("showhint"+id);
	Element.show("hint"+id);
}

function voteFailed() {
	$("voteBtn").enable();
	$("voteBtn").onclick = "";
	alert("There was an error and your rating was not saved. Please vote again.");
}

function submitVote(id) {
	
	$('voteBtn').disable();
	
	var fun4 = $('fun4');
	var fun3 = $('fun3');
	var fun2 = $('fun2');
	var fun1 = $('fun1');
	var fun0 = $('fun0');
	var fun9 = $('fun9');

	if(fun4.checked==true) fun = 4;
	else if(fun3.checked==true) fun = 3;
	else if(fun2.checked==true) fun = 2;
	else if(fun1.checked==true) fun = 1;
	else if(fun0.checked==true) fun = 0;
	else if(fun9.checked==true) fun = -1;
	
	var url = '/trivia/ajax/vote.php';
	var pars = 'id='+id+'&fun='+fun;
		
	new Ajax.Updater({ success: 'votequiz' }, url, {method: 'post', postBody: pars, onFailure: voteFailed } );
}

function showSpell(id) {
	var spell = $('spel'+id);
	var spchk = $('spchk'+id);
	
	if(spell && spchk && spchk.checked==true) Element.show(spell);
	if(spell && spchk && spchk.checked==false) Element.hide(spell);
}

function showCorrect(id) {
	var corr = $('corr'+id);
	var crchk = $('crchk'+id);
	
	if(corr && crchk && crchk.checked==true) Element.show(corr);
	if(corr && crchk && crchk.checked==false) Element.hide(corr); 
}

function getProblem(id,quiz) {
	var url = '/trivia/ajax/problem.php';
	var pars = 'id='+id+'&quiz='+quiz;
	
	Element.show("prb"+id);
	
	new Ajax.Updater('prb'+id, url, {method: 'get', parameters: pars} );
}

function submitProblem(id,quiz) {
	var url = '/trivia/ajax/report_problem.php';
	var pars = 'id='+id+'&quiz='+quiz;
	
	var spel = $F('spel'+id).stripTags().replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
	var corr = $F('corr'+id).stripTags().replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
	var spchk = $('spchk'+id);
	var crchk = $('crchk'+id);
	
	if(spel && spchk && spchk.checked==true) pars = pars+'&spel='+spel;
	if(corr && crchk && crchk.checked==true) pars = pars+'&corr='+corr;
	
	new Ajax.Updater('prb'+id, url, { method: 'post', postBody: pars });
}

var probtohide = 0;

function problemCorrected(originalRequest) {
	//var id = originalRequest.responseText;
	if(probtohide!=0) Element.hide("prob"+probtohide);
	probtohide=0;
}

function correctNoProblem(id,pid) {
	var url = '/trivia/ajax/correct_problem.php';
	var resp = $F('rej'+id).stripTags().replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
	var doit = 0;
	
	var pars = 'pid='+pid+'&reject=1'+'&resp='+resp;
	
	probtohide = id;
	
	if(resp!='') doit=1;
	if(doit==0) doit=confirm("Are you sure you want to reject this correction without writing a reason why?");
	
	if(doit==1) new Ajax.Updater('fix'+id, url, { method: 'post', postBody: pars, onSuccess: problemCorrected });
}

function correctProblem2(pid) {
	var url = '/trivia/ajax/correct_problem2.php';
	var pars = 'pid='+pid;
	probtohide = 0;

	var title = $F('title').stripTags().replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
	var summary = $F('summary').stripTags().replace(/&/g,"*amp*").replace(/\+/g,"*pls*");

	var cat = $F('formcategory');
	var sub = $F('subcat');

	pars = pars+'&title='+title+'&summary='+summary+'&cat='+cat+'&sub='+sub;
	
	new Ajax.Updater('fix0', url, { method: 'post', postBody: pars, onSuccess: problemCorrected });
}

function correctProblem(id,pid) {
	var url = '/trivia/ajax/correct_problem.php';
	var pars = 'pid='+pid;
	probtohide = id;

	var question = $F('question'+id).replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
	var extra = $F('extra'+id).replace(/&/g,"*amp*").replace(/\+/g,"*pls*");

	pars = pars+'&question='+question+'&extra='+extra;

	var type = $F('type'+id);
	if(type==0) {
		var mult1 = $('mult1'+id).checked;
		var mult2 = $('mult2'+id).checked;
		var mult3 = $('mult3'+id).checked;
		var mult4 = $('mult4'+id).checked;
		var mult5 = $('mult5'+id).checked;
		var mult = 1;
		if(mult2==true) mult=2;
		else if(mult3==true) mult=3;
		else if(mult4==true) mult=4;
		else if(mult5==true) mult=5;
		
		var manswer1 = $F('manswer1'+id).replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
		var manswer2 = $F('manswer2'+id).replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
		var manswer3 = $F('manswer3'+id).replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
		var manswer4 = $F('manswer4'+id).replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
		var manswer5 = $F('manswer5'+id).replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
		pars = pars+'&type=0&mult='+mult+'&manswer1='+manswer1+'&manswer2='+manswer2+'&manswer3='+manswer3+'&manswer4='+manswer4+'&manswer5='+manswer5;
	} else if(type==1) {
		var tf1 = $('tf1'+id).checked;
		var tf2 = $('tf2'+id).checked;
		var tf = 1;
		if(tf2==true) tf=2;
		pars = pars+'&type=1&tf='+tf;
	} else if(type==2) {
		var fillin = $F('fillin'+id).replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
		pars = pars+'&type=2&fillin='+fillin;
	}

	new Ajax.Updater('fix'+id, url, { method: 'post', postBody: pars, onSuccess: problemCorrected });
}

function correctType(id,type) {

	if(type==0) { //MC
		$("type"+id).value=0;
		$("ans"+id).update("<input type='radio' name='mult"+id+"' id='mult1"+id+"' value='1' /> <input type='text' id='manswer1"+id+"' class='form_txt_gr' style='margin-bottom: 3px;' size='35' maxlength='250' value='' /><br /><input type='radio' name='mult"+id+"' id='mult2"+id+"' value='2' /> <input type='text' id='manswer2"+id+"' class='form_txt_gr' style='margin-bottom: 3px;' size='35' maxlength='250' value='' /><br /><input type='radio' name='mult"+id+"' id='mult3"+id+"' value='3' /> <input type='text' id='manswer3"+id+"' class='form_txt_gr' style='margin-bottom: 3px;' size='35' maxlength='250' value='' /><br /><input type='radio' name='mult"+id+"' id='mult4"+id+"' value='4' /> <input type='text' id='manswer4"+id+"' class='form_txt_gr' style='margin-bottom: 3px;' size='35' maxlength='250' value='' /><br /><input type='radio' name='mult"+id+"' id='mult5"+id+"' value='5' /> <input type='text' id='manswer5"+id+"' class='form_txt_gr' style='margin-bottom: 3px;' size='35' maxlength='250' value='' /><br />Please select the correct answer with the radio buttons. To have fewer than 5 choices, leave any unused answers blank.");		
	} else if(type==1) { //TF
		$("type"+id).value=1;
		$("ans"+id).update("<input type='radio' name='tf"+id+"' id='tf1"+id+"' value='1' /> True<br /><input type='radio' name='tf"+id+"' id='tf2"+id+"' value='2' /> False<br />");
	} else if(type==2) { //FITB
		$("type"+id).value=2;
		$("ans"+id).update("<input type='text' name='fillin"+id+"' id='fillin"+id+"' class='form_txt_gr' size='45' maxlength='120' value='' /><br />The quizzer will need to type in the exact answer, so please make it clear what you are looking for in the question.  To allow multiple correct answers, separate them with a comma. The prefered answer should be first.  Example: 'dog, canine, doggy, puppy'.");
	}

}

function changeType(type) {
	Element.hide('type0'); 
	Element.hide('type1'); 
	Element.hide('type2'); 
	
	if(type==0) Element.show('type0');
	else if(type==1) Element.show('type1');
	else if(type==2) Element.show('type2');
}

function markComment(mark) {
	var url = '/trivia/ajax/mark_comment.php';
	var pars = 'mark='+mark;
		
	new Ajax.Request( url, {method: 'get', parameters: pars} );
	
	Element.hide("comment"+mark);
}

function commentPosted(originalRequest) {
	var res = originalRequest.responseText;
	
	if(res[0]=="0") { //error
		var err = $("posterror");
		if(err) err.update(res.substr(2));
	} else if(res) {
		var post = $("commentPost");
		if(post) {
			post.className="comment";
			post.update(res);
		}
	}
}

function postComment(quiz) {
	var stamp = $F("onlyonce");
	var comment = $F('comment').stripTags().replace(/&/g,"*amp*").replace(/\+/g,"*pls*");
	
	var url = '/trivia/ajax/post_comment.php';
	var pars = 'quiz='+quiz+'&stamp='+stamp+'&comment='+comment;
		
	new Ajax.Request( url, {method: 'post', postBody: pars, onSuccess: commentPosted} );
}

function checkSpelling(obj,type) {
	var check = "";
	
	if(type==1) {
		var quest = $F('quest'); //per question
		var explain = $F('explain'); //per question
		
		if(quest) check=check+" "+quest;
		if(explain) check=check+" "+explain;
		
		if(Element.visible("type2")) {
			var fillin = $F('fillin');
			if(fillin) check=check+" "+fillin;
		}
		if(Element.visible("type0")) {
			var manswer1 = $F('manswer1');
			var manswer2 = $F('manswer2');
			var manswer3 = $F('manswer3');
			var manswer4 = $F('manswer4');
			var manswer5 = $F('manswer5');
			if(manswer1) check=check+" "+manswer1;
			if(manswer2) check=check+" "+manswer2;
			if(manswer3) check=check+" "+manswer3;
			if(manswer4) check=check+" "+manswer4;
			if(manswer5) check=check+" "+manswer5;
		}
	} else {
		var title = $F('title'); //overall
		var summary = $F('summary'); //overall
		
		if(title) check=check+" "+title;
		if(summary) check=check+" "+summary;
	}
	var url = '/trivia/ajax/spelling.php';
	var pars = 'check='+check;
		
	new Ajax.Updater('spellcheck', url, {method: 'post', postBody: pars } );
	
	obj.innerHTML = "Check Spelling Again";
}

function showAllScores(id) {
	var url = '/trivia/ajax/scores.php';
	var pars = 'id='+id;

	new Ajax.Updater('allscores', url, {method: 'get', parameters: pars } );
}

function noenter(e) {
	if(!e) e = window.event;
	if(e.target) var et = e.target; //everyone
	else if(e.srcElement) var et = e.srcElement; //ie
  	if ((e.keyCode == 13) && (et.type=="text"))  {return false;} 
}

function preview() {
	var fact = $F('title');
	var phrase = $F('summary');
	var swap = $F('swap');
	var swap2 = $F('swap2');
	var swap3 = $F('swap3');
	
	new Ajax.Updater('dupfact', '/trivia/ajax/fact_dup.php', {method: 'post', postBody: 'f='+encodeURIComponent(fact)} );
	
	if(fact=='' || phrase=='') {
		alert("You did not type in a fact and key phrase.");
		return;
	}
	if(fact.indexOf(phrase)==-1) {
		alert("Your key word or phrase did not appear inside your fact.");
		return;
	}
	if(swap=='' || swap2=='' || swap3=='') {
		alert("You did not enter all the swap phrases.");
		return;
	}
	if(swap==phrase || swap2==phrase || swap3==phrase) {
		alert("Your swap phrase cannot be the same as your key phrase.");
		return;
	}
	if(swap==swap2 || swap2==swap3 || swap3==swap) {
		alert("Your swap phrases must all be different.");
		return;
	}
	$('preview').update("Loading...");
	$('preview').show();
	
	var fillin = fact.sub(phrase,'_________',10);
	var tf = fact.sub(phrase,swap,10);
	var tf2 = fact.sub(phrase,swap2,10);
	var tf3 = fact.sub(phrase,swap3,10);
	var multi="A) "+swap+"<br />B) "+swap2+"<br />C) "+swap3+"<br />D) "+phrase;
	
	var prev = "Please review these sample questions and make sure that all of them make sense. If one does not make sense, please adjust your fact or phrase.<br /><br />";
	
	if(multi!='') prev+="<b>Multiple Choice</b><br />"+fillin+"<br />"+multi+"<br /><br />";
	if(tf!='') prev+="<b>False Facts</b><br />"+tf+"<br />"+tf2+"<br />"+tf3+"<br />";

	$('preview').update(prev);
	$('prevbtn').update("Preview Again");
	$('subbtn').show();
}

function factReported(ret) {

	var res = ret.responseText;
	if(res=='' || res[0]=="0") { 
		//error
	} else { 
		$('prob'+res).remove();
	}
}

function factProblem(id) {
	var prob = prompt("Does this fact have a problem?  If so, please describe the problem below and submit it for review.",'');
	
	if(prob==undefined) return false;
	if(prob.length<5) return false;
	
	new Ajax.Request('/trivia/ajax/fact_report.php', {method: 'post', postBody: 'id='+id+'&prob='+encodeURIComponent(prob), onSuccess: factReported } );

	return false;
}
