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;
	s = s.replace(/^\s+/, '').replace(/\s+$/, '');
	
	if (s != '' && s != sSearchTxt) {
		s = encodeURIComponent(s).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');
		window.location.href = oForm.action + '/q:' + s + '.html';
	}
	return false;
}

function sendSearchForm(oForm) {
	var s = oForm.q.value;
	var sLocation = '';
	s = s.replace(/^\s+/, '').replace(/\s+$/, '');
	if (s != '') {
		s = encodeURIComponent(s).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');
		sLocation = oForm.action + '/q:' + s;

		for (var i = 0; i < oForm.length; i++) {
			if (oForm[i].type == 'checkbox' && oForm[i].checked) {
				sLocation += ',' + oForm[i].name + ':' + oForm[i].value;
			}
		}
		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 sendSalesDepsForm(oForm, oSelect) {
	var s = oSelect[oSelect.selectedIndex].value;
	window.location.href = oForm.action + '/' + s + '.html';
}

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 Z WOJEWODZTWAMI
		// onsubmit
		if (oSalesDepsBoxForm = document.getElementById('salesDepartmentsBoxForm')) {
			oSalesDepsBoxForm.elements['province'].onchange = function() {sendSalesDepsForm(oSalesDepsBoxForm, oSalesDepsBoxForm.elements['province']);};
		}
		if (oSalesDepsForm = document.getElementById('salesDepartmentsForm')) {
			oSalesDepsForm.elements['province'].onchange = function() {return sendSalesDepsForm(oSalesDepsForm, oSalesDepsForm.elements['province']);};
		}
	}
}