$(document).ready(function(){

//this is to change the value to the correct currency
if ($('#finishedcost').length){
    $('#finishedcost').html(fn_makecurrency($('#finishedcost').html()));
}
//this is to change the value to the correct currency
if($('#roomtotal').length){
    
    $('#roomtotal').html(fn_makecurrency($('#roomtotal').html()));
}
//this is to change the value to the correct currencys
if($('#grandtotal').length){
    $('#grandtotal').html(fn_makecurrency($('#grandtotal').html()));
}
function gettotal(index){
    grandtotal = 0;
    numday = $("#numdays").html();
    $('#hotelselect'+index+' select').each(function (indexs){
        var getroomtype = this.name.substring(4);
        grandtotal = grandtotal+Number($('#roomprice'+getroomtype).val()*$(this).val())*numday;
    })
    
    $('#grandtotal'+index).html(fn_makecurrency(grandtotal));
}

$('.hotelrooms').each(function (index){
    //This returns the total value of the order;
    gettotal(index);
    $('#hotelselect'+index+' select').change(function (){
          gettotal(index);
    })
    // this checks to make sure that they have selected at least one room
    $('#hotelselect'+index).submit(function (){
       var grandtotals = 0;
         $('#hotelselect'+index+' select').each(function (){
            info = $(this).val()
            //alert (info);
            grandtotals = grandtotals+Number($(this).val());
        })
        //alert(grandtotals);
        if (grandtotals == 0){
            return false;
        }
    })

})

var doOnce = 0;
$('.extras select').each(function(index){
    //alert("in"+index);
    $('#extraselect'+index).val(0);
    //make the parking days 0 when the page is refreshed
    if (this.name == "extra4"){
        $('#extraselect'+index).val("0 days");
    }
    //make number of cars 0 when the page is refreshed
    if (this.name == "extra5"){
        $('#extraselect'+index).val("0 cars");
    }
    $('#extraselect'+index).change(function(){
        
        if ((this.name == "extra4") || (this.name == "extra5")){
            var singleextra = fn_parking($('.extraclass4').val(), $('.extraclass5').val());
            if (($('.extraclass4').val().substring(0, $('.extraclass4').val().indexOf(" ")) != 0) && ($('.extraclass5').val().substring(0, $('.extraclass5').val().indexOf(" ")) != 0) && (singleextra == "0")){
                $('#parkingtotal').remove();
                $('#extratotal0').html("Free");
            } else {
                $('#extratotal0').html(fn_makecurrency(singleextra));
                $('#parkingtotal').remove();
                $('<input type="hidden" name="parkingtotal" value="'+singleextra+'" id="parkingtotal">').insertAfter('.extras');
            }
        } else {
            var extrascost = fn_makenumber($('#extracost'+index).html());
            var addextra = $('#extraselect'+index).val();
            var singleextra = (addextra*extrascost);
            if ((extrascost ==  0) && (addextra != 0)){
                $('#extratotal'+index).html("Free");
            } else {
                $('#extratotal'+index).html(fn_makecurrency(singleextra));
            }
        }
        extrastotals();

    })
    
})
//this is to check that they have selected both number of days to park and the number of cars
$('#extraform').submit(function(){
    var days = $('.extraclass4').val().substring(0, $('.extraclass4').val().indexOf(" "))
    var cars = $('.extraclass5').val().substring(0, $('.extraclass5').val().indexOf(" "))
    if (((days == 0) && (cars != 0))){
        //alert("message");
        alert("Please specify the number of days holiday parking your require. If you don't require any holiday parking set both fields to 0.");
        return false;
    }
    if ((days != 0) && (cars == 0)){
        alert("Please specify the number of cars you require parking for. If you don't require any holiday parking set both fields to 0.");
        return false;
    }
})
function fn_parking(daysparking, numcars){
    var parkdays = Number(daysparking.substring(0, daysparking.indexOf(" ")));
    var day08 = $('#parking1').html();
    var day815 = $('#parking2').html();
    var day15 = $('#parking3').html();
    var cars = Number(numcars.substring(0, numcars.indexOf(" ")));
    var parkingtotal = 0;
    parkingtotal = ((parkdays > 8 ? 8 : parkdays)*day08)*cars;
    if (parkdays > 8){
        parkingtotal = parkingtotal+((((parkdays-8) > 7 ? 7 : (parkdays-8))*day815)*cars);
    }
    if (parkdays > 15){
        parkingtotal = parkingtotal+(((parkdays-15)*day15)*cars);    
    }
    return(parkingtotal);
}
function extrastotals(){
    var extragrandtotal = 0;
    var formdata = "";
    $('.extras select').each(function(index){
        var addextra = $('#extraselect'+index).val();
        var singletotal = $('#extratotal'+index).html();
        if (singletotal == "Free"){
            singletotal = 0;
        }
        extragrandtotal = (fn_makenumber(extragrandtotal) + fn_makenumber(singletotal));
        //alert(addextra.substring(0, addextra.indexOf(" ")));
        //
        addextra = (addextra.indexOf(" ") == -1 ? addextra : addextra.substring(0, addextra.indexOf(" ")));
        //alert(addextra);
        if ((addextra != 0)){
            var addforminfo = this.name.substring(5);
            formdata = formdata+','+ addforminfo;
        }
    })
    $('#extrasarray').remove();
    $('#extrasvalue').remove();
    $('<input type="hidden" name="extrasarray" value="'+formdata.substring(1)+'" id="extrasarray">').insertAfter('.extras');
    $('<input type="hidden" name="extratotal" value="'+extragrandtotal+'" id="extrasvalue">').insertAfter('.extras');
    //alert(extragrandtotal);
    $('#extratotal').html(fn_makecurrency(extragrandtotal));
    var roomtotal = $('#roomtotal').html();
    var extraroomtotal = (fn_makenumber(roomtotal)+fn_makenumber(extragrandtotal));
    $('#grandtotal').html(fn_makecurrency(extraroomtotal));
    //alert(fn_makecurrency("3"));
}
function fn_makenumber(anumber){
    var changenumber = anumber.toString();
    var ispound =  changenumber.indexOf("£");
    if (ispound == -1){
        return(Number(changenumber));
    } else {
        return(Number(changenumber.substring(1)));
    }
}
function fn_makecurrency(anumber){
   // alert("curr");
    var changenumber = anumber.toString();
    var adddec = changenumber.indexOf(".");
    var numdecs =  changenumber.substring(adddec+1).length
    if (adddec == -1){
        return("£"+changenumber+".00");
    } else if (numdecs == 1){
        return("£"+changenumber+"0");
    } else {
        if (numdecs > 2){
            return("£"+changenumber.substring(0, (adddec+3)));
        } else {
            return("£"+changenumber);
        }
    }
}
function bookinfototal(){
    price = 0;
    $('.price').each(function(index){
        info = $(this).html();
        price = Number(info) + price;
    })
    var grandtotal =  Number($('#roomtotal').html()) +  price;
    $('#ordertotal').html(grandtotal);
   // alert(grandtotal);
}
bookinfototal();
//lala = $('.price').html();
//alert(lala);
$('.roomamount #numroomselect select').change(function(){
    quantity = $(this).val();
   // alert(quantity);
    //$("select").append("this is some more");
    $("#roomtype").load("includes/roomtype.php?numberroom="+quantity);
})
//this is to change the number of rooms auto selected to 1;
$('.roomamount #numroomselect select').val(1);
// this is to auto load the roomtype list;
//$("#roomtype").load("http://www.hotels24seven.com/roomtype.php?numberroom="+quantity);
//
$('#selection').submit(function(){
    $arrdate = $('#arrivedate').val();
    $depdate = $('#departdate').val();
    //alert($arrdate);
    if ($arrdate == ""){
        $('#error').html("Please enter an arrive date");
        return false;
    }
    if($depdate == ""){
        $('#error').html("Please enter a depart date");
        return false;
    }
  //  $('.roomamount select').val(1);
})
// this is to bookout a whole day
var request = $(".cal");
request.click(function(){
    info = $(this).val();
    //alert(info);
    $.post("bookout.php", { bookout: info }, 
        function(success){
            location.reload()
        }
    );
    
})
//change the quantity of rooms aviable in admin section
$("#calendar select").change( function() {
  // check input ($(this).val()) for validity here
  info = this.id;
  quantity = $(this).val();
  //alert(info + "/" + quantity);
	 $.post("roomupdate.php", { room: info, changequanity: quantity } );
	 $(this).css({'background-color' : '#0f0'});

});
//this updates the price of each room
$("#calendar input[type='text']").change( function() {
  // check input ($(this).val()) for validity here
  info = this.id;
  quantity = $(this).val();
 //alert(info);
	 $.post("priceupdate.php", { roomtype: info, changeprice: quantity } );
	 $(this).css({'background-color' : '#0f0'});
});
//
var $booknow = $('.booknowdiv')
var bookheight = $booknow.height(); 
$booknow.hide().css({ height : 0 }); 
$("a.booknow").click(function(){
    var theFrame = $(".bookingiframe", parent.document.body);
	if ($booknow.is(":visible")){
		$(".booknowdiv").animate({"height":0}, { duration: 400, complete:function (){
		$booknow.hide();
                $(".bookingiframe", parent.document.body).animate({"height":theFrame.height($(document.body).height() + 40)}, 400);
		}});
	} else {
		$(".booknowdiv").animate({"height":bookheight}, 400);
                $(".bookingiframe", parent.document.body).animate({"height":theFrame.height($(document.body).height() + 40 + bookheight)}, 400);
		$booknow.show();
	}
	return false;	 	
})
//this is to load the different bits about the hotel on the results page
function movedesc (var_load, var_move){
    //this gets the number out to change the correct div
    var_changediv = var_move.substring(4,5);
    //
    $(".close"+var_changediv+"s").css({"display":"block"});
    $(".hotel"+var_changediv+"s").css({"width":"422px"});
    //this loads the correct data into the correct div
    $("#changediv"+var_move.substring(4,5)).css({"display":"block"});
    $("#changediv"+var_changediv).load(var_load);
    //$("#changediv0").load(var_load);
    //this scrolls through to
        //$("#changediv"+var_changediv+"  li span").each(function(intIndex){
        $("#descnav"+var_changediv+" li").each(function(intIndex){
        idlist = this.id;
            if (idlist != ""){
                $("#"+idlist).css({"background-color":"#d8d8d8"});
            }
        })
    //$("#rev12s").css({"background-color":"#f0f"});

    $(var_move).css({"background-color":"#cccccc"});
}
$(".descnav  a").each(function(intIndex){
    $("#changediv"+this.id.substring(3,4)).css({"display":"none"});
    $(".close"+this.id.substring(3,4)+"s").css({"display":"none"});
    $(".hotel"+this.id.substring(3,4)+"s").css({
        "width":"494px",
        "float":"left",
        "text-align":"right",
        "border-right":"none"
    });
    $(this).bind("click", function (){
            //alert("hello" + intIndex);
            //this gets the id that has been clicked on
            thisid = this.id;
            //alert(thisid);
            if (thisid.substring(0,5) != "hotel"){
                if (thisid.substring(0,5) == "close"){
                    $("#changediv"+thisid.substring(5)).css({"display":"none"});
                    $(".close"+thisid.substring(5)+"s").css({"display":"none"});
                    $(".hotel"+thisid.substring(5)+"s").css({"width":"514px"});
                    $("#descnav"+thisid.substring(5)+" li").each(function(intIndex){
                        idlist = this.id;
                            if (idlist != ""){
                                $("#"+idlist).css({"background-color":"#d8d8d8"});
                            }
                        })
                }else {
                //get the href of the button clicked;
                var_sendinfo = $("#"+thisid+"s a").attr("href");
                //send all this info to the function to do its stuff
                movedesc(var_sendinfo, '#'+thisid+'s');
                }
                return false;
            }
        //})
    })
}
);
$('#parkingcheckbox').change(function(){
	//alert($('#parkingcheckbox').val());
	var info = this.name
	var checked = $('input[name='+info+']').is(':checked');
	if (checked == false){
		//$('.depdate').css({"display" : "none;"})
		$('.noparking').hide();
	} else {
		$('.noparking').show();
	}
})
//this is to work out the height of the window
var theFrame = $(".bookingiframe", parent.document.body);
theFrame.height($(document.body).height() + 40);

})



