window.onload = function() {
	getShoppingCart();
};

var cartNumItems = 0;
var cartTotal = 0;
var cartItems = null;
var cartItemFilenames = null;
var currentCartItem = 0;

function getShoppingCart() {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', "getCartContents.aspx", true);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
	        var response = self.xmlHttpReq.responseText;
	        
	        cartNumItems = response.substr(0, response.indexOf('\n') - 1);
	        response = response.substr(response.indexOf('\n') + 1);
	        cartTotal = response.substr(0, response.indexOf('\n'));
	        cartTotal = cartTotal.replace(/\s/g, "");
	        response = response.substr(response.indexOf('\n') + 1);
	        
	        var cartItemRecords = response.split('\n');
	        cartItems = new Array(cartNumItems);
	        cartItemFilenames = new Array(cartNumItems);
	        
	        for (i=0; i<cartNumItems; i++) {
		        var parts = cartItemRecords[i].split(' ');
		        cartItems[i] = parts[0];
		        cartItemFilenames[i] = parts[1];
	        }
	        
	        showShoppingCart();
        }
    }
    self.xmlHttpReq.send(null);
}

function addToCart(designId) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', "addToCart.aspx?id=" + designId, true);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
	        getShoppingCart();
        }
    }
    self.xmlHttpReq.send(null);
}

function markAsInCart(span) {
	if (span) {
		span.className = "";
		span.onclick = "";
		span.style.color = "#808080";
		span.innerHTML = "(In my cart)";
	}
}

function showShoppingCart() {
	var doc = document.body;
	if (!doc) doc = document;
	
	var shopDiv = document.getElementById('shopDiv');
	if (shopDiv != null) {
		shopDiv.innerHTML = "";
	}
	else {
		shopDiv = document.createElement("DIV");
	}
	shopDiv.id = "shopDiv";
	shopDiv.style.position = "absolute";
	shopDiv.style.left = "0px";
	shopDiv.style.top = "634px";
	shopDiv.style.width = "144px";
	shopDiv.style.height = "166px";
	shopDiv.style.padding = "0px";
	shopDiv.style.textAlign = "center";
	shopDiv.style.backgroundImage = "url(images/cartCorner.gif)";
	shopDiv.style.backgroundPosition = "top left";
	shopDiv.style.backgroundRepeat = "no-repeat";
	
	if ((cartItems != null && cartItemFilenames != null) && cartNumItems > 0) {

		// show most recently added item
		var tbl = document.createElement("TABLE");
		tbl.style.margin = "0px auto 0px auto";
		var tbod = document.createElement("TBODY");
		var tr = document.createElement("TR");
		var td = document.createElement("TD");
		td.style.verticalAlign = "middle";
		td.style.width = "92px";
		td.style.padding = "6px 0px 12px 0px";
		
		
		var subtbl = document.createElement("TABLE");
		subtbl.style.width = "100%";
		var subbod = document.createElement("TBODY");
		var subtr = document.createElement("TR");
		var subtd = document.createElement("TD");
		subtd.style.padding = "0px";
		subtd.style.textAlign = "left";
		
		var img = document.createElement("IMG");
		img.src = "images/backButtonMini.gif";
		img.style.cursor = "pointer";
		img.onclick = function() { currentCartItem--; if (currentCartItem < 0) currentCartItem = cartNumItems - 1; updateShoppingCartDisplay(); };
		
		subtd.appendChild(img);
		subtr.appendChild(subtd);
		subtd = document.createElement("TD");
		subtd.style.padding = "0px";
		subtd.style.textAlign = "left";
		
		img = document.createElement("IMG");
		img.src = "images/forwardButtonMini.gif";
		img.style.cursor = "pointer";
		img.onclick = function() { currentCartItem++; if (currentCartItem >= cartNumItems) currentCartItem = 0; updateShoppingCartDisplay(); };
		
		subtd.appendChild(img);
		subtr.appendChild(subtd);
		subtd = document.createElement("TD");
		subtd.style.padding = "0px";
		subtd.style.textAlign = "right";
		var a = document.createElement("A");
		a.className = "invert";
		a.href = "viewcart.aspx";
		a.appendChild(document.createTextNode("my cart"));
		subtd.appendChild(a);
		subtr.appendChild(subtd);
		subbod.appendChild(subtr);
		subtbl.appendChild(subbod);
		
		td.appendChild(subtbl);
		
		tr.appendChild(td);
		tbod.appendChild(tr);
		
		tr = document.createElement("TR");	
		td = document.createElement("TD");
		td.id = "shoppingCartDisplay";
		td.style.width = "92px";
		td.style.height = "92px";
		td.style.padding = "0px";
		td.style.verticalAlign = "middle";
		td.style.textAlign = "center";
		td.style.backgroundColor = "#ffffff";
		td.style.cursor = "pointer";
		td.onclick = function() { window.location = "viewcart.aspx"; };
		
		tr.appendChild(td);
		tbod.appendChild(tr);
	
		tr = document.createElement("TR");	
		td = document.createElement("TD");
		td.style.textAlign = "left";
		td.style.padding = "4px 0px 0px 0px";
			
		a = document.createElement("A");
		a.href = "viewcart.aspx";
		a.className = "invert";
		a.appendChild(document.createTextNode("items: " + cartNumItems));
		td.appendChild(a);
		
		td.appendChild(document.createElement("BR"));
		
		a = document.createElement("A");
		a.href = "viewcart.aspx";
		a.className = "invert";
		if (cartTotal == "FREE")
			a.appendChild(document.createTextNode("price: FREE"));
		else
			a.appendChild(document.createTextNode("price: $" + cartTotal));
		td.appendChild(a);
		
		tr.appendChild(td);
		tbod.appendChild(tr);
		tbl.appendChild(tbod);
		shopDiv.appendChild(tbl);
	}
	else {
		var dv = document.createElement("DIV")
		dv.style.padding = "34px 0px 0px 0px";
		dv.style.color = "#ffffff";
		dv.appendChild(document.createTextNode("Your shopping cart is empty"));
		shopDiv.appendChild(dv);
	}
	
	doc.appendChild(shopDiv);
	alignShoppingCart();
	updateShoppingCartDisplay();
}

function updateShoppingCartDisplay() {
	var td = document.getElementById('shoppingCartDisplay');
	if (td == null) return;
	
	if (td.firstChild != null)
		td.removeChild(td.firstChild);
	
	var a = document.createElement("A");
	//a.href = "viewShape.aspx?id=" + cartItems[currentCartItem];
	a.href = "viewcart.aspx";
	var img = document.createElement("IMG");
	img.src = "/silhouette/miniThumbnails/" + cartItemFilenames[currentCartItem] + ".gif";
	a.appendChild(img);
	td.appendChild(a);
}

function alignShoppingCart() {
	var el = document.getElementById('shopDiv');
	if (el != null) {
		var offset = 0;
		if (document.documentElement.scrollTop >= 0)
			offset = document.documentElement.scrollTop;
		else if (document.body.scrollTop >= 0)
			offset = document.body.scrollTop;
		else
			offset = window.pageYOffset;
		
		var num = document.documentElement.clientHeight;
		
		num = Number(num) + Number(offset);
		
		if (num - 166 < 400)
			num = 566;
		el.style.top = Number(num - 166) + "px";
		setTimeout(alignShoppingCart, 200);
	}
}



function submitSurveyAnswer(qId, val) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', "surveys/saveQuestion.aspx?surveyId=6" + "&qId=" + qId + "&answer=" + encodeURIComponent(val), true);
    self.xmlHttpReq.onreadystatechange = null;
    self.xmlHttpReq.send(null);
}

function getRadioValue(container) {
	var el;
	
	el = container.firstChild;
	while (el) {
		if (el.tagName == "INPUT" && el.type == "radio" && el.checked)
			return el.value;
			
		var recursed = getRadioValue(el);
		if (recursed != "")
			return recursed;
			
		el = el.nextSibling;
	}
	
	return "";
}

