// Tooltips
dw_Tooltip.content_vars = {
    L1: '<p class="style34"><strong>Self catering:</strong></p><ul><li>The host will not provide food for the student.</li><li>The student will be given some storage space in the host’s kitchen and <br />have access to the kitchen to cook their own meals. </li></ul><p class="style34"><strong>Bed &amp; Breakfast:</strong></p><ul><li> A continental breakfast is provided (not served) to the students.</li><li> This includes toast, jam, cereal, tea/coffee/fruit juice.</li><li> Students are not generally permitted to use the kitchen to cook or store their own food.</li><li>Some hosts permit light cooking if specifically arranged when booking.</li></ul><p class="style34"><strong>Half Board:</strong></p><ul><li> The host provides breakfast (continental) and dinner for the student.</li><li>The student is not given any access to the kitchen as meals are provided. </li></ul>',
    L2: '<p class="style34"><b>Superior Zones 1/2 rates per week</b></p><td rowspan="2" valign="top" class="standard"><table width="100%"> <tr class="tablepad"> <td colspan="4" valign="top" bgcolor="#FBDEDD" class="greylarge"><div align="center">Bed &amp; Breakfast/Self Catering</div></td> <td colspan="2" valign="top" bgcolor="#FBDEDD" class="greylarge"><div align="center">Half Board</div></td> </tr> <tr class="tablepad"> <td width="25%" valign="top" bgcolor="#E1E9EC" class="standard"><div align="center" class="style35">Single</div></td> <td colspan="3" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center" class="style34"><strong>Twin</strong></div></td> <td width="25%" valign="top" bgcolor="#E1E9EC" class="standard"><div align="center" class="style34"><strong>Single</strong></div></td> <td width="25%" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center" class="style34"><strong>Twin</strong></div></td> </tr><tr class="tablepad"> <td valign="top" bgcolor="#E1E9EC" class="standard"><div align="center"><strong>£190.00</strong></div></td><td colspan="3" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center"><strong>£180.00</strong></div></td><td valign="top" bgcolor="#E1E9EC" class="standard"><div align="center"><strong>£220.00</strong></div></td><td valign="top" bgcolor="#FFFFFF" class="standard"><div align="center"><strong>£210.00</strong></div></td></tr></table><p class="style34"><b>Homestay Zone 2 rates per week</b></p><td rowspan="2" valign="top" class="standard"><table width="100%"> <tr class="tablepad"> <td colspan="4" valign="top" bgcolor="#FBDEDD" class="greylarge"><div align="center">Bed &amp; Breakfast/Self Catering</div></td> <td colspan="2" valign="top" bgcolor="#FBDEDD" class="greylarge"><div align="center">Half Board</div></td> </tr> <tr class="tablepad"> <td width="25%" valign="top" bgcolor="#E1E9EC" class="standard"><div align="center" class="style35">Single</div></td> <td colspan="3" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center" class="style34"><strong>Twin</strong></div></td> <td width="25%" valign="top" bgcolor="#E1E9EC" class="standard"><div align="center" class="style34"><strong>Single</strong></div></td> <td width="25%" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center" class="style34"><strong>Twin</strong></div></td> </tr><tr class="tablepad"> <td valign="top" bgcolor="#E1E9EC" class="standard"><div align="center"><strong>£165.00</strong></div></td><td colspan="3" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center"><strong>£155.00</strong></div></td><td valign="top" bgcolor="#E1E9EC" class="standard"><div align="center"><strong>£195.00</strong></div></td><td valign="top" bgcolor="#FFFFFF" class="standard"><div align="center"><strong>£185.00</strong></div></td></tr></table><p class="style34"><b>Homestay Zone 3 rates per week</b></p><td rowspan="2" valign="top" class="standard"><table width="100%"> <tr class="tablepad"> <td colspan="4" valign="top" bgcolor="#FBDEDD" class="greylarge"><div align="center">Bed &amp; Breakfast/Self Catering</div></td> <td colspan="2" valign="top" bgcolor="#FBDEDD" class="greylarge"><div align="center">Half Board</div></td> </tr> <tr class="tablepad"> <td width="25%" valign="top" bgcolor="#E1E9EC" class="standard"><div align="center" class="style35">Single</div></td> <td colspan="3" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center" class="style34"><strong>Twin</strong></div></td> <td width="25%" valign="top" bgcolor="#E1E9EC" class="standard"><div align="center" class="style34"><strong>Single</strong></div></td> <td width="25%" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center" class="style34"><strong>Twin</strong></div></td> </tr><tr class="tablepad"><td valign="top" bgcolor="#E1E9EC" class="standard"><div align="center"><strong>£140.00</strong></div></td><td colspan="3" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center"><strong>£130.00</strong></div></td><td valign="top" bgcolor="#E1E9EC" class="standard"><div align="center"><strong>£170.00</strong></div></td><td valign="top" bgcolor="#FFFFFF" class="standard"><div align="center"><strong>£160.00</strong></div></td></tr></table><p class="style34"><b>Homestay Zone 4+ rates per week</b></p><td rowspan="2" valign="top" class="standard"><table width="100%"> <tr class="tablepad"> <td colspan="4" valign="top" bgcolor="#FBDEDD" class="greylarge"><div align="center">Bed &amp; Breakfast/Self Catering</div></td> <td colspan="2" valign="top" bgcolor="#FBDEDD" class="greylarge"><div align="center">Half Board</div></td> </tr> <tr class="tablepad"> <td valign="top" bgcolor="#E1E9EC" class="standard"><div align="center" class="style35">Single</div></td> <td colspan="3" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center" class="style34"><strong>Twin</strong></div></td> <td width="25%" valign="top" bgcolor="#E1E9EC" class="standard"><div align="center" class="style34"><strong>Single</strong></div></td> <td width="25%" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center" class="style34"><strong>Twin</strong></div></td> </tr> <tr class="tablepad"> <td width="25%" valign="top" bgcolor="#E1E9EC" class="standard"><div align="center"><strong>£125.00</strong></div></td> <td colspan="3" valign="top" bgcolor="#FFFFFF" class="standard"><div align="center"><strong>£115.00</strong></div></td> <td valign="top" bgcolor="#E1E9EC" class="standard"><div align="center"><strong>£155.00</strong></div></td> <td valign="top" bgcolor="#FFFFFF" class="standard"><div align="center"><strong>£14.00</strong></div></td> </tr> </table>',
    L3: '<div style="text-align:center"><img src="images/dot-com-btn.gif" /></div>',
    L4: 'You can have a tooltip pop up when hovering over an image too of course!'
}

Date.prototype.format=function(format){var returnStr='';var replace=Date.replaceChars;for(var i=0;i<format.length;i++){var curChar=format.charAt(i);if(i-1>=0&&format.charAt(i-1)=="\\"){returnStr+=curChar}else if(replace[curChar]){returnStr+=replace[curChar].call(this)}else if(curChar!="\\"){returnStr+=curChar}}return returnStr};Date.replaceChars={shortMonths:['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],longMonths:['January','February','March','April','May','June','July','August','September','October','November','December'],shortDays:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],longDays:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],d:function(){return(this.getDate()<10?'0':'')+this.getDate()},D:function(){return Date.replaceChars.shortDays[this.getDay()]},j:function(){return this.getDate()},l:function(){return Date.replaceChars.longDays[this.getDay()]},N:function(){return this.getDay()+1},S:function(){return(this.getDate()%10==1&&this.getDate()!=11?'st':(this.getDate()%10==2&&this.getDate()!=12?'nd':(this.getDate()%10==3&&this.getDate()!=13?'rd':'th')))},w:function(){return this.getDay()},z:function(){var d=new Date(this.getFullYear(),0,1);return Math.ceil((this-d)/86400000)}, W:function(){var d=new Date(this.getFullYear(),0,1);return Math.ceil((((this-d)/86400000)+d.getDay()+1)/7)},F:function(){return Date.replaceChars.longMonths[this.getMonth()]},m:function(){return(this.getMonth()<9?'0':'')+(this.getMonth()+1)},M:function(){return Date.replaceChars.shortMonths[this.getMonth()]},n:function(){return this.getMonth()+1},t:function(){var d=new Date();return new Date(d.getFullYear(),d.getMonth(),0).getDate()},L:function(){var year=this.getFullYear();return(year%400==0||(year%100!=0&&year%4==0))},o:function(){var d=new Date(this.valueOf());d.setDate(d.getDate()-((this.getDay()+6)%7)+3);return d.getFullYear()},Y:function(){return this.getFullYear()},y:function(){return(''+this.getFullYear()).substr(2)},a:function(){return this.getHours()<12?'am':'pm'},A:function(){return this.getHours()<12?'AM':'PM'},B:function(){return Math.floor((((this.getUTCHours()+1)%24)+this.getUTCMinutes()/60+this.getUTCSeconds()/ 3600) * 1000/24)}, g:function(){return this.getHours()%12||12},G:function(){return this.getHours()},h:function(){return((this.getHours()%12||12)<10?'0':'')+(this.getHours()%12||12)},H:function(){return(this.getHours()<10?'0':'')+this.getHours()},i:function(){return(this.getMinutes()<10?'0':'')+this.getMinutes()},s:function(){return(this.getSeconds()<10?'0':'')+this.getSeconds()},u:function(){var m=this.getMilliseconds();return(m<10?'00':(m<100?'0':''))+m},e:function(){return"Not Yet Supported"},I:function(){return"Not Yet Supported"},O:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+'00'},P:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+':00'},T:function(){var m=this.getMonth();this.setMonth(0);var result=this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/,'$1');this.setMonth(m);return result},Z:function(){return-this.getTimezoneOffset()*60},c:function(){return this.format("Y-m-d\\TH:i:sP")},r:function(){return this.toString()},U:function(){return this.getTime()/1000}};

jQuery(document).ready(function($) {
	
	// Form Validation
	$('form.validate-form').submit(function() {
		var formObj = $(this);
		$(this).find('.error').remove();
		var hasError = false;
		var errorMsg = '<p class="error">Please complete the fields highlighted above in red in order to calculate the price.</p>';
		
		formObj.find('.required').each(function() {
			if($(this).val() == '') {
				hasError = true;
				$(this).css('border','1px solid red');
			}
			if($(this).hasClass('email')) {
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if(!emailReg.test($(this).val())) {
					hasError = true;
					$(this).css('border','1px solid red');
				}
			}
		});
		if(hasError) {
			formObj.prepend(errorMsg);
			$("html,body").animate({ scrollTop: 0 }, 1000);
			return false;
		} else {
			return true;
		}
		return false;
	});
	
	$('form.validate-form').each(function() {
		var formObj = $(this);
		formObj.find('input,select,textarea').click(function() {
			formObj.find('input, textarea').css('border','2px inset');
			formObj.find('select').css('border','1px solid');
		});
	});
	
	// Datepicker
	$('.date-pick').datePicker({clickInput:true});
	$('#Arrival').bind(
	    'dpClosed',
	    function(e, selectedDates)
	    {
	    	var d = selectedDates[0];
	    	if (d) {
	    		d = new Date(d);
	    		$('#Departure').dpSetStartDate(d.addDays(28).asString());
	    	}
	    }
	);
	$('#Departure').bind(
	    'dpClosed',
	    function(e, selectedDates)
	    {
	    	var d = selectedDates[0];
	    	if (d) {
	    		d = new Date(d);
	    		$('#Arrival').dpSetEndDate(d.addDays(-1).asString());
	    	}
	    }
	);
	
	// Banner
	$('.with-smile').each(function() {
		var _this = $(this);
		
		var div_distance = 0;
		var smile_distance = 0;
		_this.find('.post div:eq(0)').each(function() {
			
			var div_dist = $(this).offset().top;
			var div_height = $(this).height();
			
			div_distance = div_dist + div_height;
			if ($(this).attr('id') == '_mcePaste') {
				div_distance = 0;
			}
			
		//	console.log(div_distance);
		});
		_this.find('.smile').each(function() {
			var smile_dist = $(this).offset().top;
			var smile_height = $(this).height();
			
			smile_distance = smile_dist + smile_height;
		//	console.log(smile_distance);
		});
		if(div_distance != 0) {
			var margin_top = div_distance - smile_distance - 5;
			_this.find('.smile').css('margin-top',margin_top+'px');
		}
	});
	
	// navigation
	$('.sidebar nav ul li ul li').each(function() {
		var _this = $(this);
		// Find those which have sub navigations
		if ($(this).find('.rollout').length > 0) {
			// This is the "a" of the li
			var subnavCont = $(this).find('a:first');
			// Remove the link and give it a has-subnav class
			subnavCont.attr('href','javascript:void(0)').addClass('has-subnav');
			
			// Click action
			subnavCont.click(function() {
				// Toggle displaying of the subnav
				_this.find('.rollout').slideToggle();
				// Change class...
				if(subnavCont.hasClass('has-subnav')) {
					subnavCont.removeClass('has-subnav').addClass('has-subnav-current');
				} else {
					subnavCont.removeClass('has-subnav-current').addClass('has-subnav');
				}
				changeArrow();
			});
		}
	});
	
	// Navigation main level
	$('.sidebar ul li.current-navigation').each(function() {
		$(this).find('ul.second-level').slideDown();
	});
	$('.sidebar ul li.main-level').each(function() {
		var _this = $(this);
		if (_this.find('ul.second-level > li').length > 0) {
			
			// Arrows
			var txt = _this.find('a:first').html();
			txt = txt.replace('&nbsp;&nbsp;▲','');
			txt = txt.replace('&nbsp;&nbsp;▼','');
			if (_this.hasClass('current-navigation')) {
				$(this).find('a:first').html(txt+'&nbsp;&nbsp;&#9650');
			} else {
				$(this).find('a:first').html(txt+'&nbsp;&nbsp;&#9660');
			}
			
			// submenu has.
			_this.find('a:first').live('click',function() {
				
				// Arrows
				var txt = $(this).html();
				txt = txt.replace('&nbsp;&nbsp;▲','');
				txt = txt.replace('&nbsp;&nbsp;▼','');
				
				if (_this.hasClass('current-navigation')) {
					_this.removeClass('current-navigation');
					_this.find('ul.second-level').slideUp();
					$(this).html(txt+'&nbsp;&nbsp;&#9660');
				} else {
					_this.addClass('current-navigation');
					_this.find('ul.second-level').slideDown();
					$(this).html(txt+'&nbsp;&nbsp;&#9650');
				}
				return false;
			});
		}
	});
	
	function changeArrow() {
		
		$('.sidebar nav ul li ul li a.has-subnav').each(function() {
			var txt = $(this).html();
			txt = txt.replace('▲','');
			txt = txt.replace('▼','');
			$(this).html(txt+'&#9660');
		});
		
		$('.sidebar nav ul li ul li a.has-subnav-current').each(function() {
			var txt = $(this).html();
		//	console.log(txt);
			txt = txt.replace('▲','');
			txt = txt.replace('▼','');
			$(this).html(txt+'&#9650;');
		});
		
	}
	
	$('.sidebar nav ul li ul li a.has-subnav').each(function() {
	    $(this).append('&nbsp;&nbsp;&#9660');
	});
	
	$('.sidebar nav ul li ul li a.has-subnav-current').each(function() {
	     $(this).append('&nbsp;&nbsp;&#9650');
	});
	
	// BOOKING FORM JAVASCRIPT
	
	// insert a price row
	function insert_price(title, amt)
	{
		html = '<strong>'+title+': </strong> '+amt+'<br>';
		$('.pricing-field').append(html);
	}
	
	// The main form object
	$('form#book').each(function() {
		var formObj = $(this);
		var tBody = $(this).find('table tbody');
		
		$('.calculate-price').click(function(e) {
			e.preventDefault();
			
			if ($('.pricing-field').length > 0) {
				$('.pricing-field').html('');
			}
			
			var price = 0; // initial price.
			
			// let's gather all the stuff.
			booking_fee = 50;
			
			ar_date = tBody.find('#Arrival').val().split('/');
			arrival = new Date(ar_date[2], ar_date[1]-1, ar_date[0]);
			dp_date = tBody.find('#Departure').val().split('/');
			departure = new Date(dp_date[2], dp_date[1]-1, dp_date[0]);
			
			var one_day=1000*60*60*24
			diff = Math.ceil((departure.getTime() - arrival.getTime()) / (one_day));
			
			//console.log(diff);
			
			weeks = Math.floor(diff / 7);
			remainder = diff % 7;
			
			days = 0;
			if (remainder <= 3) {
				days = remainder;
			} else {
				weeks++;
			}
			
			accomo = tBody.find('select[name=LEADCF42]');
			meal_plan = tBody.find('select[name=LEADCF15]');
			room_type = tBody.find('select[name=LEADCF25]');
			
			flag = 0; // 1 = error. 0 = good to go
			
			if (arrival == 'Invalid Date') {
				flag = 1;
				tBody.find('#Arrival').css('border','1px solid red');
			}
			if (departure == 'Invalid Date') {
				flag = 1;
				tBody.find('#Departure').css('border','1px solid red');
			}
			if (accomo.val() == null) {
				flag = 1;
				accomo.css('border','1px solid red');
			}
			if (meal_plan.val() == null) {
				flag = 1;
				meal_plan.css('border','1px solid red');
			}
			if (room_type.val() == 'Please Select') {
				flag = 1;
				room_type.css('border','1px solid red');
			}
			
			// create the runoff area
			if ($('.pricing-field').length == 0) {
				$(this).parent().parent().after('<tr><td align="right" width="10"></td><td colspan="3" align="left" class="pricing-field"></td></tr>');
			}
			
			if (flag == 0) {
				// success in putting everything in
				var explain = 'Price for ' + accomo.val() + ', ';
				
				room = room_type.val();
				room = room.replace("\\(.+?\\)","");
				explain = explain + room + ' room ';
				
				if (meal_plan.val() != 'No preference') {
					explain = explain + 'with ' + meal_plan.val();
				}
				explain = explain  + ' from ' + arrival.format("jS F Y") + ' to ' + departure.format("jS F Y");
				
				$('.pricing-field').html('<p style="text-align: center;"><strong>'+explain+'</strong></p>'); // done explaining.
				
				// Booking fee
				booking_fee = 50;
				price += booking_fee;
				insert_price('Booking Fee', '&pound;'+booking_fee); // Insert booking fee.
				
				if (weeks > 0) {
					if (days != 0) {
						insert_price('Length of stay', weeks + ' weeks, ' + days + ' days');
					} else {
						if (remainder ==0) {
							insert_price('Length of stay', weeks + ' weeks');
						} else {
							wk = weeks-1;
							insert_price('Length of stay', wk + ' weeks, ' + remainder + ' days');
						}
					}
				}
				
				accomo = accomo.val();
				meal_plan = meal_plan.val();
				room_type = room_type.val();
				
				accomo_prices = {"SUPERIOR ZONES 1/2":190, "HOMESTAY ZONE 2":165, "HOMESTAY ZONE 3":140, "HOMESTAY ZONE 4+":125};
				
				var nums = accomo.length;
				$.each(accomo,function() {
					t_accomo_price = accomo_prices[this];
					
					if (nums > 1) {
						hhh = $('.pricing-field').html();
						$('.pricing-field').html(hhh+'<p style="text-align: left;padding: 10px 0;"><strong><em>'+this+'</em></strong></p>');
					}
					
					if (meal_plan.length == 1 && meal_plan == 'No preference') {
						meal_plan = ['Self Catering / Bed and breakfast', 'Half Board'];
					}
					
					$.each(meal_plan,function() {
						
						this_accomo_price = t_accomo_price;
						
						if (meal_plan.length > 1) {
							hhh = $('.pricing-field').html();
							$('.pricing-field').html(hhh+'<p style="text-align: left;padding: 10px 0;"><strong>'+this+'</strong></p>');
						}
						
						if (this == 'Half Board') {
							// half board prices
							this_accomo_price = this_accomo_price + 30;
						}
						single_accomo_price = 0;
						if (room_type == 'Twin' || room_type == 'Double') {
							single_accomo_price = (this_accomo_price - 10);
							this_accomo_price = single_accomo_price * 2;
						} else if (room_type == 'Triple') {
							single_accomo_price = (this_accomo_price - 10);
							this_accomo_price = single_accomo_price * 3;
						}
						if (this_accomo_price > 0) {
							if (room_type != 'Single') {
								insert_price('Charge per week', '&pound;'+this_accomo_price + ' (&pound;' + single_accomo_price + ' per person)');
							} else {
								insert_price('Charge per week', '&pound;'+this_accomo_price);
							}
							total_lodge = this_accomo_price * weeks;
					
							if (days > 0) {
								day_lodge = Math.ceil(days * (this_accomo_price / 7));
							//	console.log(day_lodge);
								total_lodge = total_lodge + day_lodge;
							}
					
							insert_price('Total accomodation charge', '&pound;'+total_lodge);
							another_price = price + total_lodge;
						}
						insert_price('Total','&pound;'+another_price);
						
					});
					
				});
				
				
			} else if(flag == 1) {
				// oops!
				$('.pricing-field').html('<p style="text-align: center;"><strong>Please complete the fields highlighted above in red in order to calculate the price.</strong></p>');
			}
			
		});
		
		
	});
	
});
