var currentScriptId;

var http = null;
var runningLocation;
var kalender;
var view;
var historyTag;

function ajaxSubtype() {

	runningLocation = document.getElementById("eventRunningLocation").value;
	kalender = document.getElementById("eventKalender").value;
	view = document.getElementById("eventView").value;
	var path = runningLocation + "/com.xtention.eventCalendar/delivery/deliverEventCal.do?callback=getHTML&kalender="+kalender+"&view="+view;
	path += "&runningLoc="+runningLocation;
	
	if(document.getElementById("eventCal-dateCorrectFactor")!= null && document.getElementById("eventCal-dateCorrectFactor")!= undefined){
		path += "&correctFactor="+document.getElementById("eventCal-dateCorrectFactor").value;
	}
	if(document.getElementById("eventCal-displayId")!= null && document.getElementById("eventCal-displayId")!= undefined){
		path += "&displayId="+document.getElementById("eventCal-displayId").value;	
	}
	if(document.getElementById("eventCal-number")!= null && document.getElementById("eventCal-number")!= undefined){
		path += "&number="+document.getElementById("eventCal-number").value;	
	}
	if(document.getElementById("eventCal-link")!= null && document.getElementById("eventCal-link")!= undefined){
		path += "&link="+document.getElementById("eventCal-link").value;	
	}
	if(document.getElementById("eventCal-searchInput")!= null && document.getElementById("eventCal-searchInput")!= undefined){
		path += "&searchInput="+document.getElementById("eventCal-searchInput").value;	
	}
	
		
	

//	  request = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=' + appid + '&location=' + location + '&output=json&callback=getLocation';
	    // Create a new script object
	    aObj = new JSONscriptRequest(path);
	    // Build the script tag
	    aObj.buildScriptTag();
	    // Execute (add) the script tag
	    aObj.addScriptTag();
	    historyTag = 0;
	
	
//	alert(path);
			
//	if(window.XMLHttpRequest) {
//		http = new XMLHttpRequest();
//	} else if(window.ActiveXObject) {
//		http = new ActiveXObject("Microsoft.XMLHTTP");
//	}
//	if (http != null) {
//		http.open("GET", path, true);
//		http.onreadystatechange = ausgeben;
//		http.send(null);
//	   }
	
	}
function getHTML(jData){
	if (jData == null){
		alert("There was a problem parsing the request");
	}
	var text = jData.html;
//	alert(text);
	document.getElementById("eventcalendar").innerHTML = text;
}
	

function ausgeben() {
   if (http.readyState == 4) {
//   		document.all.checkboxes.innerHTML = "";
        var html = http.responseText;
//   		var subtype = eval("(" + daten + ")");
//		for (var i = 0; i < subtype.length; i++) {
//			var name = subtype[i];
//			var checkbox = document.createElement('INPUT');
//			checkbox.name = name;
//			checkbox.id = name;
//			var label = document.createElement('LABEL');
//			label.htmlFor = name;
//			label.innerText = name;
//      		checkbox.type = 'checkbox';        		
//      		document.all.checkboxes.appendChild(checkbox);
//       		document.all.checkboxes.appendChild(label);	
//		}
       
        document.getElementById("eventcalendar").innerHTML = html;
   }
}

function loadNewContent(path_){
	var path = runningLocation + '/com.xtention.eventCalendar/delivery/deliverEventCal.do?callback=getHTML&' +path_+"&runningLoc="+runningLocation;
//	alert(path);
//	alert(path);
			
	 // Create a new script object
    aObj = new JSONscriptRequest(path);
    // Build the script tag
    aObj.buildScriptTag();
    // Execute (add) the script tag
    aObj.addScriptTag();
    historyTag = 0;
	}
function sendMail(path_){
	var path = runningLocation + '/com.xtention.eventCalendar/delivery/deliverEventCal.do?callback=getHTML&' +path_+"&runningLoc="+runningLocation;
	if(document.getElementById("anmeldungsTitel")!= null && document.getElementById("anmeldungsTitel")!= undefined){
		path += "&anmeldungsTitel="+document.getElementById("anmeldungsTitel").value;
	}
	if(document.getElementById("anmeldungsNachname")!= null && document.getElementById("anmeldungsNachname")!= undefined){
		path += "&anmeldungsNachname="+document.getElementById("anmeldungsNachname").value;
	}
	if(document.getElementById("anmeldungsVorname")!= null && document.getElementById("anmeldungsVorname")!= undefined){
		path += "&anmeldungsVorname="+document.getElementById("anmeldungsVorname").value;
	}
	if(document.getElementById("anmeldungsAdresse")!= null && document.getElementById("anmeldungsAdresse")!= undefined){
		path += "&anmeldungsAdresse="+document.getElementById("anmeldungsAdresse").value;
	}
	if(document.getElementById("anmeldungsPLZ")!= null && document.getElementById("anmeldungsPLZ")!= undefined){
		path += "&anmeldungsPLZ="+document.getElementById("anmeldungsPLZ").value;
	}
	if(document.getElementById("anmeldungsOrt")!= null && document.getElementById("anmeldungsOrt")!= undefined){
		path += "&anmeldungsOrt="+document.getElementById("anmeldungsOrt").value;
	}
	if(document.getElementById("anmeldungsEmail")!= null && document.getElementById("anmeldungsEmail")!= undefined){
		path += "&anmeldungsEmail="+document.getElementById("anmeldungsEmail").value;
	}
	if(document.getElementById("anmeldungsTelefon")!= null && document.getElementById("anmeldungsTelefon")!= undefined){
		path += "&anmeldungsTelefon="+document.getElementById("anmeldungsTelefon").value;
	}
	if(document.getElementById("anmeldungsAnmerkungen")!= null && document.getElementById("anmeldungsAnmerkungen")!= undefined){
		var anmerkungen = document.getElementById("anmeldungsAnmerkungen").value;
		anmerkungen = anmerkungen.replace(/\n/g, '~');
		path += "&anmeldungsAnmerkungen="+anmerkungen;
	}
	if(document.getElementById("anmeldungsIdentity")!= null && document.getElementById("anmeldungsIdentity")!= undefined){
		path += "&anmeldungsIdentity="+document.getElementById("anmeldungsIdentity").value;
	}
	
	
//	alert(path);
//	alert(path);
			
	 // Create a new script object
    aObj = new JSONscriptRequest(path);
    // Build the script tag
    aObj.buildScriptTag();
    // Execute (add) the script tag
    aObj.addScriptTag();
    historyTag = 0;
	}

function searchContent(path_){
	var path = runningLocation + '/com.xtention.eventCalendar/delivery/deliverEventCal.do?callback=getHTML&' +path_+"&runningLoc="+runningLocation;
//	alert(path);
	if(document.getElementById("searchInput")!= null && document.getElementById("searchInput")!= undefined){
		path += "&searchInput="+document.getElementById("searchInput").value;	
	}
//	alert(path);
			
	 // Create a new script object
    aObj = new JSONscriptRequest(path);
    // Build the script tag
    aObj.buildScriptTag();
    // Execute (add) the script tag
    aObj.addScriptTag();
    historyTag = 0;
}


	window.onload = ajaxSubtype;


//-------------------------------------------------------------------------------
	
	// JSONscriptRequest -- a simple class for accessing Yahoo! Web Services
	// using dynamically generated script tags and JSON
	//
	// Author: Jason Levitt
	// Date: December 7th, 2005
	//
	// A SECURITY WARNING FROM DOUGLAS CROCKFORD:
	// "The dynamic <script> tag hack suffers from a problem. It allows a page 
	// to access data from any server in the web, which is really useful. 
	// Unfortunately, the data is returned in the form of a script. That script 
	// can deliver the data, but it runs with the same authority as scripts on 
	// the base page, so it is able to steal cookies or misuse the authorization 
	// of the user with the server. A rogue script can do destructive things to 
	// the relationship between the user and the base server."
	//
	// So, be extremely cautious in your use of this script.
	//

	// Constructor -- pass a REST request URL to the constructor
	//
	function JSONscriptRequest(fullUrl) {
	    // REST request path
	    this.fullUrl = fullUrl; 
	    // Keep IE from caching requests
	    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
	    // Get the DOM location to put the script tag
	    this.headLoc = document.getElementsByTagName("head").item(0);
	    // Generate a unique script tag id
	    this.scriptId = 'YJscriptId_' + JSONscriptRequest.scriptCounter++;
	    currentScriptId = this.scriptId;
	}

	// Static script ID counter
	JSONscriptRequest.scriptCounter = 1;

	// buildScriptTag method
	//
	JSONscriptRequest.prototype.buildScriptTag = function () {

	    // Create the script tag
	    this.scriptObj = document.createElement("script");
	    
	    // Add script object attributes
	    this.scriptObj.setAttribute("type", "text/javascript");
	    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
	    this.scriptObj.setAttribute("id", this.scriptId);
	}
	 
	// removeScriptTag method
	// 
	JSONscriptRequest.prototype.removeScriptTag = function () {
	    // Destroy the script tag
	    this.headLoc.removeChild(this.scriptObj);  
	}

	// addScriptTag method
	//
	JSONscriptRequest.prototype.addScriptTag = function () {
	    // Create the script tag
	    this.headLoc.appendChild(this.scriptObj);
	}

//-----------------------------------------------------------------
	function back(){
		
		
		var teile = currentScriptId.split("_");
		if(historyTag == 0){
			historyTag = teile[1];
		}
		if(document.getElementById(teile[0]+'_'+historyTag)!= null){
		document.getElementById(teile[0]+'_'+historyTag).parentNode.removeChild(document.getElementById(teile[0]+'_'+historyTag));
		}
		historyTag--;
		while(document.getElementById(teile[0]+'_'+historyTag) == null){
			historyTag--;
		}
		if(historyTag > 0){
//			alert(document.getElementById(teile[0]+'_'+historyTag).src);
			 aObj = new JSONscriptRequest(document.getElementById(teile[0]+'_'+historyTag).src+"&back=true");
//			 alert(document.getElementById(teile[0]+'_'+historyTag).src+"&back=true");
			    // Build the script tag
			    aObj.buildScriptTag();
			    // Execute (add) the script tag
			    aObj.addScriptTag();
			    document.getElementById(teile[0]+'_'+historyTag).parentNode.removeChild(document.getElementById(teile[0]+'_'+historyTag));
			  
			    
		}else{
			history.back();
		}
	}
	
	