var PdfActions = function() {
	this.logo = true;
	this.showSeparator = true;
	this.footer = true;
	this.alternative_languages = true;

	this.html = [];
	this.showFilters = false;
	this.data = null;


	this.modelID = null;
	this.dealerID = null;
	this.sv = null;
	this.currency = null;
	this.language = null;
}

PdfActions.prototype.getData = function() {
	if (this.showFilters === false) {
		this.ini();
		return;
	}

	var params = [];
	params.push('modelID='+ this.modelID);
	params.push('dealerID='+ this.dealerID);
	params.push('sv='+ this.sv);
	params.push('currency='+ this.currency);
	params.push('language='+ this.language);

	ajax('/en/daction/pricelist_filters/', 'Configurator.pdf.getDataHandler('+ Configurator.onlyData +')', params.join('&'));
}

PdfActions.prototype.getDataHandler = function(onlyData) {
//	document.body.innerHTML = '<pre>'+ajaxReq.responseText;
//	return;
	try {
		this.data = eval('('+ ajaxReq.responseText +')');
	} catch(e) {
		alert('Can\'t load PDF data.');
		throw e;
		return;
	}
	if (onlyData === false) this.ini();
}

PdfActions.prototype.ini = function() {

	this.html = [];
	this.html.push('<table align="center" class="content"><tr><td class="content_cell" align="center" valign="top">');

	if (this.showFilters === false) {
		if (mGLOBALS['db_update'] == '1') {
			this.html.push('<table class="pdf_or_configurator" id="substep1">');
			this.html.push('<tr><td align="center">Maintenance<br><br><div style="background: url(\'/_/loading_gray.gif\') top center no-repeat; height: 50px;"></div></td></tr>');
			this.html.push('</table>');
		} else {
			// step part 1
			this.html.push('<table class="pdf_or_configurator" id="substep1">');
			this.html.push('<tr><td><img src="/_/pricelist_download.jpg" alt="pricelist download" class="pointer" ');
			this.html.push('onclick="Configurator.pdf.showFilters=true; Configurator.ini();" /></td>');
			this.html.push('<td align="right"><img src="/_/car_configurator1.jpg" alt="car configurator" class="pointer" ');
			this.html.push(' onclick="Configurator.currentStep = \'dealers\'; Configurator.ini();"/></td></tr>');
			this.html.push('</table>');
		}
	} else {
		// step part 2
		this.html.push('<table class="pdf_or_configurator">');
		this.html.push('<tr><td valign="top"><table class="pdf_filters">');

		this.html.push('<tr><td class="pdf_main_cell" valign="top"><table class="pdf_filter_item">');

			// dealer
			this.html.push('<tr class="item">');
			this.html.push('<td class="left"></td>');
			this.html.push('<td class="center_part" align="left"><b>'+ Interface.translate('dealer') +'</b></td>');
			this.html.push('<td class="center_part" align="right"><select id="pdf_dealers" onchange="Configurator.pdf.dealerID=this.value; Configurator.ini();">');

			//var i = 0;
			for (el in this.data.dealers) {
				//if (this.dealerID === null && !i) this.dealerID = el;
				this.html.push('<option value="'+ el +'"'+ (this.dealerID == el ? ' selected="selected"' : '') +'>'+ this.data.dealers[el] +'</option>');
				//i++;
			}

			this.html.push('</select></td>');
			this.html.push('<td class="right"></td>');
			this.html.push('</tr><tr class="space"><td colspan="4"></td>');

			// model
			this.html.push('<tr class="item">');
			this.html.push('<td class="left"></td>');
			this.html.push('<td class="center_part" align="left"><b>'+ Interface.translate('model') +'</b></td>');
			this.html.push('<td class="center_part" align="right"><select id="pdf_models" onchange="Configurator.pdf.modelID=this.value; Configurator.ini();">');

			//i = 0;
			for (el in this.data.models) {
				//if (this.modelID === null && !i) this.modelID = el;
				this.html.push('<option value="'+ el +'"'+ (this.modelID == el ? ' selected="selected"' : '') +'>'+ this.data.models[el] +'</option>');
				//i++;
			}

			this.html.push('</select></td>');
			this.html.push('<td class="right"></td>');
			this.html.push('</tr><tr class="space"><td colspan="4"></td>');

			// sales version
			this.html.push('<tr class="item">');
			this.html.push('<td class="left"></td>');
			this.html.push('<td class="center_part" align="left"><b>'+ Interface.translate('sv') +'</b></td>');
			this.html.push('<td class="center_part" align="right"><select id="pdf_sv" onchange="Configurator.pdf.sv=this.value; Configurator.ini();">');

			//i = 0;
			for (el in this.data.sv_categories) {
				//if (this.sv === null && !i) this.sv = el;
				this.html.push('<option value="'+ el +'"'+ (this.sv == el ? ' selected="selected"' : '') +'>'+ this.data.sv_categories[el] +'</option>');
				//i++;
			}

			this.html.push('</select></td>');
			this.html.push('<td class="right"></td>');
			this.html.push('</tr><tr class="space"><td colspan="4"></td>');

			// currency
			this.html.push('<tr class="item">');
			this.html.push('<td class="left"></td>');
			this.html.push('<td class="center_part" align="left"><b>'+ Interface.translate('currency') +'</b></td>');
			this.html.push('<td class="center_part" align="right"><select id="pdf_currencies" onchange="Configurator.pdf.currency=this.value; Configurator.ini();">');

			//i = 0;
			for (el in this.data.currencies) {
				//if (this.currency === null && !i) this.currency = el;
				this.html.push('<option value="'+ el +'"'+ (this.currency == el ? ' selected="selected"' : '') +'>'+ this.data.currencies[el] +'</option>');
				//i++;
			}

			this.html.push('</select></td>');
			this.html.push('<td class="right"></td>');
			this.html.push('</tr><tr class="space"><td colspan="4"></td>');

			// language
			this.html.push('<tr class="item">');
			this.html.push('<td class="left"></td>');
			this.html.push('<td class="center_part" align="left"><b>'+ Interface.translate('language') +'</b></td>');
			this.html.push('<td class="center_part" align="right"><select id="pdf_languages" onchange="Configurator.pdf.language=this.value; Configurator.ini();">');

			//i = 0;
			for (el in this.data.lngs) {
				//if (this.language === null && !i) this.language = el;
				this.html.push('<option value="'+ el +'"'+ (this.language == el ? ' selected="selected"' : '') +'>'+ this.data.lngs[el] +'</option>');
				//i++;
			}

			this.html.push('</select></td>');
			this.html.push('<td class="right"></td>');
			this.html.push('</tr><tr class="space"><td colspan="4"></td>');

			// download button
			this.html.push('<tr height="99%"><td colspan="4" /></tr>');
			this.html.push('<tr><td colspan="4" align="right" class="button" valign="middle"><table class="pointer" ');
			this.html.push('onclick="Configurator.pdf.downloadPDF();"><tr><td>');
			this.html.push('<img src="/_/download_pdf_icon.jpg" alt="download"></td>');
			this.html.push('<td class="w5" /><td><b>'+ Interface.translate('pricelist_pdf') +'</b></td></tr></table></td></tr>');

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

		this.html.push('<td align="right"><img src="/_/car_configurator2.jpg" alt="car configurator" class="pointer" ');
		this.html.push(' onclick="Configurator.currentStep = \'dealers\'; Configurator.ini();"/></td></tr>');
		this.html.push('</table>');
	}
	this.html.push('</td></tr></table>');

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

	this.dealerID = $F('pdf_dealers');
	this.modelID = $F('pdf_models');
	this.sv = $F('pdf_sv');
	this.currency = $F('pdf_currencies');
	this.language = $F('pdf_languages');

	Interface.loading(true);

}
PdfActions.prototype.downloadPDF = function() {
	//alert(this.getPricelistParameters());
	//return;
	location.href = '/en/daction/download_pdf?'+ this.getPricelistParameters();
}

PdfActions.prototype.getPricelistParameters = function() {
	return 'year='+ this.data['year'] +
		'&model='+ this.modelID +
		'&dealer='+ this.dealerID +
		'&cur='+ this.currency +
		'&lang='+ this.language +
		'&price_type=20'+
		'&mc=10'+
		(this.sv ? '&sv_category='+ this.sv : '');
}
