var fetching = false;
var site_url = '/';
function showLoader(field_name) {
   fetchObject(field_name).innerHTML = '<img src="http://www.hotel666.de/img/loader.gif" />';
}

function ajaxHandler() {
   this.http_request = false;
   if(window.XMLHttpRequest) { // Mozilla, Safari,...
      this.http_request = new XMLHttpRequest();
      if(this.http_request.overrideMimeType) {
         this.http_request.overrideMimeType('text/xml');
      }
   } else if(window.ActiveXObject) { // IE
      try {
         this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }

   if(!this.http_request) {
      //alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
      return false;
   }
   
   this.onreadystatechange = 
      function(rdy_func_name) {
         this.http_request.onreadystatechange = rdy_func_name;
      }
   this.send = 
      function(type, url, post_vars) {
               this.http_request.open(type, url);
               this.http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
               this.http_request.setRequestHeader("Pragma", "no-cache");
               this.http_request.setRequestHeader("Cache-Control", "must-revalidate");
               this.http_request.setRequestHeader("If-Modified-Since", document.lastModified);
               this.http_request.setRequestHeader("X_EMERGE", "XMLHttpRequest");
               this.http_request.send(post_vars);
      }
}

function setField() {
   if(ajRequest) {
      if(ajRequest.http_request.readyState == 4 && ajRequest.http_request.status == 200 && ajRequest.http_request.responseText) {
      //   fetchObject(field_name).style.top = "20px";
			var splitRep = ajRequest.http_request.responseText.split(';');
         switch(splitRep[0]) { //ggf einfach 'action=blbla in der ajax.php zurückgeben und hier per doRequest verarbeitne
            case 'accept':
               fetching = false;
					toggleLightbox();
					location.reload();
               break;
            case 'locationAddOk':
               fetching = false;
               setSelectLocation(splitRep[1]);
               break;
            default:
               fetching = false;
               fetchObject(field_name).innerHTML = ajRequest.http_request.responseText;
               fetchObject(field_name).style.display = "";
               break;
         }
      }
   }
}

function initHandler() {
   ajRequest = new ajaxHandler();
   ajRequest.onreadystatechange(setField);
}

function chkBusy() {
   if(fetching) {
      alert('processing previous request');
      return true;
   }
}

//fetching the comment post form and processing the post data
function requestCform(sec,id,type) {
   if(!chkBusy()) {
      initHandler();
      field_name = "comment_sec";
      showLoader(field_name);
      ajRequest.send("POST", site_url + "ajax.php", "action=comment_form&comment_sec="+sec+"&parent_id="+id+"&form_type="+type);
      fetching = true;
   }
}

function postCform(sec,id) {
   if(!chkBusy()) {
      comment_textx = fetchObject("comment_text").value;
      if(comment_textx.replace(/ /g, '') == '') {
         alert("Bitte Text eingeben...");
         return false;
      }
      initHandler();
      field_name = "comment_sec";
      showLoader(field_name);
      ajRequest.send("POST", site_url + "ajax.php", "action=comment_post&comment_sec="+sec+"&parent_id="+id+"&comment_text="+urlencode(comment_textx));
      fetching = true;
   }
}

function delComment(sec,id) {
   if(!chkBusy()) {
      initHandler();
      field_name = "comment_sec";
      showLoader(field_name);
      ajRequest.send("POST", site_url + "ajax.php", "action=comment_del&comment_sec="+sec+"&comment_id="+id);
      fetching = true;
   }   
}

//fetching the news archive
function requestArchive(year,month) {
   if(!chkBusy()) {
      initHandler();
      if(month == 0) {
         field_name = "news_year";
      } else {
         field_name = "news_month";
         fetchObject("head_path").innerHTML = "News Archiv - "+month_ger[month-1]+" "+year;
         /*fetchObject("icontent_box").style.display = "";*/
      }
      showLoader(field_name);
      ajRequest.send("POST", site_url + "ajax.php", "action=archive&news_year="+year+"&news_month="+month);
      fetching = true;
   }   
}
//////////CALENDAR STUFF//////////

//fetching calendar entries
function requestEvents(time) {
   if(!chkBusy()) {
      initHandler();
      field_name = "hover";
      showLoader(field_name);
      fetchObject(field_name).style.visibility = "visible";
      ajRequest.send("POST", site_url + "ajax.php", "action=events&time="+time);
      fetching = true;   
   }
}

function doRequest(postString, fieldName, tggleLightbox) {
	if(tggleLightbox) toggleLightbox();
   if(!chkBusy()) {
      initHandler();
		field_name = fieldName;
      showLoader(fieldName);
      ajRequest.send('POST', site_url + 'ajax.php', postString);
      fetching = true;
   } 
}

function sendForm(frmname) {
	frm = document.getElementById(frmname);
	var last;
	var str;
	for(var i = 0; i < frm.length; i++) {
		el = frm.elements[i];
		elname = el.name;
		if(el.name != last) {
			if(el.type == 'radio') {
				s_choice = document.getElementsByName(el.name);
				for(i=0; i<s_choice.length; i++) {
      			if(s_choice[i].checked == true) {
						str = str+'&'+el.name+'='+urlencode(s_choice[i].value);
					}
				}
			} else if(el.type == 'checkbox') {
				if(fetchObject(el.name).checked) str = str+'&'+el.name+'='+urlencode(fetchObject(el.name).value);
			} else {
				str = str+'&'+el.name+'='+urlencode(fetchObject(el.name).value);
			}
			last = el.name;
		}
	}
	//alert(str);
	doRequest(str, 'greyCont', false);

}

function setSelectLocation(location_id) {
   if(!chkBusy()) {
      initHandler();
      field_name = 'a_location_id';
      showLoader(field_name);
      ajRequest.send('POST', site_url + 'ajax.php', 'action=getSelectLocation&location_id='+location_id);
      fetching = true;

   }
   toggleLightbox();
}
//alert test
function requestAlert() {
   if(!chkBusy()) {
      initHandler();
      field_name = "alert";
      showLoader(field_name);
      ajRequest.send("POST", site_url + "ajax.php", "action=alert");
      fetching = true;
   }
}


function postSec() {
   initHandler();
   field_name = "sectest";
   ajRequest.send("POST", site_url + "ajax.php", "action=sectest&bla=lol&lol=bla");
}
