﻿//Asetetaan lisävarusteitten hinnat ja hinta yhteensä uudet_autot_3_malli-sivulla
function AsetaUudenAutonHintatiedot(Tunniste) {
    try { 

        CookieTieto = ReadCookie('ToyotaLaskentaCookie');
        CookieTietoTaulukko = CookieTieto.split('|');
        TunnisteCookieArvo = CookieTietoTaulukko[0];

        if (TunnisteCookieArvo == Tunniste) {
            HintaYhteensa = CookieTietoTaulukko[1];
            VarusteetYhteensa = CookieTietoTaulukko[2];
            if (VarusteetYhteensa > 0) {
                document.getElementById("ctl00_cplhOikea_lblLisavarusteetHinta").innerHTML = VarusteetYhteensa + ' &euro;';
            }
            else {
                document.getElementById("ctl00_cplhOikea_lblLisavarusteetHinta").innerHTML = '';
            }

            if (HintaYhteensa > 0) {
                document.getElementById("ctl00_cplhOikea_lblHintaYhteensa").innerHTML = formatCurrency(HintaYhteensa) + ' &euro;';
            }
            //alert(TunnisteCookieArvo + "/" + VarusteetYhteensa + "/" + HintaYhteensa);
        }

    }
    catch (e) {
        document.getElementById("ctl00_cplhOikea_lblLisavarusteetHinta").innerHTML = '';
        document.getElementById("ctl00_cplhOikea_lblLisavarusteetHinta").innerHTML = '';
        document.getElementById("ctl00_cplhOikea_lblHintaYhteensa").innerHTML = '';
    }
}

function AlustaUudenAutonHintatiedot(ToyotaLaskentaCookie) { 
    DeleteCookie(ToyotaLaskentaCookie);
    setCookie('ToyotaLaskentaCookie', '0|', 1);
}

function ReadCookie(name) {
    var cookieValue = "";
    var search = name + "=";

    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            cookieValue = unescape(document.cookie.substring(offset, end))
        }
    }
    return cookieValue;
}


function DeleteCookie(cookieName) {
    var d = new Date();
    document.cookie = cookieName + ";expires=" + d.toGMTString() + ";" + ";";
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ' ' +
num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}


function trim(str) {
    if (!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ');
}
