var Options = function() {
	this.title = true;
	this.logo = true;
	this.bigIntro = true;
	//this.middleMenu = true;
	this.stepMenu = true;
	this.footer = true;
	this.data = null;
	this.html = [];

	this.ad_options = null;
}

Options.prototype.getData = function() {
	/*if (this.data !== null) {
		if (Configurator.onlyData === false) this.ini();
		return;
	}*/
	var params = [];
	params.push('market='+Configurator.marketID);
	params.push('region='+Configurator.regionID);
	params.push('sv='+Car.svID);
	params.push('gear='+Car.gearboxID);
	params.push('engine='+Car.engineID);
	params.push('car='+Car.modelID);
	params.push('mc='+Car.mc);
	params.push('roof=0');
	params.push('lng='+Configurator.language);
	params.push('DBnum='+ Configurator.DBnum);
	params.push('marketID='+Configurator.marketID);
	params.push('outletID='+ Configurator.outletID);
	params.push('uph='+ Car.upholsID);
	params.push('col='+ Car.colorID);
	params.push('rims='+ Car.rimsID);
	if (Car.customerID) params.push('customerID='+ Car.customerID);
	if (Car.customer_type) params.push('customer_type='+ Car.customer_type);
	ajax('/en/daction/uphRules/', 'Configurator.options.getBlockedOptions(\''+  params.join('&') +'\', '+ Configurator.onlyData +')', params.join('&'));
}

Options.prototype.getBlockedOptions = function(params, onlyData) {
	//document.body.innerHTML = ajaxReq.responseText;
	//return;
	try {
		Car.blockedOptions = [];
		Car.blockedOptionsData = eval('('+ ajaxReq.responseText +')');
	} catch(e) {
		alert('Unable to load blocked options.');
		document.body.innerHTML = ajaxReq.responseText;
		throw e;
	}
	for (el in Car.blockedOptionsData) {
		Car.blockedOptions.push(Car.blockedOptionsData[el].id);
	}
	if (this.data !== null) {
		if (Configurator.onlyData === false) this.ini();
		return;
	}
	ajax('/en/daction/step3/', 'Configurator.options.ini('+ onlyData +')', params);
}

Options.prototype.ini = function(onlyData) {
	//document.body.innerHTML = ajaxReq.responseText;
	//return;
	//Car.blockedOptions = [];

	this.html = [];
	if (this.data === null) {
		try {
			this.data = eval('('+ ajaxReq.responseText +')');
		} catch(e) {
			alert('Unable to load additional options.');
			document.body.innerHTML = ajaxReq.responseText;
			throw e;
		}
	}
	this.ad_options = this.data['options'];
	if (onlyData === true) {
		this.getPrice();
		return;
	}
	this.html.push('<table align="center" class="content"><tr><td class="content_cell">');
	this.html.push(Interface.showStepButtons());


	this.html.push('<table style="width:560px; margin: 10px 0 0 40px;">');
	var price = 0;
	var so = 0;
	var disabled = '', checked = '';
	var firstOneOfFinded = false;
	var blockedOptionsChecked = [];
	for (var el in this.data['options']) {
		this.html.push('<tr><td colspan="3" class="big_title" style="padding: 15px 0 10px 0;">'+ Interface.translate('option_category_'+ el) +'</td></tr>');
		so = this.data['options'][el].length
		for(var i = 0; i < so; ++i) {
			var currentOption = this.data['options'][el][i];
			price = currentOption[2];

			if(in_array(currentOption[1], Car.blockedOptions)) {
				disabled = '';
				checked = 'checked="checked"';
				for (var element in Car.blockedOptionsData) {
					if (Car.blockedOptionsData[element].id == currentOption[1]) {
						if (Car.blockedOptionsData[element].rule != '2') {
							disabled = ' disabled="disabled"';
						} else {
							if (!firstOneOfFinded) {
								firstOneOfFinded = true;
							} else {
								checked = '';
							}
						}
						if (Car.blockedOptionsData[element].rule === '0') {
							checked = '';
						}
						break;
					}
				}
				if (checked !== '') {
					blockedOptionsChecked.push(currentOption[1]);
				}

				this.html.push('<tr '+(i%2 == 0 ? 'class="white_line"' : '')+'><td class="w35" align="center">');
				this.html.push('<input type="checkbox" id="'+ currentOption[1] +'" ');
				this.html.push(' onclick="Rules.searchForRules(\''+currentOption[1]+'\', this.checked);"');
				this.html.push(disabled +' '+ checked +'/></td><td>('+currentOption[1]+') '+ currentOption[0] +'</td>');
				this.html.push('<td class="w60" align="right"><span>');
				this.html.push(Price.getPrice(price) +'</span>&nbsp;&nbsp;</td></tr>');
			} else {
				this.html.push('<tr '+(i%2 == 0 ? 'class="white_line"' : '')+'><td class="w35" align="center"><input type="checkbox" id="'+currentOption[1]+'" ');
				if (this.isChecked(currentOption[1])) this.html.push('checked="checked" ');
				this.html.push('onclick="Rules.searchForRules(\''+currentOption[1]+'\', this.checked);"/></td><td>');
				this.html.push('<label for="'+currentOption[1]+'"><span class="pointer">('+currentOption[1]+') ');
				this.html.push(currentOption[0] +'</span></label></td>');
				this.html.push('<td class="w60" align="right"><span class="pointer"><label for="'+currentOption[1]+'">'+ Price.getPrice(price) +'</label></span>&nbsp;&nbsp;</td></tr>');
			}
		}
	}
	this.html.push('<tr><td colspan="3" class="separator">&nbsp;</td></tr>');
	this.html.push('<tr class="white_line_bold"><td>&nbsp;</td><td>'+Interface.translate('total_eq_cost')+'</td>');
	this.html.push('<td align="right"><span id="options_total">0</span>&nbsp;&nbsp;</td></tr><tr><td colspan="3">&nbsp;</td></tr>');
	this.html.push('</table>');

	this.html.push('</td>');
	this.html.push(Interface.showRightMenu());
	this.html.push('</tr></table>');

	$('content').innerHTML = this.html.join('');
	this.getPrice();

	so = blockedOptionsChecked.length;
	for (i = 0; i < so; i++) {
		Rules.addOption(blockedOptionsChecked[i]);
	}

	Interface.loading(true);
}

Options.prototype.isChecked = function(id) {
	var so = Car.options.length;
	for (var i = 0; i < so; i++) {
		if (Car.options[i].id == id) return true;
	}
	return false;
}

Options.prototype.getPrice = function() {
	var so = Car.options.length;

	var so2 = 0;
	var temp = [];
	for (var i = 0; i < so; i++) temp.push(Car.options[i].id);
	Price.options = 0;

	for (el in Configurator.options.ad_options) {
		so2 = Configurator.options.ad_options[el].length;
		for (i = 0; i < so2; i++) {
			if (in_array(Configurator.options.ad_options[el][i][1], temp)) {
				//alert(Configurator.options.ad_options[el][i][2] +':'+ Configurator.options.ad_options[el][i][2].toFixed(0))
				//Price.options += Configurator.options.ad_options[el][i][2].format(0);
				if (in_array(Configurator.options.ad_options[el][i][1], temp)) Price.options += parseFloat(Configurator.options.ad_options[el][i][2].toFixed(0));
			}
		}
	}
	if ($('rm_options')) $('rm_options').innerHTML = Price.options.format(0)+'&nbsp;';
	if ($('options_total')) $('options_total').innerHTML = Price.options.format(0)+'&nbsp;';
	if ($('total_price')) $('total_price').innerHTML = Price.getTotalPrice()+'&nbsp;';
}
