dojo.require("dijit.Dialog");
dojo.require("dojo.parser");


function getInternetExplorerVersion() {
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat(RegExp.$1);
	}
	return rv;
}

function checkVersion() {
	var msg = "You're not using Windows Internet Explorer.";
	var ver = getInternetExplorerVersion();
	if (ver > -1) {
		if (ver >= 8.0)
			msg = "You're using a recent copy of Windows Internet Explorer."
		else
			msg = "You should upgrade your copy of Windows Internet Explorer.";
	}
	alert(msg);
}

	
function calcHeight(iframe_name)
{
	var getIE = getInternetExplorerVersion();
	var the_height;
  //find the height of the internal page
  if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
	  the_height = document.getElementById(iframe_name).contentDocument.body.scrollHeight;
	  the_height = (the_height + 70);  
  }else{
	  the_height = document.getElementById(iframe_name).contentWindow.document.body.scrollHeight;			
  		if(getIE > 7){
		  the_height = (the_height + 70);  
		}else{
			the_height = (the_height + 60);
		}
 }
 	console.log("height: " + the_height);
	document.getElementById(iframe_name).height = the_height;
}

function removeQuotes(tabName) {
    tabName = tabName.split("'").join("&#39;");
    tabName = tabName.split('"').join("&#39;");
	tabName = tabName.split("\"").join("&#39;");
	tabName = tabName.split("&quot;").join("&#39;");
    return tabName;
}

var dialogView;
function showActualSize(imgloc, imgname){
//	imgname = removeQuotes(imgname);
//	dialogView = new dijit.Dialog({
//				  title: "View Full Size Image",
//				  style: "width: auto; text-align: center",
//				  content: "<img src='" + imgloc + "' alt='"+ imgname +"' /><br><input type='button' value='Close' style='text-align: center' onclick='closeActualSize();'/>"});
//	dialogView.startup();
//	dialogView.show();
	window.open(imgloc);
}

function closeActualSize(){
	dialogView.hide();
}

dojo.require("dijit.Tooltip");
dojo.require("dijit.layout.ContentPane");
dojo.require("dojo.parser");

function getCartInfo(){
	var showtable;
	var tablerow = "";
	
	dojo.xhrPost({
		url:"fetchShoppingCartAjax.action",
		handleAs: "json",
		load: function(data) {
			dojo.byId("viewCartSubTotal").innerHTML = "$" + data.subTotal.toFixed(2);
			
			if (data.items.length > 0) {
				dojo.forEach(data.items, function(e) {
					tablerow += "<tr valign='top'><td class='firstRow' width='65px'><img src='" + e.imageName +"' width='40px' height='40px' /></td>";
					tablerow += "<td class='firstRow' width='*'>" + e.itemName + "<br>";
					if(e.quantity > 1){
						tablerow += "<font style='font-size: x-small;'>Qty: " + e.quantity + "</font><br/>";
					}
					tablerow += "<font style='font-size: x-small;'>Price: $" + e.amount.toFixed(2) + "</font>";
					tablerow += "</td></tr>";
					dojo.byId("totalQty").innerHTML = data.totalItemQty ;

				});
			}
			showtable = "<table id='viewCart' border='0' cellpadding='0' cellspacing='0'>";
			showtable += tablerow;
			showtable += "</table>";				
			
			dojo.byId("cart").innerHTML = showtable;
			

		}
	});
}
$(function() {
	  dojo.xhrPost({
		  url:"fetchShoppingCartAjax.action",
		  handleAs: "json",
		  load: function(data) {
			  if(data.totalItemQty > 0){
				  dojo.byId("totalQty").innerHTML = data.totalItemQty ;
				  dojo.byId("shoppingcart").style.display = "inline";
			  }
		  }
	  });
});

function addToCart(prodId){
	dojo.xhrPost({
		url: "addToCart.action",
		content: { productId: prodId,
			productOptionId: "",
			count: "1",
			optionalText: ""},
		handleAs: "json",
		timeout: 15000,
		sync: true,
		error: function(err) {
			console.log("error: " + err.message);
		},
		load: function() {
			getCartInfo();
			dojo.byId("shoppingcart").style.display = "inline";
			verifyAdd();
		}
	});
}


	function verifyAdd(){
			$.ui.dialog.defaults.bgiframe = true;
			$("#verifyCart").dialog({
				bgiframe: true,
				height: 100,
				width: 200,
				modal: true
			});
			$("#verifyCart").dialog("open");
			setTimeout(function(){
					$("#verifyCart").dialog("close");
			}, 2000);
	}

