
/////////////////////////////////////////////////////////////////////////
// AJAX FUNCTIONS
/////////////////////////////////////////////////////////////////////////

//Desc: Replace 'add to cart' submit button with a call to the
//      AJAX add to cart function - For Java Enabled Users
function writeAjaxSubmit(){
	//Get a reference to the qtyAddCartButton div
	var temp = document.getElementById("addButton");
	var tempQty = document.getElementById("minOrderQty").value;
	
	var newContent = "<label>Quantity:</label><input id='prodQty' type='text' name='qty' size='1' value='"+tempQty+"' onkeypress='{if (event.keyCode==13)ajaxAddToCart()}' />";
	newContent += "<div class='left'><a href='javascript:ajaxAddToCart()' title='Add to Cart'><img src='/images/graphics/bg-atc-button.gif' alt='Add to Cart' border='0' /></a></div>";
	newContent += "<a href='/Help-Center/privacy' target='new' title='Shopping with us is safe. Guaranteed' id='shopSafe'><img src='/images/graphics/safe-shopping.gif' alt='Shopping with us is safe guarenteed' /></a><div class='clear'></div>";
	temp.innerHTML = newContent;
}

/////////////////////////////////////////////////////////////////////////
// END AJAX FUNCTIONS
/////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////
// GET PRICING AND ADD TO CART FUNCTIONS
/////////////////////////////////////////////////////////////////////////

// Desc: Get item pricing by sending an AJAX call to get_pricing.php
function getPricing(){
	var num_options = document.getElementById('numOptions').value;
	
	//Set up arguments array
	pricingArgs = new2DArray((eval(3)+eval(num_options)),2);
	pricingArgs[0][0] = "sku_grp_id";
	pricingArgs[0][1] = document.getElementById('skuID').value;
	pricingArgs[1][0] = "base_sku";
	pricingArgs[1][1] = document.getElementById('itemSKU').value;
	pricingArgs[2][0] = "num_options";
	pricingArgs[2][1] = num_options;
	
	//Set up options
	for(var i=0;i<num_options;i++){
		pricingArgs[(i+3)][0] = "option"+i;
		pricingArgs[(i+3)][1] = document.getElementById('option'+i).value;
	}
	
	//Write Loading screen to itemPrice Div
	$('itemPriceDisplay').innerHTML = "<img src='/images/graphics/page-loading.gif' /><br /><br />Loading...";
	
	//Run ajax and get pricing
	//runAJAXasync('get_pricing','itemPrice',pricingArgs);
	priceData = runAJAXsync('get_pricing_new',pricingArgs);
	imageData = runAJAXsync('get_item_image',pricingArgs);
	
	//Set up price display
	if( priceData.length > 2 ){
		$('displayNum').innerHTML =  priceData.substr(0, priceData.indexOf('[PRICING]') );
		$('itemPriceDisplay').innerHTML =  priceData.substr( (priceData.indexOf('[PRICING]')+9) );
	}
	
	//Set up new image
	if( imageData.length > 2 ){
		$('main_image').src = imageData;
		$('popup_image').href= imageData;
	}
}


// Desc: Add item to cart by sending an AJAX call to add_to_cart.php
function ajaxAddToCart(){
	var returnData = "";
	var num_options = $('numOptions').value;
	var qty = eval($('prodQty').value);	
	var minOrderQty = $('minOrderQty').value;
	var orderIncrement = $('orderIncrement').value;
	
	if( isNumeric(qty) && qty > 0 ){
		//Set up arguments array
		cartArgs = new2DArray((eval(6)+eval(num_options)),2);
		cartArgs[0][0] = "sku_grp_id";
		cartArgs[0][1] = $('skuID').value;
		cartArgs[1][0] = "item_sku";
		cartArgs[1][1] = $('itemSKU').value;
		cartArgs[2][0] = "type";
		cartArgs[2][1] = $('itemType').value;
		cartArgs[3][0] = "sales_price";
		cartArgs[3][1] = $('salesPrice').value;
		cartArgs[4][0] = "qty";
		cartArgs[4][1] = qty;
		cartArgs[5][0] = "num_options";
		cartArgs[5][1] = num_options;
		
		//Set up options
		for(var i=0;i<num_options;i++){
			cartArgs[(i+6)][0] = "option"+i;
			cartArgs[(i+6)][1] = $('option'+i).value;
		}
		
		
		if(qty < minOrderQty){
			alert("This product has a minimum order quantity of " + minOrderQty);
		}else if(qty % orderIncrement > 0){
			alert("This product must be ordered in carton/case quantities of " + orderIncrement);
		}else{
			//Make item added box invisible / write loading screen
			//$('itemAddedDiv').innerHTML = "<center><br /><br /><img src='/images/graphics/page-loading.gif' /><br />Loading...</center>";
		
			//Run AJAX Call
			returnData = runAJAXsync('add_to_cart',cartArgs);
			
			//Print success if complete error if failed
			if( returnData != "ERROR" || returnData != "" ){
				//Show quatity discounts
				if( qty > 1 && $('qtyProdPg') ){
					$('qtyProdPg').style.display = "block";	
				}
				
				// Set opacity of added to cart div to zero
				$('addedAlign').setStyle('opacity','0');
				$('addedAlign').style.display = "inline";
				
				// Load content to added to cart div
				$('cartItems').innerHTML = returnData.substr(0, returnData.indexOf('[BREAK]') );
				$('addedContent').innerHTML = returnData.substr( returnData.indexOf('[BREAK]')+7, returnData.length );
				
				// Fade in added to cart div
				$('addedAlign').fade('in');
				
				// Fade out and hide added to cart div after 10 seconds
				(function(){ $('addedAlign').fade('out'); }).delay(10000);
				(function(){ $('addedAlign').style.display = 'none'; }).delay(12000);
				
				$('cartItemAdded').style.display = "block";
			} else {
				$("cartError").innerHTML = "<br /><center><b class='required'>ALERT:</b> There was an error adding your item to the cart please try again or call 800-791-2946.</center>";
			}		
		}
	} else {
		alert("ALERT: You must enter a valid quantitiy.");
	}
}

function closeCartBox(){
	// Fade out and hide added to cart div
	$('addedAlign').fade('out');
	(function(){$('addedAlign').style.display = "none";}).delay(2000);
}

/////////////////////////////////////////////////////////////////////////
// END GET PRICING AND ADD TO CART FUNCTIONS
/////////////////////////////////////////////////////////////////////////


