/**
* data.js
*
* Provides a means of extrating data fro the featured car in a JSON
* formatted data structure.
*/

//var imageDir = "../../../auto/listings/images/";
var imageDir = "../../auto/listings/images/";

/**
*   Makes an Ajax call to extract the featured car record
*
*/
function loadurl(dest) {
	xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.onreadystatechange = pop_table;
	xmlhttp.open("GET", dest);	
	xmlhttp.send(null);
}

/**
*	Processes the response from the Ajax call and loads the appropiate
*	elements with data returned.
*
*/
function pop_table() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var featuredCar = eval("(" + xmlhttp.responseText + ")");
		
		// Modify the DOM to load the featured car data.  This could be applied to
		// any elements that are needed
		document.getElementById("carprice").innerHTML 	= "" + featuredCar.record[0].price;
		document.getElementById("features").innerHTML 	= featuredCar.record[0].features;
		document.getElementById("tagline").innerHTML 	= featuredCar.record[0].tagline;
		
		// Used the default blnk image if no image is in the DB
		if (!featuredCar.record[0].imageA)
		{
			document.getElementById("shotA").src	= "images/blank.gif";
		} 
		else document.getElementById("shotA").src	= imageDir + featuredCar.record[0].imageA;
		
		if (!featuredCar.record[0].imageB)
		{
			document.getElementById("shotB").src = "images/blank.gif";
		} 
		else document.getElementById("shotB").src = imageDir + featuredCar.record[0].imageB;	

		//Update the links in the feature section to point to the correct carID value
		var carID = featuredCar.record[0].carid;
		document.getElementById("shotAlink").href = "cardetails.php?id=" + carID;	
		document.getElementById("shotBlink").href = "cardetails.php?id=" + carID;
		//document.getElementById("shotClink").href = "cardetails.php?id=" + carID;		
	}
}

/**
*	An intiailization stub to call for data and replace DOM content where
*	needed.
*
*/
function init()
{
	var url 	= "scripts/ajax/ajax_getFeaturedCar.php";
	loadurl(url);
}
