// JavaScript Document
$(document).ready(function () {
	
	var base_url 	= '{$base_url}';
	var app_url 	= '{$app_url}';
	var app_url_default 	= '{$app_url_default}';
	
	//$(document).snowfall({flakeCount: 1000}); // snow
	//$("#footer-tao").constantfooter({  
//		"opacity": "1"
//		//"closebutton": "[x]", 
//		//"showclose": "true" 
//	});
	 
	$(".menu a > div").hover(function(){
      $(this).css({backgroundPosition: '0px 92px'});
    },function(){
      $(this).css({backgroundPosition: '0px 0px'});
    });
	 
	$(".scroll").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();
		
		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;
		
		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];
		
		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;
		
		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});
	
	  /*config*/
	  $('#step2').hide();  
	  $('#step3').hide(); 
	  $('#step_booking_3').hide();
	  $('#step_booking_4').hide();
	  $('#complate').hide();
	  
	  /*check hide*/
	  $('#arrivalDate').click(function(){
		   $('#step2').hide();
		   $('#step3').hide();
		   $('#hotel_room_rate').hide();
		   $('#step_booking_3').hide();
		   $('#step_booking_4').hide();
	  });
	  
	  $('#tao_class').click(function(){
		  
		   $('#step3').hide(); 
		   $('#step_booking_4').hide();
	  });
	  
	  
	  /*แสดง step 2 เมื่อ คลิก step อักแรกสุด*/
	  $('#l_step2').click(function() { 
		if($('#arrivalDate').val()=='00-00-0000') {
				  alert($('#arrivalDate').attr('title')); 
				  return false ;
		}
		 if($('#departureDate').val()=='00-00-0000') {
				  alert($('#departureDate').attr('title')); 
				  return false ;
		}
		$('#step2').show();  
		
	  });
	  
	  $('.room').live('click', function() {
				
		  $('#step3').hide(); // form input data user
		  $('#step4').hide(); // product hide
		  $('#step_booking_4').hide(); // line next
		  $('#step_booking_5').hide(); // submit hodi
		  $('#step_booking_3').hide();
		  
		  $('#hotel_room_rate').show();
		  //$('#step_booking_3').show();
		   
		  var thelink = $(this); 
		  
		  var relid = this.id; 
		 
		  $.post('booking/hotelroom', { day1: $('#arrivalDate').val(),day2: $('#departureDate').val(),hotel_id:thelink.attr('rel')}, 
			  function(data){ 
				  
				   $(".divGetData").html(data); 
				   
				    $("#foottip a").tooltip({
				 
							bodyHandler: function() {
								return $($(this).attr("href")).html();
							}, 
							showURL: false, 
							fade: 250 
							
					}); 
				  
			  }  
		  );			 
		
		 return false;
		 
	  });
	  
	  $('#se_room').live('click', function() { 
			$('#step_booking_3').show();
			  
	  });
	  
	  $('#form_step').click(function() {
		   
		   $('#step3').show(); 
		   $('#step_booking_4').show(); 
		      
		   var room0 = $('.room_0').val();
		   var room1 = $('.room_1').val();
		   var room2 = $('.room_2').val();
		   var room3 = $('.room_3').val();
		   var dataroom = Array();
		   for(i=0;i<=3;i++){
			   //$('#checkBox').attr('checked'); 
				if($('.room_'+i).attr('checked')){
					dataroom[i] = $('.room_'+i).val();
				}
					   
		   }
		    
		   $.post("booking/RoomUserDetail", { data:dataroom}, 
			
				  function(data){
					
					  $(".room_user_detail").html(data); 
				  }  
			);   
	  });
	  
	 
	    
	   $('#button_check').live('click', function() { 
		  
		 
		  var Email = $('#f_email').val();
		  var Phone = $('#f_phone').val();
		  
		  $.post("booking/checkUser", { email:Email,phone:Phone},  
			  function(data){  
				  $(".form").html(data); 
		  });
		});	
		
		$("div.o_class").click(function(){
			$(".o_class").removeClass("n_class");
			$(this).toggleClass("n_class");
		});
		 
			
		$('.button_save').click(function() { 
		  
		  if($('#f_name').val()=='') {
				  alert($('#f_name').attr('title')); 
				  return false ;
		  }
		  if($('#f_address').val()=='') {
				  alert($('#f_address').attr('title'));
				   
				  return false ;
		  }
		  if($('#f_pro').val()=='') {
				  alert($('#f_pro').attr('title'));
				   
				  return false ;
		  }
		  
		  if($('#f_code').val()=='') {
				  alert($('#f_code').attr('title'));
				   
				  return false ;
		  }
		  if($('#f_coun').val()=='') {
				  alert($('#f_coun').attr('title'));
			   
				  return false ;
		  }
		  if($('#f_phone').val()=='') {
				  alert($('#f_phone').attr('title'));
				   
				  return false ;
		  }
		  
	   
		  if($('#f_email').val()=='') {
				  alert($('#f_email').attr('title'));
				   
				  return false ;
		  }
		  
		  if($('#f_email').val()!='') {
				
			  // value is numeric
			  str = $('#f_email').val();  
					
			  var err=false; 
				
			  var filter=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i; 
				
			  if ( filter.test(str) ) {
				err=false; 
			  }else {
				err=true; 
			  }
			   
			   
			  if(err){
				alert(str + ' is not a valid email address.');
				$('#f_email').val('');
				return false ;
			  } 
		   }  
		   
		  var clearMePrevious = ''; 
			
		  
		  var dataString = $('#bookingForm').serializeArray(); 
		  var froomtype = Array();
		  var froom = Array();
		  var fadults = Array();
		  var fchildren = Array();
		  var ftime = Array();
		  
		  for(i=0;i<=3;i++){
		  	if($('.room_type_'+i).val()){
				
				froomtype[i] 	= 	$('.room_type_'+i).val();
				froom[i] 		= 	$('#f_room_'+i).val();
				fadults[i] 		= 	$('#f_adults_'+i).val();
				fchildren[i] 	= 	$('#f_children_'+i).val();
				ftime[i] 		= 	$('#f_times_'+i).val();
				//alert(froomtype[i]);
			}
		  } 
		  
		  var hotelid  = $('.hotel_id').val();
		  var fdatestart = $('#arrivalDate').val();
		  var fdateend = $('#departureDate').val();
		  var fnight = $('#night').val();
		  var femail =  $('#f_email').val();
		  var fphone =  $('#f_phone').val();
		  var fname =  $('#f_name').val();
		  var faddress =  $('#f_address').val();
		  var famp =  $('#f_amp').val();
		  var fpro =  $('#f_pro').val(); 
		  var fcode =  $('#f_code').val(); 
		  var fcoun =  $('#f_coun').val();
		  var fphone2 =  $('#f_phone2').val();
		  
		  $.post("booking/save", { 
		  	  hotel_id:hotelid, 
			  roomtype:froomtype,
			  room:froom,
			  adults:fadults,
			  children:fchildren,
			  btimes:ftime,
			  night:fnight,
			  datestart:fdatestart,
			  dateend:fdateend,
			  email:femail,
			  phone:fphone,
			  name:fname,
			  address:faddress,
			  amp:famp,
			  pro:fpro,
			  code:fcode,
			  coun:fcoun,
			  phone2:fphone2
		  },
				function(data){  
					  //alert(data);
					  $(".form").html(data); 
				}
		  );
		  
		  $('#detail').hide();
		  $('#line1').hide();
		  $('.Choose-to-stay').hide();
		  $('#complate').show();
		  
		});
		
		//gift vu=oucher
	$(".voucher").keypress(function (e)
	{ 
	  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
	  {
		alert('Input Digits Only'); 
		return false;
	  }
	});
	
	$('#comfr').hide();
	
	$('#voucher1').attr('disabled','disabled');
	$('#voucher2').attr('disabled','disabled');
	$('#voucher3').attr('disabled','disabled');
	 
	$('#cvoucher1').click(function(){$('#voucher1').removeAttr('disabled'); });
	$('#cvoucher2').click(function(){$('#voucher2').removeAttr('disabled'); });
	$('#cvoucher3').click(function(){$('#voucher3').removeAttr('disabled'); });
	
	$("#voucher1").keyup(function () {
      var value = $(this).val();
	  if(value){
      	$("#sum1").text('Price '+(addCommas(value*400))+'.00 Bath.');
		$('#sumvoucher1').val(value*400); 
		  
	  }
    }).keyup();
	
	$("#voucher2").keyup(function () {
      var value = $(this).val();
	  if(value){
      	$("#sum2").text('Price '+(addCommas(value*775))+'.00 Bath.'); 
		$('#sumvoucher2').val(value*775); 
		 
	  }
    }).keyup();
	
	$("#voucher3").keyup(function () {
      var value = $(this).val();
	  if(value){
      	$("#sum3").text('Price '+(addCommas(value*1150))+'.00 Bath.'); 
		$('#sumvoucher3').val(value*1150); 
	  }
    }).keyup();
	 
	$('#comf').click(function(){
		if($('#f_name').val()=='') {
			alert($('input[name$="f_name"]').attr('title')); 
			$('#f_name').focus();
    		return false ;
		}
		if($('#f_address').val()=='') {
			alert($('#f_address').attr('title')); 
			$('#f_address').focus();
    		return false ;
		}
	 
		if($('#f_amp').val()=='') {
			alert($('#f_amp').attr('title')); 
			$('#f_amp').focus();
    		return false ;
		}
		if($('#f_coun').val()=='') {
			alert($('#f_coun').attr('title')); 
			$('#f_coun').focus();
    		return false ;
		}
		if($('#f_code').val()=='') {
			alert($('#f_code').attr('title')); 
			$('#f_code').focus();
    		return false ;
		}
		if($('#f_email').val()=='') {
			alert($('#f_email').attr('title')); 
			$('#f_email').focus();
    		return false ;
		}else{
			var emailFilter=/^.+@.+\..{2,3}$/;
			if (!(emailFilter.test($('#f_email').val()))) {
				alert ("Please enter your Email Platform.");
				$('#f_email').focus();
				return false;
			} 
		}	 
		if($('#f_phone').val()=='') {
			alert($('#f_phone').attr('title')); 
			$('#f_phone').focus();
    		return false ;
		}  
		
		var c = 15; 
		
		var sumnumber = sum($('#voucher1').val(),$('#voucher2').val(),$('#voucher3').val());
		var sumprice = sum($('#sumvoucher1').val(),$('#sumvoucher2').val(),$('#sumvoucher3').val());
		var sumprices = sum(sumprice,c,0);
		$('#comf').hide();
		$('#comfr').show();
		$('.gifyvoucher-il').show();
		
		$('#all').text(addCommas(sumnumber))
		$('#priceall').text(addCommas(sumprice)+".00");
		$('#pricealls').text(addCommas(sumprices)+".00");
		
		$('#sumall').val(addCommas(sumnumber));
		$('#sumprice').val(addCommas(sumprices));
	});
	 
	function sum($data1,$data2,$data3){
		var $d1 = Number($data1);
		var $d2 = Number($data2);
		var $d3 = Number($data3);
		asum = $d1+$d2+$d3;
		return asum;
	}
	
	function addCommas(nStr)
		{
			nStr += '';
			x = nStr.split('.');
			x1 = x[0];
			x2 = x.length > 1 ? '.' + x[1] : '';
			var rgx = /(\d+)(\d{3})/;
			while (rgx.test(x1)) {
				x1 = x1.replace(rgx, '$1' + ',' + '$2');
			}
			return x1 + x2;
		}
	
	 $('#all').html($('#sumvoucher3').val());
	 
	 $('ul#third a').imgPreview({
		containerID: 'imgPreviewWithStyles',
		imgCSS: {
			// Limit preview size:
			height: 180
		},
		 // When container is shown:
		onShow: function(link){
			$('<span>' + $(link).text() + '</span>').appendTo(this);
		},
		// When container hides: 
		onHide: function(link){
			$('span', this).remove();
		}

		 
	});
 
	
});

	// preload cal images
		var dateFormatString = "YYYY-MM-DD";
        moLeftArrow = new Image();
        moLeftArrow.src = "http://www.b2hotel.com/application/default/images/si_btn_arrow_left_cal.gif";
        moRightArrow = new Image();
        moRightArrow.src = "http://www.b2hotel.com/application/default/images/si_btn_arrow.gif";
        moCloseX = new Image();
        moCloseX.src = "http://www.b2hotel.com/application/default/images/4p_btn_closeX.gif";

        // method called from the calendar continue buton.  validate the form and submit
        function fnSetInfoToFields_booking(pCIDate, pCODate, pFromContinue){
            document.bookingForm.arrivalDate.value     = getFormatedDate("en", pCIDate);
            document.bookingForm.departureDate.value   = getFormatedDate("en", pCODate);
			for (i = 1;pCIDate < pCODate;i++)
				{
					pCIDate.setDate(pCIDate.getDate() + 1);
				}
			document.bookingForm.night.value   = i-1;
        }

        function fnGetDate1_booking() {
            return setDateFromString("en", document.bookingForm.arrivalDate.value);
        }

        function fnGetDate2_booking() {
            return setDateFromString("en", document.bookingForm.departureDate.value);
        }
        // The location is where to display the calendar based on the icon being pressed
        // Possible values are "topLeft" "topRight" "bottomleft" "bottomRight" -or- "x,y" where  y is the top and x is the left of where the calendar should be placed
        var calLocation_booking = "645,235";
        // added as a work around for the js being on the search jsps rather than in the tiles.  to ensure this isn't called before the js loads, do it onload of the body....
        function addCalendar(){
            BookingCalendarObj = new multiDisplayCalendar("bookingForm", fnGetDate1_booking, fnGetDate2_booking, fnSetInfoToFields_booking);
        }
        addLoadEvent(addCalendar);
 
		function addLoadEvent(func) {
		
			var oldonload = window.onload;
			if (typeof window.onload != 'function') {
				window.onload = func;
			} else {
				window.onload = function() {
					oldonload();
					func();
				}
			}
		}


