// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

jQuery(function() {

    jQuery('#add-suggestion-line').click(function(e) {
        var suggestion = jQuery("#suggestions ul li:first").clone();
        suggestion.find('input[type="text"]').val("");
        jQuery('#suggestions ul').append(suggestion);
        suggestion.find('input[type="text"]:first').focus();
        return false;
    });

    jQuery('#show-discount-handling').toggle(function() {
        jQuery("#show-discount-handling").after('<span id="discount-handling"> <input type="text" size=8 name="discount_code" id="discount_code">  <span id="recalc-price">Recalculate price</span> </span>'); return false;
    },
    function() { jQuery("#discount-handling").remove(); return false; });


    jQuery('#recalc-price').live('click', function(e) {
        var discount_code = jQuery("#discount_code").val();
        var old_price = jQuery("#amount_1").attr("value");
        var new_price = jQuery("#amount_1").attr("value"); //set default
        var code_accepted = false
        var discount_amount = 0
        switch (discount_code) {
            case "AFR34RPG":
                new_price = old_price * 0.5;
                discount_amount = 50;
                code_accepted = true;
                break;
            case "Joburg01":
                new_price = old_price * 0.4;
                discount_amount = 60;
                code_accepted = true;
                break;
        }

        if (code_accepted == true) {
            new_price = CurrencyFormatted(new_price);
            jQuery("#order-price").attr({ innerHTML: new_price })
            jQuery("#amount_1").attr({ value: new_price })
            jQuery("#discount-handling").after('<span id="discount-code-feedback">This price includes your discount of  ' + discount_amount + '% and is exclusive of any taxes.</span>')
            jQuery("#show-discount-handling").remove();
            jQuery("#discount-handling").remove();
        }

        else {
            jQuery("#discount-handling").after('<span id="discount-code-feedback">This code is invalid or it has expired, email support@mondato.com</span>');
            jQuery("#discount-code-feedback").fadeOut(3500);
        }



        return false;
    });

    function CurrencyFormatted(amount) {
        var i = parseFloat(amount);
        if (isNaN(i)) { i = 0.00; }
        var minus = '';
        if (i < 0) { minus = '-'; }
        i = Math.abs(i);
        i = parseInt((i + .005) * 100);
        i = i / 100;
        s = new String(i);
        if (s.indexOf('.') < 0) { s += '.00'; }
        if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
        s = minus + s;
        return s;
    }
    // end of function CurrencyFormatted()


    jQuery("#suggestions").submit(function() {
        //Validate required fields
        var all_not_filled_in = 0;
        jQuery("#suggestions").find('input[type="text"]').each(function() {
            //all fields except the title field is mandatory
            if (jQuery(this).val() == "" && jQuery(this).attr("name") != "subscriber_suggestion[][title]") {
                all_not_filled_in = 1;
            }
        })
        if (all_not_filled_in == 1) {
            alert("All first names, last names and email addresses are mandatory");
            return false;
        }
    });


});
