Element.implement({
    
    addMore: function(contents, keepid,counter){
        
        switch ($type(this)){
            case 'element':
                var attributes = {};
                for (var j = 0, l = this.attributes.length; j < l; j++) {
                    var attribute = this.attributes[j], key = attribute.nodeName.toLowerCase();
                    if (Browser.Engine.trident && (/input/i).test(this.tagName) && (/width|height/).test(key)) continue;
                    var value = (key == 'style' && this.style) ? this.style.cssText : attribute.nodeValue;
                    if (!$chk(value) || key == 'uid' || (key == 'id' && !keepid)) 
                        continue;
                    
                    if (value != 'inherit' && ['string', 'number'].contains($type(value)))
                        attributes[key] = value;
                    
                    if(key=="id") 
                        attributes["id"] = value+counter;
                    if(key=="class") {
                        var index = attributes["class"].indexOf("Grid");
                        if(index==-1)
                            attributes["class"] = attributes["class"] +" hide";
                    }
                }
                var element = new Element(this.nodeName.toLowerCase(), attributes);
                
                if (contents !== false){
                    for (var i = 0, k = this.childNodes.length; i < k; i++){
                        var child = Element.addMore(this.childNodes[i], true, keepid,counter);
                        if (child) { 
                            element.grab(child);
                            if($defined(child.id)) {
                                if(child.id.test("add")) {
                                    child.innerHTML = "";
                                    var imEl=new Element('img').set('src','appbase/images/trash.gif').injectInside(child);
                                    child.addEvent('click',function(){this.getParent("tr").destroy();})
                                }else if(child.id.test("help")) {
                                    child.innerHTML = "";
                                }
                            }
                        }
                    }
                }
                return element;
            case 'textnode': return document.newTextNode(this.nodeValue);
        }
        return null;
    },
    addMoreGrid: function(contents, keepid,counter){
        
        switch ($type(this)){
            case 'element':
                var attributes = {};
                for (var j = 0, l = this.attributes.length; j < l; j++) {
                    var attribute = this.attributes[j], key = attribute.nodeName.toLowerCase();
                    if (Browser.Engine.trident && (/input/i).test(this.tagName) && (/width|height/).test(key)) continue;
                    var value = (key == 'style' && this.style) ? this.style.cssText : attribute.nodeValue;
                    if (!$chk(value) || key == 'uid' || (key == 'id' && !keepid)) 
                        continue;
                    if (value != 'inherit' && ['string', 'number'].contains($type(value)))
                        attributes[key] = value;
                    if(key=="id") 
                        attributes["id"] = value+counter;
                }
                var element = new Element(this.nodeName.toLowerCase(), attributes);
                
                if (contents !== false){
                    for (var i = 0, k = this.childNodes.length; i < k; i++){
                        var child = Element.addMore(this.childNodes[i], true, keepid,counter);
                        if (child) { 
                            element.grab(child);
                            if($defined(child.id)) {
                                if(child.id.test("add")) {
                                    child.innerHTML = "Cancel";
                                    child.addEvent('click',function(){this.getParent("tr").destroy();})
                                }
                            }
                        }
                    }
                }
                return element;
            case 'textnode': return document.newTextNode(this.nodeValue);
        }
        return null;
    },
    /**
     * @author kushal
     * @description Resolving issue with the getSelected() funcitonality for the input type <select>
     */
    getSelected: function(){
        return $(this).getElements('option').filter(function(option){
            return option.selected;
        });
    }
});

function removeGridParentTRs(checkboxName) {
    var checkBoxes = document.getElementsByName(checkboxName);
    var noOfChkBox = checkBoxes.length;
    for(var index=0; index<noOfChkBox; index++) {
        if(checkBoxes[index].checked) {
            checkBoxes[index].getParent("tr").addClass('removable');
        }	
    }
    $$('.removable').each(function(el,counter){el.remove();});
};

function makeAllChildElementReadOnly(val ,elName) {
    var iCounter = val.id.substring(elName.length);
    var elId = elName+iCounter;
    var parentId  = $(elId).getParent('tr').id;
    var inputfields  = $(parentId).getElementsByTagName('input');
    for(var index=0; index<inputfields.length; index++) {
        var el = inputfields[index];
        if(el.id!=elId) {
            if(val.checked) {
                el.removeAttribute('readonly');
            }
            else {
                el.setAttribute('readonly',true);
                if($('HD'+el.id))
                    el.value = $('HD'+el.id).get('text');
            }
        }
    }
};

function getOnlyModifiedGridData(checkboxName) {
    var param = "" ;
    var checkBoxes = document.getElementsByName(checkboxName);
    var noOfChkBox = checkBoxes.length;
    for(var index=0; index<noOfChkBox; index++) {
        if(checkBoxes[index].checked) {
            var parentTR = checkBoxes[index].getParent("tr");
            var inputfields  = parentTR.getElementsByTagName('input');
            for(var insiderindex=0; insiderindex<inputfields.length; insiderindex++) {
                param = param + '&'+inputfields[insiderindex].name+'='+inputfields[insiderindex].value;
            }
        }	
    }
    return param ;	
};