//**********************************************************************
//
// Object structure defination
// 
// Objects in multidementional structure: products -> product -> colors -> sizes.
// 
// Each objects has it own operational functions.
//
//**********************************************************************

// This object provide the top level structure to hold products.
// Each productList element is a product.
//
function productsObj (n) {

	this.productList = new Array(n);
	
	this.updateByColor = updateByColor;
	
	this.updateByColorAndSize = updateByColorAndSize;
	
	this.updateQuantity = updateQuantity;
	
	this.Add2ShopCart = Add2ShopCart;
		
	this.toString = productsToString;
}

// This object is product.
// Each each colorList element is color. 
//
function productObj (styleIdProductId, styleId, n) {

	this.productShortDesc = "";					// Product short description.
	
	this.userSelectedSizeId = null;				// User actually click and select a size value at anytime store the size Id here. This change as user change size.
												// Another word this is the user clicked size and not the pre-selected size.
	
	this.styleIdProductId = styleIdProductId;  	// Unique id for this product use in this javaScript object structure.
	this.styleId = styleId;
	this.colorList = new Array(n);
	
	this.lastSelectedColor = null;				// This track the last color the user selected.
	this.lastSelectedSize = null;				// This keep track the user last selected size Object use for presistent logic part.
	
	this.buyQty = 1;							// This store user buy quantity value.
	
	this.updateUserSelectedSizeId = updateUserSelectedSizeId;
	
	this.toString = productToString;
}

// This object is color.
// Each each sizeList element is size. 
//
function colorObj (colorId, n) {

	this.colorId = colorId;
	this.sizeList = new Array(n);

	this.findSizeByColorAvailableSize = findSizeByColorAvailableSize;
	
	this.findLastSizeByColor = findLastSizeByColor;
	
	this.updateColorSizeToSelectedSize = updateColorSizeToSelectedSize;
	
	this.resetColorSizeIsSelectedStatus = resetColorSizeIsSelectedStatus;
	
	this.toString = colorToString;
}

// This object is size.
// 
function sizeObj (sizeId, isAvailable, catentryId, availQty, isSelectedSize) {

	this.sizeId = sizeId;
	this.isAvailable = isAvailable;
	this.catentryId = catentryId;
	this.availQty = availQty;
	
	this.isSelectedSize = isSelectedSize;
	
	this.toString = sizeToString;
}

//**********************************************************************
//
// Structure functon defination
//
//**********************************************************************

// This function update the tracking structure when user change color.
//
function updateByColor(thisColorElement, sizePos, styleIdProductId, colorId) {

	function getProduct (pl, sidpid) {
    	for (var i = 0; i < pl.length; ++i) {
        	if (pl[i].styleIdProductId == sidpid) {
            	return pl[i];
            }
        }
        return false;
    }
    
    function getColor (cl, cid) {
    	for (var j = 0; j < cl.length; ++j) {
        	if (cl[j] && cl[j].colorId == colorId) {
            	return cl[j];
            }
        }
        return false;
    }
    
    function getSizeFromColor (cl) {
        var ret = false;
        ret = cl.lastSelectedSize;
        return ret;
    }
    
	// Find the correct product, color and size in the array.
    
    var product = getProduct(this.productList, styleIdProductId);
	
	if (product) {
    	
        var color = getColor(product.colorList, colorId);
        
        if (color) {
        	// Color found.
            
            // If last selected size has value, then select the current color with same size, iff the size is available for this color.
            var lastSelectedSizeId = null;
            
            if (product.userSelectedSizeId) {
            	lastSelectedSizeId = product.userSelectedSizeId;
            } else if (product.lastSelectedSize) {
            	lastSelectedSizeId = product.lastSelectedSize.sizeId;
            }
            
            var currentSelectedSizeObj = null;
            
            if(lastSelectedSizeId != null) {
            
            	// No most current user selected size id was found.
                // Find out if last selected size is available in the current selected color.
                currentSelectedSizeObj = color.findSizeByColorAvailableSize(lastSelectedSizeId);
                
                if (currentSelectedSizeObj != null) {
                	// Found it.  Reset and update the current color size to indicate it is now the most recent selected size.
                    color.resetColorSizeIsSelectedStatus();
                    color.updateColorSizeToSelectedSize(lastSelectedSizeId);
                } else {
                	// Not size availble found in this color.
                    // Find out if the original selected size in this color is available.
                    currentSelectedSizeObj = color.findLastSizeByColor();
                }
                
            } else {
            	// Empty lastSelectedSize mean that there were no size for that last color or size selected by the user.
				// Find out from this color where the last selected size is and assign to lastSelectedSize.
				currentSelectedSizeObj = color.findLastSizeByColor();
            }
            
            // Update size.
            if (currentSelectedSizeObj != null) {
            	// Update productObj last selected size.
                product.lastSelectedSize = currentSelectedSizeObj;
                
                // Now update the UI CSS selection to remove the pre-selected size value from FRY original code.
                // This is vitual view only for the frontend.
                var sizeLists = $(thisColorElement).parent().siblings('div:first').find('ul');
                
                $(sizeLists).removeAttr('class');
                
                $(sizeLists).find('li').each(function () {
                    if (product.lastSelectedSize.sizeId == $(this).find('span').children().text() && !$(this).hasClass('unavailable')) {
                        $(this).parent().addClass('current-sizes').siblings('.current-sizes').removeClass('current-sizes');
                        $(this).addClass('selected-size').siblings('.selected-size').removeClass('selected-size');
                    }
                });
            }
        }
        // Update to current selected color
        product.lastSelectedColor = colorId;
    }
    
    //debug("In updateByColor: \n\n " + this.toString());
	// update the color label with the alt text from the color image
    var color_options = $(thisColorElement).parent().children("li");
    var colorLabel = thisColorElement.getElementsByTagName("img")[0].alt;
    var colorLabelWrapper = thisColorElement.parentNode.parentNode.getElementsByTagName("span")[0];
    colorLabelWrapper.innerHTML = colorLabel;
    // update the viewable available sizes
    var sizeWrapper = $(thisColorElement).parent().siblings('div.item_size_wrapper')[0];
    var sizeLists = $(sizeWrapper).children('ul');
    $(sizeLists).each(function () {
    	$(this).removeAttr('class');
    });
    var i = parseInt( $(thisColorElement).attr('rel'), 10);
    sizeLists[i].className = 'current-sizes';
    /*
    var currentList = $(".current-sizes", sizeWrapper);
    var currentSize = $(".selected-size", currentList);
    var sizeValue = $("span", currentSize)[0].firstChild.nodeValue;
    var inputField = $("input", sizeWrapper)[0];
    $(inputField).attr("value", sizeValue);
    */
    // update the price per color
    var detailWrapper = thisColorElement.parentNode.parentNode.parentNode.getElementsByTagName("div")[0];
    var priceWraper = detailWrapper.getElementsByTagName("ul")[0];
    var thePrices = priceWraper.getElementsByTagName("li");
    var totalPrices = thePrices.length;
    for(var p = 0; p<totalPrices; p++) {
    	thePrices[p].className = '';
    }
    thePrices[i].className = 'current_price';
    // determine to show message if item is not part of sale
    var nonsaleMessageArea = thisColorElement.parentNode.parentNode.getElementsByTagName("span")[1];
    if($(thisColorElement).hasClass("not_part")) {
    	var itemAmt = thePrices[i].getElementsByTagName("p")[0].firstChild.nodeValue;
        var theMessage = "Selected color is not on sale. Price is " + itemAmt;
        nonsaleMessageArea.innerHTML = theMessage;
        nonSalesItem = "true";
    } else {
    	nonsaleMessageArea.innerHTML = " ";
        nonSalesItem = "false";
    }
    if($(thisColorElement).hasClass("is_part")) {
    	var itemAmt = thePrices[i].getElementsByTagName("p")[0].childNodes[0].firstChild.nodeValue;
        var itemRegAmt = thePrices[i].getElementsByTagName("p")[0].childNodes[2].firstChild.nodeValue;
        var theMessage = "Selected color is on sale. Price is " + itemAmt + " reg " + itemRegAmt;
        nonsaleMessageArea.innerHTML = theMessage;
        nonSalesItem = "true";
    } else {
    	nonsaleMessageArea.innerHTML = " ";
        nonSalesItem = "false";
    }
    // update image
	var productWrapper = thisColorElement.parentNode.parentNode.parentNode.parentNode.getElementsByTagName("div")[0];
	//var imageWrapper = productWrapper.getElementsByTagName("div");
	var imageWrapper = $(productWrapper).hasClass('item-image-area') ? $(productWrapper).children('div') : $("div.item-image-area>div", productWrapper);
	var totalImages = imageWrapper.length;
	for(var t = 0; t<totalImages; t++) {
		imageWrapper[t].className = '';
	}
	if(imageWrapper[i] != null){
		imageWrapper[i].className = 'current-image';
	}
	// Update the product Id
	setProductId ();
	// update select color box
	$(color_options).removeClass("item_current-color");
	$(thisColorElement).addClass("item_current-color");
}

function checkLineItemQuantity(buyQty,LineLimitQuantity){
	
	var item_qty = 0;
	var lmt_qty=0;
	item_qty = parseInt(buyQty);
	lmt_qty=parseInt(LineLimitQuantity);
	if(item_qty > lmt_qty){
		return false;
	}
	return true;
}

// This funciton add product to cart
// submitType: 	0 - single product detail page submit.
//				1 - multiproduct page submit.
//
function Add2ShopCart(fm, submitType, LineLimitQuantity, alertMsg){

	var catentryIdList = "";
	var quantityList = "";
	var styleIdList = "";
	var selectedItemCount = 0;
	var processedItemCount = 0;
	var selectedItemNotAvailableCount = 0;
	
	var groupItemNotAvailableMsg = "";
	
	var appendComma = false;
	
	var msgItemNotAvailable = "not available in the quantity you requested";
	var msgOutOfStock = "out of stock";
	
	var submitFormCheck = true;
    
    var productList = this.productList;
    
    function getIndexByStyleIdProdId (sidpid) {
    	for (var i = 0; i < productList.length; i++) {
        	if (sidpid == productList[i].styleIdProductId)
            	return i;
        }
        return false;
    }
	
    if (submitType == '0') {
        var prod = productList[0];
        selectedItemCount++;
        if (prod.lastSelectedSize != null) {
        	var inValidQtyMsg = "Please enter valid quantity for item: " + prod.productShortDesc;
            var quantity = document.getElementsByName("quantity")[0].value;
            prod.buyQty = quantity;
            if (!isValidQty(prod.buyQty)) {
            	alert(inValidQtyMsg);
                return;
            }
            
            //alert("Before calling the line item method");
			if(!checkLineItemQuantity(prod.buyQty, LineLimitQuantity)){
				alert(alertMsg);
				return;
			}
			//end
					
            if (parseInt(prod.lastSelectedSize.availQty) == 0) {
            	groupItemNotAvailableMsg += "\nItem: " + prod.productShortDesc + " - " + msgOutOfStock;
                ++selectedItemNotAvailableCount;
            }
            if (parseInt(prod.lastSelectedSize.availQty) < parseInt(prod.buyQty)) {
            	groupItemNotAvailableMsg += "\nItem: " + prod.productShortDesc + " - " + msgItemNotAvailable;
                ++selectedItemNotAvailableCount;
            }
            if (appendComma == true){
            	catentryIdList += ",";
                quantityList += ",";
                styleIdList += ",";
            }
            if(prod.lastSelectedSize != null){
            	catentryIdList += prod.lastSelectedSize.catentryId;
                quantityList += prod.buyQty;		// Note that this value is actually come from the same value as the user see at the last movement of submit getting in above  logic.
                styleIdList += prod.styleId;
                appendComma = true;
                ++processedItemCount;
            }
        } else {
        	groupItemNotAvailableMsg += "\nItem: " + prod.productShortDesc + " - " + msgOutOfStock;
            ++selectedItemNotAvailableCount;
        }
    } else if (submitType == '1') {
    	var errorMsg = false;
		$(fm).find('input:checked').each(function () {
    		var index = getIndexByStyleIdProdId($(this).attr('name'));
	        if (index >= 0) {
        		var prod = productList[index];
            	selectedItemCount++;
	            if (prod.lastSelectedSize != null) {
            		var inValidQtyMsg = "Please enter valid quantity for item: " + prod.productShortDesc;
	                if (submitType == '1') {
						var inputQtyWrapper = $(this).parent().parent().find('dl:first').find('dd:first').find('input:first');
						if (inputQtyWrapper.attr('name') == 'qty') {
                    	if (!isValidQty(inputQtyWrapper.val())) {
                        	alert(inValidQtyMsg);
                            return;
                        } else {
                        	prod.buyQty = inputQtyWrapper.val();
						}
					}
                	} else if (submitType == '0') {
                		var quantity = document.getElementsByName("quantity")[0].value;	
						prod.buyQty = quantity;
                	}
	                
					// End speical condition for qty check above.
					
					// Validate qty.				
					if (!isValidQty(prod.buyQty)) {
						alert(inValidQtyMsg);
						return;
					}
					
					//Changes made for multi product page
					if(!checkLineItemQuantity(prod.buyQty, LineLimitQuantity)){
						errorMsg = true;
						return;
					}
					//end
						
					if (parseInt(prod.lastSelectedSize.availQty) == 0) {
                		groupItemNotAvailableMsg += "\nItem: " + prod.productShortDesc + " - " + msgOutOfStock;
						++selectedItemNotAvailableCount;
                	}
					
					// Validate qty verse available qty.
					if (parseInt(prod.lastSelectedSize.availQty) < parseInt(prod.buyQty)) {
					groupItemNotAvailableMsg += "\nItem: " + prod.productShortDesc + " - " + msgItemNotAvailable;
					++selectedItemNotAvailableCount;
				}
		
					// Append comma only in this format: [a, b, c,....,z] where a-z can be any number or char string.  Only and skip last comma.
					if (appendComma == true){
					catentryIdList += ",";
					quantityList += ",";
					styleIdList += ",";
				}
					
					if(prod.lastSelectedSize != null){
					catentryIdList += prod.lastSelectedSize.catentryId;
					quantityList += prod.buyQty;		// Note that this value is actually come from the same value as the user see at the last movement of submit getting in above  logic.
					styleIdList += prod.styleId;
					appendComma = true;
					++processedItemCount;
				}			
				} else {
					groupItemNotAvailableMsg += "\nItem: " + prod.productShortDesc + " - " + msgOutOfStock;
					++selectedItemNotAvailableCount;
				}
        	}
    	});
    	
    	if(errorMsg){
    		alert(alertMsg);
			return;
    	}
    }
	
	// No item was checked
	if (selectedItemCount == 0){
	
		alert("Please check item Add to Bag checkbox to add to bag");
		return;
	}	
	
	// Item not availlable.
	if(selectedItemNotAvailableCount > 0){

		alert("Sorry, the item you tried to add to your shopping bag is either out of stock or not available in the quantity you requested." 
				+ "\n" + groupItemNotAvailableMsg);
		return;	
	}
	
	// Build submit form.
	fm.catEntryId.value = catentryIdList;
	fm.quantity.value = quantityList;
	fm.addCatID.value = styleIdList;
	fm.numOfProducts.value = processedItemCount;
	
	// Single product submit.
	if(submitType == '0'){

		fm.action='TCPOrderItemAdd';
		fm.URL.value='OrderItemDisplay';	
	}
	
	fm.submit();	
}

// This function update sizeid to a color.
//
function updateByColorAndSize(styleIdProductId, colorId, sizeId){

	function getProduct (pl, sidpid) {
    	for (var i = 0; i < pl.length; i++) {
        	if (pl[i].styleIdProductId == sidpid) {
            	return pl[i];
            }
        }
        return false;
    }
    
    function getColor (cl, cid) {
    	for (var i = 0; i < cl.length; i++) {
        	if (cl[i].colorId == cid) {
            	return cl[i];
            }
        }
        return false;
    }
    
	// Find the product in the list.
	var product = getProduct(this.productList, styleIdProductId);
    
    if (product) {
    	
        var color = getColor(product.colorList, colorId);
        
        if (color) {
		
            // Color found. Check if the size is available to be selected for this color.
            
            var currentSelectedSizeObj = color.findSizeByColorAvailableSize(sizeId);
            
            if (currentSelectedSizeObj != null) {
                // Found size is available to be selected for this color.
                
                // Keep track of the initial selected with new selected size value.
                // The initial selected size is either marked as first page load or when user selected it.
                
                color.resetColorSizeIsSelectedStatus();
                color.updateColorSizeToSelectedSize(sizeId);
                
                // Keep track of what being selected in product level and that will be used for final sumbit of the form later.
                product.lastSelectedColor = colorId;
                product.lastSelectedSize = currentSelectedSizeObj;
                
                // No need to update the UI CSS selection to remove the pre-selected size value from FRY original code.
                // FRY original code is working correctly to highlight the user wanted size.
                // Keep track of the user wanted size in product level and it will be used to first check for the user wanted size if available when user switch color.
                
                product.updateUserSelectedSizeId(sizeId);
                
            }
            
            for (var i = 0; i < product.colorList.length; i++) {
            	var currentSelectedSizeObj = product.colorList[i].findSizeByColorAvailableSize(sizeId);
                if (currentSelectedSizeObj != null) {
                	product.colorList[i].resetColorSizeIsSelectedStatus();
                    product.colorList[i].updateColorSizeToSelectedSize(sizeId);
                }
            }
		}
	}
}

// This function update the product buy quantity, if available.
//
function updateQuantity (styleIdProductId, buyQty, obj){

	// Check for buyQty is number or greater than 1;
	if(!isValidQty(buyQty)){
	
		// Invalid qty set the tracking structure buyQty to zero.
		for (var i = 0; i < this.productList.length; ++i){
			if(this.productList[i].styleIdProductId == styleIdProductId){
				this.productList[i].buyQty = 0;
			}
		}
	
		/*
		alert("Please enter valid quantity");
		obj.focus();
		obj.select();
		*/
		return;
	}
	
	// Find the product in the list.
	for (var i = 0; i < this.productList.length; ++i){
	
		if(this.productList[i].styleIdProductId == styleIdProductId){
		
			// Found the product.
			var outOfStockMsg = "Sorry, the item you tried to add to your shopping bag is either out of stock or not available in the quantity you requested.";
			
			if(this.productList[i].lastSelectedSize != null){
			
				//debug(this.productList[i].lastSelectedSize.availQty + " >= " + buyQty);
				
				this.productList[i].buyQty = buyQty;
			
				/*
				if(parseInt(this.productList[i].lastSelectedSize.availQty) >= parseInt(buyQty)){
				
					// Update buying quantity.
					this.productList[i].buyQty = buyQty;
					
				}else{
				
					
					alert(outOfStockMsg);
					obj.focus();
					obj.select();
				}
				*/
			
			}else{
			
				// Quantity not available.
				
				/*
				alert(outOfStockMsg);
				obj.focus();
				obj.select();
				*/
				
			}
			
			return;
		}
	}

	//debug("In updateProductQuantity: \n\n " + this.toString());
}

// This function find out from this color where the last selected or available first selected size is.
//
function findLastSizeByColor(){

	for (var i = 0; i < this.sizeList.length; ++i){
		
		if(this.sizeList[i].isSelectedSize == true){
			return this.sizeList[i];
		}
	}
	return null;
}

// This function find out if the given size id is available to this color.
//
function findSizeByColorAvailableSize(sizeId){

	for (var i = 0; i < this.sizeList.length; ++i){
		
		if(this.sizeList[i].sizeId == sizeId && this.sizeList[i].isAvailable == true){
			return this.sizeList[i];
		}
	}
	return null;
}

// This function reset the previous selected status to false.
//
function resetColorSizeIsSelectedStatus(){

	for (var i = 0; i < this.sizeList.length; ++i){
		
		this.sizeList[i].isSelectedSize = false;
	}
}

// This function mark the size in this color as selected.
//
function updateColorSizeToSelectedSize(sizeId){

	for (var i = 0; i < this.sizeList.length; ++i){
		
		if(this.sizeList[i].sizeId == sizeId && this.sizeList[i].isAvailable == true){
			this.sizeList[i].isSelectedSize = true;
		}
	}
}

// This function update user initial selected size in product level.
//
function updateUserSelectedSizeId(sizeId){

	this.userSelectedSizeId = sizeId;
}

// This function validate a quantity value.
//
function isValidQty(qty){

	var rc = true;
	
	if(!isInteger(qty) || qty <= 0){
	
		rc = false;
	}
	
	return rc;
}

// This function add muiltiProduct page item to wish list.
// Note that current design only allow one product to be added to wish list at a time.
//
function AddToWishListMultiProducts(form, errMsgBadQty, isRegisterUser, styleIdProductId){

	var rc = true;
	
	for (var i = 0; i < productsTrack.productList.length; ++i){
	
		if(productsTrack.productList[i].styleIdProductId == styleIdProductId){
			
			if(productsTrack.productList[i].lastSelectedSize == null){
			 	rc = false;
			 	break;
			}
			
			form.catEntryId.value = productsTrack.productList[i].lastSelectedSize.catentryId;

			// 	This is a speical condition for qty check and IMPORTANT for submit of the right qty:
			// 	Validate qty value format at run time.
			// 	In this case user may update and keep ignore the warning and not to change the value
			// 	and the tracking structure does not get update due to FRY code triger last. 
			//	Drill right into the form and get the quantity value that user see on the page.
			//				
			// For multiproduct submit.
							
			var inValidQtyMsg = "Please enter valid quantity for item: " + productsTrack.productList[i].productShortDesc;
			
			var addToBagElement = document.getElementsByName(productsTrack.productList[i].styleIdProductId)[0];	
			var dlWrapper = addToBagElement.parentNode.parentNode.getElementsByTagName("dl")[0];
			var ddWrapper = dlWrapper.getElementsByTagName("dd")[0];
			var inputQtyWrapper = ddWrapper.getElementsByTagName("input")[0];
			
			if(inputQtyWrapper.name == "qty"){

				if(!isValidQty(inputQtyWrapper.value)){
				
					alert(inValidQtyMsg);
					return;
					
				}else{
				
					productsTrack.productList[i].buyQty = inputQtyWrapper.value;
				}
			}
			
			
			form.quantity.value = productsTrack.productList[i].buyQty;
		}
	}

	if(!rc){
		alert("Sorry, this item can not be added to the wish list");
	}else{
		
		// Reuse the generic add to wish list function.
		AddToWishList(form, errMsgBadQty, isRegisterUser, styleIdProductId);
	}

}

// This function add item to wish list.
//
function AddToWishList(form, errMsgBadQty, isRegisterUser, styleIdProductId)
{
	// Find the product in the list.
	for (var i = 0; i < productsTrack.productList.length; ++i){
	
		if(productsTrack.productList[i].styleIdProductId == styleIdProductId){
			
			if(isEmpty(form.catEntryId.value)){
			
				alert(errMsgBadQty);
				return false;
			}
			
			if(!checnNumeric(form.quantity.value)){
				alert(errMsgBadQty);
				return false;
			}
			
			// Found the product.
			if(productsTrack.productList[i].lastSelectedSize != null){
			
				form.quantity.value = productsTrack.productList[i].buyQty;
				form.catEntryId.value = productsTrack.productList[i].lastSelectedSize.catentryId;

				if (isRegisterUser == 'true'){
				
					form.action='InterestItemAdd';
					form.URL.value='InterestItemDisplay';
					
				}else{
				
					form.action='LogonForm';
					form.URL.value= getNotLoginWishListUrl(form.storeId.value, form.langId.value, form.catEntryId.value, form.catalogId.value, form.quantity.value);
				}
				
				form.submit();
						
			}else{
			
				alert("Sorry, this item can not be added to the wish list");
				
				return false;
			}
			
		}
	}
}

// This function construct a url for none login user to add item to wish list.
//
function getNotLoginWishListUrl(storeId, langId, catEntryId, catalogId, qty){

	return 'InterestItemAdd?storeId='+storeId+'&langId='+langId+'&catEntryId='+catEntryId+'&catalogId='+catalogId+'&quantity='+qty+'&URL=InterestItemDisplay';
}

//**********************************************************************
//
// Defination of function [x]ToString().
// Mostly use for debug purpose.
//
//**********************************************************************

function productsToString(){

	var s = this.productList.toString() + "\n";
	return s;
}

function productToString(){

	var s = "product: " + this.productShortDesc + ", " + this.styleIdProductId + ", styleId=" + this.styleId + ", userSelectedSizeId=" + this.userSelectedSizeId + ", buyQty=" + this.buyQty + "\n lastSelectedColor=" +this.lastSelectedColor + "\n lastSelectedSize= " + this.lastSelectedSize + "\n " + this.colorList.toString() + "\n";
	return s;
}

function colorToString(){

	var s = "colorId: " + this.colorId + ":\n " + this.sizeList.toString() + "\n";
	return s;
}

function sizeToString(){

	var s = "sizeId: " + this.sizeId + ", isAvailable=" + this.isAvailable + ", catentryId=" + this.catentryId + ", availQty=" + this.availQty 
			+ ", isSelectedSize=" + this.isSelectedSize + "\n";
	return s;
}

function debug(s){

	//alert(s);
}

function toggle(carouselId){
	if (typeof(callToggle) != "undefined" ) {
		toggleArrows(carouselId);
	}				
}

// Code for larger image rollovers

function init_enlargeImage(){

	$(document).ready(function() {
	  $(".item-image-area div").hover(function() {
	    if ( $(".imagePopOver").length > 0 ) {
	       $(".imagePopOver").remove();
	    }
	    var imgTag = $(this).html();
	    imgTag = imgTag.replace("width=\"185\"", "width=\"300\"");
	    imgTag = imgTag.replace("height=\"185\"", "height=\"300\"");
	    imgTag = imgTag.replace("width=185", "width=\"300\"");
	    imgTag = imgTag.replace("height=185", "height=\"300\"");
	    imgTag = imgTag.replace("_m.jpg", "_l.jpg");
	    imgTag = imgTag.replace("class=\"image\"", "");
	    $(this).parent().parent().before("<div class=\"imagePopOver newImagePopOver\">"+imgTag+"</div>");
	
	    // Create the action on mouseout
	    $(".newImagePopOver").hover(function() {
	    // Do nothing
	    },function(){
	    $(this).fadeOut(300);
	    });
	    // Remove the new Class
	    $(".newImagePopOver").removeClass("newImagePopOver");
	  },function(){
	  });
	});
}
var getIndex = function (elem) {
	var ret = false;
    $(elem).parent().children().each(function (i) {
    	if (this == elem) ret = i;
    });
    return ret;
};