/**
 * Jewson eTrade Initiative.
 * Copyright (c) 2007, 2008 Jewson Limited.
 * All rights reserved.
 * --
 * quantity.js: Routines to increase/decrease a quantity box.
 */

/**
 * This method increases the count shown in an 'add to cart' quantity box by
 * 1.
 *
 * @param id The unique identifier of the product to be added. There should be
 * a text box for this product's quantity, called
 * <code>'quantity_{code}'</code>.
 * @return Always returns false.
 */
function decreaseQuantity(id) {
  var quantity = document.getElementById("quantity_" + id);
  if (quantity && (quantity.value > 1)) {
    quantity.value = parseInt(quantity.value) - 1;
  }
  return false;
}
/**
 * This method increases the count shown in an 'add to cart' quantity box by
 * 1.
 *
 * @param id The unique identifier of the product to be added. There should be
 * a text box for this product's quantity, called
 * <code>'quantity_{code}'</code>.
 * @return Always returns false.
 */
function decreaseQuantityVanstock(id) {
  var quantity = document.getElementById("quantity_" + id);
  if (quantity && (quantity.value >= 1)) {
    quantity.value = parseInt(quantity.value) - 1;
  }
  return false;
}

/**
 * This method decreases the count shown in an 'add to cart' quantity box by
 * 1.
 *
 * @param id The unique identifier of the product to be added. There should be
 * a text box for this product's quantity, called
 * <code>'quantity_{code}'</code>.
 * @return Always returns false.
 */
function increaseQuantity(id) {
  var quantity = document.getElementById("quantity_" + id);
  quantity.value = parseInt(quantity.value) + 1;
  return false;
}

/**
* This method resizes the image on the product_detail.jsp, if the image is larger than 300 X 300 px
* 
*/
function resizeLargeImage() {
	
	 var viewportwidth = 1023;
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
		// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight 
		 viewportwidth = window.innerWidth;
	 }
	 else if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.clientWidth !=
	     'undefined' && document.documentElement.clientWidth != 0)
	 {
		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)  
		 viewportwidth = document.documentElement.clientWidth;
	 }
	 else
	 {
		 // older versions of IE
		 viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	 }
	 
	 var img = document.getElementById('toResize');
	
	 if (img != null) {
		 
		 if (viewportwidth<=1024) {
			//also resize the container div to the largest possible
			var wrapperDiv = document.getElementById('productDetailWrapper');
			if(wrapperDiv != null){
				wrapperDiv.style.width=570+ 'px';
			}

			var imageSrc = img.src;
			var newImage = new Image();
			newImage.src = imageSrc;
			var resolutionW = document.body.offsetWidth;
			calculateDimensions(img, newImage.width, newImage.height, 285);
			
			
		}else  if (viewportwidth<=1280) {
			//also resize the container div to the largest possible
			var wrapperDiv = document.getElementById('productDetailWrapper');
			if(wrapperDiv != null){
				wrapperDiv.style.width=830+ 'px';
			}
			/*var imageSrc = img.src;
			var newImage = new Image();
			newImage.src = imageSrc;
			var resolutionW = document.body.offsetWidth;
			calculateDimensions(img, newImage.width, newImage.height, 400);*/
		}
		 // Whatever happens we want to show the image..
		img.style.visibility='visible';
	 }

}

function calculateDimensions(img, width, height, xDim){
	//alert("width" + width);  alert("height" + height);
	//alert("xDim"+ xDim);
	var maxx = xDim;
	var maxy = 400;
	var newy = 400;
	var newx = xDim;
	var needResize = false;
	if(width > 200) needResize = true;
	if(height > 400) needResize = true;
	
	if(needResize){
	//alert("resizing the image");
	
	 if (width >= height) {
		// landscape
		var tmpy = (height * newx) / width;
		if (tmpy <= newy)
			newy = tmpy;
		else
			newx = (width * newy) / height;   //alert("newx" + newx);
		if (newx == 0) {
			img.width = maxx;			
		} else {
			img.width = newx;			
		}
	// alert("img.width" + img.width ); alert("img.height" + img.height );
	} else {
		// portrait
		tmpx = (width * newy) / height;
		if (tmpx <= newx)
			newx = tmpx;
		else
			newy = (height * newx) / width;  //alert("newy" + newy);
		if (newy == 0) {
			img.height = maxy;			
		} else {
			img.height = newy;			
		} 
		// alert("img.height" + img.height );alert("img.width" + img.width);
		}
	}	
}
