
var obj = null;
if (window.XMLHttpRequest) {
	obj = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	obj = new ActiveXObject("Microsoft.XMLHTTP");
}

function createAjaxObj(){
	var aobj = null;
	if (window.XMLHttpRequest) {
		aobj = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		aobj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return aobj;
}

function productRate(productId, rate){
	var target = document.getElementById("rateDiv");
	target.innerHTML = "<p>Proszę czekać...</p>";
	Show('loading2');
	if (obj) {
		obj.open("GET", "/actions/productRate.php?productId=" + productId + "&rate=" + rate);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				Hide('loading2');
				setTimeout("alert('Dziękujemy, Twoja ocena została dodana.')",500);
			}
		}
		obj.send(null);
	}
}

function sendOpinion(productId){
	var target = document.getElementById("rateDiv");
	var opinion = document.getElementById("productOpinion");
	target.innerHTML = "<p>Proszę czekać...</p>";
	Show('loading2');
	if (obj) {
		obj.open("GET", "/actions/productRate.php?productId=" + productId + "&opinion=" + opinion.value);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				Hide('loading2');
				setTimeout("alert('Dziękujemy, Twoja opinia została dodana.')",500);
			}
		}
		obj.send(null);
	}
}

/**
 *
 * @access public
 * @return void
 **/
function cpuSearch(){
	obj.abort();
	var target = document.getElementById("searchDiv");
	var input = document.getElementById("cpuSearch");
	target.innerHTML = "<p>Proszę czekać...</p>";
	//Show('loading2');
	if (obj) {
		obj.open("GET", "/actions/cpuSearch.php?query=" + escape(input.value));
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				//Hide('loading2');
			}
		}
		obj.send(null);
	}
}

function gpuSearch(){
	obj.abort();
	var target = document.getElementById("searchDiv");
	var input = document.getElementById("gpuSearch");
	target.innerHTML = "<p>Proszę czekać...</p>";
	Show('loading2');
	if (obj) {
		obj.open("GET", "/actions/gpuSearch.php?query=" + escape(input.value));
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				Hide('loading2');
			}
		}
		obj.send(null);
	}
}

function quickCeneoSearch(){

	if(window.event.keyCode == 13) {
		firstSearch = false;
		var str = document.getElementById("productSearchInput").value;
		var target = document.getElementById("productSearchDiv");
		Show('loading2');
		if (obj) {
			obj.open("GET", "/actions/ceneoSearch.php?query=" + escape(str));
			obj.onreadystatechange = function() {
				if (obj.readyState == 4 && obj.status == 200) {
					target.innerHTML = obj.responseText;
					Hide('loading2');
					Show('productSearchDiv');
				}
			}
			obj.send(null);
		}
	}
}

function quickProductHint(cat){

		var str = document.getElementById("comp"+cat).value;
		var target = document.getElementById("productHintDiv");
		if (obj) {
			obj.abort();
			obj.open("GET", "/actions/ceneoHint.php?query=" + escape(str) + "&cat=" + cat);
			obj.onreadystatechange = function() {
				if (obj.readyState == 4 && obj.status == 200) {
					target.innerHTML = obj.responseText;
					Show('productHintDiv');
				}
			}
			obj.send(null);
		}
}

function productSearch(compId, catId, automatic){

	var target = document.getElementById("productSearchDiv");
	var input = document.getElementById("productSearchInput");
	var lprice = document.getElementById("productLPrice");
	var hprice = document.getElementById("productHPrice");
	var automatic = document.getElementById("productAutomatic");
	//target.innerHTML = "<p>Trwa wyszukiwanie produktu...</p>" + target.innerHTML;
	Show('loading');
	if (obj) {
		obj.open("GET", "/actions/ceneoSearch.php?compId=" + compId + "&pCategory=" + catId + "&query=" + escape(input.value) + "&lprice=" + lprice.value + "&hprice=" + hprice.value + "&automatic=" + automatic.checked);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				//target.style.height = '0px';
				//setSlideDown('slideItUp2');
				//slide.start();
				Hide('loading');
				Show('productSearchDiv');
			}
		}
		obj.send(null);
	}
}

function showSearchForm(compId){
	var target = document.getElementById("productSearchForm");
	//target.innerHTML += "<p>Proszę czekać...</p>";
	Show('loading2');
	Hide('productSearchDiv');
	if (obj) {
		obj.open("GET", "/conf_search.php?componentId=" + compId);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				Hide('loading2');
			}
		}
		obj.send(null);
	}
}


function addSearchCondition(cond){
	var target = document.getElementById("productSearchInput");
	target.value += cond + " ";
}


function changeLoadingInner(txt){
	var loading = document.getElementById("inner");
	loading.innerHTML = txt;
}

function updateComponent(id, ceneoId){
	var target = document.getElementById("confComponent"+id);
	target.innerHTML += "<p>Proszę czekać...</p>";
	//Show('loading2');
	//obj = createAjaxObj();
	if (obj) {
		obj.open("GET", "/actions/confDrawComponent.php?compId=" + id + "&ceneoId=" + ceneoId);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				Hide('loading2');
				getConfPrice();
			}
		}
		obj.send(null);
	}
}

function getConfPrice(){
	var target = document.getElementById("confPrice");
	//Show('loading2');
	if (obj) {
		obj.open("GET", "/actions/getConfPrice.php");
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				Hide('loading2');
			}
		}
		obj.send(null);
	}
}

function checkCPUGPURelation(){
	var target = document.getElementById("compInfo2");
	target.innerHTML = "Trwa sprawdzanie relacji CPU - GPU...";
	//Show('loading2');
	if (obj) {
		obj.open("GET", "/actions/checkCPUGPURelation.php");
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				Hide('loading2');
			}
		}
		obj.send(null);
	}
}


function checkCompatibility(compId){
	var target = document.getElementById("compInfo"+compId);
	target.innerHTML = "Proszę czekać...";
	Show('loading2');
	if (obj) {
		obj.open("GET", "/actions/checkCompatibility.php?compId="+compId);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				Hide('loading2');
			}
		}
		obj.send(null);
	}
}

function saveSet(){

	if (obj) {
		obj.open("GET", "/actions/saveSet.php");
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				alert(obj.responseText+"\n");
			}
		}
		obj.send(null);
	}
}

function deleteSet(id){

	var ans = confirm("Czy napewno chcesz usunąć ten zestaw?");
	if (obj && ans) {
		obj.open("GET", "/actions/deleteSet.php?setId="+id);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				alert(obj.responseText+"\n");
				location.reload();
			}
		}
		obj.send(null);
	}
}


function getSet(){
	var lprice = document.getElementById("setLprice").value;
	var hprice = document.getElementById("setHprice").value;
	location.href = "/konfigurator/gotowe/"+lprice+"-"+hprice;
}

function setComponent(id){

	var checked = document.getElementById("setComponent"+id).checked;
	if (obj) {
		if (checked) {
			obj.open("GET", "/actions/setComponent.php?id="+id+"&status=1");
		} else {
			obj.open("GET", "/actions/setComponent.php?id="+id+"&status=0");
		}
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				//alert(obj.responseText);
			}
		}
		obj.send(null);
	}
}

function setCeneoRed(){
	var checked = document.getElementById("ceneoRed").checked;
	if (obj) {
		if (checked) {
			obj.open("GET", "/actions/setCeneoRed.php?status=1");
		} else {
			obj.open("GET", "/actions/setCeneoRed.php?status=0");
		}
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				//alert(checked+obj.responseText);
			}
		}
		obj.send(null);
	}
}


function confViewProduct(productId) {
	$('#confProductView').fadeIn(200);
	$('#confProductViewInner').html('Trwa ładowanie opisu...<br/><img src="/images/loading2.gif"/>');
	currentRequest.abort();
	currentRequest = $.ajax({
		url: '/actions/conf_getProductDescription.php',
		data: 'productId='+productId,
		success: function(data) {
			$('#confProductViewInner').html(data);
			Hide('loading2');
		}
	});
}

var currentOfferTable;
var currentRequest;
var currentCategory;
var prevStoreId = new Array();

function confLoadOffer(catId) {
	$('#confItemLoading'+catId).slideDown(100);
		currentRequest = $.ajax({
			url: '/actions/conf_loadStoreOffer.php',
			data: 'storeId='+storeId+'&catId='+catId,
			success: function(data) {
				$('#confItemLoading'+catId).slideUp(100);
				$('#storeOffer'+catId).html(data);
				$('#storeOffer'+catId).fadeIn(400);
				currentCategory = catId;
				currentOfferTable = $('#'+storeId+'d'+catId).dataTable({
						"aaSorting": [],
						"aoColumns": [ 
							null,
							{ "bSortable": false },
							null,
							{ "bSortable": false }
						]
					});
			}
		});
}


function confShowOffer(catId) {

	storeId = $('#AS_storeId').val();

	if (!catId)	catId = currentCategory;
	if (!catId) return false;

	$('.confItemChoose').slideUp(300, function() {
		if (prevStoreId[catId] != storeId) {
			$('#storeOffer'+catId).fadeOut(200);		
		}
	});
	
	$('#confItemChoose'+catId).slideDown(300, function() {
		if (($('#storeOffer'+catId).html() == '') || (prevStoreId[catId] != storeId)) {
			confLoadOffer(catId);
			prevStoreId[catId] = storeId;
		}
	});

	
	
	//classicViewPage('1','2');
	
	

	/*
	return true;
	
	// ======================= >

	if (!catId)	catId = currentCategory;
	if (!catId) return false;
	

	storeId = $('#AS_storeId').val();
	classicViewPage('1','2');
	
	Show('loading2');
	currentRequest = $.ajax({
		url: '/actions/conf_loadStoreOffer.php',
		data: 'storeId='+storeId+'&catId='+catId,
		success: function(data) {
			$('#storeOffer').html(data);
			Hide('loading2');
			currentCategory = catId;
			currentOfferTable = $('#'+storeId+'d'+catId).dataTable({
					"aaSorting": [],
					"aoColumns": [ 
						null,
						{ "bSortable": false },
						null,
						{ "bSortable": false }
					]
				});
		}
	});

	*/
}

function confAddComponent(catId, productId) {
	$('#confProductView').fadeOut(300);
	$('#confItemLoading'+catId).slideDown(100);
	currentRequest = $.ajax({
		url: '/actions/conf_setComponent.php',
		data: 'catId='+catId+'&productId='+productId,
		success: function(data) {
			$('#confItem'+catId).html(data);
			Hide('loading2');
			var totalCost = 0;
			$('.confProductPriceNum').each(function() {
                    totalCost += Number($(this).html());
                });
			$('#totalCost').html(totalCost + ' PLN');
			$('#confItemLoading'+catId).slideUp(100);
		}
	});
	

	
}
