//xmlhttp
var req;

function loadXMLDoc(url) {

	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			//alert(req.responseXML.documentElement)
            buildRecomendations();
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

function buildRecomendations () {
	
	var autos = req.responseXML.getElementsByTagName("auto");
	var suggestionList = document.getElementById("suggestionList");
	
	if (autos.length >= 3)
	{
		document.getElementById("suggestions").style.display = "block";
	}
	
	for (i=0; i<autos.length; i++)
	{
		var ca = autos[i];
		
		var vin = getElementTextNS("", "VIN", ca, 0);
			//alert(ca.getElementsByTagName("VIN")[0].firstChild.nodeValue);
		var stockNumber = ca.getAttribute("stockNumber");
		var makeName = getElementTextNS("", "makeName", ca, 0);
		var modelName = getElementTextNS("", "modelName", ca, 0);
		var year = getElementTextNS("", "year", ca, 0);
		var price = getElementTextNS("", "price", ca, 0);
		var description = getElementTextNS("", "description", ca, 0);
		description = description.replace(",", ", ")
		
		//alert(getElementTextNS("", "description", autos[i], 0));
		
		var anchorHref = "/inventorydetails.asp?StockNum=" + stockNumber + "&conv=yes&";
		
		var recLI = document.createElement("li");
		if (i==2)
		{
			recLI.className = "last";
		}
		var wrapper = recLI.appendChild(document.createElement("div"));
		wrapper.className = "suggestionListWrapper";
		
		var anchor1 = document.createElement("a");
		var anchor2 = document.createElement("a");
		var anchor3 = document.createElement("a");
		anchor1.href = anchor2.href = anchor3.href = anchorHref;
		
		
		//rec image
		var recImage = new Image();
		recImage.src = "http://fritzinfishers.com/pictures/01_" + vin + ".jpg";
		recImage.className = "suggestionImg";
		recImage.style.width = "115px";
		recImage.style.height = "86px";
		
		anchor1.appendChild(recImage);
		wrapper.appendChild(anchor1);
		
		
		var suggestionSpecs = wrapper.appendChild(document.createElement("div"));
		suggestionSpecs.className = "suggestionSpecs";
		var header = document.createElement("h5");
		header.appendChild(document.createTextNode(year + " " + makeName + " " + modelName));
		
		suggestionSpecs.appendChild(header);
		
		var des = suggestionSpecs.appendChild(document.createElement("p"));
		des.appendChild(document.createTextNode(description + " ..."));
		var p = suggestionSpecs.appendChild(document.createElement("p"));
		p.className = "priceBlock";
		anchor3.innerHTML = "<img src='images/myPriceBtn.gif' />";
		anchor3.setAttribute("style", "float:right;");
		anchor3.className = "myPriceBtn";
		
		p.appendChild(anchor3);
		p.appendChild (document.createTextNode(price));
		
		suggestionList.appendChild(recLI);
	}
}


//retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}
