// Ajax stuff
var request = null;
function createRequest(){
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft)	{
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}
	if (request == null)
		alert("Error creating request object!");
}

function getInfo(page,MyDiv){
	thisdiv = MyDiv;
	createRequest();
	request.open('GET', page, true);
	request.onreadystatechange = handleInfo;
	request.send(null);
}
function handleInfo(){
	if(request.readyState == 4){
		if(thisdiv != "") {
			var response = request.responseText;
			//alert(response);
			document.getElementById(thisdiv).innerHTML = response;
		} else {
			alert("Could not complete the operation, " + thisdiv + " was not found!" );
		}
		
	}
}

function openCat(pg,cat) {
	getInfo('/_products/__view-prod.php?pgId=' + pg + '&catId=' + cat + '&r='+Math.random(), 'main');
}

function openDocs(pg,cat,doc) {
	getInfo('/_products/__view-prod.php?forcelogin=1&pgId=' + pg + '&catId=' + cat + '&docTypeId=' + doc + '&r='+Math.random(), 'main');
}

function setCol( p_o, p_c ) {
	if ( p_o && p_o.style )
		p_o.style.backgroundColor = p_c;
}

// start popup div
function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-150;//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-150;//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}
function openArt(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);		
}
// end popup div

// Product Search
function searchProd() {
	var searchq = encodeURI(document.getElementById('searchq').value);
	var limitd = document.getElementById('limit') ;
	var limit = document.getElementById('limit').value;
	if (document.getElementById('searchq').value.length > 1) {
		document.getElementById('search-result').innerHTML='';
		var what
		for (var i=0; i<document.searchForm.what.length; i++)  {
			if (document.searchForm.what[i].checked)  {
				what = document.searchForm.what[i].value
			}
		} 
		document.getElementById('msg').style.display = "block";
		document.getElementById('msg').innerHTML = 'Searching for <strong>' + document.getElementById('searchq').value +'</strong>';
		document.getElementById('search-result').innerHTML = '<br /><img src ="/fileadmin/templates/images/searching.gif" alt="Searching" />';
		// Set the random number to add to URL request
		nocache = Math.random();
//		alert('/_products/search.php?name=' + searchq + '&what=' + what + '&nocache=' + nocache);
		getInfo('/_products/search.php?name=' + searchq + '&what=' + what + '&limit=' + limit + '&nocache=' + nocache, 'search-result');
	} else {
		document.getElementById('search-result').innerHTML='';
	}
}
// END Search
