var XpVoucher = {
	/**
	Mit getData() kann man Daten nachladen.
	getData() erzeugt einen http-Request mit den Parametern aus 'data'. Zusaetzlich wird ein anderer TypeNum (991) verwendet. Das weisst das Plugin dann an, dass es tx_xpshop2_jsquery benutzen soll, welche die Requests bearbeitet. Die Antwort wird dann wiederum hier verarbeitet.
	*/
	getData: function (data){
		// HTTP-Objekt erstellen
		var xmlHttpObject = this.getXmlHttpObject();
		
		// Abfrage vorbereiten
		var url = "index.php";
		var params = "type=996&L="+data['L'];
		
		for (var key in data){
			params = params+"&tx_xpvoucher_pi1["+key+"]="+data[key];
		}
		//document.write(url);
		// Request starten
		xmlHttpObject.open('POST', url, true);
		xmlHttpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttpObject.setRequestHeader("Content-length", params.length);
		xmlHttpObject.setRequestHeader("Connection", "close");
		
		// Wenn Request beendet
		switch (data["method"]){
			case "showForm":
				XpVoucher.writeLoading();
				XpVoucher.displayLoading();
				xmlHttpObject.onreadystatechange = showForm;
				break;
			case "checkForm":
				XpVoucher.writeLoading();
				XpVoucher.displayLoading();
				xmlHttpObject.onreadystatechange = checkForm;
				break;
		}
		xmlHttpObject.send(params);
		return false;
		
		
		function showForm(){
			if (xmlHttpObject.readyState == 4) {
				XpLib.fillContentById(data["contentElement"], xmlHttpObject.responseText);
				XpVoucher.hideLoading();
			}
		}
		
		function checkForm(){
			if (xmlHttpObject.readyState == 4) {
				XpLib.fillContentById(data["contentElement"], xmlHttpObject.responseText);
				XpVoucher.hideLoading();
			}
		}
		
	},

	writeLoading: function (){
		if (navigator.appName.indexOf("Microsoft") == -1){
			if (document.getElementById("xp_voucher_loading") == null) {
				var loading = document.createElement("div");
			
				var id = document.createAttribute("id");
				id.nodeValue = "xp_voucher_loading";
				loading.setAttributeNode(id);
				
				var style = document.createAttribute("style");
				style.nodeValue = "display: none;";
				loading.setAttributeNode(style);
				
				document.body.appendChild(loading);
			}
	
			if (document.getElementById("xp_voucher_loadingContent") == null) {
				var loadingContent = document.createElement("img");
							
				var id2 = document.createAttribute("id");
				id2.nodeValue = "xp_voucher_loadingContent";
				loadingContent.setAttributeNode(id2);
				
				var style2 = document.createAttribute("style");
				style2.nodeValue = "display: none;";
				loadingContent.setAttributeNode(style2);
				loadingContent.src = "typo3conf/ext/xp_voucher/res/loading.gif";
				
				document.body.appendChild(loadingContent);
			}
		}
	},
	
	displayLoading: function (){
		if (navigator.appName.indexOf("Microsoft") == -1){
			var loading = XpLib.getObject("xp_voucher_loading");
			var style = document.createAttribute("style");
			style.nodeValue = "display: block; position: fixed; z-index: 1000; top: 0; left: 0; width: 100%; height: 100%; background: #fff;";
			loading.setAttributeNode(style);
			XpLib.trans("xp_voucher_loading", 30);
			
			var mouse = document.createAttribute("onclick");
			mouse.nodeValue = "XpVoucher.hideLoading();";
			loading.setAttributeNode(mouse);
			
			var loadingContent = XpLib.getObject("xp_voucher_loadingContent");
			var style2 = document.createAttribute("style");
			var x = XpLib.getWindowWidth()/2-loadingContent.width/2;
			var y = XpLib.getWindowHeight()/2-loadingContent.height/2;
			style2.nodeValue = "display: block; position: fixed; z-index: 1001; top: "+y+"px; left: "+x+"px; width: 75px; height: 75px;";
			loadingContent.setAttributeNode(style2);
			var mouse2 = document.createAttribute("onclick");
			mouse2.nodeValue = "XpVoucher.hideLoading();";
			loadingContent.setAttributeNode(mouse2);
		}
	},
	
	hideLoading: function (){
		if (navigator.appName.indexOf("Microsoft") == -1){
		
			var loading = XpLib.getObject("xp_voucher_loading");
			var style = document.createAttribute("style");
			style.nodeValue = "display: none;";
			loading.setAttributeNode(style);
			
			var loadingContent = XpLib.getObject("xp_voucher_loadingContent");
			var style2 = document.createAttribute("style");
			style2.nodeValue = "display: none;";
			loadingContent.setAttributeNode(style2);
		}
	},
	
	
	/**
	Erzeugt ein Objekt aus der Klasse 'XMLHttpRequest' in Beachtung moderner Browser und des IE 5.5 und 6.
	*/
	getXmlHttpObject: function (){
		var xmlHttpObject = false;

		// Überprüfen ob XMLHttpRequest-Klasse vorhanden und erzeugen von Objekte für IE7, Firefox, etc.
		if (typeof XMLHttpRequest != 'undefined'){
			xmlHttpObject = new XMLHttpRequest();
		}

		// Wenn im oberen Block noch kein Objekt erzeugt, dann versuche XMLHTTP-Objekt zu erzeugen
		// Notwendig für IE6 oder IE5
		if (!xmlHttpObject) {
			try {
				xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) {
				try {
					xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e) {
					xmlHttpObject = null;
				}
			}
		}
		return xmlHttpObject;
	},
	
	/**
	Sammelt die Formulardaten
	*/
	getFormData: function (form_id, contentElement, credit_id, L){
		var data = new Object();
		var key;
		var value;
		for (var i=0; i < document.forms[form_id].length; i++){
			key = document.forms[form_id][i].name.replace(/tx_xpvoucher_pi1\[/g, '').replace(/\]/g, '');
			value = document.forms[form_id][i].value;
			data[key] = value;
		}
		data["method"]			= "checkForm";
		data["contentElement"]	= contentElement;
		data["credit_id"]		= credit_id;
		data["L"]				= L;
		
		return data;
	},
	
	textCounter: function (field, countfield, maxlimit) {
	if (field.value.length > maxlimit){
		field.value = field.value.substring(0, maxlimit);
	} else {
		countfield.value = maxlimit - field.value.length;
	}
}

}


/**
Da der Shop per JavaScript auf die Seite geholt wird, gibt es evtl. die Methode openPic() nicht. Diese wird nur auf die Seite geholt, wenn es ein Fenster zum oeffnen gibt.
*/

if (typeof openPic == 'undefined'){
	function openPic(url,winName,winParams)	{
		var theWindow = window.open(url,winName,winParams);
		if (theWindow)	{theWindow.focus();}
	}
}

