

function reset()
{
	document.form1.make.selectedIndex = 0;
	document.form1.model.style.visibility = "hidden";
	document.form1.year.style.visibility = "hidden";
	document.form1.engine.style.visibility = "hidden";
	document.form1.transmission.style.visibility = "hidden";
	document.getElementById('pricebutton').style.visibility = "hidden";
	document.getElementById('priceresults').innerHTML = '';

	document.form1.model.style.display = "none";
	document.form1.year.style.display = "none";
	document.form1.engine.style.display = "none";
}

function fillmodel(origin, target)
{
	document.getElementById('pricebutton').style.visibility = "hidden";
	target.form['year'].style.visibility = "hidden";
	target.form['engine'].style.visibility = "hidden";
	target.form['transmission'].style.visibility = "hidden";
	
	target.form['year'].style.display = "none";
	target.form['engine'].style.display = "none";
	target.form['transmission'].style.display = "none";

	var ovalue = origin.options[origin.selectedIndex].value;
	var url = 'inc/get-cars.php';
	var params = 'make=' + ovalue
				+ '&f=' + target.form.name
				+ '&e=' + target.name;
	var loader = new net.ContentLoader(url, fillcontent, null, "POST", params);
}

function fillyear(make, model, target)
{
	if (isNaN(parseInt(model.options[model.selectedIndex].value)))
	{
		// alert(model.options[model.selectedIndex].value);
		fillmodel(make, model);
		return;
	}

	document.getElementById('pricebutton').style.visibility = "hidden";
	target.form['engine'].style.visibility = "hidden";
	target.form['transmission'].style.visibility = "hidden";		

	target.form['engine'].style.display = "none";
	target.form['transmission'].style.display = "none";

	make = make.options[make.selectedIndex].value;
	model = model.options[model.selectedIndex].value;
	var url = 'inc/get-cars.php';
	var params = 'make=' + make
				+ '&model=' + model
				+ '&f=' + target.form.name
				+ '&e=' + target.name;			

	var loader = new net.ContentLoader(url, fillcontent, null, "POST", params);
}

function fillengine(make, model, year, target)
{
	if (isNaN(parseInt(year.options[year.selectedIndex].value)))
	{
		fillyear(make, model, year);
		return;
	}

	document.getElementById('pricebutton').style.visibility = "hidden";
	target.form['transmission'].style.visibility = "hidden";

	target.form['transmission'].style.display = "none";

	make = make.options[make.selectedIndex].value;
	model = model.options[model.selectedIndex].value;
	year = year.options[year.selectedIndex].value;
	var url = 'inc/get-cars.php';
	var params = 'make=' + make
				+ '&model=' + model
				+ '&year=' + year
				+ '&f=' + target.form.name
				+ '&e=' + target.name;			
	
	var loader = new net.ContentLoader(url, fillcontent, null, "POST", params);
}

function filltransmission(myForm)
{	
	document.getElementById('pricebutton').style.visibility = "hidden";

	var engine = myForm['engine'];
	if ((engine.options[engine.selectedIndex].value) == "Select Engine")
	{
		fillengine(myForm['make'], myForm['model'], myForm['year'], engine);
		return;
	}

	myForm.transmission.selectedIndex = 0;
	myForm['transmission'].style.visibility = 'visible';
	myForm['transmission'].style.display = 'block';
}

function getprice()
{
	document.getElementById("pricebutton").style.visibility = 'visible';
}

function fillcontent()
{
	
	// alert(this.req.responseText);
	
	var xmlDoc = this.req.responseXML.documentElement;
	
	var xSel = xmlDoc.getElementsByTagName('selectElement')[0];
	var strFormName = xSel.childNodes[0].firstChild.nodeValue;
	var strElementName = xSel.childNodes[1].firstChild.nodeValue;
	
	var target = document.forms[strFormName].elements[strElementName];
	target.options.length = 0;
	
	var xRows = xmlDoc.getElementsByTagName('entry');
	
	if (xRows.length < 2)
	{
		alert('No data found. Please try with different vehicle specifications.');
		return;
	}
	
	for (i = 0; i < xRows.length; i++)
	{
		var theText = xRows[i].childNodes[0].firstChild.nodeValue;
		var theValue = xRows[i].childNodes[1].firstChild.nodeValue;
		// alert(theValue);
		
		var option = new Option(theText, theValue);
		try
		{
			target.add(option, null);
		}
		catch (e)
		{
			target.add(option, -1);
		}
	}
	
	target.style.visibility = 'visible';
	target.style.display = 'block';
	/*
	document.getElementById(strElementName).style.background = '#ffcc33';
	target.style.visibility = 'visible';
	*/
}


/** Get Results **/
function getresults(make, model, year, engine)
{
	var url = 'inc/get-cars.php';
	var params = 
				'make=' + make
				+ '&model=' + model
				+ '&year=' + year
				+ '&engine=' + engine
				+ '&price=' + 1;
	// alert(params);
	// make=1&model=305&year=1997&engine=2.2&price=1
	var loader = new net.ContentLoader(url, displayresults, null, "POST", params);
}

function displayresults()
{
	var xmlDoc = this.req.responseXML.documentElement;

	var remoteAddress = xmlDoc.getElementsByTagName('remoteAddress')[0].firstChild.nodeValue;
	// 65.243.177.226, 10.2.2.93
	var showCuc = (remoteAddress == '65.243.177.226' || remoteAddress == '10.2.2.93') ? true : false;
	
	var modelid = xmlDoc.getElementsByTagName('modelid')[0].firstChild.nodeValue;
	var make = xmlDoc.getElementsByTagName('make')[0].firstChild.nodeValue;
	var model = xmlDoc.getElementsByTagName('model')[0].firstChild.nodeValue;
	var year = xmlDoc.getElementsByTagName('year')[0].firstChild.nodeValue;
	var engine = xmlDoc.getElementsByTagName('engine')[0].firstChild.nodeValue;
	var modelid = xmlDoc.getElementsByTagName('modelid')[0].firstChild.nodeValue;
	var appid = xmlDoc.getElementsByTagName('appid')[0].firstChild.nodeValue;
	var carid = xmlDoc.getElementsByTagName('carid')[0].firstChild.nodeValue;
	
	var vid = xmlDoc.getElementsByTagName('vid')[0].firstChild.nodeValue;
	var phone = xmlDoc.getElementsByTagName('phone')[0].firstChild.nodeValue;
	
	// create call in message - should show up regardless of whether products exists or don't exist for the selected application
	var myhtml = '<table align="center" cellspacing="10"><tr><td colspan="2"><a href="#startmake" onclick="reset();window.scroll();return false;" class="buttonlink" style="font-style: italic; font-size: 15px;">Change Vehicle Specifications</a></td></tr><tr><td align="center"><a href="javascript:webVoicePop(\'Template=57281\');"><img src="http://as00.estara.com/webcare/public/linkimage.php?ulbid=57281" border="0" alt="Click Here to Verify Part Before you Buy" align="left"></a></td><td class="brightcellcontent">The ' + make + ' ' + model + ' can be a <b>tricky vehicle to fit</b>. We\'ve noticed that sometimes even <b>local mechanics</b> can diagnose this one wrong.  <b>You shouldn\'t buy this part without first speaking to a certified radiator technician</b>.  With a simple call our tech you can verify your engine and a few other options in the car.  Go ahead and call <b> ' + phone + ' (toll free)</b>, or <a class="bottomlink" href="javascript:webVoicePop(\'Template=57281\');">click here</a> to be instantly connected for free! (Make sure your pop up blocker is off)</td></tr></table>';

	// open results table
	myhtml += '<table align="center"><tr><td>';

	var products = xmlDoc.getElementsByTagName('entry');
	// if no products returned
	if (products.length == 0)
	{
		myhtml += '<table align="center" class="radoption" style="font-size: 15px; font-style: italic" width="95%"><tr><td>Sorry, there were no parts found.<br>Please <a href="/contact.php" class="bottomlink" title="Email Technicians"><b>Email Our Radiator Technicians</b></a> for a Customized Price Quote.</td></tr></table>';
	}
	// when products exist
	else
	{

		for (i = 0; i < products.length; i++)
		{

			var cuc = '';
			var core = '';
			var flow = '';
			var cap = '';
			var eoc = '';
			var tophose = '';
			var bothose = '';
			var price = '';
			var warrantyprice = '';
			var discountprice = '';
			var finalprice = '';
			
			try{cuc = products[i].getElementsByTagName('cuc')[0].firstChild.nodeValue;}catch(e){}
			try{core = products[i].getElementsByTagName('core')[0].firstChild.nodeValue;}catch(e){}
			try{flow = products[i].getElementsByTagName('flow')[0].firstChild.nodeValue;}catch(e){}
			try{cap = products[i].getElementsByTagName('cap')[0].firstChild.nodeValue;}catch(e){}
			try{eoc = products[i].getElementsByTagName('eoc')[0].firstChild.nodeValue;}catch(e){}
			try{tophose = products[i].getElementsByTagName('tophose')[0].firstChild.nodeValue;}catch(e){}
			try{bothose = products[i].getElementsByTagName('bothose')[0].firstChild.nodeValue;}catch(e){}

			price = products[i].getElementsByTagName('price')[0].firstChild.nodeValue;
			warrantyprice = products[i].getElementsByTagName('warrantyprice')[0].firstChild.nodeValue;
			discountprice = products[i].getElementsByTagName('discount')[0].firstChild.nodeValue;
			finalprice = products[i].getElementsByTagName('finalprice')[0].firstChild.nodeValue;
			finalformattedprice = products[i].getElementsByTagName('finalformattedprice')[0].firstChild.nodeValue;
			
			// alert(finalprice + ' ' + discountprice);
			
			// open form
			myhtml += '<form action="https://www.outlawradiator.com/process/cart.php" method="post">';
			
			// open product table
			myhtml += '<table style="width: 600px;" align="center" class="radoption">';

			// heading contains radiator count
			myhtml += '<tr><td colspan="2" style="background: #000000;font-weight: bold;font-size: 15px;">Radiator Option #' + (i + 1) + (showCuc ? ' CUC: ' + cuc : ' ' ) + '</td></tr>';
			
			// first row contains two columns
			myhtml += '<tr>';

			// left column
			myhtml += '<td><table class="radspec">';

			if (core != '')
			{
				myhtml += '<tr><td nowrap align="right"><b>Core Size:</b></td><td nowrap>&nbsp; ' + core + '</td></tr>';
			}
			if (flow != '')
			{
				myhtml += '<tr><td nowrap align="right"><b>Radiator Flow:</b></td><td nowrap>&nbsp; ' + flow + '</td></tr>';
			}
			if (cap != '')
			{
				myhtml += '<tr><td nowrap align="right"><b>Cap Position:</b></td><td nowrap>&nbsp; ' + cap + '</td></tr>';
			}
			if (eoc != '')
			{
				myhtml += '<tr><td nowrap align="right"><b>Engine Oil Cooler:</b></td><td nowrap>&nbsp; ' + eoc + '</td></tr>';
			}
			if (tophose != '')
			{
				myhtml += '<tr><td nowrap align="right"><b>Top Hose Location:</b></td><td nowrap>&nbsp; ' + tophose + '</td></tr>';
			}
			if (bothose != '')
			{
				myhtml += '<tr><td nowrap align="right"><b>Bottom Hose Location:</b></td><td nowrap>&nbsp; ' + bothose + '</td></tr>';
			}

			// end left column
			myhtml += '</table></td>';

			// right column
			myhtml += '<td><table class="radspec">';

			if (price != '')
			{
				myhtml += '<tr style="background: #702717;"><td nowrap align="right"><span class="boldprice">' + price + '</span></td><td>' + year + ' ' + make + ' ' + model + ' Radiator</td></tr>';
			}
			if (warrantyprice != '')
			{
				myhtml += '<tr><td nowrap align="right"><span class="boldprice">' + warrantyprice + '</span></td><td><b>FREE</b> Lifetime Warranty</td></tr>';
			}
			if (discountprice != '')
			{
				myhtml += '<tr style="background: #702717;"><td nowrap align="right"><span class="boldprice">' + discountprice + '</span></td><td><b>DISCOUNT</b> for Ordering Today</td></tr>';
			}

			// end right column
			myhtml += '</table></td>';

			// end first row
			myhtml += '</tr>';
			
			// second row contains warranty info
			myhtml += '<tr><td colspan="2">';

			myhtml += '<table class="radspec" width="100%">';
			myhtml += '<tr><td colspan="2" align="center" style="font-size: 20px;"><span class="largeprice">' + finalformattedprice + '</span> Is Your Final Cost!</td></tr>';
			myhtml += '<tr><td colspan="2" align="center">Taxes And Shipping Cost Will Be Added To The Price Above</td></tr>';
			myhtml += '<tr><td align="center"><b>LOW PRICE GUARANTEE:</b> If you see a lower price on any other web site,<br><a class="bottomlink" href="javascript:webVoicePop(\'Template=57281\');">click here</a> to give us a call and we\'ll beat the competitor by 5%!</td>';
			myhtml += '<td align="right"><input type="submit" value="Submit" /></td></tr>';
			myhtml += '</table>';

			// end first row
			myhtml += '</td></tr>';

			// close product table
			myhtml += '</table>';

			myhtml += '<input type="hidden" name="rows" value="" />';
			myhtml += '<input type="hidden" name="modelid" value="' + modelid + '" />';
			myhtml += '<input type="hidden" name="appid" value="' + appid + '" />';
			myhtml += '<input type="hidden" name="carid" value="' + carid + '" />';
			myhtml += '<input type="hidden" name="make" value="' + make + '" />';
			myhtml += '<input type="hidden" name="model" value="' + model + '" />';
			myhtml += '<input type="hidden" name="year" value="' + year + '" />';
			myhtml += '<input type="hidden" name="liters" value="' + engine + '" />';
			myhtml += '<input type="hidden" name="ac" value="" />';
			myhtml += '<input type="hidden" name="cuc" value="' + cuc + '" />';
			myhtml += '<input type="hidden" name="salesprice" value="' + finalprice + '" />';
			myhtml += '<input type="hidden" name="cubinch" value="" />';
			myhtml += '<input type="hidden" name="engine" value="' + engine + '" />';
			myhtml += '<input type="hidden" name="qty" value="1" />';
			myhtml += '<input type="hidden" name="addtocart" value="1" />';
			myhtml += '<input type="hidden" name="vid" value="' + vid + '" />';
			myhtml += '<input type="hidden" name="action" value="add" />';

			// close form
			myhtml += '</form>';
			
		}
		// end foreach product
	}
	// end else (if products)

	// close results table
	myhtml += '</td></tr></table>';

	document.getElementById('priceresults').innerHTML = '';
	document.getElementById('priceresults').innerHTML = myhtml;

}
