currentDetailBubble = null;
function showDetailBubble(modelId, modelRangeId) {
	hideCurrentBubbles();
	showLayer("hideBubble");

	document.getElementById("detailBubble_" + modelRangeId + "_" + modelId).innerHTML = "<div class=\"loading\">Loading...</div>";
	httpUrl = "/products/functions/bubble-modelDetail.cfm?navId=" + getUrlVar("navid") + "&productLineId=" + getUrlVar("productlineid") + "&productGroupId=" + getUrlVar("productgroupid") + "&modelGroupId=" + getUrlVar("modelgroupid") + "&modelId=" + modelId + "&modelRangeId=" + modelRangeId;
	jsmx.http("GET", httpUrl, detailBubble_Callback);
	showLayer("detailBubble_" + modelRangeId + "_" + modelId);
	currentDetailBubble = "detailBubble_" + modelRangeId + "_" + modelId;
	fixHideBubble();
}

function detailBubble_Callback(obj) {
	document.getElementById("detailBubble_" + obj.modelrangeid + "_" + obj.modelid).innerHTML = obj.content;
	showTab("tabset_" + obj.modelid + "_", "tab1", "/products/functions/model-specifications.cfm?modelId=" + obj.modelid + "&modelRangeId=" + obj.modelrangeid);
  	swapImg("tabset_" + obj.modelid + "_firstLeft", left1On);
	swapImg("tabset_" + obj.modelid + "_right1", rightOn);
}

function hidecurrentDetailBubble() {
	if (currentDetailBubble != null) {
		hideLayer("hideBubble");
		hideLayer(currentDetailBubble);
		currentDetailBubble = null;
	}
}

function goDetail(productLineId, productGroupId, modelGroupId, modelRangeId, modelId) {
	document.location = "/products/index.cfm?navid=" + getUrlVar("navid") + "&theView=modelDetail&productLineId=" + productLineId + "&productGroupId=" + productGroupId + "&modelGroupId=" + modelGroupId + "&modelRangeId=" + modelRangeId + "&modelId=" + modelId;
}


currentOptionsBubble = null;
function showOptionsBubble(modelRangeId, modelId) {
	hideCurrentBubbles();
	showLayer("hideBubble");

	document.getElementById("optionsBubble_" + modelRangeId + "_" + modelId).innerHTML = "<div class=\"loading\">Loading...</div>";
	httpUrl = "/products/functions/bubble-modelOptions.cfm?modelId=" + modelId + "&productLineId=" + getUrlVar("productlineid") + "&productGroupId=" + getUrlVar("productgroupid") + "&modelGroupId=" + getUrlVar("modelgroupid") + "&modelRangeId=" + modelRangeId + "&selectedOptionId=" + document.getElementById("selectedOptionId_" + modelId).value + "&selectedWarrantyId=" + document.getElementById("selectedWarrantyId_" + modelId).value;
	jsmx.http("GET", httpUrl, optionsBubble_Callback);
	showLayer("optionsBubble_" + modelRangeId + "_" + modelId);
	currentOptionsBubble = "optionsBubble_" + modelRangeId + "_" + modelId;
	fixHideBubble();
}
function optionsBubble_Callback(obj) {
	document.getElementById("optionsBubble_" + obj.modelrangeid + "_" + obj.modelid).innerHTML = obj.content;
}

function updateBaseModelRow(baseModelId) {
	form = document.getElementById("selectedOptionId_" + baseModelId).form;

	hasWarranty = false;
	hasOption = false;
	if (form["option_" + baseModelId]) {
		radioObj = form["option_" + baseModelId];
		for (i = 0; i < radioObj.length; i++) {
			if (radioObj[i].checked && radioObj[i].value != baseModelId) {
				hasOption = true;
				form["selectedOptionId_" + baseModelId].value = radioObj[i].value;
				form["selectedOptionNumber_" + baseModelId].value = form.elements[radioObj[i].id + "_number"].value;
				form["selectedOptionName_" + baseModelId].value = form.elements[radioObj[i].id + "_name"].value;
				form["selectedOptionPrice_" + baseModelId].value = form.elements[radioObj[i].id + "_price"].value;
			}
		}
	}
	if (form["warranty_" + baseModelId]) {
		checkboxObj = form["warranty_" + baseModelId];
		if (checkboxObj.checked) {
			hasWarranty = true;
			form["selectedWarrantyId_" + baseModelId].value = checkboxObj.value;
			form["selectedWarrantyNumber_" + baseModelId].value = form.elements["warranty_" + baseModelId + "_number"].value;
			form["selectedWarrantyName_" + baseModelId].value = form.elements["warranty_" + baseModelId + "_name"].value;
			form["selectedWarrantyPrice_" + baseModelId].value = form.elements["warranty_" + baseModelId + "_price"].value;
		}
	}
	
	if (hasOption && hasWarranty) {
		document.getElementById("modelNumberLink_" + baseModelId).innerHTML = form["selectedOptionNumber_" + baseModelId].value;
		document.getElementById("optionsLink_" + baseModelId).innerHTML = form["selectedOptionName_" + baseModelId].value + "<br><br>" + form["selectedWarrantyName_" + baseModelId].value;
		document.getElementById("totalPrice_" + baseModelId).innerHTML = dollarFormat(parseFloat(form["selectedWarrantyPrice_" + baseModelId].value) + parseFloat(form["selectedOptionPrice_" + baseModelId].value));
	} else if (hasOption) {
		document.getElementById("modelNumberLink_" + baseModelId).innerHTML = form["selectedOptionNumber_" + baseModelId].value;
		document.getElementById("optionsLink_" + baseModelId).innerHTML = form["selectedOptionName_" + baseModelId].value;
		document.getElementById("totalPrice_" + baseModelId).innerHTML = dollarFormat(form["selectedOptionPrice_" + baseModelId].value);
		form["selectedWarrantyId_" + baseModelId].value = 0;
	} else if (hasWarranty) {
		document.getElementById("modelNumberLink_" + baseModelId).innerHTML = form["baseModelNumber_" + baseModelId].value;
		document.getElementById("optionsLink_" + baseModelId).innerHTML = form["selectedWarrantyName_" + baseModelId].value;
		document.getElementById("totalPrice_" + baseModelId).innerHTML = dollarFormat(parseFloat(form["selectedWarrantyPrice_" + baseModelId].value) + parseFloat(form["baseModelPrice_" + baseModelId].value));
		form["selectedOptionId_" + baseModelId].value = baseModelId;
	} else {
		document.getElementById("modelNumberLink_" + baseModelId).innerHTML = form["baseModelNumber_" + baseModelId].value;
		document.getElementById("optionsLink_" + baseModelId).innerHTML = form["baseModelName_" + baseModelId].value;
		document.getElementById("totalPrice_" + baseModelId).innerHTML = dollarFormat(form["baseModelPrice_" + baseModelId].value);
		form["selectedOptionId_" + baseModelId].value = baseModelId;
		form["selectedWarrantyId_" + baseModelId].value = 0;
	}

	hideCurrentBubbles();
}



function hidecurrentOptionsBubble() {
	if (currentOptionsBubble != null) {
		hideLayer("hideBubble");
		hideLayer(currentOptionsBubble);
		currentOptionsBubble = null;
	}
}

function hideCurrentBubbles() {
	fixHideBubble();
	hidecurrentOptionsBubble();
	hidecurrentDetailBubble();
	hidecurrentFavoritesBubble();
}

currentTab = "tab1";
currentTabSet = null;
left1On = preload("/common/images/tabs/left-1-on.gif");
left1Off = preload("/common/images/tabs/left-1-off.gif");
leftOn = preload("/common/images/tabs/left-on.gif");
leftOff = preload("/common/images/tabs/left-off.gif");
rightOn = preload("/common/images/tabs/right-on.gif");
rightOff = preload("/common/images/tabs/right-off.gif");
right1On = preload("/common/images/tabs/right-1-on.gif");
right1Off = preload("/common/images/tabs/right-1-off.gif");

function showTab(tabSet, tabId, contentUrl) {
	currentTabSet = tabSet;
	hideTab(currentTabSet, currentTab);

	currentTab = tabId;
	
	swapStyle(document.getElementById(currentTabSet + currentTab + "Cell"), "tabNameOn");

	jsmx.http("GET", contentUrl, showTab_callback);
}

function showTab_callback(obj) {
	saveOverflow = document.getElementById(currentTabSet + "tabContent").style.overflow;
	if (document.all) {
		document.getElementById(currentTabSet + "tabContent").innerHTML = "";
		document.getElementById(currentTabSet + "tabContent").style.overflow = "visible";
		document.getElementById(currentTabSet + "tabContent").style.height = document.getElementById(currentTabSet + "tabContent").offsetHeight;
	}
	document.getElementById(currentTabSet + "tabContent").innerHTML = obj;
	document.getElementById(currentTabSet + "tabContent").style.overflow = saveOverflow;
}


function hideTab(tabSet, tabId) {
	swapImg(tabSet + "firstLeft", left1Off);
	swapImg(tabSet + "right1", rightOff);
	swapImg(tabSet + "right2", rightOff);
	swapImg(tabSet + "right3", rightOff);
	swapImg(tabSet + "lastRight", right1Off);
	swapStyle(document.getElementById(tabSet + tabId + "Cell"), "tabNameOff");
}


function fixHideBubble() {
	if (document.body.scrollHeight > document.body.offsetHeight) {
		document.getElementById("hideBubble").style.height = document.body.scrollHeight;
	} else {
		document.getElementById("hideBubble").style.height = document.body.offsetHeight * .99;
	}
}
addOnLoad(fixHideBubble);


function warrantyPopup(modelId) {
	window.open("/products/warranty.cfm?modelId=" + modelId, "warrantyWindow", "width=770,height=500,scrollbars=1,resizable=1");
}


function showPerfData(modelId, figureField) {
	window.open("/products/perfData.cfm?modelId=" + modelId + "&figureField=" + figureField, "perfWindow", "width=400,height=300,resizable=1,scrollbars=1");
}


function printFriendly() {
	printWin = window.open("/products/print.cfm?" + location.href.split("?")[1], "printFriendly", "");
	printWin.focus();
}

function doFavorite(modelRangeId, baseModelId) {
	form = document.forms["modelsForm_" + modelRangeId];
	modelIds = new Array();
	
	// loop through all form fields
	for (i = 0; i < form.elements.length; i++) {
		fieldName = form.elements[i].name;
		
		// find a basemodel
		if (fieldName.indexOf("baseModelNumber_") != -1) {
			
			modelId = fieldName.split("_")[1];
			selectedOptionId = form.elements["selectedOptionId_" + modelId].value;
			selectedWarrantyId = form.elements["selectedWarrantyId_" + modelId].value;
			
	
			// check if we should even bother with this baseModel
			isOrderable = form.elements["baseModelOrderable_" + modelId].value;
			if (isOrderable == 1) {
				if (selectedOptionId != modelId) {
					modelIds[modelIds.length] = selectedOptionId;
					if (selectedWarrantyId != 0 && selectedWarrantyId != modelId) {
						modelIds[modelIds.length] = selectedWarrantyId;
					}
				} else if (selectedWarrantyId != 0 && selectedWarrantyId != modelId) {
					modelIds[modelIds.length] = modelId;
					modelIds[modelIds.length] = selectedWarrantyId;
				} else {
					modelIds[modelIds.length] = modelId;
				}
			}
		}
	}

	if (modelIds.length) {
		document.getElementById("favoritesMessage_" + baseModelId).innerHTML = "Adding...";
		
		httpUrl = "/products/functions/addFavorites.cfm?modelIds=" + modelIds + "&baseModelId=" + baseModelId;
		
		http("GET", httpUrl, favoritesAdd_callback);
		
	} else {
		alert("Enter a quantity for the items you would like to add to your favorites");
	}	
	
}

function favoritesAdd_callback(obj) {
	document.getElementById("favoritesMessage_" + obj).innerHTML = "This product has been added to your <a href=\"/ecommerce/account.cfm?theInterface=favorites\">favorites</a>.";
}


function doFavoritesBubble(modelRangeId) {
	form = document.forms["modelsForm_" + modelRangeId];
	modelIds = new Array();
	
	// loop through all form fields
	for (i = 0; i < form.elements.length; i++) {
		fieldName = form.elements[i].name;
		
		// find a basemodel
		if (fieldName.indexOf("baseModelNumber_") != -1) {
			
			modelId = fieldName.split("_")[1];
			selectedOptionId = form.elements["selectedOptionId_" + modelId].value;
			selectedWarrantyId = form.elements["selectedWarrantyId_" + modelId].value;
			
	
			// check if we should even bother with this baseModel
			isOrderable = form.elements["baseModelOrderable_" + modelId].value;
			if (isOrderable == 1) {
				baseModelQty = parseInt(form.elements["quantity_" + modelId].value);
				
				if (baseModelQty >= 0) {
					if (selectedOptionId != modelId) {
						modelIds[modelIds.length] = selectedOptionId;
						if (selectedWarrantyId != 0 && selectedWarrantyId != modelId) {
							modelIds[modelIds.length] = selectedWarrantyId;
						}
					} else if (selectedWarrantyId != 0 && selectedWarrantyId != modelId) {
						modelIds[modelIds.length] = modelId;
						modelIds[modelIds.length] = selectedWarrantyId;
					} else {
						modelIds[modelIds.length] = modelId;
					}
				}
			}
		}
	}
	
	if (modelIds.length) {
		showFavoritesBubble(modelRangeId, modelIds);
	} else {
		alert("Enter a quantity for the items you would like to add to your favorites");
	}	
}

currentFavoritesBubble = null;
function showFavoritesBubble(modelRangeId, modelIds) {

	hideCurrentBubbles();
	showLayer("hideBubble");

	document.getElementById("favoritesBubble_" + modelRangeId).innerHTML = "<div class=\"loading\">Loading...</div>";
	httpUrl = "/products/functions/bubble-addToFavorites.cfm?modelIds=" + modelIds + "&productLineId=" + getUrlVar("productlineid") + "&productGroupId=" + getUrlVar("productgroupid") + "&modelGroupId=" + getUrlVar("modelgroupid") + "&modelRangeId=" + modelRangeId;
	
	jsmx.http("GET", httpUrl, favoritesBubble_Callback);
	showLayer("favoritesBubble_" + modelRangeId);
	currentFavoritesBubble = "favoritesBubble_" + modelRangeId;
	fixHideBubble();
}
function favoritesBubble_Callback(obj) {
	document.getElementById("favoritesBubble_" + obj.modelrangeid).innerHTML = obj.content;
}

function hidecurrentFavoritesBubble() {
	if (currentFavoritesBubble != null) {
		hideLayer("hideBubble");
		hideLayer(currentFavoritesBubble);
		currentFavoritesBubble = null;
	}
}

