var browser = (navigator.appName);
browser_add = (browser.indexOf("Microsoft Internet Explorer") != -1) ? '_ie' : '';
document.write('<link rel="stylesheet" href="/de/includes/nav/css/comment'+browser_add+'.css" />');

try {
    var show_line = false;
    var margin_top = (browser.indexOf("Microsoft Internet Explorer") != -1) ? -2 : 10;
    var line    = '<div class="line" style="width:468px; height:1px; margin-top:'+margin_top+'px; margin-bottom:10px;"></div>';
    
    var ligatus_div  = document.getElementById('ligatus');
    if(ligatus_div != null) {
        var content_ligatus = ligatus_div.innerHTML;
        if(content_ligatus.length < 1100) { //keine Ligatus-Box wird angezeigt
            show_line = true;
        }    
    }
    else {
        show_line = true;
    }
    if(show_line == true) {
        document.write(line);
    }
}
catch(e) {
    alert_error(e);
}

function get_comments(art_liveid) {
	try {
		if(art_liveid != "") {
			makeHttpRequest('/sportal/comments/comment.php?art_liveid='+art_liveid, 'show_comments', false);
		}
		else {
			makeHttpRequest('/sportal/comments/comment.php?'+self.location.href, 'show_comments', false);
		}
	}
	catch(e) {
		alert_error('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_error(e);//
				}
			}
		}
		
		if (!http_request) {
			alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
			return false;
		}
	    http_request.open('POST', '/sportal/comments/save_comment.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
		// 		Anzeigen des neuen Kommentars nach Timeout: deaktiviert, redaktionelle Freigabe erforderlich
		//		if(typeof timer != "undefined") clearTimeout(timer);
		//		timer = setTimeout("makeHttpRequest('/de/sportal/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('/sportal/comments/notify.php?postid='+postid, 'notify_message', false);
		}
		catch(e) {
			alert_error('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 {
		 // Captcha vars
	    var ccs_raw  = document.getElementById("ccs").value;
	    var ccs_user  = document.getElementById("captcha_check").value;
	    
	    if(ccs_raw != ccs_user) {
            alert("Bitte geben Sie die Sicherheitsabfrage korrekt ein!");
            return;
        }
        else {
    	    // Save
    		save_post_datas(art_liveid);
    		hide_comment_form();
    		
    		refresh_zp("send");
	   }
	}
	catch(e) {
		alert_error('save_comment: ' + e);
	}
}

function show_comments(content) {
	if(content == "") {  					// keine Kommentare zum Anzeigen, oder erneuter Klick auf Kommentieren-Link
	}
	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" class="comment_inner_add"><img src="/sportal/images/pfeil_sportal.gif" style="text-align:left; margin-right:7px;" />';
	content		+= '<a class="bookmark" 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_error("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) {
	try {
		makeHttpRequest('/reporting/bannerclick_noredirect.php?banner='+bannername+'&clickcommand='+clickcommand+'&client='+client+'&site='+site, 'dummy', false);
	}
	catch(e) {
		alert_error('save_bannerclicks_ajax: '+e);
	}
}

function show_comment_form(art_liveid) {
	// Captcha string erzeugen
	var captcha_string = zufall(5);
	
	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="comment_line"></div>';
	}
	content		+= '<form action="javascript:save_comment(\''+art_liveid+'\');" method="post" name="Kommentar" onsubmit="return checkform();">' + "\n";
	content     += '<input type="hidden" name="ccs" id="ccs" value="' + captcha_string + '">';
    content		+= '<input type="hidden" name="article_path" value="'+self.location.href+'">' + "\n";
	content		+= '<div class="comment_spacer"><img src="/sportal/images/pfeil_sportal.gif" style="text-align:left; margin-right:7px;" />';
	content		+= '<span class="info"><a class="bookmark" href="javascript:hide_comment_form();">Kommentar hinzufügen</a></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     += '<div style="margin-top:5px;">Sicherheitsabfrage:</div>';
    content     += '<div style="width:100px;float:left;"><img id="captcha_img" src="/sportal/service/gewinnspiele/generate_image_comments.php?' + captcha_string + '" border="0"></div><div style="width:75px;float:left;"><input class="info" type="text" name="captcha_check" id="captcha_check" style="width:75px; height:28px;"></div><div style="clear:both;height:0;line-height:0;font-size:0;"></div>';
    content     += '<div style="margin-top:5px;"><img src="/sportal/images/pfeil_sportal.gif" style="text-align:left; margin-right:3px;"><a href="javascript:;" onclick="javascript:updateCaptcha();" class="news_text">Sicherheitsabfrage neu starten</a></div><br>';
    content		+= '<input type="checkbox" name="guidelines_check" style="vertical-align:bottom;"> <a ';
	content		+= 'class="news_text" href="/sportal/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="/sportal/images/Absenden_Button_gelb.jpg" style="margin-top:15px;">';
	content		+= '</form><div class="comment_bottom_spacer"></div>';
	change_div('comment_add', content);
	document.location.hash = "diskussion_add";
	
	refresh_zp("add");
}

/* Bestätigungsmeldung anzeigen */
function show_message_saved(content) {
	if(typeof content == "undefined" || content == "") {
		content = "Kommentar gespeichert";
	}
	change_div('comment_add', content);
}

/** Klick auf Kommentarlinks zählen */
function refresh_zp(id) {
	try {
	   var path        = "/sportal/dummy.html"; //default
//	   var title       = document.title.length > 50 ? document.title.substring(0,50) : document.title;
	   if(id == "add") {
        	path = "/de/includes/nav/netrating/netrating_comments_edit.htm";
       }
       else if(id == "send") {
        	path = "/de/includes/nav/netrating/netrating_comments_save.htm";
       }
       iframe_zp_comment.location.href = path;	
	}
	catch(e) {
		alert_error(e); 
	}
}

/* AT WORK: nach Erreichen einer maximalen Höhe scrolling des Kommentarbereichs */
function scrolldiv(divname) {
	scroll_y = 0;
	if(document.all) {
		scroll_y = document.all[divname].pageYOffset;		
	} else {
		if(document.getElementById && document.getElementById(divname)) {
			scroll_y = document.getElementById(divname).scrollTop;
		}
	}
	alert_error(scroll_y);
}

function alert_error(e) {
 //  alert(e); //bei Bedarf aktivieren
}

function setRandom(){
           return Math.round(Math.random()*(122-48))+48;
       }
          
       function zufall(z){
          var chars = new Array();
          var x=0;
          var y=0;
          
          while(chars.length!=z){
              var num =  setRandom();
              if((num>47 && num<58) || (num>65 && num<91) || (num>96 && num<123)){
                  if((num>47 && num<58)&&(x<2)){
                      chars.push(String.fromCharCode(num));
                      x++;
                  }else if(y<z-2){
                      chars.push(String.fromCharCode(num));
                      y++;
                  }
              }
          }
          return chars.join('');
       }

function updateCaptcha() {
    var ccs_new = zufall(5);
    
    document.getElementById("captcha_img").src = '/sportal/service/gewinnspiele/generate_image_comments.php?' + ccs_new;
    document.getElementById("ccs").value = ccs_new;
}