/**
 * @author michalaskowski
 */
ids = new Array("poww", "mieszz", "cena_gjj", "cena_mwhh", "wzrost_enn", "cena_metra", "kredytt", "wklad_wll", "oprocentowaniee", "czas_spp");
parametry = new Array("35", "1", "75", "0,27", "10", "5000", "1", "20", "7", "30");
lata = new Array("5", "10", "15", "20", "25", "30");
ekolumny = new Array("e5", "e10", "e15", "e20", "e25", "e30");
zkolumny = new Array("z5", "z10", "z15", "z20", "z25", "z30");
etabela = new Array("e5t", "e10t", "e15t", "e20t", "e25t", "e30t");
ztabela = new Array("z5t", "z10t", "z15t", "z20t", "z25t", "z30t");
ekolumnyW = new Array("0", "0", "0", "0", "0", "0");
zkolumnyW = new Array("0", "0", "0", "0", "0", "0");

function disableEnterKey(e){
    var key;
    if (window.event) 
        key = window.event.keyCode; //IE
    else 
        key = e.which; //firefox     
    return (key != 13);
}

function popup(stan){
    if (stan == 1) {
        document.getElementById("formularz").style.display = "block";
        document.getElementById("poww").style.display = "none";
        document.getElementById("mieszz").style.display = "none";
        document.getElementById("wzrost_enn").style.display = "none";
        document.getElementById("kredytt").style.display = "none";
        
    }
    else 
        if (stan == 0) {
        
            document.getElementById("formularz").style.display = "none";
            document.getElementById("poww").style.display = "inline";
            document.getElementById("mieszz").style.display = "inline";
            document.getElementById("wzrost_enn").style.display = "inline";
            document.getElementById("kredytt").style.display = "inline";
            
        }
}

function defaults(){

    var i = 0;
    for (i = 0; i <= 9; i++) {
        if (document.getElementById(ids[i])) {
            document.getElementById(ids[i]).value = parametry[i];
            //document.getElementById(ids[i]).onchange = function(){
            //oblicz();
            //}
            //document.getElementById(ids[i]).onkeyup = function(){
            //oblicz();
            //}
            idss = ids[i];
            window[idss] = document.getElementById(ids[i]).value;
        }
    }
    
}


function getvalues(){
    if (document.getElementById(ids[0])) {
        pow = document.getElementById(ids[0]).value;
    }
    if (document.getElementById(ids[1])) {
        miesz = document.getElementById(ids[1]).value;
    }
    cena_gj = document.getElementById(ids[2]).value.replace(/,/gi, '.');
    cena_mwh = document.getElementById(ids[3]).value.replace(/,/gi, '.');
    wzrost_en = document.getElementById(ids[4]).value.replace(/,/gi, '.');
    cena_1m2 = document.getElementById(ids[5]).value.replace(/,/gi, '.');
    kredyt = document.getElementById(ids[6]).value.replace(/,/gi, '.');
    wklad_wl = document.getElementById(ids[7]).value.replace(/,/gi, '.');
    oprocentowanie = document.getElementById(ids[8]).value.replace(/,/gi, '.');
    czas_sp = document.getElementById(ids[9]).value.replace(/,/gi, '.');
}



function kwh(){
    kwhh = document.getElementById(ids[2]).value.replace(',', '.');
    kwhh = new NumberFormat(kwhh * 0.0036).toFormatted();
    kwhh = kwhh.replace('.', ',');
    document.getElementById(ids[3]).value = kwhh;
}

function gj(){
    gjj = document.getElementById(ids[3]).value.replace(',', '.');
    gjj = Math.round(gjj / 0.0036);
    //gjj = gjj.replace('.', ',');
    document.getElementById(ids[2]).value = gjj;
}

function kredyt_open(){
    var value = document.getElementById("kredytt").value;
    tk = document.getElementById("table_kredyt");
    wklad_wlasny = wklad_wl / 100 * cena_1m2 * pow;
    if (wklad_wlasny < 0) {
        wklad_wlasny = 0;
    }
    wklad_wlasny_format = new NumberFormat(wklad_wlasny).toFormatted();
    wklad_wlasny_format = wklad_wlasny_format.replace('.', ',');
    document.getElementById('wklad_wlasnyy').innerHTML = wklad_wlasny_format + "  zł";
    czas_splaty = 12 * czas_sp;
    pmt(oprocentowanie, "12", czas_splaty, koszt_mieszkania - wklad_wlasny, 0);
    
    rata = pmt_value;
    suma_rat = rata * czas_splaty;
    if (czas_splaty < 0 || wklad_wl < 0 || oprocentowanie < 0) {
        rata = 0;
        suma_rat = 0;
    }
    rata = new NumberFormat(rata).toFormatted();
    rata = rata.replace('.', ',');
    document.getElementById('rataa').innerHTML = rata + "  zł";
    
    suma_rat_format = new NumberFormat(suma_rat).toFormatted();
    suma_rat_format = suma_rat_format.replace('.', ',');
    document.getElementById('suma_ratt').innerHTML = suma_rat_format + "  zł";
    //document.getElementById('procent_kredytu_show').style.display = "block";
    //document.getElementById('wykrzyknik').style.display = "inline";
    
    if (value == 1) {
        tk.style.display = "block";
    }
    else {
        tk.style.display = "none";
        //document.getElementById('wykrzyknik').style.display = "none";
        //document.getElementById('procent_kredytu_show').style.display = "none";
        //document.getElementById('procent_kredytu').innerHTML = ""
    }
}


function fv(rate, per, nper, pmt, pv){
    nper = parseFloat(nper);
    pmt = parseFloat(pmt);
    pv = parseFloat(pv);
    rate = eval((rate) / (per * 100));
    if ((pmt == 0) || (nper == 0)) {
        return (0);
    }
    if (rate == 0) {
        fv_value = -(pv + (pmt * nper));
    }
    else {
        x = Math.pow(1 + rate, nper);
        fv_value = (-pmt + x * pmt + rate * x * pv) / rate;
    }
}

function pmt(rate, per, nper, pv, fv){
    fv = parseFloat(fv);
    nper = parseFloat(nper);
    pv = parseFloat(pv);
    per = parseFloat(per);
    if ((per == 0) || (nper == 0)) {
        return (0);
    }
    rate = eval((rate) / (per * 100));
    
    if (rate == 0) {
        pmt_value = (fv + pv) / nper;
    }
    else {
        x = Math.pow(1 + rate, nper);
        pmt_value = ((rate * (fv + x * pv)) / (-1 + x));
    }
}




function ekoszt(lata, zestaw){
    ogrzewanie = pow * 12 * cena_mwh;
    fv(wzrost_en, "1", lata, ogrzewanie, "0");
    ogrzewanie = fv_value;
    woda = miesz * 50 * 365 / 1000 * 60 * cena_mwh * 0.05;
    fv(wzrost_en, "1", lata, woda, "0");
    woda = fv_value;
    ekolumnyW[zestaw] = ogrzewanie + woda;
    
}

function zkoszt(lata, zestaw){
    ogrzewanie = pow * 80 * cena_mwh;
    fv(wzrost_en, "1", lata, ogrzewanie, "0");
    ogrzewanie = fv_value;
    
    woda = miesz * 50 * 365 / 1000 * 60 * cena_mwh;
    fv(wzrost_en, "1", lata, woda, "0");
    woda = fv_value;
    zkolumnyW[zestaw] = ogrzewanie + woda;
}

function skala(){
    max_koszt = zkolumnyW[lata.length - 1];
    if (max_koszt < 150000) {
        max_skala = "150000";
        document.getElementById("csschart").style.background = "url(img/skala150.gif) no-repeat 0 0";
    }
    if (max_koszt >= 150000) {
        max_skala = "250000";
        document.getElementById("csschart").style.background = "url(img/skala250.gif) no-repeat 0 0";
    }
    if (max_koszt >= 250000) {
        max_skala = "500000";
        document.getElementById("csschart").style.background = "url(img/skala500.gif) no-repeat 0 0";
    }
    if (max_koszt >= 500000) {
        max_skala = "1000000";
        document.getElementById("csschart").style.background = "url(img/skala1000.gif) no-repeat 0 0";
    }
}



function oblicz(){
    getvalues();
    koszt_mieszkania = pow * cena_1m2;
    if (koszt_mieszkania < 0) {
        koszt_mieszkania = 0;
    }
    koszt_mieszkania_fromat = new NumberFormat(koszt_mieszkania).toFormatted();
    koszt_mieszkania_fromat = koszt_mieszkania_fromat.replace('.', ',');
    document.getElementById('koszt_mieszkaniaa').innerHTML = koszt_mieszkania_fromat + "  zł";
    
    if (cena_gj != "" && cena_mwh != "" && cena_1m2 != "") {
        var i = 0;
        for (i = 0; i <= lata.length - 1; i++) {
            lat = lata[i];
            ekoszt(lat, i);
            formatE = ekolumnyW[i];
            if (formatE < 0) {
                formatE = 0;
            }
            formatE = new NumberFormat(formatE).toFormatted();
            formatE = formatE.replace('.', ',');
            document.getElementById(etabela[i]).innerHTML = formatE;
            zkoszt(lat, i);
            formatZ = zkolumnyW[i];
            if (formatZ < 0) {
                formatZ = 0;
            }
            formatZ = new NumberFormat(formatZ).toFormatted();
            formatZ = formatZ.replace('.', ',');
            document.getElementById(ztabela[i]).innerHTML = formatZ;
            
        }
        skala();
        var i = 0;
        for (i = 0; i <= lata.length - 1; i++) {
            eprocent = Math.round(ekolumnyW[i] / max_skala * 100);
            if (eprocent < 0) {
                eprocent = 0;
            }
            if (eprocent > 100) {
                eprocent = 100;
            }
            eclass = "p" + eprocent;
            change_class(ekolumny[i], eclass);
            zprocent = Math.round(zkolumnyW[i] / max_skala * 100);
            if (zprocent < 0) {
                zprocent = 0;
            }
            if (zprocent > 100) {
                zprocent = 100;
            }
            zclass = "p" + zprocent;
            change_class(zkolumny[i], zclass);
        }
    }
    oszczednosc = zkolumnyW[lata.length - 1] - ekolumnyW[lata.length - 1];
    if (oszczednosc < 0) {
        oszczednosc = 0;
    }
    oszczednosc_format = new NumberFormat(oszczednosc).toFormatted();
    oszczednosc_format = oszczednosc_format.replace('.', ',');
    document.getElementById('suma_oszczędności').innerHTML = oszczednosc_format;
    document.getElementById('procent_kredytu').innerHTML = Math.round(oszczednosc / koszt_mieszkania * 100) + "%";
    kredyt_open();
}

function oblicz_small(){

    if (flash != 1) {
        getvalues();
    }
    if (cena_gj != "" && cena_mwh != "" && cena_1m2 != "") {
        var i = 0;
        for (i = 0; i <= lata.length - 1; i++) {
            lat = lata[i];
            ekoszt(lat, i);
            formatE = new NumberFormat(ekolumnyW[i]).toFormatted();
            zkoszt(lat, i);
            formatZ = new NumberFormat(zkolumnyW[i]).toFormatted();
        }
    }
    
    oszczednosc = zkolumnyW[lata.length - 1] - ekolumnyW[lata.length - 1];
    oszczednosc_format = new NumberFormat(oszczednosc).toFormatted();
    oszczednosc_format = oszczednosc_format.replace('.', ',');
	  
    if (document.getElementById('suma_oszczędności')) {
        document.getElementById('suma_oszczędności').innerHTML = oszczednosc_format;
    }
	  	
}





function change_class(ID, NAME){
    document.getElementById(ID).setAttribute("class", NAME);
    document.getElementById(ID).setAttribute("className", NAME);
}

function init(){
    defaults();
    getvalues();
	hash = getUrlVars();
    var1 = parseFloat(hash['pp']);
	document.getElementById(ids[0]).value = var1;
    var2 = parseFloat(hash['mm']);
	document.getElementById(ids[1]).value = var2;
    if (var1 && var2) {
        pow = var1;
        miesz = var2;
    }
    oblicz();
    kredyt_open();
}

function init_small(){
	flash = 0;
    defaults();
    getvalues();
    oblicz_small();
}

function init_flash(){
    flash = 1;
    pow = parametry[0].replace(/,/gi, '.');
    miesz = parametry[1].replace(/,/gi, '.');
    cena_gj = parametry[2].replace(/,/gi, '.');
    cena_mwh = parametry[3].replace(/,/gi, '.');
    wzrost_en = parametry[4].replace(/,/gi, '.');
    cena_1m2 = parametry[5].replace(/,/gi, '.');
    kredyt = parametry[6].replace(/,/gi, '.');
    wklad_wl = parametry[7].replace(/,/gi, '.');
    oprocentowanie = parametry[8].replace(/,/gi, '.');
    czas_sp = parametry[9].replace(/,/gi, '.');
    oblicz_small();
	
}


// Flash Javascript communication //

function getFlashMovieObject(movieName){
    if (window.document[movieName]) {
        return window.document[movieName];
    }
    if (navigator.appName.indexOf('Microsoft Internet') == -1) {
        if (document.embeds && document.embeds[movieName]) 
            return document.embeds[movieName];
    }
    else // if (navigator.appName.indexOf(”Microsoft Internet”)!=-1)
    {
        return document.getElementById(movieName);
    }
}

function SendDataToFlashMovie(){
    flashMovie = getFlashMovieObject('Towarzystwo');
    flashMovie.sendTextToFlash(oszczednosc_format);
}

function externalFunction(ppp, mmm){
    pow = ppp;
    miesz = mmm;
    oblicz_small();
    SendDataToFlashMovie();
}


// Read a page's GET URL variables and return them as an associative array.

function getUrlVars(){

    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
    
    
    
}



