//XMLHttpRequest erzeugen
var anfrage = null; //initialisieren
try {
 anfrage = new XMLHttpRequest();
}
catch (e) {
 try {
  anfrage = new ActiveXObject("Msxml2.XMLHTTP");
 }
 catch (e) {
  try {
   anfrage = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e) {
   anfrage = null;
  }
 }
}


//------------------------------------------------------------------------------
// KONTAKTFORMULAR
function send_contact(name,email,subj,msg) {
if (anfrage != null) {
 var url="mailer.php";
 var parameter = "&name="+encodeURI(name)+"&email="+encodeURI(email)+"&subj="+encodeURI(subj)+"&msg="+encodeURI(msg);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=infoausgeben_mail;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
 }
 else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}

//------------------------------------------------------------------------------
//BACKEND CONTENT ändern
function alter_content(content_name) {
if (anfrage != null) {
	//Check ob Design oder HTML-Modus aktiviert ist und new_value dementsprechend initialisieren
	if(typeof document.getElementById(content_name+"_text_area").contentWindow == 'undefined') {
		new_value = document.getElementById(content_name+"_text_area").value;
	} 
	else {
		new_value = document.getElementById(content_name+"_text_area").contentWindow.document.getElementsByTagName("body")[0].innerHTML;
	 }
	 
 new_value = htmlspecialchars(new_value);
 new_value = Base64.encode(new_value);
 var url="alter_content.php";
 var parameter = "&content="+encodeURI(new_value)+"&field="+encodeURI(content_name);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=infoausgeben_aenderung;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
 }
 else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}

//------------------------------------------------------------------------------
//BACKEND GIG ändern
function gig_handle(action,titel,date,location,info,uid) {
if (anfrage != null) {
 var url="gig_handler.php";
 var parameter = "&action="+encodeURI(action)+"&gig="+encodeURI(titel)+"&gig_date="+encodeURI(date)+"&gig_location="+encodeURI(location)+"&gig_info="+encodeURI(info)+"&gig_uid="+encodeURI(uid);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=infoausgeben_gig;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
}
else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}

//------------------------------------------------------------------------------
//SELECT GIG ändern
function select_gig(titel) {
if (anfrage != null) {
 var url="select_gig.php";
 var parameter = "&gig="+encodeURI(titel);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=infoausgeben_gig_aenderung;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
}
else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}

//------------------------------------------------------------------------------
//NEWSHANDLER
function news_handle(action,date,content,link) {
if (anfrage != null) {
 var url="news_handler.php";
 var parameter = "&action="+encodeURI(action)+"&news_date="+encodeURI(date)+"&news_content="+encodeURI(content)+"&news_link="+encodeURI(link);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=infoausgeben_news;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
}
else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}


//------------------------------------------------------------------------------
//TERMINHANDLER
function termin_handle(action,date,time,content) {
if (anfrage != null) {
	//Prüfen der Checkbox für Mitgliederbenachrichtigung
	if(document.getElementById('termin_msg').checked){
		var mail = "on";
	} else {var mail = "off"};
 var url="termin_handler.php";
 var parameter = "&action="+encodeURI(action)+"&termin_date="+encodeURI(date)+"&termin_time="+encodeURI(time)+"&termin_agenda="+encodeURI(content)+"&termin_msg="+encodeURI(mail);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=infoausgeben_termin;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
}
else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}


//------------------------------------------------------------------------------
//LINKHANDLER
function links_handle(action,ref,comment) {
if (anfrage != null) {
 var url="links_handler.php";
 var parameter = "&action="+encodeURI(action)+"&links_ref="+encodeURI(ref)+"&links_comment="+encodeURI(comment);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=infoausgeben_links;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
}
else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}


//------------------------------------------------------------------------------
//TERMINHANDLER
function konto_handler(betrag) {
if (anfrage != null) {
 var url="konto_handler.php";
 var parameter = "&betrag="+encodeURI(betrag);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=infoausgeben_konto;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
}
else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}

//BACKEND Forum NEW TOPIC
function new_topic(content_name,topic) {
if (anfrage != null) {
	//Check ob Checkbox gesetzt ist oder nicht und Namenszuweisung
	/*if(document.form1.forum_topic_msg.checked == true) {
		var msg = "on";
	} else {var msg = "off";}*/

	//Check ob Design oder HTML-Modus aktiviert ist und new_value dementsprechend initialisieren
	if(typeof document.getElementById(content_name).contentWindow == 'undefined') {
		new_value = document.getElementById(content_name).value;
	} 
	else {
		new_value = document.getElementById(content_name).contentWindow.document.getElementsByTagName("body")[0].innerHTML;
	 }

	//Zur Übertragung nach Base64 konvertieren 
	new_value = htmlspecialchars(new_value);
	new_value = Base64.encode(new_value);
	
 var url="add_topic.php";
 var parameter = "&detail="+encodeURI(new_value)+"&topic="+encodeURI(topic);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=infoausgeben_newtopic;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
 }
 else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}

//BACKEND Forum NEW ANTWORT
function new_answer(content_name,id) {
if (anfrage != null) {
	//Check ob Checkbox gesetzt ist oder nicht und Namenszuweisung
	/*if(document.form1.forum_answer_msg.checked == true) {
		var msg = "on";
	} else {var msg = "off";}*/

	//Check ob Design oder HTML-Modus aktiviert ist und new_value dementsprechend initialisieren
	if(typeof document.getElementById(content_name).contentWindow == 'undefined') {
		new_value = document.getElementById(content_name).value;
	} 
	else {
		new_value = document.getElementById(content_name).contentWindow.document.getElementsByTagName("body")[0].innerHTML;
	 }

	//Zur Übertragung nach Base64 konvertieren 
	new_value = htmlspecialchars(new_value);
	new_value = Base64.encode(new_value);
	 
 var url="add_answer.php";
 var parameter = "&a_answer="+encodeURI(new_value)+"&id="+encodeURI(id);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=infoausgeben_newanswer;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
 }
 else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}

//------------------------------------------------------------------------------
//FILEHANDLER
function delete_file(datei,dir) {
if (anfrage != null) {
 var url="delete_file.php";
 var parameter = "&file="+encodeURI(datei)+"&dir="+encodeURI(dir);
 anfrage.open("POST", url, true);
 if (dir == "uploads") {
 anfrage.onreadystatechange=infoausgeben_delete;
 } else {anfrage.onreadystatechange=infoausgeben_delete_pic;}
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
}
else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}

	 //-------------------------------------------------------------------------------------------------------------
//Funktion zum ausblenden der Info-Boxen
function ausblenden (box) {
	box.style.display = "none";
	}

//FÜR ALLE FUNKTIONEN
function infoausgeben_mail() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   document.getElementById("status_info").innerHTML = information;
   document.getElementById('kontakt_form').reset();
  }
 }
}

function infoausgeben_aenderung() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   var box = 'document.getElementById("text_info_box")';
   document.getElementById("text_info_box").style.display = "block";
   document.getElementById("text_info_box").innerHTML = information;
   
   	window.setTimeout("ausblenden("+box+")", 1500);
	location.reload();
  }
 }
}

function infoausgeben_gig_aenderung() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   document.getElementById("alter_gig_table").style.display = "block";
   document.getElementById("alter_gig_table").innerHTML = information;
   
   window.setTimeout("ausblenden("+box+")", 1500);
   location.reload();
  }
 }
}

function infoausgeben_gig() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   var box = 'document.getElementById("gig_info_box")';
   document.getElementById("gig_info_box").style.display = "block";
   document.getElementById("gig_info_box").innerHTML = information;
   
	window.setTimeout("ausblenden("+box+")", 1500);
	location.reload();
  }
 }
}

function infoausgeben_news() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
	var box = 'document.getElementById("news_info_box")';
   document.getElementById("news_info_box").style.display = "block";
   document.getElementById("news_info_box").innerHTML = information;
   
   	window.setTimeout("ausblenden("+box+")", 1500);
	location.reload();
  }
 }
}

function infoausgeben_links() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
	var box = 'document.getElementById("links_info_box")';
   document.getElementById("links_info_box").style.display = "block";
   document.getElementById("links_info_box").innerHTML = information;
   
   	window.setTimeout("ausblenden("+box+")", 1500);
	location.reload();
  }
 }
}

function infoausgeben_termin() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   var box = 'document.getElementById("termin_info_box")';
   document.getElementById("termin_info_box").style.display = "block";
   document.getElementById("termin_info_box").innerHTML = information;
   
   	window.setTimeout("ausblenden("+box+")", 1500);
	location.reload();
  }
 }
}

function infoausgeben_konto() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   var box = 'document.getElementById("konto_info_box")';
   document.getElementById("konto_info_box").style.display = "block";
   document.getElementById("konto_info_box").innerHTML = information;
   
   	window.setTimeout("ausblenden("+box+")", 1500);
	location.reload();
  }
 }
}


function infoausgeben_newtopic() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
  //var information = anfrage.responseText;
  //document.write(information);
   window.location.href="http://www.peppermansfriend.de/forum";
  }
 }
}

function infoausgeben_newanswer() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
  var information = anfrage.responseText;
	//document.body.innerHTML = information;
   window.location.href="http://www.peppermansfriend.de/view_topic.php?id="+information;
  }
 }
}


function infoausgeben_delete() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   var box = 'document.getElementById("file_info_box")';
   document.getElementById("file_info_box").style.display = "block";
   document.getElementById("file_info_box").innerHTML = information;
   
   	window.setTimeout("ausblenden("+box+")", 1500);
	location.reload();
  }
 }
}

function infoausgeben_delete_pic() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   var box = 'document.getElementById("pic_info_box")';
   document.getElementById("pic_info_box").style.display = "block";
   document.getElementById("pic_info_box").innerHTML = information;
   
   	window.setTimeout("ausblenden("+box+")", 1500);
	location.reload();
  }
 }
}


/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

//------------------------------------------------------------------------------
function htmlspecialchars (string, quote_style, charset, double_encode) {
    // Convert special characters to HTML entities  
    // 
    // version: 912.1315
    // discuss at: http://phpjs.org/functions/htmlspecialchars    // +   original by: Mirek Slugen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nathan
    // +   bugfixed by: Arno
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Ratheous
    // +      input by: Mailfaker (http://www.weedem.fr/)
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +      input by: felix    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: charset argument not supported
    // *     example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
    // *     returns 1: '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;'
    // *     example 2: htmlspecialchars("ab\"c'd", ['ENT_NOQUOTES', 'ENT_QUOTES']);    // *     returns 2: 'ab"c&#039;d'
    // *     example 3: htmlspecialchars("my "&entity;" is still here", null, null, false);
    // *     returns 3: 'my &quot;&entity;&quot; is still here'
    var optTemp = 0, i = 0, noquotes= false;
    if (typeof quote_style === 'undefined' || quote_style === null) {        quote_style = 2;
    }
    string = string.toString();
    if (double_encode !== false) { // Put this first to avoid double-encoding
        string = string.replace(/&/g, '&amp;');    }
    string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;');
 
    var OPTS = {
        'ENT_NOQUOTES': 0,        'ENT_HTML_QUOTE_SINGLE' : 1,
        'ENT_HTML_QUOTE_DOUBLE' : 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE' : 4    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags        quote_style = [].concat(quote_style);
        for (i=0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;            }
            else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/'/g, '&#039;');
    }    if (!noquotes) {
        string = string.replace(/"/g, '&quot;');
    }
 
    return string;}
