var businessname, zipcode, housenumber, searchmethod;

function addCheckInputEvents()
{
	businessname = document.getElementById('Bedrijfsnaam:');
	zipcode = document.getElementById('Postcode:');
	housenumber = document.getElementById('Huisnummer:');

	if(businessname)
		addEvent(businessname, 'change', checkInput);

	if(zipcode)
		addEvent(zipcode, 'change', checkInput);

	if(housenumber)
		addEvent(housenumber, 'change', checkInput);

	this.ajaxReq = makeRequest();
	this.ajaxTarget = 'call_ws.php';
}

function placeDiv()
{
	var div = document.getElementById('BusinessDiv');
	var dl = div.getElementsByTagName('DL')[0];

	/* empty definitionlist */
	while(dl.hasChildNodes())
	{
		dl.removeChild(dl.childNodes[0]);
	}

	div.className="content-block";
}

function checkInput()
{
	if(checkBusiness(businessname.value) && checkZipcode(zipcode.value) && checkHouseNumber(housenumber.value))
	{
		searchmethod = 'business';
		placeDiv();
		doAjax();
	}
	else if(checkZipcode(zipcode.value) && checkHouseNumber(housenumber.value))
	{
		searchmethod = 'address';
		placeDiv();
		doAjax();
	}
}

function checkZipcode(input)
{
	if(input == '')
	{
		return false;
	}

	var zipReg = /^\s*[1-9][0-9]{3}\s*[a-z]{2}/i;
	if(input.match(zipReg))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function checkBusiness(input)
{
	if(input == '')
	{
		return false;
	}

	if(input.length > 3)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function checkHouseNumber(input)
{
	if(input == '')
	{
		return false;
	}

	if(isNaN(parseInt(input)))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function doAjax()
{
	if (ajaxReq != undefined)
	{
		ajaxReq.open("POST", ajaxTarget, true);
		ajaxReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajaxReq.onreadystatechange = getResult;

		var param = '';
		if(searchmethod == 'business')
		{
			param = param + 'kind=Business';
		}
		else if(searchmethod == 'address')
		{
			param = param + 'kind=Address';
		}

		param = param + '&zipcode='+zipcode.value;
		param = param + '&housenumber='+housenumber.value;
		param = param + '&businessname='+businessname.value;

		ajaxReq.send(param);
	}
}

function printRow(dl, name, value, link)
{
	var dt = document.createElement('dt');
	var dd = document.createElement('dd');

	dt.appendChild(document.createTextNode(name));

	if(link)
	{
		var a = document.createElement('a');
		a.href='http://'+value;
		a.target='_blank';
		a.appendChild(document.createTextNode(value));
		dd.appendChild(a);
	}
	else
		dd.appendChild(document.createTextNode(value));

	dl.appendChild(dt);
	dl.appendChild(dd);
}

function getResult()
{
	try /* This try will catch any error that occurs in the AJAX request */
	{
		if(ajaxReq.readyState == 4)
		{
			if(ajaxReq.status == 200) /* Check if the request returned HTTP OK */
			{
				if(searchmethod == 'address')
				{
					var street = ajaxReq.responseXML.getElementsByTagName('street');
					var city = ajaxReq.responseXML.getElementsByTagName('city');

					document.getElementById('straatID').firstChild.nodeValue = '';
					document.getElementById('plaatsID').firstChild.nodeValue = '';

					document.getElementById('streetRow').className='hide';
					document.getElementById('cityRow').className='hide';

					if(street[0] && street[0].firstChild)
					{
						document.getElementById('streetRow').className = '';
						document.getElementById('straatID').className = '';
						document.getElementById('straatID').firstChild.nodeValue = street[0].firstChild.nodeValue;
					}

					if(city[0] && city[0].firstChild)
					{
						document.getElementById('cityRow').className = '';
						document.getElementById('plaatsID').className = '';
						document.getElementById('plaatsID').firstChild.nodeValue = city[0].firstChild.nodeValue;
					}
				}
				else if(searchmethod == 'business')
				{
					/* Give ID to parentdiv for styling */
					document.getElementById('BusinessDiv').parentNode.id = 'contact-form-container';

					var dl = document.getElementById('BusinessDiv').getElementsByTagName('DL')[0];

					var DossierNo = ajaxReq.responseXML.getElementsByTagName('DossierNo');
					var LegalformcodeText = ajaxReq.responseXML.getElementsByTagName('LegalformcodeText');
					var TradenameFull = ajaxReq.responseXML.getElementsByTagName('TradenameFull');
					var EstablishmentAddress = ajaxReq.responseXML.getElementsByTagName('EstablishmentAddress');
					var EstablishmentPostcodeCity = ajaxReq.responseXML.getElementsByTagName('EstablishmentPostcodeCity');
					var TelephoneNo = ajaxReq.responseXML.getElementsByTagName('TelephoneNo');
					var Domainname = ajaxReq.responseXML.getElementsByTagName('Domainname');

					if(TradenameFull[0] && TradenameFull[0].firstChild)
						printRow(dl, 'Naam:', TradenameFull[0].firstChild.nodeValue, false);

					if(EstablishmentAddress[0] && EstablishmentAddress[0].firstChild)
					{
						printRow(dl, 'Adres:', EstablishmentAddress[0].firstChild.nodeValue, false);
					}

					if(EstablishmentPostcodeCity[0] && EstablishmentPostcodeCity[0].firstChild)
					{
						printRow(dl, 'Postcode & Plaats:', EstablishmentPostcodeCity[0].firstChild.nodeValue, false);
					}

					if(DossierNo[0] && DossierNo[0].firstChild)
						printRow(dl, 'KVK Nr.:', DossierNo[0].firstChild.nodeValue, false);

					if(LegalformcodeText[0] && LegalformcodeText[0].firstChild)
						printRow(dl, 'Rechtsvorm:', LegalformcodeText[0].firstChild.nodeValue, false);

					if(TelephoneNo[0] && TelephoneNo[0].firstChild)
						printRow(dl, 'Tel Nr.:', TelephoneNo[0].firstChild.nodeValue, false);

					if(Domainname[0] && Domainname[0].firstChild)
						printRow(dl, 'WWW:', Domainname[0].firstChild.nodeValue, true);

					if(!TradenameFull[0] && !DossierNo[0] && !LegalformcodeText[0] && !TelephoneNo[0] && !Domainname[0])
						document.getElementById('BusinessDiv').className = 'hide';
				}

			}
			else /* The status was not HTTP OK so give warning */
			{
				/*alert('There was a problem with the request.');*/
			}
		}
	}
	catch (e)
	{
		throw e;
	}
}