function getById(id){
	return document.getElementById(id);
}

/*networking info*/
var file_name = 'location.php';
var tmp_text = 'loading';
function changeSubField(id_field){
	
	role_obj = getById('role');
	role_obj.innerHTML = '';
	
	str = 'sel=get_netw_fields&par=id_sub_field&id_spr='+id_field;
	destination_obj = getById('sub_field');
	anisochronous = true;
	ajaxRequest(file_name, str, destination_obj, tmp_text, anisochronous);
}
function changeRole(id_sub_field){
	str = 'sel=get_netw_fields&par=id_role&id_spr='+id_sub_field;
	destination_odj = getById('role');
	anisochronous = true;
	ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous);
}

function logoffAfter(sec){
	setTimeout(function(){location.href='index.php?sel=auto_logoff'},(sec*1000));
}
function closeAfter(sec){
	setTimeout(function(){window.close();},(sec*1000));
}

function showNotice(msg){
	if (!msg) return;
	$("<div id='temp'>"+msg+"</div>").appendTo("body").addClass("notice_div").fadeIn("slow").fadeTo(2000,1).fadeOut("slow");
}

function getJQData(data){
	if (!data){
		showNotice('system error!!!'); 
		return false;
	}
	var resp;
	eval("resp = " + data);
	if (!resp){
		showNotice('system error!!!'); 
		return false;
	}
	return resp;
}

function ajaxLoadingImage(status, id_container){
	if (status)
		$("#"+id_container).html('<img id="img_loading" src="'+TEMPLATE_PATH+'/images/ajax-loader.gif" alt="" border="0" />');
	else
		$("#"+id_container).html('');
}


function addTag(id_item){
	var file_name;
	var input_tag_val;
	file_name = $("#file_name").val();
	input_tag_val = $("#input_tag"+id_item).val();
	str = "ajax=1&sel=addtag&tag="+input_tag_val+"&id_item="+id_item;
	$.post(file_name, str, afterAddTag);
}

function afterAddTag(data){
	if (!data) return;
	var resp;
    eval("resp = " + data);
    if (!resp) return;
	showNotice(resp["msg"]);
	if (resp["code"] == '1'){
		var id_item
		id_item = resp['id_item'];
		
		/*$("#all_tags_container"+id_item).show();*/
		$("#my_tags_container"+id_item).show();
		$("#input_tag"+id_item).val('');
		$.each(resp['tags_info'], function(i, n){
			/*$("#all_tags"+id_item).append('<span><a href="'+n.search_link+'" title="'+n.tag_count+'">'+n.tag+'</a>&nbsp;&nbsp;</span>').css('display','none').fadeIn("slow");*/
			$("#my_tags"+id_item).append('<span id="my_tag'+n.id+'">'+n.tag+'&nbsp;<a href="#" onclick="delTag(\''+n.id+'\'); return false;" class="del_x_link">x</a>&nbsp;&nbsp;</span>').css('display','none').fadeIn("slow");
		});
	}
}

function delTag(id){
	file_name = $("#file_name").val();
	data = 'ajax=1&sel=deltag&id='+id;
	$.post(file_name, data, afterDelTag);
}

function afterDelTag(data){
	if (!data) return;
	var resp;
    eval("resp = " + data);
    if (!resp) return;
	showNotice(resp["msg"]);
	if (resp["code"] == '1'){
		$('#my_tag'+resp.id_tag).fadeOut('fast');
	}
}

function bookmarkSitePage(page_link, type, descr){
	page_link = escape(page_link);
	str = 'ajax=1&bookmark_url='+page_link+'&bookmark_type='+type+'&bookmark_descr='+descr;
	$.post('organizer.php?sel=bookmark_site_page',str, function(data){
		var resp = getJQData(data);														
		if (!resp) return;
		showNotice(resp["msg"]);
	});	
}

function checkLoginDublicate(login, fname, file_name, id_source, id_destination){
	$("#"+id_destination).empty();
	if (login.length < 5) return false;
	str = 'ajax=1&sel=check_login_dublicate&login='+login+'&fname='+fname;
	$.post(file_name, str, function(data){
		var resp = getJQData(data);														
		if (!resp) return;
		var html_code='';
		if (resp.msg != undefined){
			html_code += '<font class="error_msg">'+resp.msg+'</font>';
		}
		if (resp.data){
			///such login already exist
			$.each(resp.data, function(i,val){
				html_code += '<br /><a href="#" onClick="$(\'#'+id_source+'\').val(\''+val+'\'); $(\'#'+id_destination+'\').empty(); $(\'#'+id_source+'\').focus();" >'+val+'</a>';
			});
		}
		if (html_code != ''){
			$("#"+id_destination).append(html_code);
		}
	});
}

function abuseReportForm(){
	if ($("#abuse_form_parent").html() && $("#abuse_form_parent").remove()) return;
	$("#abuse_link").before("<div id='abuse_form_parent' style='display:inline; position:relative;'>"+$("#abuse_form_tpl").html()+"</div>");
	$("#abuse_form").addClass('abuse_form');
	var height = ($("#abuse_form").height()+$("#abuse_link").height());
	$("#abuse_form").css('top','-'+(height+23)+'px');
	$("#abuse_form").css('left','0px');
	
}

function sendAbuseReport(id_user, id_item, type_item){
	var type_msg = $("input[@name=ab_type_msg]:checked").attr('value');
	var msg = $("#ab_msg").val();
	if (!id_user || !id_item || !type_item || !type_msg || !msg) return;
	str = 'ajax=1&sel=save_abuse_report&id_user='+id_user+'&id_item='+id_item+'&type_item='+type_item+'&type_msg='+type_msg+'&msg='+msg;
	$.post("backend.php", str, function(data){
		$("#abuse_form").html(data);
		var height = ($("#abuse_form").height()+$("#abuse_link").height());
		$("#abuse_form").css('top','-'+(height+53)+'px');
		$("#abuse_form").css('left','0px');
		$("#abuse_form").css('white-space','nowrap');
		$("#abuse_form").fadeIn('fast').fadeTo(1000,1).fadeOut(2000,function(){$("#abuse_form_parent").remove()});
	})
}
