function fetchInfo(phone, ids){
	var data = new Array();
	data['phone'] = phone;
	
	// load img
	 
	document.getElementById('ajaxloadimg').style.display = "";
	
	makeRequestErino("http://www.daekogslanger.dk/theme/daekbutikkennew/erino.php?type=person", data, ids);
}


function makeRequestErino(url, data, ids) {
    var http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
            // See note below about this line
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    
    // construct url
    for (var key in data) {
    	if(key == 'exists') continue;
    	url += '&'+key+'='+data[key];
    }
    http_request.onreadystatechange = function() { loadDataXMLErino(http_request, ids); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function loadDataXMLErino(http_request, ids){
	var content;
    if (http_request.readyState == 4) {
         if (http_request.status == 200) {
             content = http_request.responseXML;
         } else {
             alert('There was a problem with the request.');
         }
     }else{
     	return;
     }
	content = content.getElementsByTagName("person-list");
	if(content.length == 0){
		// no data found
		document.getElementById('ajaxmessage').innerHTML = 'Ingen information fundet';
		document.getElementById('ajaxloadimg').style.display = "none";
		return;
	}
	
	var name = "";
	var email = "";
	var adr = "";
	var zip = "";
	var city = "";
	
	content = content[0];
	var person = content.getElementsByTagName("person");
	if(person.length == 0){
		document.getElementById('ajaxmessage').innerHTML = 'Ingen information fundet';
		document.getElementById('ajaxloadimg').style.display = "none";
		return;
	}else{
		person = person[0];
	}
	var address = person.getElementsByTagName("address")[0];
	
	if(person.getElementsByTagName("firstname")[0].firstChild)
		name = person.getElementsByTagName("firstname")[0].firstChild.nodeValue;
	if(person.getElementsByTagName("lastname")[0].firstChild)
		name += " "+person.getElementsByTagName("lastname")[0].firstChild.nodeValue;
		
	if(person.getElementsByTagName("email")[0].firstChild)
		email = person.getElementsByTagName("email")[0].firstChild.nodeValue;
		
	if(address.getElementsByTagName("street-name")[0].firstChild)
		adr = address.getElementsByTagName("street-name")[0].firstChild.nodeValue;
	if(address.getElementsByTagName("street-number")[0].firstChild)
		adr += " "+address.getElementsByTagName("street-number")[0].firstChild.nodeValue;
	
	if(address.getElementsByTagName("zipcode")[0].firstChild)
		zip = address.getElementsByTagName("zipcode")[0].firstChild.nodeValue;
		
	if(address.getElementsByTagName("district")[0].firstChild)
		city = address.getElementsByTagName("district")[0].firstChild.nodeValue;
		
	for(var key in ids){
		if(key == 'exists') continue;
		
		var input = document.getElementById(ids[key]);
		
		if(key == 'name')
			input.value = name;
		if(key == 'email')
			input.value = email;
		if(key == 'adr')
			input.value = adr;
		if(key == 'zip')
			input.value = zip;
		if(key == 'city')
			input.value = city;
	}
	// stop button
	document.getElementById('ajaxloadimg').style.display = "none";
}

