/*
*	Ajax Handler für NIGHTRAIN
*	Website Version 2.0
*	Mar 2010
*/

//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_mail(trigger,name,email) {
if (anfrage != null) {

	//Trigger prüfen und Infobox sichtbar machen
	if(trigger == "newsletter") {
	document.getElementById(trigger+"_infobox").style.display = "block";
	}

 var url="controller/mailer.php";
 var parameter = "&name="+encodeURI(name)+"&email="+encodeURI(email)+"&trigger="+encodeURI(trigger);
 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!");
}
}

//------------------------------------------------------------------------------
// CAPTCHA NACHLADEN
function reload_captcha() {
if (anfrage != null) {
 document.getElementById("captchapic").innerHTML = "<span style=\"width:200px;height:400px;padding-left:20px;\">Lade...</span>";
 var url="controller/captcha/captcha.php";
 //var parameter = "&name="+encodeURI(name)+"&email="+encodeURI(email)+"&trigger="+encodeURI(trigger);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=captcha;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send();
 }
 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("texte_text_area").contentWindow == 'undefined') {
		new_value = document.getElementById("texte_text_area").value;
	} 
	else {
		new_value = document.getElementById("texte_text_area").contentWindow.document.getElementsByTagName("body")[0].innerHTML;
	 }
	 
 new_value = htmlspecialchars(new_value);
 new_value = Base64.encode(new_value);
 var url="model/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!");
}
}

//------------------------------------------------------------------------------
function rte(id) {
if (anfrage != null) {
 var name = "texte";
 var url="model/get_txt.php"; 
 var parameter = "&type="+encodeURI(name)+"&id="+encodeURI(id);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=get_content;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
 }
 else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
}
}

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

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

function get_altergig(id) {
if (anfrage != null) {
 var trigger = "select";
 var url="controller/gig_handler.php"; 
 var parameter = "&trigger="+encodeURI(trigger)+"&date="+encodeURI(id);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=get_gig;
 anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 anfrage.send(parameter);
 }
 else {
 alert("XMLHttpRequest-Objekt konnte nicht erstellt werden!");
 }
}

function get_altergallery(id) {
if (anfrage != null) {
 var trigger = "select";
 var url="controller/media_handler.php"; 
 var parameter = "&trigger="+encodeURI(trigger)+"&id="+encodeURI(id);
 anfrage.open("POST", url, true);
 anfrage.onreadystatechange=get_gallery;
 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;
   switch (information) {
	case "Erfolgreich eingetragen!":
		document.getElementById("newsletter_infobox").innerHTML = information;
	break;
	
	default:
	 alert("Unbekannter Aufruf!");
	break;
   }
  }
 }
}

function get_content() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   document.getElementById("texte_text_area").contentWindow.document.getElementsByTagName("body")[0].innerHTML = information;
   //document.getElementById("texte_text_area").innerHTML = information;
  }
 }
}

function get_news() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   document.getElementById("news_alter_box").innerHTML = information;
   $('.rte-zone').rte('css/','img/');
  }
 }
}

function get_blog() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   document.getElementById("blog_alter_box").innerHTML = information;
   $('.rte-zone').rte('css/','img/');
  }
 }
}

function get_gig() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   document.getElementById("gigs_alter_box").innerHTML = information;
  }
 }
}

function get_gallery() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   document.getElementById("gallerys_alter_box").innerHTML = information;
  }
 }
}

function infoausgeben_aenderung() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
   var information = anfrage.responseText;
   alert(information);
   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 captcha() {
 if(anfrage.readyState == 4) {
  if(anfrage.status == 200) {
  var code = new Date().getTime();
  document.getElementById("captchapic").innerHTML = "<img src=\"controller/captcha/captcha.php?id="+code+"\" border=\"0\" title=\"Sicherheitscode\">";
  }
 }
}

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;
}
