

/*************************************************************************************************************
 * Classes
 ************************************************************************************************************/ 

if( !Tip ) var Tip = Tip || { };

var ToolTip = Class.create(Tip, {
  initialize: function($super, element, content) {
    element = this.createHelpIcon(element);
    $super( element , content, arguments[3] );
    this.setClassNameOnElement( element, 'iconToolTip' );
    //this.setStylesOnElement(element);
  },
  setStylesOnElement: function(e) {
    $(e).setStyle({
      'cursor': 'help'
    });
  },
  setClassNameOnElement: function(e,className) {
    $(e).addClassName( className );
  },
  createHelpIcon: function(element) {
    this.element = $(element);
    this.spanHelpIcon = document.createElement('span');
    this.spanHelpIcon.setAttribute( 'id', this.element.id+'-helpIcon' );
    this.element.appendChild(this.spanHelpIcon);
    $(this.spanHelpIcon).addClassName( 'iconToolTip' );
    $(this.spanHelpIcon).addClassName( 'fixPng' );
    return this.spanHelpIcon;
  }
  
});

/*************************************************************************************************************
 * API
 ************************************************************************************************************/ 

var API = {
  version: '1.0.0',
  date:    '2008-03-30',
  defaultErrorText: '<div class="error">Chyba: data ze serveru nejsou dostupná!</div>',
  console: null,
  debug: false,
    
  init: function() {
    API.debug = API.Utils.parseURL('debug') || false;
    if( API.debug ) {
      if( API.Console ) this.console = new API.Console();
      else API.debug = false;
    };    
    if( API.debug ) API.log('API.init.start');
    if( API.debug ) this.eventObserve();
    
    document.observe("api:ajaxLoaded", function(event) {
      if( API.debug ) API.log('event:API.init:ajaxLoaded.start');
      API.loadByLayout();
      if( API.debug ) API.log('event:API.init:ajaxLoaded.end');
    });
    
  },
  
  run: function() {
    try {
      this.init();
      this.load();
    }catch(e) {
      if( API.debug ) API.log('<span style="font-weight:900;color: red;font-size:11px;">ERROR:'+'</span>'
                              +'<br /><span style="font-weight:900;margin:0px 5px 0px 20px;font-size:11px;">message:</span><span>'+e.message+'</span>'
                              +'<br /><span style="font-weight:900;margin:0px 5px 0px 20px;font-size:11px;">lineNumber:</span><span>'+e.lineNumber+'</span>'
                              +'<br /><span style="font-weight:900;margin:0px 5px 0px 20px;font-size:11px;">fileName:</span><span>'+e.fileName+'</span>'
                             );
      else alert('ERROR:\n'+'\n  message:'+e.message+'\n  lineNumber:'+e.lineNumber+'\n  fileName:'+e.fileName);
    }
  },
  
  throwError: function(e) {
    throw e;    
  },
  
  load: function() {
    if( API.debug ) API.log('API.load');
    this.AJAX.load();
    API.Validation.init();
    this.loadByLayout();
    if( API.debug ) API.log('API.load.end');
  },
  
  loadByLayout: function() {
    
    if( API.debug ) API.log('API.loadByLayout.start');
    if( $('meraky-pis') ) {
      API.MerakyPIS.load();
    }
    if( API.debug ) API.log('API.loadByLayout.end');
  },

  log: function(text) {
    if( API.debug ) this.console.log(text);    
  },
  
  eventObserve: function() {
    Event.observe(document, 'click', (function(event){
      //var e = event.element();
      //var log = 'Event >> e:'+e.tagName.toLowerCase()+', id:'+e.id;
      //this.log(log);
    }).bind(this));
  }  
      
};

/*************************************************************************************************************
 * API / CSS
 */ 
 
API.CSS = {
  classNames: {
    hidden: 'hidden',
    formEmptyField: 'form-empty-field',
    formFieldFocus: 'form-field-focus'    
  }
}

/*************************************************************************************************************
 * API / Console
 */ 

API.Console = Class.create({
  counter: 0,
  number: 0,
  initialize: function(options) {
    this.options = {
      consoleId: 'console',
      consoleStyles: { position: 'absolute', width: (document.body.clientWidth-5)+'px', top: '0px', left: '0px', backgroundColor: '#efefef', color: '#404040', border: '3px solid #404040', zIndex: 1000, textAlign: 'left' },
      consoleLogStyles: { fontSize: '10px', lineHeight: '12px' }
    };
    Object.extend(this.options, options || { });
    
    this.init();
    
    return this;
  },

  init: function() {
    this.createConsole();
    this.eventObserve();
    this.setTooltips();
  },
  
  setTooltips: function() {

  },

  createConsole: function() {
    ++this.number;
    var body = $A(document.getElementsByTagName('body')).first();
    this.console = new Element('div', { 'class': 'console', 'id': this.options.consoleId+'-'+this.number } );
    this.console.setStyle(this.options.consoleStyles);
    var consoleTitle = new Element('div', { 'class': 'consoleTitle'}).setStyle({ fontWeight: 'bold', cursor: 'hand', cursor: 'pointer', height: '15px', backgroundColor: '#404040', color: '#efefef' }).update('LOG CONSOLE');
    Element.insert( this.console, { top: consoleTitle });
    consoleTitle.observe('click', (function(event){
      $A(event.element().parentNode.getElementsByClassName('consoleBody')).first().toggleClassName('hidden');
    }));
    this.consoleBody = new Element('div', { 'class': 'consoleBody hidden' }).setStyle({ overflow: 'auto', 'height': '150px', margin: '3px' });
    Element.insert( this.console, { bottom: this.consoleBody });
    Element.insert( body, { top: this.console });
    this.log('API.Console created...');
  },

  log: function(content) {
    this.counter++;
    var log = new Element('div', { id: 'console-log-'+this.counter });
    log = $(log);
    log.update(this.counter+': '+content);
    log.setStyle(this.options.consoleLogStyles);
    Element.insert(this.consoleBody, { bottom: log });
  },
  
  eventObserve: function() {
    Event.observe(window, 'scroll', (function(){
      //this.console.setStyle( {top: this.console.cumulativeOffset()+'px' } );
      //this.log('this.console.viewportOffset:'+this.console.viewportOffset().top+', top:'+this.console.getStyle('top')+', +cumulativeOffset:'+this.console.cumulativeOffset().top+'px');
    }).bind(this));
    Event.observe(window, 'resize', (function(){
      //this.console.setStyle( {top: this.console.cumulativeOffset().top+'px' } );
      //this.log('this.console.viewportOffset:'+this.console.viewportOffset().top+', top:'+this.console.getStyle('top')+', +cumulativeOffset:'+this.console.cumulativeOffset().top+'px');
    }).bind(this));
  }
  
});

/*************************************************************************************************************
 * API / Utils
 */ 

API.Utils = {
  counter: 0,

  changeClassName: function(e,fromClassName,toClassName)
  {
    if( API.debug ) API.log('API.Utils.changeClassName');
    var e = $(e) || API.throwError('element neexistuje');
    if( !e.hasClassName(fromClassName) ) return false;
    else e.removeClassName(fromClassName);
    e.addClassName(toClassName);
    return true;
  },
  
  switchVisibility: function(e,showClassName,hideClassName,classNameHidden)
  {
    if( API.debug ) API.log('API.Utils.switchVisibility');
    e = $(e) || API.throwError('element neexistuje');

    $A(e.getElementsBySelector('.'+showClassName)).each(function(el) {
        el.toggleClassName(classNameHidden);
    }.bind(this));
    
    $A(e.getElementsBySelector('.'+hideClassName)).each(function(el) {
        el.toggleClassName(classNameHidden);
    }.bind(this));    
  },
  
  parseURL: function(param)
  {
    param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+param+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
      return "";
    else
      return results[1];
  },
  
  getParentNode: function (element,tagName) {
    API.Utils.counter += 1;
    if( API.Utils.counter > 100 ) return false; //bezpecnostni vyskoceni
    var parent = element.parentNode;
    if( parent.tagName.toLowerCase() == 'body' ) return false;
    if( parent.tagName.toLowerCase() == tagName.toLowerCase() )
      return parent;
    else
      return API.Utils.getParentNode(parent,tagName);

  }
  
};

/*************************************************************************************************************
 * API / Validation
 */ 

/*
HELP:

Validation.add('class-name', 'Error message text', {
   pattern : new RegExp("^[a-zA-Z]+$","gi"), // only letter allowed
   minLength : 6, // value must be at least 6 characters
   maxLength : 13, // value must be no longer than 13 characters
   min : 5, // value is not less than this number
   max : 100, // value is not more than this number
   notOneOf : ['password', 'PASSWORD'], // value does not equal anything in this array
   oneOf : ['fish','chicken','beef'], // value must equal one of the values in this array
   is :  '5', // value is equal to this string
   isNot : 'turnip', //value is not equal to this string
   equalToField : 'password', // value is equal to the form element with this ID
   notEqualToField : 'username', // value is not equal to the form element with this ID
   include : ['validate-alphanum'] // also tests each validator included in this array of validator keys (there are no sanity checks so beware infinite loops!)
});

Validation.add('validate-alpha', 'Please use letters only (a-z) in this field.', function (v) {
     return Validation.get('IsEmpty').test(v) ||  /^[a-zA-Z]+$/.test(v)
});

Validation.addAllThese('validate-password', 'Your password must be more than 6 characters and not be 'password' or the same as your name', {
    minLength : 7,
    notOneOf : ['password','PASSWORD','1234567','0123456'],
    notEqualToField : 'username'
});

Validation.addAllThese([
     ['required', 'This is a required field.', function(v) {
       return !Validation.get('IsEmpty').test(v);
     }],
     ['validate-number', 'Please use numbers only in this field.', function(v) {
       return Validation.get('IsEmpty').test(v) || !isNaN(v);
     }],
     ['validate-digits', 'Please use numbers only in this field.', function(v) {
       return Validation.get('IsEmpty').test(v) ||  !/[^d]/.test(v);
     }]
]);

*/
;
API.Validation = {

  validator: null,
  
  init: function ( el, options ) {
    if( API.debug ) API.log('API.Validation.init');
    this.options = {
        onSubmit:           true,
        stopOnFirst:        true,
        immediate :         true,
        focusOnError :      false,
        useTitles :         true,
        onFormValidate :    this.validateCallback.bind(this),
        onElementValidate : (function () { })
    };

    Object.extend(this.options, options || { });
    this.setAllThese();
  },

  validateForm: function (formEl,options) {
    if( API.debug ) API.log('API.Validation.validateForm');
    var validator = new Validation( formEl, options );
    return validator;
  },

  validate: function (el,options) {
    if( API.debug ) API.log('API.Validation.validate');
    var result = Validation.validate( el, options );
    return result;
  },

  validateCallback: function (result, form) {
    if( API.debug ) API.log('API.Validation.validateCallback');
  },
  
  setAllThese: function() {
    if( API.debug ) API.log('API.Validation.setAllThese');
    Validation.add('validate-customer-email', 'E-mail je uveden ve špatném formátu. Správný formát je např.: neco@domena.cz .', {
         include : ['validate-email']
    });
    Validation.add('validate-sazba', 'Prosím zvolte Vaší sazbu za elektrickou energii.', {
         minLength : 3,
         notOneOf : ['sazba...']
    });
    Validation.add('validate-vyrobce', 'Prosím zvolte výrobce Vašeho spotřebiče.', {
         minLength : 3,
         notOneOf : ['vyberte...']
    });
    Validation.add('validate-typ-vyrobku', 'Prosím zvolte typ Vašeho výrobku.', {
         minLength : 3,
         notOneOf : ['vyberte...']
    });
    Validation.add('validate-stitkova-hodnota', 'Prosím zadejte štítkovou hodnotu Vašeho výrobku.', {
         minLength : 1,
         notOneOf : ['štítek...']
    });        
    Validation.add('validate-one-of-more', 'Prosím vyberte jednu z možností.', function (v,elm) {
        var poolName = elm.classNames().grep(/pool\-(.*)/);
        var elements = $A($$('input.'+poolName));
				return elements.any(function(e) {
				  if( $F(e) ) {
				    elements.each(function(e) { Validation.reset(e); });
					  return $F(e);
					}
				});
				
		});
        
  }
  
};


/*************************************************************************************************************
 * API / DatePicker
 */ 

API.DatePicker = {

  loadAllDatePickers: function () {
    if( datePickerController ) {
      if( API.debug ) API.log('API.DatePicker.loadAllDatePickers');
      datePickerController.create();
    }
  }
  
};



/*************************************************************************************************************
 * API / AJAX
 */ 
API.AJAX = {

  elements: { 
              loader: 'ajax-loader',
              autocompleter: 'autocompleteChoices'
            },

  load: function() {
    if( API.debug ) API.log('API.AJAX.load');
    this.registerEvents();
  },
  
  registerEvents: function() {
    if( API.debug ) API.log('API.AJAX.registerEvents');
    Ajax.Responders.register({
      onCreate: function() {
        API.AJAX.showLoader();
        Ajax.activeRequestCount++;
      },
      onLoaded: function() {
    
      },
      onInteractive: function() {

      },
      onLoading: function() {

      },
      onUninitialized: function() {
        //showError('Chyba v komunikaci se serverem!');
        Ajax.activeRequestCount--;
      },
      onSuccess: function() {

      },
      onComplete: function() {
        API.AJAX.hideLoader();
        Ajax.activeRequestCount--;
      },
      onException: function() {
        //showError('Chyba v komunikaci se serverem!');
        Ajax.activeRequestCount--;
      },
      onFailure: function() {
        //showError('Požadovaná stránka na serveru neexistuje!');
        Ajax.activeRequestCount--;
      }
    });

  },

  showLoader: function() {
    if( API.debug ) API.log('API.AJAX.showLoader');

  },
  
  hideLoader: function() {
    if( API.debug ) API.log('API.AJAX.hideLoader');

  }

};


/*************************************************************************************************************
 * MerakyPIS / Container
 */ 

API.MerakyPIS = {};

API.MerakyPIS = {

  loaded: false,

  load: function() {
      if( API.debug ) API.log('API.MerakyPIS.load.start');
      
      this.registerActionsOnDOMReady();
      
      this.loaded = true;
      if( API.debug ) API.log('API.MerakyPIS.load.end');
  },
  
  registerActionsOnDOMReady: function() {
    if( API.debug ) API.log('API.MerakyPIS.registerActionsOnDOMReady.start');
    
    if( $('kalkulacka-chladici') ) {
      new API.MerakyPIS.Kalkulacka.Chladici();
    }else if( $('kalkulacka-pms') ) {
      new API.MerakyPIS.Kalkulacka.PMS();
    }else if( $('kalkulacka-ohrev') ) {
      new API.MerakyPIS.Kalkulacka.Ohrev();
    }else if( $('kalkulacka-ostatni') ) {
      new API.MerakyPIS.Kalkulacka.Ostatni();
    }else if( $('kalkulacka-standby') ) {
      new API.MerakyPIS.Kalkulacka.Standby();
    }

    
    $A($$('input[type=text]')).each(function(e) {
        Event.observe( e, 'focus', (function(event){
          this.removeClassName(API.CSS.classNames.formEmptyField);
          this.addClassName(API.CSS.classNames.formFieldFocus);
        }).bind(e));
        Event.observe( e, 'blur', (function(event){
          if( e.value == '' ) this.addClassName(API.CSS.classNames.formEmptyField);
          this.removeClassName(API.CSS.classNames.formFieldFocus);
        }).bind(e));
        Event.observe( e, 'keyup', (function(event){
          this.prevedCislo(e);
        }).bind(this));
    }.bind(this));

    API.DatePicker.loadAllDatePickers();
    
    
    if( API.debug ) API.log('API.MerakyPIS.registerActionsOnDOMReady.end');
  },

  prevedCislo: function(e) {
    e.value = e.value.gsub(',', '.');
  }

};

/*************************************************************************************************************
 * MerakyPIS / Kalkulacka
 */ 

API.MerakyPIS.Kalkulacka = { };

/*************************************************************************************************************
 * MerakyPIS / Kalkulacka / Chladici
 */ 

API.MerakyPIS.Kalkulacka.Chladici = Class.create({

  initialize: function(options) {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Chladici.initialize');
    this.options = {

    };
    Object.extend(this.options, options || { });
    
    this.eventElements = { };
    
    //kontrola
    $H(this.eventElements).each(function(pair){
      this.eventElements[pair.key] = $(pair.value) || API.throwError('element neexistuje');
    }.bind(this));

    this.init();
  },

  init: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Chladici.init');
    this.registerEvents();
    this.setTooltips();
  },
  
  setTooltips: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Chladici.setTooltips');

  },
  
  registerEvents: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Chladici.registerEvents');
    this.eventHandlerOnDomReady();
  },
  
  eventHandlerOnDomReady: function() {

    var e = $('button-smazat');
    if( e ) {
      Event.observe( e, 'click', (function(event){
        var e = Event.element(event);
        var pokracovat = confirm("Chcete skutečně vymazat všechny zadané hodnoty a přejít na nové zadávání hodnot?");
        if(!pokracovat) Event.stop(event);
        
      }).bind(this));
    }
    
    if( $('krok-1') ) {

      new API.MerakyPIS.Autocompleter({
         eventElement:'kalkulackaPodtyp',
         eventType:'autocompleterDefault',
         trigger: 'kalkulackaPodtypTrigger',
         inputText: 'vyberte...',
         parameters: { ajax: 'subtyp', paramName: 'kalkulackaPodtyp' }
      });

      new API.MerakyPIS.Autocompleter({
         eventElement:'sazbaVyber',
         eventType:'autocompleterDefault',
         trigger: 'sazbaVyberTrigger',
         inputText: 'sazba...',
         parameters: { ajax: 'sazby', paramName: 'sazbaVyber' }
      });
      
      Event.observe( 'krok-1-button-prepocitej', 'click', (function(event){
        Event.stop(event);
        var uel = $('prehled-container');
        new Ajax.Updater({ success: uel }, API.MerakyPIS.AJAX.url , {
          method: 'post',
          parameters: $('merakyPISVypocet').serialize() +'&ajax=prehled' ,
          onComplete: function(){
            var e = $('navigace-krok-dalsi');
            var f = 'merakyPISVypocet';
            var r = false;

            if(
                Validation.get('validate-sazba').test( $F('sazbaVyber') ) &&
                Validation.get('required').test( $F('namerenaSpotreba') ) &&
                Validation.get('required').test( $F('dobaMereni') ) &&
                ( $F('dobaMereniTypDen') || $F('dobaMereniTypHod') )            
              ) { r = true; }
            if( r ) {
              if( e.hasClassName(API.CSS.classNames.hidden) ) e.removeClassName(API.CSS.classNames.hidden);
            }else {
              if( !e.hasClassName(API.CSS.classNames.hidden) ) e.addClassName(API.CSS.classNames.hidden);
            }
          }
        });        
        
      }).bind(this));

    }else if( $('krok-2') ) {
      
      new API.MerakyPIS.Autocompleter({
         eventElement:'spotrebicZadatVyrobce',
         eventType:'autocompleterDefault',
         trigger: 'spotrebicZadatVyrobceTrigger',
         inputText: 'vyberte...',
         parameters: { ajax: 'vyrobci', paramName: 'vyrobci' }
      });

      new API.MerakyPIS.Autocompleter({
         eventElement:'spotrebicZadatTypVyrobku',
         eventType:'autocompleterDefault',
         trigger: 'spotrebicZadatTypVyrobkuTrigger',
         inputText: 'vyberte...',
         parameters: { ajax: 'typVyrobku', paramName: 'spotrebicZadatTypVyrobku' }
      });
      
      new API.MerakyPIS.Autocompleter({
         eventElement:'spotrebicZadatStitkovaHodnota',
         eventType:'autocompleterDefault',
         trigger: 'spotrebicZadatStitkovaHodnotaTrigger',
         inputText: 'štítek...',
         parameters: { ajax: 'stitky', paramName: 'spotrebicZadatStitkovaHodnota' }
      });

      Event.observe( 'krok-2-button-prepocitej', 'click', (function(event){
        Event.stop(event);
        var uel = $('porovnani-spotreby-energie-container');
        new Ajax.Updater({ success: uel, failure: 'error-container' }, API.MerakyPIS.AJAX.url , {
          method: 'post',
          parameters: $H({
                         spotrebicZadatVyrobce: $F('spotrebicZadatVyrobce'),
                         spotrebicZadatTypVyrobku: $F('spotrebicZadatTypVyrobku'),
                         spotrebicZadatRokVyroby: $F('spotrebicZadatRokVyroby'),
                         spotrebicZadatStitkovaHodnota: $F('spotrebicZadatStitkovaHodnota'),
                         spotrebicZadatObjemChladiciCasti: $F('spotrebicZadatObjemChladiciCasti'),
                         spotrebicZadatObjemMraziciCasti: $F('spotrebicZadatObjemMraziciCasti'),
                         ajax: 'porovnani'
                       }).toQueryString(),
          onComplete: function(){
            var e = $('navigace-krok-dalsi');
            var f = 'merakyPISVypocet';
            var r = false;

            if(1 || (
                Validation.get('validate-vyrobce').test( $F('spotrebicZadatVyrobce') ) &&
                Validation.get('validate-typ-vyrobku').test( $F('spotrebicZadatTypVyrobku') ) &&
                /*Validation.get('required').test( $F('spotrebicZadatRokVyroby') ) &&*/
                Validation.get('validate-stitkova-hodnota').test( $F('spotrebicZadatStitkovaHodnota') ) &&
                Validation.get('required').test( $F('spotrebicZadatObjemChladiciCasti') ) &&
                Validation.get('required').test( $F('spotrebicZadatObjemMraziciCasti') )
              )) { r = true; }
         
            if( r ) {
              if( e.hasClassName(API.CSS.classNames.hidden) ) e.removeClassName(API.CSS.classNames.hidden);
            }else {
              if( !e.hasClassName(API.CSS.classNames.hidden) ) e.addClassName(API.CSS.classNames.hidden);
            }
          }
        });        
        
      }).bind(this));
      
    }else if( $('krok-3') ) {
     
      document.onkeydown = KDown;
      //Validation.validate($('email'))
      Event.observe( 'krok-3-button-email', 'click', (function(event){
        var e = Event.element(event);
        var uel = $('info');
        Event.stop(event);
        if (Validation.validate($('email'))) {
          new Ajax.Updater({ success: uel, failure: 'error-container' }, e.href , {
            method: 'post',
            parameters: $('merakyPISVypocet').serialize(),
            onComplete: function(){
              $('email').value = '';
            }
          
          });
        }
                
        
      }).bind(this));
       
       API.Validation.validateForm('merakyPISVypocet');
       
    }
    
    API.Validation.validateForm('merakyPISVypocet', {stopOnFirst:true, immediate : true} );
    
  }
  
});

function KDown(e) 
{
  var KeyID = (window.event) ? event.keyCode : e.keyCode;
  
  if (KeyID ==13 && Validation.validate($('email'))) 
  {
    var uel = $('info');
    // MSIE7 fix
    $('krok-3-button-email').focus();
    //--
    //$('krok-3-button-email').fire('click',{});
    new Ajax.Updater({ success: uel, failure: 'error-container' }, $('krok-3-button-email').href , {
          method: 'post',
          parameters: $('merakyPISVypocet').serialize(),
          onComplete: function(){
              $('email').value = '';
          }
        });
     pauza(500);   
  }

}
function pauza(mils) 
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); } 
while(curDate-date < mils);
}

/*************************************************************************************************************
 * MerakyPIS / Kalkulacka / PMS
 */ 

API.MerakyPIS.Kalkulacka.PMS = Class.create({

  initialize: function(options) {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.PMS.initialize');
    this.options = {

    };
    Object.extend(this.options, options || { });
    
    this.eventElements = { };
    
    //kontrola
    $H(this.eventElements).each(function(pair){
      this.eventElements[pair.key] = $(pair.value) || API.throwError('element neexistuje');
    }.bind(this));

    this.init();
  },

  init: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.PMS.init');
    this.registerEvents();
    this.setTooltips();
  },
  
  setTooltips: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.PMS.setTooltips');

  },
  
  registerEvents: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.PMS.registerEvents');
    this.eventHandlerOnDomReady();
  },
  
  eventHandlerOnDomReady: function() {

    var e = $('button-smazat');
    if( e ) {
      Event.observe( e, 'click', (function(event){
        var e = Event.element(event);
        var pokracovat = confirm("Chcete skutečně vymazat všechny zadané hodnoty a přejít na nové zadávání hodnot?");
        if(!pokracovat) Event.stop(event);
        
      }).bind(this));
    }
    
    if( $('krok-1') ) {

      new API.MerakyPIS.Autocompleter({
         eventElement:'kalkulackaPodtyp',
         eventType:'autocompleterDefault',
         trigger: 'kalkulackaPodtypTrigger',
         inputText: 'vyberte...',
         parameters: { ajax: 'subtyp', paramName: 'kalkulackaPodtyp' }
      });

      new API.MerakyPIS.Autocompleter({
         eventElement:'sazbaVyber',
         eventType:'autocompleterDefault',
         trigger: 'sazbaVyberTrigger',
         inputText: 'sazba...',
         parameters: { ajax: 'sazby', paramName: 'sazbaVyber' }
      });
      
      Event.observe( 'krok-1-button-prepocitej', 'click', (function(event){
        Event.stop(event);
        var uel = $('prehled-container');
        new Ajax.Updater({ success: uel }, API.MerakyPIS.AJAX.url , {
          method: 'post',
          parameters: $('merakyPISVypocet').serialize() +'&ajax=prehledPms' ,
          onComplete: function(){
            var e = $('navigace-krok-dalsi');
            var f = 'merakyPISVypocet';
            var r = false;

            if(
                Validation.get('validate-sazba').test( $F('sazbaVyber') ) &&
                Validation.get('required').test( $F('namerenaSpotreba') ) &&
                Validation.get('required').test( $F('dobaMereni') ) &&
                Validation.get('required').test( $F('pocetCykluTydne') ) &&
                Validation.get('required').test( $F('pocetCykluMesicne') )
              ) { r = true; }
            if( r ) {
              if( e.hasClassName(API.CSS.classNames.hidden) ) e.removeClassName(API.CSS.classNames.hidden);
            }else {
              if( !e.hasClassName(API.CSS.classNames.hidden) ) e.addClassName(API.CSS.classNames.hidden);
            }
          }
        });        
        
      }).bind(this));

    }else if( $('krok-2') ) {
      
      new API.MerakyPIS.Autocompleter({
         eventElement:'spotrebicZadatVyrobce',
         eventType:'autocompleterDefault',
         trigger: 'spotrebicZadatVyrobceTrigger',
         inputText: 'vyberte...',
         parameters: { ajax: 'vyrobci', paramName: 'vyrobci' }
      });

      new API.MerakyPIS.Autocompleter({
         eventElement:'spotrebicZadatTypVyrobku',
         eventType:'autocompleterDefault',
         trigger: 'spotrebicZadatTypVyrobkuTrigger',
         inputText: 'vyberte...',
         parameters: { ajax: 'typVyrobku', paramName: 'spotrebicZadatTypVyrobku' }
      });
      
      new API.MerakyPIS.Autocompleter({
         eventElement:'spotrebicZadatStitkovaHodnota',
         eventType:'autocompleterDefault',
         trigger: 'spotrebicZadatStitkovaHodnotaTrigger',
         inputText: 'štítek...',
         parameters: { ajax: 'stitky', paramName: 'spotrebicZadatStitkovaHodnota' }
      });
      
      if( $('spotrebicZadatOdvod') ) {
        new API.MerakyPIS.Autocompleter({
           eventElement:'spotrebicZadatOdvod',
           eventType:'autocompleterDefault',
           trigger: 'spotrebicZadatOdvodTrigger',
           inputText: 'vyberte...',
           parameters: { ajax: 'odvod', paramName: 'spotrebicZadatOdvod' }
        });
      }else if( $('spotrebicZadatPlneni') ) {
        new API.MerakyPIS.Autocompleter({
           eventElement:'spotrebicZadatPlneni',
           eventType:'autocompleterDefault',
           trigger: 'spotrebicZadatPlneniTrigger',
           inputText: 'vyberte...',
           parameters: { ajax: 'plneni', paramName: 'spotrebicZadatPlneni' }
        });
      }
      
      Event.observe( 'krok-2-button-prepocitej', 'click', (function(event){
        Event.stop(event);
        var uel = $('porovnani-spotreby-energie-container');
        var parameters = new Hash({ });
        parameters.set('ajax','porovnani');
        if( $('spotrebicZadatVyrobce') ) parameters.set('spotrebicZadatVyrobce',$F('spotrebicZadatVyrobce'));
        if( $('spotrebicZadatTypVyrobku') ) parameters.set('spotrebicZadatTypVyrobku',$F('spotrebicZadatTypVyrobku'));
        if( $('spotrebicZadatRokVyroby') ) parameters.set('spotrebicZadatRokVyroby',$F('spotrebicZadatRokVyroby'));
        if( $('spotrebicZadatStitkovaHodnota') ) parameters.set('spotrebicZadatStitkovaHodnota',$F('spotrebicZadatStitkovaHodnota'));
        
        if( $('spotrebicZadatPlneni') ) parameters.set('spotrebicZadatPlneni',$F('spotrebicZadatPlneni'));
        if( $('spotrebicZadatHmotnost') ) parameters.set('spotrebicZadatHmotnost',$F('spotrebicZadatHmotnost'));
        
        if( $('spotrebicZadatPocet') ) parameters.set('spotrebicZadatPocet',$F('spotrebicZadatPocet'));
        if( $('spotrebicZadatSirku') ) parameters.set('spotrebicZadatSirku',$F('spotrebicZadatSirku'));
        
        if( $('spotrebicZadatOdvod') ) parameters.set('spotrebicZadatOdvod',$F('spotrebicZadatOdvod'));
        if( $('spotrebicZadatHmotnost') ) parameters.set('spotrebicZadatHmotnost',$F('spotrebicZadatHmotnost'));
        
        new Ajax.Updater({ success: uel, failure: 'error-container' }, API.MerakyPIS.AJAX.url , {
          method: 'post',
          parameters: $H(parameters).toQueryString(),
          onComplete: function(){
            var e = $('navigace-krok-dalsi');
            var f = 'merakyPISVypocet';
            var r = false;

            var isSetAndValid = function(e,testClassName) {
                if( $(e) ) {
                  return Validation.get(testClassName).test( $F(e) );
                }
                return false;
            };

            if( 1 ||(
                isSetAndValid( 'spotrebicZadatVyrobce' , 'validate-vyrobce' ) &&
                isSetAndValid( 'spotrebicZadatTypVyrobku' , 'validate-typ-vyrobku' ) &&
                /*isSetAndValid( 'spotrebicZadatRokVyroby' , 'required' ) &&*/
                isSetAndValid( 'spotrebicZadatStitkovaHodnota' , 'validate-stitkova-hodnota' ) &&
                (
                  (
                    isSetAndValid( 'spotrebicZadatPlneni' , 'required' ) &&
                    isSetAndValid( 'spotrebicZadatHmotnost' , 'required' )
                  ) ||
                  (
                    isSetAndValid( 'spotrebicZadatPocet' , 'required' ) &&
                    isSetAndValid( 'spotrebicZadatSirku' , 'required' )
                  ) ||
                  (
                    isSetAndValid( 'spotrebicZadatOdvod' , 'required' ) &&
                    isSetAndValid( 'spotrebicZadatHmotnost' , 'required' )
                  )
                )
            )) {
              r = true;
            };

            if( r ) {
              if( e.hasClassName(API.CSS.classNames.hidden) ) e.removeClassName(API.CSS.classNames.hidden);
            }else {
              if( !e.hasClassName(API.CSS.classNames.hidden) ) e.addClassName(API.CSS.classNames.hidden);
            }
          }
        });        
        
      }).bind(this));
      
    }else if( $('krok-3') ) {
      document.onkeydown = KDown; 
      Event.observe( 'krok-3-button-email', 'click', (function(event){
        var e = Event.element(event);
        var uel = $('info');
        Event.stop(event);
        
        new Ajax.Updater({ success: uel, failure: 'error-container' }, e.href , {
          method: 'post',
          parameters: $('merakyPISVypocet').serialize(),
          onComplete: function(){

          }
        });        
        
      }).bind(this));
       
       API.Validation.validateForm('merakyPISVypocet');
       
    }

    
  }
  
});

/*************************************************************************************************************
 * MerakyPIS / Kalkulacka / Ohrev
 */ 

API.MerakyPIS.Kalkulacka.Ohrev = Class.create({

  initialize: function(options) {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Ohrev.initialize');
    this.options = {

    };
    Object.extend(this.options, options || { });
    
    this.eventElements = { kalkulacka: 'kalkulacka-ohrev' };
    //kontrola
    $H(this.eventElements).each(function(pair){
      this.eventElements[pair.key] = $(pair.value) || API.throwError('element neexistuje');
    }.bind(this));

    this.init();
  },

  init: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Ohrev.init');
    this.registerEvents();
    this.setTooltips();
  },
  
  setTooltips: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Ohrev.setTooltips');

  },
  
  registerEvents: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Ohrev.registerEvents');
    this.eventHandlerOnDomReady();
  },
  
  eventHandlerOnDomReady: function() {

  }
  
});


/*************************************************************************************************************
 * MerakyPIS / Kalkulacka / Ostatni
 */ 

API.MerakyPIS.Kalkulacka.Ostatni = Class.create({

  initialize: function(options) {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Ostatni.initialize');
    this.options = {

    };
    Object.extend(this.options, options || { });
    
    this.eventElements = { kalkulacka: 'kalkulacka-ostatni' };
    //kontrola
    $H(this.eventElements).each(function(pair){
      this.eventElements[pair.key] = $(pair.value) || API.throwError('element neexistuje');
    }.bind(this));

    this.init();
  },

  init: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Ostatni.init');
    this.registerEvents();
    this.setTooltips();
  },
  
  setTooltips: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Ostatni.setTooltips');

  },
  
  registerEvents: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Ostatni.registerEvents');
    this.eventHandlerOnDomReady();
  },
  
  eventHandlerOnDomReady: function() {
    var e = $('button-smazat');
    if( e ) {
      Event.observe( e, 'click', (function(event){
        var e = Event.element(event);
        var pokracovat = confirm("Chcete skutečně vymazat všechny zadané hodnoty a přejít na nové zadávání hodnot?");
        if(!pokracovat) Event.stop(event);
        
      }).bind(this));
    }
    
    if( $('krok-1') ) {
      /*
      new API.MerakyPIS.Autocompleter({
         eventElement:'kalkulackaPodtyp',
         eventType:'autocompleterDefault',
         trigger: 'kalkulackaPodtypTrigger',
         inputText: 'vyberte...',
         parameters: { ajax: 'subtyp', paramName: 'kalkulackaPodtyp' }
      });
      */
      new API.MerakyPIS.Autocompleter({
         eventElement:'druhSpotrebice',
         eventType:'autocompleterDefault',
         trigger: 'druhSpotrebiceTrigger',
         inputText: 'vyberte...',
         parameters: { ajax: 'ds', paramName: 'druhSpotrebice' }
      });

      new API.MerakyPIS.Autocompleter({
         eventElement:'sazbaVyber',
         eventType:'autocompleterDefault',
         trigger: 'sazbaVyberTrigger',
         inputText: 'sazba...',
         parameters: { ajax: 'sazby', paramName: 'sazbaVyber' }
      });
      
      Event.observe( 'krok-1-button-prepocitej', 'click', (function(event){
        Event.stop(event);
        var uel = $('prehled-container');
        new Ajax.Updater({ success: uel }, API.MerakyPIS.AJAX.url , {
          method: 'post',
          parameters: $('merakyPISVypocet').serialize() +'&ajax=prehled' ,
          onComplete: function(){
            var e = $('navigace-krok-dalsi');
            var f = 'merakyPISVypocet';
            var r = false;

            if(
                Validation.get('validate-sazba').test( $F('sazbaVyber') ) &&
                Validation.get('required').test( $F('namerenaSpotreba') ) &&
                Validation.get('required').test( $F('dobaMereni') ) &&
                Validation.get('required').test( $F('cykluMereni') ) &&
                ( $F('dobaMereniTypDen') || $F('dobaMereniTypHod') )          
              ) { r = true; }
            if( r ) {
              if( e.hasClassName(API.CSS.classNames.hidden) ) e.removeClassName(API.CSS.classNames.hidden);
            }else {
              if( !e.hasClassName(API.CSS.classNames.hidden) ) e.addClassName(API.CSS.classNames.hidden);
            }
          }
        });        
        
      }).bind(this));

    }else if( $('krok-2') ) {
      document.onkeydown = KDown;
      Event.observe( 'krok-3-button-email', 'click', (function(event){
        var e = Event.element(event);
        var uel = $('info');
        Event.stop(event);
        new Ajax.Updater({ success: uel, failure: 'error-container' }, e.href , {
          method: 'post',
          parameters: $('merakyPISVypocet').serialize(),
          onComplete: function(){

          }
        });        
        
      }).bind(this));
       
       API.Validation.validateForm('merakyPISVypocet');
 
    }
   
  }
  
});


/*************************************************************************************************************
 * MerakyPIS / Kalkulacka / Standby
 */ 

API.MerakyPIS.Kalkulacka.Standby = Class.create({

  initialize: function(options) {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Standby.initialize');
    this.options = {

    };
    Object.extend(this.options, options || { });
    
    this.eventElements = { kalkulacka: 'kalkulacka-standby' };
    //kontrola
    $H(this.eventElements).each(function(pair){
      this.eventElements[pair.key] = $(pair.value) || API.throwError('element neexistuje');
    }.bind(this));

    this.init();
  },

  init: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Standby.init');
    this.registerEvents();
    this.setTooltips();
  },
  
  setTooltips: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Standby.setTooltips');

  },
  
  registerEvents: function() {
    if( API.debug ) API.log('API.MerakyPIS.Kalkulacka.Standby.registerEvents');
    this.eventHandlerOnDomReady();
  },
  
  eventHandlerOnDomReady: function() {

    var e = $('button-smazat');
    if( e ) {
      Event.observe( e, 'click', (function(event){
        var e = Event.element(event);
        var pokracovat = confirm("Chcete skutečně vymazat všechny zadané hodnoty a přejít na nové zadávání hodnot?");
        if(!pokracovat) Event.stop(event);
        
      }).bind(this));
    }
    
    if( $('krok-1') ) {

      new API.MerakyPIS.Autocompleter({
         eventElement:'druhSpotrebice',
         eventType:'autocompleterDefault',
         trigger: 'druhSpotrebiceTrigger',
         inputText: 'vyberte...',
         parameters: { ajax: 'sb', paramName: 'druhSpotrebice' }
      });

      new API.MerakyPIS.Autocompleter({
         eventElement:'sazbaVyber',
         eventType:'autocompleterDefault',
         trigger: 'sazbaVyberTrigger',
         inputText: 'sazba...',
         parameters: { ajax: 'sazby', paramName: 'sazbaVyber' }
      });
      
      Event.observe( 'krok-1-button-prepocitej', 'click', (function(event){
        Event.stop(event);
        var uel = $('prehled-container');
        new Ajax.Updater({ success: uel }, API.MerakyPIS.AJAX.url , {
          method: 'post',
          parameters: $('merakyPISVypocet').serialize() +'&ajax=prehled' ,
          onComplete: function(){
            var e = $('navigace-krok-dalsi');
            var f = 'merakyPISVypocet';
            var r = false;

            if(
                Validation.get('required').test( $F('druhSpotrebice') ) &&
                Validation.get('required').test( $F('dobaPouzivani') ) &&
                ( $F('dobaPouzivaniTypDen') || $F('dobaPouzivaniTypHod') ) &&
                Validation.get('validate-sazba').test( $F('sazbaVyber') ) &&
                Validation.get('required').test( $F('namerenaSpotreba') ) &&
                Validation.get('required').test( $F('dobaMereni') ) &&
                ( $F('dobaMereniTypDen') || $F('dobaMereniTypHod') )          
              ) { r = true; }

            if( r ) {
              if( e.hasClassName(API.CSS.classNames.hidden) ) e.removeClassName(API.CSS.classNames.hidden);
            }else {
              if( !e.hasClassName(API.CSS.classNames.hidden) ) e.addClassName(API.CSS.classNames.hidden);
            }
          }
        });        
        
      }).bind(this));

    }else if( $('krok-2') ) {
      document.onkeydown = KDown;
      Event.observe( 'krok-3-button-email', 'click', (function(event){
        var e = Event.element(event);
        var uel = $('info');
        Event.stop(event);
        new Ajax.Updater({ success: uel, failure: 'error-container' }, e.href , {
          method: 'post',
          parameters: $('merakyPISVypocet').serialize(),
          onComplete: function(){

          }
        });        
        
      }).bind(this));
       
       API.Validation.validateForm('merakyPISVypocet');
 
    }

  }
  
});

/*************************************************************************************************************
 * MerakyPIS / AJAX
 */ 

API.MerakyPIS.AJAX = {

  url: window.location.href,

  getUrl: function(url) {
    if( this.url.include('?') ) url = this.url+'&'+url;
    else url = this.url+'?'+url;
    
    return url;
  }  
  
};

API.MerakyPIS.Updaters = {

  number: 0,
  elements: []
  
};

/*************************************************************************************************************
 * MerakyPIS / Autocompleter
 */

API.MerakyPIS.Autocompleter = Class.create({

  initialize: function(options) {
    if( API.debug ) API.log('API.MerakyPIS.Autocompleter.initialize => options:' + $H(options).inspect() );

    this.options = {
        classNames: {
                      formFieldFocus: API.CSS.classNames.formFieldFocus,
                      formEmptyField: API.CSS.classNames.formEmptyField
                    },
        types: { 
                 autocompleterDefault: 'autocompleterDefault'
               },
        trigger: 'NOTrigger',
        eventElement: 'NOElement',
        eventType: 'NOType',
        inputText: '...',
        defaultChoice: 'vlastní',
        minChars: 2,
        parameters:   { 
                        ajaxAction: 'NOAjaxAction',
                        paramName: null
                      }
    };

    Object.extend(this.options, options || { });

    this.eventElement = $(this.options.eventElement) || API.throwError('element neexistuje');

    this.trigger = $(this.options.trigger);
    this.eventType = this.options.eventType;
    this.isSelected = false;
 
    this.options.paramName = this.options.parameters.paramName || this.eventElement.id;
    
    this.init();
    
  },
  
  init: function() {
    this.registerEvents();
    this.loadAutocomleterByType(this.eventType);
  },
  
  registerEvents: function() {
    if(this.eventElement) {
      Event.observe( this.eventElement, 'click', (function(event){
        this.eventElement.select();
      }).bind(this));
      Event.observe( this.eventElement, 'focus', (function(event){
        this.eventElement.removeClassName(this.options.classNames.formEmptyField);
        this.eventElement.addClassName(this.options.classNames.formFieldFocus);
      }).bind(this));
      Event.observe( this.eventElement, 'blur', (function(event){
        this.eventElement.removeClassName(this.options.classNames.formFieldFocus);
        if( $(this.eventElement).value == '' || $(this.eventElement).value == this.options.defaultChoice ) {
          //$(this.eventElement).value = this.options.inputText;
          this.eventElement.addClassName(this.options.classNames.formEmptyField);
        }else {
          this.eventElement.removeClassName(this.options.classNames.formEmptyField);
        }
      }).bind(this));
      Event.observe( this.eventElement, 'mouseover', (function(event){

      }).bind(this));
      Event.observe( this.eventElement, 'mouseout', (function(event){

      }).bind(this));
      Event.observe( this.eventElement, 'keyup', (function(event){
        //this.removeReadonlyAttribute();
        if( this.eventElement.id == 'druhSpotrebice' ) {
          if($('doporucenaDobaMereni')) $('doporucenaDobaMereni').update('N/A');
        }
      }).bind(this));
      Event.observe( this.eventElement, 'keydown', (function(event){
        var e = Event.element(event);
        if( event.keyCode == Event.KEY_RETURN || event.keyCode == Event.KEY_BACKSPACE ) {
          if( e.readAttribute('readonly') ) {
            Event.stop(event);
            //alert( e.readAttribute('readonly') );
          }
        }
      }).bind(this));
     
    }
  },

  loadAutocomleterByType: function(type) {
    switch(type) {
      case 
        this.options.types.autocompleterDefault : this.autocompleterDefault();
        break;
      default : API.throwError('tento typ napovídače neexistuje');
    }
  },

  autocompleterDefault: function() {
    if( API.debug ) API.log('API.MerakyPIS.Modul.Autocompleter.autocompleterDefault');
    if(this.eventElement) {
      API.MerakyPIS.Updaters.elements[this.eventElement.id] = new Ajax.Autocompleter(this.eventElement.id, API.AJAX.elements.autocompleter, API.MerakyPIS.AJAX.getUrl( Object.toQueryString(this.options.parameters) ), {paramName: this.options.paramName, minChars: this.options.minChars, afterUpdateElement : this.afterSelected.bind(this), indicator: this.eventElement.id+'-indicator'});
      API.MerakyPIS.Updaters.elements[this.eventElement.id].baseUrl = API.MerakyPIS.Updaters.elements[this.eventElement.id].url;
      if( this.trigger ) {
        Event.observe( this.trigger, 'click', (function(event){
          Event.stop(event);
          this.eventElement.removeClassName(this.options.classNames.formEmptyField);
          this.eventElement.addClassName(this.options.classNames.formFieldFocus);
          var actualValue = this.eventElement.value;
          this.eventElement.value=this.options.inputText;
          API.MerakyPIS.Updaters.elements[this.eventElement.id].activate();
          //this.eventElement.value=actualValue;
        }).bind(this));
      }
    }
  },
  
  afterSelected: function(text, li) {

    if( API.debug ) API.log('API.MerakyPIS.Modul.Autocompleter.afterSelected => li.id:'+li.id);

    this.eventElement.value=li.innerHTML.stripTags();

    //krok 1
    if( this.eventElement.id == 'kalkulackaPodtyp' ) {
      if( $('kalkulackaPodtypId') ) $('kalkulackaPodtypId').value = li.id;
      if( $('podtypInfoText') ) {
        $A($('podtypInfoText').getElementsBySelector('.podtypInfoText')).each(function(e) {
          if( !e.hasClassName(API.CSS.classNames.hidden) ) e.addClassName(API.CSS.classNames.hidden);
        }.bind(this));
        if( $('podtypInfoText-'+li.id) ) $('podtypInfoText-'+li.id).removeClassName(API.CSS.classNames.hidden);
      }
      
      if( $F('kalkulackaPodtyp') != this.options.inputText ) {
        if(  ( !$('druhSpotrebiceId') || ( $('druhSpotrebiceId') && ( $F('kalkulackaPodtypId') && $F('druhSpotrebiceId') ) ) ) && ( $('informace-o-spotrebe') && $('informace-o-spotrebe').hasClassName(API.CSS.classNames.hidden) ) ) {
          $('informace-o-spotrebe').removeClassName(API.CSS.classNames.hidden);
        }      
      }
    }

    if( this.eventElement.id == 'druhSpotrebice' ) {
      if( $('druhSpotrebiceId') ) $('druhSpotrebiceId').value = li.id;
      if( $('doporucenaDobaMereni') ) {
        new Ajax.Updater({ success: 'doporucenaDobaMereni' }, API.MerakyPIS.AJAX.url , {
          method: 'post',
          parameters: { ajax: 'mindoba' , druhSpotrebice: $F('druhSpotrebice') },
          onComplete: function(transport){
            var response = transport.responseText || API.throwError('Při komunikaci se serverem došlo k chybě');
            if( response == 0 ) {
              $('doporucenaDobaMereni').update('N/A');
            }
          }
        });
      }

      if( $F('druhSpotrebice') != this.options.inputText ) {
        if( ( $('kalkulackaPodtypId') && $('druhSpotrebiceId') ) && ( $F('kalkulackaPodtypId') && $F('druhSpotrebiceId') ) && $('informace-o-spotrebe') && $('informace-o-spotrebe').hasClassName(API.CSS.classNames.hidden) ) {
          $('informace-o-spotrebe').removeClassName(API.CSS.classNames.hidden);
        }      
      }

    }

    //krok 2
    if( this.eventElement.id == 'spotrebicZadatVyrobce' ) {
      //this.removeReadonlyAttribute();
      API.MerakyPIS.Updaters.elements['spotrebicZadatTypVyrobku'].url = API.MerakyPIS.Updaters.elements['spotrebicZadatTypVyrobku'].baseUrl + '&vyrobce='+$F('spotrebicZadatVyrobce');
    }

    if( this.eventElement.id == 'spotrebicZadatTypVyrobku' ) {
      var data = $A(li.getElementsByClassName('informal')).first();
      this.removeReadonlyAttribute();
      
      if( data.innerHTML.isJSON() ) {
        data = data.innerHTML.evalJSON();
        $H(data).each(function(pair) {
          if( $(pair.key) ) {
            $(pair.key).value = pair.value.unescapeHTML();
            if( pair.key != this.eventElement.id ) {
              //$(pair.key).disable();
              //$(pair.key).writeAttribute({'readonly':'readonly'});
            }
          }
        }.bind(this));
      }
    }
    
    if( li.innerHTML.stripTags() == this.options.defaultChoice ) {
      this.eventElement.focus();
      this.eventElement.select();
    }else {
      this.eventElement.blur();      
    }
    
  },
  
  removeReadonlyAttribute: function() {
    var elements = ['spotrebicZadatVyrobce','spotrebicZadatTypVyrobku','spotrebicZadatRokVyroby','spotrebicZadatStitkovaHodnota','spotrebicZadatObjemChladiciCasti','spotrebicZadatObjemMraziciCasti'];
    $A(elements).each(function(e) {
      if( $(e) ) {
        if( $(e).hasAttribute('readonly') ) {
          $(e).writeAttribute({'readonly':''});
        }
      }
    }.bind(this));
  }

});


/*************************************************************************************************************
 * Page
 ************************************************************************************************************/ 


var Page = {

  load: function() {
    
    pauza(500);
    API.run();
  }

};



//Event.observe( document, 'dom:loaded' , Page.load.bind(Page) );
Event.onDOMReady( Page.load.bind(Page) );


