var xmlHttp = createXmlHttpRequestObject();
var xmlHttpNav = createXmlHttpRequestObject();
var anchor = new String() ;

function createXmlHttpRequestObject() {
	var xmlHttp;
	
	try {
		xmlHttp = new XMLHttpRequest();
	} catch(e) {
		var XmlHttpVersions = new Array(
				"MSXML2.XMLHTTP.6.0",
				"MSXML2.XMLHTTP.5.0",
				"MSXML2.XMLHTTP.4.0",
				"MSXML2.XMLHTTP.3.0",
				"MSXML2.XMLHTTP",
				"Microsofot.XMLHTTP"
		);
		
		for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
			try {
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			} catch(e) {}
		}
	}
	
//	if(!xmlHttp)
//		alert("BĹ�Ä�d podczas tworzenia obiektu XMLHttpRequest.");
//	else
		return xmlHttp;
}

function createXmlHttpRequestObjectNav() {
	var xmlHttpNav;
	
	try {
		xmlHttpNav = new XMLHttpRequest();
	} catch(e) {
		var XmlHttpVersionsNav = new Array(
				"MSXML2.XMLHTTP.6.0",
				"MSXML2.XMLHTTP.5.0",
				"MSXML2.XMLHTTP.4.0",
				"MSXML2.XMLHTTP.3.0",
				"MSXML2.XMLHTTP",
				"Microsofot.XMLHTTP"
		);
		
		for(var i=0; i<XmlHttpVersionsNav.length && !xmlHttpNav; i++) {
			try {
				xmlHttpNav = new ActiveXObject(XmlHttpVersionsNav[i]);
			} catch(e) {}
		}
	}
	
//	if(!xmlHttpNav)
//		alert("BĹ�Ä�d podczas tworzenia obiektu XMLHttpRequest.");
//	else
		return xmlHttpNav;
}

function setCurrentTab(val, par) {
	document.getElementById('currentTab').value = val;
	document.getElementById('facetedParam').value = par;
}



function pagination(s, start, rows, page, step) {
	// What is $(document).ready ? See: http://flowplayer.org/tools/documentation/basics.html#document_ready
		anchor = document.getElementById('testresults').value;
	author = document.getElementById('authoruid').value;
	sort = document.getElementById('sortResults').value;
	$('#basic-modal-content').modal();	
//	alert(sort);
	if(anchor == 'Wszystkie')
		special = 0;
	else
		special = 1;
	if(xmlHttp) {
		try {
			value = document.getElementById('facetedParam').value;
			var params = "sword=" + s + "&id=332&typeNum=1234&no_cache=1&facetedParam="+value+"&rows="+rows+"&start="+start+"&page="+page+"&ifCat="+special+"&step="+step;
			if(author != -1 && author != '')
				var params = "sword=" + s + "&id=332&typeNum=1234&no_cache=1&facetedParam="+value+"&rows="+rows+"&start="+start+"&page="+page+"&ifCat="+special+"&step="+step+"&author="+author;
			
			if(sort != '')
				params = params.concat("&sort="+sort);

//			alert("params: " + params);
			xmlHttp.open("GET", "index.php?" + params, true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		} catch(e) {
//			alert("Nie mogÄ� nawiÄ�zaÄ� poĹ�Ä�czenia z serwerem:\n" + e.toString());
		}
	}
}

function sortResults(url) {
	
}

//function suggest(e) {
//	alert(document.getElementById('sword').value);
//}

function reQuery(value, a, s, step) {
	document.getElementById('currentTab').value = a;
	document.getElementById('facetedParam').value = value;
	author = document.getElementById('authoruid').value;
	sort = document.getElementById('sortResults').value;
	$('#basic-modal-content').modal();	
	
	if(value == 'Wszystkie')
		special = 0;
	else
		special = 1;
	anchor = a;
	if(xmlHttp) {
		try {
			var params = "sword=" + s + "&id=332&typeNum=1234&no_cache=1&facetedParam="+value+"&ifCat="+special+"&step="+step;
			if(author != -1 && author != '')
				var params = "sword=" + s + "&id=332&typeNum=1234&no_cache=1&facetedParam="+value+"&ifCat="+special+"&step="+step+"&author="+author;
			
			if(sort != '')
				params = params.concat("&sort="+sort);
			
			xmlHttp.open("GET", "index.php?" + params, true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		} catch(e) {
//			alert("Nie mogÄ� nawiÄ�zaÄ� poĹ�Ä�czenia z serwerem:\n" + e.toString());
		}
	}
}

function newDrilling(childrenFlag, search, anch, h) {
	hook = h;
	author = document.getElementById('authoruid').value;
	sort = document.getElementById('sortResults').value;
	$('#basic-modal-content').modal();	
	if(childrenFlag == 0) {}
//		alert('stop');
	else {
		try {
			var params = "sword=" + search + "&id=332&typeNum=1234&no_cache=1&drilling=1&facetedParam=" + anch;
			if(author != -1 && author != '')
				var params = "sword=" + search + "&id=332&typeNum=1234&no_cache=1&drilling=1&facetedParam=" + anch+"&author="+author;
			
			if(sort != '')
				params = params.concat("&sort="+sort);
			
//			alert(params);
			xmlHttpNav.open("GET", "index.php?" + params, true);
			xmlHttpNav.onreadystatechange = handleRequestStateChangeNav;
			xmlHttpNav.send(null);
		} catch(e) {
//			alert("3 Nie mogę nawiązać połączenia z serwerem:\n" + e.toString())
		}
	}
}

function drilling(value, s) {
	author = document.getElementById('authoruid').value;
	sort = document.getElementById('sortResults').value;
	$('#basic-modal-content').modal();	
		if(xmlHttpNav) {
		try {
			var params = "sword=" + s + "&id=332&typeNum=1234&no_cache=1&drilling=1&facetedParam="+value;
			if(author != -1 && author != '')
				var params = "sword=" + s + "&id=332&typeNum=1234&no_cache=1&drilling=1&facetedParam="+value+"&author="+author;
			
			if(sort != '')
				params = params.concat("&sort="+sort);
			
			xmlHttpNav.open("GET", "index.php?" + params, true);
			xmlHttpNav.onreadystatechange = handleRequestStateChangeNav;
			xmlHttpNav.send(null);
		} catch(e) {
//			alert("2 Nie mogę nawiązać połączenia z serwerem:\n" + e.toString());
		}
	}
}


function handleRequestStateChange() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			try {
				handleServerResponse();
				$.modal.close();
			} catch(e) {
				alert("BĹ�Ä�d odczytu odpowiedzi; " + e.toString());
			}
		} else {
//			alert("PojawiÄ� siÄ� problem przy pobieraniu danych:\n" + xmlHttp.statusText);
		}
	}
}

function handleRequestStateChangeNav() {
	if(xmlHttpNav.readyState == 4) {
		if(xmlHttpNav.status == 200) {
			try {
				handleServerResponseNav();
				$.modal.close();
			} catch(e) {
				alert("2 BĹ�Ä�d odczytu odpowiedzi; " + e.toString());
			}
		} else {
//			alert("2 PojawiÄ� siÄ� problem przy pobieraniu danych:\n" + xmlHttpNav.statusText);
		}
	}
}

function setSortingFlag(flag) {
	document.getElementById('sortResults').value = flag;
}


function handleServerResponse() {
	var textResponse = xmlHttp.responseText;
	anchor = 'testresults';
	node = document.getElementById(anchor);

	while (node.hasChildNodes()) {
	  node.removeChild(node.firstChild);
	}

	myDiv = document.getElementById(anchor).innerHTML = textResponse;
}

function handleServerResponseNav() {
	var textResponse2 = xmlHttpNav.responseText;
	anchorNav = hook;
//	alert(anchorNav);
	document.getElementById(anchorNav).style.display = 'block';
	
	last = document.getElementById('lastSelection').value;
//	if(last == '') {
//		document.getElementById('lastSelection').value = anchorNav;
//	}
//	else {
//		document.getElementById(last).style.display = 'none';
//		document.getElementById('lastSelection').value = anchorNav;
//	}
	
	nodeNav = document.getElementById(anchorNav);
	while(nodeNav.hasChildNodes()) {
		nodeNav.removeChild(nodeNav.firstChild);
	}

	myDivNav = document.getElementById(anchorNav).innerHTML = textResponse2;
}
