window.onload = setBehaviours;

function inputOnFocus (oObj, sTxt) {
	if (oObj.value == sTxt) oObj.value = '';
}
function inputOnBlur (oObj, sTxt) {
	if (oObj.value == '') oObj.value = sTxt;
}

function sendSearchBoxForm(oForm) {
	var s = oForm.q.value.replace(/^\s+/, '').replace(/\s+$/, '');

	if (s != '') {
		s = encodeURIComponent(s).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');
		// kategoria
		if(document.getElementById("search_box_category") && document.getElementById("search_box_category").value != "") {
			s += ',category:' + document.getElementById("search_box_category").value;
		}
		window.location.href = oForm.action + '/q:' + s + '.html';
	}
	return false;
}

function sendSearchForm(oForm) {
	var s = oForm.q.value;	
	var sLocation = '';

	sLocation = oForm.action + '/';
	// wyszukiwana fraza
	if (s != '') sLocation = sLocation + 'q:' + s + ',';
	// producent
	if(document.getElementById("producer") && document.getElementById("producer").value != "") {
		sLocation += 'producer:' + document.getElementById("producer").value + ',';
	}
	// kategoria
	if(document.getElementById("category") && document.getElementById("category").value != "") {
		sLocation += 'category:' + document.getElementById("category").value + ',';
	}
	// sorter
	if(document.getElementById("sorter")) {
		sLocation += 'sort:' + document.getElementById("sorter").value + ',';
	}
	if (sLocation.charAt(sLocation.length - 1) == ',') {
		// obciecie przecinka
		sLocation = sLocation.slice(0,-1);
	}
	// przekierowanie przegladarki
	window.location.href = sLocation + '.html';
	return false;
}

function sendNewsletterBoxForm(oForm, sTxt) {
	var sEmail = oForm.n_email.value;
	sEmail = sEmail.replace(/^\s+/, '').replace(/\s+$/, '');
	
	if (sEmail == '' || sEmail == sTxt) {
		return false;
	}
}

function sendProductsFiltersForm(oForm) {
	var fPriceFrom = parseFloat(oForm.price_from.value.replace(',', '.'));
	var fPriceTo = parseFloat(oForm.price_to.value.replace(',', '.'));
	var sLocation = '';
	var sProducers = '';
		
	for (var i = 0; i < oForm.length; i++) {
		// producenci
		if (oForm[i].type == 'checkbox' && oForm[i].name.match(/^producer_\d+$/) && oForm[i].checked) {
			if (sProducers == '') {
				sProducers = 'producer:' + oForm[i].value;
			} else {
				sProducers += '_' + oForm[i].value;
			}
		}
	}
	sLocation += sProducers;
	// zakres cenowy
	if (!isNaN(fPriceFrom)) {
		sLocation += (sLocation != '' ? ',' : '') + 'price_from:' + fPriceFrom;
	}
	if (!isNaN(fPriceTo)) {
		sLocation += (sLocation != '' ? ',' : '') + 'price_to:' + fPriceTo;
	}
	// sorter
	if (document.getElementById("sorter")) {
		sLocation += (sLocation != '' ? ',' : '') + 'sort:' + document.getElementById("sorter").value;
	}
	sLocation = oForm.action + sLocation;
	
	window.location.href = sLocation + '.html';
	return false;
}

function setBehaviours() {
	if (document.getElementById) {
		// WYSZUKIWARKA
		// onsubmit
		if (oSearchBoxForm = document.getElementById('searchBoxForm')) {
			oSearchBoxForm.onsubmit = function() {return sendSearchBoxForm(oSearchBoxForm);};
		}
		if (oSearchForm = document.getElementById('searchForm')) {
			oSearchForm.onsubmit = function() {return sendSearchForm(oSearchForm);};
		}
		if (oProductsFiltersForm = document.getElementById('productsFiltersForm')) {
			oProductsFiltersForm.onsubmit = function() {return sendProductsFiltersForm(oProductsFiltersForm);};
		}
	}
}

