﻿jQuery.noConflict();var $j = jQuery;

// Fire off page specific init method
$j(document).ready(function() {
	 
	initNewWindowLinks();
	//initQuickSubscribe();
	//initQuickSearch();
	if (typeof initPage == "function"){
		initPage();
	}	
});

function initNewWindowLinks(){
	$j("a.new-window").click(function(){
		window.open(this.href);
		return false;
	});
}

function submitQuickSubscribe(){
	var forename = $j("#quickSubscribeForename")[0].value;
	var surname = $j("#quickSubscribeSurname")[0].value;
	var emailAddress = $j("#quickSubscribeEmail")[0].value;
	
	// Strip HTML
	forename = forename.replace(/(<([^>]+)>)/ig,"").Replace("'", "\'");
	surname = surname.replace(/(<([^>]+)>)/ig,"").Replace("'", "\'");
	emailAddress = emailAddress.replace(/(<([^>]+)>)/ig,"").Replace("'", "\'");

	if (forename == "" || forename == "first name" || surname == "" || surname == "surname" || emailAddress == "" || emailAddress == "email address" || !/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(emailAddress)){
		alert("Please complete all fields and ensure your email address is correct.");
		return;
	}

	ajaxAide.invokeDotNetMethod(
		"app_code",
		"WebsiteUtility",
		"SaveQuickSubscription",
		"forename=" + escape(forename) + "&surname=" + escape(surname) + "&emailAddress=" + escape(emailAddress),
		function(response){
			$j("#subscribeForm").html("<p>" + response.value + "</p>");	
		}
	);	
}
/*
function initQuickSearch(){
	
	$j("#quickSearchRegionsContainer").html("");
	$j("#quickSearchDestinationsContainer").html("");
	$j("#quickSearchTypesContainer").html("");
	
	var typesDropDown = "<select id='quickSearchTypes'><option value='0'>Any type</option><option value='1'>Activity and Sport</option><option value='2'>Beach</option><option value='3'>City and Nightlife</option><option value='4'>Culture and History</option><option value='5'>Family</option><option value='6'>Spa</option><option value='7'>Wildlife and Nature</option></select>";
	$j("#quickSearchTypesContainer").html(typesDropDown);
	
	var regionsDropDown = "<select id='quickSearchRegions'><option value='0'>Select region</option>";
	for (var i in regionsAndDestinations){
		regionsDropDown += "<option value='" + regionsAndDestinations[i].id + "'>" + regionsAndDestinations[i].name + "</option>";
	}
	regionsDropDown += "</select>";
	$j("#quickSearchRegionsContainer").html(regionsDropDown);
	$j("#quickSearchRegions").bind("change", function(){
		
		$j("#quickSearchDestinationsContainer")
		.html("")
		.css("display", "none");
		
		if (this.selectedIndex > 0){
			var destinationsDropDown = "<select id='quickSearchDestinations'><option value='0'>Select destination</option>";
			var region = regionsAndDestinations["region_" + this.options[this.selectedIndex].value];
			for (var i in region.destinations){
				destinationsDropDown += "<option value='" + region.destinations[i].id + "'>" + region.destinations[i].name + "</option>";
			}
			destinationsDropDown += "</select>";
			$j("#quickSearchDestinationsContainer")
			.html(destinationsDropDown)
			.css("display", "block");
		}		
	});
		
	$j("#ctl00_quickSearchSubmit")
	.css("cursor", "pointer")
	.click(function(){
		
		var region=0;
		var destination=0;
		var destinationName="";
		var holidaytype=0;
		
		var regionsDropDown = $j("#quickSearchRegions")[0];
		if (typeof regionsDropDown != "undefined"){
			region = regionsDropDown.options[regionsDropDown.selectedIndex].value;
		}
		
		var destinationDropDown = $j("#quickSearchDestinations")[0];
		if (typeof destinationDropDown != "undefined"){
			destination = destinationDropDown.options[destinationDropDown.selectedIndex].value;
			destinationName = destinationDropDown.options[destinationDropDown.selectedIndex].text
		}
		
		if ( destinationName != "" && destination != 0 ){		 
		  destinationName = destinationName.replace(", ","-").replace(" ","-");		  		  		
		  
		  var url = appPath + "destinations/" + destination + "/" + destinationName + ".htm";
		  location.href=url;	
		}	
		else {
		
		  var typesDropDown = $j("#quickSearchTypes")[0];
		  if (typeof typesDropDown != "undefined"){
			holidaytype = typesDropDown.options[typesDropDown.selectedIndex].value;
		  }
		
		  var url = appPath + "search.htm?region=" + region + "&destination=" + destination + "&holidaytype=" + holidaytype;
		  location.href=url;			
		}																					
	});
}*/