/**
 *	stop ENTER key when pressed @ form
 */
function stopRKey(evt) { 
	var evt = (evt) ? evt : ((event) ? event : null);
  	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  	if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
} 
document.onkeypress = stopRKey;

/**
 * connect using AJAX and refresh data send from PHP - MySQL
 */
var http_request = false;
function makeRequest(url, parameters, bool)
{
	http_request = false;
	if(window.XMLHttpRequest) // mozilla, safari,...
	{
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/html');
		}
	}
	else if (window.ActiveXObject) // IE
	{
		try
		{
			http_request = new ActiveXObject("Msxml12.XMLHTTP");
		}
		catch(e) 
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {}
		}
	}
	if (!http_request)
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	if (bool == true)
		http_request.onreadystatechange = alertContents;
	else if (bool == false)
		http_request.onreadystatechange = alertContentsFast;
	
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-Length", parameters.length);
	http_request.send(parameters);
}

function alertContents()
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			result = http_request.responseText;
			document.getElementById('myspan').innerHTML = result;
		}
		else
		{
			alert('There was a problem with the request');
		}
	}
}

function alertContentsFast()
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			result = http_request.responseText;
			document.getElementById('myspan2').innerHTML = result;
			positionDiv();
		}
		else
		{
			alert('There was a problem with the request');
		}
	}
}

/**
 * Default div/shadow/overlay property settings
 * 
 * @param divName
 * @return array with div/shadow/overlay properties
 */
function divProperties(divName)
{
	var el = document.getElementById(divName);
	el.style.minHeight = '300px'; 
	el.style.padding = '20px'; 
	el.style.zIndex = '200'; 
	el.style.position = 'absolute';
	el.style.backgroundColor = '#ffffff'; 
	el.style.border = '1px solid #0098a1'; 
	el.style.width = '590px';
	
	var overLay = document.getElementById('overlay');
	var shadow = document.getElementById('shadow');
	var x = 0;
	var y = 0;
	var overlayWidth = 0;
	var overlayHeight = 0;
	var width = 0; 
	var height = 0;
	var divWidth = parseInt(el.style.width) / 2;
			
	// determine browser
	if (typeof(window.innerWidth) == 'number')
	{
		//alert('FF');
		width = document.body.offsetWidth; // window.innerWidth - 17; // 17 = compensate for FF scrollbar
		height = document.body.offsetHeight;
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		//alert('IE 6+');
		width = document.documentElement.clientWidth;
		height = document.body.offsetHeight;
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		//alert('IE 4');
		width = document.body.clientWidth;
		height = document.body.clientHeight + 390;
	}
	
	// create array with variables
	var array = new Object()
	array['el'] = el;
	array['overlay'] = overLay;
	array['shadow'] = shadow;
	array['x'] = x;
	array['y'] = y;
	array['overlayWidth'] = overlayWidth;
	array['overlayHeight'] = overlayHeight;
	array['width'] = width;
	array['height'] = height;
	array['divWidth'] = divWidth;
	
	return array;
}

/**
 * 
 * advanced search section
 */

function search()
{
	var el = document.searchForm;
	var prov = el.province
	var query = "?fastSearch=false&";
	
	for (i = 0; i < el.length; i++)
	{
		if (el[i].checked == true)
			query += el[i].name + '=' + el[i].value + '&';
	}
	
	query += 'province=' + prov.value + '&';
	makeRequest('advancedSearch.php', query, true);
}

function hideShow()
{
	var array = divProperties('divResult');
	var x = array['x'];
	var y = array['y'];
	var width = array['width'];
	var height = array['height'];
	var divWidth = array['divWidth'];
	var el = array['el'];
	var overlayWidth = array['overlayWidth'];
	var overlayHeight = array['overlayHeight'];
	var shadow = array['shadow'];
	var overLay = array['overlay'];
		
	// new position
	x += (width / 2) - divWidth;
	y += 300;
	
	// new width/height
	overlayWidth += width;
	overlayHeight += height;
	
	if (el.style.display == 'none')
	{
		overLay.style.width = overlayWidth + 'px';
		overLay.style.height = overlayHeight + 'px';
		overLay.style.display = 'block';
		
		el.style.display = 'block';
		el.style.left = x + 'px';
		el.style.top = y + 'px';
		
		var divHeight = el.offsetHeight;
		shadow.style.width = (divWidth * 2 + 40) + 'px'; // 40 = padding of the div, which is 20px
		shadow.style.height = divHeight + 'px';
		shadow.style.left = (x + 6) + 'px';
		shadow.style.top = (y + 5) + 'px';
		shadow.style.display = 'block';
		
	}
	else if (el.style.display == 'block')
	{
		overLay.style.display = 'none';
		shadow.style.display = 'none';
		el.style.display = 'none';
	}
}

/**
 * 
 * fast search section
 */
function fastSearching()
{
	var el = document.fastSearchForm.fastSearch;
	var page = document.fastSearchForm.searchPage;
	var query = "?fastSearch=true&";
	
	if (el.value == '')
		alert('Geen zoekcriteria ingevuld.');
	else
	{
		query += 'criteria=' + el.value + '&';
		query += 'searchPage=' + page.value;
		makeRequest('fastSearch.php', query, false);
	}
}

function closeDiv(obj)
{
	var el = document.getElementById(obj);
	var overLay = document.getElementById('overlay');
	var shadow = document.getElementById('shadow');
	if (el.style.display == 'block')
	{
		el.style.display = 'none';
		overLay.style.display = 'none';
		shadow.style.display = 'none';
	}
}

function positionDiv()
{
	var array = divProperties('divResultFast');
	var x = array['x'];
	var y = array['y'];
	var width = array['width'];
	var height = array['height'];
	var divWidth = array['divWidth'];
	var el = array['el'];
	var overlayWidth = array['overlayWidth'];
	var overlayHeight = array['overlayHeight'];
	var shadow = array['shadow'];
	var overLay = array['overlay'];
	
	// new position
	x += (width / 2) - divWidth;
	y += 150;
	
	// new width/height
	overlayWidth += width;
	overlayHeight += height;
	
	overLay.style.width = overlayWidth + 'px';
	overLay.style.height = overlayHeight + 'px';
	overLay.style.display = 'block';
	
	el.style.left = x + 'px';
	el.style.top = y + 'px';
	
	var divHeight = el.offsetHeight;
	shadow.style.width = (divWidth * 2 + 40) + 'px'; // 40 = padding of the div, which is 20px
	shadow.style.height = divHeight + 'px';
	shadow.style.left = (x + 6) + 'px';
	shadow.style.top = (y + 5) + 'px';
	shadow.style.display = 'block';
}
