// AJAX INIT
function $$$(id) {
	return document.getElementById(id);
}
function khoitao_ajax()
{
	var x;
	try 
	{
		x	=	new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
    	try 
		{
			x	=	new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(f) { x	=	null; }
  	}
	if	((!x)&&(typeof XMLHttpRequest!="undefined"))
	{
		x=new XMLHttpRequest();
  	}
	return  x;
}
$.extend({
  ajaxIt : function(query, lnk, callbackFnk){    
	var http 	=	khoitao_ajax();
	try
	{
	  http.open("POST", lnk);
	  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  http.setRequestHeader("Cache-control", "no-cache");		
	  http.onreadystatechange = function()
	  {
		  if (http.readyState == 4)
		  {
			  if (http.status == 200)
			  {
				  $(".jGrowl-notification .jGrowl-message").each(function(i,e){
						if ($(this).text() == "Processing...")
						{
							$(this).parent().hide();
						}
					});
				  // Callback
				  if(typeof callbackFnk == 'function'){
					  x = http.responseText;
					  callbackFnk.call(this, x);
				  }
				  //---------
			   }
				else
				{
					alert('Có lỗi hệ thống. Xin vui lòng thử lại sau');
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
  }
});
function	Forward(url)
{
	window.location.href = url;
}
function	_postback()
{
	return void(1);
}
/*So sanh height cua 2 the div tgp_body_content & tgp_right*/
function EqualHeight(elements) { 
    //Xác định chiều cao của cột cao nhất 
    tallest = 0; 
    elements.each(function() { 
        elementHeight = jQuery(this).height(); 
        if(elementHeight > tallest) { 
            tallest = elementHeight; 
        } 
    }); 
    //Chỉnh chiều cao cho tất cả các cột 
    //theo chiều cao của cột cao nhất 
    elements.height(tallest); 
}
// Kiểm tra IE
	function isIE()
	{
	  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
	} 

jQuery(window).load(function(){ 
	Cufon.replace('.cufon', {fontFamily:'utm',hover:true});
});

function check_form_book(personalfrm)
{
	var ten = personalfrm.txt_ten.value;
	
	var email = personalfrm.txt_email.value;
	
	var tel = personalfrm.txtTel.value;
	
	var address = personalfrm.txtAddress.value;
	
	if (!ten || ten.length < 8)
	{
		document.personalfrm.txt_ten.focus();
		show_notify('<span style="color:#C00">Fullname must be 8 characters or more</span> !');
		return false;
	}
	
	else if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/))
	{
		document.personalfrm.txt_email.focus();
		show_notify('<span style="color:#C00">Email is invalid</span> !');
		return false;
	}
}

function send_contact(contactfrm)
{
	
	$$$("notify").innerHTML = '<img src="/images/ajax-loader.gif" align="absmiddle" border="0" /> Your email is sending.';
	
	var ten = contactfrm.txt_ten.value;
	
	var email = contactfrm.txt_email.value;
	
	var tel = contactfrm.txtTel.value;
	
	var address = contactfrm.txtAddress.value;
	
	var tieu_de = contactfrm.txt_tieu_de.value;
	
	var noi_dung = contactfrm.txt_noi_dung.value;
	
	if (!ten || ten.length < 8)
	{
		document.contactfrm.txt_ten.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Fullname must be 8 characters or more</span> !';
		return false;
	}
	
	else if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/))
	{
		document.contactfrm.txt_email.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Email is invalid</span> !';
		return false;
	}	
	else if (!tieu_de || tieu_de.length < 8)
	{
		document.contactfrm.txt_tieu_de.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Title is so short</span> !';
		return false;
	}	
	else if (!noi_dung || noi_dung.length < 16)
	{
		document.contactfrm.txt_noi_dung.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Content is so short</span> !';
		return false;
	}		
	else
	{
	}

	var	query	=	"act=contact&txt_ten="+ten+"&txt_tieu_de="+tieu_de+"&txt_noi_dung="+noi_dung+"&txt_email="+email+"&txtTel="+tel+"&txtAddress="+address;
	var http 	=	khoitao_ajax();
	try
	{
		http.open("POST", "/action.php");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					x = http.responseText;
					if (x != "OK")
					{
						$$$("notify").innerHTML = '<span style="color:#C00">'+x+'</span> !';
					}
					else
					{
						$$$("notify").innerHTML = '<span style="color:#C00">Your email have send success</span> !';
					}
				}
				else
				{
					$$$("notify").innerHTML = '<span style="color:#C00">Error ! Please try again later</span> !';
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
		$$$("notify").innerHTML = '<span style="color:#C00">Error !</span><br>Please try again later !';
	}
	return false;
	
}

$(document).ready(function()
{
	$("#txt_date_in").datepicker({changeMonth:true, changeYear:true, dateFormat: 'dd-mm-yy', minDate: +0, numberOfMonths: 2 });
	$("#txt_date_details").datepicker({changeMonth:true, changeYear:true, dateFormat: 'dd-mm-yy', minDate: +0, numberOfMonths: 2 });
	
	$("a[rel=example_group]").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});
});

var $container;
		
$(document).ready(	
	function() {
		$container = $("#container");
		$container.wtRotator({
			width:694,
			height:296,
			border:"none",
			button_width:73,
			button_height:68,
			button_margin:0,
			auto_start:true,
			delay:5000,
			transition:"random",
			transition_speed:800,
			block_size:100,
			vert_size:50,
			horz_size:50,
			display_thumbs:false,
			display_dbuttons:true,
			display_playbutton:false,
			display_tooltip:false,
			display_numbers:false,
			cpanel_mouseover:false,
			text_mouseover:false
		});
	}
);

function show_notify(msg)
{
	$.ajaxIt('act=show_thong_bao&msg='+encodeURIComponent(msg),'/action.php',function(x)
	{
		$("#luu_result").html(x);
	});
}

function newsletter()
{
	var txt_newsletter = $.trim($("#txt_newsletter").val());
	
	if (txt_newsletter == '' || txt_newsletter == 'Please enter your email...')
	{
		$("#txt_newsletter").focus();
		show_notify("Please enter your email");
		return false;
	}
	else if (!txt_newsletter.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/))
	{
		$("#txt_newsletter").focus();
		show_notify("Email invalid. Please enter again.");
		return false;
	}
	$.ajaxIt('act=newsletters&txt_newsletter='+txt_newsletter,'/action.php',function(x)
	{
		x = $.trim(x);
		if (x.substring(0,2) == 'OK')
		{
			show_notify(x.substring(3));
			$("#txt_newsletter").val('');
		}
		else
			show_notify(x);
	});
	return false;
}

function load_tour_content(id,view)
{
	var query = 'act=load_tour_content&id='+id+'&view='+view
	$.ajaxIt(query,'/action.php',function(x)
	{
		x = $.trim(x);
		$("#load_tour_content").html(x);
	});
}

$(document).ready(function()
{
	$(".tour_menu_item").live('click',function()
	{
		$(".tour_menu_item").removeClass('active');
		$(this).addClass('active');
	});
});

function book_room(id)
{
	var query = 'act=book_room&id='+id;
	$.ajaxIt(query,'/action.php',function(x)
	{
		x = $.trim(x);
		if (x.substring(0,2) == 'OK')
		{
			var query1 = 'act=update_cart';
			$.ajaxIt(query1,'/action.php',function(y)
			{
				$("#number_cart").html($.trim(y));
				window.location = '/reservation/vinh-hung-3-hotel.html';
			});
		}
		else
		{
			show_notify(x);
		}
	});
}

function $query(obj)
{
	var query = "";
 	$(obj).find("input").each(function(i)
	{
  		if (($(obj).find("input").eq(i).attr("type") != "checkbox") && ($(obj).find("input").eq(i).attr("type") != "button") && ($(obj).find("input").eq(i).attr("type") != "submit") && ($(obj).find("input").eq(i).attr("type") != "radio") )
  		{
		   var t = $(obj).find("input").eq(i);
		   query += "&"+t.attr("name")+"="+encodeURIComponent(t.attr("value"));
  		}
  		else
  		{
   			if ($(obj).find("input").eq(i).attr("type") == "checkbox")
	  		{
				var t = $(obj).find("input").eq(i);
				query += "&"+t.attr("name")+"="+t.attr("checked");
   			}
   			else if ($(obj).find("input").eq(i).attr("type") == "radio")
   			{
				var t = $(obj).find("input").eq(i);
				if (t.is(":checked"))
					query += "&"+t.attr("name")+"="+t.attr("value");
   			}
  		}
 	})
	
 	$(obj).find("textarea").each(function(i)
	{
  		var t = $(obj).find("textarea").eq(i);
  		query += "&"+t.attr("name")+"="+encodeURIComponent(t.attr("value"));
 	});
	
 	$(obj).find("select").each(function(i)
	{
  		var t = $(obj).find("select").eq(i);
  		query += "&"+t.attr("name")+"="+encodeURIComponent(t.attr("value"));
 	});
 	return query.substring(1);
}

function send_booking()
{
	
	var query = "";
	
	query = $query("#frmBookingDetails");
	if (query == "")
		query = $query("#frmBooking");
	
	query = query + '&act=send_booking';
	
	$.ajaxIt(query,'/action.php',function(x)
	{
		x = $.trim(x);
		if (x.substring(0,2) == "OK") {
			var query1 = 'act=update_cart';
			$.ajaxIt(query1,'/action.php',function(y)
			{
				$("#number_cart").html($.trim(y));
				window.location = '/reservation/vinh-hung-3-hotel.html';
			});
		}
		else {
			show_notify(x);
		}
	});
}

function change_service(cbo)
{
	var room = parseInt($("#book_room_total").html());
	var addition = parseInt($("#book_room_addition_service").html());
	var total = parseInt($("#total_cost").html());
	var value = parseInt(cbo.value);
	if (cbo.checked)
	{
		addition = addition + value;
		total = room + addition;
	}
	else
	{
		addition = addition - value;
		total = room + addition;
	}
	$("#book_room_addition_service").html(""+addition+"");
	$("#total_cost").html(""+total+"");
}

function send_booking_room()
{
	if ($("#cbo_policy").is(':checked'))
	{
		var query = "";
		query += $query("#frmRoomDetails");
		query += "&act=send_booking_room";
		
		$.ajaxIt(query,'/action.php',function(x)
		{
			x = $.trim(x);
			
			if (x.substring(0,2) == 'OK')
			{
				var query1 = 'act=update_cart';
				$.ajaxIt(query1,'/action.php',function(y)
				{
					$("#number_cart").html($.trim(y));
					window.location = '/cart/vinh-hung-3-hotel.html';
				});
			}
			else
				show_notify(x);
		});
	}
	else
	{
		show_notify("Please read and accept the terms conditions to continue !");
	}
}

function book_tour(id)
{
	var query = 'act=book_tour&id='+id;
	$.ajaxIt(query,'/action.php',function(x)
	{
		x = $.trim(x);
		
		if (x.substring(0,2) == 'OK')
		{
			var query1 = 'act=update_cart';
			$.ajaxIt(query1,'/action.php',function(y)
			{
				$("#number_cart").html($.trim(y));
				window.location = '/reservation-tour/vinh-hung-3-hotel.html';
			});
		}
		else
			show_notify(x);
	});
}

function calcul_price_tour()
{
	var tour_price = 0;
	tour_price = $(".rad_tour_price:checked").attr('gia');
	var tour_person = $("#txt_number_person").val();
	if (isNaN(tour_person)) tour_person = 1;
	else tour_person = parseInt(tour_person);
	
	var result = parseInt(tour_price) * tour_person;
	$("#total_price_tour").html(''+result+'');
	$("#txt_total_price_tour").val(''+result+'');
}

function add_tour_to_cart()
{
	if ($("#cbo_policy").is(':checked'))
	{
		var query = $query(".book_left");
		query += '&act=add_tour_to_cart';
		$.ajaxIt(query,'/action.php',function(x)
		{
			x = $.trim(x);
			if (x.substring(0,2) == 'OK')
			{
				var query1 = 'act=update_cart';
				$.ajaxIt(query1,'/action.php',function(y)
				{
					$("#number_cart").html($.trim(y));
					window.location = '/cart/vinh-hung-3-hotels.html';
				});
			}
			else
				show_notify(x);
		});
	}	
	else
	{
		show_notify("Please read and accept the terms conditions to continue !");
	}
}

function delete_gio_hang(id)
{
	var query = 'act=delete_gio_hang&id='+id;
	$.ajaxIt(query,'/action.php',function(x)
	{
		x = $.trim(x);
		if (x.substring(0,2) == 'OK')
		{
			update_cart();
			var total = parseInt($("#txt_total_price_book").val());
			var gia = parseInt($("#gio_hang_"+id).attr('gia'));
			var result = (total-gia);
			$("#txt_total_price_book").val(result);
			$("#total_price_book").html(result);
			$("#gio_hang_"+id).remove();
			show_notify(x.substring(3));
		}
		else
			show_notify(x);
	});
}

function book_transfer(id)
{
	var query = 'act=book_transfer&id='+id;
	$.ajaxIt(query,'/action.php',function(x)
	{
		x = $.trim(x);
		if (x.substring(0,2) == 'OK')
		{
			var query1 = 'act=update_cart';
			$.ajaxIt(query1,'/action.php',function(y)
			{
				$("#number_cart").html($.trim(y));
				window.location = '/reservation-transfer/vinh-hung-3-hotel.html';
			});
		}
		else
			show_notify(x);
	});
}

function calcul_transfer()
{
	var value = $(".car_type:checked").attr('gia');
	$("#total_price_transfer").html(value);
	$("#txt_total_price_transfer").val(value);
}

function add_transfer_to_cart()
{
	if ($("#cbo_policy").is(':checked'))
	{
		var query = $query(".frmBookTransfer");
		query += "&act=add_transfer_to_cart";
		$.ajaxIt(query,'/action.php',function(x)
		{
			x = $.trim(x);
			if (x.substring(0,2) == 'OK')
			{
				var query1 = 'act=update_cart';
				$.ajaxIt(query1,'/action.php',function(y)
				{
					$("#number_cart").html($.trim(y));
					window.location = '/cart/vinh-hung-3-hotel.html';
				});
			}
			else
				show_notify(x);
		});
	}
	else
	{
		show_notify("Please read and accept the terms conditions to continue !");
	}
}

function remove_cart()
{
	var query = 'act=remove_cart';
	$.ajaxIt(query,'/action.php',function(){});
	update_cart();
}

function update_cart()
{
	var query = 'act=update_cart';
	$.ajaxIt(query,'/action.php',function(x)
	{
		$("#number_cart").html($.trim(x));
	});
}

function gui_binh_luan()
{
	var query = $query("#binh_luan_tin") + "&act=gui_binh_luan";
	$.ajaxIt(query,'/action.php',function(x)
	{
		if (x.substring(0,2) == 'OK')
		{
			$("#hoten").val('');
			$("#email").val('');
			$("#noidung").val('');
			show_notify(x.substring(3));
		}
		else
			show_notify(x);
	});
	return false;
}
