function addSlashes(str) {
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\0/g,'\\0');
	return str;
}

function stripSlashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
}

function forumReply(user_id, topic_id, time){
	if($("textarea[name=reply_text]").val() != '') {
		$.post('admin/db.php?page=forum_topic_replies&action=insert', { reply_text: $("textarea[name=reply_text]").val(), member:user_id, forum_topic:topic_id, date_time_posted:time }, function(insertedID){
			var template = 'string:';
			template += '<div id="reply_{$data.id}" style="background-color:#C8DEFD; width:592px; padding:10px; margin-bottom:10px; float:left; display:none">';
			template += '<p style="color:#0E4486; margin:2px 0">Reply by <strong>{$data.member}</strong> on <strong>{$data.date_time_posted}</strong></p>';
			template += '<p style="font-weight:bold; margin:2px 0">{$data.reply_text}</p>';
			template += '</div>';

			$.get('admin/record.php', { page:'forum_topic_replies', action:'view', id:insertedID, template:template }, function(data) {
				$('#reply_form_container').before(stripSlashes(data));
				$('#reply_'+insertedID).slideDown('slow');
				$("textarea[name=reply_text]").val('');
			});
		});
	} else {
		alert('Please insert some text in the Reply Box!');
	}
	return false;
}

function forumNewTopic(user_id, time){
	if(($("textarea[name=topic_text]").val() != '') && ($("textarea[name=topic_name]").val() != '')) {
		$.post('admin/db.php?page=forum_topics&action=insert', { topic_text:$("textarea[name=topic_text]").val(), started_by:user_id, topic_name:$("input[name=topic_name]").val(), date_time_started:time }, function(insertedID){
			var template = 'string:';		
			template += '<div id="new_topic_{$data.id}" style="background-color: #C8DEFD; width:592px; padding:10px; margin-bottom:10px; float:left; display:none">';
			template += '<h2 style="width:100%; float:left; margin-bottom:5px">{$data.topic_name}</h2>';
			template += '<p style="color:#0E4486; font-weight:bold; margin:3px 0">Started by {$data.started_by} on {$data.date_time_started}</p>';
			template += '<p>{$data.topic_text|truncate:350}</p>';
			template += '<div class="buttonContainer"><a class="button" href="?page=forum-topic&id={$data.id}"><span>Enter Topic</span></a><span style="float:right">0 Replies</div>';
			template += '</div>';

			$.get('admin/record.php', { page:'forum_topics', action:'view', id:insertedID, template:template }, function(data) {
				$('#new_topic_form_container').before(stripSlashes(data));
				$('#new_topic_'+insertedID).slideDown('slow');
				$("textarea[name=topic_text]").val('');
				$("textarea[name=topic_name]").val('');
			});
		});
	} else {
		alert('Please insert some text in Title & Topic Text fields!');
	}
	return false;
}

function chatReply(chat_id, time){
	if(($("textarea[name=reply_text]").val() != '') && ($("input[name=reply_by]").val() != '')) {
		$.post('admin/db.php?page=chat_room_chat_replies&action=insert', { reply_text: $("textarea[name=reply_text]").val(), reply_by:$("input[name=reply_by]").val(), chat_id:chat_id, date_time_posted:time }, function(insertedID){
			var template = 'string:';
			template += '<div id="reply_{$data.id}" style="background-color:#C8DEFD; width:592px; padding:10px; margin-bottom:10px; float:left; display:none">';
			template += '<p style="color:#0E4486; margin:2px 0">Reply by <strong>{$data.reply_by}</strong> on <strong>{$data.date_time_posted}</strong></p>';
			template += '<p style="font-weight:bold; margin:2px 0">{$data.reply_text}</p>';
			template += '</div>';

			$.get('admin/record.php', { page:'chat_room_chat_replies', action:'view', id:insertedID, template:template }, function(data) {
				$('#reply_form_container').before(stripSlashes(data));
				$('#reply_'+insertedID).slideDown('slow');
				$("textarea[name=reply_text]").val('');
				$("input[name=reply_by]").val('');
			});
		});
	} else {
		alert('Please insert some text in the Name & Surname, and Reply Box!');
	}
	return false;
}


function chatNewChat(time){
	if(($("textarea[name=chat_text]").val() != '') && ($("textarea[name=chat_name]").val() != '') && ($("textarea[name=started_by]").val() != '')) {
		$.post('admin/db.php?page=chat_room_chats&action=insert', { chat_text:$("textarea[name=chat_text]").val(), started_by:$("input[name=started_by]").val(), chat_name:$("input[name=chat_name]").val(), date_time_started:time }, function(insertedID){
			var template = 'string:';		
			template += '<div id="new_chat_{$data.id}" style="background-color: #C8DEFD; width:592px; padding:10px; margin-bottom:10px; float:left; display:none">';
			template += '<h2 style="width:100%; float:left; margin-bottom:5px">{$data.chat_name}</h2>';
			template += '<p style="color:#0E4486; font-weight:bold; margin:3px 0">Started by {$data.started_by} on {$data.date_time_started}</p>';
			template += '<p>{$data.chat_text|truncate:350}</p>';
			template += '<div class="buttonContainer"><a class="button" href="?page=chat-room-chat&id={$data.id}"><span>Enter Chat</span></a><span style="float:right">0 Replies</div>';
			template += '</div>';

			$.get('admin/record.php', { page:'chat_room_chats', action:'view', id:insertedID, template:template }, function(data) {
				$('#new_chat_form_container').before(stripSlashes(data));
				$('#new_chat_'+insertedID).slideDown('slow');
				$("textarea[name=chat_text]").val('');
				$("textarea[name=chat_name]").val('');
				$("textarea[name=started_by]").val('');
			});
		});
	} else {
		alert('Please insert some text in Title, Name & Surname, and Chat Text fields!');
	}
	return false;
}
