/////////////////////////////////////////////////////////////////////////
// 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/grn-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(curOption){
	var num_options = $('numOptions').value;
	
	//Set up arguments array
	pricingArgs = new2DArray((eval(3)+eval(num_options)),2);
	pricingArgs[0][0] = "sku_grp_id";
	pricingArgs[0][1] = $('skuID').value;
	pricingArgs[1][0] = "num_options";
	pricingArgs[1][1] = num_options;
	pricingArgs[2][0] = "cur_option";
	pricingArgs[2][1] = curOption;
	
	//Set up options
	for(var i=0;i<num_options;i++){
		pricingArgs[(i+3)][0] = "option"+i;
		pricingArgValue = $('option'+i).value;
		pricingArgs[(i+3)][1] = pricingArgValue.replace(/ & /i," [amp] ");
	}
	
	//Reload option drop downs if nessicary 
	var dropDownData = runAJAXsync('get_dropdowns',pricingArgs);
	
	//Get New Drop Downs
	var newDropDowns = dropDownData.substr( 0, dropDownData.indexOf('[DROP]') );
	
	//Get Item Dsc Sm Drop
	var itemDscDrop = dropDownData.substr( dropDownData.indexOf('[DROP]')+6, (dropDownData.indexOf('[ITEMID]') - (dropDownData.indexOf('[DROP]')+6)) );
	
	//Load new item id
	var itemID = dropDownData.substr( dropDownData.indexOf('[ITEMID]')+8 );
	$('itemID').value = itemID;
	
	//$('testDiv').innerHTML = newDropDowns;
	
	// Get item option drop downs
	itemOptionArgs = new2DArray(1,2);
	itemOptionArgs[0][0] = "item_id";
	itemOptionArgs[0][1] = itemID;
	
	//Load new drop downs
	if( $('optionDropDowns') && itemID != "NA" ){
		// Load sku option drop downs
		if( newDropDowns.length > 6 ){
			$('optionDropDowns').innerHTML = newDropDowns;
		}
		
		// Load item dsc drop down if it exists
		if( itemDscDrop.length > 6 ){
			$('itemDscDrop').innerHTML = itemDscDrop;
		} else {
			$('itemDscDrop').innerHTML = "";
		}
	
		// Get item option drop downs
		var itemOptions = runAJAXsync('get_dropdowns',itemOptionArgs);
		
		// Get item option drop downs
		var numItemOptions = itemOptions.substr( itemOptions.indexOf('[DROPS]')+7 );
		
		// Load item option drop downs if they exist
		$('numItemOptions').value = numItemOptions;
		if( eval(numItemOptions) > 0 ){
			$('itemOptionDropDowns').innerHTML = itemOptions.substr(0,itemOptions.indexOf('[DROPS]'));
		} else {
			$('itemOptionDropDowns').innerHTML = "";	
		}
		
	}
	
	// Get Pricing
	if( itemID == "NA" ){
		$('itemPriceDisplay').innerHTML = "Item Combo Unavailable";
	} else {
		getPricingID();
	}
}


function getPricingID(){
	if( $('itemDescDrop') ){
		var itemID = $('itemDescDrop').value
		$('itemID').value = itemID;
	} else {
		var itemID = $('itemID').value;
	}

	// Get item option drop downs
	getPriceArg = new2DArray(1,2);
	getPriceArg[0][0] = "item_id";
	getPriceArg[0][1] = itemID;
	
	$('itemPriceDisplay').innerHTML = "<img src='/images/graphics/page-loading.gif' /><br /><br />Loading...";
	
	//Run ajax and get pricing
	var priceData = runAJAXsync('get_pricingID',getPriceArg);
	var imageData = runAJAXsync('get_item_image',getPriceArg);
	
	//Set up price display
	if( priceData.length > 2 ){
		
		//Load price and display number
		$('displayNum').innerHTML = priceData.substr(0, priceData.indexOf('[PRICING]') );
		$('itemPriceDisplay').innerHTML =  priceData.substr( priceData.indexOf('[PRICING]')+9, (priceData.indexOf('[SHIP]') - (priceData.indexOf('[PRICING]')+9)) );
		
		//Set Min Order Qty in the qty field
		$('prodQty').value = $('minOrderQty').value;
		
		//Hide or show fee shipping tag
		var freeship = priceData.substr( priceData.indexOf('[SHIP]')+6, (priceData.indexOf('[NAME]') - (priceData.indexOf('[SHIP]')+6)) );
		if( freeship == "YES" ){
			$('freeShipItem').style.display = "inline";
		} else {
			$('freeShipItem').style.display = "none";
		}
		
		//Load item name
		$('itemName').innerHTML = priceData.substr( priceData.indexOf('[NAME]')+6 );
	}
	
	//Set up new image
	if( imageData.length > 6 ){
		$('main_image').src = imageData;
		$('popup_image').href= imageData;
		$('notActualImage').style.display = "none";
	} else {
		$('notActualImage').style.display = "block";
	}
	
}


// Desc: Add item to cart by sending an AJAX call to add_to_cart.php
function ajaxAddToCart(){
	var returnData = "";
	var num_options = $('numItemOptions').value;
	var qty = eval($('prodQty').value);	
	var minOrderQty = $('minOrderQty').value;
	var orderIncrement = $('orderIncrement').value;
	var item_sku = $('itemID').value;
	
	if( isNumeric(qty) && qty > 0 ){
		//Set up arguments array
		cartArgs = new2DArray((eval(3)+eval(num_options)),2);
		cartArgs[0][0] = "item_sku";
		cartArgs[0][1] = item_sku;
		cartArgs[1][0] = "qty";
		cartArgs[1][1] = qty;
		cartArgs[2][0] = "num_options";
		cartArgs[2][1] = num_options;
	
		//Set up options
		for(var i=0;i<num_options;i++){
			cartArgs[(i+3)][0] = "option"+i;
			cartArgs[(i+3)][1] = $('itemOption'+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 if( item_sku == "NA" ){
			alert("Item Combo Unavailable, call for assistance.");
		}else{
			//Run AJAX Call
			returnData = runAJAXsync('add_to_cartB',cartArgs);
			
			//Print success if complete error if failed
			//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);
}

function addToCartList(num) {
	var returnData = "";
	var num_options = $('numItemOptions'+num).value;
	var qty = eval($('prodQty'+num).value);	
	var minOrderQty = $('minOrderQty'+num).value;
	var orderIncrement = $('orderIncrement'+num).value;
	var item_sku = $('itemID'+num).value;
	
	if( isNumeric(qty) && qty > 0 ){
		//Set up arguments array
		cartArgs = new2DArray((eval(3)+eval(num_options)),2);
		cartArgs[0][0] = "item_sku";
		cartArgs[0][1] = item_sku;
		cartArgs[1][0] = "qty";
		cartArgs[1][1] = qty;
		cartArgs[2][0] = "num_options";
		cartArgs[2][1] = num_options;
	
		//Set up options
		for(var i=0;i<num_options;i++){
			cartArgs[(i+3)][0] = "option"+i;
			cartArgs[(i+3)][1] = $('item'+num+'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 if( item_sku == "NA" ){
			alert("Item Combo Unavailable, call for assistance.");
		}else{
			//Run AJAX Call
			returnData = runAJAXsync('add_to_cartB',cartArgs);
			
			//Print success if complete error if failed
			if( returnData != "ERROR" || returnData != "" ){
				$('addToCart'+num).src = "/images/graphics/itemAddedList-btn.gif";
			} 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.");
	}
}
/////////////////////////////////////////////////////////////////////////
// END GET PRICING AND ADD TO CART FUNCTIONS
/////////////////////////////////////////////////////////////////////////


