var blindup_to;
function showComment() {
	$('addComment').style.display = 'none';
	$('comment').style.display = 'block';
	$('cart_comment').focus();
}

function showMoreInfo(more_dot) {
	$('moreinfo').style.top = (more_dot.offsetTop - 0 + 10) +'px';
	$('moreinfo').style.left = (more_dot.offsetLeft - 0 + 20)+'px';
	$('moreinfo').style.display = 'block';
	$('moreinfo').style.zIndex = 20;
}

function hideMoreInfo() {
	$('moreinfo').style.display = 'none';
}

function setComment() {
	var parms = "cart_comment="+encodeURI($('cart_comment').value);
	var url = "http://"+location.host+"/setcomment.php";
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: parms 
		});
}

function get_discount(input){
	var params = "code="+encodeURI($F(input));
    var url = "http://"+location.host+"/check_discount.php";
	var myAjax = new Ajax.Updater(
	    'total-data',
		url,
		{
			method:'get',
			parameters: params,
			evalScripts:1
		}
	);
}

function remove_discount(){
    var url = "http://"+location.host+"/remove_discount.php";
    var myAjax = new Ajax.Updater(
        'total-data',
    	url,
    	{
    		method:'get',
    		evalScripts:1
    	}
    );
}

function submit_newsletter(tlink, tform) {
	//$(tform).disable();
	//$(tlink).disable();
	//if($F('email').isValidEmail()) {
		var params = "email=" + $F('email');
		var url = "http://"+location.host+"/newsletter.php"
		var myUpdater = new Ajax.Updater(
			'nl_content',
			url,
			{
				method: 'get',
                parameters: params,
				evalScripts:1
			});
		return false;
	//} else {
	//	$(tform).enable();
	//	$(tlink).enable();
	//	$F('email').focus();
	//	alert("You must supply a valid email address.");
	//	return false;
	//}
}

function showShipping() {
	$('shipping').style.display = 'block';
}

function hideShipping() {
	$('shipping').style.display = 'none';
}

function getItem(itemnumber) {
	var url = "http://"+location.host+"/item.php";
	var queryString = location.href.substring(location.href.indexOf("?")+1);
	queryString = queryString.replace(/\&item=[^\&]+/, '');
	queryString = queryString.replace(/\&pid=[^\&]+/, '');
	var parms = queryString+"&item="+itemnumber;
	var myAjax = new Ajax.Updater(
		'itemDetail',
		url, 
		{
			method: 'get', 
			parameters: parms 
		});
	Element.scrollTo('head');
}

function updateCart(itemnum, quantity) {
	var parms;
	if(itemnum > 0) {
		parms = "add="+itemnum;
		if(quantity > 1) {
			parms += "&q=" + quantity;
		}
		if($('cartAdding')){
			$('cartAdding').innerHTML = 'Adding Item&nbsp;<img src="/images/spinner.gif" width="12" height="12" alt="Loading" />';
		}
	} else if(itemnum < 0) {
		if(confirm("Are you sure you want to empty the cart.")){
			parms = "empty=1";				
		}
	}
	Element.scrollTo('head');
	var url = "http://"+location.host+"/cart.php";
	var myAjax = new Ajax.Updater(
		'cart', 
		url, 
		{	method: 'get', 
			parameters: parms,
			evalScripts: 1});
}

function quantityMinus() {
	var qu = $('itemDetailQuantity').innerHTML;
	if(qu > 1) {
		$('itemDetailQuantity').innerHTML = qu - 1;
	} 
}

function quantityPlus() {
	var qu = $('itemDetailQuantity').innerHTML;
	if(qu == 99) {
		alert('You cannot order more than 99 items to the cart at a time. If you are interested in bulk or wholesale orders please contact sales@baublebath.com.\nThanks,\nSarah :)');
	} else {
		$('itemDetailQuantity').innerHTML = (qu -1) + 2;
	}
}

function reviewCart(){
	Element.scrollTo('head');
	var parms = "review=1";
	var url = "http://"+location.host+"/cart.php";
//	new Effect.SlideDown('cartReview');
	new Effect.Fade('cart', {duration: .5});
	$('cartReview').style.zIndex = '100';
	new Effect.Appear('cartReview', { duration: .75 });
	$('diableLayer').style.zIndex = '99';
	$('diableLayer').height = '100%';
	new Effect.Appear('diableLayer', { duration: .5, from: 0.0, to: 0.75});
	
	
	var myAjax = new Ajax.Updater(
		'cartReview', 
		url, 
		{	method: 'get', 
			parameters: parms,
			evalScripts: 1});
}

function moveLayersBack() {
	$('diableLayer').style.zIndex = '-1';
	$('cartReview').style.zIndex = '-1';		
}

function closeCart() {
		Element.scrollTo('head');
		var url = "http://"+location.host+"/cart.php";
		new Effect.Fade('cartReview', {duration: .5});
		new Effect.Fade('diableLayer', { duration: .5, from: 0.75, to: 0.0});
		new Effect.Appear('cart', { duration: .5 });
		setTimeout('1000', 'moveLayersBack');
		var myAjax = new Ajax.Updater(
			'cart', 
			url, 
			{	method: 'get', 
				parameters: '',
				evalScripts: 1});
	
}

function checkout(which) {
	var checkouts = { google:"Google Checkout", paypal: "PayPal Checkout" };
	if(!checkouts[which]) {
		return false;
	}
	$('cartCheckout').hide();
	var url = "http://"+location.host+"/process_"+which+".php";
	if(which == 'google'){
    	var myAjax = new Ajax.Updater(
    		'cartCheckout', 
    		url, 
    		{	
    			method: 'get', 
    			parameters: '',
    			evalScripts: 1
    		}
    	);
	} else {
         new Ajax.Request(url);
	}
	$('cartReview').hide();
	$('modal-message').innerHTML = 'Opening ' + checkouts[which] + '&nbsp;<img src="/images/spinner.gif" width="24" height="24" alt="Loading" />';
	$('modal-message').show();
	return false;
}

function removeItem(id) {
	if(!confirm("Contunue to remove item?")) {
		return false;
	}
	var parms = "review=1&remove=1&item="+id;
	$('cartReview').innerHTML = 'Updateing cart&nbsp;<img src="/images/spinner.gif" width="22" height="22" alt="Loading" />';
	var url = "http://"+location.host+"/cart.php";
	var myAjax = new Ajax.Updater(
		'cartReview', 
		url, 
		{	method: 'get', 
			parameters: parms,
			evalScripts: 1
		});
}

function emptyCart(){
	if(!confirm("Continuing will remove all items?")){
		return false;
	}
	var parms = "review=1&empty=1";
	$('cartReview').innerHTML = 'Removing All Items&nbsp;<img src="/images/spinner.gif" width="20" height="20" alt="Loading" />';
	var url = "http://"+location.host+"/cart.php";
	var myAjax = new Ajax.Updater(
		'cartReview', 
		url, 
		{	method: 'get', 
			parameters: parms,
			evalScripts: 1
		});
}

function onemore(id){
	var qty = $('cIRQ_'+id).innerHTML;
	if(qty < 99) {
		var price = $('cIRP_'+id).innerHTML.replace('$', '');
		var dc = price.split('.');
	        var dollars = new Number(dc[0]);
	        var cents = new Number(dc[1]);
		qty++;
		$('cIRQ_'+id).innerHTML = qty;
		var total = (qty * dollars)+((qty * cents)/100);
		$('cIRS_'+id).innerHTML = '$' + outputDollars(total);
		//$('total-data').innerHTML = 'Updating total <img src="/images/spinner.gif" width="16" height="16" alt="Loading" />';
		var parms = "change=1&item="+id;
		var url = "http://"+location.host+"/update_count.php";
		var myAjax = new Ajax.Updater(
			'total-data', 
			url, 
			{	method: 'get', 
				parameters: parms,
				evalScripts: 1
			});
	} else {
		alert("If you want more than 99 of one items you must contact sarah@baublebath.com");
	}
}

function oneless(id){
	var qty = $('cIRQ_'+id).innerHTML;

	if(qty == 1) {
		removeItem(id);
		return false;
	}
	var price = $('cIRP_'+id).innerHTML.replace('$', '');
	var dc = price.split('.');
	var dollars = new Number(dc[0]);
	var cents = new Number(dc[1]);
	qty--;
	$('cIRQ_'+id).innerHTML = qty;
	var total = (qty * dollars)+((qty * cents)/100);
	$('cIRS_'+id).innerHTML = '$' + outputDollars(total);
	//$('total-data').innerHTML = 'Updating total <img src="/images/spinner.gif" width="16" height="16" alt="Loading" />';
	var parms = "change=-1&item="+id;
	var url = "http://"+location.host+"/update_count.php";
	var myAjax = new Ajax.Updater(
		'total-data', 
		url, 
		{	method: 'get', 
			parameters: parms,
			evalScripts: 1
		});
}

function outputDollars(num) {
	var mynum = new String(num);
	var dc = mynum.split('.');
	var dollars = new String(dc[0]);
	var cents = new String(dc[1]);
	if(cents == 'undefined'){
		return dollars+".00";
	} else if(cents.length == 1) {
		cents = cents + '0';
   	}
	return dollars+'.'+cents;
}
