// JavaScript Document

function Importer(collection, xmlFeed, dealernr, groupnr) {
	
	
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = generateCollection;
	} else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) generateCollection()
		};
 	} else {
		alert('Your browser does not support Ajax.');
		return;
	}
	
	if (dealernr > 0)
		xmlDoc.load(xmlFeed + "?dealernr=" + dealernr);
	else
		xmlDoc.load(xmlFeed + "?groupnr=" + groupnr);
	
	function setCallback(object) {
		callback = object;
	}
	
	function generateCollection() {
		var x = xmlDoc.getElementsByTagName('car');
		
		for (i = 0; i < x.length; i++) {
			
			number = '';
			manufacture = '';
			model = '';
			variant = '';
			engine = '';
			year = '';
			mileage = '';
			picture = '';
			price = '';
			department = '';
			
			for (j = 0; j < x[i].childNodes.length; j++) {
				if (x[i].childNodes[j].nodeType != 1) continue;
				
				switch (x[i].childNodes[j].nodeName) {		
					case ("number") : {
						if (x[i].childNodes[j].firstChild != null)
							number = x[i].childNodes[j].firstChild.nodeValue;
						
						break;
					}
					case ("manufacture") : {
						if (x[i].childNodes[j].firstChild != null)
							manufacture = x[i].childNodes[j].firstChild.nodeValue;
						
						break;
					}
					case ("model") : {
						if (x[i].childNodes[j].firstChild != null)
							model = x[i].childNodes[j].firstChild.nodeValue;
						
						break;
					}
					case ("variant") : {
						if (x[i].childNodes[j].firstChild != null)
							variant = x[i].childNodes[j].firstChild.nodeValue;
						
						break;
					}
					case ("engine") : {
						if (x[i].childNodes[j].firstChild != null)
							engine = x[i].childNodes[j].firstChild.nodeValue;
						
						break;
					}
					case ("year") : {
						if (x[i].childNodes[j].firstChild != null)
							year = x[i].childNodes[j].firstChild.nodeValue;
						
						break;
					}
					case ("mileage") : {
						if (x[i].childNodes[j].firstChild != null)
							mileage = x[i].childNodes[j].firstChild.nodeValue;
						
						break;
					}
					case ("picture") : {
						if (x[i].childNodes[j].firstChild != null)
						picture = x[i].childNodes[j].firstChild.nodeValue;
						
						break;
					}
					case ("price") : {
						if (x[i].childNodes[j].firstChild != null)
							price = x[i].childNodes[j].firstChild.nodeValue;
						
						break;
					}
					case ("department") : {
						if (x[i].childNodes[j].firstChild != null)
							department = x[i].childNodes[j].firstChild.nodeValue;
							
						break;
					}
				}
			}
			
			collection.addItem(number, manufacture, model, variant, year, mileage, engine, picture, price, department);
		}
		
		callback.start();
	}
	
	this.setCallback = setCallback;
}
