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;
	s = s.replace(/^\s+/, '').replace(/\s+$/, '');

	if (s != '') {
		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 = '';

	sLocation = oForm.action + '/';
	if(window.location.href.indexOf("zaawansowane") != -1) sLocation = sLocation + 'zaawansowane,';
	
	var sAdv = document.getElementById("adv").value != '' ? document.getElementById("adv").value : "none";

	if (s != ''){
		sLocation = sLocation + 'q:' + s + ',';
	}
	for( i = 0; i < oForm.dest.length; i++ ) {
		if( oForm.dest[i].checked == true )
			dest = oForm.dest[i].value;
	}

	sLocation += 'dest:' + dest + ',';
	

	sLocation += oForm.adv.value == "0" ? 'adv:0,' : 'adv:1,';

	
	if (sLocation.charAt(sLocation.length - 1) == ','){
		sLocation = sLocation.slice(0,-1);
	}
	if(document.getElementById("adv").value == '1'){
		for (var i = 0; i < oForm.length; i++) {

			if (oForm[i].type == 'checkbox' && oForm[i].checked) {

				//oForm[i].name = oForm[i].name.replace(/\[[0-9]\]/, "");
				sLocation += ',' + oForm[i].name + ':' + oForm[i].value;
			}
			if (oForm[i].type == 'select-one' && oForm[i].value != '') {
				sLocation += ',' + oForm[i].name + ':' + oForm[i].value;
			}
			if (oForm[i].type == 'radio' && oForm[i].checked && oForm[i] != '') {
				sLocation += ',' + oForm[i].name + ':' + oForm[i].value;
			}
			if (oForm[i].type == 'text' && oForm[i].value != '' && oForm.name != 'q') {
				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 sendSorterForm(oForm) {
	/*var sLocation = (navigator.userAgent.indexOf('MSIE') != -1 ? ''  : '') + oForm.action;*/

	var sLocation = oForm.action;
	sLocation += '/sort:' + oForm.sort.options[oForm.sort.options.selectedIndex].value;
	window.location.href = sLocation + '.html';

}

function sendSearchSorterForm(oForm) {
	var sLocation = window.location.href;
	if(sLocation.indexOf("sort") != -1){
		sLocation = sLocation.split(",sort");
	} else {
		sLocation = sLocation.split(".html");
	}
	sLocation = sLocation[0] + ',sort:' + oForm.sort.options[oForm.sort.options.selectedIndex].value;
	window.location.href = sLocation + '.html';
}

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);};
	//	}
		
		// 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 SORTOWANIA
		if (oSorterForm = document.getElementById('sorterForm')) {
			oSorterForm.sort.onchange = function() {sendSorterForm(oSorterForm)};
		}
		
		// FORMULARZ SORTOWANIA W WYSZUKIWARCE
		if (oSorterSearcherForm = document.getElementById('sorterSearchForm')) {
			oSorterSearcherForm.sort.onchange = function() {sendSearchSorterForm(oSorterSearcherForm)};
		}


	}
}

