
function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}


function getHashParams() {
    var hashParams = {};
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&;=]+)=?([^&;]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.hash.substring(1);

    while (e = r.exec(q))
       hashParams[d(e[1])] = d(e[2]);

    return hashParams;
}
var hash = getHashParams(document.location.hash);

$('select#change_cal').live('change', function() {
	$(".normalBooking").css("background-color","#FFFFDB");
	$(".customBooking").css("background-color","#eee");
	var period = $('#change_cal').val();
	var hid = $('#house_id').val();
	displayCalender(hid, period);
	
	var split = period.split('_');
		
	window.location.href = "#&month="+split[0]+"&year="+split[1];

});

function displayCalender(hid, period) {
    $.ajax({
        type: "GET",
        url: "/includes/ajax.calls.php",
        data: "cal=1&hid="+hid+"&period="+period,
        success: function(data){

  			$("#calendar").html(data);
        },
        failure: function(){
            alert('failed');
        }
    });	
	
}


$('.favo').click(function(){
    var house_id = $(this).attr('id');
    $.ajax({
        type: "POST",
        url: "/includes/ajax.calls.php",
        data: "favo=1&hid="+house_id,
        success: function(data){
         	if(data == "removed") {
         		$("#"+house_id).attr("src", "/images/favorite_add.png");
         		$("#fav_"+house_id).remove();
         		var rowCount = $("#favtable .cntFav").length;
         		if(rowCount == 0) $("#no_fav2").show();
         		if(rowCount == 0) $("#no_fav").show();
				$("#favCount").text(rowCount);
				$("#fav_text_"+house_id).text("Add to favourites");
         	}
         	if(data == "added") {
         		$("#no_fav").hide();
         		var housename = $("#name_"+house_id).html();
         		$("#"+house_id).attr("src", "/images/favorite_remove.png");
				$("#favtable tr:last").after('<tr class="cntFav" id="fav_'+house_id+'"><td width="40px"><img src="/images/favorite.png" /></td><td style="font-size:1em; font-weight: bold;">'+housename+'</td></tr>');
         		var rowCount = $("#favtable .cntFav").length;
         		$("#favCount").text(rowCount);
				$("#fav_text_"+house_id).text("Remove from favourites");
         	}
        },
        failure: function(){
            alert('failed');
        }
    });
});



$('#customBookingSubmit').click(function(){
	$(".customBooking").css("background-color","#FFFFDB");
	$(".normalBooking").css("background-color","#eee");
    var hid = $('#house_id').val();
    var from = $('#from').val();
    var to = $('#to').val();
    $.ajax({
        type: "GET",
        url: "/includes/ajax.calls.php",
        data: "customBooking=1&hid="+hid+"&from="+from+"&to="+to,
        success: function(data){
         	$("#calendar").html(data);
        },
        failure: function(){
            alert('failed');
        }
    });
});

