$().ajaxStop($.unblockUI);
function block() {
	//$.blockUI(loading_img); 
	$.blockUI({ message: loading_img});
}

function global_ajax_submit(saveForm,url1,url2){
	$('#'+saveForm).ajaxSubmit({
		url : url1,
		beforeSubmit: block,
		clearForm: true,
		success: function(msg){
			if(url2){
				window.location = url2;
			}
		}
	});
}


function global_ajax_submit_noneblock(saveForm,url1,url2){
	$('#'+saveForm).ajaxSubmit({
		url : url1,
		clearForm: true,
		success: function(msg){
			if(url2){
				window.location = url2;
			}
		}
	});
}



function global_ajax_submit_validate(saveForm,url1,url2){	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#"+saveForm).validate({
    	    meta: "validate",
            submitHandler: function(form) {	
			
				$('#'+saveForm).ajaxSubmit({
					url : url1,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						
						if(url2){
							window.location = url2;
						}
						
					}
				});
				
            }
       });	
}


function global_icon_input_em(obj_name, em){	
	document.getElementById(obj_name).value = document.getElementById(obj_name).value+em;
}


function getUrl(url_go) {
	block();
	window.location = url_go;
}

function global_members_introduce(qWord){
	$('#div_error').html('');
	$('#myform').ajaxSubmit({
		url : 'members_introduce_process.php?action='+qWord,
		beforeSubmit: block,
		success: function(msg){
			msg = eval('('+msg+')');
			if (typeof msg.error != 'undefined'){				
				$('#div_error').html(msg.error);		
				$('.error').removeClass('error');		
				$.each(msg.elemId, function(i, value){ 
					if ( ! i) $('#'+value).focus();
					$('#'+value).addClass('error'); 
				});
			} else {
				$('#div_introduce').html(msg.content);
			}
			$('html, body').animate({ scrollTop: 0 }, "normal"); 
		}
	});	
}
var timeOut;
function countDown(elemDisplayId, amount, callFunc){	
	$('#'+elemDisplayId).text(amount);
	if ( ! amount){
		eval(callFunc);
	} else {
		--amount;		
		timeOut = setTimeout("countDown(\""+elemDisplayId+"\", "+amount+", \""+callFunc+"\")", 1000);
	}		
}

function dlg_confirm(msg, strFunc){
	var ans = confirm(msg);
	if (ans) eval(strFunc);
}

function global_submit_msg(submit_form, url_goto, div_msg, clear_form){
	$('#'+submit_form).ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: clear_form,
		success: function(msg){
			$('#'+div_msg).html(msg);			
		}
	});
}

function check_email_format(check_email){
	var apos = check_email.indexOf("@");
	var dotpos = check_email.lastIndexOf(".");
	if((apos < 1) || (dotpos-apos<2)){
		return 0;
	}else{
		return 1;
	}
}

function global_members_switch(objForm,objDiv,objVal){
	if(objVal){
		url_goto = 'members_register_switch.php?members_dhc='+objVal+'&objdiv='+objDiv;
	}else{
		url_goto = 'members_register_switch.php?objdiv='+objDiv;
	}
	$('#'+objForm).ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#'+objDiv).html(msg);
		}
	});
}

function check_username(object_value){
	$('#myform').ajaxSubmit({
		url : 'members_check_username.php?members_username='+object_value,
		clearForm: false,
		success: function(msg){
			$('#username_div').html(msg);
		}
	});
}

// --------------------------- BEGIN : แสดงขื่อ-นามสกุลที่ Billing Name ------------------
function global_name_show_billing(){
	document.getElementById('members_billing_name').value = document.getElementById('members_name_th').value;
}

function global_surname_show_billing(){
	var members_name_th = document.getElementById('members_name_th');
	var members_surname_th = document.getElementById('members_surname_th');
	if(members_name_th.value == ''){
		alert('กรุณากรอก ชื่อ(ภาษาไทย) ก่อนค่ะ [Please input Name(Thai) before input this point.]');
		members_surname_th.value = '';
		members_name_th.focus();
		return false;
	}else{
		document.getElementById('members_billing_name').value = document.getElementById('members_name_th').value+'  '+document.getElementById('members_surname_th').value;
	}
}
// --------------------------- END : แสดงชื่อ-นามสกุลที่ Billing Name --------------------


// --------------------------- BEGIN : Fix members colde ----------------------------

function global_members_code_key(){
	var members_code = document.getElementById("members_code").value;
	if(members_code == ""){
		jQuery(function($){
			$("#members_code").mask("999999999");
		});
	}
}

// --------------------------- END : Fix members colde ------------------------------

// --------------------------- BEGIN : Fix members ID Card --------------------------

function global_members_id_card_key(){
	var members_id_no = document.getElementById("members_id_no").value;
	if(members_id_no == ""){
		jQuery(function($){
			$("#members_id_no").mask("9999999999999");
		});
	}
}

// --------------------------- END : Fix members ID Card ----------------------------

function global_members_register(objForm,objUrl1,objUrl2,objDiv){

	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#"+objForm).validate({
    	    meta: "validate",
            submitHandler: function(form) {

				var members_shipping_email = document.getElementById('members_shipping_email').value;
				if(check_email_format(members_shipping_email) == 0){
					document.getElementById('members_check_email').value = 'INCORRECT';
				}else{
					document.getElementById('members_check_email').value = '';
				}
				$('#'+objForm).ajaxSubmit({
					url : objUrl1,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						if(objUrl2){
							
							$('#div_members_check').html(msg);
							
							if(document.getElementById('check_success').value == 'success'){
								window.location = objUrl2;
							}else{
								
								if(document.getElementById('check_success').value == 'exist_center_members'){
									document.getElementById('div_button_signup').style.display = 'none';
									document.getElementById('div_button_sigup_confirm').style.display = '';
								}else{
									document.getElementById('div_button_signup').style.display = '';
									document.getElementById('div_button_sigup_confirm').style.display = 'none';
								}
								
								
								$('#div_members_email_incorrect').html(msg);
							}
							
						}else{
							
							$('#div_members_check').html(msg);
							
							
							if(document.getElementById('check_success').value == 'success'){
								$('#div_dhcContent').html(msg);
								top.location = '#';
								timer = setTimeout('global_after_members_register()',10000);
							}else{
								
								
								if(document.getElementById('check_success').value == 'exist_center_members'){
									document.getElementById('div_button_signup').style.display = 'none';
									document.getElementById('div_button_sigup_confirm').style.display = '';
								}else{
									document.getElementById('div_button_signup').style.display = '';
									document.getElementById('div_button_sigup_confirm').style.display = 'none';
								}
								
								
								$('#div_members_email_incorrect').html(msg);
							}
							
							
						}
					}
				});
				
				
            }
        });
}








function global_members_register_after_check(objForm,objUrl1,objUrl2,objDiv){
	
	/*
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#"+objForm).validate({
    	    meta: "validate",
            submitHandler: function(form) {
	*/
				
				
				var members_shipping_email = document.getElementById('members_shipping_email').value;
				if(check_email_format(members_shipping_email) == 0){
					document.getElementById('members_check_email').value = 'INCORRECT';
				}else{
					document.getElementById('members_check_email').value = '';
				}
				
				$('#'+objForm).ajaxSubmit({
					url : objUrl1,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						
						
						if(objUrl2){
							$('#div_members_check').html(msg);
							if(document.getElementById('check_success').value == 'success'){
								window.location = objUrl2;
							}else{
								$('#div_members_email_incorrect').html(msg);
							}
						}else{
							$('#div_members_check').html(msg);
							
							
							if(document.getElementById('check_success').value == 'success'){
								$('#div_dhcContent').html(msg);
								top.location = '#';
								timer = setTimeout('global_after_members_register()',10000);
							}else{
								$('#div_members_email_incorrect').html(msg);
							}
							
							
						}
						
					}
				});
				
		
	
	/*
				
            }
        });
	*/
		
}






function global_after_members_register() {
	window.location = 'index.php'
}


function id_pass_onload(){
	document.getElementById('members_id_no').style.display = '';
	document.getElementById('members_id_no_02').style.display = 'none';
	document.getElementById('personal_id').style.display = '';
	document.getElementById('passport_id').style.display = 'none';
}
function id_passport(obj){
	if(obj.value == 'ID'){
		id_pass_onload();	
	}else{
		document.getElementById('members_id_no').style.display = 'none';
		document.getElementById('members_id_no_02').style.display = '';
		document.getElementById('personal_id').style.display = 'none';
		document.getElementById('passport_id').style.display = '';
	}
}

function members_call_calendar() {
	jQuery(function($){
		$("#members_birthday").attachDatepicker();
	});
}

function ShippingTobilling(obj){
	if(obj.checked == true){
		var province = document.getElementById('members_shipping_province').value;
		var amphur = document.getElementById('members_shipping_amphur').value;
		var district = document.getElementById('members_shipping_district').value;
		
		global_select_relation('members_billing_amphur','location_relation_process.php?action=amphur&province_id='+province+'&amphur_id='+amphur,'members_billing_amphur','','');
		global_select_relation('members_billing_district','location_relation_process.php?action=district&amphur_id='+amphur+'&district_id='+district,'members_billing_district','','');
		
		document.getElementById('members_billing_province').value = province;
		document.getElementById('members_billing_company').value = document.getElementById('members_shipping_company').value;
		document.getElementById('members_billing_no').value = document.getElementById('members_shipping_no').value;
		document.getElementById('members_billing_moo').value = document.getElementById('members_shipping_moo').value;
		document.getElementById('members_billing_placement').value = document.getElementById('members_shipping_placement').value;
		document.getElementById('members_billing_mooban').value = document.getElementById('members_shipping_mooban').value;
		document.getElementById('members_billing_soi').value = document.getElementById('members_shipping_soi').value;
		document.getElementById('members_billing_road').value = document.getElementById('members_shipping_road').value;
		document.getElementById('members_billing_zipcode').value = document.getElementById('members_shipping_zipcode').value;
		document.getElementById('members_billing_tel').value = document.getElementById('members_shipping_tel').value;
		document.getElementById('members_billing_mobile').value = document.getElementById('members_shipping_mobile').value;
		document.getElementById('members_billing_fax').value = document.getElementById('members_shipping_fax').value;
		
	}else{
		
		$("#members_billing_amphur").html('<option value="">' +$("#members_billing_amphur").attr("real")+ '</option>');
		$("#members_billing_district").html('<option value="">' +$("#members_billing_district").attr("real")+ '</option>');
		document.getElementById('members_billing_province').value = '';
		document.getElementById('members_billing_company').value = '';
		document.getElementById('members_billing_no').value = '';
		document.getElementById('members_billing_moo').value = '';
		document.getElementById('members_billing_placement').value = '0';
		document.getElementById('members_billing_mooban').value = '';
		document.getElementById('members_billing_soi').value = '';
		document.getElementById('members_billing_road').value = '';
		document.getElementById('members_billing_zipcode').value = '';
		document.getElementById('members_billing_tel').value = '';
		document.getElementById('members_billing_mobile').value = '';
		document.getElementById('members_billing_fax').value = '';
	}
}

function ShippingToPermanent(obj){
	if(obj.checked == true){
		var province = document.getElementById('members_shipping_province').value;
		var amphur = document.getElementById('members_shipping_amphur').value;
		var district = document.getElementById('members_shipping_district').value;
		
		global_select_relation('members_permanent_amphur','location_relation_process.php?action=amphur&province_id='+province+'&amphur_id='+amphur,'members_permanent_amphur','','');
		global_select_relation('members_permanent_district','location_relation_process.php?action=district&amphur_id='+amphur+'&district_id='+district,'members_permanent_district','','');
		
		document.getElementById('members_permanent_province').value = province;
		document.getElementById('members_permanent_no').value = document.getElementById('members_shipping_no').value;
		document.getElementById('members_permanent_moo').value = document.getElementById('members_shipping_moo').value;
		document.getElementById('members_permanent_soi').value = document.getElementById('members_shipping_soi').value;
		document.getElementById('members_permanent_road').value = document.getElementById('members_shipping_road').value;
		document.getElementById('members_permanent_zipcode').value = document.getElementById('members_shipping_zipcode').value;
		document.getElementById('members_permanent_tel').value = document.getElementById('members_shipping_tel').value;
		
	}else{
		
		$("#members_permanent_amphur").html('<option value="">' +$("#members_permanent_amphur").attr("real")+ '</option>');
		$("#members_permanent_district").html('<option value="">' +$("#members_permanent_district").attr("real")+ '</option>');
		document.getElementById('members_permanent_province').value = '';
		document.getElementById('members_permanent_no').value = '';
		document.getElementById('members_permanent_moo').value = '';
		document.getElementById('members_permanent_soi').value = '';
		document.getElementById('members_permanent_road').value = '';
		document.getElementById('members_permanent_zipcode').value = '';
		document.getElementById('members_permanent_tel').value = '';
	}
}

function global_select_relation(objAction,url,objClear01,objClear02,objClear03) {
	var urls = url;
	
	if(objClear01 != ''){
		$("#"+objClear01).html('<option value="">' +$("#"+objClear01).attr("real")+ '</option>');
	}
		
	if(objClear02 != ''){
		$("#"+objClear02).html('<option value="">' +$("#"+objClear02).attr("real")+ '</option>');
	}

	if(objClear03 != ''){
		$("#"+objClear03).html('<option value="">' +$("#"+objClear03).attr("real")+ '</option>');
	}
		
		
	$.getJSON(urls, function(j){
		
		if(objClear01 != ''){
			$("#"+objClear01).html('<option value="">' +$("#"+objClear01).attr("real")+ '</option>');
		}
		
		if(objClear02 != ''){
			$("#"+objClear02).html('<option value="">' +$("#"+objClear02).attr("real")+ '</option>');
		}

		if(objClear03 != ''){
			$("#"+objClear03).html('<option value="">' +$("#"+objClear03).attr("real")+ '</option>');
		}

		var options = '';
		for (var i = 0; i < j.length; i++) {
			if(j[i].optionValue == 0){
				options_value = '';
			}else{
				options_value = j[i].optionValue;
			}
			options += '<option value="'+options_value+'"  '+j[i].optionSelect+'>' + j[i].optionDisplay + '</option>';
		}
		
		$("#"+objAction).html(options);
		//$('#'+objAction+' option:first').attr('selected', 'selected');
	})
}

function global_clear_postal_code(objEmpty){
	if(objEmpty){
		document.getElementById(objEmpty).value = '';	
	}
}


function global_zipcode(obj,div_obj,url_goto){
	$('#myform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){
			$('#'+div_obj).html(msg);
			document.getElementById(obj).value = document.getElementById(div_obj).innerHTML;
		}
	});
}


function global_sub_promotion(product_id, obj, url){
	var x;
	var check_val = 0;
	var my_split = new Array();
	var spro_id_check = document.getElementById('spro_id_check').value;
	
	my_split = spro_id_check.split(',');
	for (x in my_split){		
		if(document.getElementById('sub_product_code_'+my_split[x]).value == ''){
			check_val = 1;
		}
	}
	
	
	if(check_val == 1){
		
		$('#myform').ajaxSubmit({
			url : 'cart_promotion_check.php?action_check=promotion',
			beforeSubmit: block,
			clearForm: false,
			success: function(msg){
				$('#div_promotion_check').html(msg);
			}
		});
	
	}else{
		$('#div_promotion_check').html('');
		add_to_cart(product_id, obj, url);
	}
}














// --------------------- DHC Supplement Activity promotion 25 ก.ค. - 10 ก.ย. 2552 หลังจากนี้ให้ลบทิ้ง ------------------------
function global_dhc_supplement_activiti_alert_before_add(product_id, obj, url, discount_percent){
	alert('ทางบริษัท ดีเอชซี (ประเทศไทย) จำกัด ได้จัดโปรโมชั่น Healthy in Style "ช้อปสินค้าถูกใจตามสไตล์คุณ" ในกลุ่มสินค้า DHC Supplement\nตั้งแต่วันที่ 25 กรกฎาคม ถึง 10 กันยายน 2552\n\n\t*** เพียงซื้อผลิตภัณฑ์อาหารเสริม DHC Supplement ใดๆ 3 ชิ้น (ซ้ำกันได้ 2 ชิ้น) รับส่วนลดทันที '+discount_percent+'% จากราคาปกติ (1 สิทธิ์ต่อ 1 ใบสั่งซื้อ)\n\t*** การคำนวณส่วนลด '+discount_percent+'% จากราคาปกติทางเว็บไซต์นั้น จะให้ความสำคัญกับสำดับการเก็บสินค้าใส่ในตะกร้า กล่าวคือ จะนำสินค้า DHC Supplement ลำดับแรกไปคำนวณส่วนลดก่อนจากนั้นจึงนำสินค้าลำดับถัดไปมาคำนวณส่วนลดตามลำดับไป นะคะ');
	add_to_cart(product_id, obj, url);
}
// --------------------- DHC Supplement Activity promotion 25 ก.ค. - 10 ก.ย. 2552 หลังจากนี้ให้ลบทิ้ง ------------------------






function add_to_cart(product_id, obj, url,product_code, mode){
	//---remove error section
	
	$('#error_section').remove();
	//---
	var tmp = $('#chk_voucher').val();
	if (typeof tmp == 'undefined' || ! tmp) tmp = '';
	if(tmp != '' && ((product_code != tmp && tmp == '8998') || (product_code != tmp && product_code == '8998'))){
		var url = "includes/public_error.php";
		var err = {
			msg: 'คุณไม่สามารสั่งซื้อสินค้ารายการอื่นร่วมกับการสั่งซื้อ DHC Voucher ได้ค่ะ',
			title: 'ผิดพลาด'
		};
		$('#myform').ajaxSubmit({
			url : url,
			clearForm: true,
			success: function(msg){				
				$('#div_cart').prepend(msg);
				$('#display_error').append(err.msg);
				$('#display_error strong').html(err.title);
				$('#'+obj).val(1);
			}			
		});
		return false ;
	}
		
	var go_url = url+'?product_id='+product_id+'&quantity='+$('#'+obj).val();
	if (typeof mode != 'undefined'){
		if (mode == 'special_dc' 
			|| mode == 'birthday'){ go_url += '&mode='+mode; }
	}
	
	$('#myform').ajaxSubmit({
		url : go_url,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			//***birthday
			if ($('#hdnbirthday').length){ 
				$("img[name^='addtocart']").removeAttr('onclick');
				window.location = 'products_list.php?cat_id='+$('#hdnbirthday').val(); return; 
			}
			//***			
			$('#div_cart').html(msg);
			$('#'+obj).val(1);
		}
	});
}



// -------------------- DHC Tisco promotion discount 15% and 20% หลังจากโปรโมชั่นลบทิ้งได้เลย ----------------------------------
function add_to_cart_tisco(product_id, obj, url){
	var go_url = url+'&product_id='+product_id+'&quantity='+document.getElementById(obj).value;
	$('#myform').ajaxSubmit({
		url : go_url,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_cart').html(msg);
			document.getElementById(obj).value = 1;
		}
	});
}
// -------------------- DHC Tisco promotion discount 15% and 20% หลังจากโปรโมชั่นลบทิ้งได้เลย ----------------------------------




function delete_to_cart(url){

	$('#myform').ajaxSubmit({
		url : url,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_cart').html(msg);
			//***for birthday
			if ($('#hdnhasbd').length){
				$('.div_bd:first').show();
				$('.div_bd:last').hide();
			}
			//***
			/***for exchange_point module (shared function)***/
			if ($('.orcpoint').length){
				$('#div_section_point span:first').text($('.orcpoint:first').val());
				$('#div_section_point span:last').text($('.orcpoint:last').val());			
			}
/*			if(document.getElementById('check_files_8716').value == 'products_8716_oc02.php'){

			}
*/			
		}
	});
}


function global_members_login(){
	var url_goto = 'members_login.php';
	$('#frm_login').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_members_login').html(msg);
			
			if(document.getElementById('check_login_success').value == 'successful'){
				window.location = 'members_control_panel.php';
			}
			
		}
	});
}

function global_top_view(product_id){
	var url_goto = 'products_view_count.php?product_id='+product_id;
	$('#header_form').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){			
		}
	});
}


function global_goto_comment(product_id){
	
	var url_goto = 'products_proccess.php?cond_action=set_session&product_id='+product_id;
	$('#myform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){
			window.location = 'products_comment.php';
		}
	});
	
}


function global_product_comment(product_id){
	var url_goto = 'products_proccess.php?cond_action=add_comment&product_id='+product_id;
	$('#myform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){		
			$('#div_product_comment').html(msg);
		}
	});
}


function global_product_comment_em(em){	
	document.getElementById('procom_comment').value = document.getElementById('procom_comment').value+em;
}


function global_product_comment_save(){	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#myform').ajaxSubmit({
					url : 'products_proccess.php?cond_action=save',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_product_comment').html(msg);
						global_product_comment_refresh();
					}
				});
				
            }
        });
}


function global_product_comment_refresh(){
	$('#myform').ajaxSubmit({
		url : 'products_proccess.php?cond_action=refresh',
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_comment_list').html(msg);
		}
	});
}


function global_product_comment_cancel(){
	
	var url_goto = 'products_proccess.php?cond_action=cancel';
	$('#myform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){		
			$('#div_product_comment').html(msg);
		}
	});
	
}

function global_product_comment_advise_del(procom_id){
	var alert_msg = document.getElementById('procom_advise_del_alert'+procom_id).value;
	var url_goto = 'products_proccess.php?cond_action=advise_del&procom_id='+procom_id;
	$('#myform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){		
			alert(alert_msg);
		}
	});
}


function global_product_comment_star(objVal){
	if(objVal == 1){
		document.getElementById('div_start_one').style.display = "block";	
		document.getElementById('div_start_two').style.display = "none";
		document.getElementById('div_start_three').style.display = "none";
		document.getElementById('div_start_four').style.display = "none";
		document.getElementById('div_start_five').style.display = "none";
	}
	
	if(objVal == 2){
		document.getElementById('div_start_one').style.display = "none";	
		document.getElementById('div_start_two').style.display = "block";
		document.getElementById('div_start_three').style.display = "none";
		document.getElementById('div_start_four').style.display = "none";
		document.getElementById('div_start_five').style.display = "none";
	}
	
	if(objVal == 3){
		document.getElementById('div_start_one').style.display = "none";	
		document.getElementById('div_start_two').style.display = "none";
		document.getElementById('div_start_three').style.display = "block";
		document.getElementById('div_start_four').style.display = "none";
		document.getElementById('div_start_five').style.display = "none";
	}
	
	if(objVal == 4){
		document.getElementById('div_start_one').style.display = "none";	
		document.getElementById('div_start_two').style.display = "none";
		document.getElementById('div_start_three').style.display = "none";
		document.getElementById('div_start_four').style.display = "block";
		document.getElementById('div_start_five').style.display = "none";
	}
	
	if(objVal == 5){
		document.getElementById('div_start_one').style.display = "none";	
		document.getElementById('div_start_two').style.display = "none";
		document.getElementById('div_start_three').style.display = "none";
		document.getElementById('div_start_four').style.display = "none";
		document.getElementById('div_start_five').style.display = "block";
	}
}

function global_products_search(){
	var url_goto = 'products_proccess.php?cond_action=search_products';

	$('#searchform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){
			window.location = 'products_search.php';
		}
	});
	
}

function global_members_login_order(saveForm,url1,url2){
	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#"+saveForm).validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#'+saveForm).ajaxSubmit({
					url : url1,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_members_login_form').html(msg);
						if(document.getElementById('check_login_success').value == 'successful'){
							window.location = url2;
						}
					}
				});
				
            }
        });
}


function global_forgot_password(){
	var members_email = document.getElementById('members_email').value;
	if(check_email_format(members_email) == 0){
		url_goto = 'forgot_password_process.php?check_email=FALSE';
	}else{
		url_goto = 'forgot_password_process.php';
	}
	$('#myform').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#messagebox').html(msg);
			if(document.getElementById('check_return').value == 'success'){
				document.getElementById('members_email').value = '';
			}
		}
	});
}



// ------------------------------------ BEGIN : Order Step --------------------------------------------------------

function global_voucher_code_key(){
	var orders_gift_voucher_code = document.getElementById("orders_gift_voucher_code").value;
	if(orders_gift_voucher_code == ""){
		jQuery(function($){
			$("#orders_gift_voucher_code").mask("9999999");
		});
	}
}


function global_orders_switch_address(objVal){
	if(objVal == 'NM'){ 
		
		document.getElementById('div_normal_address').style.display = 'block';
		document.getElementById('div_pod_address').style.display = 'none';
		document.getElementById('div_pay_method').style.display = 'block';
		document.getElementById('div_pod_method').style.display = 'none';
		document.getElementById('div_pay_remark').style.display = 'block';
		document.getElementById('div_pay_remark02').style.display = 'block';
	}else{
		document.getElementById('div_normal_address').style.display = 'none';
		document.getElementById('div_pod_address').style.display = 'block';
		document.getElementById('div_pay_method').style.display = 'none';
		document.getElementById('div_pod_method').style.display = 'block';
		document.getElementById('div_pay_remark').style.display = 'none';
		document.getElementById('div_pay_remark02').style.display = 'none';
	}
	
	global_calculate_orders(null);
}


function global_calculate_orders(ddObj){	
	//check pod

	var objSel = $("select[name='orders_voucher_code[]'] :selected[value!='0']");
	var gv = $(".hdnprodcode_jschk[value='8998']");	//specified	
	var chkObj = Boolean(typeof ddObj != 'undefined' && ddObj);
	if($('#orders_send_address02').attr('checked')){				
		if (objSel.length){
			var type = objSel.val().substring(0,2).toLowerCase();  
			if(type == 'gv' || type == 'ev') { 
				window.scrollTo(0,0);				
				alert('คุณไม่สามารถใช้ '+(type == 'gv' ? 'Gift' : 'E')+' Voucher ในการสั้งซื้อแบบ POD ได้ค่ะ');
				if (chkObj) $("select[name='orders_voucher_code[]']").val(0);
				$('#orders_send_address01').trigger('click');								
				return ;
			}
		}
	}
	
	if (chkObj){
		var obj = $(ddObj); //obj jquery style		
		var sum_voucher = 0;
		var chk_warn = false;		
		//check buy GV
		if ($(".hdnprodcode_jschk[value='8998']").length){
			alert('ขออภัย DHC Voucher & Discount ไม่สามารถชำระด้วยคูปองเงินสดได้ค่ะ');
			obj.val(0);			
			return
		}		
		//check duplicate with another
		$("select[name='orders_voucher_code[]']").each(function(inx){
			if ($(this).val() == obj.val() && $(this).attr('id') != obj.attr('id') && obj.val() != '0'){
				alert("คุณเลือกรหัสคูปองใบนี้แล้วค่ะ");
				obj.val(0);
				chk_warn = true;
				sum_voucher = 0;
				return;
			}
			sum_voucher += cv2num($('#orders_voucher_value_'+inx).val());
		});
		if ( ! chk_warn){
			if (obj.val() != '0' && sum_voucher >= cv2num($('#div_grand_total_before_cal_voucher').text())){	// && cv2num($('#div_grand_total_before_cal_voucher').text())
				alert('จำนวนเงินเพียงพอสำหรับยอดที่ต้องชำระแล้วค่ะ');
				obj.val(0);
			}
		}		
		//---
		var inx = obj.attr('id').substring(obj.attr('id').lastIndexOf('_') + 1);
		var value = obj.val().split('_').pop();		
		$('#orders_voucher_value_'+inx).val(currency_format(value, 2));
	}		
	var url_goto = 'orders_process.php?action_calculate=calculate_order';

	$('#myform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){

			$('#div_orders_messagebox').html(msg);
			
			$('#after_coupong_promotion').html(document.getElementById('total_46810_return_value').value);
			$('#div_value_after_cal_46810').html(document.getElementById('value_after_cal_46810_return_value').value);
			
			/*****for transport*****/			
			document.getElementById('orders_transport_cost').value = document.getElementById('transport_cost_return_value').value;
			
			var tcrv = '0.00';
			if ( ! cv2num($('#transport_cost_return_value').val())){ 
				$('#chk_point_transport').attr('checked', false);
				$('#span_point_transport').hide();
			} else {
				$('#span_point_transport').show();							
				if ( ! $('#chk_point_transport').attr('checked')) tcrv = $('#transport_cost_return_value').val();				
			}
			$('#div_transport_cost').html(tcrv);
			/*****/
			$('#div_grand_total').html(document.getElementById('grand_total_return_value').value);			
			$('#div_orc_point').html(document.getElementById('orc_point_return').value);
			$('#div_none_orc_point').html(document.getElementById('none_orc_point_return').value);
			$('#div_grand_total_before_cal_voucher').html(document.getElementById('grand_total_before_return_value').value);
			
			document.getElementById('grand_total_post_value').value = document.getElementById('grand_total_post_return_value').value;
			
			document.getElementById('mem_point_value').value = document.getElementById('mem_none_orc_point_total_return').value;			
			document.getElementById('mem_orc_point_one').value = document.getElementById('mem_orc_point_one_return').value;
			document.getElementById('mem_orc_point_two').value = document.getElementById('mem_orc_point_two_return').value;
			document.getElementById('mem_orc_point_three').value = document.getElementById('mem_orc_point_three_return').value;
			document.getElementById('mem_orc_point_four').value = document.getElementById('mem_orc_point_four_return').value;
			document.getElementById('mem_orc_point_five').value = document.getElementById('mem_orc_point_five_return').value;
			document.getElementById('mem_orc_point_total').value = document.getElementById('mem_orc_point_total_return').value;
			
			document.getElementById('mem_none_orc_point_one').value = document.getElementById('mem_none_orc_point_one_return').value;
			document.getElementById('mem_none_orc_point_two').value = document.getElementById('mem_none_orc_point_two_return').value;
			document.getElementById('mem_none_orc_point_three').value = document.getElementById('mem_none_orc_point_three_return').value;
			document.getElementById('mem_none_orc_point_four').value = document.getElementById('mem_none_orc_point_four_return').value;
			document.getElementById('mem_none_orc_point_five').value = document.getElementById('mem_none_orc_point_five_return').value;
			document.getElementById('mem_none_orc_point_total').value = document.getElementById('mem_none_orc_point_total_return').value;
			
			//check value before display payment method
			var cv2bool =  ! Boolean(parseFloat($('#grand_total_post_value').val()));
			if ($('#orders_payment_method:visible').length) $('#orders_payment_method').attr('disabled', cv2bool);
			if ($('#orders_pod_method:visible').length) $('#orders_pod_method').attr('disabled', cv2bool);
			//---
			//global_calculate_orders_sample(document.getElementById('total_sample_return').value);
			
		}
	});
	
}

function global_calculate_orders_sample(total_sample_value){
	var url_goto = 'orders_process.php?action_calculate=total_sample&total_sample_value='+total_sample_value;
	
	$('#myform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){
			
			$('#div_orders_products_sample').html(msg);
			
		}
	});
	
}

/****not use
function global_gift_voucher_input(){
	
	var orders_gift_voucher_code_alert = $("#orders_gift_voucher_code").attr("real");
	var orders_gift_voucher_code = document.getElementById('orders_gift_voucher_code');
	var orders_gift_voucher_value = document.getElementById('orders_gift_voucher_value');
		
	if(orders_gift_voucher_code.value == ''){
		alert(orders_gift_voucher_code_alert);
		orders_gift_voucher_value.value = '0.00';
		orders_gift_voucher_code.focus();
		return false;	
	}
	
	global_calculate_orders();
	
}
*/

function global_check_sample_select(objChange){
	var product_sample_qty_check = document.getElementById('product_sample_qty_check').value;
	var product_sample_id_check = document.getElementById('product_sample_id_check').value;	
	var x;
	var check_val = 0;
	var my_split = new Array();
	
	my_split = product_sample_id_check.split(',');
	for (x in my_split){	
		value_obj = document.getElementById('product_sample_qty_'+my_split[x]).value;
		if(value_obj != ''){
			check_val = check_val + (value_obj*1);
		}
	}
	
	if(check_val > product_sample_qty_check){
		alert('คุณได้เลือกสินค้าตัวอย่างทั้งหมด '+check_val+' รายการ ซึ่งมากกว่า '+product_sample_qty_check+' รายการแล้ว กรุณาตรวจสอบใหม่อีกครั้งค่ะ');	
		$(objChange).val(0) ;
	}
}

function global_orders_success_save(ordertype){

	var url_goto = (typeof ordertype == 'undefined' || ! ordertype) ? 'orders_process.php' : 'exchange_point_orders_process.php';
	var url_after_success;
	var orders_payment_method = (typeof ordertype == 'undefined' || ! ordertype) ? ($('#orders_payment_method').attr('disabled') ? -2 : document.getElementById('orders_payment_method').value) : -1;
	var orders_send_address01 = ($('#orders_send_address01').length) ? document.getElementById('orders_send_address01').checked : true;

	$('#myform').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
		
			$('#div_orders_check').html(msg);
			
			if(document.getElementById('orders_success').value == 'success'){
					
				if(orders_send_address01 == true){
				
					if(orders_payment_method == 'bkkb_ipay'){
						document.getElementById('bt_ipay_submit').click();
					}
					if(orders_payment_method == 'paysbuy'){
						document.getElementById('bt_orders_submit').click();
					}
					if(orders_payment_method == 'atm'){
						window.location = 'orders_atm.php';
					}
					if(orders_payment_method == 'bank'){
						window.location = 'orders_bank_counter.php';
					}
					if(orders_payment_method == 'internetbank'){
						window.location = 'orders_internet_banking.php';
					}
					if(orders_payment_method == -1){
						window.location = 'exchange_point_orders_thank.php';
					}
					if(orders_payment_method == -2){
						global_orders_finish();
					}		

				}else{
					window.location = 'orders_pod_thank.php';
				}
			}
			
			if(document.getElementById('orders_success').value == 'none_success_sess_lost'){
				timer = setTimeout('global_after_members_register()',20000);
			}
	

		}
	});
}


function global_orders_finish(){
	var url_goto = 'orders_process.php?action_finish=TRUE';
	$('#myform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){
			window.location = 'members_orders_history.php';
		}
	});
}

// ------------------------------------ END : Order Step --------------------------------------------------------

// ------------------------------------ BEGIN : Edit payment at view order page -------------------------------------------------
function global_payment_save(ordersType){
	var url_goto = (typeof ordersType == 'undefined' || ! ordersType ? 'orders_process.php' : 'exchange_point_orders_process.php');
	var orders_payment_method = document.getElementById('orders_payment_method').value;
	$('#myform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){
			$('#div_orders_check').html(msg);
			/*****for exchange point*****/
			if (typeof ordersType != 'undefined' && ordersType){
				window.location = 'exchange_point_orders_thank.php';
				return;
			}
			/*****/
			if(orders_payment_method == 'bkkb_ipay'){
				document.getElementById('bt_ipay_submit').click();
			}
			if(orders_payment_method == 'paysbuy'){
				document.getElementById('bt_orders_submit').click();
			}
			if(orders_payment_method == 'atm'){
				window.location = 'orders_atm.php';
			}
			if(orders_payment_method == 'bank'){
				window.location = 'orders_bank_counter.php';
			}
			if(orders_payment_method == 'internetbank'){
				window.location = 'orders_internet_banking.php';
			}

		}
	});
}

// ------------------------------------ END : Edit payment at view order page ---------------------------------------------------

function global_add_qa_topics(qacat2_id){
	$('#myform').ajaxSubmit({
		url : 'qa_process.php?action=add_topics&qacat2_id='+qacat2_id,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_add_topics').html(msg);
		}
	});
}

function global_qa_input_em(em){	
	document.getElementById('qa_detail').value = document.getElementById('qa_detail').value+em;
}


function global_qa_clear(qacat2_id){	
	document.getElementById('action').value = 'topic_cancel';	
	$('#myform').ajaxSubmit({
		url : 'qa_process.php?qacat2_id='+qacat2_id,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_add_topics').html(msg);
		}
	});
	
}

function private_refresh_topics_list(qacat2_id){
	$('#myform').ajaxSubmit({
		url : 'qa_process.php?action=refresh_topics&qacat2_id='+qacat2_id,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_topics_list').html(msg);
		}
	});
}

function global_qa_save(){
	//var qacat2_id = document.getElementById('qacat2_id').value;
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#myform').ajaxSubmit({
					url : 'qa_process.php',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_add_topics').html(msg);
						//private_refresh_topics_list(qacat2_id);
					}
				});
				
            }
        });
}


function global_add_question(qacat2_id, qa_id){
	$('#myform').ajaxSubmit({
		url : 'qa_process.php?action=add_question&qacat2_id='+qacat2_id+'&qa_id='+qa_id,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_add_question').html(msg);
		}
	});
}

function global_question_clear(qacat2_id, qa_id){
	//document.getElementById('qa_detail').value = '';
	document.getElementById('action').value = 'question_cancel';	
	$('#myform').ajaxSubmit({
		url : 'qa_process.php?qacat2_id='+qacat2_id+'&qa_id='+qa_id,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_add_question').html(msg);
		}
	});
}

function global_question_save(){
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#myform').ajaxSubmit({
					url : 'qa_process.php',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_add_question').html(msg);
					}
				});
				
            }
        });
}

function global_careers_apply(position_id){
	$('#myform').ajaxSubmit({
		url : 'careers_process.php?position_id='+position_id,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_careers').html(msg);
			top.location = '#';
		}
	});
}

function global_careers_cancel(){
	document.getElementById('action').value = 'cancel';
	$('#myform').ajaxSubmit({
		url : 'careers_process.php',
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_careers').html(msg);
			top.location = '#';
		}
	});
}



function global_careers_apply_save(){
	/*
	var check_val = 0;
	var applyer_name = document.getElementById('applyer_name');
	var applyer_nikename = document.getElementById('applyer_nikename');
	
	if(applyer_name.value == ''){
		applyer_name.style.backgroundColor = '#FFCCCC';
		applyer_name.focus();
		check_val = 1;
	}
	
	if(applyer_nikename.value == ''){
		applyer_nikename.backgroundColor = '#FFCCCC';
		check_val = 1;
	}
	
	if(check_val == 1){
		return false;	
	}
	*/


	/*
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {	
			
				
				
				$('#myform').ajaxSubmit({
					
					url : 'careers_process.php',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_careers').html(msg);
						top.location = '#';
						//window.location = 'careers.php';
					}
					
				});
				
				
				
            }
       });
	*/
	var applyer_name = document.getElementById('applyer_name');
	var applyer_name_alert = $("#applyer_name").attr("real");
	var applyer_nikename = document.getElementById('applyer_nikename');
	var applyer_nikename_alert = $("#applyer_nikename").attr("real");
	var applyer_height = document.getElementById('applyer_height');
	var applyer_height_alert = $("#applyer_height").attr("real");
	var applyer_weight = document.getElementById('applyer_weight');
	var applyer_weight_alert = $("#applyer_weight").attr("real");
	var applyer_domicile_address = document.getElementById('applyer_domicile_address');
	var applyer_domicile_address_alert = $("#applyer_domicile_address").attr("real");
	var applyer_birthday = document.getElementById('applyer_birthday');
	var applyer_birthday_alert = $("#applyer_birthday").attr("real");
	var applyer_religion = document.getElementById('applyer_religion');
	var applyer_religion_alert = $("#applyer_religion").attr("real");
	var applyer_nationality = document.getElementById('applyer_nationality');
	var applyer_nationality_alert = $("#applyer_nationality").attr("real");
	var applyer_origin = document.getElementById('applyer_origin');
	var applyer_origin_alert = $("#applyer_origin").attr("real");
	var applyer_current_address = document.getElementById('applyer_current_address');
	var applyer_current_address_alert = $("#applyer_current_address").attr("real");
	var applyer_tel = document.getElementById('applyer_tel');
	var applyer_tel_alert = $("#applyer_tel").attr("real");
	var applyer_email = document.getElementById('applyer_email');
	var applyer_email_alert = $("#applyer_email").attr("real");
	var applyer_email_incorrect_alert = $("#applyer_email_incorrect").attr("real");
	var applyer_start_date = document.getElementById('applyer_start_date');
	var applyer_start_date_alert = $("#applyer_start_date").attr("real");
	
	if(applyer_name.value == ''){
		alert(applyer_name_alert);
		applyer_name.focus();
		return false;
	}
	if(applyer_nikename.value == ''){
		alert(applyer_nikename_alert);
		applyer_nikename.focus();
		return false;
	}
	if(applyer_height.value == ''){
		alert(applyer_height_alert);
		applyer_height.focus();
		return false;
	}
	if(applyer_weight.value == ''){
		alert(applyer_weight_alert);
		applyer_weight.focus();
		return false;
	}
	if(applyer_domicile_address.value == ''){
		alert(applyer_domicile_address_alert);
		applyer_domicile_address.focus();
		return false;
	}
	if(applyer_birthday.value == ''){
		alert(applyer_birthday_alert);
		applyer_birthday.focus();
		return false;
	}
	if(applyer_religion.value == ''){
		alert(applyer_religion_alert);
		applyer_religion.focus();
		return false;
	}
	if(applyer_nationality.value == ''){
		alert(applyer_nationality_alert);
		applyer_nationality.focus();
		return false;
	}
	if(applyer_origin.value == ''){
		alert(applyer_origin_alert);
		applyer_origin.focus();
		return false;
	}
	if(applyer_current_address.value == ''){
		alert(applyer_current_address_alert);
		applyer_current_address.focus();
		return false;
	}
	if(applyer_tel.value == ''){
		alert(applyer_tel_alert);
		applyer_tel.focus();
		return false;
	}
	if(applyer_email.value == ''){
		alert(applyer_email_alert);
		applyer_email.focus();
		return false;
	}else{
		if(check_email_format(applyer_email.value) == 0){
			alert(applyer_email_incorrect_alert);
			applyer_email.focus();
			return false;
		}
	}
	if(applyer_start_date.value == ''){
		alert(applyer_start_date_alert);
		applyer_start_date.focus();
		return false;
	}
	
}


function global_call_calendar(obj_id){
	jQuery(function($){
		$("#"+obj_id).attachDatepicker();
	});
}

function global_open_panel(condition_val){
	if(condition_val == 1){
		document.getElementById('content_panel_01').style.display = '';
		document.getElementById('content_panel_02').style.display = 'none';
		document.getElementById('content_panel_03').style.display = 'none';
	}else if(condition_val == 2){
		document.getElementById('content_panel_01').style.display = 'none';
		document.getElementById('content_panel_02').style.display = '';
		document.getElementById('content_panel_03').style.display = 'none';
	}else if(condition_val == 3){
		document.getElementById('content_panel_01').style.display = 'none';
		document.getElementById('content_panel_02').style.display = 'none';
		document.getElementById('content_panel_03').style.display = '';
	}else{
		document.getElementById('content_panel_01').style.display = '';
		document.getElementById('content_panel_02').style.display = 'none';
		document.getElementById('content_panel_03').style.display = 'none';
	}
	top.location = '#';
}


function global_contact_send(){	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {	
			
				var contact_email = document.getElementById('contact_email').value;
				if(check_email_format(contact_email) == 0){
					document.getElementById('check_email').value = 'Y';
				}else{
					document.getElementById('check_email').value = '';
				}
			
				$('#myform').ajaxSubmit({
					url : 'contact_us_process.php',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_contact_err').html(msg);
						
						if(document.getElementById('contact_success').value == 'success'){
							document.getElementById('contact_name').value = '';
							document.getElementById('contact_email').value = '';
							document.getElementById('contact_subject').value = '';
							document.getElementById('contact_message').value = '';
							document.getElementById('check_email').value = '';
						}
					}
				});
				
            }
       });	
}

// ------------------------------ BEGIN : E-mail รับ-ไม่รับข่าวสาร DHC --------------------------------

function global_receive_email(){	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {	
			
				var dhcr_email_email = document.getElementById('dhcr_email_email').value;
				if(check_email_format(dhcr_email_email) == 0){
					document.getElementById('check_email').value = 'INCORRECT';
				}else{
					document.getElementById('check_email').value = 'CORRECT';
				}
			
				$('#myform').ajaxSubmit({
					url : 'dhc_receive_email_process.php?action=save_request',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_body').html(msg);
						if(document.getElementById('receive_success').value == 'success'){
							document.getElementById('dhcr_email_email').value = '';
							timer = setTimeout('global_receive_email_go()',6000);
						}
					}
				});
				
				
            }
       });	
}



function global_receive_email_go(){
	window.location='dhc_receive_email.php';
}


/*
function global_receive_email_status(url_process){		
	$('#myform').ajaxSubmit({
		url : url_process,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			global_receive_email_go();
		}
	});		
}
*/


// ------------------------------ END : E-mail รับ-ไม่รับข่าวสาร DHC ----------------------------------


function global_express_product(express_pro_id, product_id){
	document.getElementById('express_order_id_'+product_id).value = express_pro_id;	
}

function global_news(newscat_id){
	if(newscat_id != ''){
		goto_url = 	'news_process.php?newscat_id='+newscat_id
	}else{
		goto_url = 'news_process.php';
	}
	$('#myform').ajaxSubmit({
		url : goto_url,
		clearForm: false,
		success: function(msg){
			$('#div_news').html(msg);
		}
	});	
}

function global_activity_gallery(album_id){
	$('#myform').ajaxSubmit({
		url : 'news_process.php?album_id='+album_id+'&action=gallery',
		beforeSubmit: block,
		clearForm: true,
		success: function(msg){
			$('#div_news').html(msg);
		}
	});
}


// ------------------------- BEGIN : Members Join Games ---------------------------------------------
function global_members_join_games(){
	var url_goto = 'memberjoin_process.php';
	$('#myform').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_memberjoin_games').html(msg);
			if(document.getElementById('games_success').value == 'success'){
				window.location='memberjoin_games.php';
			}
		}
	});
}

function global_members_join_login(){
	var url_goto = 'memberjoin_process.php';
	document.getElementById('action').value = 'login';
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#myform').ajaxSubmit({
					url : url_goto,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_memberjoin_games').html(msg);
						
						if(document.getElementById('check_login_success').value == 'success'){
							window.location = 'memberjoin_games.php';
						}
						
					}
				});
				
            }
        });
}

function global_members_join_play_games(){
	var url_goto = 'memberjoin_process.php?action=play_games';
	$('#myform').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_memberjoin_games').html(msg);
		}
	});
}

function global_members_join_cancel_games(){
	var url_goto = 'memberjoin_process.php?action=cancel_games';
	$('#myform').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_memberjoin_games').html(msg);
		}
	});
}

function global_members_join_check_submit(){
	var joingame_reason_love = document.getElementById('joingame_reason_love');
	var joingame_img = document.getElementById('joingame_img');
	
	if(joingame_img.value == ''){
		alert($("#joingame_img").attr("real"));
		joingame_img.focus();
		return false;
	}
	
	if(joingame_reason_love.value == ''){
		alert($("#joingame_reason_love").attr("real"));	
		joingame_reason_love.focus();
		return false;
	}
}

function global_em(em,obj){	
	document.getElementById(obj).value = document.getElementById(obj).value+em;
}

function global_members_join_games_detail(joingame_id){
	var url_goto = 'memberjoin_process.php?joingame_id='+joingame_id+'&action=goto_detail';
	$('#myform').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			window.location = 'memberjoin_games_detail.php';
		}
	});
}

// ------------------------- END : Members Join Games -----------------------------------------------




// ------------------------- BEGIN : Members Email Games --------------------------------------------

function global_members_email_games(){
	var url_goto = 'members_email_games_process.php';
	$('#myform').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_members_email_games').html(msg);
			if(document.getElementById('games_success').value == 'success'){
				window.location='members_email_games_send.php';
			}
		}
	});
}

function global_members_email_games_login(){
	var url_goto = 'members_email_games_process.php?action=login';
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#myform').ajaxSubmit({
					url : url_goto,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_members_email_games').html(msg);
						
						if(document.getElementById('check_login_success').value == 'success'){
							window.location = 'members_email_games.php';
						}
						
					}
				});
				
            }
        });
}

function global_members_email_games_add(){
	
	var url_goto = 'members_email_games_process.php?action=add';
	var emailgame_friends_email = document.getElementById('emailgame_friends_email');
	
	if(check_email_format(emailgame_friends_email.value) == 0){
		document.getElementById('email_incorrect').value = 'Y';
	}else{
		document.getElementById('email_incorrect').value = 'N';
	}
	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {
				
				
				$('#myform').ajaxSubmit({
					url : url_goto,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_email_games_send').html(msg);
						
						if(document.getElementById('check_success').value == 'success'){
							document.getElementById('emailgame_friends_email').value = '';
							document.getElementById('emailgame_friends_name').value = '';
						}
						
					}
				});
				
            }
        });
	
}



function global_members_email_games_send(){
	var url_goto = 'members_email_games_process.php?action=send';
	$('#myform').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_email_games_send').html(msg);
		}
	});
}


// ------------------------- END : Members Email Games ----------------------------------------------



// -------------------------- BEGIN : Game of OC 2009-03 --------------------------------------------------

function global_oc200903_game(){
	
	var url_goto = 'oc200903_game_process.php?action=save_answer';
	
	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {
				
				
				$('#myform').ajaxSubmit({
					url : url_goto,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_oc200903_game').html(msg);						
					}
				});
				
            }
        });
	
}

function global_oc200903_game_login(){
	var url_goto = 'oc200903_game_process.php?action=login';
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#myform').ajaxSubmit({
					url : url_goto,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_oc200903_game').html(msg);						
					}
				});
				
            }
        });
}

// -------------------------- END : Game of OC 2009-03 ----------------------------------------------------



// -------------------------- BEGIN : Member Join Member Share Game ---------------------------------------

function global_member_join_shar_game(myform, obj_respond, url_process, url_after){
	
	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#"+myform).validate({
    	    meta: "validate",
            submitHandler: function(form) {


				$('#'+myform).ajaxSubmit({
					url : url_process,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						if(url_after != ''){
							window.location = url_after;
						}else{
							$('#'+obj_respond).html(msg);
						}
					}
				});
				
				
            }
        });
	
}

// -------------------------- END : Member Join Member Share Game -----------------------------------------

// ------------------------- BEGIN : DHC Olive Club Cover Activity ----------------------------------
function global_game_cover_vote(div_obj, url_vote){	
	$('#myform').ajaxSubmit({
		url : url_vote,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#'+div_obj).html(msg);
		}
	});
}


function global_display_cover_iframe(url){
		oc_cover_game.location.href=url;
}
	
	
	
	
	function global_open_cover_style(condition){
		if(condition == 'main'){
			document.getElementById('div_cover_main').style.display = '';
			document.getElementById('div_cover_left').style.display = 'none';			
			document.getElementById('div_cover_right').style.display = 'none';		
			document.getElementById('div_cover_bottom').style.display = 'none';			
			document.getElementById('div_cover_dhc_style').style.display = 'none';
		}
		
		
		
		if(condition == 'left'){
			document.getElementById('div_cover_main').style.display = 'none';
			document.getElementById('div_cover_left').style.display = '';			
			document.getElementById('div_cover_right').style.display = 'none';		
			document.getElementById('div_cover_bottom').style.display = 'none';			
			document.getElementById('div_cover_dhc_style').style.display = 'none';			
		}
		
		
		
		if(condition == 'right'){
			document.getElementById('div_cover_main').style.display = 'none';
			document.getElementById('div_cover_left').style.display = 'none';			
			document.getElementById('div_cover_right').style.display = '';		
			document.getElementById('div_cover_bottom').style.display = 'none';			
			document.getElementById('div_cover_dhc_style').style.display = 'none';
		}
		
		
		if(condition == 'bottom'){
			document.getElementById('div_cover_main').style.display = 'none';
			document.getElementById('div_cover_left').style.display = 'none';			
			document.getElementById('div_cover_right').style.display = 'none';		
			document.getElementById('div_cover_bottom').style.display = '';			
			document.getElementById('div_cover_dhc_style').style.display = 'none';
		}
		
		if(condition == 'dhc_style'){
			document.getElementById('div_cover_main').style.display = 'none';
			document.getElementById('div_cover_left').style.display = 'none';			
			document.getElementById('div_cover_right').style.display = 'none';		
			document.getElementById('div_cover_bottom').style.display = 'none';			
			document.getElementById('div_cover_dhc_style').style.display = '';
		}
		
		
	}
	

function global_game_cover_save(){	
		jQuery.validator.messages.required = "";
		$.metadata.setType("attr", "rule");
		var v = jQuery("#myform").validate({
				meta: "validate",
				submitHandler: function(form) {	
				
					$('#myform').ajaxSubmit({
						url : 'game_cover_process.php?action=save_cover',
						beforeSubmit: block,
						clearForm: false,
						success: function(msg){
						
							$('#div_body').html(msg);
							
							timer = setTimeout('global_game_cover_finish()',5000);
						}
					});
					
				}
		   });	
}
	
function global_game_cover_finish(){
	window.location.href = 'game_cover_list.php';
}




function global_game_cover_edit(){	
		jQuery.validator.messages.required = "";
		$.metadata.setType("attr", "rule");
		var v = jQuery("#myform").validate({
				meta: "validate",
				submitHandler: function(form) {	
				
					$('#myform').ajaxSubmit({
						url : 'game_cover_process.php?action=save_cover',
						beforeSubmit: block,
						clearForm: false,
						success: function(msg){
						
							$('#div_body').html(msg);
							
							timer = setTimeout('global_game_cover_edit_finish()',5000);
						}
					});
					
				}
		   });	
}
	
function global_game_cover_edit_finish(){
	window.location.href = 'game_cover_my_list.php';
}


function global_game_cover_delete(saveForm,url1,url2){
	if(confirm('คุณต้องการลบปก DHC ของคุณ ใช่หรือไม่?') == true) {
		$('#'+saveForm).ajaxSubmit({
			url : url1,
			clearForm: true,
			success: function(msg){
				
				if(url2){
					window.location = url2;
				}else{
					$('#'+divObj).html(msg);
				}
			}
		});
	}
}


function global_game_cover_comment(){	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
	var v = jQuery("#myform").validate({
			meta: "validate",
			submitHandler: function(form) {	
				
				$('#myform').ajaxSubmit({
					url : 'game_cover_process.php?action=save_comment',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						
						$('#div_body').html(msg);
						
						if(document.getElementById('check_success').value == 'success'){
							timer = setTimeout('global_game_cover_comment_finish()',4000);
						}
					}
				});
					
			}
		  });	
}
	
function global_game_cover_comment_finish(){
	window.location.href = 'game_cover_detail.php';
}


function global_game_cover_login(myform, obj_respond, url_process){
	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#"+myform).validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#'+myform).ajaxSubmit({
					url : url_process,
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#'+obj_respond).html(msg);
						if(document.getElementById('check_success').value == 'success'){
							window.location.href='game_cover_my_list.php';
						}
					}
				});
				
            }
        });
	
}

function global_CheckIsIE(){ 
	if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER'){ 
		return true;
	}else{ 
		return false; 
	} 
} 


function global_game_cover_PrintThisPage(){
	if(global_CheckIsIE() == true){ 
		document.oc_cover_game_detail.focus(); 
		document.oc_cover_game_detail.print(); 
	}else{ 
		window.frames['oc_cover_game_detail'].focus(); 
		window.frames['oc_cover_game_detail'].print(); 
	} 
} 

	
// ------------------------- END : DHC Olive Club Cover Activity ------------------------------------


// ------------------------- Images Over ------------------------------------------------------------
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
// --------------------------------------------------------------------------------------------------


// --------------------------- // Check input number only -------------------------------------------

function keypress(e) {
	var keycode = (event) ? event.keyCode : e.which;	
	if (keycode < 45 || keycode > 57) {		
		if (keycode == 13)
			$(document.body).focus();
		else 
			if ( ! e) event.returnValue = false; else e.preventDefault();		
		return false;			
//		alert('กรุณาป้อนข้อมูลเป็นตัวเลขเท่านั้น');
	}

}

// --------------------------------------------------------------------------------------------------

// --------------------------- // Blink // ----------------------------------------------------------

	function BlinkLoad(){
		setInterval('blinkIt()',550)
	
	}
	
	function blinkIt() {
	
		
	
		if (!document.all) return;
	
		else {
	
			for(i=0;i<document.all.tags('blink').length;i++){
	
				s=document.all.tags('blink')[i];
	
				s.style.visibility=(s.style.visibility=='visible')?'hidden':'visible';
	
			}
	
		}
	
	}

// ------------------------- // Bling // ------------------------------------------------------------

// ------------------------- BEGIN : Middle window --------------------------------------------------
function kPopup(theURL,winName,posx,posy,width,height,features) {
	if (!winName.closed && winName.location) {
		winName.location.href = theURL;
	} else {
		var left,top = 0;
		if(posx=="right"){
			left = screen.width - width;
		} else if(posx=="center") {
			left = (screen.width - width)/2;
		} else {
			left = 0;
		}

		if(posy=="bottom") {
			top = screen.height - height;
		} else if(posy=="middle") {
			top = (screen.height - height)/2;
		} else {
			top = 0;
		}
		winName=window.open(theURL,winName,"width="+width+",height="+height+",left="+left+",top="+top+","+features);
		if (!winName.opener) {
			winName.opener = self;
		}
	}
	if (window.focus) {
		winName.focus();
	}
	return false;
}



function global_popup_midel(theURL,winName,posx,posy,width,height,features) {
	if (!winName.closed && winName.location) {
		winName.location.href = theURL;
	} else {
		var left,top = 0;
		if(posx=="right"){
			left = screen.width - width;
		} else if(posx=="center") {
			left = (screen.width - width)/2;
		} else {
			left = 0;
		}

		if(posy=="bottom") {
			top = screen.height - height;
		} else if(posy=="middle") {
			top = (screen.height - height)/2;
		} else {
			top = 0;
		}		
		winName=window.open(theURL,winName,'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=yes,resizable=no,width='+width+',height='+height+',left='+left+',top='+top);
		
		if (!winName.opener) {
			winName.opener = self;
		}
	}
	if (window.focus) {
		winName.focus();
	}
	return false;
}
// ------------------------- END : Middle window ----------------------------------------------------

// ------------------------- BEGIN : Printing -------------------------------------------------------
function global_print(){  
	if (window.print) {
		window.print() ;  
	}else{
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
	}
}
// ------------------------- END : Printing ---------------------------------------------------------

// ------------------------- BEGIN : Show image -----------------------------------------------------
function MM_openBrWindow(theURL,winName,features){
		window.open(theURL,winName,features);
}
function decision(message, url){
		if(confirm(message)) location.href = url;
}
	
	hs.registerOverlay(
    	{
    		thumbnailId: null,
    		overlayId: 'controlbar',
    		position: 'top right',
    		hideOnMouseOut: true
		}
	);
	
    hs.graphicsDir = 'js/highslide/graphics/';
    hs.outlineType = 'rounded-white';
    window.onload = function() {
    hs.preloadImages(5);
}
// ------------------------- END : Show image --------------------------------------------------------



/* ----------------- BEGIN : For Tisco promotion ---------------------------*/

function global_strtoupper(obj) {
	var obj_value = document.getElementById(obj.id).value;
	document.getElementById(obj.id).value = obj_value.toUpperCase();
}


function global_tisco_check_code(){
	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#myform').ajaxSubmit({
					url : 'products_tisco_process.php?action=check_code',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						$('#div_check_code').html(msg);
						if(document.getElementById('check_tisco_code').value == 'success'){
							window.location.href='products_tisco.php';
						}
					}
				});
				
            }
        });
	
}
/* ----------------- END : For Tisco promotion -----------------------------*/

// ----------- Function Webboard 26 ต.ค.52 --------------//


function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}


function global_check_login(){
	var url_goto = 'webboard_process.php';
	$('#myform').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_members_login').html(msg);
			
			/*if(document.getElementById('check_login_success').value == 'successful'){
				window.location = 'webboard_post.php';
			}*/
			
		}
	});
}


function global_members_login_webboard(){
	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#myform').ajaxSubmit({
					url : 'members_login.php',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						
						$('#div_members_login').html(msg);
						if(document.getElementById('check_login_success').value == 'successful'){
							window.location.href = window.location.href;
						}
					}
				});
				
            }
        });
	

	
}

function global_post_webboard(url_goto,room_board_id){
	
	$('#myform').ajaxSubmit({
			beforeSubmit: block,
			clearForm: false,
			success: function(msg){
						
				$('#div_members_login').html(msg);
				window.location.href = url_goto+'?room_board_id='+room_board_id ;
				
			}
	});
	
}

function global_webboard_input_em(em){	
	document.getElementById('topic_board_detail').value = document.getElementById('topic_board_detail').value+em;
}

function global_webboard_input_reply_em(em){	
	document.getElementById('reply_board_detail').value = document.getElementById('reply_board_detail').value+em;
}
function global_webboard_save(room_board_id){
	//var qacat2_id = document.getElementById('qacat2_id').value;

	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#myform").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#myform').ajaxSubmit({
					url : 'webboard_process.php',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						
						$('#div_members_login').html(msg);
							
							if(document.getElementById('post_board_success').value == 1){
								if(room_board_id==0){
									window.location.href = 'webboard.php';
								}else{
									window.location.href = 'webboard_topic_list.php?room_board_id='+room_board_id;
								}
							}
					}
				});
				
            }
        });
}


function global_webboard_save_reply(room_board_id,topic_code){
	
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#form_reply").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#form_reply').ajaxSubmit({
					url : 'webboard_process.php',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						
						$('#div_reply_webboard').html(msg);
							var page = $("#Page").val();
						
							if($('#reply_board_success').val() == 1){
								 window.location.href = 'webboard_showtopic.php?topic_code='+topic_code+'&room_board_id='+room_board_id+'&Page='+page;
							}
						
						}
				});
				
            }
        });
}

function goto_login(){
	document.getElementById('members_username').focus();
}

function global_webboard_save_report(){
	jQuery.validator.messages.required = "";
	$.metadata.setType("attr", "rule");
    var v = jQuery("#form_report").validate({
    	    meta: "validate",
            submitHandler: function(form) {

				$('#form_report').ajaxSubmit({
					url : 'webboard_process.php',
					beforeSubmit: block,
					clearForm: false,
					success: function(msg){
						
						$('#div_report_webboard').html(msg);
						
						/*if(document.getElementById('report_board_success').value == 1){
							window.location.href = 'webboard_showtopic.php?topic_code='+topic_code+'&room_board_id='+room_board_id;
						}*/
						
					}
				});
				
            }
        });
	
}


function global_reply_webboard(room_board_id,topic_board_code){
	
	$('#form_reply').ajaxSubmit({
			beforeSubmit: block,
			clearForm: false,
			success: function(msg){
						
				$('#div_members_login').html(msg);
				window.location.href = 'webboard_reply.php?room_board_id='+room_board_id+'&topic_board_code='+topic_board_code ;
				
				}
	});
	
}
// ----------- Function Webboard 26 ต.ค.52 --------------//

/*********************************/
/*------------New Function--------------*/
/********************************/
function cv2num(str){ return (str.replace(/,/ig,'') * 1); }

function number_format (number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands
    //
    // version: 906.1806
    // discuss at: http://phpjs.org/functions/number_format
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival
    // +     input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +     improved by: davook
    // +     improved by: Brett Zamir (http://brett-zamir.me)
    // +     input by: Jay Klehr
    // +     improved by: Brett Zamir (http://brett-zamir.me)
    // +     input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'
    // *     example 10: number_format('1.20', 2);
    // *     returns 10: '1.20'
    // *     example 11: number_format('1.20', 4);
    // *     returns 11: '1.2000'
    // *     example 12: number_format('1.2000', 3);
    // *     returns 12: '1.200'
    var n = number, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

function currency_format($number, $digit){
	return number_format($number, $digit, '.', ',');
}

function display_out_of_stock(obj, mainId){
	var selfInx = obj.selectedIndex;
	obj = $(obj);
	$('#express_order_id_'+mainId).val(obj.val());
	var qty = $('#qty_multiple_id_'+mainId).val().split('||')[selfInx].split('::')[1];
	if (qty > 0){
		$('#tr_'+mainId+' td:last').children('img:first').css('display', 'block');
		$('#tr_'+mainId+' td:last').children('img:last').css('display', 'none');
		$('#tr_'+mainId+' div').children('select').val(1);
		$('#tr_'+mainId+' div').css('display', 'block');
	} else {
		$('#tr_'+mainId+' td:last').children('img:first').css('display', 'none');
		$('#tr_'+mainId+' td:last').children('img:last').css('display', 'block');			
		$('#tr_'+mainId+' div').css('display', 'none');		
	}
}

function calculate_exchange_point(){
	var objPoint = { 
		factor: 10,
		minimum: function(recMin){ return recMin; },
		msg_min: function(recMin){ return "คะแนนที่ใช้สำหรับแลกของกำนัลที่คุณเลือกต้อง "+recMin+" คะแนนขึ้นไปค่ะ"; },
		msg_notenough: "คะแนนของคุณไม่เพียงพอในการแลกของกำนัลค่ะ"
	};
	var bf_cal = cv2num($('#div_before_cal').text());
	var used = cv2num($('#txt_point_used').val());
	var p_min = cv2num($('#hdn_point_min').val());
	if ( ! used){
		$('#txt_point_used').val($('#hdn_point_min').val()).focus();
	} else if (used < p_min){
		alert(objPoint.msg_min(p_min));
		$('#txt_point_used').val($('#hdn_point_min').val()).focus();
	} else if (used > Math.ceil(bf_cal / objPoint.factor) && used > p_min){
		$('#txt_point_used').val(Math.ceil(bf_cal / objPoint.factor)).focus();
	}
	used = cv2num($('#txt_point_used').val());
	var used_value = currency_format((used * objPoint.factor), 2);
	$('#txt_point_used_value').val(used_value);
	global_calculate_ex_point_orders();
}

function global_calculate_ex_point_orders(ddObj){
	if (ddObj){
		var obj = $(ddObj); //obj jquery style		
		var sum_voucher = 0;
		var chk_warn = false;
		//check duplicate with another	
		$("select[name='orders_voucher_code[]']").each(function(inx){
			if ($(this).val() == obj.val() && $(this).attr('id') != obj.attr('id') && obj.val() != '0'){
				alert("คุณเลือกรหัสคูปองใบนี้แล้วค่ะ");
				obj.val(0);
				chk_warn = true;
				sum_voucher = 0;
				return;
			}
			sum_voucher += cv2num($('#orders_voucher_value_'+inx).val());			
		});
		if ( ! chk_warn){
			if (obj.val() != '0' && sum_voucher >= cv2num($('#div_grand_total').text())){	// && cv2num($('#div_grand_total').text())
				alert('จำนวนเงินเพียงพอสำหรับยอดที่ต้องชำระแล้วค่ะ');
				obj.val(0);
			}
		}
		//---		
		var inx = obj.attr('id').substring(obj.attr('id').lastIndexOf('_') + 1);
		var value = obj.val().split('_').pop();		
		$('#orders_voucher_value_'+inx).val(currency_format(value, 2));
	}
	var url_goto = 'exchange_point_orders_process.php?action_calculate=calculate_order';	
	$('#myform').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){			
			$('#div_orders_messagebox').html(msg);

			$('#div_grand_total').text($('#grand_total_diff_return_value').val());
			$('#div_grand_total_append').text($('#grand_total_append_return_value').val());			
			$('#div_orc_point').text($('#orc_point_return').val());
			$('#div_none_orc_point').text($('#none_orc_point_return').val());
			
			$('#orders_transport_cost').val(cv2num($('#transport_cost_return_value').val()));
			$('#grand_total_post_value').val(cv2num($('#grand_total_post_return_value').val()));
			
			$('#mem_point_value').val(cv2num($('#mem_none_orc_point_total_return').val()));
			
			$('#mem_orc_point_one').val(cv2num($('#mem_orc_point_one_return').val()));
			$('#mem_orc_point_two').val(cv2num($('#mem_orc_point_two_return').val()));
			$('#mem_orc_point_three').val(cv2num($('#mem_orc_point_three_return').val()));
			$('#mem_orc_point_four').val(cv2num($('#mem_orc_point_four_return').val()));
			$('#mem_orc_point_five').val(cv2num($('#mem_orc_point_five_return').val()));
			$('#mem_orc_point_total').val(cv2num($('#mem_orc_point_total_return').val()));
			
			$('#mem_none_orc_point_one').val(cv2num($('#mem_none_orc_point_one_return').val()));
			$('#mem_none_orc_point_two').val(cv2num($('#mem_none_orc_point_two_return').val()));
			$('#mem_none_orc_point_three').val(cv2num($('#mem_none_orc_point_three_return').val()));
			$('#mem_none_orc_point_four').val(cv2num($('#mem_none_orc_point_four_return').val()));
			$('#mem_none_orc_point_five').val(cv2num($('#mem_none_orc_point_five_return').val()));
			$('#mem_none_orc_point_total').val(cv2num($('#mem_none_orc_point_total_return').val()));
			
			//check value before display payment method
			var cv2bool =  ! Boolean(parseFloat($('#grand_total_post_value').val()));
			if ($('#orders_payment_method:visible').length) $('#orders_payment_method').attr('disabled', cv2bool);
			if ($('#orders_pod_method:visible').length) $('#orders_pod_method').attr('disabled', cv2bool);
			//---
		}
	});	
}

function add_voucher(){
	if ($('tr.voucher').length == $("select[name='orders_voucher_code[]']:first option").length - 1){
		alert("ขออภัย ไม่สามารถเพิ่มรายการคูปองได้อีก\nเนื่องจากเกินจำนวนคูปองที่มีอยู่ค่ะ");
		return false;
	}
	var last_voucher = $('tr.voucher:last');
	var new_voucher = last_voucher.clone(true);
	new_voucher.insertAfter(last_voucher);
	//***get last inx
	var inx = parseInt(last_voucher.find('select').attr('id').substring(last_voucher.find('select').attr('id').lastIndexOf('_') + 1)) + 1;	
	last_voucher.find("img[name='btnaddrow']").css('visibility', 'hidden');
	//***clean new
	new_voucher.find('select').attr('id', 'orders_voucher_code_'+inx).val(0);
	new_voucher.find("input[name='orders_voucher_value[]']").attr('id', 'orders_voucher_value_'+inx).val(currency_format(0, 2));	
//	new_voucher.find('img').attr('src', 'images/addmore_out.gif').mouseout(MM_swapImgRestore).mouseover(function(){
//		MM_swapImage('btnaddrow_','','images/addmore_over.gif',1)				 
//	});;
//	onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('btnaddrow_<?=$inx;?>','','images/addmore_over.gif',1)"
}

// --------------------- DHC Treatment Discount promotion 25 พ.ย. 52 - 10 ม.ค. 53 หลังจากนี้ให้ลบทิ้ง ------------------------

function global_dhc_treatment_alert_before_add(product_id, obj, url, product_price,product_code){
	
	//alert(product_treatment_code_1);
	if($('#treatment_no').val()==2){
			var answer = confirm('***คุณต้องการเปลี่ยนสินค้าโปรโมชั่น Facial Treatment ที่เลือกหรือไม่ ถ้าต้องการระบบจะลบสินค้าในตระกร้าของคุณทั้งหมด***\n***หากต้องการเลือกโปรโมชั่น Facial Treatment เพิ่ม กรุณาทำการเปิด Order ใหม่');
			if(answer == true){
					delete_to_cart('cart_delete.php?action=deleteall');
					return false ;				
			}else{
				return false ;
			}
		}else if($('#treatment_no').val()==null){
			alert('ทางบริษัท ดีเอชซี (ประเทศไทย) จำกัด ได้จัดโปรโมชั่น Treatment Special Discount 50%ตั้งแต่วันที่ 25 พฤศจิกายน 2552 ถึง 10 มกราคม 2553\n\n\t*** เพียงซื้อคอร์สนวดหน้ารับสิทธิพิเศษ ลดทันที 50% สำหรับคอร์สที่2 เมื่อซื้อในครั้งเดียวกัน(ไม่รวมคอร์สทรีทเมนต์อื่นนอกจากที่กำหนด) สามารถเลือก Facial Treatment คอร์สใดๆก็ได้ไม่จำเป็นต้องเป็นคอร์สเดียวกันแ่ต่คอร์สที่ได้รับส่วนลด 50% จะต้องมีราคาไม่สูงกว่าึคอร์สแรกทีี่่เลือกค่ะ ');
		}
		add_to_cart(product_id, obj, url,product_code);
}

// --------------------- DHC Treatment Discount promotion 25 พ.ย. 52 - 10 ม.ค. 53 หลังจากนี้ให้ลบทิ้ง ------------------------

function add_to_cart_fleece(product_id, obj, url,product_code){
/*	alert(product_id);
	return ;*/
	if($(':checkbox + img').length){
		alert('ขอโทษน่ะค่ะ ใบเสร็จ 1 ใบสามารถใช้เป็นสิทธิ์แลกซื้อได้ 1สิทธิ์ค่ะ');
		return ;
	}
	var go_url = url+'?product_id='+product_id+'&quantity='+document.getElementById(obj).value+'&action=get_fleece';
	$('#myform').ajaxSubmit({
		url : go_url,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){
			$('#div_cart').html(msg);
			$('#'+obj).val(1);
			if($('#add_fleece').val()==1){
				window.location.href="orders.php";	
			}
		}
	});
	
}

function delete_to_cart_fleece(url){
	$('#myform').ajaxSubmit({
		url : url,
		beforeSubmit: block,
		clearForm: false,
		success: function(msg){			
			$('#div_cart').html(msg);
			if($('#del_fleece').val()==1){
				window.location.href="orders.php";	
			}
		}
	});
}


/*####### Surprise gift  8 - 19 feb 2010 #########*/

function chk_surprise(){
			if($('td.surprise_card :checked').attr('checked')){
				alert('สงวนสิทธิ์ให้บริการเฉพาะในเขตกทม.และปริมณฑล ด้วยการจัดส่งแบบปกติเท่านั้นค่ะ (ไม่รวมบริการ Payment on Delivery)');
				$('td.surprise_card :checked').attr('checked',false);
				$('#surprise_text').val('');
				$('#reciever').val('');
				$('#sender').val('');
				$('#surprise_text').attr('disabled',true);	
				$('#tb_display').css('display','none');
			}
		}
		
function surprise_gift(obj){
		 	var chk = $(obj).attr('checked') ;
			var chk_pod = $('#orders_send_address02').attr('checked');
			if(chk_pod){
				alert('สงวนสิทธิ์ให้บริการเฉพาะในเขตกทม.และปริมณฑล ด้วยการจัดส่งแบบปกติเท่านั้นค่ะ (ไม่รวมบริการ Payment on Delivery)');
				$('td.surprise_card :checked').attr('checked',false);
				$('#surprise_text').val('');
				
				$('#surprise_text').attr('disabled',true);	
				
				//$('#orders_send_address01').attr('checked',true);
				/*$('#surprise_text').attr('disabled',false);
				$('#surprise_text').val('ของขวัญเพื่อคุณคนพิเศษ ขอให้มีความสุข สดใส สุขภาพดี ในทุกๆวันนะคะ');*/
				return false;
			}
			
			$('td.surprise_card :checked').attr('checked',false);
			
			
			$(obj).attr('checked',chk);
			$('#surprise_text').attr('disabled',!chk);
			$('#reciever').val('');
			$('#sender').val('');
			if(chk){
				clr_form() ;
				$('#surprise_text').val('ของขวัญเพื่อคุณคนพิเศษ ขอให้มีความสุข สดใส สุขภาพดี ในทุกๆวันนะคะ');
			}else{
				$('#surprise_text').val('');
				
			}
		
		}	
		
function clr_form(){
			$('#div_normal_address').find('select').val(0) ;
			$('#div_normal_address').find(':text').val('');
			
}
		
function chk_province_surprise(province_id){
			
			if($('td.surprise_card :checked').attr('checked') && (province_id!=1 && province_id!=2 && province_id!=4 )){
				alert('DHC Surprise Gift Delivery สงวนสิืทธิ์ให้บริการเฉพาะในเขตกทม.และปริมณฑลค่ะ');
				$('#members_conf_order_province').val(0) ;
				return false ;
			}
}
		
function show_card(code){
			
			if($('#chk_card1').attr('checked') || $('#chk_card2').attr('checked') || $('#chk_card3').attr('checked')  ){
				$('#tb_display').css('display','block');
				$('#show_card').css('background','url(images/card/l/'+code+'.jpg) no-repeat top center');
			
				
			}else {
				$('#show_card').css('background','none');
				$('#tb_display').css('display','none');
				
			}
			
			
}

    
		
/*####### Surprise gift  8 - 19 feb 2010 #########*/




function add_to_cart_set_983(product_id, obj, url, sub_product_id){
//	alert(sub_product_id);return;
	var chk = 0 ;

		$("select[name='sub_product_code_983[]']").each(function(){
																
				if($(this).val()==''){
					alert('คุณยัำงเลือกสินค้าไม่ครบค่ะ');
					chk =1 ;
					return false;
				}
			 }
			)
	
	
	if(chk==0){
				var go_url = url+'?product_id='+product_id+'&quantity='+document.getElementById(obj).value+'&sub_product_id_mini_sales='+sub_product_id ;
			$('#myform').ajaxSubmit({
				url : go_url,
				beforeSubmit: block,
				clearForm: false,
				success: function(msg){
					
					$('#div_cart').html(msg);
					
				}
			});
	}
	
	
}


function add_to_cart_set_982(product_id, obj, url, sub_product_id){

var chk = 0 ;

		$("select[name='sub_product_code_982[]']").each(function(){
																
				if($(this).val()==''){
					alert('คุณยัำงเลือกสินค้าไม่ครบค่ะ');
					chk =1 ;
					return false;
				}
			 }
			)
	
	
	if(chk==0){
				var go_url = url+'?product_id='+product_id+'&quantity='+document.getElementById(obj).value+'&sub_product_id_mini_sales='+sub_product_id ;
			$('#myform').ajaxSubmit({
				url : go_url,
				beforeSubmit: block,
				clearForm: false,
				success: function(msg){
					
					$('#div_cart').html(msg);
					
				}
			});
	}

}




