$(document).ready(function() {

	/* Facebook Like */
	$('#shareFB').html('<iframe src="http://www.facebook.com/plugins/like.php?href='+$(location).attr('href')+'&amp;send=false&amp;layout=button_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:21px;" allowTransparency="true"></iframe>');
    
    $('#shareTwitter').html('<a href="http://twitter.com/share" data-via="chesterhouseapt" class="twitter-share-button" data-count="none">Tweet</a>');
	
	/* Mailing List*/
	
	var defaultValue = 'Enter Your Email';
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
	$("#menu .mail-form .wpcf7-text").val(defaultValue);	
	
	$("#menu .mail-form .wpcf7-text").focus(function(){
		if(this.value==defaultValue)
		{
			this.value='';
		}
	});
		
	$("#menu .mail-form .wpcf7-text").blur(function(){
		if(this.value=='')
		{
			this.value=defaultValue;
		}
	});
		
	$('#menu .mail-form .wpcf7-submit').bind('click', function(){
		clearErrors();
		
		var hasError = false;
        var emailaddressVal = $("#menu .mail-form .wpcf7-text").val();
        
        if(emailaddressVal == '' || emailaddressVal == defaultValue) {
            $('#menu .mail-form .wpcf7-response-output').html('<span class="error">Please enter your email address!</span>');
            hasError = true;
        }
        else if(!emailReg.test(emailaddressVal)) {
             $('#menu .mail-form .wpcf7-response-output').html('<span class="error">Please enter a valid email address!</span>');
            hasError = true;
        }
 		
        if(hasError == true)
        	return false;
        else 
        	form.submit();
        
	});
	
	
	/* Book a Room form */
	
	var nameDefault = 'Name';
	var emailDefault = 'Email Address';
	var phoneDefault = 'Phone Number';
	
	$(".booking-container .your-name .wpcf7-text").focus(function(){
		if(this.value==nameDefault)	{this.value='';	}
	});
		
	$(".booking-container .your-name .wpcf7-text").blur(function(){
		if(this.value==''){this.value=nameDefault;}
	});
	
	$(".booking-container .your-email .wpcf7-text").focus(function(){
		if(this.value==emailDefault)	{this.value='';	}
	});
		
	$(".booking-container .your-email .wpcf7-text").blur(function(){
		if(this.value==''){this.value=emailDefault;}
	});
	
	$(".booking-container .your-phone .wpcf7-text").focus(function(){
		if(this.value==phoneDefault)	{this.value='';	}
	});
		
	$(".booking-container .your-phone .wpcf7-text").blur(function(){
		if(this.value==''){this.value=phoneDefault;}
	});
	
	
	$('.booking-container .wpcf7-submit').bind('click', function(){
			
		clearBookingErrors();
		
		var hasError = false;
        
        //Name validation
        if($(".booking-container .your-name .wpcf7-text").val() == '' || $(".booking-container .your-name .wpcf7-text").val() == nameDefault) {
            $('.booking-container span.your-name').after('<span class="error">Please enter your name!</span>');
            hasError = true;
        }
        
        //Email validation
        if($(".booking-container .your-email .wpcf7-text").val() == '' || $(".booking-container .your-email .wpcf7-text").val() == emailDefault) {
            $('.booking-container span.your-email').after('<span class="error">Please enter your email address!</span>');
            hasError = true;
        }
        else if(!emailReg.test($(".booking-container .your-email .wpcf7-text").val())) {
             $('.booking-container span.your-email').after('<span class="error">Please enter a valid email address!</span>');
            hasError = true;
        }
 		
 		 //Phone validation
        if($(".booking-container .your-phone .wpcf7-text").val() == '' || $(".booking-container .your-phone .wpcf7-text").val() == phoneDefault) {
            $('.booking-container span.your-phone').after('<span class="error">Please enter your phone number!</span>');
            hasError = true;
        }
 		
 		//Room Type validation	
 		if(!$("input[@name='your-room']:checked").val()){
 			$('.booking-container div.room-radio').after('<span class="error">Please select a room type!</span>');
            hasError = true;
 		}
 		
 		var now = new Date();
  	
  		$checkin = $('.your-checkin-date input').val();
  		$checkout = $('.your-checkout-date input').val();
    	
    	var divdate = new Date();
    	var divdate2 = new Date();
    	var dateparts = $checkin.split('/');
    	var dateparts2 = $checkout.split('/');
    
    	divdate.setDate(parseInt(dateparts[0]));
    	divdate.setMonth((parseInt(dateparts[1])-1));
    	divdate.setYear(parseInt(dateparts[2]));
    	
    	divdate2.setDate(parseInt(dateparts2[0]));
    	divdate2.setMonth((parseInt(dateparts2[1])-1));
    	divdate2.setYear(parseInt(dateparts2[2]));
    	
    	//Checkin/Checkout dates
 		if($(".booking-container .your-checkin-date input").val() == '' || $(".booking-container .your-checkout-date input").val() == '') {
            $('.booking-container div.dates-div').after('<span class="error">Please enter your check in and check out dates</span>');
            hasError = true;
        } else if (divdate.getTime() <= now.getTime() || divdate2.getTime() <= now.getTime()) {
      		 $('.booking-container div.dates-div').after('<span class="error">Check in and check out dates cannot be before or the same as today!</span>');
    	} else if (divdate.getTime() == divdate2.getTime()) {
    		$('.booking-container div.dates-div').after('<span class="error">Check in and check out dates cannot be the same!</span>');
    	} else if (divdate.getTime() > divdate2.getTime()) {
    		$('.booking-container div.dates-div').after('<span class="error">Check out dates cannot be earlier than check in dates!</span>');
    	}
 		
 		
        if(hasError == true)
        	return false;
        else 
        	form.submit();
        
	});
	
	$( ".your-checkin-date input" ).datepicker({dateFormat: 'dd/mm/yy'});

	$( ".your-checkout-date input" ).datepicker({dateFormat: 'dd/mm/yy'});

	//Span labels are identical to the values declared on the contact form page
	jQuery.each($('.wpcf7-radio .wpcf7-list-item-label'), function(){
		if ($(this).text() == "Studio")
		    $(this).append(' - ' + $('#hid-price-1').val() + ' a night' ).after('<a href="http://www.chesterhouse.co.ke/our-rooms/studio/" title="" class="info-more">Read more</a>');
		if ($(this).text() == "1 Bedroom")
		    $(this).append(" - " + $('#hid-price-2').val() + " a night").after('<a href="http://www.chesterhouse.co.ke/our-rooms/1-bedroom/" title="" class="info-more">Read more</a>');
		if ($(this).text() == "2 Bedroom")
		    $(this).append(" - " + $('#hid-price-3').val() + " a night").after('<a href="http://www.chesterhouse.co.ke/our-rooms/2-bedrooms/" title="" class="info-more">Read more</a>');
		if ($(this).text() == "Penthouse")
		    $(this).append(" - " + $('#hid-price-4').val() + " a night").after('<a href="http://www.chesterhouse.co.ke/our-rooms/penthouse/" title="" class="info-more">Read more</a>');
	});
	
	
});

/* Helper functions */

function clearErrors() {
	$('#menu .mail-form .wpcf7-response-output').html('');
}

function clearBookingErrors() {
	$('.booking-container span.error').remove();
}

