var xmlhttp;
var iWho = "AXO";
var curYear;
var curSel;
var curID;
iTetValue;

function $(id)
{
	return document.getElementById(id);
}

function iLoad(iRel)
{
	xmlhttp = GetXmlHttpObject();

	if (xmlhttp==null)
	{
		alert ("An error has occurred. Please try again later.");
		return;
	}

	var url = "/files/1886303/My_JSON_Files/JSON_" + iRel;
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("get",url,true);
	xmlhttp.send();
}

function stateChanged()
{
	$("iPR").innerHTML = "<img src=\"/files/1886303/uploaded/loading.gif\"/><br/>loading...";

	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		var xmlDoc = xmlhttp.responseText;

		var iHead = document.getElementsByTagName('head').item(0);
		var iScript = document.createElement( "script" );
		iScript.language = "javascript";
		iScript.type = "text/javascript";
		iScript.id = "iMagic";
		iScript.defer = true;
		iScript.text = xmlDoc;
		iHead.appendChild( iScript ); 

		$("iPR").innerHTML = "";

// ################### [ YEAR ]
if(curSel == "year") {
  try{	iClear("iMake");
		oDef = document.createElement("option");
		oDef.appendChild(document.createTextNode("Select Make"));
		$("iMake").appendChild(oDef);
	iClear("iModel");
		oDef = document.createElement("option");
		oDef.appendChild(document.createTextNode("Select Model"));
		$("iModel").appendChild(oDef);
 
	// populate second dropdown - MAKE
	for(i = 0; i < cars.all.length; i++)
	{
			try{
 		var iCurMake;
		if(cars.all[i].year == sYear && cars.all[i].make != iCurMake)
		{
			iCurMake = cars.all[i].make;
			oYEAR = document.createElement("option");
			oYEAR.setAttribute("value", cars.all[i].make);
			oYEAR.appendChild(document.createTextNode(cars.all[i].make));
 
		$("iMake").appendChild(oYEAR);
 
		}
 
			} catch(d) { alert("maPop: " + d); break;}
	}
 
	$("iMake").onchange = maCheck;
	$("iMake").disabled = false;

   } catch(e) { alert(e); }
 }
		return;
	}

}

function GetXmlHttpObject()
{
	if(window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}

	return null;
}

function defIMG()
{
	this.src = "/files/1886303/uploaded/na.jpg";
	this.width = 133;
	this.height = 100;
}

function iClear(iWho)
{
	for(i = 0; i < $(iWho).length; i++)
	{
		$(iWho).remove(i);
	}
 
	if($(iWho).length > 0)
	{
		iClear(iWho);
	}
}

function iCheckout()
{

try {
	iContentHTML = "<table class=\"text\"><td></td><td width=\"120\" align=\"center\" valign=\"middle\"><p style=\"color: #c00\; font-weight:bold\;\">SKU </p></td><td width=\"120\" align=\"center\" valign=\"middle\"><p style=\"color: #c00\; font-weight:bold\;\">Price </p></td><td width=\"120\" align=\"center\" valign=\"middle\"><p style=\"color: #c00\; font-weight:bold\;\"> Part Type </p></td>";

	for(i = 0; i < cars.all.length; i++)
	{
		if(cars.all[i].year == sYear && cars.all[i].make == $("iMake").value && cars.all[i].model == $("iModel").value && cars.all[i].engine == $("iEngine").value)
		{
			curID = cars.all[i].id;

			iSKU = cars.all[i].sku;
			iPRICE = cars.all[i].price;
			iTYPE = cars.all[i].type;
			xID = cars.all[i].xID;
			iInfo = cars.all[i].info;

			replace404i("/files/1886303/uploaded/" + cars.all[i].sku + ".jpg");

iContentHTML += "<tr><td align=\"left\" height=\"100\" valign=\"middle\" width=\"133\">";
iContentHTML += "<div onmouseover=\"javascript: iHoverDesc(\'iDIVinfo" + i + "\', 1)\;\" onmouseout=\"javascript: iHoverDesc(\'iDIVinfo" + i + "\', 0)\;\" style=\"text-align:center\;display:block\; width: 133px\; margin:0\;cursor:default\;\">";

iContentHTML += "<img src=\"" + iTetValue;
iContentHTML += "\" width=\"133\" height=\"100\" border=\"1\" class=\"iIMG\"/>";

iContentHTML += "<div style=\"position:relative\;top:-100px\;left:0\;\"><div style=\"position: absolute\;top:0\; left:100px\;width:100px\;height:100px\;\"><div style=\"position: absolute\; top: -25px\; left: 55px\; _left: -10px\; display:none\;z-index:50\;\" id=\"iDIVinfo" + i + "Shell\">";
iContentHTML += "<img src=\"/files/1886303/uploaded/arr.png\" style=\"position: absolute\; top:62px\;left:-43px\;\"/>";
iContentHTML += "<div style=\"margin: 0\;width: 457px\; height:21px\; background: url(/files/1886303/uploaded/div-box-top.png) no-repeat\;\"></div> ";
iContentHTML += "<div style=\"margin: 0\;width: 457px\; background: url(/files/1886303/uploaded/div-box-body.png) repeat-y\; padding:20px\;\">";
iContentHTML += "<div id=\"iDIVinfo" + i + "CONT\" style=\"text-align:left\;width:420px\;margin:0\;margin-top: -40px\; _margin-top: -20px\;margin-left:-2px\;\">";

iContentHTML += "<h3 id=\"iDIVinfoTitle\" style=\"width:300px\;\">Product Number: " + iSKU + " <br/>Type:  " + iTYPE + "</h3>";
iContentHTML += "<div style=\"text-align: center\;float:right\;margin-left: 15px\;\"><a href=\"" + iTetValue + "\" target=\"_blank\"><img src=\"" + iTetValue + "\" width=\"100\"/><br/><span style=\"font-size: 11px\;\">Click image to enlarge</span></a></div>";
iContentHTML += "<p id=\"iDIVinfoBody\">" + iInfo + "</p>";

iContentHTML += "<div style=\"clear:both\;\"></div></div>";
iContentHTML += "</div>";
iContentHTML += "<div style=\"margin: 0\;width: 457px\;height:21px\;background: url(/files/1886303/uploaded/div-box-bottom.png) no-repeat\;\"></div>";
iContentHTML += "</div></div></div></div>";


iContentHTML += "<a onmouseover=\"javascript: iHoverDesc(\'iDIVinfo" + i + "\', 1)\;\" onmouseout=\"javascript: iHoverDesc(\'iDIVinfo" + i + "\', 0)\;\" style=\"text-align:center\;display:block\; width: 133px\; height:20px\;position: relative\; top: 0\; left: 0\;margin:0\;cursor:default\;\">view more info";

iContentHTML += "<div style=\"position: absolute\; top: -80px\; left: 150px\;display:none\;z-index:50\;\" id=\"iDIVinfo" + i + "Shell\">";
iContentHTML += "<img src=\"/files/1886303/uploaded/arr.png\" style=\"position: absolute\; top:62px\;left:-43px\;\"/>";
iContentHTML += "<div style=\"margin: 0\;width: 457px\; height:21px\; background: url(/files/1886303/uploaded/div-box-top.png) no-repeat\;\"></div> ";
iContentHTML += "<div style=\"margin: 0\;width: 457px\; background: url(/files/1886303/uploaded/div-box-body.png) repeat-y\; padding:20px\;\">";
iContentHTML += "<div id=\"iDIVinfo" + i + "CONT\" style=\"text-align:left\;width:420px\;margin:0\;margin-top: -40px\; _margin-top: -20px\;margin-left:-2px\;\">";

iContentHTML += "<h3 id=\"iDIVinfoTitle\">" + iSKU + " - " + iTYPE + "</h3>";
iContentHTML += "<p id=\"iDIVinfoBody\">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum id placerat enim. Praesent nisl eros, pellentesque in pulvinar vel, porttitor quis justo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse potenti. Maecenas rhoncus vehicula dapibus. <br/><span style=\"color:#c00\;font-weight:bold\;\">" + iSKU + "</span></p>";

iContentHTML += "</div>";
iContentHTML += "</div>";
iContentHTML += "<div style=\"margin: 0\;width: 457px\;height:21px\;background: url(/files/1886303/uploaded/div-box-bottom.png) no-repeat\;\"></div>";
iContentHTML += "</div></a>";


	iContentHTML += "</td><td align=\"center\" valign=\"middle\"><p>" + iSKU;
	iContentHTML += "</p></td><td align=\"center\" valign=\"middle\"><p>$" + iPRICE;
	iContentHTML += "</p></td><td align=\"center\" valign=\"middle\"><p>" + iTYPE;
	iContentHTML += "</p></td><td align=\"center\" valign=\"middle\" width=\"100\"><a href=\"/store/1886303/product/" + iSKU + "?addToCart_" + xID + "=addToCart_" + xID + "\" target=\"iBlock\">Add to cart now!</a>";
	iContentHTML += "</td></tr><tr><td colspan=\"5\" valign=\"middle\" height=\"10\"><hr/></td></tr>";
		}
	}

	iContentHTML += "</table>";
	$("iResults").innerHTML = iContentHTML;
	setTimeout("testIMG404()",1000);
    } catch(e) { alert(e); }
}

function moCheck()
{
 try{	iClear("iEngine");
		oDef = document.createElement("option");
		oDef.appendChild(document.createTextNode("Select Engine"));
		$("iEngine").appendChild(oDef);
 
	// populate second dropdown - MAKE
	for(i = 0; i < cars.all.length; i++)
	{
			try{
 		var iCurEngine;
		if(cars.all[i].year == sYear && cars.all[i].make == $("iMake").value && cars.all[i].model == $("iModel").value && cars.all[i].engine != iCurEngine)
		{
			iCurEngine = cars.all[i].engine;
			oYEAR = document.createElement("option");
			oYEAR.setAttribute("value", cars.all[i].engine);
			oYEAR.appendChild(document.createTextNode(cars.all[i].engine));
 
		$("iEngine").appendChild(oYEAR);
 
		}
 
			} catch(d) { alert("moPop: " + d); break;}
	}
 
	$("iEngine").onchange = iCheckout;
	$("iEngine").disabled = false;

   } catch(e) { alert(e); }
}

function maCheck()
{
 try{	iClear("iModel");
		oDef = document.createElement("option");
		oDef.appendChild(document.createTextNode("Select Model"));
		$("iModel").appendChild(oDef);
	iClear("iEngine");
		oDef = document.createElement("option");
		oDef.appendChild(document.createTextNode("Select Engine"));
		$("iEngine").appendChild(oDef);
 
	// populate second dropdown - MAKE
	for(i = 0; i < cars.all.length; i++)
	{
			try{
 		var iCurModel;
		if(cars.all[i].year == sYear && cars.all[i].make == $("iMake").value && cars.all[i].model != iCurModel)
		{
			iCurModel = cars.all[i].model;
			oYEAR = document.createElement("option");
			oYEAR.setAttribute("value", cars.all[i].model);
			oYEAR.appendChild(document.createTextNode(cars.all[i].model));
 
		$("iModel").appendChild(oYEAR);
 
		}
 
			} catch(d) { alert("moPop: " + d); break;}
	}
 
	$("iModel").onchange = moCheck;
	$("iModel").disabled = false;

   } catch(e) { alert(e); }
}

function yCheck()
{
	sYSel = $("iYear");
	sYear = sYSel.options[sYSel.selectedIndex].text;
	curSel = "year";
	$("iMake").disabled = true;
	$("iModel").disabled = true;
	$("iEngine").disabled = true;
	if($("iYear").value == curYear && cars.all.length > 0) { stateChanged(); }
	if(sYear == "Select Year") { return; }
	iLoad($("iYear").value);
	curYear = $("iYear").value;
	$("iResults").innerHTML = "";
}


function replace404i(iWho)
{
	iTetValue = iWho;
	xmlhttp = GetXmlHttpObject();

	if(xmlhttp==null)
	{
		alert ("An error has occurred. Please try again later.");
		return;
	}

	var url = iWho;
	xmlhttp.onreadystatechange = stateChanged2i;
	xmlhttp.open("get",url,false);
	xmlhttp.send();
}

function stateChanged2i()
{
	if(xmlhttp.readyState == 4)
	{

		if(xmlhttp.status == 200)
		{
			return;
		}
		else
		{
			iTetValue =  "/files/1886303/uploaded/na.jpg";
		}
	}
}
function iHoverDesc(iWho, ON)
{
	iWhoRef = iWho + "CONT";
	iWhoShell = iWho + "Shell";

	if(ON)
	{
		$(iWhoShell).style.display = "block";
		$(iWhoRef).innerHTML = $("testContent").innerHTML;
	}
	else
	{
		$(iWhoShell).style.display = "none";
	}
}


function iHoverImg(iWho, ON)
{
	iWhoRef = iWho + "DIV";
	iWhoI = iWho + "IMG";

	if(ON)
	{
		$(iWhoI).src = $(iWho).name;
		$(iWhoRef).style.display = "block";
	}
	else
	{
		$(iWhoRef).style.display = "none";
	}
}
