function Join()
{
	 location.href = "join_form.php";
	//alert("Temporary unavailable.\nIt will be available soon.");
}

function join_Submit1()
{
	if( ValidateUserid())
		if( join_ValidateFields1())
			document.getElementById("frmMain").submit();
}

function join_ValidateFields1()
{
	bRetVal = false;

	if( ValidateLength('join_id','User id',3) )
		if( ValidateLength('join_pw','Password',3) )
			if( ValidateLength('confirm','Confirm password',3) )
				if( ValidateLength('fname','First Name',2) )
					if( ValidateLength('lname','Last Name',2) )
						if( ValidateLength('phone3','Area code', 2) )
							if( ValidateLength('phone','Telephone', 8) )
								if( ValidateLength('address','Address',5) )
									if( ValidateLength('suburb','Suburb',3) )
										if( ValidateLength('post','Post code',3) )
											bRetVal = true;
										else
											document.getElementById("post").focus();
									else
										document.getElementById("suburb").focus();
								else
									document.getElementById("address").focus();
							else
								document.getElementById("phone").focus();
						else
							document.getElementById("phone3").focus();
					else
						document.getElementById("lname").focus();
				else
					document.getElementById("fname").focus();
			else
				document.getElementById("confirm").focus();
		else
			document.getElementById("join_pw").focus();
	else
		document.getElementById("join_id").focus();

	if( bRetVal ) {
		pw = document.getElementById("join_pw").value;
		pw2 = document.getElementById("confirm").value;

		if( pw != pw2 ) {
			bRetVal = false;
			alert("Password and confirm password does not match!");
			document.getElementById("join_pw").value = "";
			document.getElementById("confirm").value = "";
			document.getElementById("join_pw").focus();
		}
	}
	return bRetVal;
}

function Login()
{
	id = document.getElementById("userid").value;
	pw = document.getElementById("passwd").value;
	if( id.length > 2 ) {
		if( pw.length >2 ) {
			document.getElementById("frmLogin").action = "code_login.php";
			document.getElementById("frmLogin").submit();
		} else {
			alert("Password must be longer than 3 characters!");
			document.getElementById("passwd").focus();
		}
	}else {
		alert("User id must be longer than 3 characters!");
		document.getElementById("userid").focus();
	}    
}

function InitPass()
{
	pw = document.getElementById("passwd").value;
	pw_cn = document.getElementById("confirm").value;
	if( pw.length > 3 ) {
		if( pw_cn.length >3 ) {
			if( pw == pw_cn ) {
				if( pw != "1111" ) {
					document.getElementById("frmLogin").action = "code_initpass.php";
					document.getElementById("frmLogin").submit();
				}
				else {
					alert("New password must not be same as old one!");
					document.getElementById("passwd").value = "";
					document.getElementById("confirm").value = "";
					document.getElementById("passwd").focus();
				}
			}else {
				alert("New password and confirm password must be same!");
				document.getElementById("confirm").value = "";
				document.getElementById("confirm").focus();
			}
		} else {
			alert("Confirm password must be longer than 3 characters!");
			document.getElementById("confirm").focus();
		}
	}else {
		alert("Password must be longer than 3 characters!");
		document.getElementById("passwd").focus();
	}    
}

function Logout()
{
	location.href="logout.php";
}

function addCart()
{
	result = confirm("Are you sure to add this item to your cart?");
	if( result )
		document.getElementById("frmMain").submit();
}

function checkoutCart()
{
	result = confirm("Are you sure to check out?");
	if( result ) {
		document.getElementById("recv_name").value = document.getElementById("recv_name").value;
		document.getElementById("dl_remark").value = document.getElementById("dl_remark").value;
		document.getElementById("frmMain").submit();
	}
}


function checkoutCart_r()
{
	result = confirm("Are you sure to check out?");
	if( result ) {		
		document.getElementById("frmMain").submit();
	}
}

function backtoCart()
{
	location.href = "cart_view.php";
}

function backtoPromoCart()
{
	location.href = "promo_cart_view.php";
}

function confirmCart()
{
	result = confirm("Are you sure?");
	if( result )
		document.getElementById("frmMain").submit();
}

function updateCart(seqno)
{
	/*document.getElementById("frmItem").action = "cart_update.php";
	document.getElementById("frmItem").submit();*/
	var qty = document.getElementById("order_"+seqno).value;
	location.href = "cart_update.php?item_seqno="+seqno+"&order_"+seqno+"="+qty;
}

function deleteCart(seqno)
{
	result = confirm("Are you sure to delete this item from cart?");
	if( result ) {
		/*document.getElementById("frmItem").action = "cart_delete.php";
		document.getElementById("frmItem").submit();*/

		location.href="cart_delete.php?item_seqno="+seqno;
	}
}

function selectPayType()
{
	var pay_type = getSelectedValue(frmMain.pay_type);
	if( pay_type == "ac" ) {
		card_detail.style.display = "none";
		acc_password.style.display = "inline";
	}
	if( pay_type == "cc" ) {
		acc_password.style.display = "none";
		card_detail.style.display = "inline";
	}
	if( pay_type == "" ) {
		acc_password.style.display = "none";
		card_detail.style.display = "none";
	}
}

function Available()
{
    w_name = "new_customer"
	result = ValidateUserid();
	if( result ) {
	    userid = document.getElementById("i_userid").value;
		url = "available_id.php?userid="+userid;
	    Width = 500;
		Height = 400;
	    window_string = "toolbar=no,menubar=no,resizable=no,scrollbars=no, width=" + Width + ",height=" + Height;
		if( ValidateLength('i_userid','User id',2) )
			window.open(url,w_name,window_string);
		else
			document.getElementById("i_userid").focus();
	}
}


function Search()
{
	alert("Under contruction!");
}

function focus_Login()
{
	document.getElementById("userid").focus();
}

function MiniCalendar(field)
{
    w_name = "calendar"
    url = "/mini_cal.php?field=" + field;
    Width = 240;
    Height = 150;
    Top = 170;
    Left = 380;
    window_string = "toolbar=no,menubar=no,resizable=no,scrollbarow=no, width=" + Width + ",height=" + Height + ",top=" + Top + ",left=" + Left;
    window.open(url,w_name,window_string);
}

function uncheckField(field)
{
	frmMain.elements(field).checked = false;
}

function resetPasswd()
{
	result = confirm("Distributor's password is going to reset to '1111'.\rDo you want to proceed?");
	if( result ) {
		document.getElementById("frmMain").action = "user_reset.php";
		document.getElementById("frmMain").submit();
	}
}

function shop_search()
{
	post = frmShopSearch.postcode.value;
	if( post.length > 3 ) {
		w_name = "post_search";
		url = "shop_search.php?postcode="+post;
	    Width = 350;
		Height = 250;
	    window_string = "toolbar=no,menubar=no,resizable=no,scrollbars=yes, width=" + Width + ",height=" + Height;
		window.open(url,w_name,window_string);
		// frmShopSearch.submit();
	} else {
		alert("Please input available post code first!");
		frmShopSearch.postcode.focus();
	}
}

function OrderProcess()
{
	bRetVal = false;
	//var recv_choise = getSelectedValue(frmMain.recv_choise);
	var recv_choise = document.getElementById("recv_choise").value;
	if( recv_choise.length > 0 ) {
		if( ValidateLength('order_name','Your Name',2) )
			if( ValidateLength('customer_order_no','Customer Order No.',3) )
				if( ValidateLength('recv_name','Receiver Name',2) )
					if( ValidateLength('recv_address','Address',10) )
						if( ValidateLength('recv_suburb','Suburb',3) )
							if( ValidateLength('recv_state','State',2) )
								if( ValidateLength('recv_post','Post code',4) )
									bRetVal = true;
								else
									document.getElementById("recv_post").focus();
							else
								document.getElementById("recv_state").focus();
						else
							document.getElementById("recv_suburb").focus();
					else
						document.getElementById("recv_address").focus();
				else
					document.getElementById("recv_name").focus();
			else
				document.getElementById("customer_order_no").focus();
		else
			document.getElementById("order_name").focus();
	} else {
		alert("Choose delivery address");
	}

	if( bRetVal ) {
		document.getElementById("frmMain").submit();
	}		
}


function OrderProcess_r()
{
	bRetVal = false;
	//var recv_choise = getSelectedValue(frmMain.recv_choise);
	var recv_choise = document.getElementById("recv_choise").value;
	if( recv_choise.length > 0 ) {
		if( ValidateLength('order_name','Your Name',2) )
			if( ValidateLength('customer_order_no','Customer Order No.',3) )
				if( ValidateLength('recv_name','Receiver Name',2) )					
					bRetVal = true;								
				else
					document.getElementById("recv_name").focus();
			else
				document.getElementById("customer_order_no").focus();
		else
			document.getElementById("order_name").focus();
	} else {
		alert("Choose delivery address");
	}

	if( bRetVal ) {
		document.getElementById("frmMain").submit();
	}		
}

function CustomerOrderProcess()
{
	bRetVal = false;
	
	if( payment_status ) {
		alert("Please wait");
	}else {
	
		if( ValidateLength('recv_name','Receiver Name',2) ) {
			if( ValidateLength('cc_holder','Card Holder name',5) )
				if( ValidateLength('ccnum','Credit Card No.',10) )
					if( ValidateLength('cc_exp1','Card Expire Month',2) )
						if( ValidateLength('cc_exp2','Card Expire Year',2) )
							bRetVal = true;
						else
							document.getElementById("cc_exp2").focus();
					else
						document.getElementById("cc_exp1").focus();
				else
					document.getElementById("ccnum").focus();
			else
				document.getElementById("cc_holder").focus();
		} else
			document.getElementById("recv_name").focus();
	
		if( bRetVal ) {
			payment_status = true;
			document.getElementById("frmMain").submit();
		}
	}
}

function inputAddress()
{
	bRetVal = false;
	if( ValidateLength('recv_phone','Phone No.',8) )
		if( ValidateLength('recv_address','Address',10) )
			if( ValidateLength('recv_suburb','Suburb',3) )
				if( ValidateLength('recv_post','Post code',4) )
					document.getElementById("frmMain").submit();
				else
					document.getElementById("recv_post").focus();
			else
				document.getElementById("recv_suburb").focus();
		else
			document.getElementById("recv_address").focus();
	else
		document.getElementById("recv_phone").focus();
}

function deleteDelivery( seqno )
{
	result = confirm("Do you want to delete this address?");
	if( result ) {
		document.getElementById("seqno").value = seqno;
		document.getElementById("frmMain").action = "addr_delete.php";
		document.getElementById("frmMain").submit();
	}
}

function updateDelivery()
{
	if( ValidateFields_delivery()) {
		document.getElementById("frmMain").action = "addr_update.php";
		document.getElementById("frmMain").submit();
	}
}

function updateDeliveryForm( seqno )
{
	document.getElementById("seqno").value = seqno;
	document.getElementById("frmMain").action = "addr_update_form.php";
	document.getElementById("frmMain").submit();
}

function updateDefaultDelivery()
{
	document.getElementById("frmMain").action = "addr_update_default.php";
	document.getElementById("frmMain").submit();
}

function receiveDelivery(form)
{
	result = confirm("Did you receive this order?");
	if( result )
		document.getElementById("form").submit();
}

function minOrder()
{
	alert("Your order amount is under minimum order amount.\rPlease add more items.");
}

function updatePromoCart( form )
{
	document.getElementById("form").action = "promo_cart_update.php";
	document.getElementById("form").submit();
}

function deletePromoCart( form )
{
	result = confirm("Are you sure to delete this item from cart?");
	if( result ) {
		document.getElementById("form").action = "promo_cart_delete.php";
		document.getElementById("form").submit();
	}
}

function printForm(seqno)
{
	w_name = "_blank";
	url = "order_print.php?seqno="+seqno;
	Width = 780;
	Height = 800;
	window_string = "toolbar=no,menubar=no,resizable=no,scrollbars=yes, width=" + Width + ",height=" + Height;
	window.open(url,w_name,window_string);
}

function inputEnq()
{
	//var enq_type = getSelectedValue(document.getElementById("enq_type").value);
	enq_type = document.getElementById("enq_type").value;
	if( enq_type.length > 0 ) {
		if( ValidateLength('fullname','Full name',2) )
			if( ValidateLength('email','Email',5) )
				document.getElementById("frmMain").submit();
			else
				document.getElementById("email").focus();
		else
			document.getElementById("fullname").focus();
	}else {
		alert("Please choose type of enquery");
	}
}

function inputReturn()
{
	var enq_type = getCheckedValue(frmMain.enq_type);
	var repl_parts = getCheckedValue(frmMain.repl_parts);
	if( enq_type.length > 0 ) {
		if( ValidateLength('fullname','Full name',2) )
			if( ValidateLength('email','Email',5) )
				if( ValidateLength('product_code','Product Code',5) )
					if( repl_parts.length > 0 )
						document.getElementById("frmMain").submit();
					else
						alert("Please choose parts");
				else
					document.getElementById("product_code").focus();
			else
				document.getElementById("email").focus();
		else
			document.getElementById("fullname").focus();
	}else {
		alert("Please choose type of return");
	}
}

function loginFirst()
{
	alert("Please login first");
}

function writeForumForm()
{
	location.href="forum_write_form.php";
}

function writeForum()
{
	if( ValidateLength('subject','Subject',5) )
		if( ValidateLength('content','Detail',10) )
			document.getElementById("frmMain").submit();
		else
			document.getElementById("content").focus();
	else
		document.getElementById("subject").focus();
}

function replyForum()
{
	if( ValidateLength('comment','Comment',5) ) {
		document.getElementById("frmMain").action = "forum_reply.php";
		document.getElementById("frmMain").submit();
	}else
		document.getElementById("comment").focus();
}

function delReply(ref_no)
{
	result = confirm("Are you sure to delete this reply?");
	if( result ) {
		document.getElementById("ref_no").value = ref_no;
		document.getElementById("frmMain").action = "forum_reply_del.php";
		document.getElementById("frmMain").submit();
	}
}
function delForum()
{
	result = confirm("Are you sure to delete this post?");
	if( result ) {
		document.getElementById("frmMain").action = "forum_delete.php";
		document.getElementById("frmMain").submit();
	}
}

function cart_address_change()
{
	document.getElementById("frmSub").action = "cust_cart_addr_change.php";
	document.getElementById("frmSub").submit();
}

function cart_address_update()
{
	location.href = "addr_list.php";
}

function searchProd()
{
	p_detail = document.getElementById("p_detail").value;
	if( p_detail.length > 0 ) {
		document.getElementById("frmProdSearch").action = "prod_search.php";
		document.getElementById("frmProdSearch").submit();
	}else {
		alert("Please type in product detail!");
		document.getElementById("p_detail").focus();
	}
}

function closePopup()
{
	document.getElementById("popup").style.display = "none";
}