
po = new PrintOrder();



function PrintOrder() {

   
   /* Variables */
   this.clickcounter = 0;
   this.currentvalue = 0;
   this.registeredImages = new Array();
   this.registeredArtnrs = new Array();
   this.isprocessing = false;
   this.nextcounter = 0;
   this.loadingimage = 'layout_grafikk/ajax-loader.gif';
   
   this.registerImage = function( imageid ) {

      this.registeredImages.push( imageid );
      
   }
   
   
  
  /* Register an artnr as displayed */
   this.registerArtnr = function( artnr ) {
     
      this.registeredArtnrs.push( artnr );
     
   }
   
   
   
   
  
   /* Send user to next page if everything ok */
   this.order = function() {
      
      if( po.isprocessing == true ) {
         
         setTimeout( 'po.order()', 300 );
         
      } else {
         
         document.location.href = 'do_order_prints2.php';
         
      }
      
   }
    
   
   /* Change qty for all images */   
   this.quickChange = function() {
      
      
      artnr = $( 'quickchangeselect' ).options[$( 'quickchangeselect' ).selectedIndex].value;
      qty = $( 'quickchange_quantity' ).value;
      
      for( var i = 0; i<this.registeredImages.length; i++ ) {
           
         var newelement = $( 'quantity_' + artnr + '_' + this.registeredImages[i] );
            
         if( newelement ) {
             
            newelement.value = qty;
            
            if( $( 'sizetr_' + artnr + '_' + this.registeredImages[i] ) ) {
               
               new Effect.Highlight( $( 'sizetr_' + artnr + '_' + this.registeredImages[i] ), { duration:2 } );
            
            } 
            
         }
            
      }
      
      
      this.sendUpdate( 'quickChange', 'artnr=' + artnr + '&qty=' + qty );
      
   }
   
   
  
   
   /* Add a new size to page */
   this.addNewSize = function() {
      
      element = $( 'addsizeselect' );
      value = element.options[element.selectedIndex].value;
      text = element.options[element.selectedIndex].text;
      string = '';
      
      if( !this.inarray( value, this.registeredArtnrs ) ) {

         this.registerArtnr( value );
      
         for( var i = 0; i<this.registeredImages.length; i++ ) {
           
            var newelement = $( 'sizetr_' + value + '_' + this.registeredImages[i] );
            
            if( newelement ) {
             
               newelement.style.display = '';
               newelement.style.visibility = 'visible';
               newelement.style.background = '#eee';
               new Effect.Highlight( newelement, { duration:3 } );
            
            }
            
         }
         
         select = $( 'quickchangeselect' );
               
         if( select ) {
            
            option = new Option( text, value );
                  
            if( option ) {
               
               select.options.add( option );
                     
            }
              
         }
         
         this.sendUpdate( 'addSize', 'artnr=' + value );
         
      }
      
   }

   
   this.inarray = function( value, array ) {
      
      if( array.length > 0 ) {
         
         for( var i=0; i<array.length; i++ ) {
            
            if( array[i] == value ) {
               
               return true;
               
            }
         
         }
         
      }
            
      return false;
      
   }
   
   
   
   /* Change page visa select */
   this.changePage = function() {
      
      var element = $( 'pageselect' );
      var offset = element.options[element.selectedIndex].value;
      location.href = 'set_order_images.php?offset=' + offset;
      
   }
   
   
   /* remove image from order images */
   this.removeImage = function( imageid ) {
      
      this.sendUpdate( 'removeImage', 'imageid=' + imageid, 'refreshPage' );
      
   }
   
   
   /* Save a changed quantity directly in input */
   this.saveQuantity = function( elementid ) {
      
      this.displayLoadingImage();
      
      allvalues = elementid.split( '_' );
         
      var imageid = allvalues[2];
      var artnr = allvalues[1];
      var quantity = $( elementid ).value;
         
      var params = 'imageid=' + imageid + '&artnr=' + artnr + '&quantity=' + quantity;
         
      this.sendUpdate( 'updateImageQuantity', params );
      
   }
   
   
   this.displayLoadingImage = function() {
      
      $( 'imageinfocontainer' ).innerHTML = '<img src="' + po.loadingimage + '" />';
      
   }
   
   
   /* Change print nrs */
   this.addPrint = function( elementid ) {
      
      this.displayLoadingImage();
      
      var element = $( elementid );
      element.value++;
      
      this.clickcounter++;
      
      setTimeout( 'po.sendAddPrint( \'' + elementid + '\', ' + this.clickcounter + ' )', 500 );
      
   }
   
   
   
   
   /* Change print nrs */
   this.removePrint = function( elementid ) {
      
      this.displayLoadingImage();
      
      var element = $( elementid );
      
      if( element.value > 0 ) {
         
         element.value--;
      
         this.clickcounter++;
      
         setTimeout( 'po.sendRemovePrint( \'' + elementid + '\', ' + this.clickcounter + ' )', 500 );
         
      }
      
   }
   
   
   
   
   
   /* Send add one print */
   this.sendAddPrint = function( elementid, counter ) {
      
      if( counter == this.clickcounter ) {

         allvalues = elementid.split( '_' );
         
         var imageid = allvalues[2];
         var artnr = allvalues[1];
         var quantity = $( elementid ).value;
         
         var params = 'imageid=' + imageid + '&artnr=' + artnr + '&quantity=' + quantity;
         
         this.sendUpdate( 'updateImageQuantity', params );
      
      }
      
   }
   
   
   
   
   
   /* Send removed one print */
   this.sendRemovePrint = function( elementid, counter ) {
      
      
      if( counter == this.clickcounter ) {
         
         allvalues = elementid.split( '_' );
         
         var imageid = allvalues[2];
         var artnr = allvalues[1];
         var quantity = $( elementid ).value;
         
         var params = 'imageid=' + imageid + '&artnr=' + artnr + '&quantity=' + quantity;
         
         this.sendUpdate( 'updateImageQuantity', params );
      
      }
      
   }
   
   
   
   
   this.setValue = function( elementid, value ) {
      
      
   }
   
   
   this.updateField = function( element, value ) {
      
      element.value = value;
      
   }
   
   
   
   /* Send the ajax request specified */
   this.sendUpdate = function( action, params ) {
      
      po.isprocessing = true;
      new Ajax.Request( 'ajax_print_order.php?action=' + action + '&' + params, { onSuccess:po.responseSuccess, onFailure:po.responseFailure } );     
      
   }
   
   
   
   this.recalcImages = function() {
      
      new Ajax.Updater( 'imageinfocontainer', 'ajax_print_order.php?action=recalcImages' );
      
   }
   
   
   
   this.responseSuccess = function( result ) {
      
      po.isprocessing = false;
      if( result.responseText == 'OK:removeImage' ) {
         
         location.reload( true );
         
      }
      
      po.recalcImages();
      
   }
   
   
   this.responseFailure = function( result ) {

      po.isprocessing = false;
      
   }
   
   
}
