// **************************************** CONFIGURATION **************************************** //
// ID of the select element (location dropdown)
var LOCATION_SELECT_ID = 'location_dropdown'
// class name when dropdown is static
var LOCATION_DROPDOWN_STATIC_CLASS = 'location_dropdown_static';
// class name when dropdown is dynamic and values set from the WebService
var LOCATION_DROPDOWN_DYNAMIC_CLASS = 'location_dropdown_dynamic';

// ProductSearch instance website - HAS TO END WITH "/"
var SEARCH_URL = 'http://uk-search.eviivo.com/productsearchcityvisitor/'; //"http://uk-search.eviivo.com/lastminute/";
// relative URL of ServiceScriptProxy page with the name of function called
var DEFAULT_LOCATION_PROXY_PAGE = 'ServicesScriptProxy.ashx/GetLocationCollection';
var DEFAULT_TIMEOUT = 1000;

// using the same definition as returned from the Web Service {Value:"",Key:""}, where <OPTION VALUE="Value">Key</OPTION>
// when no Key defined than using the same as Value...
var DEFAULT_LOCATIONS = [
	{Value:"",Key:"Select destination >>"},
	{Value:"",Key:"-----------------------------"},
	{Value:"",Key:"Top 10 destinations"},
	{Value:"",Key:"-----------------------------"},
	{Value:"Blackpool"},
	{Value:"Cheshire"},
	{Value:"Cornwall"},
	{Value:"Devon"},
	{Value:"East Sussex"},
	{Value:"Kent"},
	{Value:"Northumberland"},
	{Value:"Oxfordshire"},
	{Value:"Somerset"},
	{Value:"Torquay"},
	{Value:"",Key:"-----------------------------"},
	{Value:"",Key:"In alphabetical order"},
	{Value:"Abergele"},
	{Value:"Alnmouth"},
	{Value:"Alnwick"},
	{Value:"Andover"},
	{Value:"Avon"},
	{Value:"Axminster"},
	{Value:"Bamburgh"},
	{Value:"Bampton"},
	{Value:"Barnard Castle"},
	{Value:"Barnstaple"},
	{Value:"Basingstoke"},
	{Value:"Bath"},
	{Value:"Bath &amp; Northeast Somerset"},
	{Value:"Beadnell"},
	{Value:"Bedale"},
	{Value:"Bedford"},
	{Value:"Bedfordshire"},
	{Value:"Belford"},
	{Value:"Belmont"},
	{Value:"Berkshire"},
	{Value:"Berwick Upon Tweed"},
	{Value:"Berwick-Upon-Tweed"},
	{Value:"Biddenden"},
	{Value:"Bideford"},
	{Value:"Birkenhead"},
	{Value:"Birmingham"},
	{Value:"Bishop Auckland"},
	{Value:"Blackpool"},
	{Value:"Bognor Regis"},
	{Value:"Bournemouth"},
	{Value:"Braunton"},
	{Value:"Bridgwater"},
	{Value:"Bridlington"},
	{Value:"Brighton"},
	{Value:"Bristol"},
	{Value:"Broadstairs"},
	{Value:"Brotton"},
	{Value:"Buckfastleigh"},
	{Value:"Buckinghamshire"},
	{Value:"Canterbury"},
	{Value:"Carnforth"},
	{Value:"Chagford"},
	{Value:"Chard"},
	{Value:"Cheltenham"},
	{Value:"Cheshire"},
	{Value:"Chester"},
	{Value:"Chester le Street"},
	{Value:"Chester-Le-Street"},
	{Value:"Chichester"},
	{Value:"Chippenham"},
	{Value:"Chorley"},
	{Value:"Chulmleigh"},
	{Value:"Churcham"},
	{Value:"Cirencester"},
	{Value:"Cleveland"},
	{Value:"Clitheroe"},
	{Value:"Colyton"},
	{Value:"Congleton"},
	{Value:"Consett"},
	{Value:"Conwy"},
	{Value:"Cookham"},
	{Value:"Corbridge"},
	{Value:"Cornwall"},
	{Value:"County Durham"},
	{Value:"Crewkerne"},
	{Value:"Crook"},
	{Value:"Darlington"},
	{Value:"Dartmoor"},
	{Value:"Dartmouth"},
	{Value:"Deal"},
	{Value:"Devizes"},
	{Value:"Devon"},
	{Value:"Dorchester"},
	{Value:"Dorking"},
	{Value:"Dorset"},
	{Value:"Dover"},
	{Value:"Dulverton"},
	{Value:"Durham"},
	{Value:"East Sussex"},
	{Value:"East Yorkshire"},
	{Value:"Eastbourne"},
	{Value:"Edenbridge"},
	{Value:"Edinburgh"},
	{Value:"Esher"},
	{Value:"Exeter"},
	{Value:"Exmoor"},
	{Value:"Exmouth"},
	{Value:"Falmouth"},
	{Value:"Farnborough"},
	{Value:"Fife"},
	{Value:"Fifield"},
	{Value:"Folkestone"},
	{Value:"Freuchie Cupar"},
	{Value:"Frosterley"},
	{Value:"Garstang"},
	{Value:"Gateshead"},
	{Value:"Glastonbury"},
	{Value:"Glendale"},
	{Value:"Gloucester"},
	{Value:"Gloucestershire"},
	{Value:"Godalming"},
	{Value:"Greater Manchester"},
	{Value:"Gt Ayton"},
	{Value:"Guildford"},
	{Value:"Guisborough"},
	{Value:"Haltwhistle"},
	{Value:"Hampshire"},
	{Value:"Hannafore"},
	{Value:"Harrow"},
	{Value:"Hartlepool"},
	{Value:"Hastings"},
	{Value:"Haydon Bridge"},
	{Value:"Helston"},
	{Value:"Henley on Thames"},
	{Value:"Hertfordshire"},
	{Value:"Hexham"},
	{Value:"Highbridge"},
	{Value:"Holsworthy"},
	{Value:"Holt NrTrowbridge"},
	{Value:"Hove"},
	{Value:"Ilfracombe"},
	{Value:"Isle of Man"},
	{Value:"Isle of Skye"},
	{Value:"Isle of Wight"},
	{Value:"Kent"},
	{Value:"Kineton"},
	{Value:"Kingsbridge"},
	{Value:"Knutsford"},
	{Value:"Lancashire"},
	{Value:"Lancaster"},
	{Value:"Lancs"},
	{Value:"Leatherhead"},
	{Value:"Leicestershire"},
	{Value:"Lifton"},
	{Value:"Liskeard"},
	{Value:"Liss"},
	{Value:"Liverpool"},
	{Value:"London"},
	{Value:"Lydney"},
	{Value:"Lynton"},
	{Value:"Macclesfield"},
	{Value:"Maidenhead"},
	{Value:"Maidstone"},
	{Value:"Malpas"},
	{Value:"Manchester"},
	{Value:"Market Harborough"},
	{Value:"Martock"},
	{Value:"Mawdesley"},
	{Value:"Merseyside"},
	{Value:"Middlesborough"},
	{Value:"Middlesex"},
	{Value:"Middleton in Teesdale"},
	{Value:"Midlothian"},
	{Value:"Milton Keynes"},
	{Value:"Minehead"},
	{Value:"Morecambe"},
	{Value:"Moreton-in-Marsh"},
	{Value:"Morpeth"},
	{Value:"Newcastle Upon Tyne"},
	{Value:"Newquay"},
	{Value:"Newton Abbot"},
	{Value:"North Devon"},
	{Value:"North Yorkshire"},
	{Value:"Northumberland"},
	{Value:"Northwood"},
	{Value:"Ogborne St George"},
	{Value:"Okehampton"},
	{Value:"Otterburn"},
	{Value:"Oxford"},
	{Value:"Oxfordshire"},
	{Value:"Paignton"},
	{Value:"Parracombe"},
	{Value:"Penzance"},
	{Value:"Pershore"},
	{Value:"Petersfield"},
	{Value:"Pickering"},
	{Value:"Plymouth"},
	{Value:"Port Erin"},
	{Value:"Portsmouth"},
	{Value:"Preston"},
	{Value:"Ramshaw"},
	{Value:"Reading"},
	{Value:"Redbourn"},
	{Value:"Redcar"},
	{Value:"Redditch"},
	{Value:"Rickmansworth"},
	{Value:"Romsey"},
	{Value:"Rothbury"},
	{Value:"Rufford"},
	{Value:"Rye"},
	{Value:"Salcombe"},
	{Value:"Salisbury"},
	{Value:"Saltburn"},
	{Value:"Sandown"},
	{Value:"Scarborough"},
	{Value:"Seahouses"},
	{Value:"Seaton"},
	{Value:"Seaton Carew"},
	{Value:"Shaftesbury"},
	{Value:"Shanklin"},
	{Value:"Shepton Mallet"},
	{Value:"Sherborne"},
	{Value:"Sidmouth"},
	{Value:"Simonburn"},
	{Value:"Sittingbourne"},
	{Value:"Skipton"},
	{Value:"Somerset"},
	{Value:"South Brent"},
	{Value:"South Devon"},
	{Value:"South Gloucestershire"},
	{Value:"South Molton"},
	{Value:"South Petherton"},
	{Value:"South Shields"},
	{Value:"South Wingate"},
	{Value:"South Zeal"},
	{Value:"Southampton"},
	{Value:"Southport"},
	{Value:"Spennymoor"},
	{Value:"St Leonards On Sea"},
	{Value:"Stanford Dingley"},
	{Value:"Stanhope"},
	{Value:"Stanley"},
	{Value:"Stevenage"},
	{Value:"Stockton on Tees"},
	{Value:"Stourport on Severn"},
	{Value:"Street"},
	{Value:"Sturminster Newton"},
	{Value:"Sunderland"},
	{Value:"Surrey"},
	{Value:"Swanage"},
	{Value:"Tarporley"},
	{Value:"Taunton"},
	{Value:"Tavistock"},
	{Value:"Tees Valley"},
	{Value:"Teeside"},
	{Value:"Teignmouth"},
	{Value:"Thornley"},
	{Value:"Tiverton"},
	{Value:"Torquay"},
	{Value:"Torrington"},
	{Value:"Trimdon Station"},
	{Value:"Truro"},
	{Value:"Tunbridge Wells"},
	{Value:"Tyne and Wear"},
	{Value:"Tynemouth"},
	{Value:"Umberleigh"},
	{Value:"Virginia Water"},
	{Value:"Wantage"},
	{Value:"Warminster"},
	{Value:"Warrington"},
	{Value:"Warwickshire"},
	{Value:"Wellington"},
	{Value:"Wells"},
	{Value:"West Auckland"},
	{Value:"West Midlands"},
	{Value:"West Sussex"},
	{Value:"Weston-Super-Mare"},
	{Value:"Weymouth"},
	{Value:"Whitchurch"},
	{Value:"Whitley Bay"},
	{Value:"Wiltshire"},
	{Value:"Winkleigh"},
	{Value:"Winsford"},
	{Value:"Wirral"},
	{Value:"Wolsingham"},
	{Value:"Wolverhampton"},
	{Value:"Woodstock"},
	{Value:"Worcestershire"},
	{Value:"Worthing"},
	{Value:"Yelverton"},
	{Value:"Yeovil"},
	{Value:"York"},
	{Value:"Yorkshire"}
];

// **************************************** SETUP VARIABLES **************************************** //
// sample generated Location Proxy URL:
// http://localhost/ProductSearch/ServicesScriptProxy.ashx/GetLocationCollection?var=SERVICE_RESULT
// - GetLocationCollection - name of Web Service method
// - var=SERVICE_RESULT - the result of Web Service calling is returned in this variable
// do not touch these...
var SERVICE_RESULT;
var SERVICE_RESULT_VARIABLE = "SERVICE_RESULT";
var LOCATION_PROXY_PARAM = '?var='+SERVICE_RESULT_VARIABLE
// industry type is hardcoded now, could be modified according to same element's value for some instance
var INDUSTRY_TYPE_PROXY_PARAM = "&IndustryType=";
var SERVICE_RETRIES = 0;
var SERVICE_RETRIES_MAX = 3;

// **************************************** HELPER VARIABLES **************************************** //

var is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );
var is_ie5 = ( is_ie && /msie 5\.0/i.test(navigator.userAgent) );
var is_ie6 = ( is_ie && /msie 6\.0/i.test(navigator.userAgent) );
var is_opera = /opera/i.test(navigator.userAgent);
var is_khtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);

// **************************************** HELPER FUNCTIONS **************************************** //
function $(id) {return document.getElementById(id);}

// **************************************** CUSTOM FUNCTIONS **************************************** //

function initPage() {
	// first populate the dropdown with default values in case the WebService is not accessible
	populateLocationDropdown(DEFAULT_LOCATIONS);
	// than try to get values from the WebService
	initService();
}


function getIndustryType() {
	// hardcoded now, could retrieve from the selected element on page
	return 1; //$(INDUSTRY_TYPE_ID).value
}

// **************************************** SERVICE FUNCTIONS **************************************** //

function initService()
{
	SERVICE_RETRIES = 0;
	SERVICE_RESULT = null;

	var head = document.getElementsByTagName("head").item(0);
    var script = document.createElement("script");
	var URL = getServiceURL();
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", URL);
    head.appendChild(script);
	processServiceResultTimeout();
}
function getServiceURL() {
	var industryTypeValue = getIndustryType();
	return SEARCH_URL + DEFAULT_LOCATION_PROXY_PAGE + LOCATION_PROXY_PARAM + INDUSTRY_TYPE_PROXY_PARAM + industryTypeValue;
}
function processServiceResultTimeout() {
	setTimeout(processServiceResult,DEFAULT_TIMEOUT);
}
function processServiceResult()
{	
	// NOTE: to test Service unavailability uncomment the following line
	//SERVICE_RESULT = null;

	// if result returned from Web Service proxy is still null, try to wait for the result
	if(SERVICE_RESULT==null)
	{
		SERVICE_RETRIES++;
	}
	if(SERVICE_RESULT==null &&  SERVICE_RETRIES < SERVICE_RETRIES_MAX) {
		// try to retrieve data again
		return setTimeout(processServiceResult,DEFAULT_TIMEOUT);
	}

	// if number of tries to connect to Web Service exceeds maximum number of tries do not replace the dropdown
	if( SERVICE_RETRIES == SERVICE_RETRIES_MAX || SERVICE_RESULT==null ) {
		$(LOCATION_SELECT_ID).className = LOCATION_DROPDOWN_STATIC_CLASS;
		return false;
	}
	
	$(LOCATION_SELECT_ID).className = LOCATION_DROPDOWN_DYNAMIC_CLASS;
	// populate dropdown
	populateLocationDropdown(SERVICE_RESULT);
}
function populateLocationDropdown(locations) {
	var locationObj = $(LOCATION_SELECT_ID);
	var value;
	var text;
	// reset the select dropdown
	locationObj.innerHTML = "";
	for(var i=0;i<locations.length;i++) {
		text = locations[i].Key;
		value = locations[i].Value;
		if(!text)
			text = value;
		var option = document.createElement('option');
		option.value = value;
		option.innerHTML = text;
		locationObj.appendChild(option);
	}
}