window.onload = setBehaviours;

function goToStartPage() {
	window.location.href = sBaseHref;
}

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;
	var sLocation = '';
	s = s.replace(/^\s+/, '').replace(/\s+$/, '');
	
	if (s != '' && s != sSearchTxt) {
		s = encodeURIComponent(s).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');
		var iever = getInternetExplorerVersion();
		sLocation = ((iever > -1 && iever < 8.0)  ? sBaseHref : '') + oForm.action + 'q:' + s + ',qa:1';

		for (var i = 0; i < oForm.length; i++) {
			switch (oForm[i].type) {
				case 'checkbox':
					if (oForm[i].checked) {
						sLocation += ',' + oForm[i].name + ':' + oForm[i].value;
					}
				break;
				
				case 'select-one':
					if (oForm[i].options.selectedIndex > 0) {
						sLocation += ',' + oForm[i].name + ':' + oForm[i].options[oForm[i].options.selectedIndex].value;
					}
				break;
			}
		}
		window.location.href = sLocation + '.html';
	}
	return false;
}

function sendSearchForm(oForm) {
	var s = oForm.q.value;
	var sLocation = '';
	var oAction = null;
	s = s.replace(/^\s+/, '').replace(/\s+$/, '');

	if (s != '') {
		s = encodeURIComponent(s).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');
		var iever = getInternetExplorerVersion();
		sLocation = ((iever > -1 && iever < 8.0) ? sBaseHref : '') + oForm.action;
		if ((oAction = document.getElementById('act')) != undefined) {
			sLocation += oAction.value + ',';
		}
		sLocation += 'q:' + s;

		for (var i = 0; i < oForm.length; i++) {
			switch (oForm[i].type) {
				case 'checkbox':
					if (oForm[i].checked) {
						sLocation += ',' + oForm[i].name + ':' + oForm[i].value;
					}
				break;
				
				case 'select-one':
					if (oForm[i].options.selectedIndex > 0) {
						sLocation += ',' + oForm[i].name + ':' + oForm[i].options[oForm[i].options.selectedIndex].value;
					}
				break;
				
				case 'text':
					oForm[i].value = oForm[i].value.replace(/^\s+/, '').replace(/\s+$/, '');
					if (oForm[i].value != '') {
						if (oForm[i].name == 'po_price_from' || oForm[i].name == 'po_price_to') {
							oForm[i].value = oForm[i].value.replace(',', '.')
							sLocation += ',' + oForm[i].name + ':' + encodeURIComponent(oForm[i].value).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');
						}
						else {
							sLocation += ',' + oForm[i].name + ':' + encodeURIComponent(oForm[i].value).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');
						}
					}
				break;
			}
		}
		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 = oForm.f_price_from.value.replace(',', '.');
	var fPriceTo = oForm.f_price_to.value.replace(',', '.');
	var sLocation = '';
		
	for (var i = 0; i < oForm.length; i++) {
		if (oForm[i].type == 'checkbox' && oForm[i].checked) {
			sLocation += oForm[i].name + ':' + oForm[i].value + ',';
		}
	}
	sLocation += 'f_price_from:' + fPriceFrom + ',f_price_to:' + fPriceTo;
	
	var iever = getInternetExplorerVersion();
	
	sLocation = ((iever > -1 && iever < 8.0) ? sBaseHref : '') + oForm.action + sLocation;
	
	window.location.href = sLocation + '.html';
	return false;
}

function togglePaymentAvailability(oInp) {
	var aPaymentMethods = ['postal_fee', 'platnosci_pl'];
	
	for (var i = 0; i < aPaymentMethods.length; i++) {
		if (oMethodInp = document.getElementById('ptype_' + aPaymentMethods[i])) {
			oMethodInp.disabled = oInp.checked;
			if (oInp.checked && oMethodInp.checked) {
				oMethodInp.checked = false;
			}
		}
	}
}

function selectSelfCollection(oInp) {
	oInp.checked = true;
}

function setBehaviours() {
	if (document.getElementById) {
		// WYSZUKIWARKA
		// onfocus, onblur
		if (oSearchInp = document.getElementById('q')) {
			oSearchInp.value = sSearchTxt;
			oSearchInp.onfocus = function() {inputOnFocus(oSearchInp, sSearchTxt);};
			oSearchInp.onblur = function() {inputOnBlur(oSearchInp, sSearchTxt);};
		}
		// onsubmit
		if (bModRewrite && (oSearchBoxForm = document.getElementById('searchBoxForm'))) {
			oSearchBoxForm.onsubmit = function() {return sendSearchBoxForm(oSearchBoxForm);};
		}
		if (bModRewrite && (oSearchForm = document.getElementById('searchForm'))) {
			oSearchForm.onsubmit = function() {return sendSearchForm(oSearchForm);};
		}
		
		// NEWSLETTER
		// onfocus, onblur
		if (oNewsletterInp = document.getElementById('n_email')) {
			oNewsletterInp.value = sNewsletterTxt;
			oNewsletterInp.onfocus = function() {inputOnFocus(oNewsletterInp, sNewsletterTxt);};
			oNewsletterInp.onblur = function() {inputOnBlur(oNewsletterInp, sNewsletterTxt);};
		}
		// onsubmit
		if (oNewsletterBoxForm = document.getElementById('newsletterBoxForm')) {
			oNewsletterBoxForm.onsubmit = function() {return sendNewsletterBoxForm(oNewsletterBoxForm, sNewsletterTxt);};
		}
		
		// FORMULARZ LOGOWANIA
		if (oLoginForm = document.getElementById('loginForm')) {
			oLoginForm.login.focus();
		}
		
		// FILTRY LISTY OFERTY PRODUKTOWEJ
		// onsubmit
		if (bModRewrite && (oProductsFiltersForm = document.getElementById('productsFiltersForm'))) {
			oProductsFiltersForm.onsubmit = function() {return sendProductsFiltersForm(oProductsFiltersForm);};
		}
		
		// WYBOR METODY PLATNOSCI I DOSTAWY TOWARU
		if (oSelfCollectionInp = document.getElementById('self_collection')) {
			oSelfCollectionInp.onclick = function() {togglePaymentAvailability(oSelfCollectionInp);};
			togglePaymentAvailability(oSelfCollectionInp);
			// PLATNOSC GOTOWKA
			if (oSelfCollectionPaymentInp = document.getElementById('ptype_self_collection')) {
				oSelfCollectionPaymentInp.onclick = function() {selectSelfCollection(oSelfCollectionInp);};
			}
		}
	}
}
