additem.js (not sure it's related to new_acq)
[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 quantityrec = document.getElementById('quantityrec');
5     quantityrec.setAttribute('value',parseFloat(quantityrec.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         CloneButtonPlus = clone.getElementsByTagName('a')[0];
21         CloneButtonPlus.setAttribute('onclick',"cloneItemBlock('" + index + random + "')");
22     CloneButtonMinus = clone.getElementsByTagName('a')[1];
23     CloneButtonMinus.setAttribute('onclick',"deleteItemBlock('" + index + random + "')");
24     CloneButtonMinus.setAttribute('style',"display:inline");
25     // change itemids of the clone
26     var elems = clone.getElementsByTagName('input');
27     for( i = 0 ; elems[i] ; i++ )
28     {
29         if(elems[i].name.match(/^itemid/)) {
30             elems[i].value = random;
31         }
32     }    
33    // }
34     //catch(e){        // do nothig if ButtonPlus & CloneButtonPlus don't exist.
35     //}
36     // insert this line on the page    
37     original.parentNode.insertBefore(clone,original.nextSibling);
38     var quantityrec = document.getElementById('quantityrec');
39     quantityrec.setAttribute('value',parseFloat(quantityrec.getAttribute('value'))+1);
40 }
41 function check_additem() {
42         var     barcodes = document.getElementsByName('barcode');
43         var success = true;
44         for(i=0;i<barcodes.length;i++){
45                 for(j=0;j<barcodes.length;j++){
46                         if( (i > j) && (barcodes[i].value == barcodes[j].value) && barcodes[i].value !='') {
47                                 barcodes[i].className='error';
48                                 barcodes[j].className='error';
49                                 success = false;
50                         }
51                 }
52         }
53         // TODO : Add AJAX function to test against barcodes already in the database, not just 
54         // duplicates within the form.  
55         return success;
56 }
57 $(document).ready(function(){
58         $(".cloneItemBlock").click(function(){
59                 var clonedRow = $(this).parent().parent().clone(true);
60                 clonedRow.insertAfter($(this).parent().parent()).find("a.deleteItemBlock").show();
61                 // find ID of cloned row so we can increment it for the clone
62                 var count = $("input[id^=volinf]",clonedRow).attr("id");
63                 var current = Number(count.replace("volinf",""));
64                 var increment = current + 1;
65                 // loop over inputs
66                 var inputs = ["volinf","barcode"];
67                 jQuery.each(inputs,function() {
68                         // increment IDs of labels and inputs in the clone
69                         $("label[for="+this+current+"]",clonedRow).attr("for",this+increment);
70                         $("input[name="+this+"]",clonedRow).attr("id",this+increment);
71                 });
72                 // loop over selects
73                 var selects = ["homebranch","location","itemtype","ccode"];
74                 jQuery.each(selects,function() {
75                         // increment IDs of labels and selects in the clone
76                         $("label[for="+this+current+"]",clonedRow).attr("for",this+increment);
77                         $("input[name="+this+"]",clonedRow).attr("id",this+increment);
78                         $("select[name="+this+"]",clonedRow).attr("id",this+increment);
79                         // find the selected option and select it in the clone
80                         var selectedVal = $("select#"+this+current).find("option:selected").attr("value");
81                         $("select[name="+this+"] option[value="+selectedVal+"]",clonedRow).attr("selected","selected");
82                 });
83                 
84                 var quantityrec = parseFloat($("#quantityrec").attr("value"));
85                 quantityrec++;
86                 $("#quantityrec").attr("value",quantityrec);
87                 return false;
88         });
89         $(".deleteItemBlock").click(function(){
90                 $(this).parent().parent().remove();
91                 var quantityrec = parseFloat($("#quantityrec").attr("value"));
92                 quantityrec--;
93                 $("#quantityrec").attr("value",quantityrec);
94                 return false;
95         });
96 });