jQuery(document).ready(function() {
	String.prototype.forSearch = function() {
		var text = this;
	    var trans = {
	    	"á" : "a", "ä" : "a", "à" : "a", "â" : "a", "č" : "c", "ď" : "d",
	    	"é" : "e", "ě" : "e", "ë" : "e", "í" : "i", "ň" : "n", "ó" : "o",
	    	"ö" : "o", "ř" : "r", "š" : "s", "ť" : "t", "ú" : "u", "ů" : "u",
	    	"ü" : "u", "ý" : "y", "ž" : "z", "Á" : "A", "Ä" : "A", "Č" : "C",
	    	"Ď" : "D", "É" : "E", "Ě" : "E", "Ë" : "E", "Í" : "I", "Ň" : "N",
	    	"Ó" : "O", "Ö" : "O", "Ř" : "R", "Š" : "S", "Ť" : "T", "Ú" : "U",
	    	"Ů" : "U", "Ü" : "U", "Ý" : "Y", "Ž" : "Z", "ľ" : "l"
	    };
	    for (var k in trans) {
	    	var old_text = '';
	    	while (old_text != text) {
	    		old_text = text;
	    		text = text.replace(k, trans[k]);
	    	}
	    }
		return text.toLowerCase();
	};
	jQuery('div.search input.box').keyup(function() {
		var search = jQuery(this).val().forSearch().split(' ');
		jQuery('table.moduleList tr').each(function() {
			if (jQuery(this).hasClass('header')) {
				return;
			}
			var name = jQuery('td.detail span.name', this).text().forSearch();
			var number = jQuery('td.detail span.id', this).text().forSearch();
			var show = true;
			for (var i = 0; search[i]; i++) {
				if (name.indexOf(search[i]) == -1 && number.indexOf(search[i]) == -1) {
					show = false;
					break;
				}
			}
			if (show) {
				jQuery(this).show();
			} else {
				jQuery(this).hide();
			}
		});
		return true;
	});
	jQuery('div.search input.button').click(function() {
		jQuery('div.search input.box').val('').keyup();
	});
});


var somethingHasChanged = false;

function getFreeSlots() {
	var schema = document.getElementById('specSlotUsage');
	return schema.getElementsByClassName('empty').length;
}

function getProductSlots(id) {
	var input = document.getElementById('product_slots_'+id);
	if (input) {
		return parseInt(input.value);
	} else {
		return 0;
	}
}

function getProductCount(id) {
	var p = document.getElementById('product_count_'+id);
	var checkbox = document.getElementById('product_checkbox_'+id);
	var checked = checkbox ? checkbox.checked : false;
	if (p && checked) {
		var count = parseInt(p.value);
		if (parseInt(count) != count) {
			count = 0;
		}
		return count;
	}
	else return 0;
}

function setProductCount(id, count) {
	var p = document.getElementById('product_count_'+id);
	if (p) {
		p.value = count;
	}
}

function updateSchema() {
	somethingHasChanged = true;
	var schema = document.getElementById('slotUsage');
	if (schema) {
		schema.innerHTML = AX_getRequest('getSlotSchema');
		
		// aktualizovat cenu
		var  price = document.getElementById('total_price');
		if (price) {
			var response = AX_getRequest('getPrice');
			var patt = new RegExp(/^OK /);
			if (patt.test(response)) {
				price.innerHTML = response.substr(3);
			} else {
				alert('ERROR: Cannot retrieve correct price response');
			}
		}
		
		// aktualizivat cenu bez slev
		var price = document.getElementById('total_no_discount');
		if (price) {
			var response = AX_getRequest('getPriceNoDiscount');
			var patt = new RegExp(/^OK /);
			if (patt.test(response)) {
				price.innerHTML = response.substr(3);
			} else {
				alert('ERROR: Cannot retrieve correct price response');
			}
		}
	}
}

function insertProduct(id, count) {
	var request = 'insertModule/'+id+'/'+getCategory(id)+'/'+count;
	var response = AX_getRequest(request);
	var count = AX_getRequest('getModuleCount/'+id);
	var patt = new RegExp(/^OK /);
	if (patt.test(count)) {
		setProductCount(id, count.substr(3));
		var checkbox = document.getElementById('product_checkbox_'+id);
		checkbox.checked = count.substr(3) > 0 ? true : false;
	} else {
		alert('ERROR: Cannot retrieve correct count response');
	}
	if (response == 'OK') {

	} else {
		var patt = new RegExp(/^DEPEND /);
		if (patt.test(response)) {
			if (confirm(AX_postRequest('translate', 'word=RESOLVE_DEPENCIES_QUESTION')+"\n"+response.substr(7))) {
				AX_getRequest('addRequiredModules');
			} else {
				AX_getRequest('deleteRequiredModules');
			}
		} else {
			alert(response);
		}
		window.location.reload();
	}
}

function deleteProduct(id) {
	insertProduct(id, 0);
}

function specSwitchTab(newCategoryId) {
	var specForm = document.getElementById('specForm');
	if (specForm) {
		var loc = location.href.replace(/[0-9]+\/?$/, '');
		var part = loc.split('/');
		if (newCategoryId == 'finish') {
			specForm.action = part[0]+'//'+part[2]+'/'+part[3]+'/summary/';
		} else {
			specForm.action = part[0]+'//'+part[2]+'/'+part[3]+'/specification/'+newCategoryId;
		}
		document.location.href = specForm.action;
	}
}

function getCategory(id) {
	var e = document.getElementById('product_category_'+id);
	if (e) {
		return e.value;
	} else {
		alert('ERROR: Category not found for ID '+id);
	}
}

function setChangeGuard() {
	changeGuard();
}

function changeGuard() {
	if (somethingHasChanged) {
		window.location.reload();
	} else {
		setTimeout('changeGuard()', 500);
	}
}





function onCountFocus(input, id) {
	var e = document.getElementById('product_tick_'+id);
	if (e) {
		e.style.visibility = 'visible';
	}
	if (input.value < 1) {
		input.value = '';
	}
}

function onCountBlur(input, id) {
	var e = document.getElementById('product_tick_'+id);
	if (e) {
		e.style.visibility = 'hidden';
	}
	var count = input.value = parseInt(input.value);
	var checkbox = document.getElementById('product_checkbox_'+id);
	checkbox.checked = count > 0 ? true : false;
	checkboxClick(id);
}

function checkboxClick(id) {
	startLoader();
	var checkbox = document.getElementById('product_checkbox_'+id);
	checkbox.parentNode.parentNode.className = checkbox.checked ? 'selected' : 'normal';
	onCheckboxClick(checkbox, id);
	endLoader();
}

function onCheckboxClick(checkbox, id) {
	if (checkbox.checked) {
		if (getProductCount(id) < 1) {
			setProductCount(id, 1);
		}
		insertProduct(id, getProductCount(id));
	}
	else {
		setProductCount(id, 0);
		deleteProduct(id);
	}
	updateSchema();
}

function startLoader() {
	var loader = document.getElementById('specLoader');
	if (loader) {
		loader.style.visibility = 'visible';
	}
}

function endLoader() {
	var loader = document.getElementById('specLoader');
	if (loader) {
		loader.style.visibility = 'hidden';
	}
}