$().ajaxStop($.unblockUI);
function block() {
	$.blockUI(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 getUrl(url_go) {
	block();
	window.location = url_go;
}

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;
	}
}


// --------------------------- 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 members_call_calendar() {
	jQuery(function($){
		$("#members_birthday").attachDatepicker();
	});
}

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);
	})
}

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_top_view(products_id){
	var url_goto = 'products_view_count.php?products_id='+products_id;
	$('#header_form').ajaxSubmit({
		url : url_goto,
		clearForm: false,
		success: function(msg){			
		}
	});
}


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_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_tel').value = '';
							document.getElementById('contact_subject').value = '';
							document.getElementById('contact_message').value = '';
							document.getElementById('check_email').value = '';
						}
					}
				});
				
            }
       });	
}



function global_products_tap(url_goto){
	$('#myform').ajaxSubmit({
		url : url_goto,
		beforeSubmit: block,
		clearForm: true,
		success: function(msg){
			$('#div_body').html(msg);			
		}
	});
}


// ------------------------- 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() {

		if (event.keyCode < 45 || event.keyCode > 57) {
	
			event.returnValue = 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 global_poup_middle(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;
}
// ------------------------- 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 -------------------------------------------------------
