var Process = Class.create({
  CLASSDEF: {
      name: 'Process'
  },
  
  initialize: function(options) {
    this.id = options.id;
    this.abbr = options.abbr;
    this.name = options.name;
    this.zIndex = options.z;
    this.usesDPI = options.dpi;
    this.flags = options.flags;
    
    this.allowImageUpload = options.allow_image_upload;
    this.allow = {};
    this.allow["image"] = this.allowImages = options.allow_images;
    this.allow["text"] = this.allowText = options.allow_text;
    this.allow["teamname"] = this.allowTeamNames = options.allow_teamnames;
    
    //this.allowDigitization = options.dig;
    this.digitizationFee = options.dig_fee;
    this.digitizationFeePerInch = options.dig_fee_per_inch;
    this.stitchesPerInch = options.s_per_inch;
    this.maxColors = options.max_colors;
    this.autoSplit = options.auto_split;
    this.defaultTextColorId = options.def_text_color;
  },
  
  isWilcomEMB: function() {
    return ((this.flags & 1) == 1);
  }
});

var PriceTable  = Class.create({
  CLASSDEF: {
      name: 'PriceTable'
  },
  
  initialize: function(options) {
    this.id = options.id;
    this.name = options.n;
    this.rows = options.rows;
    //remove repeater....
    this.rowRepeater = this.rows.splice(this.rows.length-1,1)[0];
    this.cols = options.cols;
    this.prices = options.p;
  },
  
  calcPrice: function(rowValue, colValue) {
		log("PriceTable.calcPrice:" + rowValue + "," + colValue);
		var rowIndex = -1; 
		var colIndex = -1;
		for(var i=0; i < this.rows.length; i++) { //skip first row (the end repeater..)
			var row = this.rows[i];
			if((rowValue >= row[0])&&(rowValue <= row[1])) {
				rowIndex = i;
				break;
			}
		}
		for(var i=0; i < this.cols.length; i++) {
			var col = this.cols[i];
			if((colValue >= col[0])&&((colValue <= col[1])||(col[1] == -1))) {
				colIndex = i;
				break;
			}
		}
		if(colIndex == -1) {
			log("PriceTable.calcPrice:Unable to get the price column for " + colValue);
			return 0;
		}
		if(rowIndex == -1) { //we are above largest defined range... use repeater...
			log(this.rowRepeater);
			
			var price = (this.rows.length == 0) ? 0 : this.prices[colIndex][this.rows.length-1];
			var basePrice = price;
			//TODO: deduct stitch counts then mod to get repeat count...
			var left = rowValue - ((this.rows.length == 0) ? 0 : this.rows[this.rows.length - 1][1]);
			var blocks = parseFloat(left) / parseFloat(this.rowRepeater[1]);
			//round blocks up
			blocks = Math.ceil(blocks);
			price += blocks * this.prices[colIndex][this.rows.length];
			log("PriceTable.calcPrice: Row Index using repeater, basePrice=" + basePrice + " price=" + price + " left=" + left + " blocks=" + blocks);
			var from = ((this.rows.length == 0) ? 0 : this.prices[colIndex][this.rows.length-1][0]) + ((blocks -1) * this.rowRepeater[1]);
			var to = from + this.rowRepeater[1] ;
			log("PriceTable.calcPrice: Price=" + price);
			return [price, from, to];
		} else {
			log("Price Table price=" + this.prices[colIndex][rowIndex] );
			return [this.prices[colIndex][rowIndex], this.rows[rowIndex][0], this.rows[rowIndex][1]];
		}
	}
});
