
function sz(t) {
	t.rows = Math.floor(t.value.length/50)+2;
}

function finishDelete(dom,rid,uid,tochange) {
	//window.location.reload();
	textareaElm =document.getElementById('answer_'+rid);
	//document.getElementById('aid_' + rid).value=0; //answer deleted so Edits are now inserts
	textareaElm.value = '';
	finishSave(dom,rid,uid,tochange);
}


function finishSave(dom,rid,uid,tochange) {
	var new_id = dom.getElementsByTagName('result')[0].getAttribute('value');
	var aid = document.getElementById('aid_' + rid).value;
	var qid = document.getElementById('qid_' + rid).value;
	textareaElm =document.getElementById('answer_'+rid);
	textareaElm.style.border='0px';
	textareaElm.readOnly = true;
	
	esMenu = document.getElementById('es_' + rid);
	esMenu.href = "javascript:editAnswer(" + rid + "," + uid+ "," + tochange + ")";
	esMenu.innerHTML = "Edit"; //show me another way to change the text between a tags
	
	document.getElementById('aid_' + rid).value=new_id;
	var anyText = (textareaElm.value).length;
	del = document.getElementById('del_' + rid);
	if (anyText>0) {
		//document.getElementById('aid_' + rid).value=new_id; //very important allows an edit of a newly inserted question answer
		document.getElementById('msg_' +rid).style.color = 'green';
		document.getElementById('msg_' +rid).innerHTML = "Saved.";
		del.href = "javascript:deleteAnswer(" + rid +"," + uid + "," + tochange + ")";
		del.innerHTML = "Delete";
	} else {
		document.getElementById('msg_' +rid).style.color = 'red';
		document.getElementById('msg_' +rid).innerHTML = "Deleted.";
		del.href = "";
		del.innerHTML = "";
	}
	
}

function editAnswer(rid,uid,tochange) {
	//get textarea ready for editing
	document.getElementById('msg_' +rid).innerHTML = '';
	var aid = document.getElementById('aid_' + rid).value;
	var qid = document.getElementById('qid_' + rid).value;
	textareaElm =document.getElementById('answer_'+rid);
	textareaElm.readOnly = false;
	textareaElm.style.border='thin solid black';
	textareaElm.focus();
	//show save button
	esMenu = document.getElementById('es_' + rid);
	esMenu.href = "javascript:saveAnswer(" + rid + "," + uid + "," + tochange + ")";
	esMenu.innerHTML = "Save"; //show me another way to change the text between a tags
}

function deleteAnswer(rid,uid,tochange) {
	var aid = document.getElementById('aid_' + rid).value; // answer id
	var qid = document.getElementById('qid_' + rid).value;
	
	var confirmDelete = window.confirm("Are you sure you want to delete the answer:'" + document.getElementById('answer_'+rid).value + "'");
	
	if (confirmDelete) {
		ajax = new ajaxRequest;
		ajax.send("userside_ajax.php?action=answerDelete", "question_id=" + qid + "&answer_id=" + aid + "&tochange=" + tochange +"&user_id=" + uid + "&module_id=" + document.getElementById('module_id').value, "finishDelete", rid +","+ uid + "," + tochange);		
	}
}

function saveAnswer(rid,uid,tochange) {
	var aid = document.getElementById('aid_' + rid).value; // answer id
	var qid = document.getElementById('qid_' + rid).value;
	textareaElm =document.getElementById('answer_'+rid);
	
	if ((textareaElm.value).length>0) { //this if-statement is new and probably will need to go
		var answerText = encode64(textareaElm.value);
		ajax = new ajaxRequest;
		ajax.send("userside_ajax.php?action=answerEditor", "question_id=" + qid + "&answer_id=" + aid + "&module_id=" + document.getElementById('module_id').value  + "&tochange=" + tochange  + "&user_id=" + uid + "&answer=" + answerText, "finishSave", rid +","+ uid + "," + tochange);		
		
	} else { //not allowing a blank answer in the db
		ajax = new ajaxRequest;
		ajax.send("userside_ajax.php?action=answerDelete", "question_id=" + qid + "&answer_id=" + aid + "&tochange=" + tochange +"&user_id=" + uid + "&module_id=" + document.getElementById('module_id').value, "finishDelete", rid +","+ uid + "," + tochange);		
	
	}
		
}

function encode64(inp)
{
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
"abcdefghijklmnopqrstuvwxyz" + //all lowercase
"0123456789+/="; // all numbers plus +/=
	var out = ""; //This is the output
	var chr1, chr2, chr3 = ""; //These are the 3 bytes to be encoded
	var enc1, enc2, enc3, enc4 = ""; //These are the 4 encoded bytes
	var i = 0; //Position counter

	do { //Set up the loop here
		chr1 = inp.charCodeAt(i++); //Grab the first byte
		chr2 = inp.charCodeAt(i++); //Grab the second byte
		chr3 = inp.charCodeAt(i++); //Grab the third byte

		//Here is the actual base64 encode part.
		//There really is only one way to do it.
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}

		//Lets spit out the 4 encoded bytes
		out = out + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) +
		keyStr.charAt(enc4);

		// OK, now clean out the variables used.
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";

	} while (i < inp.length); //And finish off the loop

	//Now return the encoded values.
	return out;
}

