jQuerySignup = new function() {
    this.type = false;
    this.errors = {};
    this._timeouts = {};
    this._timeouts_delay = 100;
    this._amount = {};

    // private
    // {{{ _delayError()
    this._delayError = function(product) {
        var amount = this.getAmount(product);

        return false;
    }
    // }}
    // {{{ _getQuantityFromInput()
    /**
    * Gets quantity from the input based on the product
    *
    * @param	{String}	product
    *
    * @return	{Integer}	Quantity from input field or radio
    */

    this._getQuantityFromInput = function(product) {
        var count = 0;
        var form_el = jQuery('#Signup' + product.charAt(0).toUpperCase() + product.substr(1));
        return count;
    }

    // {{{ delay()
    this.delay = function(product, callback) {
        if (this._timeouts[product]) {
            clearTimeout(this._timeouts[product]);
        }
        this._timeouts[product] = window.setTimeout(callback, this._timeouts_delay);
    }
    // }}}
    // {{{ disableSubmit()
    this.disableSubmit = function() {
        if (!jQuery('#nextButton').hasClass('disabled')) {
            jQuery('#nextButton').addClass('disabled');
            jQuery("#nextButton").attr("disabled", true);

        }
    }
    // }}}
    // {{{ enableSubmit()
    this.enableSubmit = function() {
        jQuery('#nextButton').attr('disabled', false);
        jQuery('#nextButton').removeClass('disabled');
    }
    // }}}
    // {{{ formatCurrency()
    this.formatCurrency = function(num) {
        num = num.toString().replace(/\$|\,/g, '');
        if (isNaN(num)) {
            num = "0";
        }
        var sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        var cents = num % 100;
        num = Math.floor(num / 100).toString();
        if (cents < 10 && cents > 0) {
            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) ? '' : '-') + '&pound;' + num + (cents == 0 ? '.00' : '.' + cents));
    }
    // }}}
    // {{{ getAmount()
    this.getAmount = function(product) {
        var count = 0;

        switch (product) {
            case ONLINEMAIL:
                var onlinemail_list = [ONLINESTANDARD, ONLINEPROFESSIONAL, ONLINEENTERPRISE];
                for (var i = 0, ii = onlinemail_list.length; i < ii; i++) {
                    var tmp_count = this.getAmount(onlinemail_list[i]);
                    if (tmp_count == String(Number(tmp_count)) && tmp_count > 0) {
                        count += parseInt(tmp_count);
                    }
                }
                break;
            case EXTRASTORAGE:
                var extrastorage_list = [COMPANYSTORAGE, USERSTORAGE];
                for (var i = 0, ii = extrastorage_list.length; i < ii; i++) {
                    var tmp_count = this.getAmount(extrastorage_list[i]);
                    if (tmp_count == String(Number(tmp_count)) && tmp_count > 0) {
                        count += parseInt(tmp_count);
                    }
                }
                break;
            case APPLICATION:
                var application_list = [MICROSOFTPROJECT, MICROSOFTVISIO, PDFCREATOR];
                for (var i = 0, ii = application_list.length; i < ii; i++) {
                    var tmp_count = this.getAmount(application_list[i]);
                    if (tmp_count == String(Number(tmp_count)) && tmp_count > 0) {
                        count += parseInt(tmp_count);
                    }
                }
                break;
            case MSOFFICEAPPS:
                var msofficeapps_list = [MICROSOFTSTD, MICROSOFTPRO];
                for (var i = 0, ii = msofficeapps_list.length; i < ii; i++) {
                    var tmp_count = this.getAmount(msofficeapps_list[i]);
                    if (tmp_count == String(Number(tmp_count)) && tmp_count > 0) {
                        count += parseInt(tmp_count);
                    }
                }
                break;
            case MOBILE:
                var mobile_list = [BLACKBERRY, WINDOWS, IPHONE];
                for (var i = 0, ii = mobile_list.length; i < ii; i++) {
                    var tmp_count = this.getAmount(mobile_list[i]);
                    if (tmp_count == String(Number(tmp_count)) && tmp_count > 0) {
                        count += parseInt(tmp_count);
                    }
                }
                break;
            default:
                if (this._amount[product]) {
                    count = this._amount[product];
                } else {

                    count = this._getQuantityFromInput(product);
                    this._amount[product] = count;
                }
                break;
        }
        if (count == '') {
            count = 0;
        }
        return count;
    }

    // }}}
    // {{{ getErrorMsg()
    this.getErrorMsg = function(product) {
        var amount = this.getAmount(product);
        if (isNaN(amount) || (amount != String(Number(amount)))) {
            return 'Please enter a valid number.';
        } else {
            // The amount passed through could be a string, but the above conditions still pass
            // because of automatic type-casting. We don't want this logic in getAmount because '5%' still
            // gets parsed to '5', and we don't catch that as invalid input.
            amount = parseInt(amount);
        }
        switch (product) {

            case ONLINEBASIC:
            case ONLINESTANDARD:
            case ONLINEPROFESSIONAL:
            case ONLINESTANDARD:
            case ONLINEENTERPRISE:
                if (amount < 1) {
                    return 'Enter 1 or more mailboxes.';
                } else if (amount > ONLINEMAIL_MAX) {
                    return 'For ' + ONLINEMAIL_MAX + '+ onlinemail, call for a quote';
                }
                break;
            case MICROSOFTSTD:
            case MICROSOFTPRO:
                if (!this.isValidAmount(DESKTOPLITE)) {
                    return 'Please correct  error above.';
                } else if (this.getAmount(DESKTOPLITE) == 0) {
                    return 'Enter a number of Desktoplite above.';
                } else if (amount < 0) {
                    return 'Enter 0 or more licenses.';
                }
                break;
            case WINDOWS:
            case IPHONE:
            case BLACKBERRY:
                if (!this.isValidAmount(ONLINEBASIC) || !this.isValidAmount(ONLINESTANDARD) || !this.isValidAmount(ONLINEPROFESSIONAL) || !this.isValidAmount(ONLINEENTERPRISE)) {
                    return 'Please correct the mailbox error above.';
                } else if (this.getAmount(ONLINEMAIL) == 0) {
                    return 'Enter a number of onlinemail boxes above.';
                } else if (amount < 0) {
                    return 'Enter 0 or more licenses.';
                }
                break;
            case ONLINEMAILSTORAGE:
                var onlineTotal = this.getAmount(ONLINEMAIL) * 10;
                var onlinebasicTotal = this.getAmount(ONLINEBASIC) * 10;
                var mytotal = onlineTotal + onlinebasicTotal;

                if (!this.isValidAmount(ONLINEBASIC) || !this.isValidAmount(ONLINESTANDARD) || !this.isValidAmount(ONLINEPROFESSIONAL)
                || !this.isValidAmount(ONLINEENTERPRISE)) {
                    return 'Please correct the online mailbox error above.';
                } else if (this.getAmount(ONLINEMAIL) == 0) {
                    return 'Enter 1 or more onlinemail boxes above.';
                } else if (amount < 0) {
                    return 'Enter 0 or more GBs.';
                } else if (amount > mytotal) {
                    return 'Enter ' + mytotal + ' or fewer GBs.';
                }
                break;
            case DESKTOPLITE:
                if (amount < 1) {
                    return 'Enter 1 or more mailboxes.';
                } else if (amount > DESKTOPLITE_MAX) {
                    return 'For ' + DESKTOPLITE_MAX + '+ onlinemail, call for a quote';
                }
                break;
            case EXTRASTORAGE:
                if (!this.isValidAmount(DESKTOPLITE)) {
                    return 'Please correct the onlinemail box error above.';
                } else if (this.getAmount(EXTRASTORAGE) == 0) {
                    return 'Enter a number of onlinemail onlinemail boxes above.';
                } else {
                    return 'The combined total of all mobile cannot exceed ' + this.getAmount(EXTRASTORAGE) + '.';
                }
                break;
            case APPLICATION:
                if (!this.isValidAmount(DESKTOPLITE)) {
                    return 'Please correct the onlinemail box error above.';
                } else if (this.getAmount(EXTRASTORAGE) == 0) {
                    return 'Enter a number of onlinemail onlinemail boxes above.';
                } else {
                    return 'The combined total of all mobile cannot exceed ' + this.getAmount(EXTRASTORAGE) + '.';
                }
                break;
            case MICROSOFTPROJECT:
            case MICROSOFTVISIO:
            case PDFCREATOR:
                if (!this.isValidAmount(DESKTOPLITE)) {
                    return 'Please correct the mailbox error above.';
                } else if (this.getAmount(APPLICATION) == 0) {
                    return 'Enter a number of onlinemail boxes above.';
                } else if (amount < 0) {
                    return 'Enter 0 or more licenses.';
                } else {
                    return 'cannot exceed ' + this.getAmount(DESKTOPLITE) + '.';
                }
                break;
            case USERSTORAGE:
            case COMPANYSTORAGE:
                if (!this.isValidAmount(DESKTOPLITE)) {
                    return 'Please correct the mailbox error above.';
                } else if (this.getAmount(DESKTOPLITE) == 0) {
                    return 'Enter a number of onlinemail boxes above.';
                } else if (amount < 0) {
                    return 'Enter 0 or more storage.';
                } else if (amount > 1000) {
                    return 'Enter 1000 or fewer GBs.';
                }
                break;
            case MSOFFICEAPPS:
                if (!this.isValidAmount(DESKTOPLITE)) {
                    return 'Please correct the Desktoplite error above.';
                } else if (this.getAmount(DESKTOPLITE) == 0) {
                    return 'Enter a number of Desktoplite above.';
                } else {
                    return 'Total cannot exceed ' + this.getAmount(DESKTOPLITE) + '.';
                }
                break;
            case MOBILE:
                if (!this.isValidAmount(ONLINEBASIC) || !this.isValidAmount(ONLINESTANDARD) || !this.isValidAmount(ONLINEPROFESSIONAL)
                || !this.isValidAmount(ONLINEENTERPRISE)) {
                    return 'Please correct the onlinemail box error above.';
                } else if (this.getAmount(ONLINEMAIL) == 0) {
                    return 'Enter a number of onlinemail onlinemail boxes above.';
                } else {
                    return 'The combined total of all mobile cannot exceed ' + this.getAmount(ONLINEMAIL) + '.';
                }
                break;
            case TOPLEVELSITES:
                if (amount < 1) {
                    return 'Enter 1 or more Top Level Sites.';
                } else if (amount > TOPLEVELSITES_MAX) {
                    return 'For ' + TOPLEVELSITES_MAX + '+ sites, call for a quote';
                }
                break;

            case NUMBEROFUSERS:
                if (!this.isValidAmount(TOPLEVELSITES)) {
                    return 'Please correct the error above.';
                } else if (this.getAmount(TOPLEVELSITES) == 0) {
                    return 'Enter 1 or more sites above.';
                } else if (amount < 1) {
                    return 'Enter 1 or more users.';
                } else if (amount > NUMBEROFUSERS_MAX) {
                    return 'For ' + NUMBEROFUSERS_MAX + '+ users, call for a quote';
                }
                break;

            case WORKSPACESTORAGE:
                if (!this.isValidAmount(TOPLEVELSITES)) {
                    return 'Please correct the error above.';
                } else if (this.getAmount(TOPLEVELSITES) == 0) {
                    return 'Enter 1 or more sites above.';
                } else if (amount < 1) {
                    return 'Enter 0 or more GBs.';
                } else if (amount > WORKSPACESTORAGE_MAX) {
                    return 'Enter ' + (WORKSPACESTORAGE_MAX) + ' or fewer GBs.';
                }
                break;
            case CRMMICROSOFT:
                if (amount < 0) {
                    return 'Enter 0 or more sites.';
                } else if (amount > CRMMICROSOFT_MAX) {
                    return 'For ' + CRMMICROSOFT_MAX + '+ CRM, call for a quote';
                }
                break;
            case CRMSTORAGE:
                if (amount < 0) {
                    return 'Enter 0 or more storage.';
                } else if (amount > CRMSTORAGE_MAX) {
                    return 'For ' + CRMSTORAGE_MAX + '+ storage, call for a quote';
                }
                break;
            default:
                break;
        }

        return 'Error';
    }
    // }}}
    // {{{ getId()
    this.getId = function(input) {
        // the input field id is 'SignupBlah', we want 'blah'
        var id = input.id.substring(6, 7).toLowerCase() + input.id.substring(7);
        return id;
    }
    // }}}
    // {{{ getInput()
    this.getInput = function(product) {
        var id = 'Signup' + product.substring(0, 1).toUpperCase() + product.substring(1);
        return document.getElementById(id);
    }
    // }}}
    // {{{ getPrice()
    this.getPrice = function(product) {
        if (!this.prices) {
            this.prices = {};
            this.prices[ONLINEBASIC] = ONLINEBASIC_PRICE;
            this.prices[ONLINESTANDARD] = ONLINESTANDARD_PRICE;
            this.prices[ONLINEPROFESSIONAL] = ONLINEPROFESSIONAL_PRICE;
            this.prices[ONLINEMAILSTORAGE] = ONLINEMAILSTORAGE_PRICE;
            this.prices[TOPLEVELSITES] = TOPLEVELSITES_PRICE;
            this.prices[NUMBEROFUSERS] = NUMBEROFUSERS_PRICE;
            this.prices[WORKSPACESTORAGE] = WORKSPACESTORAGE_PRICE;
            this.prices[ONLINEENTERPRISE] = ONLINEENTERPRISE_PRICE;
            this.prices[CRMMICROSOFT] = CRMMICROSOFT_PRICE;
            this.prices[CRMSTORAGE] = CRMSTORAGE_PRICE;
            this.prices[BLACKBERRY] = BLACKBERRY_PRICE;
            this.prices[WINDOWS] = WINDOWS_PRICE;
            this.prices[IPHONE] = IPHONE_PRICE;
            this.prices[DESKTOPLITE] = DESKTOPLITE_PRICE;
            this.prices[COMPANYSTORAGE] = COMPANYSTORAGE_PRICE;
            this.prices[USERSTORAGE] = USERSTORAGE_PRICE;
            this.prices[MICROSOFTPRO] = MICROSOFTPRO_PRICE;
            this.prices[MICROSOFTSTD] = MICROSOFTSTD_PRICE;
            this.prices[MICROSOFTPROJECT] = MICROSOFTPROJECT_PRICE;
            this.prices[MICROSOFTVISIO] = MICROSOFTVISIO_PRICE;
            this.prices[PDFCREATOR] = PDFCREATOR_PRICE;
        }
        return this.prices[product];
    }
    // }}}
    // {{{ getPriceDiv()
    this.getPriceDiv = function(product) {

        return jQuery('#' + product + 'Total div');

    }
    // }}}
    // {{{ getTotal()
    this.getTotal = function(product) {
        var amount = this.getAmount(product);
        if (isNaN(amount)) {
            return false;
        }
        return this.getPrice(product) * amount;
    }
    // }}}

    // {{{ isExtrastorage()
    this.isExtrastorage = function(product) {
        var extrastorage_arr = {};
        extrastorage_arr[USERSTORAGE] = extrastorage_arr[COMPANYSTORAGE] = true;
        return (extrastorage_arr[product] == true);
    }
    // }}}

    // {{{ isExtrastorage()
    this.isApplication = function(product) {
        var isApplication_arr = {};
        isApplication_arr[MICROSOFTPROJECT] = isApplication_arr[MICROSOFTVISIO] = isApplication_arr[PDFCREATOR] = true;
        return (isApplication_arr[product] == true);
    }
    // }}}
    // {{{ isMsofficeapps()
    this.isMsofficeapps = function(product) {
        var msoffice_arr = {};
        msoffice_arr[MICROSOFTSTD] = msoffice_arr[MICROSOFTPRO] = true;
        return (msoffice_arr[product] == true);
    }
    // }}}

    // {{{ isMobile()
    this.isMobile = function(product) {
        var mobile_arr = {};
        mobile_arr[BLACKBERRY] = mobile_arr[WINDOWS] = mobile_arr[IPHONE] = true;
        return (mobile_arr[product] == true);
    }
    // }}}
    // {{{ isValidAmount()
    /**
    * Whether the amount of a product is a valid number
    */
    this.isValidAmount = function(product, amount) {
        if (typeof amount == 'undefined') {
            var amount = this.getAmount(product);
        }
        if (amount != String(Number(amount))) {
            return false;
        }

        amount = Number(amount); // typecast amount as a number
        if (isNaN(amount) || amount != parseInt(amount)) {
            return false; 	// non numbers cannot be valid amounts
        }

        // we allow for 0 of anything, though you can't submit it
        if (amount == 0) {
            return true;
        }

        switch (product) {

            case ONLINEBASIC:
            case ONLINESTANDARD:
            case ONLINEPROFESSIONAL:
            case ONLINESTANDARD:
            case ONLINEENTERPRISE:
                if (amount < ONLINEMAIL_MIN || amount < 0 || amount > ONLINEMAIL_MAX) {
                    return false;
                }
                break;
            case MICROSOFTSTD:
            case MICROSOFTPRO:
                if (amount < 0 || !this.isValidAmount(DESKTOPLITE) || amount > this.getAmount(DESKTOPLITE)) {
                    return false;
                }
                break;
            case WINDOWS:
            case IPHONE:
            case BLACKBERRY:
                if (!this.isValidAmount(ONLINEBASIC) || !this.isValidAmount(ONLINESTANDARD) ||
                    !this.isValidAmount(ONLINEPROFESSIONAL) || !this.isValidAmount(ONLINEENTERPRISE) || amount < 0) {
                    return false;
                }
                break;
            case EXTRASTORAGE:
                if (amount < 0 || !this.isValidAmount(DESKTOPLITE)) {
                    return false;
                }
                break;

            case APPLICATION:
                if (amount < 0 || !this.isValidAmount(DESKTOPLITE)) {
                    return false;
                }
                break;
            case DESKTOPLITE:
                if (amount < 0 || amount > DESKTOPLITE_MAX) {
                    return false;
                }
                break;
            case MICROSOFTPROJECT:
            case MICROSOFTVISIO:
            case PDFCREATOR:
                if (!this.isValidAmount(DESKTOPLITE) || amount < 0 || amount > this.getAmount(DESKTOPLITE)) {
                    return false;
                }
                break;
            case USERSTORAGE:
            case COMPANYSTORAGE:
                if (!this.isValidAmount(DESKTOPLITE) || amount < 0 || amount > (1000)) {
                    return false;
                }
                break;
            case TOPLEVELSITES:
                if (amount < 1 || amount < 0 || amount > TOPLEVELSITES_MAX) {
                    return false;
                }
                break;
            case NUMBEROFUSERS:
                if (!this.isValidAmount(TOPLEVELSITES) || this.getAmount(TOPLEVELSITES) == 0 || amount < 0 || amount > (NUMBEROFUSERS_MAX)) {
                    return false;
                }
                break;
            case WORKSPACESTORAGE:
                if (!this.isValidAmount(TOPLEVELSITES) || this.getAmount(TOPLEVELSITES) == 0 || amount < 0 || amount > (WORKSPACESTORAGE_MAX)) {
                    return false;
                }
                break;
            case CRMMICROSOFT:
                if (amount > CRMMICROSOFT_MAX || amount < 0) {
                    return false;
                }
                break;
            case CRMSTORAGE:
                if (amount > CRMSTORAGE_MAX || amount < 0) {
                    return false;
                }
                break;
            case MSOFFICEAPPS:
                if (amount < 0 || !this.isValidAmount(DESKTOPLITE) || amount > this.getAmount(DESKTOPLITE)) {
                    return false;
                }
                break;
            case MOBILE:
                if (amount < 0 || !this.isValidAmount(ONLINEBASIC) || !this.isValidAmount(ONLINESTANDARD)
                || !this.isValidAmount(ONLINEPROFESSIONAL) || !this.isValidAmount(ONLINEENTERPRISE) || amount > this.getAmount(ONLINEMAIL)) {
                    return false;
                }
                break;
            case ONLINEMAILSTORAGE:
                var onlineTotal = this.getAmount(ONLINEMAIL) * 10;
                var onlinebasicTotal = this.getAmount(ONLINEBASIC) * 10;
                var mytotal = onlineTotal + onlinebasicTotal;

                if (!this.isValidAmount(ONLINEBASIC) || !this.isValidAmount(ONLINESTANDARD) || !this.isValidAmount(ONLINEPROFESSIONAL)
                || !this.isValidAmount(ONLINEENTERPRISE) || amount < 0 || amount > mytotal) {
                    return false
                }
                break;

            default:
                break;
        }

        if (amount != parseInt(amount) || amount < 0) {
            return false;
        }
        return true;
    }


    // {{{ removeError()
    this.removeError = function(product) {
        // there's an error to be shown that we can remove
        if (this._timeouts[product]) {
            window.clearTimeout(this._timeouts[product]);
            this._timeouts[product] = false;
        }

        if (this.errors[product]) {
            this.errors[product] = false;
        } else {
            // there was no error extant for this item
            return false;
        }

        if (product == APPLICATION) {
            var licenses = [MICROSOFTPROJECT, MICROSOFTVISIO, PDFCREATOR];
            for (var i = 0, ii = licenses.length; i < ii; i++) {
                if (this.isValidAmount(licenses[i])) {
                    jQuery(this.getInput(licenses[i])).parent('td').removeClass('showError');
                    jQuery(this.getPriceDiv(licenses[i])).parent('td').removeClass('showError');
                    this.showPrice(licenses[i]);
                    this.updateSummaryRow(licenses[i]);
                }
            }
        } if (product == EXTRASTORAGE) {
            var licenses = [COMPANYSTORAGE, USERSTORAGE];
            for (var i = 0, ii = licenses.length; i < ii; i++) {
                if (this.isValidAmount(licenses[i])) {
                    jQuery(this.getInput(licenses[i])).parent('td').removeClass('showError');
                    jQuery(this.getPriceDiv(licenses[i])).parent('td').removeClass('showError');
                    this.showPrice(licenses[i]);
                    this.updateSummaryRow(licenses[i]);
                }
            }
        } if (product == MSOFFICEAPPS) {
            var licenses = [MICROSOFTSTD, MICROSOFTPRO];
            for (var i = 0, ii = licenses.length; i < ii; i++) {
                if (this.isValidAmount(licenses[i])) {
                    jQuery(this.getInput(licenses[i])).parent('td').removeClass('showError');
                    jQuery(this.getPriceDiv(licenses[i])).parent('td').removeClass('showError');
                    this.showPrice(licenses[i]);
                    this.updateSummaryRow(licenses[i]);
                }
            }
        } if (product == MOBILE) {
            var licenses = [BLACKBERRY, WINDOWS, IPHONE];
            for (var i = 0, ii = licenses.length; i < ii; i++) {
                if (this.isValidAmount(licenses[i])) {
                    jQuery(this.getInput(licenses[i])).parent('td').removeClass('showError');
                    jQuery(this.getPriceDiv(licenses[i])).parent('td').removeClass('showError');
                    this.showPrice(licenses[i]);
                    this.updateSummaryRow(licenses[i]);
                }
            }
        } else {
            jQuery(this.getInput(product)).parents('td').removeClass('showError');

            jQuery(this.getPriceDiv(product)).parents('td').removeClass('showError');

        }

        // to re-enable the submit button
        var disabled = false;
        for (var item in this.errors) {
            if (this.errors[item] && this.errors[item] == true) {
                disabled = true;
                break;
            }
        }
        if (!disabled) {
            this.enableSubmit();
        }

    }
    // }}}
    // {{{ setAmount()
    this.setAmount = function(product, amount) {
        if (amount == '') {
            amount = 0;
        }
        this._amount[product] = amount;
    }
    // }}}
    // {{{ showError()
    this.showError = function(product) {
        this.errors[product] = true;
        var error_msg = this.getErrorMsg(product);

        if (product == APPLICATION) {
            var licenses = [MICROSOFTPROJECT, MICROSOFTVISIO, PDFCREATOR];
            for (var i = 0, ii = licenses.length; i < ii; i++) {
                if (this.isValidAmount(licenses[i]) && this.getAmount(licenses[i]) != 0) {
                    jQuery(this.getInput(licenses[i])).parent('td').addClass('showError');
                    // show the error message
                    jQuery(this.getPriceDiv(licenses[i])).html(error_msg).parent('td').addClass('showError');
                    this.updateSummaryRow(licenses[i], 0);
                }
            }
        } if (product == EXTRASTORAGE) {
            var licenses = [COMPANYSTORAGE, USERSTORAGE];
            for (var i = 0, ii = licenses.length; i < ii; i++) {
                if (this.isValidAmount(licenses[i]) && this.getAmount(licenses[i]) != 0) {
                    jQuery(this.getInput(licenses[i])).parent('td').addClass('showError');
                    // show the error message
                    jQuery(this.getPriceDiv(licenses[i])).html(error_msg).parent('td').addClass('showError');
                    this.updateSummaryRow(licenses[i], 0);
                }
            }
        } if (product == MSOFFICEAPPS) {
            var licenses = [MICROSOFTSTD, MICROSOFTPRO];
            for (var i = 0, ii = licenses.length; i < ii; i++) {
                if (this.isValidAmount(licenses[i]) && this.getAmount(licenses[i]) != 0) {
                    jQuery(this.getInput(licenses[i])).parent('td').addClass('showError');
                    // show the error message
                    jQuery(this.getPriceDiv(licenses[i])).html(error_msg).parent('td').addClass('showError');
                    this.updateSummaryRow(licenses[i], 0);
                }
            }
        } if (product == MOBILE) {
            var licenses = [BLACKBERRY, WINDOWS, IPHONE];
            for (var i = 0, ii = licenses.length; i < ii; i++) {
                if (this.isValidAmount(licenses[i]) && this.getAmount(licenses[i]) != 0) {
                    jQuery(this.getInput(licenses[i])).parent('td').addClass('showError');
                    // show the error message
                    jQuery(this.getPriceDiv(licenses[i])).html(error_msg).parent('td').addClass('showError');
                    this.updateSummaryRow(licenses[i], 0);
                }
            }
        } else {
            jQuery(this.getInput(product)).parent('td').addClass('showError');
            // show the error message
            jQuery(this.getPriceDiv(product)).html(error_msg).parent('td').addClass('showError');
            this.updateSummaryRow(product, 0);
        }
        this.disableSubmit();
    }
    // }}}
    // {{{ showPrice()
    this.showPrice = function(product) {
        var amount = this.getAmount(product);
        var price = 0;

        price = this.getPrice(product) * amount;

        var jQuerycurrent = jQuery(this.getPriceDiv(product));
        jQuerycurrent.html(this.formatCurrency(price));
    }
    // }}}
    // {{{ updateProduct()
    this.updateProduct = function(product) {
        if (this.isValidAmount(product)) {
            if (this.isApplication(product) && !this.isValidAmount(APPLICATION)) {
                this.showError(APPLICATION);
            } else {
                if (this.isApplication(product)) {
                    this.removeError(APPLICATION);
                }
                this.removeError(product);
                this.showPrice(product);
                this.updateSummaryRow(product);
            }
            if (this.isExtrastorage(product) && !this.isValidAmount(EXTRASTORAGE)) {
                this.showError(EXTRASTORAGE);
            } else {
                if (this.isExtrastorage(product)) {
                    this.removeError(EXTRASTORAGE);
                }
                this.removeError(product);
                this.showPrice(product);
                this.updateSummaryRow(product);
            }
            if (this.isMsofficeapps(product) && !this.isValidAmount(MSOFFICEAPPS)) {
                this.showError(MSOFFICEAPPS);
            } else {
                if (this.isMsofficeapps(product)) {
                    this.removeError(MSOFFICEAPPS);
                }
                this.removeError(product);
                this.showPrice(product);
                this.updateSummaryRow(product);
            }
            if (this.isMobile(product) && !this.isValidAmount(MOBILE)) {
                this.showError(MOBILE);
            } else {
                if (this.isMobile(product)) {
                    this.removeError(MOBILE);
                }
                this.removeError(product);
                this.showPrice(product);
                this.updateSummaryRow(product);
            }
        } else {
            // if we want to delay the error
            if (this._delayError(product)) {
                this.delay(product, function() {
                    jQuerySignup.showError(product);
                    jQuerySignup.validateDependents(product);
                    jQuerySignup.updateSummaryTotal();


                });
                return;
            } else {
                this.showError(product);
            }
        }

        // validate all products that rely on the one that changed
        if (product == EXTRASTORAGE && this.getAmount(product) == 0) {
            this.delay(product, function() {
                jQuerySignup.validateDependents(product);
                jQuerySignup.updateSummaryTotal();


            });
        }

        if (product == APPLICATION && this.getAmount(product) == 0) {
            this.delay(product, function() {
                jQuerySignup.validateDependents(product);
                jQuerySignup.updateSummaryTotal();


            });
        }
        if (product == MSOFFICEAPPS && this.getAmount(product) == 0) {
            this.delay(product, function() {
                jQuerySignup.validateDependents(product);
                jQuerySignup.updateSummaryTotal();


            });
        }
        if (product == ONLINEMAIL && this.getAmount(product) == 0) {
            this.delay(product, function() {
                jQuerySignup.validateDependents(product);
                jQuerySignup.updateSummaryTotal();


            });
        } else {
            // Update summary and enable submit -- validation will handle errors
            this.updateSummaryRow(product);
            this.validateDependents(product);
            this.updateSummaryTotal();

        }
    }
    // }}}
    // {{{ updateSummary()
    this.updateSummary = function() {

        jQuery('#summarySection').find('tbody').find('tr').each(function() {

            var product = jQuery(this).attr('id').slice(0, jQuery(this).attr('id').lastIndexOf('Summary'));
            var count = jQuerySignup.getAmount(product);
            jQuerySignup.updateSummaryRow(product, count, jQuery(this));
        });
        jQuerySignup.updateSummaryTotal();

    }
    // }}}
    // {{{ updateSummaryRow()
    /**
    */
    this.updateSummaryRow = function(product, count, row) {
        if (typeof count == 'undefined') {
            var count = this.getAmount(product);
        }
        if (typeof row == 'undefined') {
            var row = jQuery('#' + product + 'Summary');
        }
        if (!row) { return false; }

        if (count == 0) {
            row.hide();
        } else {
            if (jQuerySignup.isValidAmount(product, count)) {
                if (jQuerySignup.isApplication(product) && !jQuerySignup.isValidAmount(APPLICATION)) {
                    row.hide();
                }
                if (jQuerySignup.isExtrastorage(product) && !jQuerySignup.isValidAmount(EXTRASTORAGE)) {
                    row.hide();
                }
                if (jQuerySignup.isMsofficeapps(product) && !jQuerySignup.isValidAmount(MSOFFICEAPPS)) {
                    row.hide();
                }
                if (jQuerySignup.isMobile(product) && !jQuerySignup.isValidAmount(MOBILE)) {
                    row.hide();
                } else {
                    row.find('td.rowCount').html(count);
                    row.find('td.rowTotal').html(this.formatCurrency(Number(this.getPrice(product) * count)));
                    row.show();
                }
            }
        }
    }
    // }}}
    // {{{ updateSummaryTotal()
    this.updateSummaryTotal = function() {
        var sub_total = 0;
        var total_VAT = 0;
        var grand_total = 0;
        jQuery('#monthlySection .summary_table tr:visible').each(function() {
            var price = jQuery.trim(jQuery(this).find('td.rowTotal').html());
            price = price.replace(',', '');
            price = Number(price.replace(/\u00a3/g, ''));
            vat = price * 0.175;
            grand = price + vat;
            if (price) {
                sub_total += price;
                total_VAT += vat;
                grand_total += grand;

            }

            var product = jQuery.trim(jQuery(this).find('td.rowDesc').html());
            var amount = jQuery.trim(jQuery(this).find('td.rowCount').html());
            product = product.replace(' ', '');
            product = product.replace(' ', '');
            var combined = amount + "/" + price;
            combined = combined.replace(' ', '');
            combined = combined.replace(' ', '');
            var subTotal = sub_total
            var VAT = total_VAT
            var grandTotal = grand_total
           
            if (product != "") {
                jQuerySignup.enableSubmit(this);
                //if the the element isnt already in the list

            } else { jQuerySignup.disableSubmit(this); }
        });
        jQuery('#monthlySection div.total span.subtotal').html(this.formatCurrency(sub_total));
        jQuery('#monthlySection div.total span.totalvat').html(this.formatCurrency(total_VAT));
        jQuery('#monthlySection div.total span.grandtotal').html(this.formatCurrency(grand_total));


    }
    // }}}

    this.addDependent = function(id, validate, dependents) {
        if (jQuery('#' + id).attr('checked')) {
            for (var i = 0, ii = dependents.length; i < ii; i++) {
                validate.push(dependents[i]);
            }
        }
    }
    // }}}
    // {{{ validateDependents()
    this.validateDependents = function(product_id) {
        var to_validate = [];
        switch (product_id) {
            case ONLINEBASIC:
            case ONLINESTANDARD:
            case ONLINEPROFESSIONAL:
            case ONLINEENTERPRISE:
                this.addDependent('SignupmobileOptions', to_validate, [BLACKBERRY, WINDOWS, IPHONE, MOBILE]);
                this.addDependent('SignupextraStorage', to_validate, [ONLINEMAILSTORAGE]);
                break;
            case TOPLEVELSITES:
                this.addDependent('SignupToplevelSitesOptions', to_validate, [NUMBEROFUSERS, WORKSPACESTORAGE]);
                break;
            case DESKTOPLITE:
                this.addDependent('SignupadditionalApplication', to_validate, [MICROSOFTSTD, MICROSOFTPRO, MICROSOFTPROJECT, MICROSOFTVISIO, PDFCREATOR, MSOFFICEAPPS, APPLICATION]);
                this.addDependent('SignupextraDesktopStorage', to_validate, [COMPANYSTORAGE, USERSTORAGE, EXTRASTORAGE]);
                break;
            default:
                break;
        }

        for (var i = 0, ii = to_validate.length; i < ii; i++) {
            if (!this.isValidAmount(to_validate[i])) {
                this.showError(to_validate[i]);
            } else {
                // need to remove errors if extant
                this.updateProduct(to_validate[i]);
            }
        }
    }
    // {{{ evUpdateInput()
    this.evUpdateInput = function(input) {
        var product_id = this.getId(input);
        this.setAmount(product_id, input.value);
        this.updateProduct(product_id);
    }
    // }}}
}

// }}}
// {{{ Fieldset toggle
jQuery('.toggleCheckbox').click(function() {
    var product = jQuerySignup.getId(this);
    jQuery(this).parents('fieldset').eq(0).find('.toggleContent').slideToggle(100);
    jQuery(this).parents('fieldset').eq(1).find('.toggleContent3').slideToggle(100);
    if (!this.checked) {
        var inputs = jQuery(this).parents('fieldset').find('input');
        jQuery.each(inputs, function() {
            if (jQuery(this).attr('value') != '' && jQuery(this).attr('value') != 0) {
                jQuery(this).attr({ 'value': '' });
                jQuerySignup.updateProduct(product);
                jQuerySignup.evUpdateInput(this);
            }
        });
    }
});
// }}}
// {{{ Fieldset toggle
jQuery('.toggleCheckbox2').click(function() {
    var product = jQuerySignup.getId(this);
    jQuery(this).parents('fieldset').eq(1).find('.toggleContent2').slideToggle(100);
    if (!this.checked) {
        var inputs = jQuery(this).parent('fieldset').find('input');
        jQuery.each(inputs, function() {
            if (jQuery(this).attr('value') != '' && jQuery(this).attr('value') != 0) {
                jQuery(this).attr({ 'value': '' });
                jQuerySignup.updateProduct(product);
                jQuerySignup.evUpdateInput(this);
            }
        });
    }
});

// }}}
// {{{ Fieldset toggle
jQuery('.toggleCheckbox3').click(function() {
    var product = jQuerySignup.getId(this);
    jQuery(this).parents('fieldset').eq(1).find('.toggleContent3').slideToggle(100);
    if (!this.checked) {
        var inputs = jQuery(this).parent('fieldset').find('input');
        jQuery.each(inputs, function() {
            if (jQuery(this).attr('value') != '' && jQuery(this).attr('value') != 0) {
                jQuery(this).attr({ 'value': '' });
                jQuerySignup.updateProduct(product);
                jQuerySignup.evUpdateInput(this);
            }
        });
    }
});

// }}}
jQuery('input.update').blur(function() {
    jQuerySignup.evUpdateInput(this);
});
jQuery('input.update').keyup(function() {
    jQuerySignup.evUpdateInput(this);
});
// {{{ document.ready
jQuery(document).ready(function() {
    // toggle fieldsets
    //if(jQuery('.toggleCheckbox').is(':checked'))
    //{
    // $(".toggleCheckbox").attr("checked", false);   
    //}
    jQuery('.toggleCheckbox:not(:checked)').parents('fieldset').find('.toggleContent').hide();
    jQuery('.toggleCheckbox:checked').parents('fieldset').find('.toggleContent').show();

    jQuery('.toggleCheckbox2:not(:checked)').parents('fieldset').find('.toggleContent2').hide();
    jQuery('.toggleCheckbox2:checked').parents('fieldset').find('.toggleContent2').show();

    jQuery('.toggleCheckbox3:not(:checked)').parents('fieldset').find('.toggleContent3').hide();
    jQuery('.toggleCheckbox3:checked').parents('fieldset').find('.toggleContent3').show();


    jQuery('.toggleContent:visible input[type=text]').each(function() {
        jQuery(this).blur();
    });

    jQuery('.toggleContent2:visible input[type=text]').each(function() {
        jQuery(this).blur();
    });

    jQuery('.toggleContent3:visible input[type=text]').each(function() {
        jQuery(this).blur();
    });



    jQuerySignup.updateSummary();

    jQuery(window).bind("beforeunload", function() {
        if (document.getElementById("AlreadyEmailed") == null) {
            var sub_total = 0;
            var total_VAT = 0;
            var grand_total = 0;
            jQuery('#monthlySection .summary_table tr:visible').each(function() {
                var price = jQuery.trim(jQuery(this).find('td.rowTotal').html());
                var product = jQuery.trim(jQuery(this).find('td.rowDesc').html());
                var amount = jQuery.trim(jQuery(this).find('td.rowCount').html());
		product = product.replace('or', '');
                product = product.replace(',', '');
                product = product.replace('+', '');
                product.replace(new RegExp("\\n", "g"),'');
                product = product.replace(' ', '');
                product = product.replace(' ', '');
                price = price.replace(',', '');
                price = Number(price.replace(/\u00a3/g, ''));
                var combined = amount + "/" + price;
                combined = combined.replace(' ', '');
                combined = combined.replace(' ', '');

                vat = price * 0.175;
                grand = price + vat;
                if (price) {
                    sub_total += price;
                    total_VAT += vat;
                    grand_total += grand;

                }
               
                jQuery("#emailSummary").append("<input name='" + product + "'type='hidden' id='" + product + "' value='" + combined + "'/>");
                if (document.getElementById("subTotal") == null) 
                {
                    jQuery("#emailSummary").append("<input name='subTotal' type='hidden' id='subTotal' value='" + sub_total + "'/>");
                    jQuery("#emailSummary").append("<input name='VAT' type='hidden' id='VAT' value='" + total_VAT + "'/>");
                    jQuery("#emailSummary").append("<input name='grandTotal' type='hidden' id='grandTotal' value='" + grand_total + "'/>");
                   
                } else {
                    var old_sub_total = document.getElementById("subTotal"); // So we can replace it
                    var old_total_VAT = document.getElementById("VAT"); // So we can replace it
                    var old_grand_total = document.getElementById("grandTotal"); // So we can replace it


                    var new_sub_total = "<input name='subTotal' type='hidden' id='subTotal' value='" + sub_total + "'/>"; //create new input
                    var new_total_VAT = "<input name='VAT' type='hidden' id='VAT' value='" + total_VAT + "'/>"; //create new input
                    var new_grand_total = "<input name='grandTotal' type='hidden' id='grandTotal' value='" + grand_total + "'/>"; //create new input
                    // Swap type field

                    // Swap type field
                    jQuery(old_sub_total).before(jQuery(new_sub_total));
                    jQuery(old_sub_total).remove();
                    // Swap type field
                    jQuery(old_total_VAT).before(jQuery(new_total_VAT));
                    jQuery(old_total_VAT).remove();
                    // Swap type field
                    jQuery(old_grand_total).before(jQuery(new_grand_total));
                    jQuery(old_grand_total).remove();
                }
               

            });

            var name = jQuery("input#name").val();
            var phone = jQuery("input#phone").val();
            var email = jQuery("input#email").val();

            var onlineBasic = jQuery('#onlinemailBasic').val();

            var onlineStd = jQuery("#onlinemailStandard").val();

            var onlinePro = jQuery("#onlinemailProfessional").val();
            var onlineEnt = jQuery("#onlinemailEnterprise").val();

            var workspaceStorage = jQuery("#ExtraworkspaceStorage").val();
            var workspaceUsers = jQuery("#NumberofUsers").val();
            var worksSpaceSites = jQuery("#TopLevelSites").val();

            var crmStorage = jQuery("#ExtraCRMStorage").val();
            var crmUsers = jQuery("#MicrosoftCRMUsers").val();

            var desktopLite = jQuery("#DesktopLite").val();
            if ((onlineBasic) || (onlineStd) || (onlinePro) || (onlineEnt) || (workspaceStorage) || (workspaceUsers) || (worksSpaceSites) || (crmStorage) || (crmUsers) || (desktopLite)) {



                jQuery('body').append('<form id="form-to-submit"  method="post" style="visibility:hidden;"></form>');
                jQuery("#emailSummary").append("<input name='EmailMeSummary' type='hidden' id='EmailMeSummary' value='" + email + "'/>");
                jQuery('#emailAddress').remove();

                //Clone the fieldset into the new form.
                jQuery('#form-to-submit').html(jQuery('#emailSummary').clone());

                //Serialize the data
                var mydata = jQuery('#form-to-submit').serialize();
               
                //Remove the form
                jQuery('#form-to-submit').remove();


                jQuery.ajax({
                    url: "quickquote.aspx",
                    type: "POST",
                    data: mydata,
                    cache: false,
                    success: function(html) {

                    }
                });

            }
        }
    });

});
// }}}