Merge remote-tracking branch 'origin/new/bug_7368'
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / additem.js
1 function deleteItemBlock(index) {
2     var aDiv = document.getElementById(index);
3     aDiv.parentNode.removeChild(aDiv);
4     var quantity = document.getElementById('quantity');
5     quantity.setAttribute('value',parseFloat(quantity.getAttribute('value'))-1);
6 }
7 function cloneItemBlock(index) {    
8     var original = document.getElementById(index); //original <div>
9     var clone = clone_with_selected(original)
10     var random = Math.floor(Math.random()*100000); // get a random itemid.
11     // set the attribute for the new 'div' subfields
12     clone.setAttribute('id',index + random);//set another id.
13     var NumTabIndex;
14     NumTabIndex = parseInt(original.getAttribute('tabindex'));
15     if(isNaN(NumTabIndex)) NumTabIndex = 0;
16     clone.setAttribute('tabindex',NumTabIndex+1);
17     var CloneButtonPlus;
18     var CloneButtonMinus;
19   //  try{
20         var jclone = $(clone);
21         CloneButtonPlus = $("a.addItem", jclone).get(0);
22         CloneButtonPlus.setAttribute('onclick',"cloneItemBlock('" + index + random + "')");
23     CloneButtonMinus = $("a.delItem", jclone).get(0);
24     CloneButtonMinus.setAttribute('onclick',"deleteItemBlock('" + index + random + "')");
25     CloneButtonMinus.setAttribute('style',"display:inline");
26     // change itemids of the clone
27     var elems = clone.getElementsByTagName('input');
28     for( i = 0 ; elems[i] ; i++ )
29     {
30         if(elems[i].name.match(/^itemid/)) {
31             elems[i].value = random;
32         }
33     }    
34    // }
35     //catch(e){        // do nothig if ButtonPlus & CloneButtonPlus don't exist.
36     //}
37     // insert this line on the page    
38     original.parentNode.insertBefore(clone,original.nextSibling);
39     var quantity = document.getElementById('quantity');
40     quantity.setAttribute('value',parseFloat(quantity.getAttribute('value'))+1);
41 }
42 function check_additem() {
43         var     barcodes = document.getElementsByName('barcode');
44         var success = true;
45         for(i=0;i<barcodes.length;i++){
46                 for(j=0;j<barcodes.length;j++){
47                         if( (i > j) && (barcodes[i].value == barcodes[j].value) && barcodes[i].value !='') {
48                                 barcodes[i].className='error';
49                                 barcodes[j].className='error';
50                                 success = false;
51                         }
52                 }
53         }
54         // TODO : Add AJAX function to test against barcodes already in the database, not just 
55         // duplicates within the form.  
56         return success;
57 }
58
59 function clone_with_selected (node) {
60            var origin = node.getElementsByTagName("select");
61            var tmp = node.cloneNode(true)
62            var selectelem = tmp.getElementsByTagName("select");
63            for (var i=0; i<origin.length; i++) {
64                selectelem[i].selectedIndex = origin[i].selectedIndex;
65            }
66            origin = null;
67            selectelem = null;
68            return tmp;
69         }
70
71 $(document).ready(function(){
72         $(".cloneItemBlock").click(function(){
73                 var clonedRow = $(this).parent().parent().clone(true);
74                 clonedRow.insertAfter($(this).parent().parent()).find("a.deleteItemBlock").show();
75                 // find ID of cloned row so we can increment it for the clone
76                 var count = $("input[id^=volinf]",clonedRow).attr("id");
77                 var current = Number(count.replace("volinf",""));
78                 var increment = current + 1;
79                 // loop over inputs
80                 var inputs = ["volinf","barcode"];
81                 jQuery.each(inputs,function() {
82                         // increment IDs of labels and inputs in the clone
83                         $("label[for="+this+current+"]",clonedRow).attr("for",this+increment);
84                         $("input[name="+this+"]",clonedRow).attr("id",this+increment);
85                 });
86                 // loop over selects
87                 var selects = ["homebranch","location","itemtype","ccode"];
88                 jQuery.each(selects,function() {
89                         // increment IDs of labels and selects in the clone
90                         $("label[for="+this+current+"]",clonedRow).attr("for",this+increment);
91                         $("input[name="+this+"]",clonedRow).attr("id",this+increment);
92                         $("select[name="+this+"]",clonedRow).attr("id",this+increment);
93                         // find the selected option and select it in the clone
94                         var selectedVal = $("select#"+this+current).find("option:selected").attr("value");
95                         $("select[name="+this+"] option[value="+selectedVal+"]",clonedRow).attr("selected","selected");
96                 });
97                 
98                 var quantityrec = parseFloat($("#quantityrec").attr("value"));
99                 quantityrec++;
100                 $("#quantityrec").attr("value",quantityrec);
101                 return false;
102         });
103         $(".deleteItemBlock").click(function(){
104                 $(this).parent().parent().remove();
105                 var quantityrec = parseFloat($("#quantityrec").attr("value"));
106                 quantityrec--;
107                 $("#quantityrec").attr("value",quantityrec);
108                 return false;
109         });
110 });