function loadXMLString(txt) {
	try // Internet Explorer
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.loadXML(txt);
		return xmlDoc;
	} catch (e) {
		try // Firefox, Mozilla, Opera, etc.
		{
			parser = new DOMParser();
			xmlDoc = parser.parseFromString(txt, "text/xml");
			return xmlDoc;
		} catch (e) {
			alert(e.message);
		}
	}
	return null;
}

function getXmlHttpObject() {
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
		return null;
	}
}

function ajaxRequest(url) {
	var xmlHttp = getXmlHttpObject();
	if (xmlHttp != null) {
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					// alert('Answer1: ' + xmlHttp.responseText);
				}
			}
		}

		// alert('Asking: ' + url);
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}

function ajaxSelectRequest(url, selectObjectId) {
	var xmlHttp = getXmlHttpObject();
	if (xmlHttp != null) {
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				//			if (xmlHttp.status == 200) {

				responseXML = xmlHttp.responseText;
				// alert('Answer: ' + responseXML);

				var xmlDoc = loadXMLString(responseXML);

				option = xmlDoc.getElementsByTagName("option");
				var selectObject = document.getElementById(selectObjectId);

				removeOptions(selectObject);
				if (option.length > 0) {
					selectObject.disabled = null;
				} else {
					selectObject.disabled = "disabled";
				}

				for (i = 0; i < option.length; i++) {
					var optionObject = document.createElement('option');
					optionObject.text = option[i].childNodes[0].nodeValue;
					optionObject.value = option[i].getAttribute("value");

					try {
						selectObject.add(optionObject, null);
					} catch (ex) {
						selectObject.add(optionObject); // IE only
					}
				}
				//			}
			}
		}

		//	alert('Asking: ' + url);
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}

function removeOptions(selectObject) {
	for (i = selectObject.options.length - 1; i >= 0; i--) {
		selectObject.remove(i);
	}
}

function ajaxInnerHtmlRequest(url, htmlObjectId) {
	var xmlHttp = getXmlHttpObject();
	if (xmlHttp != null) {
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				//				alert('Answer2:' + xmlHttp.responseText);
				document.getElementById(htmlObjectId).innerHTML = xmlHttp.responseText;
			}
		}

		//	alert('Asking: ' + url);
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}

//product list
function getProductListAjaxParams(filter) {
	var linkParams = '';

	var jsListType = document.getElementById('listType');
	if (jsListType != null) {
		linkParams += '&listType=' + jsListType.value;
	}

	var jsListPageSize = document.getElementById('listPageSize');
	if (jsListPageSize != null) {
		linkParams += '&listPageSize=' + jsListPageSize.value;
	}

	var jsListSortBy = document.getElementById('listSortBy');
	if (jsListSortBy != null) {
		if (filter == 1) {
			linkParams += '&listSortBy=hit';
		} else {
			linkParams += '&listSortBy=' + jsListSortBy.value;
		}
	}

	var jsListSortDir = document.getElementById('listSortDir');
	if (jsListSortDir != null) {
		if (filter == 1) {
			linkParams += '&listSortDir=DESC';
		} else {
			linkParams += '&listSortDir=' + jsListSortDir.value;
		}
	}

	var jsListPageNo = document.getElementById('listPageNo');
	if (jsListPageNo != null) {
		if (filter == 1) {
			linkParams += '&listPageNo=0';
		} else {
			linkParams += '&listPageNo=' + jsListPageNo.value;
		}
	}

	var jsListLeftMenuId = document.getElementById('listLeftMenuId');
	if (jsListLeftMenuId != null) {
		linkParams += '&leftMenuId=' + jsListLeftMenuId.value;
	}

	var jsListSearchText = document.getElementById('listSearchText');
	if (jsListSearchText != null && jsListSearchText.value != '') {
		linkParams += '&searchText=' + jsListSearchText.value;
	}

	var jsListPriceFrom = document.getElementById('listPriceFrom');
	if (jsListPriceFrom != null && jsListPriceFrom.value > 0) {
		linkParams += '&priceFrom=' + jsListPriceFrom.value;
	}

	var jsListPriceTo = document.getElementById('listPriceTo');
	if (jsListPriceTo != null && jsListPriceTo.value > 0) {
		linkParams += '&priceTo=' + jsListPriceTo.value;
	}

	var jsListProducer = document.getElementById('listProducer');
	if (jsListProducer != null && jsListProducer.value > 0) {
		linkParams += '&producer=' + jsListProducer.value;
	}

	var jsListCategory = document.getElementById('listCategory');
	if (jsListCategory != null && jsListCategory.value > 0) {
		linkParams += '&category=' + jsListCategory.value;
	}

	return linkParams;
}

function getProductList(mainModule) {
	var link = 'ajax.php?module=productList&mainModule=' + mainModule
			+ '&moduleListType='
			+ document.getElementById('moduleListType').value
			+ getProductListAjaxParams();
	ajaxInnerHtmlRequest(link, 'productList');
}

function refreshProductList() {
	ajaxInnerHtmlRequest('ajax.php?module=productList&moduleListType='
			+ document.getElementById('moduleListType').value
			+ getProductListAjaxParams(), 'productList');
}

function refreshProductListFilter() {
	ajaxInnerHtmlRequest('ajax.php?module=productList&moduleListType='
			+ document.getElementById('moduleListType').value
			+ getProductListAjaxParams(1), 'productList');
}

function changleListType(listType) {
	document.getElementById('listType').value = listType;
	refreshProductList();
}

function changeListSortBy(sortBy, sortDir) {
	document.getElementById('listSortBy').value = sortBy;
	document.getElementById('listSortDir').value = sortDir;
	refreshProductList();
}

function changeListPageSize(pageSize) {
	document.getElementById('listPageSize').value = pageSize;
	refreshProductList();
}

function changeListPageNo(pageNo) {
	document.getElementById('listPageNo').value = pageNo;
	refreshProductList();
}

// basket
function addToBasket(productId) {
	var colorSelect = document.getElementById('colorSelect');
	if(colorSelect != null && colorSelect.value > 0){
		productId = productId + '&color=' + colorSelect.value;
	}
	var gratisSelect = document.getElementById('gratisSelect');
	if(gratisSelect != null && gratisSelect.value > 0){
		productId = productId + '&gratis=' + gratisSelect.value;
	}
	ajaxInnerHtmlRequest(
			'ajax.php?module=basket&action=add&productId=' + productId,
			'basketTop');
	addToBasketMsg();
}
function addSetToBasket(productId, setId) {
	var colorSelect = document.getElementById('colorSelect');
	if(colorSelect != null && colorSelect.value > 0){
		productId = productId + '&color=' + colorSelect.value;
	}
	ajaxInnerHtmlRequest('ajax.php?module=basket&action=add&productId='
			+ productId + '&setId=' + setId, 'basketTop');
	addToBasketMsg();
}
function addToBasketMsg(){
	$.blockUI( {
		message : $('#basketAddedImg'),
		css : {
		border : 'none',
		backgroundColor : '#fff',
		width : '0',
		height : '0'
	},
	overlayCSS : {
		backgroundColor : '#fff',
		opacity : 0.6
	}
	});
	$('#basketAddedImg').click($.unblockUI);
	setTimeout($.unblockUI, 2000);
}

// notepad
function addToNotepad(productId) {
	ajaxRequest('ajax.php?module=shopUser&page=notepad&action=add&productId=' + productId);
	$.blockUI( {
		message : $('#notepadAddedImg'),
		css : {
			border : 'none',
			backgroundColor : '#fff',
			width : '0',
			height : '0'
		},
		overlayCSS : {
			backgroundColor : '#fff',
			opacity : 0.6
		}
	});
	$('#notepadAddedImg').click($.unblockUI);
	setTimeout($.unblockUI, 2000);
}
function removeFromNotepad(productId) {
	ajaxRequest('ajax.php?module=notepad&action=remove&productId=' + productId);
}

//product
function sendTellFriendForm() {
	var link = 'ajax.php?module=product&save=1&page=tellFriend&pseudonim='
			+ document.getElementById('pseudonim').value + '&idProduktu='
			+ document.getElementById('idProduktu').value + '&email='
			+ document.getElementById('email').value + '&tekst='
			+ document.getElementById('tekst').value;
	ajaxInnerHtmlRequest(link, 'powiadom_znajomego');
}

//main
function sendAskForm() {
	var link = 'ajax.php?module=main&save=1&page=ask&pseudonim='
			+ document.getElementById('pseudonim').value + '&phone='
			+ document.getElementById('phone').value + '&email='
			+ document.getElementById('email').value + '&question='
			+ document.getElementById('question').value;
	ajaxInnerHtmlRequest(link, 'zadaj_pytanie');
}

// other
function hideElement(pElementId) {
	document.getElementById(pElementId).style.display = 'none';
	document.getElementById('addConfirmBg').style.display = 'none';
}
function showElement(pElementId) {
	document.getElementById('addConfirmBg').style.display = 'block';
	document.getElementById(pElementId).style.display = 'block';
}

