﻿/*
jQuery plugin to add functionality to your forms to add multiple records on single click on the fly using client side javascript
author: Vipul Limbachiya
http://vipulilmbachiya.com
*/

/* 
 * This program is free software. It comes without any warranty, to the extent permitted by applicable law. 
 * You can redistribute it and/or modify it without any terms.
 * Save developers, use Firefox http://firefox.com
*/

(function (jQuery) {

    jQuery.fn.EnableMultiField = function (options) {

        options = jQuery.extend({
            linkText: '+ Afegir bloc',
            linkClass: 'addMoreFields',
            enableRemove: true,
            removeLinkText: '- Esborra bloc',
            removeLinkClass: 'removeFields',
            confirmOnRemove: true,
            confirmationMsgOnRemove: 'Estàs segur que vols esborrar aquest bloc??',
            addEventCallback: null,
            removeEventCallback: null,
            maxItemsAllowedToAdd: null,
            maxItemReachedCallback: null
        },
        options);

        return this.each(function () {

            var self = jQuery(this);

            jQuery(self).attr("TotalFieldsAdded", "0");

            jQuery(self).attr("maxCountReached", "false");

            jQuery(self).attr("FieldCount", "0");

            jQuery(self).attr("uniqueId", options.linkClass + Math.random());

            jQuery(self).find("." + options.linkClass).remove();

            jQuery(self).find("." + options.removeLinkClass).remove();

            jQuery(self).append(" <a href='#' class='" + options.linkClass + "'>" + options.linkText + "</a>");

            jQuery(self).find("." + options.linkClass).click(function () {
                return handleAdd(jQuery(this));
            });

            var myClone = jQuery(self).clone();

            function handleAdd(elem) {
                var totalCount = parseInt(jQuery(self).attr("TotalFieldsAdded"), 10);
                var fieldCount = parseInt(jQuery(self).attr("FieldCount"), 10);
                if (options.maxItemsAllowedToAdd === null || totalCount < options.maxItemsAllowedToAdd) {

                    var newElem = myClone.clone();

                    jQuery(newElem).find("*[id!=''][name!='']").each(function () {

                        if (jQuery(this).attr("id")) {
                            var strid = jQuery(this).attr("id");
                            var strname = "";

                            if (jQuery(this).attr("name")) {
                                strname = jQuery(this).attr("name");
                            }

                            jQuery(this).attr("id", strid + "_" + fieldCount);
														jQuery(this).attr("name", strname);
                            //jQuery(this).attr("name", strname + "jQuery" + fieldCount);
                        }
                    });

                    totalCount++;
                    fieldCount++;

                    jQuery(self).attr("TotalFieldsAdded", totalCount);
                    jQuery(self).attr("FieldCount", fieldCount);

                    jQuery(newElem).removeAttr("uniqueId");

                    if (options.enableRemove && jQuery(self).attr("uniqueId") != jQuery(elem).parent().attr("uniqueId")) {
                        if (jQuery(elem).parent().find("." + options.removeLinkClass).length === 0) {
                            jQuery(elem).parent().append(" <a href='#' class='" + options.removeLinkClass + "'>" + options.removeLinkText + "</a>");
                        }
                        jQuery(elem).parent().find("." + options.removeLinkClass).click(function () {
                            return handleRemove(jQuery(this));
                        });
                    }

                    jQuery(newElem).attr("uniqueId", options.linkClass + Math.random());

                    jQuery(elem).parent().after(newElem);

                    jQuery(elem).parent().find("." + options.linkClass).remove();

                    jQuery(newElem).find("." + options.linkClass).remove();
                    jQuery(newElem).find("." + options.removeLinkClass).remove();

                    if (options.enableRemove) {
                        if (jQuery(newElem).find("." + options.removeLinkClass).length === 0) {
                            jQuery(newElem).append(" <a href='#' class='" + options.removeLinkClass + "'>" + options.removeLinkText + "</a>");
                        }
                        jQuery(newElem).find("." + options.removeLinkClass).click(function () {
                            return handleRemove(jQuery(this));
                        });
                    }

                    jQuery(self).attr("maxCountReached", "false");

                    jQuery(newElem).append(" <a href='#' class='" + options.linkClass + "'>" + options.linkText + "</a>");

                    newElem.find("." + options.linkClass).click(function () {
                        return handleAdd(jQuery(this));
                    });

                    if (options.addEventCallback !== null) {
                        options.addEventCallback(jQuery(newElem), self);
                    }
                }

                if (options.maxItemsAllowedToAdd !== null && totalCount >= options.maxItemsAllowedToAdd) {
                    newElem.find("." + options.linkClass).hide();

                    if (options.maxItemReachedCallback !== null) {
                        options.maxItemReachedCallback(jQuery(newElem), self);
                    }
                }

                return false;
            }

            function handleRemove(elem) {
                var cnt = true;

                if (options.confirmOnRemove) {
                    cnt = confirm(options.confirmationMsgOnRemove);
                }
                if (cnt) {
                    var prevParent = jQuery(elem).parent().prev();

                    var totalCount = parseInt(jQuery(self).attr("TotalFieldsAdded"), 10);
                    totalCount--;

                    jQuery(self).attr("TotalFieldsAdded", totalCount);

                    if (jQuery(elem).parent().find("." + options.linkClass).length > 0) {
                        if (options.enableRemove && jQuery(self).attr("uniqueId") != jQuery(prevParent).attr("uniqueId")) {
                            if (jQuery(prevParent).find("." + options.removeLinkClass).length === 0) {
                                jQuery(prevParent).append(" <a href='#' class='" + options.removeLinkClass + "'>" + options.removeLinkText + "</a>");
                            }

                            jQuery(prevParent).find("." + options.removeLinkClass).click(function () {
                                return handleRemove(jQuery(this));
                            });
                        }

                        jQuery(prevParent).append(" <a href='#' class='" + options.linkClass + "'>" + options.linkText + "</a>");

                        prevParent.find("." + options.linkClass).click(function () {
                            return handleAdd(jQuery(this));
                        });
                    }

                    if (options.maxItemsAllowedToAdd !== null && totalCount < options.maxItemsAllowedToAdd) {
                        jQuery(self).siblings().find("." + options.linkClass).show();
                    }

                    jQuery(elem).parent().remove();

                    if (options.removeEventCallback !== null) {
                        options.removeEventCallback(jQuery(prevParent), self);
                    }
                }
                return false;
            }
        });

    };
})(jQuery);
