﻿function loadEvents() {
    $('.pagePanels').empty();

    $('.sd-quote-btn').click(function () {
        var $quote = $('.sd-quote-panel');

        $quote.empty();
        $quote.prev().css('display', 'none');
        if (!$quote.hasClass('quote-panels')) {
            $quote.addClass('quote-panels');
        }
        $quote.append('<h2>Smartdiary Pricing Options</h2>');
        var r = parseFloat($('.sd-quote-count').val());
        var m = parsePrice(mPrice(r).toString());
        var mt = parsePrice((120 + mPrice(r)).toString());
        var amt = parsePrice((120 + (12 * mPrice(r))).toString());
        var a = parsePrice((parseFloat(mPrice(r)) * 11).toString());
        var at = parsePrice((120 + (mPrice(r) * 11)).toString());
        //var $q = $('<div style=\'padding:0;border:0;\'></div>');
        
        var $m = $('<div class=\'sd-quote-a\' style=\'width:48%;float:left;margin-right:4%;\'></div>');
        $m.append('<span class=\'headerLarge\'>Smartdiary Monthly</span>');
        $m.append('<hr/>');
        $m.append(createQDiv('One off installation..', '&#163;120.00'));
        $m.append(createQDiv('Monthly license fee.', '&#163;' + m));
        $m.append(createQDiv('Initial charge.', '&#163;' + mt));
        $m.append(createQDiv('Total 1st year.', '&#163;' + amt));
        var $a = $('<div class=\'sd-quote-a\' style=\'width:48%;float:left;\'></div>');
        $a.append('<span class=\'headerLarge\'>Smartdiary Annual</span>');
        $a.append('<hr/>');
        $a.append(createQDiv('One off installation..', '&#163;120.00'));
        $a.append(createQDiv('Annual license fee.', '&#163;' + a));
        $a.append(createQDiv('Initial charge.', '&#163;' + at));
        $a.append(createQDiv('Total 1st year.', '&#163;' + at));
        $quote.append($m);
        $quote.append($a);
        $em = $('<span style=\'font-size:110%\;display:block;\'>To receive your quote by email enter your address and click \'Send\'</span><input type=\'text\' style=\'width:260px;\' class=\'sd-quote-email\' />&nbsp;&nbsp;&nbsp;&nbsp;');
        $eb = $('<button class=\'sd-send-quote\' style=\'margin-left:10px;\'>Send</button>');
        $eb.css('font-size', '11.5px');
        $eb.button();
        var $val = $('<span class=\'sd-email-val\' style=\'color:red;font-size:70%;display:none;\'>* To receive your quote please enter a valid email address</span>');
        // $em.append($eb);

        // $('.sd-quote-panel').append($q);
        $quote.append('<br/><br/><hr/>');
        $quote.append($em);
        $quote.append($eb);
        $quote.append($val);

        $('.sd-send-quote').bind('click', function () {

            $('.sd-send-quote').css('display', 'none');
            $('.sd-email-val').css('display', 'none');

            var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

            var em = $('.sd-quote-email').val();

            var validated = true;

            if (em.length < 3) {
                validated = false;
                $('.sd-email-val').css('display', 'block');
            }

            else if (!filter.test(em)) {
                validated = false;
                $('.sd-email-val').css('display', 'block');
            }

            if (validated == false) {
                $('.sd-send-quote').css('display', '');
                return false;
            }

            var r = parseFloat($('.sd-quote-count').val());
            var m = parsePrice(mPrice(r).toString());
            var mt = parsePrice((120 + mPrice(r)).toString());
            var mtfy = parsePrice((120 + (12 * mPrice(r))).toString());

            var a = parsePrice((parseFloat(mPrice(r)) * 11).toString());
            var at = parsePrice((120 + (mPrice(r) * 11)).toString());

            var ev = parsePrice((parseFloat(mPrice(r)) * 3).toString());
            var evt = parsePrice((120 + (mPrice(r) * 3)).toString());

            $.post('quote.ashx?em=' + em + '&r=' + r + '&m=' + m + '&mt=' + mt + '&mtfy=' + mtfy + '&a=' + a + '&at=' + at + '&ev=' + ev + '&evt=' + evt, function (resp) {
                if (resp == "true") {
                    $('.sd-email-h2').html('Thank you, your quote has been sent to you by email');
                    $('.sd-send-quote').css('display', 'none');
                    $('.sd-quote-email').css('display', 'none');
                }
            });

        });
    });

    var options = {
        target: '#thanks',
        beforeSubmit: showRequest,
         success: showResponse,
        resetForm: true,
        url: 'quote.ashx'
    };
}

function showResponse(responseText, statusText, xhr, $form) {
    alert(responseText);
}

function showRequest(formData, jqForm, options) {

    //if (validate(formData, jqForm, options) != true)
    //    return false;
    var queryString = $.param(formData);
    return true;
}

function createQDiv(txt, val) {
    var div = $('<div></div>');
    div.css('height', '30px');
    var lbl = $('<label>' + txt + '</label>');
    lbl.css('display', 'block');
    lbl.css('width', '130px');
    lbl.css('float', 'left');
    var h2 = $('<h2 style=\'display:inline;\'>' + val + '</h2>');
    div.append(lbl);
    div.append(h2);
    return div;
}
function getQuote() {
    var m = 0;
    var a = 0;

}
function parsePrice(p) {
    var l = p.length;
    var r = p.indexOf('.');
    if (r == -1)
        return p + '.00';
    if (l - r == 1)
        return p + '00';
    if (l - r == 2)
        return p + '0';
    if (l - r == 3)
        return p;
    else {
        var objP = p.toString();
        objP = objP.substr(0, r + 3);
        return objP;
    }
}
function mPrice(r) {
    var p = 14, u11 = 6, u26 = 4.5, u51 = 3.5, u101 = 2.6, u201 = 2, u501 = 1.5, u1001 = 1.1, u2001 = 0.8;
    var price = p;
    if (r < 11) {
        price += (u11 * r); return price;
    } 
    else { price += (u11 * 10); }
    if (r < 26) {
        price += (u26 * (r - 10)); return price;
    } 
    else { price += (u26 * 15); }
    if (r < 51) {
        price += (u51 * (r - 25)); return price;
    } 
    else { price += (u51 * 25); }
    if (r < 101) {
        price += (u101 * (r - 50)); return price;
    } 
    else { price += (u101 * 50); }
    if (r < 201) {
        price += (u201 * (r - 100)); return price;
    } 
    else { price += (u201 * 100); }
    if (r < 501) {
        price += (u501 * (r - 200)); return price;
    } 
    else { price += (u501 * 300); }
    if (r < 1001) {
        price += (u1001 * (r - 500)); return price;
    } 
    else { price += (u1001 * 500); }
    if (r < 2001) {
        price += (u2001 * (r - 1000)); return price;
    } 
    else return 0;
}

