function confirmLink(theLink, theSqlQuery)
{
    // Confirmation is not required in the configuration file
    var is_confirmed = confirm(theSqlQuery);
    if (is_confirmed) {
        theLink.href += '&submit=yes';
    }

    return is_confirmed;
} // end of the 'confirmLink()' function


function extendsubmitfileform(formid,file_id,action_file){
	res = document.getElementById(formid);

	res.file_id.value=file_id;
	res.action_file.value=action_file;

	try {
		res.onsubmit();
		}
	catch(e){}
	res.submit.click();
}

function fakeformsubmit(formid,value){
	res = document.getElementById(formid);
	res.onchangeerror.value=value;

	try {
		res.onsubmit();
		}
	catch(e){}
	res.submit.click();
}

function extendsubmitform(formid,pressbutton,i,addsubmit){
	checkfields=check_count(formid);

	if (checkfields != 1 && i==1){
		alert('Zaznacz tylko jeden element');
	} else if (checkfields == 0 && i==2) {
		alert('Zaznacz jeden lub wi�cej elemet�w');
	} else if (checkfields != 2 && i==3) {
		alert('Zaznacz tylko dwa elementy');
	} else if (checkfields < 2 && i==4) {
		alert('Zaznacz dwa lub wi�cej elementow');
	} else {

		res = document.getElementById(formid);
		res.action.value=pressbutton;
		if (addsubmit==1) {
			res.submit2.value=pressbutton;
		}
		try {
			res.onsubmit();
			}
		catch(e){}
		res.submit();
	}
}

function check_count(formid) {
	var licznik = 0;

	res = document.getElementById(formid);
	pola= res.elements;
	for (i=0;i<pola.length;++ i) {
		if (pola[i].checked) {
			licznik++;
		}
	}
	return licznik;
}


function check_uncheck(formid,check) {
	var licznik = 0;
	res = document.getElementById(formid);
	pola= res.elements;

	for (i=0;i<pola.length;++ i) {
		if (pola[i].type=='checkbox') {
			pola[i].checked=check;
		}
	}
	return true;
}

function Str_AddValue(tabela,add_index) {
	var new_table= new Array();
	if (typeof tabela == 'string')
		if (tabela.length !== 0) {
			tmp_table= tabela.split(',');
			for (x in tmp_table)
				if (tmp_table[x] != add_index)
					new_table.push(tmp_table[x]);
	}

	if (add_index>'')
		new_table.push(add_index);

	out_string=new_table.join();
	return out_string;
}

function Str_DelValue (tabela,del_index) {
	tmp_table= tabela.split(',');

	var new_table= new Array();
	for (x in tmp_table)
		if (tmp_table[x] != del_index)
			new_table.push(tmp_table[x]);

	out_string=new_table.join();
	return out_string;
}

function Tmp_Delete(theQuery,str_index,del_value) {

	var is_confirmed = confirm(theQuery);
	if (is_confirmed) {
		var tmp_index = document.getElementById(str_index);

		var tmp_value=Str_DelValue(tmp_index.value,del_value);
		tmp_index.value=tmp_value;
		$("#"+str_index).trigger("refresh");
	}
}

function tmp_add (in_index,add_value)
{
	var tmp_index = document.getElementById(in_index);
	tmp_index.value=Str_AddValue(tmp_index.value,add_value);
	$("#"+in_index).trigger("refresh");
	var ile=$("div:visible").filter(".jqmWindow").length;
	$("div:visible").filter(".jqmWindow").eq(ile-1).jqmHide();
}

function tmp_replace (in_index,add_value)
{
	var tmp_index = document.getElementById(in_index);
	tmp_index.value=add_value;
	$("#"+in_index).trigger("refresh");
	var ile=$("div:visible").filter(".jqmWindow").length;
	$("div:visible").filter(".jqmWindow").eq(ile-1).jqmHide();
}

function PopupAttach(link_id,popup_id,params) {
		if ($("#"+popup_id).size()==0) {
			$("body").append('<div class="jqmWindow2" id="'+popup_id+'" style="display:none"></div>');
		} else {

		}
		$("#"+popup_id).jqm({modal:true,totop:true,onShow:function(hash) {
		$.ajax({
		   type: "POST",
   		   url: "ajax.php",
   		   data: params,
   		   success: function(msg){
     	   		$("#"+popup_id).html(msg);
     	   		$("#"+popup_id).jqmAddClose(".jqmClose");
   		   },
   		   error: function (XMLHttpRequest, textStatus, errorThrown) {
     	   		alert( "Nieoczekiwany error:a " + textStatus+ errorThrown);
		   }
 		 });
 		 hash.w.css("opacity",0.88).show();

		},onHide:function(hash) {
			hash.o.remove();
			hash.w.fadeOut("2000");
			$("body").unbind("on_close");
			$("#"+popup_id).remove();
		}});
}

function Popup_Attach2Link(link_id,popup_id,params,query) {
   	$("#"+link_id).click(function() {
		var is_confirmed=(query=='');
		if (!is_confirmed)
			is_confirmed = confirm(query);

		if (is_confirmed) {
			PopupAttach(link_id,popup_id,params);
			$("#"+popup_id).jqmShow();
			$("body").unbind("on_close");
			$("body").bind("on_close",function(){
   				$("#"+link_id).trigger("on_close");
   				return false;
			});
		}
	});
}

function RaR (params,ref_id,query) {

	var is_confirmed=(query=='');
	if (!is_confirmed)
		is_confirmed = confirm(query);

	if (is_confirmed) {
		$.ajax({
		   type: "POST",
		   url: "ajax.php",
	   	   data: params,
		   success: function(msg){
	   		   if (msg=="OK") {
				   $("#popup_window").jqmHide();
	   			   $("#"+ref_id).trigger("refresh");
	   		   } else {
	   			   alert(msg);
		       }
	   		},
   	   		error: function (XMLHttpRequest, textStatus, errorThrown) {
     	 		alert( "Nieoczekiwany error: " + textStatus );
	   		}
 		});
	}
}

function DoAndClose (params,ref_id) {

	$.ajax({
	   type: "POST",
	   url: "ajax.php",
   	   data: params,
	   success: function(msg){
   		   if (msg=="OK") {
			   $("#popup_window").jqmHide();
   			   $("#"+ref_id).trigger("refresh");
   		   } else {
   			   alert(msg);
	       }
   		},
 	   		error: function (XMLHttpRequest, textStatus, errorThrown) {
    	 		alert( "Nieoczekiwany error: " + textStatus);
   		}
		});
}

function SaS(form_id,field_id,val) {
	$("#"+field_id).val(val);
	$("#"+form_id).submit();
}



function BSearch_CloseLevels(level) {
  	 $("#parent td").each(function(i){
  	 	if (this.id!=='okno') {
  	 		tmp_level=Number(this.id.substring(5));
 	 		if (tmp_level>level) {
  	 			$("#level"+tmp_level).remove();
  	 		}
  	 	}
  	 });
}

function BSearch_NextNode(elem_id,level,media_type) {
	 BSearch_CloseLevels(level);
     $("#level"+level).after('<td id="level'+(level+1)+'"></td>');
	 $("#level"+(level+1)).load('ajax.php',{action:'bsearch_node',level:level+1,elem_id:elem_id,mt:media_type});
	 BSearch_MarkNode(elem_id,level);
	 BSearch_Show(1,elem_id,media_type);
}

function BSearch_Show(tag_type,tag_id,media_type) {

	params = {action:'bsearch_result',tag_type:tag_type,tag_id:tag_id,media_type:media_type};

	ShowMeBusy("okno");

	wynik= $.ajax({
		   	type: "POST",
   		   	url: "ajax.php",
   		   	data: params,
   		   	success: function(msg){
			  		$("#okno").html(msg);
   		   	},
   		  	 error: function (XMLHttpRequest, textStatus, errorThrown) {
     	   				alert( "Nieoczekiwany error:a " + textStatus+ errorThrown);
		   		}
	 });

	any= $("#okno").data("ajax");
	if (typeof any == "object" ) {
		any.abort();
		$("#okno").removeData("ajax");
		delete any;
	}
	$("#okno").data("ajax",wynik);
}

function BSearch_MarkNode(elem_id,level) {
  	 $("#level"+level+" a").removeClass("chosen");
  	 $("#node_"+elem_id).addClass("chosen");
}

function BSearch_MarkTag(elem_id,level) {
  	 $("#level"+level+" a").removeClass("chosen");
  	 $("#tag_"+elem_id).addClass("chosen");
}

function ShowMeBusy(area_id,prepend) {
	if (prepend==true)
		$("#"+area_id).prepend('<center><img src="images/spinner.gif"></center>');
	else
		$("#"+area_id).html('<center><img src="images/spinner.gif"></center>');
}

function News_Show_Hide(news_full,news_short,link_full,link_short) {
  	 $("#"+news_full).hide();
  	 $("#"+news_short).click(function(){
  	 	$("#"+news_short).hide();
		$("#"+news_full).show();
		return false;
  	 });
  	 $("#"+news_full).click(function(){
  	 	$("#"+news_full).hide();
		$("#"+news_short).show();
		return false;
  	 });
}
