jQuery(document).ready(function() {
	jQuery(".kv-dialog-close").click(function(e) {
		jQuery("#kv_dialog").hide();
		if (jQuery(".show_login_form").length > 0) {
			jQuery('#login_dropdown').slideDown();
			jQuery(this).attr("class","kv-dialog-close");	
		}
		jQuery("#dimming").hide();
	});
	
	jQuery("#dimming").click(function() {
		jQuery("#kv_dialog").hide();
		jQuery("#preorderpopup").hide();
		jQuery("#product_instock_popup").hide();
		jQuery("#dimming").hide();
	});	
	
	jQuery("#preorder").click(function() {
		show_preorder();
	});		
});

function show_dialog(title, content) {
	jQuery("#kv_dialog_title").text(title);
	jQuery("#kv_dialog_content").html(content);
	if (jQuery("#kv_dialog_content").html() != '') {	
		jQuery("#dimming").width(jQuery(window).width());
		jQuery("#dimming").height(jQuery(document).height());
		jQuery("#dimming").show();
		jQuery("#kv_dialog").show();
	}
}

function show_preorder() {
	jQuery("#dimming").width(jQuery(window).width());
	jQuery("#dimming").height(jQuery(document).height());
	jQuery("#dimming").show();
	jQuery("#preorderpopup").show();
}

function show_map(branch_address, branch_phone, product_name, product_colors, branch_map) {
	jQuery("#branch_address").html(branch_address);
	jQuery("#branch_phone").html(branch_phone);
	jQuery("#product_name").html(product_name);
	jQuery("#product_colors").html(product_colors);
	jQuery("#branch_map").attr("src", branch_map);
	
	jQuery("#dimming").width(jQuery(window).width());
	jQuery("#dimming").height(jQuery(document).height());
	jQuery("#dimming").show();
	
	var ww = jQuery(window).width();
	var left = parseInt((ww - 600)/2); 
	
	jQuery("#product_instock_popup").css('left', left);
	jQuery("#product_instock_popup").show();
}


function send_preorder() {
	if (jQuery("#po_fullname").val() == '') {
		alert('Bạn vui lòng nhập Họ và Tên !');
		jQuery("#po_fullname").focus();
		return false;		
	}
	if (jQuery("#po_phone").val() == '') {
		alert('Bạn vui lòng nhập Số điện thoại di động !');
		jQuery("#po_phone").focus();
		return false;		
	} else if (!isTelNo(jQuery("#po_phone").val())) {
		alert('Số điện thoại di động không hợp lệ !');
		jQuery("#po_phone").focus();
		return false;		
	}
	
	if (jQuery("#po_email").val() != '' && !isEmailAddr(jQuery("#po_email").val())) {
		alert('Địa chỉ email không hợp lệ !');
		jQuery("#po_email").focus();
		return false;		
	}	
	
	jQuery.post(webroot + "pre_orders/add_preorder", 
		{ fullname: jQuery("#po_fullname").val(), phone: jQuery("#po_phone").val(), email: jQuery("#po_email").val(), mid: jQuery("#po_mid").val(), form_token: jQuery("#form_token").val() },
		function(data) {
			if (data == '1') {
				jQuery("#preorderpopup").hide();
				jQuery("#dimming").hide();
				jQuery("#po_fullname").val('');
				jQuery("#po_phone").val('');
				jQuery("#po_email").val('');
				alert('Đặt hàng thành công !');
				return true;
			} else {
				jQuery("#preorderpopup").hide();
				jQuery("#dimming").hide();
				alert('Có trục trặc trong quá trình đặt hàng, bạn vui lòng Refresh trang và thử lại !');
				return false;
			}
		}
	);
}
