windowManager = new WindowManager();

/**
 * Register a window.
 */
function addWindow(pathRoot, windowContainer, windowManager, windowId, width, height, url) {

	// Create a container div for this window and add it as a child of the big
	// windowContainer containing all
	// windows
	var subContainer = document.createElement("div");
	subContainer.setAttribute("id", windowId);
	subContainer.setAttribute("class", "PhotoStoreWindowContainer");
	windowContainer.appendChild(subContainer);
	subContainer.outerHTML = subContainer.outerHTML;

	// Load the CSS style specific to this window. Note that this cannot be done
	// by adding the <link>
	// reference to the window's source code itself, as it is loaded dynamically
	// during runtime, and
	// Internet Explorer does not seem to consider CSS styles referenced by
	// dynamically loaded HTML contents.
	// Also, creating a HTMLLinkElement and appending it using appendChild()
	// does not seem to work in Firefox,
	// so we'll add the style by directly writing into the document source.
	document.write("<link rel='stylesheet' href='" + pathRoot + "/css/" + windowId
			+ ".css' type='text/css'>");

	// Create the new window object
	var window = new OverlayWindow(windowId, width, height);
	if (url == null) {
		window.setContentSourceUrl(pathRoot + "/" + windowId + ".html");
	} else {
		window.setContentSourceUrl(url);
	}

	// Register the window object with the window manager
	windowManager.registerWindow(window);
	return window;
}

windowContainer = document.getElementById("WindowContainer");

windowManager.ADD_TO_CART_ID = "AddToCartWindow";
windowManager.SHOPPING_CART_ID = "ShoppingCartWindow";
windowManager.LOGIN_ID = "LoginWindow";
windowManager.FAQ_ID = "FAQWindow";
windowManager.CHECKOUT_ID = "CheckoutWindow";

function addToCart() {
	var form = document.getElementById("AddToCartForm");
	var formValid = true;
	var useType = getSelectedRadio(form.UseType);
	if (useType == -1) {
		formValid = false;
	}
	if (useType != -1) {
		useTypeMediumSize = document.getElementById("Medium" + useType).value;
		if (!useTypeMediumSize) {
			formValid = false;
		}
	}
	if (formValid) {
		DivUtils.makeDivInvisible("IncompleteAddToCartFormErrorMessage");
	} else {
		DivUtils.makeDivVisible("IncompleteAddToCartFormErrorMessage");
		return false;
	}

	var useTypeMediumSizeArray = useTypeMediumSize.split("|");
	var typeOfUse = useTypeMediumSizeArray[0];
	var medium = useTypeMediumSizeArray[1];
	var size = useTypeMediumSizeArray[2];
	var shoppingCartInterfaceUrl = "/ShoppingCartInterface.php?action=additem&gallerypath="
			+ galleryPath
			+ "&picture="
			+ picture
			+ "&use="
			+ typeOfUse
			+ "&medium=" + medium + "&size=" + size;
	trackWindow('/PHOTO_STORE/ADD_TO_CART_WINDOW/SUBMIT/' + picture);
	// document.write(shoppingCartInterfaceUrl);
	CommunicationUtils.loadPage(shoppingCartInterfaceUrl, new function() {
		//HACK: fix this
		setTimeout("windowManager.openSoleWindow(windowManager.SHOPPING_CART_ID);", 350);
		div = document.getElementById('ShoppingCartLinks');
		if (div) {
			div.style.display = 'inline';
		}
		trackWindow('/PHOTO_STORE/SHOPPING_CART_WINDOW/VIEW');
		});
}

function updateUseSelectorVisibilities() {
	form = document.getElementById("AddToCartForm");
	selectedRadio = getSelectedRadio(form.UseType);

	for (i = 0; i <= 4; i++) {
		explanationName = "UseExplanation" + i;
		selectorName = "UsePriceSelector" + i;

		if (selectedRadio == -1) {
			DivUtils.makeDivVisible(explanationName);
		} else {
			DivUtils.makeDivInvisible(explanationName);
		}

		if (i == selectedRadio) {
			DivUtils.makeDivVisible(selectorName);
		} else {
			DivUtils.makeDivInvisible(selectorName);
		}
	}
}
