Merge remote-tracking branch 'origin/new/bug_6328'
[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 = original.cloneNode(true);
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 $(document).ready(function(){
59         $(".cloneItemBlock").click(function(){
60                 var clonedRow = $(this).parent().parent().clone(true);
61                 clonedRow.insertAfter($(this).parent().parent()).find("a.deleteItemBlock").show();
62                 // find ID of cloned row so we can increment it for the clone
63                 var count = $("input[id^=volinf]",clonedRow).attr("id");
64                 var current = Number(count.replace("volinf",""));
65                 var increment = current + 1;
66                 // loop over inputs
67                 var inputs = ["volinf","barcode"];
68                 jQuery.each(inputs,function() {
69                         // increment IDs of labels and inputs in the clone
70                         $("label[for="+this+current+"]",clonedRow).attr("for",this+increment);
71                         $("input[name="+this+"]",clonedRow).attr("id",this+increment);
72                 });
73                 // loop over selects
74                 var selects = ["homebranch","location","itemtype","ccode"];
75                 jQuery.each(selects,function() {
76                         // increment IDs of labels and selects in the clone
77                         $("label[for="+this+current+"]",clonedRow).attr("for",this+increment);
78                         $("input[name="+this+"]",clonedRow).attr("id",this+increment);
79                         $("select[name="+this+"]",clonedRow).attr("id",this+increment);
80                         // find the selected option and select it in the clone
81                         var selectedVal = $("select#"+this+current).find("option:selected").attr("value");
82                         $("select[name="+this+"] option[value="+selectedVal+"]",clonedRow).attr("selected","selected");
83                 });
84                 
85                 var quantityrec = parseFloat($("#quantityrec").attr("value"));
86                 quantityrec++;
87                 $("#quantityrec").attr("value",quantityrec);
88                 return false;
89         });
90         $(".deleteItemBlock").click(function(){
91                 $(this).parent().parent().remove();
92                 var quantityrec = parseFloat($("#quantityrec").attr("value"));
93                 quantityrec--;
94                 $("#quantityrec").attr("value",quantityrec);
95                 return false;
96         });
97 });