// JavaScript Document

/* == DETECT BROWSER TYPE == */
var isDOM=document.getElementById?1:0, // General detection - not very useful.
 	 isIE=document.compatMode && document.all, // Detects IE6+
	 isIE7=document.documentElement && typeof document.documentElement.style.maxHeight!="undefined", // Detects only IE7
	 isFF=window.Iterator, // Detects only FireFox
 	 isOp=self.opera?1:0,
 	 isDyn=isDOM||isIE;

function displayBlock(e) {
	var divname = e;
	var thediv = document.getElementById(divname);
	thediv.style.display = 'block';
}
function displayNone(e) {
	var divname = e;
	var thediv = document.getElementById(divname);
	thediv.style.display = 'none';
}

/* == THIS METHOD IS FOR CHANGING THE VNAV's == */
function changeMenu(e) {
	var elToShow = document.getElementById(e);
	var mainContainer = document.getElementById('navigation');
	var mcChildren = mainContainer.childNodes;
	
	for (var i=0; i < mcChildren.length; i++) {
		var name = mcChildren[i].id;
		
		if ((name !== undefined) && (name.indexOf('vNav', 0) == 0)) {
			var elToHide = document.getElementById(name);

			if ((elToHide.style.display == "block") && (name == e)) {
				return
			} else if ((elToHide.style.display == "block") && (name !== e)) {
				elToHide.style.display = "none";
				elToShow.style.display = "block";
			}
		}
	}
}

/* == BELOW IS FOR SHOWING/HIDING THE SELECT DROP DOWN == */
var timeout	= 300;
var closetimer	= 0;
var ddShown	= 0;
// open hidden layer
function showDropDown(id) {	
	// cancel close timer
	ddClearCloseTime();
	// close old layer
	if (ddShown) {
		ddShown.style.visibility = 'hidden';
	}
	  // get new layer and show it
	  ddShown = document.getElementById(id);
	  ddShown.style.visibility = 'visible';
}
// close showed layer
function hideDropDown() {
	if (ddShown) {
		ddShown.style.visibility = 'hidden';
	}
}
// go close timer
function ddCloseTime() {
	closetimer = window.setTimeout(hideDropDown, timeout);
}
// cancel close timer
function ddClearCloseTime() {
	if (closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}
// close layer when click-out
//document.onclick = hideDropDown;


function hideBrowseBy() {
	var container = document.getElementById('browseBy');
	container.style.visibility = "hidden";
}
function switchKitsBrowseBy(navToShow) {
	var container = document.getElementById('browseBy');
	var label1 = document.getElementById('bbLabel1');
	var label2 = document.getElementById('bbLabel2');
	if (navToShow == 'vNavCarTypes') {
		container.innerHTML = '<a href="javascript: changeMenu(\'vNavCarMakes\'); switchKitsBrowseBy(\'vNavCarMakes\'); ddCloseTime(); hideBrowseBy()">Car Makes</a><a href="javascript: changeMenu(\'vNavCarYear\'); switchKitsBrowseBy(\'vNavCarYear\'); ddCloseTime(); hideBrowseBy()">Year Car Made</a><a class="last" href="javascript: changeMenu(\'vNavKitManufacturers\'); switchKitsBrowseBy(\'vNavKitManufacturers\'); ddCloseTime(); hideBrowseBy()">Kit Manufacturers</a>';
		label1.innerHTML = 'Car Types';
		label2.innerHTML = 'Car Types';
	} else if (navToShow == 'vNavCarMakes') {
		container.innerHTML = '<a href="javascript: changeMenu(\'vNavCarTypes\'); switchKitsBrowseBy(\'vNavCarTypes\'); ddCloseTime(); hideBrowseBy()">Car Types</a><a href="javascript: changeMenu(\'vNavCarYear\'); switchKitsBrowseBy(\'vNavCarYear\'); ddCloseTime(); hideBrowseBy()">Year Car Made</a><a class="last" href="javascript: changeMenu(\'vNavKitManufacturers\'); switchKitsBrowseBy(\'vNavKitManufacturers\'); ddCloseTime(); hideBrowseBy()">Kit Manufacturers</a>';
		label1.innerHTML = 'Car Makes';
		label2.innerHTML = 'Car Makes';
	} else if (navToShow == 'vNavCarYear') {
		container.innerHTML = '<a href="javascript: changeMenu(\'vNavCarTypes\'); switchKitsBrowseBy(\'vNavCarTypes\'); ddCloseTime(); hideBrowseBy()">Car Types</a><a href="javascript: changeMenu(\'vNavCarMakes\'); switchKitsBrowseBy(\'vNavCarMakes\'); ddCloseTime(); hideBrowseBy()">Car Makes</a><a class="last" href="javascript: changeMenu(\'vNavKitManufacturers\'); switchKitsBrowseBy(\'vNavKitManufacturers\'); ddCloseTime(); hideBrowseBy()">Kit Manufacturers</a>';
		label1.innerHTML = 'Year Car Made';
		label2.innerHTML = 'Year Car Made';
	} else if (navToShow == 'vNavKitManufacturers') {
		container.innerHTML = '<a href="javascript: changeMenu(\'vNavCarTypes\'); switchKitsBrowseBy(\'vNavCarTypes\'); ddCloseTime(); hideBrowseBy()">Car Types</a><a href="javascript: changeMenu(\'vNavCarMakes\'); switchKitsBrowseBy(\'vNavCarMakes\'); ddCloseTime(); hideBrowseBy()">Car Makes</a><a class="last" href="javascript: changeMenu(\'vNavCarYear\'); switchKitsBrowseBy(\'vNavCarYear\'); ddCloseTime(); hideBrowseBy()">Year Car Made</a>';
		label1.innerHTML = 'Kit Manufacturers';
		label2.innerHTML = 'Kit Manufacturers';
	}
}

/* == Functions below are for showing and hiding the tool tip == */
/* == FOUR METHODS TO LOCATE X AND Y POSITION OF THE ELEMENT OR INPUT FIELD CALLING UP THE <DIV> CONTAINING THE MESSAGE ==*/
// Use two below if you DON'T know the element ID
function getX(e) {
    if (!e && this) {
        e = this;
    }
    var x = e.offsetLeft;
    var eParent = e.offsetParent;
    while (eParent != null) {
        x += eParent.offsetLeft;
        eParent = eParent.offsetParent;
    }
	//alert("X Position is " + x);
    return x;
}

function getY(e) {
    if (!e && this) {
        e = this;
    }

    var y = e.offsetTop;
    var eParent = e.offsetParent;
    while (eParent != null) {
        y += eParent.offsetTop;
        eParent = eParent.offsetParent;
    }
	//alert("Y position is " + y);
    return y;
}
// Use two below if you DO know the element ID
function getXid(eid) {
    var e = document.getElementById(eid);
    var x = e.offsetLeft;
    var eParent = e.offsetParent;
    while (eParent != null) {
        x += eParent.offsetLeft;
        eParent = eParent.offsetParent;
    }
	//alert("X Position is " + x);
    return x;
}
function getYid(eid) {
	var e = document.getElementById(eid);
    var y = e.offsetTop;
    var eParent = e.offsetParent;
    while (eParent != null) {
        y += eParent.offsetTop;
        eParent = eParent.offsetParent;
    }
	//alert("Y position is " + y);
    return y;
}

/* METHOD FOR SHOWING SALES TAX TIP ON THE SH0PPING CART */
function showSalesTaxTip(e) {
	var x = getXid(e);
	var y = getYid(e);
	x += 3;
	y -= 42;
	//alert(callerX);
	//alert(callerY);
	var tip = document.getElementById('salesTaxTipFond');
	var content = document.getElementById('salesTaxTip');
	if (e == "shipping") {
		content.innerHTML = "Actual cost calculated when you checkout.";
	}
	if (e == "taxes") {
		content.innerHTML = "Applies only to California Residents";
	}
	tip.style.left = (x) +"px";
	tip.style.top = (y) +"px";
	tip.style.visibility = "visible";
}
function hideSalesTaxTip() {
	var tip = document.getElementById('salesTaxTipFond');
	tip.style.visibility = "hidden";
}
/* METHOD FOR QUANTITY VERIFICATION ON THE PRODUCT & PRODUCT DETAIL PAGES WHEN ADD TO CART BUTTON IS CLICKED
** THIS METHOD ALSO DISPLAYS THE MINI-CART VERIFICATION MODAL WINDOW
*/
function verifyQty(eid, layer) {
	// NOTE: The eid is the same as the product_id
	// New addition for form action to javascript
	hideQtyTip();
	//Reference to the text input
	var e = document.getElementById(eid);
	//Hack for IE6
	e.blur();
	//Reference to the floating qty tool tip
	var qtip = document.getElementById('qtyTip');
	//Reference to the content div within the qty tool tip
	var qtipContent = document.getElementById('qtyTipContent');
	//Get the value of the Qty input
	var qty = e.value;
	//Is it one or two chars long?
	var qtyLength = qty.length;
	//var qtyMatch = /^[1|2|3|4|5|6|7|8|9][0-9]?$/; //qty can't start with 0 and can only contain integers
	var qtyMatch = /^\d$|^\d\d/;
	var qtyValidation = qty.match(qtyMatch);
	//alert(qtyLength);
	
	// FIRST CHECK OF THERE IS ANYTHING ENTERED IN THE FIELD
	if (qtyLength == 0) {
		// Add the messge to the tool tip
		qtipContent.innerHTML = "Please enter a quantity.";
		x = getXid(eid);
		y = getYid(eid);
	   	// x -= 3;
	   	y -= 54;
		qtip.style.visibility="visible";
		qtip.style.left = (x) +"px";
		qtip.style.top = (y) +"px";
		return
	}
	//SECOND CHECK IF THE NUMBER ENTERED IS 0
	if (qtyLength == 1 && qty == 0) {
		// Add the messge to the tool tip
		qtipContent.innerHTML = "Quantity must be at least 1.";
		x = getXid(eid);
		y = getYid(eid);
		// x -= 3;
	   	y -= 54;
		qtip.style.visibility="visible";
		qtip.style.left = (x) +"px";
		qtip.style.top = (y) +"px";
		return
	}
	//THIRD CHECK IF THE NUMBER ENTERED IS 00
	if (qtyLength == 2 && qty == 00) {
		// Add the messge to the tool tip
		qtipContent.innerHTML = "Quantity must be at least 1.";
		x = getXid(eid);
		y = getYid(eid);
		// x -= 3;
	   	y -= 54;
		qtip.style.visibility="visible";
		qtip.style.left = (x) +"px";
		qtip.style.top = (y) +"px";
		return
	}
	// FOURTH CHECK IF THE QTY VALUE IS A NUMBER
	if (qtyValidation == null) {
		// Add the messge to the tool tip
		qtipContent.innerHTML = "\"" + qty + "\" is not a valid quantity.";
		// Now get the x y coords of the input
		x = getXid(eid);
		y = getYid(eid);
		// x -= 3;
	   	y -= 54;
		qtip.style.visibility="visible";
		qtip.style.left = (x) +"px";
		qtip.style.top = (y) +"px";
		return
	}
	// FINALLY IF EVERYTHING ELSE IS OK - - CLEAN UP THE QTY IF THE NUMBER BEGINS WITH 0
	if (qty.indexOf(0) == 0 && qty !== 00) {
		qty = qty.charAt(qty.length - 1);
		//alert("QTY = " + qty);
	}
	
	//IF EVERYTHING IS OK, PASS THE eid AND qty TO THE addToCart() and set the cartItems Cookie
	// NEW CODE TO ADD AND ALTER COOKIE INFORMATION STORED TO SHOW THE NUMBER OF ITEMS IN THE PAGE SHOPPING CART
	var currentCartItems = getCookie('cartItems');
	if (!currentCartItems) {
		//alert("NO CART ITEM COOKIE IS SET");
		setCookie('cartItems', qty);
		var newCartItems = qty;
	} else {
		var newCartItems = Number(currentCartItems) + Number(qty);
		//alert("NEW CART ITEMS = " + newCartItems);
		setCookie('cartItems', newCartItems);
		//alert(getCookie('cartItems'));
	}
	//BUT FIRST DETERMINE FROM WHERE THE ADD TO CART WAS MADE
	if (layer) {
		var cartItemsDiv = this.parent.document.getElementById('cartItems');
		var messageDiv = this.parent.document.getElementById('cartInfo2');
		if (newCartItems == 1) {
			messageDiv.innerHTML = " item in your shopping cart.";
		} else {
			messageDiv.innerHTML = " items in your shopping cart.";
		}
		cartItemsDiv.innerHTML = newCartItems;
		this.parent.showAtc(eid, qty);
		this.parent.hideProductDetail();
	} else {
		var cartItemsDiv = document.getElementById('cartItems');
		var messageDiv = document.getElementById('cartInfo2');
		if (newCartItems == 1) {
			messageDiv.innerHTML = " item in your shopping cart.";
		} else {
			messageDiv.innerHTML = " items in your shopping cart.";
		}
		cartItemsDiv.innerHTML = newCartItems;
		showAtc(eid, qty);
	}
}
// FUNCTION TO HIDE THE QTY TIP
function hideQtyTip() {
	var qtip = document.getElementById('qtyTip');
	if (qtip.style.visibility == 'visible') {
		qtip.style.visibility = 'hidden';
	}
}

/* Method to show any type of static text containing layer */
function showInfo(infoFrameDivID, infoFrameID, infoWinMaskID, infoMaskFrameID, url, footer) {
	var winMask = document.getElementById(infoWinMaskID);
	var mask = document.getElementById(infoMaskFrameID);
	var infoFrameDiv = document.getElementById(infoFrameDivID);
	var infoFrame = document.getElementById(infoFrameID);
	infoFrame.setAttribute('src', url);
	// Set the size of the window mask
	if (isIE) {
		var winMaskWidth = document.body.offsetWidth;
		var winMaskHeight = document.body.offsetHeight;
	} else {
		var winMaskWidth = window.innerWidth;
		var winMaskHeight = document.height; //window.innerHeight;
	}
	winMask.style.width = winMaskWidth + "px";
	winMask.style.height = winMaskHeight + "px";
	// Determine the position of the contentContainer 
	x = getXid('contentContainer');
	y = getYid('contentContainer');
	// NEW ADDITION FOR FOOTER LINKS
	if (footer) {
		var footer = document.getElementById("footerNav");
		var footerY = getYid("footerNav");
		//alert(footerY);
		//Set the XY coords for the mask in relation to the contentContainer and Footer
		x += 90;
		y = footerY - 575;
		//Position the mask
		mask.style.top = y + "px";
		mask.style.left = x + "px";
		// Add 50 to X and Y for the infoFrame window
		x += 50;
		y += 50;
		infoFrameDiv.style.top = y + "px";
		infoFrameDiv.style.left = x + "px";
		// Now display
		winMask.style.visibility = "visible";
		mask.style.visibility = "visible";
		infoFrameDiv.style.visibility = "visible";
	} else {
		//Set the XY coords for the mask in relation to the contentContainer
		x += 90;
		y += 64;
		//Position the mask
		mask.style.top = y + "px";
		mask.style.left = x + "px";
		// Add 50 to X and Y for the infoFrame window
		x += 50;
		y += 50;
		infoFrameDiv.style.top = y + "px";
		infoFrameDiv.style.left = x + "px";
		// Now display
		winMask.style.visibility = "visible";
		mask.style.visibility = "visible";
		infoFrameDiv.style.visibility = "visible";
	}
}

/* Method to show any type of static text containing layer ON THE CO PAGES! */
function showCOInfo(infoFrameDivID, infoFrameID, infoWinMaskID, infoMaskFrameID, url, footer) {
	var winMask = document.getElementById(infoWinMaskID);
	var mask = document.getElementById(infoMaskFrameID);
	var infoFrameDiv = document.getElementById(infoFrameDivID);
	var infoFrame = document.getElementById(infoFrameID);
	infoFrame.setAttribute('src', url);

	// Set the size of the window mask
	if (isIE) {
		var winMaskWidth = document.body.offsetWidth;
		var winMaskHeight = document.body.offsetHeight;
	} else {
		var winMaskWidth = window.innerWidth;
		var winMaskHeight = document.height; //window.innerHeight;
	}
	winMask.style.width = winMaskWidth + "px";
	winMask.style.height = winMaskHeight + "px";
	// Determine the position of the contentContainer 
	x = getXid('coContainer');
	y = getYid('coContainer');
	// NEW ADDITION FOR FOOTER LINKS
	if (footer) {
		var footer = document.getElementById("footerNav");
		var footerY = getYid("footerNav");
		//alert(footerY);
		//Set the XY coords for the mask in relation to the contentContainer and Footer
		x += 180;
		y = footerY - 575;
		//Position the mask
		mask.style.top = y + "px";
		mask.style.left = x + "px";
		// Add 50 to X and Y for the text window
		x += 50;
		y += 50;
		infoFrameDiv.style.top = y + "px";
		infoFrameDiv.style.left = x + "px";
		// Now display
		winMask.style.visibility = "visible";
		mask.style.visibility = "visible";
		infoFrameDiv.style.visibility = "visible";
	} else {
		//Set the XY coords for the mask in relation to the contentContainer
		x += 90;
		y += 64;
		//Position the mask
		mask.style.top = y + "px";
		mask.style.left = x + "px";
		// Add 50 to X and Y for the text window
		x += 50;
		y += 50;
		infoFrameDiv.style.top = y + "px";
		infoFrameDiv.style.left = x + "px";
		// Now display
		winMask.style.visibility = "visible";
		mask.style.visibility = "visible";
		infoFrameDiv.style.visibility = "visible";
	}
}
/* Method to hide text information */
function hideInfo(infoFrameDivID, infoWinMaskID, infoMaskFrameID) {
	var winMask = document.getElementById(infoWinMaskID);
	var mask = document.getElementById(infoMaskFrameID);
	var infoFrameDiv = document.getElementById(infoFrameDivID);
	winMask.style.visibility = "hidden";
	mask.style.visibility = "hidden";
	infoFrameDiv.style.visibility = "hidden";
}

/* Method to show enlarged image */
function showImage(imageURL) {
	var winMask = document.getElementById('imageWinMask');
	var mask = document.getElementById('imageMaskFrame');
	var imageContainer = document.getElementById('imageContainer');
	var image = document.getElementById('image');
	
	// Set the size of the window mask
	if (isIE) {
		var winMaskWidth = document.body.offsetWidth;
		var winMaskHeight = document.body.offsetHeight;
	} else {
		var winMaskWidth = window.innerWidth;
		var winMaskHeight = window.innerHeight;
	}
	winMask.style.width = winMaskWidth + "px";
	winMask.style.height = winMaskHeight + "px";
	// Determine the position of the contentContainer 
	x = getXid('contentContainer');
	y = getYid('contentContainer');
	//Set the XY coords for the mask in relation to the contentContainer
	x += 90;
	y += 64;
	//Position the mask
	mask.style.top = y + "px";
	mask.style.left = x + "px";
	// Add 50 to X and Y for the text window
	x += 50;
	y += 50;
	imageContainer.style.top = y + "px";
	imageContainer.style.left = x + "px";
	// Now display
	image.setAttribute('src', imageURL);
	winMask.style.visibility = "visible";
	mask.style.visibility = "visible";
	imageContainer.style.visibility = "visible";
}
/* Method to hide enlarged image */
function hideImage() {
	var winMask = document.getElementById('imageWinMask');
	var mask = document.getElementById('imageMaskFrame');
	var imageContainer = document.getElementById('imageContainer');
	var image = document.getElementById('image');
	winMask.style.visibility = "hidden";
	mask.style.visibility = "hidden";
	imageContainer.style.visibility = "hidden";
	image.setAttribute('src', "");
}

/* Method to show product detail */
function showProductDetail(url) {
	//First get all of your elements
	var winMask = document.getElementById('productWinMask');
	var mask = document.getElementById('productMaskFrame');
	var kit = document.getElementById('productContainer');
	// Set the size of the window mask
	if (isIE) {
		var winMaskWidth = document.body.offsetWidth;
		var winMaskHeight = document.body.offsetHeight;
	} else {
		var winMaskWidth = window.innerWidth;
		var winMaskHeight = document.height; //window.innerHeight;
	}
	winMask.style.width = winMaskWidth + "px";
	winMask.style.height = winMaskHeight + "px";
	// Determine the position of the contentContainer 
	x = getXid('contentContainer');
	y = getYid('contentContainer');
	//Set the XY coords for the mask in relation to the contentContainer
	x += 90;
	y += 64;
	//Position the mask
	mask.style.top = y + "px";
	mask.style.left = x + "px";	
	// Add 50 to X and Y for the kit window
	x += 50;
	y += 50;
	kit.style.top = y + "px";
	kit.style.left = x + "px";
	var targetFrame = document.getElementById('productIframe');
	targetFrame.setAttribute('src', url);
	winMask.style.visibility = "visible";
	mask.style.visibility = "visible";
	kit.style.visibility = "visible";
}

/* Method to hide product detail */
function hideProductDetail() {
	var winMask = document.getElementById('productWinMask');
	var mask = document.getElementById('productMaskFrame');
	var kit = document.getElementById('productContainer');
	winMask.style.visibility = "hidden";
	mask.style.visibility = "hidden";
	kit.style.visibility = "hidden";
	// Clear iframe so as to remove 'persistence' for next showKit call
	var targetFrame = document.getElementById('productIframe');
	targetFrame.setAttribute('src', '');
}

// !! THIS FUNCTION IS USED TO CALL UP THE add-to-cart.php !!
function showAtc(p, q) {
	var pid = p;
	var qty = q;
	var winMask = document.getElementById('atcWinMask');
	if (isIE) {
		var winMaskWidth = document.body.offsetWidth;
		var winMaskHeight = document.body.offsetHeight;
	} else {
		var winMaskWidth = window.innerWidth;
		var winMaskHeight = document.height; //window.innerHeight;
	}
	winMask.style.width = winMaskWidth + "px";
	winMask.style.height = winMaskHeight + "px";
	var mask = document.getElementById('atcMaskFrame');
	// Determine the position of the contentContainer 
	x = getXid('contentContainer');
	y = getYid('contentContainer');
	//set the coords for the mask in relation to the contentContainer
	x += 90;
	y += 64;
	//Set the position of the mask
	mask.style.top = y + "px";
	mask.style.left = x + "px";
	// Set the position of the ATC frame
	var atc = document.getElementById('atcContainer');
	x += 50;
	y += 50;
	atc.style.top = y + "px";
	atc.style.left = x + "px";
	var targetFrame = document.getElementById('atcIframe');
	targetFrame.setAttribute('src', "add-to-cart.php?pid=" + pid + "&qty=" + qty);
	winMask.style.visibility = "visible";
	mask.style.visibility = "visible";
	atc.style.visibility = "visible";
}
function hideAtc() {
	var winMask = document.getElementById('atcWinMask');
	var mask = document.getElementById('atcMaskFrame');
	var atc = document.getElementById('atcContainer');
	winMask.style.visibility = "hidden";
	mask.style.visibility = "hidden";
	atc.style.visibility = "hidden";
	// Clear iframe so as to remove 'persistence' for next showKit call
	var targetFrame = document.getElementById('atcIframe');
	targetFrame.setAttribute('src', '');
}
//TWO FUNCTIONS FOR THE ATC BUTTONS. . .
function atcContinueShopping() {
	this.parent.hideAtc();
}
function atcViewCart() {
	setCookie('storeLocation', this.parent.location);
	this.parent.hideAtc();
	this.parent.location = "shopping-cart.php";
}
//FUNCTION FOR THE page_shopping_cart.html
function getCartItems() {
	var currentCartItems = getCookie('cartItems');
	if (!currentCartItems) {
		document.write("0");
	} else {
		var messageDiv = document.getElementById('cartInfo2');
		if (currentCartItems == 1) {
			messageDiv.innerHTML = " item in your shopping cart.";
		} else {
			messageDiv.innerHTML = " items in your shopping cart.";
		}
		document.write(currentCartItems);
	}
}
function viewCart() {
	setCookie('storeLocation', window.location);
	window.location = "shopping-cart.php";
}

// FUNCTIONS FOR THE shopping-cart.php
function removeItem(inputID) {
	var input = document.getElementById(inputID);
	//code to update the cartItems Cookie
	var currentInputQuantity = input.value;
	var cartItemCookieValue = getCookie('cartItems');
	var newCartItemCookieValue = Number(cartItemCookieValue) - Number(currentInputQuantity);
	setCookie('cartItems', newCartItemCookieValue);
	input.value = 0;
	document.shoppingCart.submit();
}
function continueShopping() {
	var cookieInfo = getCookie('storeLocation')
	if (!cookieInfo) {
		window.location = "http://www.v8models.com/";
	} else {
		window.location = cookieInfo;
	}
}
function updateItem() {
	/* All this code is doing is getting the qty values 
	** of all of the items in the shopping cart
	** so it can update the cartItems cookie to the new total
	** and then submitting the form.
	** NOTE: The form has one hidden input so the loop MUST NOT get it's value and add it to the cookie!
	*/
	var totalItems = 0;
	for (i = 0; i < document.shoppingCart.elements.length - 1; i++) {
		var quantity = document.shoppingCart.elements[i].value;
		totalItems = Number(totalItems) + Number(quantity);
	}
	setCookie('cartItems', totalItems);
	document.shoppingCart.submit();
}
/* THIS ADDITIONAL JAVASCRIPT CODE SIMPLY ASSURES THAT THE cartItems Cookie GETS PROPERLY UPDATED IF THE USER CLICKS INTO ONE OF THE QUANTITY INPUTS AND HITS THE RETURN KEY INSTEAD OF USING THE "UPDATE" BUTTON. */
function updateCartItems() {
	var totalItems = 0;
	for (i = 0; i < document.shoppingCart.elements.length - 1; i++) {
		var quantity = document.shoppingCart.elements[i].value;
		totalItems = Number(totalItems) + Number(quantity);
	}
	setCookie('cartItems', totalItems);
}

/* FORM UI ERROR FIELD AND FIELD LABLE HANDLER METHOD - 
** This depends on using a uniform naming convention for the form input ID's AND the class names in the style sheet)
** This is used on the shipping-info.php and the order-review.php
*/
function fieldErrors(field, inputClass) {
	// The if clause was added to handle the special case cc Exp Date on the order-review.php
	if (field == 'cardMonth' || field == 'cardYear') {
		var expDateLable = document.getElementById('cardDate');
		expDateLable.className = "fieldLabelError";
		var fieldInput = document.getElementById(field);
		fieldInput.className = inputClass + "Error";
	} else {
		var fieldLable = document.getElementById(field);
		var fieldInput = document.getElementById(field + 'I');
		fieldLable.className = "fieldLabelError";
		fieldInput.className = inputClass + "Error";
	}
}

// This is used on the customer-shipping-information.php - IF and WHEN they decide to ship outside US. . . .
function changeCountry(form) {
   var usForm = document.getElementById('usAddress');
   var nonUsForm = document.getElementById('address');
  if (form.country.value == "US") {
	  nonUsForm.style.display = "none";
	  usForm.style.display = "block";
	  // Set all of the non-US form values to ""
	  form.address1.value = "";
	  form.address2.value = "";
	  form.address3.value = "";
	  form.city.value = "";
	  form.state.value = "";
	  form.postal_code.value = "";
  } else {
	  usForm.style.display = "none";
	  nonUsForm.style.display = "block";
	  //Set all of the US form values to ""
	  form.us_address1.value = "";
	  form.us_address2.value = "";
	  form.us_city.value = "";
	  form.us_state.value = "";
	  form.us_postal_code.value = "";
  }
}

// This is used on the shipping-info.php
function toggleBilling() {
	var form = document.shippingInfo;
	var billingForm = document.getElementById("billingAddress");
	if (form.ba1.checked == true) {
		billingForm.style.display = "none";
	} else {
		billingForm.style.display = "block";
	}
}

/* This is used for re-populating the shipping form
** if the user partially fills out the form then 
** decides to go back to the shopping cart */
 function backToShoppingCart() {
	 var sfn = document.getElementById("sfnameI").value;
	 if (sfn == '') {
	 	window.location = "http://www.v8models.com/shopping-cart.php";
	 } else {
		var sln = document.getElementById("slnameI").value;
		var sadd1 = document.getElementById("sadd1I").value;
		var sadd2 = document.getElementById("sadd2I").value;
		var scity = document.getElementById("scityI").value;
		var sstate = document.getElementById("sstateI").value;
		var szip = document.getElementById("szipI").value;
		var email = document.getElementById("emailI").value;
		var phone = document.getElementById("phoneI").value;
		var ml = document.getElementById("mailinglist");
		if (ml.checked) {
			mlchecked = "true";
		} else {
			mlchecked = "false";
		}
		var bfn = document.getElementById("bfnameI").value;
		var bln = document.getElementById("blnameI").value;
		var badd1 = document.getElementById("badd1I").value;
		var badd2 = document.getElementById("badd2I").value;
		var bcity = document.getElementById("bcityI").value;
		var bstate = document.getElementById("bstateI").value;
		var bzip = document.getElementById("bzipI").value;
		// Was "Different Billing Address" radio button Checked ?
		var ba2 = document.getElementById("ba2");
		if (ba2.checked) {
			bchecked = "true";
		} else {
			bchecked = "false";
		}
		var shipInfo = sfn+";"+sln+";"+sadd1+";"+sadd2+";"+scity+";"+sstate+";"+szip+";"+email+";"+phone+";"+mlchecked+";"+bchecked+";"+bfn+";"+bln+";"+badd1+";"+badd2+";"+bcity+";"+bstate+";"+bzip+";";
		setCookie('shipInfo', shipInfo);
		window.location = "http://www.v8models.com/shopping-cart.php";
	 }								
 }

function getShippingInfo() {
	 var shipInfo = getCookie('shipInfo');
	 if (!shipInfo) {
		 return
	 } else {
		 var shipArray = shipInfo.split(";");
		 // Now populate the shipping / contact fields
		 document.getElementById("sfnameI").value = shipArray[0];
		 document.getElementById("slnameI").value = shipArray[1];
		 document.getElementById("sadd1I").value = shipArray[2];
		 document.getElementById("sadd2I").value = shipArray[3];
		 document.getElementById("scityI").value = shipArray[4];
		 document.getElementById("sstateI").value = shipArray[5];
		 document.getElementById("szipI").value = shipArray[6];
		 document.getElementById("emailI").value = shipArray[7];
		 document.getElementById("phoneI").value = shipArray[8];
		 if (shipArray[9] == 'true') {
			 var ml = document.getElementById("mailinglist");
			 ml.checked = "checked";
		 }
		 
		 // Is the billing address different?
		 if (shipArray[10] == 'true') {
			 var ba2 = document.getElementById("ba2");
			 ba2.checked = "checked";
			 document.getElementById("bfnameI").value = shipArray[11];
			 document.getElementById("blnameI").value = shipArray[12];
			 document.getElementById("badd1I").value = shipArray[13];
			 document.getElementById("badd2I").value = shipArray[14];
			 document.getElementById("bcityI").value = shipArray[15];
			 document.getElementById("bstateI").value = shipArray[16];
			 document.getElementById("bzipI").value = shipArray[17];
			 toggleBilling();
		 }
	 }
}

// Object to store sslCert timer and attributes
var sslCertControl = { "certTimer" : null, "certHeight" : 75 };
// Function to slide down the SSL Certificate info. . .
function toggleSSLCert() {
	var sslCert =  document.getElementById("sslCert");
	var sslCertHeight = sslCertControl.certHeight;
	var height = null;
	var heightIncrement = sslCertHeight / 8;
	if (sslCert.style.display == "block") {
		height = sslCertHeight;
		if (!sslCertControl.certTimer) {
			sslCertControl.certTimer = setInterval(function() {
			height = height - heightIncrement;
			sslCert.style.height = Math.floor(height) + "px";	
			if (height <= 8) {
				sslCert.style.display = "none";
				clearInterval(sslCertControl.certTimer); 
				sslCertControl.certTimer = null;
			}
			}, 10);
		}
	}
	
	if (sslCert.style.display == "none") {
		// set the height var to 0
		height = 0;
		// Set the actual cert div height to 0
		sslCert.style.height = "0px";	
		if (!sslCertControl.certTimer) {
			sslCert.style.display = "block";
			sslCertControl.certTimer = setInterval(function() {
			height = height + heightIncrement;
			// Only allow the sslCert to be as high as its native height
			if (height >= sslCertHeight) {
				sslCert.style.height = "75px";
				clearInterval(sslCertControl.certTimer); 
				sslCertControl.certTimer = null;
			} else if (height < sslCertHeight) {
				sslCert.style.height = Math.floor(height) + "px";
			}
			
			}, 10);
		}
	}
}

/* Method to show CCV detail */
function showCCVDetail() {
	//First get all of your elements
	var winMask = document.getElementById('ccvWinMask');
	var mask = document.getElementById('ccvMaskFrame');
	var ccvInfo = document.getElementById('ccvContainer');
	// Set the size of the window mask
	if (isIE) {
		var winMaskWidth = document.body.offsetWidth;
		var winMaskHeight = document.body.offsetHeight;
	} else {
		var winMaskWidth = window.innerWidth;
		var winMaskHeight = window.innerHeight;
	}
	winMask.style.width = winMaskWidth + "px";
	winMask.style.height = winMaskHeight + "px";
	// Determine the position of the contentContainer 
	x = getXid('coTopBtmBorder');
	y = getYid('coTopBtmBorder');
	//Set the XY coords for the mask in relation to the coTopBtmBorder
	x += 140;
	y += 0;
	//Position the mask
	mask.style.top = y + "px";
	mask.style.left = x + "px";	
	// Add 50 to X and Y for the ccvInfo window
	x += 50;
	y += 50;
	ccvInfo.style.top = y + "px";
	ccvInfo.style.left = x + "px";
	//var targetFrame = document.getElementById('productIframe');
	//targetFrame.setAttribute('src', url);
	winMask.style.visibility = "visible";
	mask.style.visibility = "visible";
	ccvInfo.style.visibility = "visible";
}
/* Method to hide CCV detail */
function hideCCVDetail() {
	var winMask = document.getElementById('ccvWinMask');
	var mask = document.getElementById('ccvMaskFrame');
	var ccvInfo = document.getElementById('ccvContainer');
	winMask.style.visibility = "hidden";
	mask.style.visibility = "hidden";
	ccvInfo.style.visibility = "hidden";
	// Clear iframe so as to remove 'persistence' for next showKit call
	var targetFrame = document.getElementById('productIframe');
	targetFrame.setAttribute('src', '');
}
// BEGIN Methods to disable/enable the Buy Now button
function disableBuyNowBtn() {
	var btn = document.getElementById("buyNow");
	btn.disabled = true;
}
function enableBuyNowBtn() {
	var btn = document.getElementById("buyNow");
	btn.disabled = false;
}
// END Methods to disable/enable the Buy Now button

//Functions to get and set js cookies
function getCookie(c_name) {
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}

function setCookie(c_name,value,expiredays) {
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+"; path=/";
}

// Function to set focus on desired input field  using ID of field.
function setFieldFocus(e) {
	var field = document.getElementById(e);
	field.focus();
}

// Methods to show the 2 Promocode Error Tips and Message on the Order Review Page
function showPcodeTip(e, which_one, msg) {
	// e is the input element to anchor it to
	//which_one is either the long or short tip
	//msg is the text string to place within the tip
	if (which_one == "short") {
		var pTipContainer = document.getElementById("pcodeShortTipFond");
		var pTip = document.getElementById("pcodeShortTip");
	} else if (which_one == "long") {
		var pTipContainer = document.getElementById("pcodeLongTipFond");
		var pTip = document.getElementById("pcodeLongTip");
	}
	pTip.innerHTML = msg;
	var x = getXid(e);
	var y = getYid(e);
	x += 103;
	y -= 47;
	pTipContainer.style.left = (x) +"px";
	pTipContainer.style.top = (y) +"px";
	pTipContainer.style.visibility = "visible";
}
function hidePcodeTip() {
	// Simply hide them both
	var shortTipContainer = document.getElementById("pcodeShortTipFond");
	var longTipContainer = document.getElementById("pcodeLongTipFond");
	shortTipContainer.style.visibility = "hidden";
	longTipContainer.style.visibility = "hidden";
}
