var p;
var xmlhttp;
var xmlDoc;

function getDealerFromLocation(loc){
	p = loc;
	if (loc.length > 0){ 
		listDealers();
	} 
	else{ 
		document.getElementById(loc).innerHTML="";
	} 

}
function displayDealers(){
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4){// if "OK"
		if (xmlhttp.status==200){
			xmlDoc = xmlhttp.responseXML.documentElement;
			displayDealersByLoc("IntDealers");
			displayDealersByLoc("USDealers");
					
			//Re-apply Behaviour
			Behaviour.apply();
		}
	}
}
function displayDealersByLoc(Loc){
	var d = xmlDoc.getElementsByTagName(Loc);
	var c = d[0].childNodes;
	var jString;

	var e = document.getElementById("dealer_listing");
	var eUL = document.createElement("ul");
	var eBR = document.createElement("br");

	for(var i=0; i<c.length; ++i){
		if (c[i].nodeName!="#text") {
			var eLI = document.createElement("li");			
			var jLink = document.createElement("a");
			jLink.innerHTML = c[i].nodeName;
			//jLink.href = "javascript:getDealerFromLocation('" + c[i].nodeName + "')";
			jLink.href = c[i].nodeName;
			eLI.appendChild(jLink);
			eUL.appendChild(eLI);		
		}
	}

	e.appendChild(eUL);	
	e.appendChild(eBR);
}
function listDealers(){
	var d = xmlDoc.getElementsByTagName(p);
	var d2 = d[0].getElementsByTagName("dealer");

	var e = document.getElementById("results");
	
	//Clear Content
	e.innerHTML="";
	
	for(var i=0; i<d2.length; ++i){
		var eUL = document.createElement("ul");
		
		var eLI1 = document.createElement("li");
		var eLI2 = document.createElement("li");
		var eLI3 = document.createElement("li");
		var eLI4 = document.createElement("li");
		var eLI5 = document.createElement("li");
		var eLI6 = document.createElement("li");
		var eLI7 = document.createElement("li");
		var eLI8 = document.createElement("li");
		
		var l1 = d2[i].getElementsByTagName("line1");
		var l2 = d2[i].getElementsByTagName("line2");
		var l3 = d2[i].getElementsByTagName("line3");
		var p1 = d2[i].getElementsByTagName("phone");
		var p2 = d2[i].getElementsByTagName("phone2");
		var fx = d2[i].getElementsByTagName("fax");
		var lk = d2[i].getElementsByTagName("link");
		var em = d2[i].getElementsByTagName("email");

		if (l1[0].firstChild){eLI1.innerHTML = l1[0].firstChild.nodeValue; eUL.appendChild(eLI1);}
		if (l2[0].firstChild){eLI2.innerHTML = l2[0].firstChild.nodeValue; eUL.appendChild(eLI2);}
		if (l3[0].firstChild){eLI3.innerHTML = l3[0].firstChild.nodeValue; eUL.appendChild(eLI3);}
		if (p1[0].firstChild){eLI4.innerHTML = p1[0].firstChild.nodeValue; eUL.appendChild(eLI4);}
		if (p2[0].firstChild){eLI5.innerHTML = p2[0].firstChild.nodeValue; eUL.appendChild(eLI5);}
		if (fx[0].firstChild){eLI6.innerHTML = fx[0].firstChild.nodeValue; eUL.appendChild(eLI6);}
		if (lk[0].firstChild){var eLink = document.createElement("a"); eLink.href = lk[0].firstChild.nodeValue; eLink.innerHTML = lk[0].firstChild.nodeValue; eLI7.appendChild(eLink); eUL.appendChild(eLI7);}
		if (em[0].firstChild){var eEmail = document.createElement("a"); eEmail.href = "mailto:"+em[0].firstChild.nodeValue; eEmail.innerHTML = em[0].firstChild.nodeValue; eLI8.appendChild(eEmail); eUL.appendChild(eLI8);}
		
		e.appendChild(eUL);
	}
		
}
function loadDealers(){
	// code for Mozilla, etc.
	if (window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();  
		xmlhttp.onreadystatechange=displayDealers;
		xmlhttp.open("GET",'dealers.xml',true);
		xmlhttp.send(null);
	}
	// code for IE
	else if (window.ActiveXObject){
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttp){
			xmlhttp.onreadystatechange=displayDealers;
			xmlhttp.open("GET",'dealers.xml',true);
			xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );  //Prevent IE caching XML file
			xmlhttp.send();
		}
  	}
	else{
		alert("Your browser can't handle this script");
		return;
	}
}

//Behaviour rules 
var dealerRules = {
	'#dealer_listing a' : function(el){
		el.onmouseover = function(){
			window.status = this.innerHTML;
			return true;
		}
		el.onmouseout = function(){
			window.status = "";
		}
		el.onclick = function(){
			getDealerFromLocation(this.innerHTML);
			return false;
		}
	}
};
loadDealers();
Behaviour.register(dealerRules);