function getElem(id) {
	if(document.getElementById)
		return document.getElementById(id);
	else if(document.all)
		return document.all[id];
	else if(document.layers)
		return document.layers[id];
}


 /**
  * Processes the plugins grid and adds the products to the cart.
  */
 function AddPluginsToCart(addToCartAnchorId) {
	 //alert('adding plugins');
	var pluginForm = getElem('plugin_add_form');
	var added = false;
	//alert('num plugins '+pluginForm.elements.length);
	var checked = new Array();
	var ctr = 0;
	for(i=0; i<pluginForm.elements.length; i++) {
		if (pluginForm.elements[i].checked == true) {
			//alert(pluginForm.elements[i].name+' is checked');
			var pluginId = getStringAfterChar(pluginForm.elements[i].name, '_');
			checked[ctr] = pluginId;
			ctr++;
			//AddProductToCart(pluginId, '', addToCartAnchorId);
			added = true;
		} else {
			//alert(pluginForm.elements[i].name+' is not checked');
		}
	}
	//alert('found '+checked.length);
	var params = 'pluginIds=';
	if (checked.length > 0) {
		for (j=0; j<checked.length; j++) {
			if (j != 0) params += '_';
			params += checked[j];
		}
			//alert('sending to the responder '+params);
			
		window.location="/shopping_cart/responders/add_plugins_to_cart.php?" + params;
		
	  /*
		new Ajax.Updater('cart_sidebar', '/shopping_cart/responders/add_plugins_to_cart.php', {
		  evalScripts: true,	
		  postBody: params
		  
		});
		*/
	
	
	
	//we only want to highlight once, so we manually do the highlighting
	//right here
		if (added == true) {
			/*
			var addAnchor = getElem(addToCartAnchorId);
			addAnchor.removeAttribute('onclick');
			setTimeout(function(){addAnchor.setAttribute('onclick', "AddPluginsToCart('add_plugins_to_cart_link')");},1000);
			new Effect.Highlight('plugin_add_form');
			*/
			//window.location = "/checkout"
		}
	}
 }



 /**
  * Processes the plugins grid and adds the products to the cart.
  */
/*
function AddPluginsToCart(addToCartAnchorId) {
	 alert('adding plugins');
	var pluginForm = getElem('plugin_add_form');
	var added = false;
	alert('num plugins '+pluginForm.elements.length);
	for(i=0; i<pluginForm.elements.length; i++) {
		if (pluginForm.elements[i].checked == true) {
			alert(pluginForm.elements[i].name+' is checked');
			var pluginId = getStringAfterChar(pluginForm.elements[i].name, '_');
			AddProductToCart(pluginId, '', addToCartAnchorId);
			added = true;
		} else {
			alert(pluginForm.elements[i].name+' is not checked');
		}
	}
	//we only want to highlight once, so we manually do the highlighting
	//right here
	if (added == true) {
		var addAnchor = getElem(addToCartAnchorId);
    	addAnchor.removeAttribute('onclick');
		setTimeout(function(){addAnchor.setAttribute('onclick', "AddPluginsToCart('add_plugins_to_cart_link')");},1000);
		new Effect.Highlight('plugin_add_form');
	} 
 }
 */
 
 
 /**
  * Helper function for AddPluginsToCart
  */
 function getStringAfterChar(str, char) {
		var idx = str.indexOf(char);
		return str.substring((idx+1));
 }
 
/**
 * Adds product to cart
 * productId is the database Id of the product to be added
 * productDivId is the optional html id of the div to highlight after
 * it has been added
 * addToCartAnchorId is the anchor whose onClick calls this function.  we keep track of it
 * so that we can temporarily disable the anchor to prevent double clicks.
 *
 */
function AddProductToCart(productId, productDivId, addToCartAnchorId) {
	var params = 'productId=' + productId;
	if (productDivId != '') params = params +'&productDivId='+productDivId;
	if (addToCartAnchorId != '') params = params + '&addToCartAnchorId='+addToCartAnchorId;
	params = params + '&refer=' + window.location;
	
	new Ajax.Request('/shopping_cart/responders/add_product_to_cart.php',{
		onSuccess: function doIt() {
			window.location = "/checkout";
		},
		postBody: params
	});
	
	
	//new Ajax.Request('/shopping_cart/responders/add_product_to_cart.php',{});
	
	/*
	new Ajax.Updater('cart_sidebar', '/shopping_cart/responders/add_product_to_cart.php', {
	  evalScripts: true,	
	  postBody: params
	  
	});
	*/
	
}

/**
 * Removes a product from the cart.
 *
 */
function RemoveProductFromCart(productIdx) {
	var params = 'productIdx=' + productIdx;
	/*
	new Ajax.Updater('cart_sidebar', '/shopping_cart/responders/remove_product_from_cart.php', {
	  evalScripts: true, 
	  postBody: params
	});
	*/
	new Ajax.Request('/shopping_cart/responders/remove_product_from_cart.php',{ postBody:params });
	new Ajax.Updater('cart_display','/shopping_cart/responders/update_cart_review_display.php',{ postBody:params });
}

/**
 * Removes all items from the cart and updates the appropriate displays.
 */
function EmptyCart() {
	new Ajax.Updater('cart_sidebar', '/shopping_cart/responders/empty_cart.php', {
	  evalScripts: true
	});
	//if we are viewing the cart, we need to update that display as well
	var display = getElem('cart_display');
	if (null != display) {
		new Ajax.Updater('cart_display', '/shopping_cart/responders/update_cart_review_display.php', {
			evalScripts: true
		});
	}
}


/**
 * This function updates the AddToCart() call when the user types in
 * a quantity. 
 */
function UpdateMultiUserAddLink(skuId) {
	var numSeats = getElem('num_seats_'+skuId).value;
	if (numSeats == '') numSeats = null;
	AddProductToCart.qty = numSeats;
}

/**
 * Not currently used.  When/if e-check payment method is enabled, this would show and hide
 * the CC or e-check info, depending on which way the user wanted to pay.
 */
function ShowPayInfo(payType) {
	var ccSet = getElem('credit_card_info');
	var ecSet = getElem('e_check_info');
	var sRow = getElem('submit_row');
	var ccStyle = 'none';
	var ecStyle = 'none';
	if(payType == 'credit_card')
		ccStyle = '';
	else
		ecStyle = '';
	ccSet.style.display = ccStyle;
	ecSet.style.display = ecStyle;
	sRow.style.display = '';
}


/**
 * Used to copy the customer's info to the Billing fields.
 */
function CopyBilling(cBox,payType) {
	if(!cBox.checked)
		return;
	var fKeys = new Array('address','city','state','zip');
	for(var i = 0; i < fKeys.length; i++) {
		var fVal = billInfo[fKeys[i]];
		var elem = getElem(payType + '_' + fKeys[i]);
		if(fKeys[i] == 'state')
			SetMenuOption(elem,fVal);
		else
			elem.value = fVal;
	}
}

/**
 * Helper for the CopyBilling function.
 */
function SetMenuOption(menu,value) {
	for(var j = 0; j < menu.options.length; j++) {
		if(menu.options[j].value == value) {
			menu.options[j].selected = true;
			return;
		}
	}
}


// Not used for now.
function isNumberKey(evt) {
	 var charCode = (evt.which) ? evt.which : event.keyCode
	 if (charCode > 31 && (charCode < 48 || charCode > 57))
		alert('The character you just entered is not a number.  Only numbers are allowed here.');
 }
 
