function changeImage(type,edp)
{
	if (edp !== '')
	{
		var edp = '000000' + edp
		edp = edp.substring(edp.length-7);
		var theimage = document.getElementById("prodOpt" + type);
		var xmlhttp = new GetXmlHttpObject();
		xmlhttp.open('get', '/images/products/P' + edp + 'B.jpg', true);
		xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.status==200){
				if (theimage) theimage.src = "/images/products/P" + edp + ".jpg";
			} else if (xmlhttp.status==404){
				if (theimage) theimage.src = "/images/buggies/noimage.jpg";
			} else alert("Status is "+xmlhttp.status)
			}
		}
		xmlhttp.send(null);
		theimage.style.display = 'inline';
		document.getElementById('prodImg').style.display = 'none';
	} else {
		document.getElementById("prodOpt" + type).style.display = 'none';
		if (document.getElementById("prodOpt1").style.display == 'none' && document.getElementById("prodOpt2").style.display == 'none' && document.getElementById("prodOpt3").style.display == 'none')
		{
			document.getElementById('prodImg').style.display = 'inline';
		}
	}
}
function popWheels(edp)
{
	priceUpdate(edp,'1','liftkit');
	removeAllOptions(document.getElementById('wheels'));
	addOption(document.getElementById('wheels'),'Choose Wheels', '');
	removeAllOptions(document.getElementById('tires'));
	addOption(document.getElementById('tires'),'Choose Tires', '');
	if (edp !== '') {
		var xmlhttp = new GetXmlHttpObject();
		var url = "/ajax/popWheels.asp?edp=" + edp;
		var optionArray = "";
		xmlhttp.open('GET',url,true);
		xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				optionArray = eval(xmlhttp.responseText);
				document.getElementById('wheels').disabled = false;
				for (var i=0; i<optionArray.length; i++) {
					addOption(document.getElementById('wheels'), optionArray[i][1], optionArray[i][0]);
				}
			}
		}
		xmlhttp.send(null);
	} else {
		resetImages('all');
		document.getElementById('wheels').disabled = true;
		document.getElementById('tires').disabled = true;
	}
}
function popTires(edp)
{
	priceUpdate(edp,'4','wheels');
	removeAllOptions(document.getElementById('tires'));
	addOption(document.getElementById('tires'),'Choose Tires', '');
	if (edp !== '') {
		xmlhttp = new GetXmlHttpObject();
		var url = "/ajax/popTires.asp?edp=" + edp;
		var optionArray = "";
		xmlhttp.open('GET',url,true);
		xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				optionArray = eval(xmlhttp.responseText);
				document.getElementById('tires').disabled = false;
				for (var i=0; i<optionArray.length; i++) {
					addOption(document.getElementById('tires'), optionArray[i][1], optionArray[i][0]);
				}
			}
		}
		xmlhttp.send(null);
	} else {
		resetImages('tires');
		resetImages('wheels');
		document.getElementById('tires').disabled = true;
	}
}
function addOption(OptionList,OptionText,OptionValue)
{
   OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}
function removeAllOptions(OptionList)
{
   for (x = OptionList.length; x >= 0; x = x - 1) {
      OptionList[x] = null;
   }
}
function priceUpdate(sku,num)
{
	if (sku !== '') {
		var xmlhttp = new GetXmlHttpObject();
		var url = "/ajax/lookupPrice.asp?sku=" + sku;
		var optionArray = "";
		xmlhttp.open('GET',url,true);
		xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				optionArray = eval(xmlhttp.responseText);
				var price = optionArray[0];
				var edp = optionArray[1];
				document.getElementById('kitprice_' + num).value = parseFloat(price);
				priceDisplay();
				changeImage(num,edp);
			}
		}
		xmlhttp.send(null);
	} else {
		document.getElementById('kitprice_' + num).value = 0;
		priceDisplay();
		changeImage(num,'');
	}
}
function priceDisplay()
{
	if (document.getElementById('totalprice'))
	{
		var compnum = document.getElementById('kitcomps').value;
		var price = 0;
		var totalprice = 0;
		for (var i=1; i<=compnum; i++) {
			price = document.getElementById('kitprice_' + i).value;
			totalprice = roundNumber(parseFloat(totalprice) + parseFloat(price),2);
		}
		document.getElementById('totalprice').innerHTML = "$" + totalprice;
	}
}
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function resetImages(which) {
	if (which == 'all' || which == 'liftkit')
	{
		document.getElementById("liftkit_img").src = "/images/kbbg_wh.jpg";
	}
	if (which == 'all' || which == 'wheels')
	{
		document.getElementById("wheels_img").src = "/images/kbbg_wh.jpg";
	}
	if (which == 'all' || which == 'tires')
	{
		document.getElementById("tires_img").src = "/images/kbbg_ti.jpg";
	}
}
function valProd()
{
	var compnum = document.getElementById('kitcomps').value;
	for (var i=1; i<=compnum; i++) {
		if (isEmpty(document.getElementById('kitcomp' + i).value)) {
			alert("Please choose all options.");
			return false;
		}
	}
	return true;
}
function valProdEnclosure()
{
	var compnum = document.getElementById('kitcomps').value;
	for (var i=1; i<=compnum; i++) {
		if (document.getElementById('kitcomp' + i).value == "" && i != 3) {
			alert("Please choose all options.");
			return false;
		}
	}
	return true;
}
function priceUpdateEnclosure(sku,num)
{
	if (sku !== '') {
		var xmlhttp = new GetXmlHttpObject();
		var url = "/ajax/lookupPrice.asp?sku=" + sku;
		var optionArray = "";
		xmlhttp.open('GET',url,true);
		xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				optionArray = eval(xmlhttp.responseText);
				var price = optionArray[0];
				var edp = optionArray[1];
				document.getElementById('kitprice_' + num).value = parseFloat(price);
				priceDisplay();
			}
		}
		xmlhttp.send(null);
	} else {
		document.getElementById('kitprice_' + num).value = 0;
		priceDisplay();
	}
}

// PRODUCT REVIEW
function checkLogin(frm)
{
	if (isEmpty(frm.EmailID.value))
	{
		alert("Please enter your email address.")
		frm.EmailID.focus()
		return false
	} else
	{	if (isEmail(frm.EmailID.value)  != true)
		{
			alert("Please enter a valid email address.");
			frm.EmailID.focus()
			return false;
		}
	}
	if (isEmpty(frm.PassWord.value))
	{
		alert("Please enter your password.")
		frm.PassWord.focus()
		return false
	}
	return true
}

function updateValue(rating, id)
{
	document.getElementById("rating").value  = rating;
	//document.frmProductReview.rating.value  = rating;

	var thetag = document.getElementById(id);
	var newOutput = '<ul class="unit-rating">';
	newOutput = newOutput + '<li class="current-rating" style="width:' + (rating * 12) + 'px;"></li>';

	newOutput = newOutput + '<li><a href="javascript:updateValue(\'1\',\'star_rating\');"  onmouseover="javascript:updateTxt(\'1\');" onmouseout="resetTxt()" class="r1-unit">1</a></li>';
	newOutput = newOutput + '<li><a href="javascript:updateValue(\'2\',\'star_rating\');"  onmouseover="javascript:updateTxt(\'2\');" onmouseout="resetTxt()" class="r2-unit">2</a></li>';
	newOutput = newOutput + '<li><a href="javascript:updateValue(\'3\',\'star_rating\');"  onmouseover="javascript:updateTxt(\'3\');" onmouseout="resetTxt()" class="r3-unit">3</a></li>';
	newOutput = newOutput + '<li><a href="javascript:updateValue(\'4\',\'star_rating\');"  onmouseover="javascript:updateTxt(\'4\');" onmouseout="resetTxt()" class="r4-unit">4</a></li>';
	newOutput = newOutput + '<li><a href="javascript:updateValue(\'5\',\'star_rating\');"  onmouseover="javascript:updateTxt(\'5\');" onmouseout="resetTxt()" class="r5-unit">5</a></li>';
	newOutput = newOutput + '</ul>';
	thetag.innerHTML = newOutput;
	
}

function updateTxt(rating)
{
	/*
	switch(rating)
	{
	case '1':
		document.getElementById("RatingText").innerHTML = 'I didn\'t like it. (Rating: 1)';
	  break    
	case '2':
		document.getElementById("RatingText").innerHTML = 'I liked it a little. (Rating: 2)';
	  break
	case '3':
		document.getElementById("RatingText").innerHTML = 'It was ok. (Rating: 3)';
	  break
	case '4':
		document.getElementById("RatingText").innerHTML = 'I liked it. (Rating: 4)';
	  break
	case '5':
		document.getElementById("RatingText").innerHTML = 'I liked it a lot. (Rating: 5)';
	  break
	default:
		document.getElementById("RatingText").innerHTML = '&nbsp;';
	}
	*/
}

function resetTxt()
{
	/*
	switch(document.getElementById("rating").value)
	{
	case '1':
		document.getElementById("RatingText").innerHTML = 'I didn\'t like it. (Rating: 1)';
	  break    
	case '2':
		document.getElementById("RatingText").innerHTML = 'I liked it a little. (Rating: 2)';
	  break
	case '3':
		document.getElementById("RatingText").innerHTML = 'It was ok. (Rating: 3)';
	  break
		document.getElementById("RatingText").innerHTML = 'I liked it. (Rating: 4)';
	  break
	case '5':
		document.getElementById("RatingText").innerHTML = 'I liked it a lot. (Rating: 5)';
	  break
	default:
		document.getElementById("RatingText").innerHTML = '&nbsp;';
	}
	*/
}

function chkProductReview(f) {
	var xstr, msg = [];
	
	xstr = document.getElementById("rating").value;
	if (xstr == "0" || isEmpty(xstr)) {
		msg.push("Please select an overall rating.");
	}
	
	xstr = document.getElementById("author").value;
	if (isEmpty(xstr)) {
		//document.getElementById("author").focus();
		msg.push("You must provide your name! (or nickname).");
	}
	
	xstr = document.getElementById("title").value;
	if (isEmpty(xstr)) {
		//document.getElementById("title").focus();
		msg.push("You must give your review a title.");
	}
		
	xstr = document.getElementById("review").value;
	if (isEmpty(xstr)) {
		//document.getElementById("review").focus();
		msg.push("You must provide a review of the product.");
	}
	
	xstr = document.getElementById("ccode").value;
	if (isEmpty(xstr)) {
		//document.getElementById("ccode").focus();
		msg.push("You must provide the captcha code.");
	}
	
	if (!document.getElementById("chkAccept").checked) {
		msg.push("Please verify that you have read and accept our product review guidelines.");
	}
	
	if (msg.length > 0) {
		alert(msg.join("\n"));
		return false;
	} else return true;
}
