var tpTecnopedia;
if(!tpTecnopedia) tpTecnopedia = {};

tpTecnopedia.OfferProfileForm = function(elementNames,classNames)
{
   // Element-Names
   this.offerAppointmentTypeEltName = elementNames.offerAppointmentTypeEltName;
   this.offerRegistrationNeededEltName = elementNames.offerRegistrationNeededEltName;
   this.offerHasContactPersonEltName = elementNames.offerHasContactPersonEltName;
   this.offerHasNoLocationEltName = elementNames.offerHasNoLocationEltName;

   // Class-Names
   this.offerAppointmentDateClassName = classNames.offerAppointmentDateClassName;
   this.offerAppointmentTimeClassName = classNames.offerAppointmentTimeClassName;
   this.offerContactPersonClassName = classNames.offerContactPersonClassName;
   this.offerLocationClassName = classNames.offerLocationClassName;

   ///////////////////////////////////////////////////////////////////////////////////////////

   /**
    * Initialsiert die offerProfileForm, sodass auch bei einem Reload
    * alle Felder richtig gesetzt werden
    */
   this.init = function()
   {
      this.checkOfferAppointmentType();
      this.checkOfferRegistrationNeeded();
      this.checkOfferHasContactPerson();
      this.checkOfferHasLocation();
   }
   // End init

   ///////////////////////////////////////////////////////////////////////////////////////////

   /**
    * offerProfileForm:
    * Prüft die Checkbox für die Terminart und schaltet das display entsprechend
    */
   this.checkOfferAppointmentType = function()
   {
      var domElt;
      if(domElt = document.getElementsByName(this.offerAppointmentTypeEltName)[0])
      {
         switch(domElt.value)
         {
         case 'Ganztägig':
            setDisplayByClassName(this.offerAppointmentTimeClassName,'none',domElt.form);
            setDisplayByClassName(this.offerAppointmentDateClassName,'block',domElt.form);
            this.resetInputEltsByClassName(this.offerAppointmentTimeClassName,domElt.form);
            break;

         case 'Zeitraum':
            setDisplayByClassName(this.offerAppointmentDateClassName,'none',domElt.form);
            setDisplayByClassName(this.offerAppointmentTimeClassName,'block',domElt.form);
            this.resetInputEltsByClassName(this.offerAppointmentDateClassName,domElt.form);
            break;

         default:
            setDisplayByClassName(this.offerAppointmentDateClassName,'none',domElt.form);
            setDisplayByClassName(this.offerAppointmentTimeClassName,'none',domElt.form);
            this.resetInputEltsByClassName(this.offerAppointmentDateClassName,domElt.form);
            this.resetInputEltsByClassName(this.offerAppointmentTimeClassName,domElt.form);
            break;
         }
      }
   }
   // End checkOfferAppointmentType

   ///////////////////////////////////////////////////////////////////////////////////////////

   /**
    * offerProfileForm:
    * Prüft die Checkbox 'Anmeldung erforderlich' und schaltet das display entsprechend
    */
   this.checkOfferRegistrationNeeded = function()
   {
      var domElt;
      if(domElt = document.getElementsByName(this.offerRegistrationNeededEltName)[0])
      {
         var domEltHasContactPerson = document.getElementsByName(this.offerHasContactPersonEltName)[0];

         if(domElt.checked)
         {
            domEltHasContactPerson.checked = 'checked';
            domEltHasContactPerson.disabled = 'disabled';
            setDisplayByClassName(this.offerContactPersonClassName,'block',domElt.form);
         }
         else
         {
            domEltHasContactPerson.removeAttribute('disabled');
         }
      }
   }
   // End checkOfferRegistrationNeeded

   ///////////////////////////////////////////////////////////////////////////////////////////

   /**
    * offerProfileForm:
    * Prüft die Checkbox 'Kontakt möglich' und schaltet das display entsprechend
    */
   this.checkOfferHasContactPerson = function()
   {
      var domElt;
      if(domElt = document.getElementsByName(this.offerHasContactPersonEltName)[0])
      {
         if(domElt.disabled != 'disabled')
         {
            setDisplayByClassName(this.offerContactPersonClassName,domElt.checked?'block':'none',domElt.form);

            if(!domElt.checked)
               this.resetInputEltsByClassName(this.offerContactPersonClassName,domElt.form);
         }
      }
   }
   // End checkOfferHasContactPerson

   ///////////////////////////////////////////////////////////////////////////////////////////

   /**
    * offerProfileForm:
    * Prüft die Checkbox 'Abweichender Veranstaltungsort' und schaltet das display entsprechend
    */
   this.checkOfferHasLocation = function()
   {
      var domElt;
      if(domElt = document.getElementsByName(this.offerHasNoLocationEltName)[0])
      {
         setDisplayByClassName(this.offerLocationClassName,!domElt.checked?'block':'none',domElt.form);

         if(domElt.checked)
            this.resetInputEltsByClassName(this.offerLocationClassName,domElt.form);
      }
   }
   // End checkOfferHasAlternateLocation

   ///////////////////////////////////////////////////////////////////////////////////////////

   /**
    * offerProfileForm:
    * setzt die values aller Inputelemente, die Kinder des Elements mit der
    * angegebenen Klasse sind, zurück
    */
   this.resetInputEltsByClassName = function(className,node)
   {
      var wrappers = document.getElementsByClassName(className,node);
      for (var i = wrappers.length; i--;)
      {
         var inputElts = wrappers[i].getElementsByTagName('INPUT');
         for (var j = inputElts.length; j--;)
            inputElts[j].value = '';
      }
   }
   // End resetInputEltsByClassName

   ///////////////////////////////////////////////////////////////////////////////////////////

   this.init();
};

///////////////////////////////////////////////////////////////////////////////////////////

if(!tpTecnopedia.Finder)
    tpTecnopedia.Finder = {};

/**
 * Handles the radius switch
 */
tpTecnopedia.Finder.setRadius = function(radiusOptNode, formName, index)
{
    document.forms[formName].radius.value = radiusOptNode.firstChild.nodeValue;

    var EltList = document.getElementsByClassName('radiusOpt', document.getElementById('radiusChooser'), 'li');

    for(var i in EltList )
        EltList[i].className = 'radiusOpt';

    EltList[index].className='radiusOpt active';
    return false;
};
// End setRadius

///////////////////////////////////////////////////////////////////////////////////////////

/**
 * Resets the postcode field
 */
tpTecnopedia.Finder.emptyPostcode = function(nodeId)
{
    document.getElementById(nodeId).value = '';
    return false;
};
// End emptyPostcode


///////////////////////////////////////////////////////////////////////////////////////////

if(!tpTecnopedia.Experiment)
    tpTecnopedia.Experiment = {};

/**
 * Maps a rate to its css class
 */
tpTecnopedia.Experiment.ratedMap = {1: 'ratedonestar',
                                    2: 'ratedtwostar',
                                    3: 'ratedthreestar',
                                    4: 'ratedfourstar',
                                    5: 'ratedfivestar'};


/**
 * Simple stack, that holds the information which vote was set
 */
tpTecnopedia.Experiment.voteStack = new Array();

/**
 * Sets or unsets a vote for the given star array
 */
tpTecnopedia.Experiment.vote = function(hiddenEltId, ulId, rate)
{
    DlElt = document.getElementById('exRatings');

    if(DlElt.className != 'loggedIn')
        return;

    HiddenElt = document.getElementById(hiddenEltId);
    UlElt     = document.getElementById(ulId);

    /**
     * Check for an existing vote
     */
    if(UlElt.className.search(/rated\w+star/g) >= 0)
    {
        /**
         * If the same star was hit twice, remove the vote
         */
        if(UlElt.className.search(tpTecnopedia.Experiment.ratedMap[rate]) >= 0)
        {
            UlElt.className = UlElt.className.replace(/rated\w+star/g, '');
            HiddenElt.value = '';
            tpTecnopedia.Experiment.voteStack.pop();
        }
        else
        {
            UlElt.className = UlElt.className.replace(/rated\w+star/g, tpTecnopedia.Experiment.ratedMap[rate]);
            HiddenElt.value = rate;
        }
    }
    else
    {
        UlElt.className += ' '+ tpTecnopedia.Experiment.ratedMap[rate];
        HiddenElt.value = rate;

        tpTecnopedia.Experiment.voteStack.push(ulId);
    }

    /**
     * Get the vote link and show it in case at least one vote was set
     */
    VoteLink = document.getElementById('exVoteLink');

    if(VoteLink)
    {
        if(tpTecnopedia.Experiment.voteStack.length > 0)
            VoteLink.style.display = '';
        else
            VoteLink.style.display = 'none';
    }
};