$j(function() {
		$j("#DateVisited").datepicker();
		$j("#bookArrival").datepicker();
		$j("#ArrivalDate").datepicker();
	});

var marstons = {

	submitSearch: function() 
	{
//		//Priority is given to the drop down list 
//		//If no inn is selected from the drop dow, 
//		//only then will the other search fields be taken into consideration
		if($F('InnUrl') != '0') {
			document.location.href = $F('InnUrl');
		}
		else {
			if($F('PostcodeTown') == '' || $F('PostcodeTown') == 'Town or postcode'){
				alert('Please enter a town or postcode.');
				return;
			}

			document.location.href = "/search/"+$F('PostcodeTown');
		}

	},
	submitFilterSearch: function() 
	{
		//Priority is given to the drop down list 
		//If no inn is selected from the drop dow, 
		//only then will the other search fields be taken into consideration
		if($F('InnUrl') != '0') {
			document.location.href = $F('InnUrl');
		}
		
		else {
			if($F('PostcodeTown') == '' || $F('PostcodeTown') == 'Enter town or postcode'){
				alert('Please enter a town or postcode, or select an inn from the list.');
				return;
			}
			
			var weddings = $j('#Weddings').is(":checked") ? 'Yes' : '';
			var functionRoom = $j('#FunctionRoom').is(":checked") ? 'Yes' : '';			
//
//			
//			var weddings = document.getElementById('Weddings').checked ? 'Yes' : '';
//			var functionRoom = document.getElementById('FunctionRoom').checked ? 'Yes' : '';
//			var pubTavern = document.getElementById('PubTavern').checked ? 'Yes' : '';

			

			document.location.href = "/search/"+$F('PostcodeTown')
			+"/?distance="+$F('Distance')
			+"&wedd="+weddings
			+"&fr="+functionRoom
			
//			document.location.href = "/search/"+$F('PostcodeTown')
//			+"/?distance="+$F('Distance')
//			+"&wedd="+$F('Weddings')
//			+"&fr="+$F('FunctionRoom')
//			+"&pt="+$F('PubTavern');
		}
	},
	
	checkAvailability: function(dateInputId) 
	{
		if(ValidateDate(dateInputId)!=false) {
			
			if($F('BookInnId') != '0') {
				document.location = "/accommodation?bookInn="+$('BookInnId').value
				+"&bookArrival="+$('ArrivalDate').value
				+"&bookNights="+$('Nights').value
				+"&bookRooms="+$('RoomsForm').value;
			}
			
			else {
				if($F('BookPostcodeTown') == '' || $F('BookPostcodeTown') == 'Enter town or postcode'){
					alert('Please enter a town or postcode, or select an inn from the list.');
					return;
				}
				
				document.location = "/booking?bookInn="+$('BookInnId').value
				+"&location="+$('BookPostcodeTown').value+",UK"
				+"&bookArrival="+$('ArrivalDate').value
				+"&bookNights="+$('Nights').value
				+"&bookRooms="+$('RoomsForm').value;
			}
		}
	},
	
	selectRoom: function() 
	{
		var t = new Array();
		var size = 0;
//		var adults = 0;
//		var children = 0;
//		var infants = 0;
		
		$$('input[type=radio]').each(function( n ){
			if(n.checked){	
				var r = new Array();
				var roomId = n.value;
				var RoomSelection = n.name;
				var adults = $(RoomSelection+'noOfAdults').value;
				var children = $(RoomSelection+'noOfChildren').value;
				var infants = 0;//$(RoomSelection+'noOfInfants').value;
				r.push(roomId,adults,children,infants);
				
				t.push(r);
				size++;
			};
		})
		
//		sum up party:
//		$$('select[name=noOfAdults]').each(function( n ){
//			adults+=parseInt(n.value);
//		})
//		
//		$$('select[name=noOfChildren]').each(function( n ){
//			children+=parseInt(n.value);
//		})
//		
//		$$('select[name=noOfInfants]').each(function( n ){
//			infants+=parseInt(n.value);
//		})

		if(size < 1) {
			alert('Please select the rooms you wish to book');
		}
		else {
//			alert("a:"+adults+"  c:"+children+"  i: "+infants);
			xajax_selectRoom(t);
		}
		
	},
	
	updateRoomList: function(room, roomCount) 
	{
		var adults = $(room+'noOfAdults').value;
		var children = $(room+'noOfChildren').value;
		var partySize = parseInt(adults) + parseInt(children);
		
		//alert("a:"+adults+" -c:"+children+" -p:"+partySize);
		var tableRows = $('Table'+room).rows;

		for (i = 1; i < tableRows.length; i++){   
	        var row = tableRows[i];//$('Room'+room+'-'+i);
	        var roomSize = row.getAttribute('max');

	        var availability = row.getAttribute('availability');
	        if (partySize > roomSize && row.style.display=='') {
	        	var check = row.cells[4].firstChild;
	        	check.checked=false;
	        	row.style.display='none';
	        	marstons.showOnAnotherRoom(roomCount, room, i);
	        } else if (row.style.display=='none'){
	        	var visibility = marstons.roomVisibility(roomCount, i);
	        	if(visibility == availability) {
	        		marstons.hideFromAnotherRoom(roomCount, room, i);
	        	}
	        	
	        	visibility = marstons.roomVisibility(roomCount, i);
	        	if (visibility<availability && partySize <= roomSize) {
	        		row.style.display='';
	        	}
	        }
	    }
	},
	
	showOnAnotherRoom: function(roomcount, hiddenFrom, roomId)
	{
		roomId--;
		for(x=1 ; x <= roomcount; x++) {
			if(x==hiddenFrom) continue;
			
			var adults = $(x+'noOfAdults').value;
			var children = $(x+'noOfChildren').value;
			var partySize = parseInt(adults) + parseInt(children);
			
			var tableRow = $('Room'+x+'-'+roomId);
			var roomSize = tableRow.getAttribute('max');
			if(tableRow.style.display=='none' && partySize <= roomSize) {
				tableRow.style.display='';
				break;
			}
		}
	},
	
	hideFromAnotherRoom: function(roomcount, shownOn, roomId)
	{
		roomId--;
		for(x=roomcount ; x > 0; x--) {
			if(x==shownOn) continue;

			var tableRow = $('Room'+x+'-'+roomId);
			if(tableRow.style.display=='') {
				var rowRadio = $('Check'+x+'-'+roomId);
				if(rowRadio.checked==false) {
					tableRow.style.display='none';
					break;
				}
			}
		}
	},
	
	roomVisibility: function(roomCount, roomId) 
	{
		roomId--;
		visibility = 0;
		for(x=1 ; x <= roomCount; x++) {
			var tableRow = $('Room'+x+'-'+roomId);
			if(tableRow.style.display=='') {
				visibility++;
			}
		}
		
		return visibility;
	},
	
	initialRemoval: function(rooms) 
	{
		for(x=1 ; x <= rooms; x++) {
			var tableRows = $('Table'+x).rows;
			
			for (y = 1; y < tableRows.length; y++){   
		        var row = tableRows[y];
		        var availability = row.getAttribute('availability');
		        if (availability < x) {
		        	 row.style.display='none';
		        } 
		    }
		}
	}
		
};

var offers = {

	submitSearch: function(PostcodeTown,SearchQuery) 
	{
		if($F('PostcodeTown') == '' || $F('PostcodeTown') == 'Enter town or postcode'){
			alert('Please enter a town or postcode.');
			return;
		}
	},

	offerLocationSearch: function()
	{
		document.location = "/offer-details/"+$F('OfferId')+"/"+$F('LocationSearch');
	}
};

var weddings = {
			
	weddingsLocationSearch: function()
	{
		document.location = "/weddings/"+$F('LocationSearch');
	}
};

var functions = {

	functionsLocationSearch: function()
	{
		document.location = "/functions/"+$F('LocationSearch');
	}
};

var MarstonsFeedback = {
	selectInn: function(innId) {
		if(innId == 0) {
			$('ManualInn').show();
		} else {
			$('ManualInn').hide();
		}
	}
};

// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
//var minYear=1900;
//var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	

	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("Please enter a valid date (dd-mm-yyyy)");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strYear.length != 4 || year==0 ){
		alert("Please enter a valid 4 digit year");
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
	
	var today = new Date();
	var nextYear = new Date();
	nextYear.setDate(nextYear.getDate()+365);
	var selectedDate = new Date(strYear,strMonth-1,strDay, '23', '59', '59');
	
	if(selectedDate<today){
		alert("You can not arrive before today's date");
		return false;
	}
	
	if(selectedDate>nextYear){
		alert("You can not make a booking more than a year in advance");
		return false;
	}
	
return true
}

function ValidateDate(dateInputId){
	var dt=document.getElementById(dateInputId)
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }