Table = new Object();

Table.loader= function(flag) {
	if (flag) {
		$('table_loader').style.display='block';
	}
	else {
		$('table_loader').style.display='none';
	}
}

Table.Load = function() {	
	var line_num = $('select_line').value;
	Table.divEl = $('main_table_div');
	Table.loader(true);
	new Ajax.Request('servicies/analyts.php', {
		 parameters: {set: line_num},		
		 onSuccess: function(transport) {
		 	Table.units=Array();
			Table.data = transport.responseText.evalJSON();			
    		Table.CreateTable();
    		Table.loader(false); 	  
  		 }
	});
}

Table.CreateTable = function() {
	var result = '<table id="main_table"><tr class="my_caption"><td class="td_analyt">Аналит</td><td class="td_method">Метод</td><td  class="td_num">Значение N</td><td class="td_num">Значение H</td><td class="td_unit">Единицы измерения</td></tr>';
	for (var i=0; i<Table.data.length; i++) {
		result += Table.CreateRow(Table.data[i]);
	}
	result += '</table><input type="hidden" value="0" name="is_send_value" id="is_send_value" /><br />';
	result += '<span id="table_buttons">';
	result += '<input class="my_button" type="button" value="Отправить данные" onclick="Table.Send();" /></span><input type="hidden" name="simple" value="0" id="table_simple" /> ';
	result += ' <input class="my_button" type="button" value="Определить своё относительное смещение" onclick="Table.Calculate();" />';
	Table.divEl.innerHTML = result;
}

Table.Send = function() {
	if (Table.TotalCorrect()) {	
		$('table_simple').value = '1';
		new Ajax.Request('calculation.php', {
			 parameters: $('main_form').serialize(true),		
			 onSuccess: function(transport) {
			 	$('is_send_value').value = '1';
			 	$('table_simple').value = '0';
			 	$('table_buttons').innerHTML = 'Ваши данные успешно отправлены ';
  			 }
		});
	}
}

Table.Calculate = function() {
	if (Table.TotalCorrect()) {	
		$('main_form').submit();
	}
}

Table.CreateRow = function(dataEl) {
	var result = '<tr class="disabled" id="tr_'+dataEl['analyt']['id']+'">';
	result += Table.CreateAnalytCell(dataEl['analyt']);
	result += Table.CreateMethodCell(dataEl['analyt']['id'], dataEl['methods']);
	result += Table.CreateNCell(dataEl['analyt']['id']);
	result += Table.CreateHCell(dataEl['analyt']['id']);
	result += Table.CreateUnitCell(dataEl['analyt']['id'], dataEl['methods']);
	result += '</tr>';
	return result;
}

Table.CreateAnalytCell = function(analyt) {
	var result = '<td><input id="cor_analyt_'+analyt['id']+'" name="analyt_'+analyt['id']+'" value="1" type="checkbox" onclick="Table.AnalytCheck(this, '+analyt['id']+')"/> '+analyt['name']+'</td>';
	return result;
}

Table.CreateMethodCell = function(id, methods) {	
	var result = '<td><div id="td_method0_'+id+'" >&nbsp;</div><div id="td_method_'+id+'" style="display: none">';	
	result += '<input type="radio" id="method_radio_'+id+'_custom" name="method_radio_'+id+'" value="custom" onclick="Table.MethodCustomOn('+id+')" checked /> ';
	result += '<select name="method_'+id+'" class="method_input" id="method_select_'+id+'" onchange="Table.ChangeUnits('+id+')">';
	Table.units[id] = Array();
	for (var i=0; i<methods.length; i++) {
		result += '<option value="'+methods[i]['id']+'">'+methods[i]['name']+'</option>';
		Table.units[id][methods[i]['id']] = methods[i]['units'];
	}
	result += '</select><br />';
	result += '<input type="radio" id="method_radio_'+id+'_user" name="method_radio_'+id+'" value="user" onclick="Table.MethodUserOn('+id+')"/> ';
	result += '<input type="text" name="user_method_'+id+'" id="method_text_'+id+'" class="method_input" disabled />';
	result += '</div></td>';
	return result;
}

Table.CreateNCell = function(id) {
	var result = '<td class="tr_caption"><div id="td_N0_'+id+'" >&nbsp;</div><div id="td_N_'+id+'" style="display: none;"><input id="cor_N_'+id+'" type="text" onchange="Table.Correct(this);" name="N_'+id+'" class="number_input" /></div></td>';
	return result;
}

Table.CreateHCell = function(id) {
	var result = '<td class="tr_caption"><div id="td_H0_'+id+'" >&nbsp;</div><div id="td_H_'+id+'" style="display: none;"><input id="cor_H_'+id+'" type="text" onchange="Table.Correct(this);" name="H_'+id+'" class="number_input" /></div></td>';
	return result;
}

Table.CreateUnitCell = function(id, methods) {
	var result = '<td class="tr_caption"><div id="td_unit0_'+id+'" >&nbsp;</div><div id="td_unit_'+id+'" style="display: none"><span id="unit_span_'+id+'">'+methods[0]['units']+'</span><input style="display: none" id="unit_input_'+id+'" type="text" name="user_units_'+id+'" class="unit_input" /></div></td>';
	return result;
}

Table.AnalytCheck = function(el, id) {
	if (el.checked) {
		$('tr_'+id).className = '';
		$('td_N_'+id).style.display = 'block';
		$('td_N0_'+id).style.display = 'none';
		$('td_H_'+id).style.display = 'block';
		$('td_H0_'+id).style.display = 'none';
		$('td_method_'+id).style.display = 'block';
		$('td_method0_'+id).style.display = 'none';
		$('td_unit_'+id).style.display = 'block';
		$('td_unit0_'+id).style.display = 'none';
	}
	else {
		$('tr_'+id).className = 'disabled';
		$('td_N_'+id).style.display = 'none';
		$('td_N0_'+id).style.display = 'block';
		$('td_H_'+id).style.display = 'none';
		$('td_H0_'+id).style.display = 'block';
		$('td_method_'+id).style.display = 'none';
		$('td_method0_'+id).style.display = 'block';
		$('td_unit_'+id).style.display = 'none';
		$('td_unit0_'+id).style.display = 'block';				
	}
}

Table.ChangeUnits = function(id) {
	$('unit_span_'+id).innerHTML = Table.units[id][$('method_select_'+id).value];
}

Table.MethodUserOn = function(id) {
	$('method_radio_'+id+'_user').checked = true;
	$('method_select_'+id).disabled = true;
	$('method_text_'+id).disabled = false;
	$('unit_span_'+id).style.display = 'none';
	$('unit_input_'+id).style.display = 'block';
}

Table.MethodCustomOn = function(id) {
	$('method_select_'+id).disabled = false;
	$('method_text_'+id).disabled = true;
	$('unit_input_'+id).style.display = 'none';
	$('unit_span_'+id).style.display = 'block';
	
}

Table.Correct = function(el) {
	if (!Table.isMyNumber(el.value)) {
		alert('Введите, пожалуйста, корректное число, например: 2.15');
		el.value = '';
	}
}

Table.isMyNumber = function(s) {
	return (!isNaN(s));
}

Table.TotalCorrect = function() {
	var result = true;
	var id_n;
	var is_any = false;
	for (var i=0; i<Table.data.length; i++) {
		id_n = Table.data[i]['analyt']['id'];
		if ($('cor_analyt_'+id_n).checked) {		
			is_any = true;		
			if (($('cor_N_'+id_n).value == '') || ($('cor_H_'+id_n).value == '')) {
				result = false;
				break;
			}
			if (!$('method_text_'+id_n).disabled) {
				if (($('method_text_'+id_n).value == '') || ($('unit_input_'+id_n).value == '')) {
					result = false;
					break;
				}
			}
		}
	}
	if (!result) alert("Заполните, пожалуйста, все поля");
	if (!is_any && result) {result = false; alert("Выберите хотя бы один аналит");}
	return result;
}