﻿var ajaxRequest = new AjaxRequest();
var ObjectOnOff = new ObjectOnOff();
var URL = 'http://symcon.yoome.de/';

function AjaxRequest() {
   this.xmlHttpRequest = null;
   this.callbackFunction = null;
   this.response = null;
   this.openPost = function(url, postData, callbackFunction) {
      if (this.openXMLHttpRequest()) {
         this.setCallbackFunction(callbackFunction);
         this.xmlHttpRequest.open('POST', url, true);
         this.xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
         this.xmlHttpRequest.send(postData);
      return true;
      }
   return false;
   }
   this.openGet = function(url, callbackFunction) {
      if (this.openXMLHttpRequest()) {
      // alert(this.xmlHttpRequest.readyState);
      this.setCallbackFunction(callbackFunction);
      this.xmlHttpRequest.open('GET', url, true);
      this.xmlHttpRequest.send(null);

      return true;
      }
   return false;
   }

   this.setCallbackFunction = function(callbackFunction) {
      this.callbackFunction = callbackFunction;
   }
   this.openXMLHttpRequest = function() {
      if (this.xmlHttpRequest) {
         if (this.xmlHttpRequest.readyState != 0 && this.xmlHttpRequest.readyState != 4) {
      return false;
      }
   this.xmlHttpRequest.abort();
   }
   // Internet Explorer
   try {
   // this.xmlHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
      this.xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
   }
   catch (e) {
      try {
         // this.xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
         this.xmlHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
      }
      catch (e) {
         this.xmlHttpRequest = null;
      }
   }
   // Mozilla, Opera und Safari
   if (!this.xmlHttpRequest) {
      if (typeof XMLHttpRequest != 'undefined') {
         this.xmlHttpRequest = new XMLHttpRequest();
         if (this.xmlHttpRequest.overrideMimeType) {
            this.xmlHttpRequest.overrideMimeType('text/xml');
         }
      }
      else {
      return false;
      }
   }
   this.xmlHttpRequest.onreadystatechange = this.handleResponse;
   return true;
   }
   this.handleResponse = function() {
      if (ajaxRequest.xmlHttpRequest.readyState == 4) {
         if (ajaxRequest.xmlHttpRequest.status != 200) {
            alert('debug: ajax error');
         }
         else if (ajaxRequest.callbackFunction) {
            ajaxRequest.response = ajaxRequest.xmlHttpRequest.responseXML;
            eval(ajaxRequest.callbackFunction);
         }
         else if (ajaxRequest.xmlHttpRequest.responseText != '') {
          //  alert(ajaxRequest.xmlHttpRequest.responseText);
         }
      }  
   }
}

function ObjectOnOff() {

   this.init = function(ONo) {
      this.ObjectId = ONo;
      var icon = document.getElementById(ONo);
      if (icon) {
         icon.name = ONo;
         icon.onclick = function() { ObjectOnOff.OnOff(parseInt(this.name)); }
      }
   }
   this.OnOff = function(ONo) {
      var icon = document.getElementById(ONo);
      var zahl = Math.round(Math.random() * 350);
      var link = URL+'set.status.object.php?id='+this.ObjectId+'pi='+zahl;
      if (ajaxRequest.openGet(link)) {
         var suche = icon.src.search(/schalteron.+/);
         if (suche != -1) icon.src = icon.src.replace(/schalteron/, 'schalteroff');
         else icon.src = icon.src.replace(/schalteroff/, 'schalteron');
      }
   }
} 
