//Dollar function used below
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
 
function orderchange(session) {
	//Init variables
	this.s = session; 
	this.vat = parseFloat(session.settings.order_vatpct/100);
	this.isprepaid = (session.settings.invoice_model=='prepaid'); 
	this.storageprice = 0; 
	this.mailboxprice = 0;
	this.ismailbox = false;  
	this.isstorage = false;
 	this.isrebate = false; 
 	
 	var decimalseparator = session.settings.invoice_decimalseparator;
 	var thousandseparator = session.settings.invoice_thousandseparator;
 	
	//Init 
	orderchange.prototype.init = function(paymenttype, fieldsubtotal, fieldsubtotalreturn, fieldtotal, fieldtotalreturn, fieldtotalyearnotax, fieldtotalyearnotaxreturn){
	  this.subtotalline = $(fieldsubtotal);
	  this.subtotalreturn = $(fieldsubtotalreturn);
	  this.totalline = $(fieldtotal);
	  this.totalreturn = $(fieldtotalreturn); 
	  this.totalyearnotax = $(fieldtotalyearnotax);
	  this.totalyearnotaxreturn = $(fieldtotalyearnotaxreturn);
	  this.isprepaid = (session.settings.invoice_model=='prepaid'); 
	}    
	  
	//Init mailbox fields
	orderchange.prototype.initmailbox = function(fieldvalue, fieldtotal, fielderror, fieldreturn){
	  this.mailboxvalue = $(fieldvalue);	  
	  this.mailboxtotal = $(fieldtotal);
	  this.mailboxerror = $(fielderror);
	  this.mailboxreturn = $(fieldreturn);
	  if(this.mailboxvalue) this.ismailbox=true;
	 }    
	  
	//Init storage fields
	orderchange.prototype.initstorage = function(fieldvalue, fieldtotal, fielderror, fieldreturn){
	  this.storagevalue = $(fieldvalue);  	  	  
	  this.storagetotal = $(fieldtotal);
	  this.storageerror = $(fielderror);
	  this.storagereturn = $(fieldreturn);
	  if(this.storagevalue) this.isstorage=true;
	}
  
 	//Init rebate field
 	orderchange.prototype.initrebate = function(fieldvalue, fieldtotal, fieldreturn){
 	  this.rebatedata = $(fieldvalue);
 	  this.rebatetotal = $(fieldtotal);
 	  this.rebatereturn = $(fieldreturn);
 	  if(this.rebatedata) {
 	  	this.rebate = Right(this.rebatedata.value, 2);
 	  } else {
 	  	this.rebate = 0;
 	  }
 	  if(this.rebatedata) this.isrebate=true;
 	}

 	//Updateline prototype
 	//----------------
 	orderchange.prototype.updateline = function(stocknumber, value){
 	 	var decimalseparator = session.settings.invoice_decimalseparator;
 		var thousandseparator = session.settings.invoice_thousandseparator;
 	
 	 	$(stocknumber+ '_price').innerText = eval('price'+stocknumber+'(' + value + ').formatMoney(2, "' + decimalseparator + '", "' + thousandseparator + '");');
 	}  

 	//Update prototype
 	//----------------
	orderchange.prototype.update = function(){
	
	  //Handle storage input 
	  if(this.isstorage) {
	    var storage = this.storagevalue.value;
	    if((storage<0)||(storage>10000)) storage=0;
	    this.storageprice = price0301(storage)	    
	  }
	 
	  //Handle mailbox input
	  if(this.ismailbox) { 
	    var mailbox = this.mailboxvalue.value;
	    this.mailboxprice = price0310(mailbox); 	  
	  } 
	   
	  //calculate values
	  var nettotal = this.storageprice + this.mailboxprice;
	  var rebateprice = nettotal * this.rebate / 100;
	  var subtotal = nettotal - rebateprice; 
	  var vattotal = parseFloat(subtotal * this.vat);
	  var total = subtotal + vattotal;
 
	  if(this.isprepaid){
  		this.subtotallinedata = subtotal;
  		this.subtotalyearlinedate = subtotal * 12;
  		this.totallinedata = total * 12;
	  } else {
  		this.subtotallinedata = subtotal;
  		this.subtotalyearlinedate = subtotal;
  		this.totallinedata = total;
	  }	  	
	 
	  //output to browser
	  // - Rebate
	  if (this.isrebate)  {
	    if (rebateprice==0) {
	      if(this.rebatetotal) this.rebatetotal.innerText = '0.00';
	      if(this.rebatereturn) this.rebatereturn.value = 0;
	    } else {	
	      if(this.rebatetotal) this.rebatetotal.innerText = '-' + fixed(rebateprice);
	      if(this.rebatereturn) this.rebatereturn.value = rebateprice.formatMoney(2, ',', '.');
	    } 
	  }
	   
	  // - Storage price
	  if(this.isstorage) {
	    if(this.storagetotal) this.storagetotal.innerText = this.storageprice.formatMoney(2, decimalseparator, thousandseparator);
	    if(this.storagereturn) this.storagereturn.value = this.storageprice.formatMoney(2, ',', '.');
	  }
	  
	  // - Mailbox price 
	  if(this.ismailbox) {
	    if(this.mailboxtotal) this.mailboxtotal.innerText = this.mailboxprice.formatMoney(2, decimalseparator, thousandseparator);
	    if(this.mailboxreturn) this.mailboxreturn.value = this.mailboxprice.formatMoney(2, ',', '.');	   
	  }
	          
	  // - Total lines	 
	  if(this.totalyearnotax) this.totalyearnotax.innerText = this.subtotalyearlinedate.formatMoney(2, decimalseparator, thousandseparator);
	  if(this.totalyearnotaxreturn) this.totalyearnotaxreturn.value = this.subtotalyearlinedate.formatMoney(2, ',', '.');
	  if(this.subtotalline) this.subtotalline.innerText = this.subtotallinedata.formatMoney(2, decimalseparator, thousandseparator);
	  if(this.subtotalreturn) this.subtotalreturn.value = this.subtotallinedata.formatMoney(2, ',', '.');
	  if(this.totalline) this.totalline.innerText = this.totallinedata.formatMoney(2, decimalseparator, thousandseparator);
	  if(this.totalreturn) this.totalreturn.value = this.totallinedata.formatMoney(2, ',', '.');
	}
}
