function add_news(id){
	$("#add_news").load("scripts/get_news_update.php",{"id":id},function(){
		$("#add_news").toggle();
	});
}

function cancel_news(){
	var my_form = document.add_news_form;
	clearForm(my_form);
	$("#add_news").toggle();
}

function delete_news(id){
	if (confirm("Sunteti sigur?")){
		document.location.href = "scripts/scr_news_delete.php?id="+id;
	}
}

function add_event(id){
	$("#add_news").load("scripts/get_event_update.php",{"id":id},function(){
		$("#add_news").toggle();
	});
}

function cancel_event(){
	var my_form = document.add_event_form;
	clearForm(my_form);
	$("#add_news").toggle();
}

function delete_event(id){
	if (confirm("Sunteti sigur?")){
		document.location.href = "scripts/scr_event_delete.php?id="+id;
	}
}

var sel_q = '';
function show_q(q){
	if (sel_q!=''){
		$("#"+sel_q).hide();
		sel_q = '';
	}
	if (sel_q==''){
		$("#"+q).show();
		sel_q = q;
	}
}

function insertAtCaret(areaId,text) {
	var txtarea = document.getElementById(areaId);
	var scrollPos = txtarea.scrollTop;
	var strPos = 0;
	var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? 
		"ff" : (document.selection ? "ie" : false ) );
	if (br == "ie") { 
		txtarea.focus();
		var range = document.selection.createRange();
		range.moveStart ('character', -txtarea.value.length);
		strPos = range.text.length;
	}
	else if (br == "ff") strPos = txtarea.selectionStart;
	
	var front = (txtarea.value).substring(0,strPos);  
	var back = (txtarea.value).substring(strPos,txtarea.value.length); 
	txtarea.value=front+text+back;
	strPos = strPos + text.length;
	if (br == "ie") { 
		txtarea.focus();
		var range = document.selection.createRange();
		range.moveStart ('character', -txtarea.value.length);
		range.moveStart ('character', strPos);
		range.moveEnd ('character', 0);
		range.select();
	}
	else if (br == "ff") {
		txtarea.selectionStart = strPos;
		txtarea.selectionEnd = strPos;
		txtarea.focus();
	}
	txtarea.scrollTop = scrollPos;
}


function addImage(areaId){
	var url = prompt('Introduceti adresa la care se gaseste imaginea.','http://');
	if (url!=null && url!='http://'){
		insertAtCaret(areaId,'[img]'+url+'[/img]');
	}

}

function addLink(areaId){
	var url = prompt('Introduceti adresa.','http://');
	if (url!=null && url!='http://'){
		var name = prompt('Introduceti numele link-ului.');
		if (name==null) name = url;
		insertAtCaret(areaId,'[url][link]'+url+'[/link][name]'+name+'[/name][/url]');
	}
}
