// Generiert das AJAX-Object
function getHTTPObject(){   
	var ajax;
	// Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
	if (typeof XMLHttpRequest != 'undefined') {
    		ajax = new XMLHttpRequest();
	}
	if (!ajax) {
    		// Internet Explorer 6 und älter
	if (window.ActiveXObject){
	
		try {
        		ajax = new ActiveXObject("Msxml2.XMLHTTP.6.0");
    		} catch(e) {
        		try {
            		ajax = new ActiveXObject("Microsoft.XMLHTTP");
        		} catch(e) {
            		ajax = null;
			}
    		}
	}
	}
	return ajax;
}   

function retrieveSpezialisierung(rubrikid){
	httpObject = getHTTPObject();    
	if (httpObject != null) {       
		var divname = "rubrikspez"+rubrikid;
		
		httpObject.open("GET", "/track/ajax/rubrik/"+rubrikid);        
		httpObject.onreadystatechange = function () {
        		if (httpObject.readyState == 4) {
				var divrubrikspez = document.getElementById(divname);        
				var response = httpObject.responseText;  
				eval(response);
				if (!divrubrikspez.hasChildNodes()){

					if(typeof spezifikation != 'undefined'){
					for (i in spezifikation) {
						//alert(spezifikation[i].title);
						var newCheckbox = document.createElement("input");
						newCheckbox.type = "checkbox";
						newCheckbox.name = "spezifikation"+rubrikid+"[]";
						newCheckbox.value = spezifikation[i].id;
						newCheckbox.id = "spez"+spezifikation[i].id;


						var newLabel=document.createElement("label");
						newLabel.htmlFor = "spez"+spezifikation[i].id;
						newLabel.appendChild(document.createTextNode(spezifikation[i].title));


						var newBR = document.createElement("br");
						divrubrikspez.appendChild(newCheckbox);
						divrubrikspez.appendChild(newLabel);
						divrubrikspez.appendChild(newBR);

					} 
					divrubrikspez.appendChild(newBR);

					}
				}

			}
		}
        
		httpObject.send(null);   
	}	

}

function setOutput(rub){    
	var divname = "rubrik_"+rub;
	//alert(divname);
	if(httpObject.readyState == 4){     
		var divrubrikspez = document.getElementById(divname);        
		var response = httpObject.responseText;  
		eval(response);
		
		if(typeof spezifikation != 'undefined'){
			for (i in spezifikation) {
				alert(spezifikation[i].title);
			} 

		}

		/*if(typeof response != 'undefined' && response != ""){
			var items = response.split("#");        
			var count = items.length;  
			combo.options[0] = new Option("Bundesland/Region","");       
			for (var i=1;i<(count-1);i++){            
				var options = items[i].split("_");    
				combo.options[i] = new Option(options[1],options[0]);        
			}
		}*/
	}
} 

function retrieve_search_data(ids, countmap, page, land, bundesland, landkreis, rubrik, group){

	httpObject = getHTTPObject();    
		if (httpObject != null) {  
			var param = "page=1&remote_ids[]="+ids+"&page="+page+"&land="+land+"&bundesland="+bundesland+"&rubrik="+rubrik+"&landkreis="+landkreis+"&group="+group;
			 
			httpObject.open("POST", '/track/sucheajax', true);
			httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");       
			httpObject.onreadystatechange = function () {
        			if (httpObject.readyState == 4) {
					var listitem = document.getElementById("listitem"); 
					var suchphrase = document.getElementById("suchphrase");
					var jumplist = document.getElementById("jumplist");
					var anzahl = document.getElementById("suchanzahl");         
					var response = httpObject.responseText;  
					eval(response);
					
					anzahl.innerHTML = dbgesamtanzahl + " Treffer insgesamt, <br/>";

					anzahl.innerHTML += countmap + " Treffer in diesem Kartenausschnitt gefunden";
					//suchphrase.innerHTML = "Suche nach \""+ land + bundesland + landkreis + rubrik +"\"";
					//alert(jump);
					jumplist.innerHTML = jump;					
					if(typeof list != 'undefined'){
						var str = "";
						for (i in list) {

							str += '<div class="tourbox">';
							str += '<a href="'+ list[i].link +'">'+ list[i].title +'</a>';

							str += '<table style="margin-top:4px;">';
							str += '<tr><td width="150">Distanz: '+ list[i].distance +' km</td>';
          						str += '<td>Start: '+ list[i].start +'</td>';
         						str += '</tr><tr>';	
							str += '<td>Kategorie: '+ list[i].tracktype;
							str += '<br/></td><td>Ziel: '+ list[i].ende +'</td></tr>';
							str += '</table>';
							str += '</div>';
							
						}
					listitem.innerHTML = str;

					}
					else{
						listitem.innerHTML = "<br/>Zu Ihrer Suchanfrage wurden keine Treffer gefunden<br/><br/>";
					}
					
				}
			}
        
		httpObject.send(param);

		}
}
function rate(artid, ratetype, star){
	if(artid && ratetype && ratetype > 0 && ratetype <=5 && star && star > 0 && star <= 5){
		
		httpObject = getHTTPObject();    
		if (httpObject != null) {  
			httpObject.open("GET", "/track/ajax/rate/id/"+artid+"/ratetype/"+ratetype+"/star/"+star);        
			httpObject.onreadystatechange = function () {
        			if (httpObject.readyState == 4) {
					       
					var response = httpObject.responseText;  
					eval(response);
					

					switch(ratetype){
						case 1:$("ratelandschaft").className="rating "+ratingOutput;
						var rate_count = document.getElementById("landschaftcount");
						rate_count.innerHTML = "("+ countRatings +")";
						break;
						case 2:
						$("rateanspruch").className="rating "+ratingOutput;
						var rate_count = document.getElementById("anspruchcount");
						rate_count.innerHTML = "("+ countRatings +")";
						break;
						case 3:$("ratetechnik").className="rating "+ratingOutput;
						var rate_count = document.getElementById("technikcount");
						rate_count.innerHTML = "("+ countRatings +")";
						break;
						case 4:$("ratespass").className="rating "+ratingOutput;
						var rate_count = document.getElementById("spasscount");
						rate_count.innerHTML = "("+ countRatings +")";
						break;
						case 5:$("ratebeschreibung").className="rating "+ratingOutput;
						var rate_count = document.getElementById("beschreibungcount");
						rate_count.innerHTML = "("+ countRatings +")";
						break;
					}
					
				}
			}
        
		httpObject.send(null);
		}
	}
	return false;
}

function loadUserTracks(user_id, order, sort, page) {
  if ('undefined' == typeof page) var page = 1;
  jQuery('#usertracksContainer').load('/track/ajax/user/'+user_id+'/'+order+'/'+sort+'?page='+page);
}