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, '/');

		window.location.href = oForm.action + '/q:' + s + ',s:form.html';
	}
	return false;
}

function sendSearchForm(oForm) {
	var s = oForm.q.value.replace(/^\s+/, '').replace(/\s+$/, '');
	if(document.getElementById("adv")) {
		var sIndex = oForm.index.value;
		var sSeria = oForm.series.value;
		var sIsbn = oForm.isbn.value.replace(/^\s+/, '').replace(/\s+$/, '');
		var sAuthor = oForm.author.value.replace(/^\s+/, '').replace(/\s+$/, '');
		var sYear = oForm.year.value.replace(/^\s+/, '').replace(/\s+$/, '');
		var sPriceFrom = oForm.price_from.value.replace(/^\s+/, '').replace(/\s+$/, '');
		var sPriceTo = oForm.price_to.value.replace(/^\s+/, '').replace(/\s+$/, '');
		var bAval = oForm.aval.checked == true ? 1 : 0;
	}
	var sLocation = '';
	
	sLocation = oForm.action + '/';
	if(window.location.href.indexOf("zaawansowane") != -1) sLocation = sLocation + 'zaawansowane,';

	s = encodeURIComponent(s).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');
	sLocation = sLocation + 'q:' + s + ',';
	
	if(document.getElementById("adv")) {
		if (sIndex != '-- wszystkie --') {
			sLocation = sLocation + 'index:' + sIndex + ',';
			bIs = true;
		}
		if (sSeria != '-- wszystkie --') {
			sLocation = sLocation + 'seria:' + sSeria + ',';
			bIs = true;
		}
		if (sIsbn != '') {
			sIsbn = encodeURIComponent(sIsbn).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');	
			sLocation = sLocation + 'isbn:' + sIsbn + ',';
			bIs = true;
		}
		if (sAuthor != '') {
			sAuthor= encodeURIComponent(sAuthor).replace(/%20/g, "+").replace(/%2C/g, ' ').replace(/%2F/g, '/');	
			sLocation = sLocation + 'autor:' + sAuthor + ',';
			bIs = true;
		}
		if (sYear != '') {
			sLocation = sLocation + 'year:' + sYear + ',';
			bIs = true;
		}
		if (sPriceFrom != '') {
			var sPriceFrom = sPriceFrom.replace(",",".");
			sLocation = sLocation + 'price_from:' + sPriceFrom + ',';
			bIs = true;
		}
		if (sPriceTo != '') {
			var sPriceTo = sPriceTo.replace(",",".");
			sLocation = sLocation + 'price_to:' + sPriceTo + ',';
			bIs = true;
		}
		if (bAval == 1){
			sLocation = sLocation + 'aval:' + bAval + ',';
		}
	}

	if (sLocation.charAt(sLocation.length - 1) == ','){
		sLocation = sLocation.slice(0,-1);
	}
	window.location.href = sLocation + ',s:form.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);};
		}
		
		// DOMYSLNE USTAWIENIE KURSORA W POLU LOGIN
		if (oLoginForm = document.getElementById('loginForm') || document.getElementById('userLogin')) {
			document.getElementById('login').focus();	
		}
		
		// 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)};
		}
		
		// adobe digital editions
		if (oADE = document.getElementById('ADE_ShowContentDiv')) {
			 ADE_Detect_doOnLoad('', false, false);
		}
		
		// GORNE BOKSY - TOP15, Indeks rzeczowy, Serie, Autorzy
		// zaznaczona zakladka
		var name = "beck_actTab";

		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) {
				if ((oTopBox = document.getElementById("boxTop_"+c.substring(nameEQ.length,c.length))) && (oTopBoxS = document.getElementById("l_" + c.substring(nameEQ.length,c.length)))) {
					oTopBox.style.display = "block";
					oTopBoxS.className = "selected";
					var exist = 1;
				}
			}
		}
		if(exist != 1 && (oTopBox = document.getElementById("boxTop_1")) && (oTopBoxS = document.getElementById("l_1"))) {
			oTopBox.style.display = "block";
			oTopBoxS.className = "selected";
		}
	}
}

