function login(url)
{
	f = document.login;

	utype = f.utype[0].checked;
	if (utype)
	{
		f.page.value = "login";
		f.login.value = "1";
		f.submit();
	}
	else
	{
		email = f.email.value;
		wcheck = f.wcheck.value;

		url = url + "/?page=register&email=" + email;
		if (wcheck == 1)
		{
			url = url + "&wcheck=1";
		}

		location.href = url;
	}
}

function jump_category(type)
{
	f = document.category;

	if (type == 1)
	{
		if (f.cps)
		{
			f.cps.selectedIndex = 0
		}
	}

	f.submit();
}

function jump_product_size(obj, url, pid, vmm)
{
	psid = obj.value;
	url = url + "?page=product&pid=" + pid + "&psid=" + psid + "&vmm=" + vmm;
	location.href = url;
}

function jump_topseller(obj, url)
{
	tsintv = obj.value;
	url = url + "?page=topseller&tsintv=" + tsintv;
	location.href = url;
}

function wish_update()
{
	f = document.wishlist;
	f.cmd.value = "update";
	f.submit();
}

function wish_delete(wid)
{
	f = document.wishlist;

	if (confirm("Are you sure to delete this item from your wish list?"))
	{
		f.wid.value = wid;
		f.cmd.value = "delete";
		f.submit();
	}
}

function wish_to_cart(wid)
{
	f = document.wishlist;
	f.page.value = "cart";
	f.wid.value = wid;
	f.cmd.value = "addwish";
	f.submit();
}

function cart_add()
{
	f = document.product;

	var tc = product_qty_total();
	if (tc <= 0)
	{
		alert("Choose the item!");
		return;
	}
	else
	{
		f.method = "post";
		f.page.value = "cart";
		f.cmd.value = "add";
		f.submit();
	}
}

function cart_delete(duid)
{
	f = document.cart;

	if (confirm("Are you sure to delete this item from your cart?"))
	{
		f.cmd.value = "delete";
		f.duid.value = duid;
		f.submit();
	}
}

function cart_update()
{
	f = document.cart;
	f.cmd.value = "update";
	f.submit();
}

// change line price total
function product_change_line_price(obj, code)
{
	var qty = obj.value * 1;
	var ll = code + "_lprice";
	var llayer = document.getElementById(ll);
	var ltotal = qty * lprice;
	llayer.innerHTML = number_format2(ltotal);

	product_change_total();
}

function product_qty_total()
{
	f = document.product;

	var tc = 0;
	for(i = 0; i < f.length; i++)
	{
		var fname = f[i].name;
		if (fname.indexOf("qty__") != -1)
		{
			tc += f[i].value * 1;
		}
	}

	return tc;
}

// change total
function product_change_total()
{
	var tc = product_qty_total();
	var tprice = lprice * tc;

	// chanage total price
	layer_t_price.innerHTML = number_format2(tprice);
}

// checkout1
function cart_checkout()
{
	f = document.cart;
	f.cmd.value = "checkout";
	f.submit();
}

function is_usecredit()
{
	f = document.cart;
	if (f.ausecredit && f.ausecredit.value == 1) return true;
	else
	{
		if (f.usecredit && f.usecredit[0] && f.usecredit[0].checked) return true;
		else return false;
	}
}

function update_credit()
{
	f = document.cart;
	var usecredit = false;
	if (is_usecredit()) usecredit = true;
	else f.store_credit.value = f.o_credit.value;

	f.store_credit.disabled = !usecredit;

	cart_change_total();
}

function update_store_credit()
{
	f = document.cart;
	var o_credit = f.o_credit.value * 1;
	var store_credit = f.store_credit.value * 1;

	if (is_usecredit())
	{
		n_credit = o_credit - store_credit;
		if (n_credit <= 0 || !is_number(store_credit))
		{
			n_credit = 0;
			f.store_credit.value = o_credit;
		}

		cart_change_total();
	}
}

function cart_change_total()
{
	f = document.cart;
	
	var st = f.stotal.value * 1; // subtotal
	var cdc = f.cdtotal.value * 1; // coupon discount
	var credit = 0; // store credit
	var tax = 0;
	var sr = 0; // shipping amount
	var si = 0; // shipping insure
	var ngt = 0; // new grand total

	if (f.store_credit) credit = f.store_credit.value * 1;
	if (f.tax) tax = f.tax.value * 1;
	if (f.shinsuer) si = f.shinsuer.value * 1;
	
	if (f.sm)
	{
		// find index
		srn = find_radio_value(f.sm);

		if (srn == 0) {
			sr = 0;
		} else {
			sr = find_value(f, srn);
			sr = sr * 1;
		}

		// update shipping value
		if (f.shamount) {
			f.shamount.value = sr;
		}

		if (f.shipping_amount) {
			f.shipping_amount.value = '$' + number_format(sr);
		}

		if (f.isinsure && f.isinsure.checked) {
			sr = sr + si;
			if (f.shtmp) {
				f.shtmp.value = '$' + number_format(si);
			}
		} else {
			if (f.shtmp) {
				f.shtmp.value = '$' + number_format(0);
			}
		}
	}

	ngt = st - cdc;
	if (is_usecredit()) {
		ngt = ngt - credit;
		if (ngt <= 0) {
			ngt = 0;
			f.store_credit.value = Math.round((st - cdc) * 100) / 100;
		}
	}

	ngt = ngt + sr + tax;

	f.gtotal.value = "$" + number_format(ngt);
}

function delete_credit()
{
	f = document.cart;
	f.page.value = "cart";
	f.cmd.value = "deletecredit";
	f.submit();
}

function update_shipping(value)
{
	f = document.cart;

	if (f.sm)
	{
		for (i = 0; i < f.sm.length; i++)
		{
			if (f.sm[i].value == value)
			{
				f.sm[i].checked = true;
				cart_change_total();
				break;
			}
		}
	}
}

function checkout()
{
	f = document.cart;
	f.page.value = "cart";
	f.cmd.value = "checkout";
	f.submit();
}

function checkout2()
{
	f = document.cart;
	btn = document.getElementById('btn_chkout');
	btn.style.visibility = 'hidden';
	f.page.value = "checkout";
	f.cmd.value = "checkout2";
	f.submit();
}

function place_order()
{
	f = document.cart;
//	f.btn_place.style.display = "none";
	f.page.value = "checkout2";
	f.cmd.value = "placeorder";
	btn = document.getElementById('placeorder');
	btn.style.visibility = 'hidden';

	if (confirm("Please click 'OK' to place order.\nDon't click twice 'Place Order' button."))
	{
		f.submit();
	}else{
		btn.style.visibility = '';
	}
}

function contact_us(f)
{
	// check values
	if (!f.first_name.value) alert("Enter the first name");
	else if (!f.last_name.value) alert("Enter the last name");
	else if (!f.phone.value) alert("Enter the day-time phone number");
	else if (!f.email.value) alert("Enter the e-mail address");
	else if (!f.comments.value) alert("Ente the comments");
	else return true;

	return false;
}

function on_keyword()
{
	document.search.keyword.value = "";
}

function coupon_update() {
	var f = document.cart;
	var couponcode = f.couponcode.value;
	if (!couponcode) {
		f.cmd.value = "deletecoupon"
	} else {
		f.cmd.value = "updatecoupon";
	}

	f.submit();
}