var kursartFlag=false;
var detailsFlag = false;
var courseDetailsFlag = false;
var kursbezeichungFlag = false;
var courseKurzFlag = false;

function makePOSTRequest(url, parameters) {
	http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
    	http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/plain; charset=ISO-8859-2');
        }
    }
	else if (window.ActiveXObject) { // IE
        try {
    	    http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
		catch(e){
        	try {
            	http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
			catch(e){}
        }
	}
    if (!http_request) {
    	alert('Cannot create XMLHTTP instance');
        return false;
    }	
    http_request.onreadystatechange = alertContents;
    http_request.open('POST', url, true); // SET method to GET or POST according to form method specified
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);	
}

function alertContents() {
	if (http_request.readyState == 4) {
		returnVal = http_request.responseText;
		if(detailsFlag) {
			detailsArray = returnVal.split("#@#");
			for(var i=1; i<detailsArray.length; i++){
				detailsInfoArray = detailsArray[i].split("#!#");
				document.getElementById(containerArea).innerHTML=detailsInfoArray[0];
				document.getElementById(containerArea).innerHTML+="<br><br>";
				document.getElementById(containerArea).innerHTML+=detailsInfoArray[1];
				document.getElementById(containerArea).innerHTML+="<br><br>";
				document.getElementById(containerArea).innerHTML+=detailsInfoArray[2];
				
			}
			
			detailsFlag=false;
		}
		if(kursartFlag) { 
			document.forms['advanceSearch'].tourenart.options.length=1;
			kursartArray = returnVal.split("#@#");
			for(var i=1; i<kursartArray.length; i++){				
				kursartInfoArr = kursartArray[i].split("#!#");
				document.forms['advanceSearch'].tourenart.options[i] = new Option(kursartInfoArr[1], kursartInfoArr[1]);
			}
			document.getElementById(containerArea).innerHTML = '';	
			kursartFlag=false;
		}
		if(kursbezeichungFlag) { 
			document.forms['advanceSearch'].Kursbezeichnung.options.length=1;
			kursbezeichungArray = returnVal.split("#@#");
			for(var i=1; i<kursbezeichungArray.length; i++){				
				kursbezeichungInfoArr = kursbezeichungArray[i].split("#!#");
				document.forms['advanceSearch'].Kursbezeichnung.options[i] = new Option(kursbezeichungInfoArr[1], kursbezeichungInfoArr[1]);
			}
			document.getElementById(containerArea).innerHTML = '';	
			kursbezeichungFlag=false;
		}
		if(courseDetailsFlag) {
			document.getElementById(containerArea).innerHTML=returnVal;
			courseDetailsFlag = false;
		}
		if(courseKurzFlag) {
			document.getElementById(containerArea).innerHTML=returnVal;
			courseKurzFlag = false;
		}
	}
	else document.getElementById(containerArea).innerHTML = '<img src="fileadmin/images/loading.gif">'; 		
}

function selectTourenatInfo(pageId){
	containerArea='status_msg';
	kursartFlag = true;
	var poststr = "Kursart=" + encodeURI(document.forms['advanceSearch'].kursartId.options[document.forms['advanceSearch'].kursartId.selectedIndex].value);
   	makePOSTRequest("index.php?id="+pageId+"&mode=info&no_cache=1", poststr);
}
function selectKursbezeichnungInfo(pageId){
	containerArea='status_msg';
	kursbezeichungFlag = true;
	var poststr = "Kursbezeichnung=" + encodeURI(document.forms['advanceSearch'].tourenart.options[document.forms['advanceSearch'].tourenart.selectedIndex].value);
	//alert(poststr);
	if (document.forms['advanceSearch'].tourenart.options[document.forms['advanceSearch'].tourenart.selectedIndex].value == 'Skitouren')
	 {
		document.getElementById('skitoMsg').style.display = 'block'; 
	} 
	else 
	{
		document.getElementById('skitoMsg').style.display = 'none'; 	
	}
   	makePOSTRequest("index.php?id="+pageId+"&mode=courseInfo&no_cache=1", poststr);
}
function showDetails(courseId,pageId){
	containerArea=courseId;
	detailsFlag = true;
	poststr = "uid="+courseId;
	makePOSTRequest("index.php?id="+pageId+"&mode=courseDetails&no_cache=1", poststr);
}
function showCourseDetails(courseId,pageId) {
	containerArea='child_'+courseId;
	courseDetailsFlag = true;
	if(document.getElementById('child_'+courseId).innerHTML != '') {
		if(document.getElementById('child_'+courseId).style.display=="none") {
			document.getElementById('parent_'+courseId).innerHTML = '<img src="typo3conf/ext/bergundtal_search/res/images/minus.gif" border="0" />';
			document.getElementById('child_'+courseId).style.display="block";	
		}
		else {
			document.getElementById('parent_'+courseId).innerHTML = '<img src="typo3conf/ext/bergundtal_search/res/images/plus.gif" border="0" />';
			document.getElementById('child_'+courseId).style.display="none";				
		}
	}
	else {
		poststr = "uid="+courseId;
		makePOSTRequest("index.php?id="+pageId+"&mode=courseDetailsInfo&no_cache=1", poststr);
		document.getElementById('parent_'+courseId).innerHTML = '<img src="typo3conf/ext/bergundtal_search/res/images/minus.gif" border="0" />';
	}
}

function showKurzbeschreibung(courseId,pageId){
	containerArea='kurz_child_'+courseId;
	courseKurzFlag = true;
	if(document.getElementById('kurz_child_'+courseId).innerHTML != '') {
		if(document.getElementById('kurz_child_'+courseId).style.display=="none") {
			document.getElementById('kurz_parent_'+courseId).innerHTML = '<img src="typo3conf/ext/bergundtal_search/res/images/minus.gif" border="0" />';
			document.getElementById('kurz_child_'+courseId).style.display="block";	
		}
		else {
			document.getElementById('kurz_parent_'+courseId).innerHTML = '<img src="typo3conf/ext/bergundtal_search/res/images/plus.gif" border="0" />';
			document.getElementById('kurz_child_'+courseId).style.display="none";				
		}
	}
	else {
		poststr = "KurzId="+courseId;
		makePOSTRequest("index.php?id="+pageId+"&mode=courseDetailsInfo&no_cache=1", poststr);
		document.getElementById('kurz_parent_'+courseId).innerHTML = '<img src="typo3conf/ext/bergundtal_search/res/images/minus.gif" border="0" />';
	}
}




