var link_id;
var voted = false;
var monitoring = false;
var jump;
check = new Array();

function display_commentdiv(link_id, type)
{
	newcontent = "<b>D&auml;i Kommentar:</b> &nbsp; &nbsp; (<a href='#' onclick=\"return openPGPopup('misc/bb_comments.html');\">Format&eacute;irungscode</a>, <a href='#' onclick=\"return openPGPopup('misc/smileys.html');\">Smileys</a>)<form action='#' method='POST' id='form_comment_main' OnSubmit='return addcomment();'><div align='left'><textarea cols='66' rows='10' id='text_comment' onblur='commentTextBoxActive = false;' onfocus='commentTextBoxActive = true;'></textarea><br><input type='hidden' id='link_id' value='"+link_id+"'><input type='hidden' value='"+type+"' id='pagetype'><input type='submit' id='btn_commentsave' value='Sp&auml;icheren' class='button'></div></form>";
	$('extradiv').innerHTML = newcontent;
    return false;
}
function displaycommdiv(comment_id, link_id, type)
{
	var tempi = 'commdiv_' + comment_id;
    if (this.check[comment_id] != 1 ) {
    	this.check[comment_id] = 1;
    	newcontent = '<div style="text-align:left; margin-left:30px;"><b>Deng &Auml;ntwert:</b> &nbsp; &nbsp; (<a href="#" onclick="return openPGPopup(\'misc/bb_comments.html\');">Format&eacute;irungscode</a>, <a href="#" onclick="return openPGPopup(\'misc/smileys.html\');">Smileys</a>)<form action=\'#\' id="form_comment_' + comment_id + '" OnSubmit=\"return addcommentanswer(' + comment_id + ');\" ><textarea cols=64 rows=9 name=comment_answer onblur=\"commentTextBoxActive = false;\" onfocus=\"commentTextBoxActive = true;\"></textarea><input type=hidden value=' + comment_id + ' name=comment_id><br /><input type="submit" value="Sp&auml;icheren" class="button" id="btn_commentsave_' + comment_id + '" /></form></div>';
    	$(tempi).innerHTML = newcontent;
    } else {
    	$(tempi).innerHTML = '';
    	delete(this.check[comment_id]);
    }
    return false;
}

function displayeditdiv(comment_id, ctype)
{
	para = "&id=" + comment_id + "&type=" + ctype;
	var req = new Request(
		{
			url: 'ajaxRequest.php',
			onSuccess: function(txt) {
				handleServerResponse_getcomment(txt, comment_id);
			},
			onFailure: function() { alert(ajax_request_error); }
		}
	).send("page=getcomment" + para);
	return false;
}

function display_monitordiv(link_id, author_id)
{
	AjaxUpdater("page=showmonitoring&foto_id=" + link_id + "&author_id=" + author_id, 'post', "extradiv", true);
	return false;
}

function save_Monitoring(post_id)
{
	folder_id = getSelectedValue("select_folder_id");
	para = "&foto_id=" + post_id + "&folder_id=" + folder_id;
	var req = new Request(
	{
		url: 'ajaxRequest.php',
		onSuccess: function(txt) {
			handleServerResponse_saveMonitoring(txt);
		},
		onFailure: function() { alert(ajax_request_error); }
	}
	).send("page=savemonitoring" + para);
}
function handleServerResponse_saveMonitoring(response)
{
	if (response == "true") 
	{
		monitoring = true;
		$("extradiv").innerHTML = "<b>D&euml;s Foto ass elo an denger Iwwerwaachung.</b>";
	}
	else $("extradiv").innerHTML = response;
}

function display_votediv(message, adm_list, link_id, type)
{
	newcontent = '<div style="margin:5px;">';
	if (voted == false)
	{
		if (message == "")
		{
			newcontent += '<select name="vote" style="margin:10px;" id="select_vote_val"><option selected value="0">- Bew&auml;rten -</option><option value="5">5 (Top)</option><option value="4">4</option><option value="3">3 (Ganz OK)</option><option value="2">2</option><option value="1">1 (Net gutt)</option></select>';
			newcontent += "<br><input type='button' class='button' onclick=\"save_vote(\'"+link_id+"\', \'"+type+"\');\" value='Bew&auml;rtung sp&auml;icheren'>";
		}
		else newcontent += message;
	} else newcontent += "<b>Du hues grad schonn des Foto bew&auml;rt.</b>";
	
	newcontent += '</div>';
	if (adm_list != "")
	{
		newcontent += "<div style='margin:10px;'>" + adm_list + "</div>";
	}
	$('extradiv').innerHTML = newcontent;
	return false;
}

function save_vote(post_id, type)
{
	index = $("select_vote_val").selectedIndex;
	option = $("select_vote_val").options[index];
	
	voten = option.innerHTML;
	
	if (voten == "5 (Top)") voten = 5;
	if (voten == "3 (Ganz OK)") voten = 3;
	if (voten == "1 (Net gutt)") voten = 1;
	
	para = "&link_id=" + post_id + "&type=" + type + "&vote=" + voten;
	var req = new Request(
	{
		url: 'ajaxRequest.php',
		onSuccess: function(txt) {
			handleServerResponse_savevote(txt);
		},
		onFailure: function() { alert(ajax_request_error); }
	}
	).send("page=savevote" + para);
}
function handleServerResponse_savevote(response)
{
	if (response == "true") 
	{
		voted = true;
		$("extradiv").innerHTML = "<b>Merci fir deng Bew&auml;rtung</b>";
	}
	else $("extradiv").innerHTML = response;
}

function ws_subscribe(ws_id)
{	
	if (confirm("Wierklech aschreiwen fir desen Workshop?"))
	{
		AjaxUpdater("page=wssubscribe&ws_id=" + ws_id, 'post', "extradiv", true);
	}
	return false;
}

function ws_getusers(ws_id)
{	
	AjaxUpdater("page=wsgetusers&id=" + ws_id, 'post', "extradiv", true);

	return false;
}

var ws_user_id;
function ws_validateSubscription(ws_id, user_id)
{
	ws_user_id = user_id;
	
	var req = new Request(
	{
		url: 'ajaxRequest.php',
		onSuccess: function(txt) {
			handleServerResponse_wsValidatedSubscription(txt);
		},
		onFailure: function() { alert(ajax_request_error); }
	}
	).send("page=wsvalidatesubscription&ws_id=" + ws_id + "&user_id=" + user_id);
	return false;
}
function handleServerResponse_wsValidatedSubscription(txt)
{
	if (txt == "1")
	{
		$("ws_user_status_" + ws_user_id).innerHTML = "Fest ageschriwwen";
	}
	else alert("Error: " + txt);
}

function saveNewChrono()
{
	link_id = encodeURI($("link_id").value);
	type = encodeURI($("pagetype").value);
	
	var req = new Request(
		{
			url: 'ajaxRequest.php',
			onSuccess: function(txt) {
				handleServerResponse_savenewchrono(txt);
			},
			onFailure: function() { alert(ajax_request_error); }
		}
	).send("page=savenewcommentchrono");
	return false;
}
function handleServerResponse_savenewchrono(response)
{
	if (response == "true") reloadcomments();
	else alert("Error: " + response);
}

function addcomment(j)
{
	$("btn_commentsave").value = "Gett gespaichert ...";
	$("btn_commentsave").disabled = true;
	
	content = escape($("text_comment").value);
	link_id = escape($("link_id").value);
	content = content.replace(/\+/gm, "%2B");
	type = $("pagetype").value;
	para = "&comment=" + content + "&type=" + type + "&link_id=" + link_id + "&comment_id=0";
	var req = new Request(
		{
			url: 'ajaxRequest.php',
			onSuccess: function(txt) {
				handleServerResponse_newcomment(txt);
			},
			onFailure: function() { alert(ajax_request_error); }
		}
	).send("page=addcomment" + para);
	jump = j;
	return false;
}

function addcommentanswer(comment_id)
{
	ref = $("form_comment_" + comment_id);
	btnSave = $("btn_commentsave_" + comment_id);
	btnSave.value = "Gett gespaichert ...";
	btnSave.readonly = "readonly";
	
	content = escape(ref.comment_answer.value);
	link_id = $("link_id").value;
	type = $("pagetype").value;
	para = "&comment=" + content + "&link_id=" + link_id + "&comment_id=" + comment_id + "&type=" + type;
	var req = new Request(
		{
			url: 'ajaxRequest.php',
			onSuccess: function(txt) {
				handleServerResponse_newcomment(txt);
			},
			onFailure: function() { alert(ajax_request_error); }
		}
	).send("page=addcomment" + para);
	return false;
}

function editcomment(comment_id)
{
	btnEdit = $("btn_commenteditsave_" + comment_id);
	btnEdit.value = "G&euml;tt gesp&auml;ichert ...";
	btnEdit.readonly = "readonly";
	
	content = escape($("textarea_" + comment_id).value);
	link_id = $("link_id").value;
	type = $("pagetype").value;
	para = "&comment=" + content + "&comment_id=" + comment_id + "&type=" + type;
	var req = new Request(
		{
			url: 'ajaxRequest.php',
			onSuccess: function(txt) {
				handleServerResponse_newcomment(txt);
			},
			onFailure: function() { alert(ajax_request_error); }
		}
	).send("page=editcomment" + para);
	return false;
}

function handleServerResponse_newcomment(response)
{
	if (response == "true")
	{
		reloadcomments();
		if (jump == 1)
		{
			window.location.href = '#bottompage';
		}
	}
	else {
		reloadcomments();
		alert("Error: " + response);
	}
}

function handleServerResponse_getcomment(response, edit_id)
{
	ref = $("comment_content_" + edit_id);
	if (response != "false")
	{
		ref.innerHTML = "<form action='#' method='POST' OnSubmit='return editcomment(" + edit_id + ");'><div align='left'><textarea cols='50' rows='8' id='textarea_" + edit_id + "' onblur=\"commentTextBoxActive = false;\" onfocus=\"commentTextBoxActive = true;\">" + response + "</textarea><br /><input type='submit' id='btn_commenteditsave_"+edit_id+"' value='Sp&auml;icheren' class='button' /></div></form>";
	}
	else ref.innerHTML = "E Feeler ass opgetrueden.";
}

function reloadcomments()
{
	para = "&link_id=" + link_id + "&type=" + type;
	var req = new Request(
		{
			url: 'ajaxRequest.php',
			onSuccess: function(txt) {
				handleServerResponse_reloadcomments(txt);
			},
			onFailure: function() { alert(ajax_request_error); }
		}
	).send("page=reloadcomments" + para);
	return false;
}

function handleServerResponse_reloadcomments(newcomments)
{
	$("div_comments").innerHTML = newcomments;
}