/*
 * Script for calculating heat pump efficiency
 * Author: rocktech.pl
 */
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setTime(exdate.getTime()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+';path=/'+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

var counter = 0;
var licz;
var val_e;
var val_x;
var val_y;
var e;
var hr;
var os;
var zzz;
var zzx;
var water;
var sum_price=0;
document.observe("dom:loaded", function() {
    //tootltips
    $$("span.ss_help").each( function(input) {
        new Tooltip(input, {
            backgroundColor: "#FFF",
            borderColor: "#063F76",
            textColor: "#063F76",
            opacity:'100'
        });
    });
    function calc_heat(base,step,number){
        return (step*parseInt(number))+base;
    }
    function get_chart1(e,os){

        $('barchart').update(
            new Element('img', {
                width:'470',
                height:'230',
                src: '/calc/graph1/'+Math.ceil(e)+'.'+Math.ceil(os)+'.png',
                alt: 'Porównanie kosztów ogrzewania'
            })
            );
    }
    function get_chart2(){
        $('barchart2').update(
            new Element('img', {
                width:'470',
                height:'230',
                src: '/calc/graph2/'+Math.ceil(val_e)+'.'+Math.ceil(compare)+'.'+ global_price+'.png',
                alt: 'Op\u0142acalno\u015b\u0107 i okres zwrotu inwestycji w pomp\u0119 ciep\u0142a'
            })
            );
        $('link2').value='<a href="'+BASEURI+'kalkulator-ceny-pompy-ciepla-i-koszty-ogrzewania?'+$('calc').serialize()+'"><img src="'+BASEURI+'calc/graph2/'+parseInt(val_e)+'.'+compare+'.'+global_price+'.png" alt="Pompa ciep\u0142a op\u0142acalno\u015b\u0107, Ceny pompy ciep\u0142a" /></a><a href="'+BASEURI+'kalkulator?'+$('calc').serialize()+'">Kliknij aby zobaczy\u0107 więcej</a>';
        $('link1').value='<a href="'+BASEURI+'kalkulator-ceny-pompy-ciepla-i-koszty-ogrzewania?'+$('calc').serialize()+'"><img src="'+BASEURI+'calc/graph1/'+parseInt(val_e)+'.'+compare+'.'+global_price+'.png" alt="Porównanie kosztów ogrzewania" /></a><a href="'+BASEURI+'kalkulator?'+$('calc').serialize()+'">Kliknij aby zobaczy\u0107 więcej</a>';
    }
    function calc_heat2(hr){
        switch(hr){
            case '0':
                return 1.8;
                break;
            case '1':
                return 2.1;
                break;
            case '2':
                return 2.4;
                break;
        }
    }
    function can_count(elem){
        a=sanitizeInt($F(elem));
        if(a+''!=$F(elem)){
            $(elem).value=a;
            return false;
        }else{
            return true;
        }
    }
    function can_count2(elem){
        a=sanitizeFloat($F(elem));
        if(a+''!=$F(elem)){
            $(elem).value=a;
            return false;
        }else{
            if($F(elem)=='0'){
                $(elem).value='';
                return false;
            }
            return true;
        }
    }
    function validateInt(arg){
        //fast prototype i think its sanitize more equivalent
        if(!isNaN(arg) && !/^\s+$/.test(arg) && !arg.blank()) {
            return arg;
        }else{
            return 0;
        }
    }
    function sanitizeInt(arg){
        a=arg.replace('/.*(\d+).*/i',"$1");
        a=parseInt(a,10);
        return isNaN(a) ? 0 : a;
    }
    function sanitizeFloat(arg){
        a=arg.replace(',','.').replace('/.*([\d\.]+).*/ig',"$1");
        //a=parseFloat(a);
        return isNaN(parseFloat(a)) ? '' : a;
    }
    function modifyURI () {
        /*this all what u can do for security reason that cannot be modified (without reloading the page) just like input type file has no value :) */
        var new_url=BASEURI+'kalkulator-ceny-pompy-ciepla-i-koszty-ogrzewania?'+$('calc').serialize()+'#pompa';
        window.location.replace(new_url);
        return false;
    }
    function count(){

        if($F('wattage').blank() || $F('wattage')==0){
            licz=true;
        }else{
            licz=false;
        }
        water=$F('heat_water');
        os=parseInt($F('habitants'));

        if($F('habitants')=='' && water=='true'){
            $F('habitants').value=0;
            os=0;
        }
        if(water=='true'){
            val_y=os*50;
            if(val_y<=200){
                val_y=200;
            }else{
                val_y=Math.ceil(val_y);
            }
            $('val_y').innerHTML=val_y;
        }else{
            $('val_y').innerHTML='-';
            os=0;
        }
        hr=$F('heat_req');
        if(licz){
            var m=parseInt($F('area'))+parseInt($F('extra_area'));
            switch($F('build_year')){
                case '1999':
                    val_x=(m*50);
                    if(water=='true') val_x+=os*250;
                    val_e=m*calc_heat(90,15,hr)
                    if(water=='true') val_e+=(os*1250);
                    break;
                case '1997':
                    val_x=(m*75);
                    if(water=='true') val_x+=os*250;
                    val_e=m*calc_heat(120,20,hr);
                    if(water=='true') val_e+=(os*1250);
                    break;
                case '1992':
                    val_x=(m*100);
                    if(water=='true') val_x+=os*250;
                    val_e=m*calc_heat(160,20,hr);
                    if(water=='true') val_e+=(os*1250);
                    break;
                case '1984':
                    val_x=(m*125);
                    if(water=='true') val_x+=os*250;
                    val_e=m*calc_heat(240,20,hr);
                    if(water=='true') val_e+=(os*1250);
                    break;
            }
            switch($F('window_size')){
                case 's':
                    val_x=val_x-Math.ceil(val_x*0.1);
                    val_e=val_e-Math.ceil(val_e*0.1);
                    break;
                case 'l':
                    val_x=val_x+Math.ceil(val_x*0.1);
                    val_e=val_e+Math.ceil(val_e*0.1);
                    break;
            }
            $('val_x').innerHTML='<a href="/products/offer/'+val_x+'">Poka\u017c oferty</a>';
            $('val_e').innerHTML=val_e;
        //for pomp offer
        }else{
            val_x=parseFloat($F('wattage').replace(',','.'))*1000;
            zzz=calc_heat2(hr);
            zzx=1250*os;
            val_e=(zzz*val_x);
            val_e=Math.ceil(zzx+val_e);
            val_x+=os*250;

            $('val_x').innerHTML='<a href="/products/offer/'+val_x+'">Poka\u017c oferty</a>';
            $('val_e').innerHTML=val_e;

        }

        counter++;
        $('counter').innerHTML=counter;
        compare = $RF('medium');
        get_offer(parseInt(val_x),parseInt(val_y));
        //alert(sum_price);
        get_chart1(val_e,os);
	
        //get_chart2(val_e,compare);
        //get_chart2();
        setTimeout(get_chart2,3000);
        //$('area').setValue(sanitizeInt($F('area')));
        //$('extra_area').value=sanitizeInt($F('extra_area'));
        value=Math.round(parseFloat(val_x/10))/100;
        $('demand').innerHTML=value+' kW';
        $('link').value=BASEURI+'kalkulator-ceny-pompy-ciepla-i-koszty-ogrzewania?'+$('calc').serialize();
        // setCookie('calcStatus', $('calc').serialize(true).toJSONString());
       setCookie('calcStatus',encodeURI($('calc').serialize()),3600000);
    }

    new Form.Element.Observer('isolation', 0.2, function(){
        if($('isolation').checked == true){
            $('build_year').value='1999';
            $('build_year').disable();
        }else{
            $('build_year').enable();
        }

    });
    new Form.Element.Observer('wattage', 0.2, function(){
        a=$F('wattage').blank();
        b=$F('heat_water');
        if(!a){
            $('area').disable();
            $('extra_area').disable();
            $('build_year').disable();
            $('heat_req').enable();
            $('window_size').disable();
            $('habitants').enable();
            $('isolation').disable();
            $('heat_water').enable();
            if(b!='true'){
                $('habitants').disable();
                $('habitants').value='';
            }
        }else{
            $('area').enable();
            $('extra_area').enable();
            $('build_year').enable();
            $('heat_req').enable();
            $('window_size').enable();
            $('habitants').enable();
            $('isolation').enable();
            $('heat_water').enable();
        }
    });
    new Form.Element.Observer('heat_water', 0.2, function(){
        if($F('heat_water')!='true'){
            $('habitants').disable();
            $('habitants').value='0';
        }else{
            $('habitants').enable();
            $('habitants').value='0';
        }
    });

    /**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
    function $RF(el, radioGroup) {
        if($(el).type && $(el).type.toLowerCase() == 'radio') {
            var radioGroup = $(el).name;
            var el = $(el).form;
        } else if ($(el).tagName.toLowerCase() != 'form') {
            return false;
        }

        var checked = $(el).getInputs('radio', radioGroup).find(
            function(re) {
                return re.checked;
            }
            );
        return (checked) ? $F(checked) : null;
    }



    /* Here we start the fun */
   
    count();
    new Form.Observer('calc', 2.0, function(el,ev){
        if(can_count('area') && can_count('extra_area') && can_count('habitants') && can_count2('wattage'))
            count();
    });
    new Form.Observer('medium', 0.2, function(){
        count();
    });
    /* for links managment */
    $$('.link').each(function(s) {
        s.observe('click', function(event){
            Event.element(event).select();
        });
    });


    $('links').observe('click', showLinks);

    function showLinks(event) {
        var element = Event.element(event);
        element.next('p').toggle();
    }





});
