

$(document).ready(function(){

//$('#holidayextra').get("holidayextra.php");

$.get('hotelextra.php', function(data){
	$('#holidayextra').html(data);
})
$('#changehotels').change(function (){
	//alert("clicked");
	var info = $(this).val();
	//alert(info);
	$.post("hotelsessionchange.php", {change : info},
		function(sucess){
			location.reload()
		}
	)
})

//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 currency
if($('#standardroomtotal').length){
	extrastotals();
	if (fn_makecurrency($('#standardroomtotal').html()) == $('#roomtotal').html()) {
		$('#standardroomtotal').css({'display':'none'});
		
	} else {
		$('#standardroomtotal').html(fn_makecurrency($('#standardroomtotal').html()));
	}
		
}
//this is to change the value to the correct currencys
/*if($('#grandtotal').length){
    $('#grandtotal').html(fn_makecurrency($('#grandtotal').html()));
}*/
largestgrandtotal = 0;
function gettotal(index){
	
    var grandtotal = 0;
    var saving = 0;
    var numday = $("#numdays").html();
	var parkonce = 0;
    $('#hotelselect'+index+' select ').each(function (indexs){
        var getroomtype = this.name.substring(4);
        //alert(Number($('#rackrate'+getroomtype).html()*$(this).val()));
        if(Number($('#rackrate'+getroomtype).html()*$(this).val()) == 0){
            saving = saving+Number($('#roomprice'+getroomtype).val()*$(this).val())*numday
        } else {
            saving = saving+Number($('#rackrate'+getroomtype).html()*$(this).val())*numday;
        }
        grandtotal = grandtotal+Number($('#roomprice'+getroomtype).val()*$(this).val())*numday;
		
		if (parkonce == 0) {
			parkonce = 1;
			saving = saving + Number($('#parksave' + getroomtype).html());
			grandtotal = grandtotal + Number($('#parktotal'+ getroomtype).html());
		}
		//alert(saving);
    })
	//salert("wefsdfdsf");
    $('#hotelselect'+index+' .numrooms').each(function (indexs){
	
	//if ($('#hotelselect' + index + ' span.numrooms ').html() != undefined) {
		var getroomtype = this.id.substring(11);
		//alert(this.id.html());
		//alert(Number($('#rackrate'+getroomtype).html()*$(this).val()));
		if (Number($('#rackrate' + getroomtype).html() * $('#'+this.id).html()) == 0) {
			saving = saving + Number($('#roomprice' + getroomtype).val() * $('#'+this.id).html()) * numday
		}
		else {
			saving = saving + Number($('#rackrate' + getroomtype).html() * $('#'+this.id).html()) * numday;
		}
		
		grandtotal = grandtotal + Number($('#roomprice' + getroomtype).val() * $('#'+this.id).html()) * numday;
	//alert(saving+"/"+Number($('#roomprice'+getroomtype).val()*$(this).val()));
	//}
	grandtotal = grandtotal + Number($('#parktotal'+ getroomtype).html());
	saving = saving + Number($('#parksave'+ getroomtype).html());
	
    })
	//alert($('#parktotal'+ getroomtype).html() + getroomtype);
	//grandtotal = grandtotal + $('#parktotal'+ getroomtype).html();
	//saving = saving + $('#parksave'+ getroomtype).html();
    if (saving == grandtotal){
        $('#savingtotal'+index).html("");
        $('#savingtext'+index).html("");
    } else {
        //$('#savingtext'+index).html("Save ");
        $('#savingtotal'+index).html("<strike>"+fn_makecurrency(saving)+"</strike>");
    }
	if (largestgrandtotal < grandtotal) {
		largestgrandtotal = grandtotal;
	}
    $('#grandtotal'+index).html(fn_makecurrency(grandtotal));
}

$('.hotelrooms').each(function (index){
    //This returns the total value of the order;
    gettotal(index);
 	if ($('#hotelselect'+index+' span.numrooms ').html() != undefined){
		 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());
        })
		$('#hotelselect'+index+' .numrooms').each(function (){
			info = $(this).html()
            //alert (info);
            grandtotals = grandtotals+Number($(this).html());
		})
        if (grandtotals == 0){
            return false;
        }
    })

})

var doOnce = 0;
$('.extras select').each(function(index){

    //$('#extraselect'+index).val(0);
    //make the parking days 0 when the page is refreshed
    var parkingclass = $(this).attr("class").substring(10);

    if (parkingclass == 5){
        //$('#extraselect'+index).val("0 days");
    }
		  
    //make number of cars 0 when the page is refreshed
    if (!((parkingclass == 6) || (parkingclass == -1))){
    //if ((parkingclass != 6)){
		//$('#extraselect'+index).val(0);
        //$('#extraselect'+index).val("0 cars");
    }
	
	//alert($('#extraselect'+index).val());
	if (($('#extraselect'+index).val() != 0) && !((parkingclass == 6) || (parkingclass == -1))){
		//alert("this"+index);
		fn_otherextras(index);
	}
	
	if ((parkingclass == 5) || (parkingclass == 6)) {
		fn_addupparking();
	}
	
	if (parkingclass == -1){
		//var stayinfo = $('#extraselect'+index).attr("name");
		//alert($(stayinfo.val());
		fn_stayonreturn($('#extraselect'+index).val(), index);
	}
	
	extrastotals();

    $('#extraselect'+index).change(function(){
		    //alert(parkingclass);
        if ((parkingclass == 5) || (parkingclass == 6)){
            fn_addupparking();
		} else if (parkingclass == -1){
			//alert($('#extraselect'+index).val());	
			fn_stayonreturn($('#extraselect'+index).val(), index);
		}else {
            fn_otherextras(index);
        }
        extrastotals();

    })
    
})
function fn_otherextras(index){
	var extrassalecost = fn_makenumber($('#standardcost'+index).html());
	var extrascost = fn_makenumber($('#extracost'+index).html());
    var addextra = $('#extraselect'+index).val();
	
    var singleextra = (addextra*extrascost);
	var standardextrarate = (addextra*extrassalecost);
	//alert(standardextrarate + "/"+singleextra);
    if ((extrascost ==  0) && (addextra != 0)){
        $('#extratotal'+index).html("Free");

    } else {
        $('#extratotal'+index).html(fn_makecurrency(singleextra));
    }
	if (standardextrarate > singleextra) {
		$('#extrastandardtotal' + index).html(fn_makecurrency(standardextrarate)+ " ");
	} else {
		$('#extrastandardtotal' + index).html("");
	}
}
// function to work out the saving of the stay on return

function fn_stayonreturn(var_return, thisid){
	var var_returntick = var_return;
	var returncost = $('#returncost').val();
	var returnsavingtotal = $('#returnsavingtotal').val();
	var returnrackrate = $('#returnrackrate').val();

	if (var_returntick == 1){
		if (returnrackrate != returncost) {
			$('#extrastandardtotal' + thisid).html(fn_makecurrency(returnrackrate));
		}
		$('#extratotal'+thisid).html(fn_makecurrency(returncost));
	} else {

		$('#extrastandardtotal'+thisid).html("");
		$('#extratotal'+thisid).html("£0.00");
	}

	extrastotals();

}

//
function fn_addupparking(){

	var parkinginfo = fn_parking($('.extraclass5').html(), $('.extraclass6').val());
			var singleextra = parkinginfo.substring(0, parkinginfo.indexOf("/"));
			var standardextrarate = Number(parkinginfo.substring(parkinginfo.indexOf("/")+1));
			//alert($('.extraclass5').html()+"/" +$('.extraclass6').val());
			if (Number(standardextrarate) > Number(singleextra)) {
				$('#extrastandardtotal0').html(fn_makecurrency(standardextrarate)+" ")
			} else {
				$('#extrastandardtotal0').html("");
			}
            if (($('.extraclass5').html().substring(0, $('.extraclass5').html().indexOf(" ")) != 0) && ($('.extraclass6').val().substring(0, $('.extraclass6').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');
            }
}
//this is to check that they have selected both number of days to park and the number of cars
$('#extraform').submit(function(){
	//if (var_ingore != undefined){
	//alert(var_ignore);
	//}
	if ($("#ignore").html() == 0) {
		//var days = $('.extraclass5').html().substring(0, $('.extraclass5').html().indexOf(" "))
		var days = $('.extraclass5').html();
		var cars = $('.extraclass6').val().substring(0, $('.extraclass6').val().indexOf(" "))
		var trans = $('.extralinks3').val();
		//alert(days +"?"+ cars +"?"+trans);
		if (((days == 0) && (cars != 0))) {
			//alert("Please specify the number of days holiday parking your require. If you don't require any holiday parking set both fields to 0.");
			$("#dialog-nodays").dialog('open');
			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.");
			$("#dialog-nocars").dialog('open');
			return false;
		}
		if ((days != 0) && (cars != 0) && (trans == 0)){
			$("#dialog-notransfers").dialog('open');
			return false;
		}
	}
})
function fn_parking(daysparking, numcars){
    var parkdays = daysparking;
	//Number(daysparking.substring(0, daysparking.indexOf(" ")));
	var cars = Number(numcars.substring(0, numcars.indexOf(" ")));
	if ((parkdays != 0) && (cars != 0)){
    var day08 = $('#parking1').html();
    var day815 = $('#parking2').html();
    var day15 = $('#parking3').html();
    var initialfee = $('#parking4').html();
	var standardday08 = $('#parkingstandard1').html();
    var standardday815 = $('#parkingstandard2').html();
    var standardday15 = $('#parkingstandard3').html();
    var standardinitialfee = $('#parkingstandard4').html();
    //alert(initialfee);
    
    var parkingtotal = 0;
	var standardparkingtotal = 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);    
    }
	
	standardparkingtotal = ((parkdays > 8 ? 8 : parkdays)*standardday08)*cars;
    if (parkdays > 8){
        standardparkingtotal = standardparkingtotal+((((parkdays-8) > 7 ? 7 : (parkdays-8))*standardday815)*cars);
    }
    if (parkdays > 15){
        standardparkingtotal = standardparkingtotal+(((parkdays-15)*standardday15)*cars);    
    }
	
	var standardrates = Number(standardparkingtotal)+Number(standardinitialfee);
	var currentrates =Number(parkingtotal)+Number(initialfee); 
    
	} else {
		var currentrates = 0;
		var standardrates = 0;
	}
	//alert(currentrates+"/"+standardrates);
	return(currentrates+"/"+standardrates);
}
function extrastotals(){

    var extragrandtotal = 0;
	var extrastandardgrandtotal = 0;
	var singlestandardtotaladdon = 0;
	var extraoriginaltotal = 0;
    var formdata = "";
	
    $('.extras select').each(function(index){
		
        var addextra = $('#extraselect'+index).val();
        var singletotal = $('#extratotal'+index).html();
		var singlestandardtotal = $('#extrastandardtotal'+index).html();
        if (singletotal == "Free"){
            singletotal = 0;
        }
	
		if (singlestandardtotal == ""){
			singlestandardtotaladdon = singlestandardtotaladdon + fn_makenumber(singletotal);
		} else {
			singlestandardtotaladdon = singlestandardtotaladdon + fn_makenumber(singlestandardtotal);
		}
		
        extragrandtotal = (fn_makenumber(extragrandtotal) + fn_makenumber(singletotal));
		extraoriginaltotal = (fn_makenumber(extraoriginaltotal) + fn_makenumber(singlestandardtotal));
		//extrastandardgrandtotal = (fn_makenumber(extrastandardgrandtotal) + fn_makenumber(singlestandardtotal));
		
        addextra = (addextra.indexOf(" ") == -1 ? addextra : addextra.substring(0, addextra.indexOf(" ")));
        if ((addextra != 0) && (this.name != "stayonreturn")){
            var addforminfo = this.name.substring(5);
            formdata = formdata+','+ addforminfo;
        }

    })

	if($('.extraclass5').length){
		formdata = formdata +','+$('.extraclass5val').attr("name").substring(5)
	}
	
	//alert(extraoriginaltotal);
    $('#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);
	//extragrandtotal = fn_makenumber($('#returncost').val())+extragrandtotal;
	//alert(fn_makenumber($('#returncost').val()));
	//alert(index);
    $('#extratotal').html(fn_makecurrency(extragrandtotal));
	
	//alert(extrastandardgrandtotal);

    var roomtotal = $('#roomtotal').html();
	var standardroomtotal = $('#standardroomtotal').html();
	
    var extraroomtotal = (fn_makenumber(roomtotal)+fn_makenumber(extragrandtotal));
	
	var extrastandardroomtotal = (fn_makenumber(standardroomtotal)+(singlestandardtotaladdon));
	
	//var includecurrent = extrastandardgrandtotal+singlestandardtotaladdon;
	var includecurrent =0;
	var difference = (extrastandardroomtotal+includecurrent)-extraroomtotal;
	
	
	//alert(extrastandardroomtotal+"/"+(extrastandardroomtotal+includecurrent)+"-"+extraroomtotal);
//	alert(includecurrent);
    $('#grandtotal').html(fn_makecurrency(fn_makenumber(extraroomtotal)));
	if (extrastandardroomtotal == 0){
		$('#extrastandardtotal').html("");
		$('#grandstandardtotal').html("");
	} else 	if (extraroomtotal != (extrastandardroomtotal)) {
    	
		$('#savingtext').html("This booking is reduced from "+fn_makecurrency(extrastandardroomtotal)+", saving you a fantastic "+fn_makecurrency(difference)+"!");
	if ((extraoriginaltotal != 0) ){
		$('#extrastandardtotal').html(fn_makecurrency(extraoriginaltotal)+" ");
	} else {
		$('#extrastandardtotal').html("");
	}
	}

}
$('#editbookingaddextra').submit(function(){
	var additionalinfo = "";
	var formdata = "";
	$('.extras select').each(function(index){
		
        var addextra = $('#extraselect'+index).val();
        var singletotal = $('#extratotal'+index).html();
		var singlestandardtotal = $('#extrastandardtotal'+index).html();
        if (singletotal == "Free"){
            singletotal = 0;
        }
		
		 if ((addextra != 0) && (this.name != "stayonreturn")){
		 
            var addforminfo = this.name.substring(5);
				
            formdata = formdata+','+ addforminfo;

			additionalinfo = additionalinfo + '<input type="hidden" name="extraprice'+addforminfo+'" value="'+fn_makenumber(singletotal)+'">';

			//
        }
		
	})
	$(additionalinfo).insertAfter('.extras');
	//alert(additionalinfo);
	return false;
})

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("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 }  );
     //$.post("roomupdateghp.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;
			var selectedid = thisid.substring(3);
            alert(selectedid);
            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;
            }
        //})
    })
}
);*/
$(".descnav  a").each(function(intIndex){

	$(this).bind("click", function (){
		var thisid = this.id.substring(3);
		var thisrow = this.id.substring(3, (this.id.length-1));
		var thisbutton = this.id.substring((this.id.length-1));
		var hideall = 0;
		
		if (this.id.substring(0, 5) == "close") {
			thisrow = this.id.substring(5);
			var hideall = 1;
		}

		for (x=0;x<=4;x++){
			$('#tab'+thisrow+x).css({"display" : "none"});
		}
		
		if (hideall == 0) {
			$('#tab' + thisid).css({
				"display": "block"
			});
		}
		
		if (thisbutton == 4){
			$('#tab'+thisid).load("gallery.php?num="+thisrow+"&imagename="+$('#gallery'+thisrow).html());
		}
		//$('#tab'+thisid).css({"background-color":"#d8d8d8"});
		return false;
	})
})
$('#editextra select').change(function(){
    var info = this.name;
    var changeto = $(this).val();
    //alert(info + "/" + changeto);
    $.post("extratypeupdate.php", { uid: info, change: changeto} );
})
//to turn the extra on
$("#editextra input[type='checkbox']").change(function(){
    var info = this.name;
    var checked = $('input[name='+info+']').is(':checked');
    var extracost = $('#extracost'+info).val();
    //alert(checked +"/"+info);
    fn_ischecked(info);
    fn_updateextra(info, ($('input[name='+info+']').is(':checked') ? 1 : 0), extracost,-1, info);
})

//when page is loaded it checks to see what is ticked and calls the function to turn the text box on and off
$('#editextra input[type=\'checkbox\']').each(function(index){
    //alert("inher");
    var info = this.name;
    //alert(info);
    fn_ischecked(info, 0);
})
//Change the value of the extra
$("#editextra input[type='text']").change(function(){
    var info = this.name;
	//alert(info);
    //var checked = $('input[name='+info+']').is(':checked');
    //var extracost = $('#extracost'+info).val();
    var classname = $(this).attr("class");
	//alert(info);
	var orderby  = $('#orderby'+info).val();
    if (classname == "changenumber"){
        var checked = $('input[name='+info+']').is(':checked');
        var extracost = $('#extracost'+info).val();
        //fn_updateparking(info, 2, extracost);
        fn_updateextra(info, 2, extracost, "-1/-1/-1");
		alert(1);
    } else if ((classname == "monthcost") || (classname == "salestext")){
		var salecost = $('#saleprice'+info).val();
		var salestext = $('#saledesc'+info).val();
		var month = $('#month'+info).val();
		var year = $('#year'+info).val();
		var monthupdate = $('#monthupdate'+info).val();
		var monthyear = month+"/"+year+"/"+monthupdate;
		fn_updateextra(info, 5, salecost+"|"+salestext, monthyear, orderby);
		$('#monthupdate'+info).val(month);
		$('#remove'+info).removeAttr("disabled");
	} else {
        var thisid = this.id;
        var desc = $("#"+thisid).val();
		//alert(thisid);
        var whichchangebox = thisid.substring(0, (thisid.length-info.length));
        //alert(desc);
        //fn_updateparking(info, (whichchangebox == "extraname" ? 3 : 4), desc);
        fn_updateextra(info, (whichchangebox == "extraname" ? 3 : 4), desc, -1, orderby);
    }
   
})
//remove the sale price 
$("#editextra input[type='button']").click(function(){
	var info = this.id.substring(6);
	var month = $('#month'+info).val();
	var year = $('#year'+info).val();
	var monthupdate = $('#monthupdate'+info).val();
	var monthyear = month+"/"+year+"/"+monthupdate;
	
	//alert(info+", 6, ,"+ monthyear);
	//alert( monthyear);
	fn_updateextra(info, 6, "", monthyear, "");
	location.reload()
	
})
//remove the sale price 
$("#addparking input[type='button']").click(function(){
	var info = this.id.substring(6);
	var month = $('#pmonth'+info).val();
	var year = $('#pyear'+info).val();
	var monthupdate = $('#pmonthupdate'+info).val();
	var monthyear = month+"/"+year+"/"+monthupdate;
	
	//alert(info+", 6, ,"+ monthyear);
	//alert( monthyear);
	fn_updateextra(info, 6, "", monthyear, "");
	location.reload()
	//alert("afert");
	
})
//this function makes the text boxes appear and disappear if they have been ticked
function fn_ischecked(info){
    if ($('input[name='+info+']').is(':checked')){
        //$('#extracost'+info).removeAttr("disabled");
        $('#extracost'+info).css({"display":"inline"});
    } else {
        $('#extracost'+info).css({"display":"none"});
    }
}
//updates the extras and changes the text box green if it is changes
function fn_updateextra(info, update, extrachange, monthyear, orderby){
    //alert(info+"?"+update+"?"+extracost);
    $.post("addremoveextra.php", { extra: info, update: update, change : extrachange , orderby : orderby, salesmonth : monthyear } );
    if (update == 2){
        $("#extracost"+info).css({'background-color' : '#0f0'});
    }
}
//Change the value of the parking
$("#addparking input[type='text']").change(function(){
    var info = this.name;
    var classname = $(this).attr("class");
	//alert(classname);
	
	if  (classname == "parkingcost"){
		var orderby  = $('#orderby'+info).val();
		var salecost = $('#parkingsaleprice'+info).val();
		
		var salestext = $('#parkingsale').val();
		var month = $('#pmonth'+info).val();
		var year = $('#pyear'+info).val();
		var monthupdate = $('#pmonthupdate'+info).val();
		var monthyear = month+"/"+year+"/"+monthupdate;
		//alert(monthyear);
		//alert(info, 5, salecost+"-"+salestext, monthyear);
		fn_updateextra(info, 5, salecost+"|"+salestext, monthyear, orderby);
		$('#pmonthupdate'+info).val(month);
		$('#remove'+info).removeAttr("disabled");
	}	else if (classname == "changenumber"){
        var checked = $('input[name='+info+']').is(':checked');
        var extracost = $('#extracost'+info).val();
		var salecost = $('#saleprice'+info).val();
        fn_updateparking(info, 2, extracost, "-1/-1/-1");
    } else {
        var thisid = this.id;
        var desc = $("#"+thisid).val();
        var whichchangebox = thisid.substring(0, (thisid.length-info.length));
		
		var salestext = $('#saledesc'+info).val();
       // alert(desc);
        fn_updateparking(info, (whichchangebox == "extraname" ? 3 : 4), desc);
    }
})
//updates the extras and changes the text box green if it is changes
function fn_updateparking(info, update, extrachange, monthyear){
    //alert(extrachange);
    //alert(info+"?"+update+"?"+extracost);
    $.post("addremoveextra.php", { extra: info, update: update, change : extrachange , orderby : 1, salesmonth : monthyear} );
    if (update == 2){
        $("#extracost"+info).css({'background-color' : '#0f0'});
    }
}
$('#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);

})




