window.alertException = true;
timeout = 10 * 1000;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function enable(id){
	try{
		var btn = document.getElementById(id);
		btn.disabled = false;
		if(btn.getAttribute('bakValue')) btn.value = btn.getAttribute('bakValue');
		if(btn.getAttribute('disableTimer')) window.clearTimeout(btn.getAttribute('disableTimer'));
	}catch(e){
		if(window.alertException) alert('Function enable(id): ' + e.message);
	}
}

function disable(id, withTimeout, waitMessage){
	try{
		var btn = document.getElementById(id);
		btn.disabled = true;
		if(typeof(waitMessage) != 'undefined'){
				btn.setAttribute('bakValue', btn.value);
				btn.value = waitMessage;
		}
		if(withTimeout == true) {
			var timeoutId = window.setTimeout("enable('" + id + "')", timeout);
			btn.setAttribute('disableTimer', timeoutId);
		}
	}catch(e){
		if(window.alertException) alert('Function disable(id): ' + e.message);
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function disableWithCounter(id, timeout){
	try{
		var btn = document.getElementById(id);
		if(timeout > 0){
			if(btn.disabled == false) btn.setAttribute('bakValue', btn.value);
			btn.disabled = true;
			btn.value = btn.getAttribute('bakValue') + ' (' + timeout + ')';
			timeout--;
			var timer = window.setTimeout('disableWithCounter("' + id + '", ' + timeout + ')', 1000);
			btn.setAttribute('disableWithCounterTimer', timer);
		}else{
			btn.disabled = false;
			btn.value = btn.getAttribute('bakValue');
		}
	}catch(e){
		if(window.alertException) alert('Function disableWithCounter(id, timeout): ' + e.message);
	}
}

function unlockWithTimer(id){
	try{
		var btn = document.getElementById(id);
		timer = btn.getAttribute('disableWithCounterTimer');
		if(timer) window.clearTimeout(timer);
		btn.disabled = false;
		if(btn.getAttribute('bakValue')) btn.value = btn.getAttribute('bakValue');
	}catch(e){
		if(window.alertException) alert('Function unlockWithTimer(id): ' + e.message);
	}
}

// - - - service - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

function updateTableStyle(){
	try{
		var allTables = document.getElementsByTagName('TABLE');
		var tblRows = null;
		var firstCell = null;
		for(var i=0; i<allTables.length; i++) {
			if(allTables[i].getAttribute('name') != 'catalogueTable') continue;
			tblRows = allTables[i].rows;
			for(var j=0; j<tblRows.length; j++) {
				firstCell = tblRows[j].cells[0];
				if(firstCell.tagName == 'TH') firstCell.innerHTML = '<span>' + firstCell.innerHTML + '</span>';
				firstCell.className = "leftAlign";
			}
		}
	}catch(e){
		if(window.alertException) alert('Function updateTableStyle(): ' + e.message);
	}
}

// - - - menu - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

function showID(id, isCurrent) {
	try{
		if(isCurrent) return;
		var doc=document.getElementById(id);
		doc.style.display='block';
	}catch(e){
		if(window.alertException) alert('Function showID(id, isCurrent): ' + e.message);
	}
}
		
function hideID(id, isCurrent) {
	try{
		if(isCurrent) return;
		var doc=document.getElementById(id);
		doc.style.display='none';
	}catch(e){
		if(window.alertException) alert('Function hideID(id, isCurrent): ' + e.message);
	}
}

// - - - далее всёs устарело - - -
function showSubmenu(main, id){
	try{
		var div = document.getElementById(id);
		div.style.left = 210;
		div.style.top = main.style.top;
		div.style.display = 'block';
		main.style.backgroundColor = 'rgb(120, 122, 135)';
	}catch(e){
		if(window.alertException) alert('Function showSubmenu(main, id): ' + e.message);
	}
}

function hideSubmenu(main, id){
	try{
		var div = document.getElementById(id);
		div.style.display = 'none';
		main.style.backgroundColor = 'rgb(255, 255, 255)';
	}catch(e){
		if(window.alertException) alert('Function hideSubmenu(main, id): ' + e.message);
	}
}

function hightlight(td){
	try{
		td.style.backgroundColor = 'rgb(120, 122, 135)';
	}catch(e){
		if(window.alertException) alert('Function hightlight(td): ' + e.message);
	}
}

function unhightlight(td){
	try{
		td.style.backgroundColor = 'rgb(242, 242, 242)';
	}catch(e){
		if(window.alertException) alert('Function unhightlight(td): ' + e.message);
	}
}

// - - - search - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

function updateSearchForm(type){
	try{
		switch(type){
			case 'context':
					document.getElementById('datesRow').style.display = 'none';
					// скрытие полей для каталога
					break;
			case 'news':
					document.getElementById('datesRow').style.display = 'block';
					// скрытие полей для каталога
					break;
			case 'catalogue':
					document.getElementById('datesRow').style.display = 'none';
					// отображение полей для каталога
					break;
		}
	}catch(e){
		if(window.alertException) alert('Function updateSearchForm(type): ' + e.message);
	}
}

function calcOffer(value){
	try{
		var price = document.getElementById('calc_price');
		var total = document.getElementById('calc_total');
		var totalVal = parseFloat(value) * parseFloat(price.innerHTML);
		if(isNaN(totalVal)) totalVal = 0;
		totalVal = Math.round(totalVal * 100) / 100;
		total.innerHTML = totalVal;
	}catch(e){
		if(window.alertException) alert('Function calcOffer(value): ' + e.message);
	}
}

function calcRequest(){
	try{
		var price = document.getElementById('price');
		var qnt = document.getElementById('qnt');
		var total = document.getElementById('total');
		var totalVal = parseFloat(qnt.value) * parseFloat(price.value);
		if(isNaN(totalVal)) totalVal = 0;
		totalVal = Math.round(totalVal * 100) / 100;
		total.innerHTML = totalVal;
	}catch(e){
		if(window.alertException) alert('Function calcRequest(): ' + e.message);
	}
}

window.onload = updateTableStyle;