/**
 * Dateiname       : module/typolight_quickfinder/javascript/secraquickfinder.js
 * Erzeugungsdatum : 11.02.2009
 * Autor           : Carsten Kube
 * Version         : 1.0
 * Letzte Akt.     : 12.02.2009 (CK)
 * 
 * (c) Copyright SECRA GmbH
 */

var secra;
if(!secra) {secra = {};}
else if(typeof secra !== "object") {throw new Error("secra ist kein Objekt!");}

if(!secra.Handler) {
  secra.Handler = {};

  // In DOM-compliant browsers, our functions are trivial wrappers around
  // addEventListener() and removeEventListener().
  if (document.addEventListener) 
  {
    secra.Handler.add = function(element, eventType, handler)
    {
      element.addEventListener(eventType, handler, false);
    };
    
    secra.Handler.remove = function(element, eventType, handler)
    {
      element.removeEventListener(eventType, handler, false);
    };
  }
  // In IE 5 and later, we use attachEvent() and detachEvent(), with a number of
  // hacks to make them compatible with addEventListener and removeEventListener.
  else if (document.attachEvent) 
  {
    secra.Handler.add = function(element, eventType, handler)
    {
      // Don't allow duplicate handler registrations
      // _find() is a private utility function defined below.
      if (secra.Handler._find(element, eventType, handler) != -1) {
        return;
      }
      // To invoke the handler function as a method of the
      // element, we've got to define this nested function and register
      // it instead of the handler function itself.
      var wrappedHandler = function(e)
      {
        if (!e) {
          e = window.event;
        }
        // Create a synthetic event object with partial compatibility
        // with DOM events.
        var event = 
        {
          _event: e, // In case we really want the IE event object
          type: e.type, // Event type
          target: e.srcElement, // Where the event happened
          currentTarget: element, // Where we're handling it
          relatedTarget: e.fromElement ? e.fromElement : e.toElement,
          eventPhase: (e.srcElement == element) ? 2 : 3,
          
          // Mouse coordinates
          clientX: e.clientX,
          clientY: e.clientY,
          screenX: e.screenX,
          screenY: e.screenY,
          
          // Key state
          altKey: e.altKey,
          ctrlKey: e.ctrlKey,
          shiftKey: e.shiftKey,
          charCode: e.keyCode,
          
          // Event management functions
          stopPropagation: function()
          {
            this._event.cancelBubble = true;
          },
          preventDefault: function()
          {
            this._event.returnValue = false;
          }
        };
        
        // Invoke the handler function as a method of the element, passing
        // the synthetic event object as its single argument.
        // Use Function.call() if defined; otherwise do a hack
        if (Function.prototype.call) {
          handler.call(element, event);
        }
        else 
        {
          // If we don't have Function.call, fake it like this
          element._currentHandler = handler;
          element._currentHandler(event);
          element._currentHandler = null;
        }
      };
      
      // Now register that nested function as our event handler.
      element.attachEvent("on" + eventType, wrappedHandler);
      
      // Now we must do some record keeping to associate the user-supplied
      // handler function and the nested function that invokes it.
      
      // We have to do this so that we can deregister the handler with the
      // remove() method and also deregister it automatically on page unload.
      
      // Store all info about this handler into an object
      var h = 
      {
        element: element,
        eventType: eventType,
        handler: handler,
        wrappedHandler: wrappedHandler
      };
      
      // Figure out what document this handler is part of.
      // If the element has no "document" property, it is not
      // a window or a document element, so it must be the document
      // object itself.
      var d = element.document || element;
      // Now get the window associated with that document
      var w = d.parentWindow;
      
      // We have to associate this handler with the window,
      // so we can remove it when the window is unloaded
      var id = secra.Handler._uid(); // Generate a unique property name
      if (!w._allHandlers) {
        w._allHandlers = {}; // Create object if needed
      }
      w._allHandlers[id] = h; // Store the handler info in this object
      // And associate the id of the handler info with this element as well
      if (!element._handlers) {
        element._handlers = [];
      }
      element._handlers.push(id);
      
      // If there is not an onunload handler associated with the window,
      // register one now.
      if (!w._onunloadHandlerRegistered) 
      {
        w._onunloadHandlerRegistered = true;
        w.attachEvent("onunload", secra.Handler._removeAllHandlers);
      }
    };
    
    secra.Handler.remove = function(element, eventType, handler)
    {
      // Find this handler in the element._handlers[] array.
      var i = secra.Handler._find(element, eventType, handler);
      if (i == -1) {
        return; // If the handler was not registered, do nothing
      }
      // Get the window of this element
      var d = element.document || element;
      var w = d.parentWindow;
      
      // Look up the unique id of this handler
      var handlerId = element._handlers[i];
      // And use that to look up the handler info
      var h = w._allHandlers[handlerId];
      // Using that info, we can detach the handler from the element
      element.detachEvent("on" + eventType, h.wrappedHandler);
      // Remove one element from the element._handlers array
      element._handlers.splice(i, 1);
      // And delete the handler info from the per-window _allHandlers object
      delete w._allHandlers[handlerId];
    };
    
    // A utility function to find a handler in the element._handlers array
    // Returns an array index or -1 if no matching handler is found
    secra.Handler._find = function(element, eventType, handler)
    {
      var handlers = element._handlers;
      if (!handlers) {
        return -1; // if no handlers registered, nothing found
      }
      // Get the window of this element
      var d = element.document || element;
      var w = d.parentWindow;
      
      // Loop through the handlers associated with this element, looking
      // for one with the right type and function.
      // We loop backward because the most recently registered handler
      // is most likely to be the first removed one.
      for (var i = handlers.length - 1; i >= 0; i--) 
      {
        var handlerId = handlers[i]; // get handler id
        var h = w._allHandlers[handlerId]; // get handler info
        // If handler info matches type and handler function, we found it.
        if (h.eventType == eventType && h.handler == handler) {
          return i;
        }
      }
      return -1; // No match found
    };
    
    secra.Handler._removeAllHandlers = function()
    {
      // This function is registered as the onunload handler with 
      // attachEvent.  This means that the this keyword refers to the
      // window in which the event occurred.
      var w = this;
      
      // Iterate through all registered handlers
      for (var id in w._allHandlers) 
      {
        // Get handler info for this handler id
        var h = w._allHandlers[id];
        // Use the info to detach the handler
        h.element.detachEvent("on" + h.eventType, h.wrappedHandler);
        // Delete the handler info from the window
        delete w._allHandlers[id];
      }
    };
    
    // Private utility to generate unique handler ids
    secra.Handler._counter = 0;
    secra.Handler._uid = function()
    {
      return "h" + secra.Handler._counter++;
    };
  }
}


if(!secra.InputFilter) {
  secra.InputFilter = function(allowedChars, id)
  {
    this.allowed = allowedChars.join("");
    this.elem = document.getElementById(id);
    if (this.elem) 
    {
      this.elem.filterObj = this;
      this.elem.onkeypress = this.filter;
    }
  };
  
  secra.InputFilter.prototype.filter = function(event)
  {
    var e = event || window.event;
    var code = e.charCode || e.keyCode;
    if (e.charCode == 0) 
    {
      return true;
    }
    if (e.ctrlKey || e.altKey) 
    {
      return true;
    }
    if (code < 32) 
    {
      return true;
    }
    var c = String.fromCharCode(code);
    if (this.filterObj.allowed.indexOf(c) != -1) 
    {
      return true;
    }
    else 
    {
      if (e.preventDefault) 
      {
        e.preventDefault();
      }
      if (e.returnValue) 
      {
        e.returnValue = false;
      }
      return false;
    }
  };
}

if(!secra.OPCookie) {
  secra.OPCookie = function(name) {
    var i;
    this.$name = name;
    var allcookies = document.cookie;
    if(allcookies == "") {
      return;
    }
    var cookies = allcookies.split(/;\s+/);    
    var cookie = null;
    for(i = 0; i < cookies.length; i++) {
      if(cookies[i].substring(0, name.length+1) == (name + "=")) {
        cookie = cookies[i];
        break;
      }
    }
  
    if(cookie == null) {
      return;
    }
    var cookieval = cookie.substring(name.length+1);
    var a = cookieval.split('&');
    for(i=0; i < a.length; i++) {
      a[i] = a[i].split(':');
    }
    for(i = 0; i < a.length; i++) {
      this[a[i][0]] = decodeURIComponent(a[i][1]);
    }
  };
  
  secra.OPCookie.prototype.store = function(daysToLive, path, domain, secure) {
    var cookieval = "";
    for(var prop in this) {
      if((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) {
        continue;
      }
      if(cookieval != "") {
        cookieval += '&';
      }
      cookieval += prop + ':' + encodeURIComponent(this[prop]);
    }
  
    var cookie = this.$name + '=' + cookieval;
    if(daysToLive || daysToLive == 0) { 
        cookie += "; max-age=" + (daysToLive*24*60*60);
    }
  
    if(path) {
      cookie += "; path=" + path;
    }
    if(domain) {
      cookie += "; domain=" + domain;
    }
    if(secure) {
      cookie += "; secure";
    }
    document.cookie = cookie;
  };
  
  secra.OPCookie.prototype.remove = function(path, domain, secure) {
    for(var prop in this) {
      if(prop.charAt(0) != '$' && typeof this[prop] != 'function') {
        delete this[prop];
      }
    }
    this.store(0, path, domain, secure);
  };
  
  secra.OPCookie.enabled = function() {
    if(navigator.cookieEnabled != undefined) {
      return navigator.cookieEnabled;
    }
    if(secra.OPCookie.enabled.cache != undefined) {
      return secra.OPCookie.enabled.cache;
    }
    document.cookie = "testcookie=test; max-age=10000";
    var cookies = document.cookie;
    if(cookies.indexOf("testcookie=test") == -1) {
      return secra.OPCookie.enabled.cache = false;
    } else {
      document.cookie = "testcookie=test; max-age=0";
      return secra.OPCookie.enabled.cache = true;
    }
  };
}

function secraquickfinder() {
  var anreise     = document.getElementById("secraanreise") || null;
  var abreise     = document.getElementById("secraabreise") || null;
  var objart      = document.getElementById("secraobjart") || null;
  var zimart      = document.getElementById("secrazimmerart") || null;
  var erwachsene  = document.getElementById("secraerwachsene") || null;
  var kinder      = document.getElementById("secrakinder") || null;
  var blockpers   = document.getElementById("secrapersonenblock") || null;
  var blockzimart = document.getElementById("secrazimartblock") || null;
  var secradatum  = document.getElementById("secradatum") || null;

  var opc = new secra.OPCookie("secraquickfinder");
  var p = location.search.split(/&|\?/), i, kv;
  var getanreise = null, getabreise = null, getobjart = null, geterwachsene = null, getkinder = null, getzimmerart = null;
  for(i=0; i<p.length; i+=1) {
    kv = p[i].split("=");
    if(p[i].indexOf("secraanreise=") !== -1) {
      getanreise = kv[1];
    } else if(p[i].indexOf("secraabreise=") !== -1) {
      getabreise = kv[1];
    } else if(p[i].indexOf("secraobjart=") !== -1) {
      getobjart = kv[1];
    } else if(p[i].indexOf("secraerwachsene=") !== -1) {
      geterwachsene = kv[1];
    } else if(p[i].indexOf("secrakinder=") !== -1) {
      getkinder = kv[1];
    } else if(p[i].indexOf("secrazimmerart=") !== -1) {
      getzimmerart = kv[1];
    }
  }

  if(objart) {
    if(opc.objart && getobjart === null) {
      objart.value = opc.objart;
    }
    opc.objart  = objart.value;
    if(blockpers && blockzimart) {
      objart.onchange = function() {
        if(this.value === "hotel") {
          blockpers.style.display = "none";
          blockzimart.style.display = "";
        } else {
          blockpers.style.display = "";
          blockzimart.style.display = "none";
        }
      }
      blockpers.style.visibility = "";
      objart.onchange();
    }
  }
  if(zimart) {
    if(opc.zimart && getobjart === null) {
      zimart.value = opc.zimart;
    }
    opc.zimart  = zimart.value;
  }
  if(anreise) {
    if(opc.anreise && anreise.value == "" && getanreise === null) {
      anreise.value = opc.anreise;
    }
    opc.anreise = anreise.value;
  }
  if(abreise) {
    if(opc.abreise && abreise.value == "" && getabreise === null) {
      abreise.value = opc.abreise;
    }
    opc.abreise = abreise.value;
  }
  if(erwachsene) {
    if(opc.erwachsene && erwachsene.value == "" && geterwachsene === null) {
      erwachsene.value = opc.erwachsene;
    }
    opc.erwachsene = erwachsene.value;
  }
  if(kinder) {
    if(opc.kinder && kinder.value == "" && getkinder === null) {
      kinder.value = opc.kinder;
    }
    opc.kinder = kinder.value;
  }
  opc.store(1, "/");
}

secra.Handler.add(window,"load", secraquickfinder);

