function num_round(n)
{
	_s1=""+n;
	if (_s1.indexOf(".")!=-1) 
		_s1=_s1.substring(0,_s1.indexOf(".")+3);
	return _s1;
}

function OrderRow(id, arrProperties) {

  this.initParameters(id);

  for (var i in arrProperties) {
    this.parameters[i] = arrProperties[i];
  }

}

// далее прототип

  OrderRow.prototype.useTypes = [
    '<Choose Aim of Use>',
    'Corporate Printing',
    'Printing with Commercial Purposes',
    'Publishing Purposes',
    'Mass Media Advertising',
    'Outdoor Advertising',
    'Interior and Private Purposes',
    'Internet Publishing'
  ];
  
  OrderRow.prototype.formats = [
    [],
    ['A8','A6','A5','A4','A3','A2','A1'],
    ['A8','A6','A5','A4','A3','A2','A1'],
    ['A8','A7','A6','A5','A4','A3 and more'],
    ['A8','A6','A5','A4','A3','A2','A1'],
    ['Up to 05 sq.m.','1 sq.m.','2 sq.m.','3 sq.m.','5 sq.m.','7 sq.m.','10 sq.m.','More than 10 sq.m.'],
    ['A4','A3','A2','A1','More than A1'],
    ['3 months','6 months','1 year']
  ]
  OrderRow.prototype.largeFlags = [
    0,
    5,
    5,
    5,
    5,
    7,
    2,
    0
  ]
  OrderRow.prototype.formatDescriptions = [
    '',
    'Format',
    'Format',
    'Format',
    'Format',
    'Size',
    'Format',
    'Period'
  ]  
  OrderRow.prototype.circulations = [
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    ['512x384','768x512','1024x768']
  ]  
  OrderRow.prototype.circulationDescriptions = [
    '',
    'Circulation',
    'Circulation',
    'Amount of Publications',
    'Amount of Publications',
    'Circulation',
    'Amount of Photos',
    'Size'
  ]  
  OrderRow.prototype.factors = [
    {circulation: [0,0],      period: [0, '']},
    {circulation: [5000,1],   period: [12,'month(s)']},
    {circulation: [100000,1], period: [12,'month(s)']},
    {circulation: [1,1],      period: [0, '']},
    {circulation: [1,1],      period: [3, 'month(s)']},
    {circulation: [1,1],      period: [1, 'month(s)']},
    {circulation: [1,2],      period: [0, '']},
    {circulation: [0,0],      period: [0, '']}
  ]  
  OrderRow.prototype.prices = [
    [
      [0]
    ],
    [
      [1500,1900,2400,3000,4500,6000,7500]
    ],
    [
      [4500,5200,6000,7500,9000,10500,12000]
    ],
    [
      [200,300,400,500,700,900]
    ],
    [
      [4500,5200,6000,7500,9000,10500,12000]
    ],
    [
      [1800,2700,4700,5900,7200,8000,9400,12000]
    ],
    [
      [1000,1300,1950,2535,3275],
      [1200,1560,2340,3042,4563],
      [1440,1872,2808,3650,5475],
      [1720,2236,3354,4360,6540],
      [1900,2470,3705,4816,7224]
    ],
    [
      [500,650,750],
      [650,750,870],
      [901,1035,1200]
    ]
  ]
  
  OrderRow.prototype.initParameters = function(id) {
    this.parameters = new Array();
    this.parameters['id']          = id;
    this.parameters['useType']     = 0;
    this.parameters['circulation'] = 1;
    this.parameters['period']      = 1;
    this.parameters['format']      = 0;
    this.parameters['imageType']   = 0; //0 - обычное, 1 - панорама, 2 - Третьяковка, 3 - Русский музей
    this.parameters['flagPublic']  = ''; //при оформлении частного интерьера, если будет использовано в общественных местах
  }
  
  OrderRow.prototype.getPrice = function() {
    var factor = 1;
    var add = 0;
    var this_circulation = parseInt(this.parameters['circulation']);
    if (isNaN(this_circulation))
      this_circulation = 0;
		if (this.parameters['useType'] == 7)
			this_circulation++;
    var max_circulation = this.prices[this.parameters['useType']].length;
    var cur_circulation = Math.max(1,Math.min(max_circulation,this_circulation));
    if (this_circulation > max_circulation) {
      if (this.factors[this.parameters['useType']]['circulation'][1] == 1) {
        factor = factor + 0.3*(Math.ceil(this_circulation / this.factors[this.parameters['useType']]['circulation'][0]) - 1);
      } else if (this.factors[this.parameters['useType']]['circulation'][1] == 2) {
        add = add + (this_circulation - max_circulation)*120;
      }
    }
    var this_period = parseInt(this.parameters['period']);
    if (isNaN(this_period))
      this_period = 0;
    if (this.factors[this.parameters['useType']]['period'][0] > 0) {
      factor = factor + 0.3*(Math.ceil(Math.max(1,this_period) / this.factors[this.parameters['useType']]['period'][0]) - 1);
    }
    if (this.parameters['imageType'] == 1) {
      factor = factor + 0.15;
    } else if (this.parameters['imageType'] == 2) {
      add = add + 3500;
    } else if (this.parameters['imageType'] == 3) {
      add = add + 2000;
    }
    if (this.parameters['flagPublic'] == 'on') {
      factor = factor + 0.9;
    }
    return Math.round(this.prices[this.parameters['useType']][cur_circulation-1][this.parameters['format']]*factor + add);
  }
  OrderRow.prototype.renderPrice = function() {
    var price = this.getPrice();
    $('price_'+this.parameters['id']).innerHTML = "" + (num_round(price * r_to_ue)) + " $";
    $('sum_'+this.parameters['id']).value = num_round(price);
  }
  OrderRow.prototype.getWeight = function() {
    return 0;
  }
  OrderRow.prototype.render = function(div_thisrow) {
    var el;
    div_thisrow.innerHTML = '';
    div_thisrow.insert('<label>Aim of Use: </label>');
    el = new Element('select',{id: 'useType_'+this.parameters['id'], name: 'use_'+this.parameters['id']});
    options_init(el,this.useTypes);
    el.options[this.parameters['useType']].selected = true;
    el.onchange = function() {update_row(this.id, this.options[this.selectedIndex].value)};
    div_thisrow.insert(el);
    div_thisrow.insert('<br />');
    if (this.parameters['useType'] > 0) {
      div_thisrow.insert('<label>'+this.formatDescriptions[this.parameters['useType']]+': </label>');
      el = new Element('select',{id: 'format_'+this.parameters['id'], name: 'for_'+this.parameters['id']});
      options_init(el,this.formats[this.parameters['useType']]);
      el.options[this.parameters['format']].selected = true;
      el.onchange = function() {update_row(this.id, this.options[this.selectedIndex].value)};
      div_thisrow.insert(el);
      div_thisrow.insert('<br />');
      div_thisrow.insert('<label>'+this.circulationDescriptions[this.parameters['useType']]+': </label>');
      if (this.circulations[this.parameters['useType']].length > 0 ) {
        el = new Element('select',{id: 'circulation_'+this.parameters['id'], name: 'tir_'+this.parameters['id']});
        options_init(el,this.circulations[this.parameters['useType']]);
        el.options[this.parameters['circulation']].selected = true;
        el.onchange = function() {update_row(this.id, parseInt(this.options[this.selectedIndex].value))};
      } else {
        el = new Element('input',{id: 'circulation_'+this.parameters['id'], name: 'tir_'+this.parameters['id'], type: 'text', value: ''+this.parameters['circulation']});
        el.onblur = function() {
          if (isNaN(parseInt(this.value)) || parseInt(this.value) < 1)
            this.value = 1;
          update_row(this.id, parseInt(this.value));
        };
      }
      div_thisrow.insert(el);
      div_thisrow.insert('<br />');
      if (this.factors[this.parameters['useType']]['period'][0] > 0) {
        div_thisrow.insert('<label>Period of Use: </label>');
        el = new Element('input',{id: 'period_'+this.parameters['id'], name: 'per_'+this.parameters['id'], type: 'text', value: ''+this.parameters['period']});
        el.onblur = function() {
          if (isNaN(parseInt(this.value)) || parseInt(this.value) < 1)
            this.value = 1;
          update_row(this.id, parseInt(this.value));
        };
        div_thisrow.insert(el);
        div_thisrow.insert(' '+this.factors[this.parameters['useType']]['period'][1]);
        div_thisrow.insert('<br />');
      }
    }
    
    if (this.parameters['useType'] == 6) {
      div_thisrow.insert('<label>Will be used in decoration of public places: </label>');
      el = new Element('input',{id: 'flagPublic_'+this.parameters['id'], name: 'fp_'+this.parameters['id'], type: 'checkbox', checked: (this.parameters['flagPublic'] == 'on')});
      el.onclick = function() {update_row(this.id, (this.checked?'on':''))};
      div_thisrow.insert(el);
    }
    
    el = new Element('input',{name: 'imgtype_'+this.parameters['id'], type: 'hidden', value: this.parameters['imageType']});
    div_thisrow.insert(el);
    if (this.parameters['imageType'] == 2) {
      div_thisrow.insert('<p class="rights">The owner of this image is the State Tretiakov\'s gallery. The charges will cost 3500 roubles.</p>');
    } else if (this.parameters['imageType'] == 3) {
      div_thisrow.insert('<p class="rights">The owner of this image is the State Russian Museum. The charges will cost 2000 roubles.</p>');
    }
    
    el = new Element('p',{id: 'warning_'+this.parameters['id']});
    el.insert('Please contact our manager to get more information about cost and availability of image of so large size');
    div_thisrow.insert(el);
    this.warnIfLarge();
    
    this.renderPrice();
  }
  
  OrderRow.prototype.modify = function(prop, val) {
    this.parameters[prop] = val;
    if (prop == 'useType'){
	    this.parameters['period']      = 1;
	    this.parameters['format']      = 0;
      if (val == 7) {
        this.parameters['circulation'] = 0;
      } else {
        this.parameters['circulation'] = 1;
      }
      var el = $('row_'+this.parameters['id']);
      this.render(el);
    } else {
      this.renderPrice();
      if (prop == 'format'){
        this.warnIfLarge();
      }  
    }
  }
  
  OrderRow.prototype.warnIfLarge = function() {
    var el = $('warning_'+this.parameters['id']);
    if (this.largeFlags[this.parameters['useType']] > 0 && this.parameters['format'] >= this.largeFlags[this.parameters['useType']]) {
      el.show();
    } else {
      el.hide();
    }
  }
