var ShoppingCart = {
	
	updateCartQty : {
		url : '',
		json : new JsonRequest('ShoppingCart.updateCartQty.json', new Function('data','ShoppingCart.updateCartQty.callback(data)')),
		timers : {},
		temp : {},
		
		
		callback : function(d){
			setTimeout("$('qtyInput"+id+"').style.backgroundColor = '#fff';",500);
			ShoppingCart.recalculateTotal();
		},
	
		save : function(id,el){
			el.value = String(el.value).replace(/[^0-9]/,"");
			el.value = String(el.value).replace(/^0/,"");
			if (!el.value) el.value = 0;
			clearTimeout(this.timers[id]);
			this.timers[id] = setTimeout("$('qtyInput"+id+"').style.backgroundColor = '#FFA'; ShoppingCart.updateCartQty.json.makeRequest('" + this.url + '?id=' + id + '&quantity=' + el.value +"')",300);
		}
	
	},
	
	recalculateTotal : function(){
		if (ShoppingCart.cartData){
			var t = 0;
			for (i in this.cartData) {
				t += this.cartData[i]['price'] * document.cartForm.elements['qty[' + i + ']'].value;
			}
			
			var discountContainer = document.getElementById('discountContainer');
			if (discountContainer) {
				var discounts = discountContainer.getElementsByTagName('span');
				for (var i = 0; i < discounts.length; i++) {
					if (discounts[i].className.match(/amount/) && (amount = parseFloat(discounts[i].innerHTML))) {
						t += amount;
					}
				}
			}
			
			$('totalPrice').innerHTML = dollars(t);
		}
	},
	
	loginCheck : function(f){ 
		msg = '';
		eColor = '#FFA'
		fe = f['login[details][email]'];
		fp = f['login[password]'];
		if (fe){
			if (fe.value.length < 3){
				msg += 'Please enter your email address.\n';
				if (fe.value.length < 3) fe.style.background = eColor;
			}
			else fe.style.background = 'none';
		}
		if (fp){
			if (fp.value.length < 4){
				msg += 'Please enter your password.\n';
				fp.style.background = eColor;
			}
			else fp.style.background = 'none';
		}
		if (msg){
			alert('FORM ERROR:\n' + msg)
			return false;
		}
		else return true;
	}
	
		
};

function dollars(n){
    n = eval(n);
    n = Math.round(n*100)/100;
    return  (n == Math.round(n)) ? n += ".00":
            (n*10 == Math.round(n*10)) ? n += "0":
            n;
}


// generic form validation function
var formCheck={check:function(f){var r=true;var u,t,msg='',pwd,pwdMin=4,pwdMax=20,eColor='#FFA';for(i=0;i<f.elements.length;i++){el=f.elements[i];check=el.getAttribute('check');t=-1;if(!check)continue;else if(check=='email'&&!this.isEmail(el.value))t='Email address is not valid';else if(parseInt(check)==check&&el.value.length<check){t=u?'':'Please fill in all fields';u=true}else if(check=='pwd'&&!pwd&&(el.value.length<pwdMin||el.value.length>pwdMax))t='Password must be between '+pwdMin+' and '+pwdMax+' characters';else if(check=='pwd'&&!pwd)pwd=el.value;else if(check=='pwd'){if(el.value!=pwd){t='Password fields do not match'}pwd=''}if(t!=-1){msg+=t?t+'\n':'';el.style.background=eColor;r=false}else el.style.background='none'}if(msg)alert('FORM ERROR:\n'+msg);return r},isEmail:function(m){var filter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;if(filter.test(m))return true;return false},isNum:function(a){return typeof a=='number'&&isFinite(a)}};



