var timer = null;				// some stuff to prevent IE6
var getResultsCalled = false;	// from calling getResults() twice

// Ajax sender
function sendForm(oForm, fnCallback) {
	var xmlHttp = null;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlHttp = null;
		}
	}
	@end @*/
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			xmlHttp = null;
		}
	}
	if (!xmlHttp && window.createRequest) {
		try {
			xmlHttp = window.createRequest();
		} catch (e) {
			xmlHttp = null;
		}
	}

	if (!xmlHttp) {
		return true;	// normal form submit!
	}
	
	var oAjax = document.getElementById('ajaxloader');
	oAjax.style.visibility = "visible";
	
	var url = oForm.action;
	var data = "formname=" + oForm.name;
	
	// construct the rest of the data string
	for (var i = 0; i < oForm.elements.length; i++) {
		data += "&" + oForm.elements[i].name + "=";
		data += oForm.elements[i].value;
	}
	
	data = data.replace(/\+/g, '%2B');
	
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {	// if "OK"
				fnCallback(xmlHttp);
			} else {
				alert("Error: cannot send form data");
			}
		}
	};
	
	xmlHttp.open("POST", url, true);	// load data asynchronously
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	xmlHttp.send(data);
	
	return false;	// disable form submitting in the form's onsubmit handler
}

// Ajax updaters
function showResults(xmlHttp) {
	var oResults = document.getElementById('searchresults');
	var oProduct = document.getElementById('product');
	var oAjax = document.getElementById('ajaxloader');
	
	oResults.innerHTML = xmlHttp.responseText;
	oAjax.style.visibility = "hidden";
	oResults.style.visibility = "visible";
	oProduct.style.visibility = "hidden";
}

function showProduct(xmlHttp) {
	var oProduct = document.getElementById('product');
	var oAjax = document.getElementById('ajaxloader');
	
	oProduct.innerHTML = xmlHttp.responseText;
	oAjax.style.visibility = "hidden";
	oProduct.style.visibility = "visible";
}

function showPage(xmlHttp) {
	var oPage = document.getElementById('page_nunnauuni');
	var oAjax = document.getElementById('ajaxloader');
	
	oPage.innerHTML = xmlHttp.responseText;
	oAjax.style.visibility = "hidden";
}

function showSubPage(xmlHttp) {
	var oSubPage = document.getElementById('subpage_nunnauuni');
	var oAjax = document.getElementById('ajaxloader');
	
	oSubPage.innerHTML = xmlHttp.responseText;
	oAjax.style.visibility = "hidden";
	if (myLightbox)
		myLightbox.updateImageList();
}

// Ajax getters
function getResults(cat, heatCat, name, sortCriteria) {
	if (getResultsCalled) return;
	clearTimeout(timer);
	getResultsCalled = true;
	var oForm = document.forms['searchform'];
	oForm.category.value = (cat) ? cat : "";
	oForm.heatcategory.value = (heatCat) ? heatCat : "";
	oForm.searchtext.value = (name) ? name : "";
	oForm.sorting.value = (sortCriteria) ? sortCriteria : "";
	sendForm(oForm, showResults);
	timer = setTimeout("getResultsCalled = false", 100);
}

function getPage(nr, lang) {
	var oForm = document.forms['pageform'];
	oForm.nr_page.value = nr;
	oForm.lang.value = lang;
	sendForm(oForm, showPage);
}

function getSubPage(nr, lang) {
	var oForm = document.forms['subpageform'];
	oForm.nr_subpage.value = nr;
	oForm.lang.value = lang;
	sendForm(oForm, showSubPage);
}

function getProduct(productID) {
	var oForm = document.forms['productform'];
	oForm.id.value = productID;
	sendForm(oForm, showProduct);
}

function sendContact() {
	var oForm = document.forms['contactform'];
	sendForm(oForm, hideContactForm);
	return false;
}

// Show/hide functions
function hideSearch() {
	var oResults = document.getElementById('searchresults');
	var oProduct = document.getElementById('product');
	oResults.style.visibility = "hidden";
	oProduct.style.visibility = "hidden";
}

function hideProduct() {
	var oProduct = document.getElementById('product');
	oProduct.style.visibility = "hidden";
}

function showContactForm() {
	var oContact = document.getElementById('yhteyslomake');
	oContact.style.top = "20px";
}

function hideContactForm() {
	var oContact = document.getElementById('yhteyslomake');
	var oAjax = document.getElementById('ajaxloader');
	
	oContact.style.top = "-600px";
	oAjax.style.visibility = "hidden";
}

function openWin(url, width, height) {
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	var features = "channelmode=no,directories=no,left=" + left + ",top=" + top + ",location=no,menubar=no,status=no,titlebar=no,toolbar=no,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height;
	url = "../popup.php?url=" + url + "&width=" + width + "&height=" + height;
	var newWin = window.open(url, 'nu_popup', features);
	newWin.opener = self;
	newWin.focus();
}

function changeLanguage(lang) {
	var oForm = document.forms["languageForm"];
	oForm.elements["lang"].value = lang;
	oForm.submit();
}