formchangedstatus = false;

function cancelbooking(id)
{
	var proceed = confirm("Are you sure you wish to cancel this booking?");
	if(proceed) document.location="/bookingE/lib/cancelbooking.php?id="+id;
}

function honourbooking(id)
{
	var proceed = confirm("Are you sure you wish to honour this booking?");
	if(proceed) document.location="/bookingE/lib/honourbooking.php?id="+id;
}

function updateChildren(room)
{
	var childselector = document.getElementsByName("children"+room)[0];
	var adultselector = document.getElementsByName("adults"+room)[0];
	var kidtitle = document.getElementById("kidtitle"+room);
	var rmtitle = document.getElementById("rm"+room);
	var tttitle = document.getElementById("tt"+room);
	var roomsrequired = document.getElementsByName("roomsrequired"+room)[0];
	var totalroomprice = document.getElementsByName("totalroomprice"+room)[0];
	var totalp = document.getElementById("total"+room);
	childselector.options.length = 0;
	childselector.disabled = false;
	childselector.className="selectoron";
	kidtitle.className="l";
	rmtitle.className="l";
	tttitle.className="l";
	roomsrequired.className="roomreqon";
	totalroomprice.className="roomreqon";
	totalp.className="totalon";
	var maxadults = parseInt(document.getElementsByName("maxadults"+room)[0].value);
	var adultsselected = parseInt(adultselector.value);
	if(isNaN(adultsselected)) adultsselected = 0;
	var occupancy = parseInt(document.getElementsByName("occupancy"+room)[0].value);
	var roomsallowed = parseInt(document.getElementsByName("roomsallowed"+room)[0].value);
	var adultrequired = false;
	var kidsallowed = 0;
	if(document.getElementsByName("adultrequired"+room)[0].value=="1") adultrequired = true;
	if(adultrequired) kidsallowed = (occupancy*(Math.ceil(adultsselected/maxadults)))-adultsselected;
	else kidsallowed = (roomsallowed * occupancy) - adultsselected;
	for(i=0;i<=kidsallowed;i++) childselector.options[i] = new Option(i,i);
	updateRoomCount(adultsselected,childselector.value,room);
}
function updateRoomPrice(room)
{
	var adultselector = document.getElementsByName("adults"+room)[0];
	var childselector = document.getElementsByName("children"+room)[0];
	if (adultselector.value=="...") var adultvalue = 0;
	else adultvalue = parseInt(adultselector.value);
	updateRoomCount(adultvalue,childselector.value,room);
}
function updateRoomCount(adultvalue,childvalue,room)
{
	if(isNaN(adultvalue)) adultvalue = 0;
	var childdiscount = parseInt(document.getElementsByName("childdiscount")[0].value)/100;
	var maxadults = parseInt(document.getElementsByName("maxadults"+room)[0].value);
	var roomcountfield = document.getElementsByName("roomsrequired"+room)[0];
	var roomtotalfield = document.getElementsByName("totalroomprice"+room)[0];
	var roomplan = document.getElementsByName("pricingplan"+room)[0].value;
	var unitprice = parseFloat(document.getElementsByName("unitprice"+room)[0].value);
	var occupancy = parseInt(document.getElementsByName("occupancy"+room)[0].value);
	var nights = parseInt(document.getElementsByName("nights")[0].value)+1;
	var totaloccupants = parseInt(adultvalue) + parseInt(childvalue);
	var tempRoomCount = Math.ceil(totaloccupants / occupancy);
	if (tempRoomCount>Math.ceil(parseInt(adultvalue)/maxadults)) roomcountfield.value = tempRoomCount;
	else roomcountfield.value = Math.ceil(parseInt(adultvalue)/maxadults);
	if(roomplan=="1") roomtotalfield.value = unitprice * parseInt(roomcountfield.value) * nights;
	else roomtotalfield.value = 
		(unitprice * parseInt(adultvalue) * nights)+(unitprice * childdiscount * parseInt(childvalue) * nights);
	roomtotalfield.value = (Math.round(parseFloat(roomtotalfield.value)*100)/100).toFixed(2)
	updateTotal();
}
function noAction(obj)
{
	alert("Sorry you cannot edit this field. The number of rooms is determined by your adult / child selections.");
	obj.blur();
}
function updateTotal()
{
	
	var roomsonpage = parseInt(document.getElementsByName("roomsavailable")[0].value);
	var subtotal = 0;
	for(i=0;i<=roomsonpage;i++) subtotal += parseFloat(document.getElementsByName("totalroomprice"+i)[0].value);
	var totalfield = document.getElementsByName("totalamount")[0];
	totalfield.value = (Math.round(subtotal*100)/100).toFixed(2);
	if(parseFloat(totalfield.value)>0)
	document.getElementsByName("submitbutton")[0].disabled = false;
	else
	document.getElementsByName("submitbutton")[0].disabled = true;
}
function enable(room)
{
	var btn = document.getElementsByName("select"+room)[0];
	var adults = document.getElementsByName("adults"+room)[0];
	var children = document.getElementsByName("children"+room)[0];
	var roomsrequired = document.getElementsByName("roomsrequired"+room)[0];
	var totalroomprice = document.getElementsByName("totalroomprice"+room)[0];
	var name = document.getElementById("n"+room);
	var adtitle = document.getElementById("adtitle"+room);
	var kidtitle = document.getElementById("kidtitle"+room);
	var rmtitle = document.getElementById("rm"+room);
	var tttitle = document.getElementById("tt"+room);
	var totalp = document.getElementById("total"+room);
	var unitprice = document.getElementsByName("runit"+room)[0];
	if (btn.value=="Select") 
	{
		adults.disabled = false;
		btn.value="De-Select";
		btn.className="selectbuttonon";
		adults.className="selectoron";
		name.className="roomnameon";
		unitprice.className="unitpriceon";
		adtitle.className="l";
	}
	else
	{
		adults.selectedIndex = 0;
		children.selectedIndex = 0;
		roomsrequired.value = "0";
		totalroomprice.value = "0";
		adults.disabled = true;
		children.disabled = true;
		btn.value="Select";
		btn.className="selectbuttonoff";
		adults.className="selectoroff";
		children.className="selectoroff";
		name.className="roomnameoff";
		roomsrequired.className="roomreqoff";
		totalroomprice.className="roomreqoff";
		unitprice.className="unitpriceoff";
		totalp.className="totaloff";
		adtitle.className="o";
		kidtitle.className="o";
		rmtitle.className="o";
		tttitle.className="o";
	}
	updateTotal();
}
function ShowHotelTerms(id)
{
	window.open("hotelterms.php?id="+id,'','toolbar=no,status=no,scrollbars=yes');
}
function ShowOurTerms()
{
	window.open("ourterms.htm",'','toolbar=no,status=no,scrollbars=yes');
}
function UseContactDetails(field)
{
	var val = document.getElementsByName("first_name")[0].value + ' ' + document.getElementsByName("last_name")[0].value;
	var exists = false;
	for(i=0;i<document.contactdetailsform.elements.length;i++) 
	if(document.contactdetailsform.elements[i].value == val) exists = true;
	if(exists) alert("Only one room can be assigned to the contact person.")
	else document.getElementsByName(field)[0].value = val;
}
function in_array(needle,haystack)
{
	for(i=0;i<haystack.length;i++) if(haystack[i]==needle) return true;
	return false;
}
function ProceedToPayment()
{
	var form = document.contactdetailsform;
	var ourterms = form.ourterms;
	var hotelterms = form.hotelterms;
	var firstname = form.first_name.value;
	var lastname = form.last_name.value;
	var address1 = form.address1.value;
	var address2 = form.address2.value;
	var city = form.city.value;
	var state = form.state.value;
	var zip = form.zip.value;
	var email = form.email.value;
	var email2 = form.email2.value;
	var uniquerooms = parseInt(form.rooms.value)+1;
	var extrabits = uniquerooms * 9;
	var leadstart = extrabits + 31;
	var leadfinish = leadstart + (parseInt(form.totalroomsbooked.value)*2);
	var tel = form.night_phone_b.value;
	var leadguestnames = new Array();
	var no_guest_names = false;
	var missingperson = false;
	var duplicatenames = false;
	for(k=leadstart;k<leadfinish;k=k+2) 
	{
		if(in_array(form.elements[k].value,leadguestnames)) duplicatenames = true;
		else leadguestnames.push(form.elements[k].value);	
	}
	
	for(k=leadstart;k<leadfinish;k=k+2) if(form.elements[k].value=="") missingperson = true;
	if(ourterms.checked==false || hotelterms.checked==false) 
		alert("You must agree to both the hotel's terms and our terms in order to proceed.");
	else if(firstname=="") 
		alert("Please enter your first name.");
	else if(lastname=="") 
		alert("Please enter your last name.");
	else if(address1=="") 
		alert("Please enter the first line of your address.");
	else if(city=="") 
		alert("Please enter your town / district.");
	else if(state=="") 
		alert("Please enter your county.");
	else if (!isNumber(tel)) 
		alert("Please enter a valid telephone number!"); 
	else if(zip=="") 
		alert("Please enter your Post Code.");
	else if(email=="") 
		alert("Please enter your email address.");
	else if(email!=email2) 
		alert("Your email addresses do not match.");
	else if(email==''||!goodEmail(email)) alert("You must enter a valid email address.");
	else if(missingperson) alert("You need to specify a lead guest name for each of the rooms you have booked.");
	else if(duplicatenames) alert("You cannot have the same guest as the lead guest for more than one room.");
	else form.submit();
}
function amenddetails()
{
	document.confirmform.action = "availability.php";
	document.confirmform.submit();
}

function GoToHotel(hotelid)
{
	document.location = "showproperty.php?site="+hotelid;
}


function updatecutoff()
{
	var val = document.getElementsByName("leadtime")[0].value;
	var div = document.getElementById("cutoffdiv");
	var cutoff = document.getElementsByName("cutoff")[0];
//	if (document.all) { // is IE
	if (val=='0') div.style.visibility = 'visible';
	else 
	{
		div.style.visibility = 'hidden';
		cutoff.selectedIndex = 0;
	}
}

function submitpropertydetails()
{
	if(document.propertydetailsform.description.value=="") 
		alert("Please enter a short description of your site in the field provided.");
	else if (document.propertydetailsform.openingtimes.value=="") 
		alert("Please provide some idea of your opening times, such as 'all year round', for example.");
	else 
		document.propertydetailsform.submit();
}
function submitpropertyimages()
{
	document.propertyimagesform.submit();
}

function deletesiteimage(number,site)
{
	var answer = confirm("Are you sure you want to delete this image?")
	if(answer) document.location='../lib/deletesitepic.php?id='+site+'&picid='+number;
}
function deletesiteimage2(number,site)
{
	var answer = confirm("Are you sure you want to delete this image?")
	if(answer) document.location='../lib/deletesiteimages.php?id='+site+'&picid='+number;
}

function submittransportdetails()
{
	var form = document.transportdetailsform;
	
	if(form.ucoach[0].checked) form.unitcoach.value = " Miles";
	else form.unitcoach.value = " Km";
	if(form.ulocal[0].checked) form.unitlocal.value = " Miles";
	else form.unitlocal.value = " Km";
	if(form.umajor[0].checked) form.unitmajor.value = " Miles";
	else form.unitmajor.value = " Km";
	if(form.uunder[0].checked) form.unitunder.value = " Miles";
	else form.unitunder.value = " Km";
	if(form.uairport[0].checked) form.unitairport.value = " Miles";
	else form.unitairport.value = " Km";
	if(form.uroad[0].checked) form.unitroad.value = " Miles";
	else form.unitroad.value = " Km";
	
	
	var baddistance = false;
	for(i=0;i<6;i++) if(!isNumber(form.elements[(i*4)+3].value)&&form.elements[(i*4)+3].value!="0"&&form.elements[(i*4)+3].value!="") baddistance = true;
	if(form.directions.value=="") 
		alert("General Directions:\nPlease provide some directions on how to find your site from the nearest major transport connection.");
	else if(baddistance)
		alert("You have entered an invalid distance. (Enter a blank if not applicable).");
	else 
		form.submit();
}

function submit_to_us(site)
{
	var answer = confirm("The information you have entered will now be processed and listed on our top ranked Google pages.\n\nCheck List:\n\n  1. Added all your rooms.\n  2. Entered as much detail as possible.\n  3. Entered accurate availability for all your rooms.\n  4. Added pictures to all your rooms and to your property where desired.\n\nIf you are completely happy with the information you have entered, click 'OK' to proceed.\n\nIf you would like to review your details, please click 'Cancel'.");
	if(answer) document.location='../lib/submitdetailstous.php?site='+site;
}

function isPic(file)
{
	return ((file).substring(file.length-3) == "gif"||(file).substring(file.length-3) == "GIF"||(file).substring(file.length-3) == "jpg"||(file).substring(file.length-3) == "JPG")
}

function formchanged()
{
	formchangedstatus = true;	
}

function submitavailabilityform()
{
	var formcomplete = true;
	var form = document.availabilityform;
	var ignore = new Array("showmonths","monthstart","year","firstday","lastday","siteID","roomname","maximum","submitbutton","registered");
	var missingvalues = new Array();
	for (i=0; i<form.elements.length; i++)
	{
		tempname = form[i].name;
		needtovalidate = true;
		for (j = 0;j<ignore.length;j++) if (tempname==ignore[j]||form[i].disabled) needtovalidate = false;
		if(needtovalidate)
		{
			if (!isNumber(form[i].value)&&form[i].value!=""&&form[i].value!="0")
			{
				alert("Invalid Input!\n\nYou can only enter numbers into the availability fields. You have entered "+form[i].value+" for "+tempname);
				return -1;
			}
			if (parseInt(form[i].value) > parseInt(form.maximum.value) || parseInt(form[i].value) < 0)
			{
			alert("Careful!\n\nYou have entered an invalid availability value for "+tempname+".\n\nYou can only enter a value between 0 and "+form.maximum.value+".\n\nYou have entered: "+form[i].value);
				return -1;	
			}
			if(form[i].value=="") missingvalues[missingvalues.length] = tempname;	
		}
	}
	carryon = true;
	if (missingvalues.length!=0)
	{
		missingdates = "";
		for(i=0;i<missingvalues.length;i++) missingdates = missingdates+missingvalues[i]+" ";
		carryon = confirm("You have not entered any availability for "+missingdates+".\n\nWould you like to continue anyway?\n\nClick 'OK' to CONTINUE.\n\nClick 'Cancel' to RETURN TO THE FORM.");
	}
	if(carryon) 
	{
		form.submitbutton.value = "Submitting....";
		form.submit();
	}
}

function submitsiteform()
{
	var form = document.register_form;
	var nametaken = false;
	for(i=20;i<form.elements.length;i++) if(form.username.value==form.elements[i].value) nametaken = true;
	if(form.username.value=='') alert("You must enter a username!");
	else if(nametaken) alert("Sorry but the username you have specified has already been taken. Please choose another.");
	else if(form.pass1.value==''||form.pass2.value=='') alert("You must enter a password in both fields!");
	else if(form.pass1.value!=form.pass2.value) alert("Your passwords must match!");
	else if(form.hotelierfirstname.value=='') alert("Please enter partner's first name.");
	else if(form.hotelierlastname.value=='') alert("Please enter partner's last name.");
	else if(form.sitename.value=='') alert("Please enter the name of your holiday property.");
	else if(form.sitestreet.value=='') alert("Please enter the street of your property.");
	else if(form.sitetown.value=='') alert("Please enter the town of your property.");
	else if(form.sitecounty.value=='') alert("Please enter the county of your property.");
	else if(form.sitepostcode.value=='') alert("Please enter the postcode of your property.");
	else if(form.sitetel.value=='') alert("You must enter a telephone number for your property!");
	else if (!isNumber(form.sitetel.value)) alert("Please enter a valid telephone number!"); 
	else if (!isNumber(form.sitefax.value)&&form.sitefax.value!="") alert("Please enter a valid fax number!");
	else if(form.siteemail.value==''||!goodEmail(form.siteemail.value)) alert("You must enter a valid email address!");
	else if(!form.agreetoterms.checked) alert("You must agree to the terms of this service in order to proceed!");
	else form.submit();
}
function isNumber(value,allowfloat)
{
	if (allowfloat) value = value.replace('.','');
	value = replaceIt(value," ","");
	value = replaceIt(value,"0","");
	value = replaceIt(value,".","");
	return (parseInt(value)==value);
}

function replaceIt(sString, sReplaceThis, sWithThis) { 
if (sReplaceThis != "" && sReplaceThis != sWithThis) { 
var counter = 0; 
var start = 0; 
var before = ""; 
var after = ""; 
while (counter<sString.length) { 
start = sString.indexOf(sReplaceThis, counter); 
if (start == -1) { 
break; 
} else { 
before = sString.substr(0, start); 
after = sString.substr(start + sReplaceThis.length, sString.length); 
sString = before + sWithThis + after; 
counter = before.length + sWithThis.length; 
} 
} 
} 
return sString; 
} 

function goodEmail(value)
{
	 var at = (value.indexOf('@')!=-1);
	 var dot = (value.indexOf('.')!=-1);
	 return (at&&dot);
}
function define(obj)
{
	window.open('../lib/tooltip.php?title=' + obj,'', 'width=200,height=255,scrollbars=no,status=no,left=300,top=250,menubar=no,toolbar=no,screenX=300,screenY=250');
}
function showmap(code)
{
	window.open('/bookingE/lib/showmap.php?code=' + code,'', 'width=620,height=560,scrollbars=no,status=no,resizable=no,left=300,top=250,menubar=no,toolbar=no,screenX=300,screenY=250');
}
function showmap2(code)
{
	window.open('/bookingE/lib/showmap2.php?code=' + code,'', 'width=620,height=560,scrollbars=no,status=no,resizable=no,left=300,top=250,menubar=no,toolbar=no,screenX=300,screenY=250');
}
function href(action)
{
	document.location='control.php?action=' + action;
}
function roomdelete(id,name)
{
	var answer = confirm("Are you sure you want to delete your "+name+" room?")
	if(answer) document.location='../lib/deleteroom.php?id='+id+'&type='+name;
}
function deleteroompic(id,name)
{
	var answer = confirm("Are you sure you want to delete your "+name+" room picture?")
	if(answer) document.location='../lib/deleteroompic.php?id='+id+'&roomtype='+name;
}
function submittariffdetails()
{
	var form = document.tariffdetailsform;
	badnumber = baddiscount = false;
	form.actualage.value = form.childlimit.value;
	if(form.discount[0].checked) form.actualdiscount.value = form.discountamount.value;
	else form.actualdiscount.value = "0";	
	if(form.discount[0].checked&&form.actualdiscount.value=="0") baddiscount = true;
	
	
	if (form.highstartdate.value == "DD-MM-YYYY" || form.highstartdate.value == "") 
		alert ("Please specify the date when your high-season begins.");
	else if (form.highenddate.value == "DD-MM-YYYY" || form.highenddate.value == "")
		alert ("Please specify the date when your high-season ends.");
	else if (form.actualage.value=="0") alert("Please specify the age of guests considered as children at your establishment.");
	else if (baddiscount) alert("You have specified a child discount but not specified any discount for it.");
	else if (form.leadtime.value=="0"&&form.cutoff.value=="-") alert("Please specify your cut-off time for taking bookings when the arrival date is the same day.");
	else if (form.cancelpolicy.value == "") alert ("Please enter some form of cancellation policy.");
	else if (!(form.payment1.checked || form.payment2.checked || form.payment3.checked || form.payment4.checked || form.payment5.checked || form.payment6.checked || form.payment7.checked))
		alert("Please specify at least one payment method.");
	else form.submit();
}
function submitroomdetails()
{
	form = document.roomdetailsform;
	actualname = form.actualname;
	pricingplan = form.pricingplan;
	if(form.site_type.value=="4"||form.site_type.value=="2") 
	{
		form.breakfast.value=="0";
		start = 19;
		startb = 6;
	}
	else 
	{
		if(form.breakfastT.checked) form.breakfast.value = "1";
		start = 20;
		startb = 6;
	}
	if(form.accompanyT.checked) form.accompany.value = "1";
	if(form.ensuiteT.checked) form.ensuite.value = "1";
	roomexists = false;
		
	if(form.pType.selectedIndex != (form.pType.length-1)) actualname.value =  form.pType.options[form.pType.selectedIndex].text;
	else actualname.value = form.cType.value;
	
	for(i=0;i<form.elements.length;i++) if(form.elements[i].name == actualname.value) roomexists = true;
	
	if(form.plantype[0].checked) pricingplan.value = "0";
	else pricingplan.value = "1";
		
	pricesdone = true;
	for(i=0;i<14;i++) if(form.elements[start+i].value=="") pricesdone = false;
	
	badprice = false;
	for(i=0;i<14;i++) if(!isNumber(form.elements[start+i].value,true)&&form.elements[start+i].value!="0") badprice = true;
	
	specdone = true;
	for(i=0;i<4;i++) if(form.elements[startb+i].value=="") specdone = false;
	
	badspec = false;
	for(i=0;i<4;i++) if(!isNumber(form.elements[startb+i].value)&&form.elements[startb+i].value!="0") badspec = true;
	
	if( actualname.value=="Select..."||actualname.value=="") alert("You need to specify a type of room.");
	else if (roomexists) alert("You have already installed a room of this type.\n\nIf you would like to edit it, select 'Edit Details' from the 'Room Setup' page on the left.");
	else if (!specdone) alert("You have not completed all the specifications!");
	else if (badspec) alert("You have entered an invalid specification value.");
	else if (parseInt(form.maxadults.value)>parseInt(form.occupancy.value)) 
		alert("The maximum number of adults you have specified exceeds the occupancy limit of your room.");
	else if (parseInt(form.count.value)<1) alert("You must have at least one room of this type.");
	else if (!isPic(form.imgfile.value)&&form.imgfile.value!="")
		alert("You can only enter a JPG image for your room image.");
	else if (!pricesdone) alert("Please specify a price for each of the days in the week.");
	else if (badprice) alert("You have entered an invalid price for your pricing plan!");
	else 
	{
		if (form.original.value == "")
		answer = confirm("Would you like to enter availability details now?\n\nClick 'OK' to proceed to complete availability.\n\nClick 'Cancel' to complete availability later.");
		else answer = false;
		if (answer) form.alsoenteravailability.value = "1";
		else form.alsoenteravailability.value = "0"
		form.submit();
	}
}
function update()
{
	var form = document.roomdetailsform;
	var shortcuts = form.pType;
	var custom = form.cType;
	var text = shortcuts.options[shortcuts.selectedIndex].text;
	var occs = form.occupancy;
	var desc = form.roomdesc;
	var adts = form.maxadults;
	var first = false;
	if (form.original.value=="") first = true;
	if (shortcuts.selectedIndex != (shortcuts.length-1))
	{
		custom.value = "";
		if (text == "Double") if (first) {
			occs.value = "2";
			desc.value = "Standard double room with a double bed.";
			adts.value = "2";
		}
		if (text == "Single") if (first) {
			occs.value = "1";
			desc.value = "Standard single room with a single bed.";
			adts.value = "1";
		}
		if (text == "Twin") if (first) {
			occs.value = "2";
			desc.value = "Standard twin room with two single beds.";
			adts.value = "2";
		}
		if (text == "Family") if (first) {
			occs.value = "4";
			desc.value = "Standard family room with a double bed and two single beds. Perfect for the small family.";
			adts.value = "2";
		}
	} 
	else 
	{
		custom.focus();
	}
}
function setCustom()
{
	var widget = document.roomdetailsform.pType;
	widget.selectedIndex = widget.length-1;
}
function updatediscount()
{
	
	field1 = document.tariffdetailsform.discountamount;
	if (document.tariffdetailsform.discount[1].checked) field1.disabled = true;
	else field1.disabled = false;
		
}
function show_availability()
{
	document.location = "control.php?action=enteravailability&roomtype="+document.availabilityform.typeselector.value;
}


















