
function get_comments(art_liveid) {
	try {
		if(art_liveid != "") {
			makeHttpRequest('/ch/comments/comment.php?art_liveid='+art_liveid, 'show_comments', false);
		}
		else {
			makeHttpRequest('/ch/comments/comment.php?'+self.location.href, 'show_comments', false);
		}
	}
	catch(e) {
	//	alert('get_comments: ' + e);
	}
}

function save_post_datas(art_liveid) {
	var post_datas = '';
	post_datas += 'poster_name=' + escape(document.forms['Kommentar'].poster_name.value) + '&';
	post_datas += 'poster_email=' + escape(document.forms['Kommentar'].poster_email.value) + '&';
	post_datas += 'post_text=' + escape(document.forms['Kommentar'].post_text.value) + '&';
	post_datas += 'article_path=' + escape(document.forms['Kommentar'].article_path.value) + '&';
	try {
		var http_request	= false;
		if(window.XMLHttpRequest) {
			http_request	= new XMLHttpRequest();
			if(http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
		} else if(window.ActiveXObject) {
			try {
				http_request	= new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request	= new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
		//			alert(e);//nix
				}
			}
		}
		
		if (!http_request) {
			alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
			return false;
		}
	    http_request.open('POST', '/ch/comments/save_comment_no_lock.php?art_liveid='+art_liveid, true);
	    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    http_request.setRequestHeader("Connection", "close");
	    http_request.send(post_datas);
	    http_request.onreadystatechange = function() {
		    if(http_request.readyState == 4)  {
		//		var content = "<p style=\"margin-top:17px; padding-left:5px; font-size:12px;\"><b>Ihr Kommentar wurde gespeichert und wird nach redaktioneller Sichtung freigeschaltet.</b></p>\n";
		//		show_message_saved(content);
				if(typeof timer != "undefined") clearTimeout(timer);
				timer = setTimeout("change_div('comment_add', '');", 5000); // Meldung nach 5 Sek. löschen
				if(typeof timer != "undefined") clearTimeout(timer);
				timer = setTimeout("makeHttpRequest('/de/ch/comments/comment.php?art_liveid="+art_liveid+"', 'show_comments', false);", 2000);
			}
		}
	}
	catch(e) {
	//	alert('save_post_datas: ' + e);
	}
}

/** Missbrauch melden */
function notify(postid) {
	var send = confirm("Sind Sie sicher ?");
	if(send == true) { 
		try {
			makeHttpRequest('/ch/comments/notify.php?postid='+postid, 'notify_message', false);
		}
		catch(e) {
		//	alert('notify: ' + e);
		}
	}
}
/* Bestätigung der Meldung */
function notify_message(content) {
	if(typeof timer2 != "undefined") clearTimeout(timer2);
	change_div('comment_add', content);
	timer2 = setTimeout("hide_comment_form();", 2000);
}

function save_comment(art_liveid) {
	try {
		save_post_datas(art_liveid);
//		hide_comment_form();
		show_comment_form(art_liveid); //wieder leeres Formular anzeigen 
	}
	catch(e) {
	//	alert('save_comment: ' + e);
	}
}

function show_comments(content) {
	if(content == "") {  //keine Kommentare zum Anzeigen
	}
	else {
//		hide_comment_form(); //nur Link anzeigen ("Kommentar hinzufügen")
	}
	change_div('comments', content);
}

function hide_comments() {
	var content = '<span style="margin-top:10px;"><a class="article_href" href="javascript:get_comments();"><i>Kommentare anzeigen</i></a></span>';
	change_div('comments', content);
	change_div('comment_add', '');
}

function hide_comment_form() {
	var content	= '';
	content	 	= '<div id="inner_add" style="height:15px; margin-top:20px; margin-bottom:17px;"><img src="/de/includes/nav/images/arrows.gif" style="text-align:left; margin-right:3px;" />';
	content		+= '<a class="bookmark" style="vertical-align:top; line-height:15px;" href="javascript:show_comment_form(article_liveid)">Kommentar hinzufügen</a></div>';
	change_div('comment_add', content);
}

/** Einträge im Formular überprüfen */
function checkform() {
	var sendBool 	= true;
	var error_msg	= "";
	if(document.Kommentar.guidelines_check.checked !== true) 	{ sendBool = false; error_msg += "Bitte bestätigen Sie Ihr Einverständnis mit den Richtlinien.\n"; }
	if(document.Kommentar.poster_name.value == "") 				{ sendBool = false; error_msg += "Bitte geben Sie einen Namen an.\n"; }
	if(document.Kommentar.post_text.value == "") 				{ sendBool = false; error_msg += "Sie haben keinen Kommentar eingegeben!\n"; }
	var error_msg_email = check_email();
	if(error_msg != "") alert(error_msg);
	else if(error_msg_email != "") sendBool = confirm(error_msg_email);
	return sendBool;
}

function check_email() {
	var message 			= "";
	var errormessage 		= "Die angegebene Emailadresse ist nicht korrekt. Trotzdem senden?\n";
	var correct_emailBool 	= true;
	var emailvalue 			= document.Kommentar.poster_email.value;
	try {
		var at_pos 				= emailvalue.indexOf("@");
		var last_point_pos 		= emailvalue.lastIndexOf(".");
		if(at_pos == -1 || last_point_pos == -1) {
			correct_emailBool = false;
		}
		else if(at_pos > last_point_pos) {	// "@" hinter dem letzten Punkt
			correct_emailBool = false;
		}
		else {
			var str_a_array = emailvalue.split("@");
			var str_b_array = str_a_array[1].split(".");
			if(str_a_array[0].length < 2) 							correct_emailBool = false;	// vor dem "@" weniger als zwei Zeichen
			else if(str_a_array[1].length < 2) 						correct_emailBool = false;	// nach "." weniger als zwei Zeichen
			else if(emailvalue.charAt(at_pos-1) == "." ||
					emailvalue.charAt(at_pos-2) == ".")				correct_emailBool = false;  // "." vor dem @-Zeichen, zwischen "." und @ weniger als zwei Zeichen;
			else if( at_pos != emailvalue.lastIndexOf("@") )		correct_emailBool = false;	// return "Nein, nicht genau ein @-Zeichen";
			else {
				if(str_b_array[str_b_array.length-1].length > 3)	correct_emailBool = false;	// nach "." mehr als drei Zeichen
			}
		}
		if(correct_emailBool == false ) {
			message = errormessage;
		}
	}
	catch(e) { 
//		alert("check_email" + e); 
		message = "Fehler";
	}
	return message;
}

/** Kommentar speichern */
function save_comment_link(art_liveid) {
	var sendBool = checkform();
	if(sendBool == true) {
		save_comment(art_liveid);
	}
}
/* Formular zur Kommentareingabe wieder anzeigen */
function add_comment(art_liveid) {
	if(typeof timer2 != "undefined") clearTimeout(timer2);
	show_comment_form(art_liveid);
}

function dummy(content) {
//	change_div('comment_add', content);//TEST
}

/** Klick ohne Weiterleitung: nur Click speichern */
function save_bannerclicks_ajax(clickcommand, bannername, client, site) {
//nix
}

function show_comment_form(art_liveid) {
	var content  = '';
	if(document.getElementById && document.getElementById('comments')) {
		content_cd	= document.getElementById('comments').innerHTML;
	} else if(document.all && document.all['comments']) {
		content_cd = document.all['comments'].innerHTML;
	}
	if(content_cd == '') {
		content		+= '<div class="line" style="width:468px; height:1px; margin-top:3px; margin-bottom:20px;"></div>';
	}
	content		+= '<form action="javascript:save_comment(\''+art_liveid+'\');" method="post" name="Kommentar" onsubmit="return checkform();">' + "\n";
	content		+= '<input type="hidden" name="article_path" value="'+self.location.href+'">' + "\n";
	content		+= '<div style="margin-top:20px; margin-bottom:17px;"><img src="/de/includes/nav/images/arrows.gif" style="text-align:left; margin-right:3px;" />';
//	content		+= '<span class="info"><a class="bookmark" style="vertical-align:top;" href="javascript:hide_comment_form();">Kommentar hinzufügen</a></span></div>';
	content		+= '<span class="info bookmark" style="vertical-align:top;">Kommentar hinzufügen</span></div>';
	content		+= 'Name:<br /><input class="info" type="text" name="poster_name" style="width:200px; height:22px;"><br />' + "\n";
	content		+= 'Email (optional):<br /><input class="info" type="text" name="poster_email" style="width:200px; height:22px;"><br />' + "\n";
	content		+= 'Kommentar:<br /><textarea class="info" rows="10" cols="50" name="post_text" style="width:360px; height:100px;"></textarea><br />' + "\n";
	content		+= '<input type="checkbox" name="guidelines_check" style="vertical-align:bottom;"> <a ';
	content		+= 'class="news_text" href="/ch/service/kommentare_richtlinien.html" target="_blank">Ich akzeptiere die <b>Richtlinien</b> für User-Kommentare</a><br />' + "\n";
	content		+= '<input type="image" value="ABSENDEN" src="/ch/images/absenden.jpg" style="margin-top:15px; margin-bottom:10px;">';
	content		+= '</form>';
	change_div('comment_add', content);
	
	// jump_to_form("comment_add");
}

function jump_to_form(comment_form) {
	if( document.getElementById && document.getElementById(comment_form) ) {
		form_left	= document.getElementById(comment_form).offsetLeft;
		form_top	= document.getElementById(comment_form).offsetTop;
		window.scrollTo(form_left, form_top);
	} 
	else if(document.all && document.all[comment_form]) {
		form_left	= document.all[comment_form].style.left;
		form_top	= document.all[comment_form].style.top;
		window.scrollTo(form_left, form_top);
	}
}

/* Bestätigungsmeldung anzeigen */
function show_message_saved(content) {
	if(typeof content == "undefined" || content == "") {
		content = "Kommentar gespeichert";
	}
	change_div('comment_add', content);
}
