function azercellTariff_UItools() {
	this.calc = new azercellTariffCalculator(azercellTariff_init_data);
	this.ie = /*@cc_on ! @*/ false;
	this.namespace = 'aztc_';
	this.deny_processing = false;
	this.errors = [];
	this.subs = [];

	this.print_r = function(variable, deep, index) {
		if (deep===undefined) {var deep = 0;}
		if (index===undefined) {var index = '';} else {index+=': ';}
		var mes = '';
		var i = 0;
		var pre = '\n';
		while (i<deep) {pre+='\t'; i++;}
		if (typeof(variable)=='object') {
			mes+=pre+index+' {';
			for (index in variable) {
				mes+=this.print_r(variable[index], (deep+1), index);
			}
			mes+=pre+'}';
		} else {
			mes+=pre+index+variable;
		}
		if (deep) {return mes;} else {alert(mes);}
	}
	this.array_remove = function(arr, val) {
		var res = [];
		for (i in arr) {
			if (isNaN(i)) {continue;}
			if (arr[i]==val) {continue;}
			res.push(arr[i]);
		}
		return res;
	}

	this.getRadioValue = function(radio_name) {
		var inputs = document.getElementsByName(radio_name);
		var j = 0;
		for (var i in inputs) {
			if (this.ie) {
				if (i!=radio_name) {continue;}
				if (inputs[j].checked) {return inputs[j].value;}
				j++;
			} else {
				if (!inputs.hasOwnProperty(i)) {continue;}
				if (inputs[i].checked) {return inputs[i].value;}
			}
		}
		return '0';
	}
	this.max = function(set) {
		var max_i = 0;
		var max_val = 'NONE';
		var cur_val = 0;
		for (var i in set) {
			if (!set.hasOwnProperty(i)) {continue;}
			if (isNaN(set[i])) {continue;}
			cur_val = parseFloat(set[i]);
			if (max_val=='NONE') {
				max_i = i; max_val = cur_val;
			} else {
				if (max_val<cur_val) {
					max_i = i; max_val = cur_val;
				}
			}
		}
		return max_i;
	}
	this.getCheckboxSetGreatestValue = function(checkboxes) {
		var values = [];
		for (var i = 0; i<checkboxes.length; i++) {
			if (checkboxes[i].checked) {
				values.push(checkboxes[i].value);
			}
		}
		var max = this.max(values);
		return values[max];
	}

	this.getEl = function(id) {
		return document.getElementById(this.namespace+id);
	}
	this.getValue = function(id) {
		var val = this.getEl(id).value;
		if (isNaN(val)) {
			var forced_param = this.namespace+id+'_true_val';
			if (this[forced_param]!==undefined) {
				val = this[forced_param];
			}
		}
		return val;
	}
	this.show = function(el) {
		el.style.display = 'block';
	}
	this.hide = function(el) {
		el.style.display = 'none';
	}
	this.copy = function(from, to) {
		var target = document.getElementById(to);
		target.value = document.getElementById(from).value;
		this.check(target);
	}
	this.check = function(input) {
		var max = parseInt(input.getAttribute('max'));
		var min = parseInt(input.getAttribute('min'));
		var error = false;

		if (isNaN(input.value)) {
			error = true;
		} else if ((input.value<min)||(input.value>max)) {
			error = true;
		}

		var input_control = input.parentNode.parentNode;
		if (error) {
			input_control.className = input_control.className+'_err';
			this.errors.push(input_control.id);

			var err = document.createElement('div'); // pop-up notification
			err.setAttribute('id', input_control.id+'_errorNotificator');
			err.className = this.namespace+'errorNotificator';
			input_control.appendChild(err);
			err.innerHTML = '<p>'+azercellTariff_errors['EXC']+'</p>';
		} else {
			input_control.className = input_control.className.replace('_err', '');
			this.errors = this.array_remove(this.errors, input_control.id);

			var err = document.getElementById(input_control.id+'_errorNotificator');
			if (err) {input_control.removeChild(err);}
		}
	}

	// * * * //

	this.reset = function() {
		var butdiv = this.getEl('calcCtrlCnt'); this.show(butdiv);
		var resdiv = this.getEl('calcResCnt'); this.hide(resdiv);
	};

	this.hideOptionalSubs = function(subs) {
		for (var i in subs) {
			if (!subs.hasOwnProperty(i)) {continue;}
			var el = this.getEl(subs[i]);
			var target = this.getEl(subs[i]+'_target');
			if ((el!=undefined)&&(target!=undefined)) {
				target.style.display = (el.checked? 'block': 'none');
				this.subs[i] = {
					el: el,
					target: target
				};
			}
		}
	};

	this.process = function() {
		//if (this.deny_processing) {return true;}
		if (this.errors.length) {
			this.getEl('aCalculateButton').href = '#'+this.errors[0];
			return true;
		}

		this.getEl('aCalculateButton').href = '#aztc_calcResCnt';

		var input = {
			daily_calls_amount: this.getValue('inputCallsAmount'),
			avg_calls_duration: this.getValue('inputAvgCallDuration'),
			onnet_calls_percentage: this.getRadioValue('destination_alt'),
			offpeak_use_percentage: this.getRadioValue('offpeak_alt'),
			long_calls_percentage: this.getRadioValue('long_calls_alt')
		};

		var recommended_tariff_index = this.calc.calculate(input);
		var recommended_tariff = azercellTariff_init_data.tariffset[recommended_tariff_index];

		var butdiv = this.getEl('calcCtrlCnt'); //this.hide(butdiv);
		var resdiv = this.getEl('calcResCnt'); this.show(resdiv);
		var target = this.getEl('calcResTarget');
		var showit = this.getEl('aShowButton');
		target.innerHTML = '<a href="'+recommended_tariff.tariff_url+'" title="">'+recommended_tariff.advice_title+'</a>';
		showit.href = recommended_tariff.tariff_url;

		this.processSubs();

		/*var result_inputs = document.getElementsByName('tariff[]');
		var j = 0;
		for (var i in result_inputs) {
			if (this.ie) {
				if (i!='tariff[]') {continue;}
				result_inputs[j].value = this.calc.results[j].toFixed(2);
				j++;
			} else {
				if (!result_inputs.hasOwnProperty(i)) {continue;}
				result_inputs[i].value = this.calc.results[i].toFixed(2);
			}
		}
		document.getElementById('outputResultRecommendation').value = recommended_tariff_index;*/
		return false;
	}

	this.processSubs = function() {
		var res = {};
		var lng = azercellTariff_init_data.lang;
		var pkg = azercellTariff_init_data.packageset;
		var k = 0;
		for (var i in this.subs) {
			if (!this.subs.hasOwnProperty(i)) {continue;}
			var type = this.subs[i].el.id.replace(this.namespace+'switch', '');
			switch (type) {
				case 'SMS':
					var val = this.getValue('inputSMSAmount');
				break;
				case 'MMS':
					var val = this.getRadioValue('mms_alt');
				break;
				case 'GPRS':
					//var val = this.getRadioValue('gprs_alt');
					var val = this.getCheckboxSetGreatestValue(document.getElementsByName('gprs_alt[]'));
				break;
			}
			if ((this.subs[i].el.checked)&&(val!=undefined)) {
				res[type] = pkg[type].callback_func(val, pkg[type].corellation);
				k++;
			}
			type = null; val = null;
		}

		var targets_holder = this.getEl('packagesResults');
		var target_content = '';
		var j = 0;
		for (var i in res) {
			if (!res.hasOwnProperty(i)) {continue;}
			if (i=='GPRS') {var rstr = ((res[i].amount<1000)? (res[i].amount+' MB'): (Math.round(res[i].amount/1000)+' GB'));} else {var rstr = res[i].amount;}
			rstr = lng.pack_note.replace('{amount}', rstr).replace('{service}', i);
			if (res[i].url!=undefined) {rstr = '<a href="'+res[i].url+'" title="">'+rstr+'</a>';}
			if (res[i].saves!=undefined) {rstr+=lng.pack_save_note.replace('{saves}', res[i].saves);}
			target_content+='<div class="aztc_'+i+'pack"'+((j==(k-1))? ' id="aztc_packagesResults_last"': '')+'>'+rstr+'</div>';
			rstr = null;
			j++;
		}

		targets_holder.innerHTML = target_content;
	}

	var ui = this;
	this.hideNotice = function(target) {
		var err_id = target.id+'_errorNotificator';
		var err = document.getElementById(err_id);
		if (err) {
			target.removeChild(err);
			//alert('ui.array_remove('+ui.errors+', '+err_id+')');
			ui.errors = ui.array_remove(ui.errors, err_id);
			//ui.print_r(ui.errors);
		}

		var parent = target.parentNode; // turn off hightlighting
		parent.className = parent.className.replace('aztc_err', '');
		parent.className = parent.className.replace(' ', '');
		var children = target.getElementsByTagName('div');
		for (i in children) {
			if (isNaN(i)) {continue;}
			if (children[i].className==(ui.namespace+'slider_hood_err')) {
				children[i].className = children[i].className.replace('_err', '');
			}
			if (children[i].className==(ui.namespace+'slider_elapse')) {
				children[i].style.background = '#F8F8F8';
			}
		}
	}
	this.notify = function(note, target) {
		ui.hideNotice(target);
		//ui.deny_processing = true;

		var parent = target.parentNode; // hightlight block
		parent.className = parent.className+' aztc_err';
		var children = target.getElementsByTagName('div');
		for (i in children) {
			if (isNaN(i)) {continue;}
			if (children[i].className==(ui.namespace+'slider_hood')) {
				children[i].className = children[i].className+'_err';
			}
			if (children[i].className==(ui.namespace+'slider_elapse')) {
				children[i].style.background = '#FDF2F8';
			}
		}

		var err = document.createElement('div'); // pop-up notification
		var err_id = target.id+'_errorNotificator';
		err.setAttribute('id', err_id);
		//err.setAttribute('class', ui.namespace+'errorNotificator');
		err.className = ui.namespace+'errorNotificator';
		target.appendChild(err);
		err.innerHTML = '<p>'+note+'</p>';

		ui.errors.push(err_id);
	}
}
