
var sitefx = new Class(  
{  
  zip: null,
  kwh: null,

  initialize: function() {  
    this.zip='zip';
    this.kwh='kwh-static';

	if ($chk($('send-button'))) { $('send-button').addEvent('click',this.prechange.bind(this)); }

    //if(isnull($('click-dynamic')) == false) { $('click-dynamic').addEvent('click',(function(e){
    //  e=new Event(e).stop();
    //  $('static').set('style','display:none');
	//  $('p5').set('style','display:none');
    //  $('dynamic').set('style','display:block');
    //  //this.kwh='kwh-dynamic';
    //}).bind(this)); }

    //$('click-static').addEvent('click',(function(e){
    //  e=new Event(e).stop();
    //  $('dynamic').set('style','display:none');
	//  $('p5').set('style','display:none');
    //  $('static').set('style','display:block');
    //  //this.kwh='kwh-static';
    //}).bind(this));

  },  

  change : function() {
    new Request.HTML( {
      url : 'appService.php',
      method : 'post',
      //update : $('result-inner') ,
      data : {zip: encodeURIComponent($(this.zip).value),
              kwh: encodeURIComponent($(($('dynamic').getStyle('display')=='none'?'kwh-static':'kwh-dynamic')).value)},
      evalScripts:false,
      evalResponse:false,
      onSuccess : function(tree,elements,html,js) {
        if (html==='') {
          $exec(js);
          if (obj.energy===true) {
            $('order-error').set('html','');
			if ($chk($('send-order'))) {
              $('send-order').set('href',obj.url.redirectUrl);
			}

            $('resultMonthPrice').set('html',obj.monthly+' EUR<sup>*</sup>');
            $('resultTotalPrice').set('html',obj.result+' EUR<sup>*</sup>');
            $('resultBonus').set('html',obj.bonus);
            $('resultBasePrice').set('html',obj.baseP+' EUR/Jahr');
            $('resultUsagePrice').set('html',obj.usageP+' ct/kWh');

            $('form-box').set('style','display:none');
			$('p5').set('style','display:none');
            $('result-box').set('style','display:block');

            $('order-change').addEvent('click',(function(e){
              e=new Event(e).stop();
              $('form-box').set('style','display:block');
			  $('p5').set('style','display:block');
              $('result-box').set('style','display:none');
            }).bind(this));

          } else {
            $('order-error').set('html','<span style="color:#ff0000;">'+obj.error.join('<br/>')+'</span><br/>');
          }
          //console.log(obj);
        }
      }.bind(this)
    }).send();
  },

  prechange : function(e) {
    e=new Event(e).stop();

    /*
    if ( $(this.zip).value.clean().trim() == '' || $(this.kwh).value.clean().trim() == '')
    {
      return false;
    }
    */

    this.change();

    return false;
  }

});  
   
window.addEvent('domready', function() {  
 var myfx = new sitefx();  
});  


