/*
Infobox - Nathan Rose
Shows and hides a div just above a target.

open(infobox, target, delay, hideClassName)
    Parameters:
    infobox			- the element ID for the div
    target			- the element ID where the infobox should appear (should be just above)
    delay			- how long the infobox stays visible
    hideClassName	- (optional) if a class is used to hide the infobox (instead of inline styles), this will remove the class name and hide the infobox using inline styles for better control
*/
var Infobox = Class.create({
    open: function(infobox, target, delay, hideClassName) {
        if ($(infobox) && $(target)) {
            $(infobox)._moving = false;

            $(infobox).hide();
            if (hideClassName) {
                if ($(infobox).hasClassName(hideClassName)) $(infobox).removeClassName(hideClassName);
            }
            //  Prototype 1.6.0 bug, wait for 1.6.1 release
            $(infobox).clonePosition(target, { setLeft: true, setTop: true, setWidth: false, setHeight: false, offsetTop: (($(infobox).getHeight() * -1) - (useEffects ? 20 : 0)), offsetLeft: (($(target).getWidth() - $(infobox).getWidth()) / 2) });
            //var cumulativeOffset = $(target).cumulativeOffset();
            //$(infobox).setStyle({ position: 'absolute', top: (cumulativeOffset.top - $(infobox).getHeight() + 20) + 'px', left: (cumulativeOffset.left + (($(target).getWidth() - $(infobox).getWidth()) / 2)) + 'px' });

            this.onClick(infobox, delay, null);
        }
    },

    show: function(infobox) {
        if (useEffects) {
            $(infobox)._moving = true;
            new Effect.Parallel([
                new Effect.Move(infobox, { sync: true, mode: 'relative', y: 20 }),
                new Effect.Appear(infobox, { sync: true })
            ], {
                duration: 0.5,
                afterFinish: function(infobox) { $(infobox)._moving = false; }
            });
            //$(infobox).appear({ duration: 0.3, afterFinish: function(infobox) { /*$(infobox)._moving = false;*/ } });
        }
        else {
            $(infobox)._moving = true;
            $(infobox).show();
            $(infobox)._moving = false;
        }
    },

    hide: function(infobox) {
        if (useEffects) {
            $(infobox)._moving = true;
            new Effect.Parallel([
                new Effect.Move(infobox, { sync: true, mode: 'relative', y: 20 }),
                new Effect.Fade(infobox, { sync: true })
            ], {
                duration: 0.5,
                afterFinish: function(infobox) { $(infobox)._moving = false; }
            });
            //$(infobox).fade({ duration: 0.3, afterFinish: function(infobox) { /*$(infobox)._moving = false;*/ } });
        }
        else {
            $(infobox)._moving = true;
            $(infobox).hide();
            $(infobox)._moving = false;
        }
    },

    onClick: function(infobox, delay, event) {
        if (!$(infobox)._moving) {
            this.show(infobox);
            this.hide.delay(delay, infobox);
        }
    }
});

//  We don't use effects for IE or Opera
var useEffects = (Prototype.Browser.IE || Prototype.Browser.Opera ? false : true);
var infobox = new Infobox();

/*
Cart - Nathan Rose
Values are checked by the server, this is immune to SQL injection

add(productID, options, redirect, callbackElement)
    Parameters:
    productID           - product ID to add to the cart
    options             - option string to pass to the cart (<group ID>;<option value>)
    redirect            - true if the user should be redirected to the cart; false for an AJAX message
    callbackElement     - can be null if redirect = true; should be the ID of the element that caused the cart to add the product
*/
var Cart = Class.create({
	initialize: function() {
		this._postUrl = '/PreOrderForm.asp';
	},

	add: function(productID, quantity, options, redirect, callbackElement) {
		var iQuantity = 1;
		if (!isNaN(quantity)) {
			if (quantity > 0 && quantity <= 999) {
				iQuantity = quantity;
			}
		}

		var url = this._postUrl + '?prod=' + productID + '&quantity=' + iQuantity + '&options=' + options;
		if (redirect) {
			document.location.href = url;
		}
		else {
			new Ajax.Request(url, {
				method: 'get',
				onSuccess: function() {
					msg = 'Product was added to the cart.\n\nTo see your cart, click the "Your Cart" link at the top of this page.';
					if ($('customInfobox')) {
						$('customInfobox').innerHTML = '<span style="font-size: 1.4em;">' + msg + '</span>';
						infobox.open('customInfobox', callbackElement, 4, 'Description');
					}
					else {
						alert(msg);
					}
				}
			});
		}
	}
});
var cart = new Cart();

function validateSingle() {
    var bValid = true;

    if ($('options')) {
        bValid = false;
        
        if ($('options').value.length > 0) {
            bValid = true;
        }
    }

    if (bValid) {
        $('productForm').submit();
    }
    else {
        msg = 'Please select an option before adding this product to the cart.';
        if ($('customInfobox') && $('addToCart')) {
            $('customInfobox').innerHTML = '<span style="font-size: 1.2em; color: #cc0000;">' + msg + '</span>';
            infobox.open('customInfobox', 'addToCart', 4, 'Description');
        }
        else {
            alert(msg);
        }
    }
}
function cartAjaxPopup() {
    //myLightWindow = new lightwindow();
    //myLightWindow.activateWindow({    href: '/vip-cart-popup.asp',  type: 'page',    title: 'VIP',     width:400,     height:310    });

    myLightWindow.activateWindow({
        href: "/savings-cart-popup.asp",
        type: "page",
        title: 'Wait!  Join!',
        width: 715,
        height: 316
    });
}


function validateBundle() {
    var iQuantity = 0;
    var aItems = $$('input.BundleQuantity');

    for (var i = 0, len = aItems.length; i < len; ++i) {
        iQuantity += parseInt(aItems[i].value);
    }
    aItems.clear();

    var aValidQuantities;
    var bValid = false;
    if ($('BundleQuantity')) {
    	aValidQuantities = $('BundleQuantity').value.split(' or ');
    	
    	for (var i = 0, len = aValidQuantities.length; i < len; ++i) {
    		if (iQuantity == parseInt(aValidQuantities[i])) {
    			bValid = true;
    			break;
    		}
    	}
    }
    
    if (bValid) {
		$('productForm').submit();
    }
    else {
        msg = 'Invalid Quantity:  Quantity for all items must be ' + $('BundleQuantity').value + '. (You have ' + iQuantity + ')';
        if ($('customInfobox') && $('addToCart')) {
            $('customInfobox').innerHTML = '<span style="font-size: 1.2em;">' + msg + '</span>';
            infobox.open('customInfobox', 'addToCart', 4, 'Description');
        }
        else {
            alert(msg);
        }
    }
}

function toggleQuestions() {
    var ele = document.getElementById("ProductMoreQuestions");
    if (ele.style.display == "block") {
        ele.style.display = "none";
    }
    else {
        ele.style.display = "block";

    }
} 