function remove_and_signs(str){
	return str.replace('&', '_#38_amp_sc_');
}

// Display the user level status int the header of all pages
function load_level(username) {
	jQuery.ajax({
		type: "POST",
		url: "get_level.php",
		data: "username="+username,
		success: function(msg){
			document.getElementById('level').innerHTML = msg;
	   	}
	});
}

// Display the user status info in the header of all pages
function load_status(username) {
	jQuery.ajax({
		type: "POST",
		url: "get_status.php",
		data: "username="+username,
		success: function(msg){
			document.getElementById('status').innerHTML = msg;
			load_level(username);
	   	}
	});
}

//Reload the questions that exist at the moment
function reload_qstns(){
	jQuery.ajax({
		url: "get_questions.php",
		success: function(msg){
			document.getElementById('lwr_faq').innerHTML = msg;
	   	}
	});
}

// Display the form to add a new question
function add_question_form_faq(){
	jQuery.ajax({
		url: "add_question.php",
		success: function(msg){
			if(msg=="user_not_logged_in") 
				window.location = "index.php?from=add_qstn_forum";
			else document.getElementById('add_form_upr_faq').innerHTML = msg;
			
	   	}
	});
}

// Enable the question input text
function enable_txt_fld_faq(id_txt_fld) {
	if(document.getElementById(id_txt_fld).value == 'Write something...') {
		document.getElementById(id_txt_fld).value = '';
	}
}

// Check the input form values to add a new question.
// If it is valid, store the new question
function add_form_upr_faq_post() {
	var title = document.getElementById('nq_title').value;
	var question = document.getElementById('nq_question').value;
	var points = document.getElementById('nq_points').value;
	
	title = remove_and_signs(title);
	question = remove_and_signs(question);

	var errn = 0;
	var ersttl = '';
	var erstq = '';
	var errpnts = '';
	if(title=='') {
		ersttl='Title';
		errn++;
	}
	if((question=='')||(question=='Write something...')) {
		if(errn)erstq=', ';
		erstq+='Question';
		errn++;
	}
	if(points=='') {
		if(errn)errpnts=', ';
		errpnts+='Points';
		errn++;
	}
	else if(parseInt(points) != points){
		if(errn)errpnts=', ';
		errpnts+='Points';
		errn++;
	}

	if(errn) 
		alert('Please insert valid value for '+ersttl+erstq+errpnts+' fields.');
	else
		jQuery.ajax({
			type: "POST",
		   	url: "add_question.php",
		   	data: "title="+title+"&question="+question+"&points="+points,
		   	success: function(msg){
				if(msg==1) {
					document.getElementById('add_form_upr_faq').innerHTML = '';
					reload_qstns();
				}
				else alert(msg);
		   	}
		});
}

function add_form_upr_faq_cancel() {
	document.getElementById('add_form_upr_faq').innerHTML = '';
}

// Display the question information to allow anwering it
function get_question(question){
	jQuery.ajax({
		type: "POST",
	   	url: "get_question.php",
	   	data: "question="+question,
	   	success: function(msg){
			document.getElementById('lwr_faq').innerHTML = msg;
	   	}
	});
}

// Add a new answer to a posted forum question
function add_ans_forum_qstn_post(question) {
	
	var answer = document.getElementById('forum_qstn_answer_txt').value;
	answer = remove_and_signs(answer);
	
	if((answer == '') && (answer == ' ')) 
		alert('Please insert valid value for answer.');
	else {
		jQuery.ajax({
		   type: "POST",
		   url: "insert_ans.php",
		   data: "question="+question+"&answer="+answer,
		   success: function(msg){
			 if(msg==1) get_qstn_ans_posted(question);
			 else {
				 if(msg=="user_not_logged_in") window.location = "index.php?from=reply_forum";
				 else alert('Error occured while inserting the Answer. Please try again refreshing the browser.');
			 }
		   }
		});
	}
}

// Refresh the list of answers after post a new one
function get_qstn_ans_posted(question){
	jQuery.ajax({
		type: "POST",
	   	url: "get_question.php",
	   	data: "question="+question,
	   	success: function(msg){
			document.getElementById('lwr_faq').innerHTML = msg;
			document.getElementById('forum_qstn_answer').innerHTML = '<b>Thanks for your submission.<b>';
	   }
	});
}

// Approve a forum answer
function approve_forum_ans(username, answer, question){
	jQuery.ajax({
		type: "POST",
	   	url: "approve_ans.php",
	   	data: "answer="+answer+"&question="+question,
	   	success: function(msg){
			if(msg){
				load_status(username);
				get_question(question);
			}
			else alert('Error occured while approving the Answer. Please try again refreshing the browser.');
		}
	});
}

function srch_kwrd_faq() {
	var kwrd = document.getElementById('srch_txt_faq').value;
	kwrd = remove_and_signs(kwrd);
	jQuery.ajax({
	   type: "POST",
	   url: "get_questions.php",
	   data: "filter="+kwrd,
	   success: function(msg){
		 document.getElementById('lwr_faq').innerHTML = msg;
	   }
	});
}