var Cart = {
	msg:[
		 'Item added to cart.',
		 'Please type the quantity',
		 'Please type a number',
		 'Please type a number greater than zero',
		 'Please choose items to add to the cart',
		 'Quantities updated.',
		 'Please choose the ',
		 'Please choose items to add to the wishlist',
		 'Item added to your wishlist.',
		 'Please type the '
		 ],
	img: [],
	uANames: [], aNames: [],
	
	add: function(id, f, wl) {
		var wl = wl || false;
		this.notify('');
		var qty = document.getElementsByName('qty['+id+']')[0].value;
		var model_id = document.getElementsByName('model_id['+id+']')[0].value;
		var sel = f.getElementsByTagName('select');
		var tel = f.getElementsByTagName('textarea');
		if (!qty.length)
			this.notify(id, this.msg[1], 'err');
		else if (isNaN(qty)) {
			this.notify(id, this.msg[2], 'err');
		} else if (parseInt(qty) <= 0) {
			this.notify(id, this.msg[3], 'err');
		} else {
			var s = '';
			for (var j = 0; j < sel.length; j++) {
				var Re = new RegExp("ua_attributes\\["+id+"\\]\\[(.*)\\]");
				var a = Re.exec(sel[j].name);
				if (a) {
					var aid = a[1];
					if (!sel[j].value.length || !sel[j].value) {
						this.notify(id, this.msg[6] + this.uANames[aid], 'err');
						return false;
					}
					s += '&' + (sel[j].name) + '=' + sel[j].value;
				}
				var Re = new RegExp("addons\\["+id+"\\]\\[\\]");
				if (Re.test(sel[j].name))
					s += '&' + (sel[j].name) + '=' + sel[j].value;
			}
			for (var j = 0; j < tel.length; j++) {
				var Re = new RegExp("utext\\["+id+"\\]\\[(.*)\\]");
				var a = Re.exec(tel[j].name);
				if (a) {
					var aid = a[1];
					if (!tel[j].value.length || !tel[j].value) {
						this.notify(id, this.msg[9] + this.aNames[aid], 'err');
						return false;
					}
					s += '&' + (tel[j].name) + '=' + tel[j].value;
				}
			}
			eval("new Ajax.Request('/cart/add.req.php', {method:'post', postBody:'model_id[0]='+model_id+'&qty[0]='+qty+s+'&wl='+(wl?1:0), onComplete:function(t) { Cart.added("+id+", t.responseText); }});");
		}
	},
	
	addAll: function(wl, legacy) {
		var wl = wl || false;
		var legacy = legacy || false;
		var e = $('mod_cont');
		var el = e.getElementsByTagName('input');
		var sel = e.getElementsByTagName('select');
		var tel = e.getElementsByTagName('textarea');
		var s = '';
		var ids = [];
		for (var i = 0; i < el.length; i++) {
			//alert(el[i].name);
			if (el[i].name.length && el[i].value.length && el[i].name.substr(0, 3) == 'qty' && (a = /^qty\[(\w+)\]$/.exec(el[i].name)).length > 1) {
				var id = a[1];
				var qty = el[i].value;
				this.notify(id, 0);
				if (isNaN(qty)) {
					this.notify(id, this.msg[2], 'err');
					el[i].focus();
					return false;
				} else if (parseInt(qty) <= 0) {
					this.notify(id, this.msg[3], 'err');
					el[i].focus();
					return false;
				} else {
					for (var j = 0; j < sel.length; j++) {
						var Re = new RegExp("ua_attributes\\["+id+"\\]\\[(.*)\\]");
						var a = Re.exec(sel[j].name);
						if (a) {
							var aid = a[1];
							if (!sel[j].value.length || !sel[j].value) {
								this.notify(id, this.msg[6] + this.uANames[aid], 'err');
								return false;
							}
							s += '&' + (sel[j].name) + '=' + sel[j].value;
						}
						var Re = new RegExp("addons\\["+id+"\\]\\[\\]");
						if (Re.test(sel[j].name))
							s += '&' + (sel[j].name) + '=' + sel[j].value;
						var Re = new RegExp("attributes\\["+id+"\\]\\[(34|60)\\]");
						var a = Re.exec(sel[j].name);
						if (a) {
							if (!sel[j].value.length || !sel[j].value) {
								this.notify(id, this.msg[6] + 'size', 'err');
								return false;
							}
						}
					}
					for (var j = 0; j < tel.length; j++) {
						var Re = new RegExp("utext\\["+id+"\\]\\[(.*)\\]");
						var a = Re.exec(tel[j].name);
						if (a) {
							var aid = a[1];
							if (!tel[j].value.length || !tel[j].value) {
								this.notify(id, this.msg[9] + this.aNames[aid], 'err');
								return false;
							}
							s += '&' + (tel[j].name) + '=' + tel[j].value;
						}
					}
					s += '&' + el[i].name + '=' + qty;
					s += '&model_id['+id+']=' + document.getElementsByName('model_id['+id+']')[0].value
					ids.push(id);
				}
			}
		}
		if (!ids.length) {
			this.notify('', this.msg[wl ? 7 : 4], 'err');
			return false;
		}
		else {
			qs_ = s.substr(1);
			ids = ids.join(',');
			if (!legacy)
				eval("new Ajax.Request('/cart/add.req.php', {method:'post', postBody:qs_ + '&wl=' + (wl ? '1':'0'), onComplete:function(t) { Cart.addedAll('"+ids+"', t.responseText, "+wl+"); }});");
			return true;
		}
	},
	
	setQty: function(wl) {
		var wl = wl || false;
		var e = $('cart_c');
		var el = e.getElementsByTagName('input');
		var s = '';
		var ids = [];
		for (var i = 0; i < el.length; i++)
			if (el[i].name.length && el[i].value.length && (a = /^qty\[(\d+)\]$/.exec(el[i].name)).length > 1) {
				var id = a[1];
				var qty = el[i].value;
				if (isNaN(qty)) {
					this.notify(id, this.msg[2], 'err');
					el[i].focus();
					return;
				} else if (parseInt(qty) <= 0) {
					this.notify(id, this.msg[3], 'err');
					el[i].focus();
					return;
				} else {
					s += '&' + (el[i].name) + '=' + el[i].value;
					ids.push(id);
				}
			}
		if (!ids.length)
			this.notify(0, this.msg[4], 'err');
		else {
			if ($('id_cont_ship_meth'))
				$('id_cont_ship_meth').innerHTML = '';
			qs_ = s.substr(1);
			ids = ids.join(',');
			eval("new Ajax.Request('/cart/update.req.php', {method:'post', postBody:qs_ + '&wl=' + (wl ? '1':'0'), onComplete:function(t) { if (t.responseText.length) alert(t.responseText); Cart.notify(0, Cart.msg[5]); Cart.update("+wl+"); }});");
		}
	},
	
	move: function(id, wl) {
		var wl = wl || false;
		if ($('id_cont_ship_meth'))
			$('id_cont_ship_meth').innerHTML = '';
		eval("new Ajax.Request('/cart/move.req.php', {method:'post', postBody:'item_id=' + id + '&wl=' + (wl?1:0), onComplete: function(t) { if (t.responseText.length) alert(t.responseText); Cart.update("+wl+"); }});");
	},
	
	remove: function(id, wl, inWl) {
		var wl = wl || false;
		var inWl = inWl || false;
		if ($('id_cont_ship_meth'))
			$('id_cont_ship_meth').innerHTML = '';
		eval("new Ajax.Request('/cart/remove.req.php', {method:'post', postBody:'item_id=' + id + '&wl=' + (wl?1:0), onComplete: function(t) { Cart.update("+wl+", "+inWl+"); }});");
	},
	
	added:function(id, t) {
		var qty = document.getElementsByName('qty['+id+']')[0];
		qty.value = '';
		this.notify(id, t);
		this.update();
	},
	
	addedAll:function(ids, t, wl) {
		var wl = wl || false;
		var a = ids.split(',');
		for (var i = 0; i < a.length; i++) {
			var qty = document.getElementsByName('qty['+a[i]+']')[0];
			qty.value = '';
			this.notify(a[i], this.msg[wl ? 8 : 0]);
		}
		this.notify('', t);
		this.update();
	},
	
	notify: function(id, m, c) {
		var c = c || '';
		var m = m || '';
		var o = $('mstatus_' + id);
		o.innerHTML = m;
		o.className = m ? ('mstatus ston ' + c) : 'mstatus';
		if (id == '') {
			var o = $('mstatus2_');
			if (o) {
				o.innerHTML = m;
				o.className = m ? ('mstatus ston ' + c) : 'mstatus';
			}
		}
		o = $('mstatustr_' + id);
		if (o)	o.style.display = m ? (navigator.userAgent.indexOf('MSIE') == -1?'table-row':'block') : 'none';
	},
	
	update:function(wl, inWl, clr, inCh) {
		var wl = wl || false;
		var inWl = inWl || false;
		if (typeof(clr) == 'undefined')
			clr = true;
		var inCh = inCh || false;
		if (clr && $('id_cont_ship_meth'))
			$('id_cont_ship_meth').innerHTML = '';
		new Ajax.Updater('small_cart_c', '/cart/top_small.req.php');
		new Ajax.Updater('big_cart_c', '/cart/box_cart.req.php?wl='+(wl ? 1 : 0)+'&inwl='+(inWl ? 1 : 0)+'&inch='+(inCh ? 1 : 0));
		if ($('cart_c') && !inWl)
			new Ajax.Updater('cart_c', '/cart/cart.req.php?wl='+(wl ? 1 : 0));
	},
	
	toggle:function(im) {
		var e = $('cart_toggle_c');	
		e.style.display = e.style.display == 'none' ? 'block' : 'none';
		im.src = this.img[e.style.display].src;
		setCookie('cart_display', e.style.display, 10);
	},
	
	infoE: false,	
	showInfo:function(t, e) {
		var e = e || window.event;
		if (!this.infoE) {
			this.infoE = document.createElement('div');
			this.infoE.className = 'cart_info_e';
			document.body.appendChild(this.infoE);
		}
		this.infoE.className = 'cart_info_e';
		this.infoE.innerHTML = t;
		this.moveInfo(e);
		this.infoE.style.display = 'block';
	},
	
	moveInfo:function(e) {
		var e = e || window.event;
		if (!this.infoE)
			this.showInfo('', e);
		this.infoE.style.left = (e.clientX - 100) + 'px';
		this.infoE.style.top = (e.clientY + 20 + (typeof(window.pageYOffset) == 'undefined' ? document.documentElement.scrollTop : window.pageYOffset)) + 'px';
	},
	
	hideInfo:function() {
		if (this.infoE)
			this.infoE.style.display = 'none';
	},
	
	pickFI: function(wl, inWl) {
		var wl = wl || false;
		var inWl = inWl || false;
		eval("new Ajax.Request('/cart/set_fi.req.php', {method:'post', postBody:'model_id=' + $('free_item_sel').value + '&wl=' + (wl?1:0), onComplete: function(t) { if (t.responseText.length) alert(t.responseText); Cart.update("+wl+", "+inWl+"); }});");
	},
	
	discDet: function(id, e) {
		var e = e || window.event;
		eval("new Ajax.Request('/req/disc_info.req.php?id='+id, { onComplete: function(t) { Cart.showInfo(t.responseText, {clientX:"+e.clientX+", clientY:"+e.clientY+"}); Cart.infoE.className='cart_info_e discinfo'; } } );");
		return false;
	}
}