


var region_requested=new Hash();

var widget_data = eval([["NV","22","1246593540","Thursday, Jul 2","1246852740","Sunday, Jul 5","110","Reno"],["NV","22","1247371140","Saturday, Jul 11","1247543940","Monday, Jul 13","133","Las Vegas"],["NV","22","1247371140","Saturday, Jul 11","1247630340","Tuesday, Jul 14","133","Las Vegas"],["NV","22","1248580740","Saturday, Jul 25","1248839940","Tuesday, Jul 28","109","Lake Tahoe"]]);


			// Selectable regions map objects, [object->key, object->value]
			var regions_data = eval([{"key":"31","value":"Caribbean"},{"key":"32","value":"Europe"},{"key":"33","value":"Mexico\/Central America"},{"key":"34","value":"Canada"},{"key":"35","value":"United States"},{"key":"41","value":"South America"}]);
			
			// Selectable regions map hash, regions_keys_hash[key] = value
			var region_keys_hash = new Hash();
			regions_data.each(function(item) {
				region_keys_hash.set(item.key, item.value);
			});
		
			// States map objects, [object->key, object->value]
			var states_data = eval([{"key":"AL","value":"Alabama"},{"key":"AK","value":"Alaska"},{"key":"AB","value":"Alberta"},{"key":"AS","value":"American Samoa"},{"key":"AZ","value":"Arizona"},{"key":"AR","value":"Arkansas"},{"key":81,"value":"Baker Island"},{"key":"BC","value":"British Columbia"},{"key":"CA","value":"California"},{"key":"CO","value":"Colorado"},{"key":"CT","value":"Connecticut"},{"key":"DE","value":"Delaware"},{"key":"FM","value":"Fed. States Of Micronesia"},{"key":"FL","value":"Florida"},{"key":"GA","value":"Georgia"},{"key":"GU","value":"Guam"},{"key":"HI","value":"Hawaii"},{"key":84,"value":"Howland Island"},{"key":"ID","value":"Idaho"},{"key":"IL","value":"Illinois"},{"key":"IN","value":"Indiana"},{"key":"IA","value":"Iowa"},{"key":86,"value":"Jarvis Island"},{"key":67,"value":"Johnston Atoll"},{"key":"KS","value":"Kansas"},{"key":"KY","value":"Kentucky"},{"key":89,"value":"Kingman Reef"},{"key":"LA","value":"Louisiana"},{"key":"ME","value":"Maine"},{"key":"MB","value":"Manitoba"},{"key":"MH","value":"Marshall Islands"},{"key":"MD","value":"Maryland"},{"key":"MA","value":"Massachusetts"},{"key":"MI","value":"Michigan"},{"key":71,"value":"Midway Islands"},{"key":"MN","value":"Minnesota"},{"key":"MS","value":"Mississippi"},{"key":"MO","value":"Missouri"},{"key":"MT","value":"Montana"},{"key":76,"value":"Navassa Island"},{"key":"NE","value":"Nebraska"},{"key":"NV","value":"Nevada"},{"key":"NB","value":"New Brunswick"},{"key":"NH","value":"New Hampshire"},{"key":"NJ","value":"New Jersey"},{"key":"NM","value":"New Mexico"},{"key":"NY","value":"New York"},{"key":"NF","value":"Newfoundland"},{"key":"NC","value":"North Carolina"},{"key":"ND","value":"North Dakota"},{"key":"MP","value":"Northern Mariana Islands"},{"key":"NT","value":"Northwest Territories"},{"key":"NS","value":"Nova Scotia"},{"key":"OH","value":"Ohio"},{"key":"OK","value":"Oklahoma"},{"key":"ON","value":"Ontario"},{"key":"OR","value":"Oregon"},{"key":"PW","value":"Palau"},{"key":95,"value":"Palmyra Atoll"},{"key":"PA","value":"Pennsylvania"},{"key":"PE","value":"Prince Edward Island"},{"key":"PR","value":"Puerto Rico"},{"key":"QC","value":"Quebec"},{"key":"RI","value":"Rhode Island"},{"key":"SK","value":"Saskatchewan"},{"key":"SC","value":"South Carolina"},{"key":"SD","value":"South Dakota"},{"key":"TN","value":"Tennessee"},{"key":"TX","value":"Texas"},{"key":"UM","value":"Us Minor Outlying Islands"},{"key":"UT","value":"Utah"},{"key":"VT","value":"Vermont"},{"key":"VI","value":"Virgin Islands"},{"key":"VA","value":"Virginia"},{"key":79,"value":"Wake Island"},{"key":"WA","value":"Washington"},{"key":"DC","value":"Washington D.C."},{"key":"WV","value":"West Virginia"},{"key":"WI","value":"Wisconsin"},{"key":"WY","value":"Wyoming"},{"key":"YT","value":"Yukon Territory"}]);
			
			// States map hash, state_keys_hash[key] = value
			var state_keys_hash = new Hash();
			states_data.each(function(item) {
				state_keys_hash.set(item.key, item.value);
			});

// Gets a state name based in it's prefix
			function GetStateName(prefix) {

states_data.each(function(item) {
	
				
if(prefix==item.key){
return(item.value);
}

});

			

}

			// Gets a region named based in it's key
			function GetRegionName(key) {


regions_data.each(function(item) {
				
if(key==item.key){
return(item.value);
}

});

			}


function FillRegions() {

				// Hash for the states and regions
				var states = new Hash();
				var regions = new Hash();
				
				
				
				// Current region value
				var current_value = $F('in_region_pd_key');
				


				// Clear the region box
				$('in_region_pd_key').length = 0;
				
				// Sort the states and regions
				regions = regions.sortBy(function(item) {return(item.value)});
				states = states.sortBy(function(item) {return(item.value)});
				
				// Fill the regions select with the first two options
				$('in_region_pd_key').options[$('in_region_pd_key').length] = new Option('Choose a Region or State', '');
				$('in_region_pd_key').options[$('in_region_pd_key').length] = new Option('See All Destinations', '');
							
				// Add to the region field the regions select

regions_data.each(function(item) {
				
$('in_region_pd_key').options[$('in_region_pd_key').length] = new Option(item.value, item.key);
			});


				
				// Add the ---OR--- option to the regions select
				$('in_region_pd_key').options[$('in_region_pd_key').length] = new Option('-------- OR --------', '');
				
				// Add the states to the regions select

states_data.each(function(item) {
$('in_region_pd_key').options[$('in_region_pd_key').length] = new Option(item.value, item.key);

			});

				
				// Set the current value
				if (current_value)
					for (var i = 0; i < $('in_region_pd_key').length; i++)
						if ($('in_region_pd_key').options[i].value == current_value)
							 $('in_region_pd_key').options[i].selected = true;
			}



// Called when the region is changed
			function RegionChanged() {
				//when data is loaded calls to RegionChangedActualize
				ActualizeWidgetInfo('RegionChangedActualize', widget_data);
				
			}
			function RegionChangedActualize() {
				FillCities();
			}


function IsRegion(key) {


regions_data.each(function(item) {
				
if(key==item.key){
return(true);
}
			});
				


				return(false);
			}

// Helper function for FillCities()
			function FillCityHelper(region_key, widget_data, cities, i) {


if (widget_data[i][1] == region_key){
						cities.set(widget_data[i][6], widget_data[i][7]);

}
if (widget_data[i][0] == region_key) {
						name = widget_data[i][7];
						if (!name.match(/ Vicinity/i) &&
							!name.match(/ Room & Car/i) &&
							!name.match(/ Room and Car/i) )
							cities.set(widget_data[i][6], widget_data[i][7]);


					}

return;

				// Don't add the cities that should be not used in select
				// Check if the region_key is equal to the region key or state and add the city

				if (IsRegion(region_key)) {
					if (widget_data[i][1] == region_key)
						cities.set(widget_data[i][6], widget_data[i][7]);


				} else {



					if (widget_data[i][0] == region_key) {
						name = widget_data[i][7];
						if (!name.match(/ Vicinity/i) &&
							!name.match(/ Room & Car/i) &&
							!name.match(/ Room and Car/i) )
							cities.set(widget_data[i][6], widget_data[i][7]);
					}
				}
			}


// Fill the cities box
			function FillCities() {
				// Values selected
				var region_key = $F('in_region_pd_key');
				var start_date = $F('in_start_date');
				
				
				// Current value
				var current_value = $F('in_pd_key'); 
				
				// Clear the cities box
				$('in_pd_key').length = 0;
//goTo('ci');
				
				// Hash to store the cities
				var cities = new Hash();
//cities.set(region_key,region_key);


				
				// If there is not region selected... set a empty option and return
				if (region_key == '') {
					$('in_pd_key').options[$('in_pd_key').length] = new Option('--------------------', '');
					return;
				}
				
				
				// If we don't have any date selected
				
					// Loop ver the data and get all the possible regions
					for (var i = 0; i < widget_data.length; i++)
						FillCityHelper(region_key, widget_data, cities, i);
									
				
				// Sort the regions, we pass the data to an array and then again to a hash
				//cities = cities.sortBy(function(item) {return(item.value)});
				

				// Check the cities that we have for that region
				if (cities.length == 0) { // No cities at all
					$('in_pd_key').options[$('in_pd_key').length] = new Option('No city available', '');
				} else if (cities.length == 1) { // One city for that regionn
					// Add the only city that we have and select it
					cities.each(function(item) {
						$('in_pd_key').options[0] = new Option(item.value, item.key);
						$('in_pd_key').options[0].selected = true;
					});
				} else { // More than 1 city for the region
					// Add the first two options to the city select
					$('in_pd_key').options[$('in_pd_key').length] = new Option('Choose a Destination', '');
					$('in_pd_key').options[$('in_pd_key').length] = new Option('See All Destinations', '');
					
					// Add the cities to the city select
					cities.each(function(item) {
						$('in_pd_key').options[$('in_pd_key').length] = new Option(item.value, item.key);
					});
				}

				
				// Set the current value
				if (current_value)
					for (var i = 0; i < $('in_pd_key').length; i++)
						if ($('in_pd_key').options[i].value == current_value)
							 $('in_pd_key').options[i].selected = true;
			}


function ActualizeWidgetInfo(callbak, widget_data){
				
				var dk_qry='';var st_qry='';var et_qry='';
				if(ok=$F('in_origination_key')) ok_qry ='&ori_k='+ ok;
				if(dk=$F('in_region_pd_key')) dk_qry ='&des_k='+ dk;
				

				if(ok){
					//no region choosed or not requested before
					if(dk && !region_requested[dk]){
						
//						var url = 'http://res99.lmdeals.com/widgetTunnel.php?ori_k='+ ok + dk_qry
						 var url = 'http://vegasdealadvisor.com/test_site/tt.php?ori_k='+ ok + dk_qry;

//var url = 'tt.php';


//var url = 'http://res99.lmdeals.com/widgetTunnel.php';

//goTo('zer');
						new Ajax.Request(url, {
						  method: 'get',
						  onComplete: function(transport) {


						  
						  	//goTo(transport.status);
					  		var new_widget_data = eval(transport.responseText);

					  		var widget_data_temp=new Array()
					  		var i = 0
					  		if(isNaN(dk))
					  			key=0;
					  		else
					  			key=1;


							while( i < widget_data.length){
								if(widget_data[i][key]==dk){
									widget_data.splice(i,1);


								}else{
									i++;

								}
							}



							for (var i = 0; i < new_widget_data.length; i++){
								widget_data.push(new_widget_data[i]);

							

							}

					  		

							if(dk) region_requested[dk]= 1;
					  		eval(callbak+'();');
						  },
						  onFailure: function(transport) {

						  	eval(callbak+'();');


						  }
						});
					}else{
						//the data is in the widget not need actualize
						eval(callbak+'();');
					}
				}else{
					//no Origin
				}
			}





// Called when the form is submited
			function setFormAction(affiliateId) {
				var page; // Store the page destination
			
				// Set the redirection page by checking if a city is selected 
				if ($F('in_pd_key'))
					page = 'http://res99.lmdeals.com/results.html';
				else
					page = 'http://res99.lmdeals.com/list.html';

				// Set the action form checking if it's an affiliate page
				if (typeof affiliateId != 'undefined' && affiliateId.match(/\d+/) != null)
					$('widget_form').action = '/affiliate/' + affiliateId + page;
				else
					$('widget_form').action = page;
				
		

				
									
				// Set the in_destform_state value
				if ($F('in_region_pd_key')) {
					if (IsRegion($F('in_region_pd_key'))) { // The region selected selected is a selectable region
						if (GetRegionName($F('in_region_pd_key')) == 'Canada')
							$('in_destform_state').value = 'canada';  // Why do we have to put 'canada' and not 'Canada' ?
						else
							$('in_destform_state').value = GetRegionName($F('in_region_pd_key'));
					} else { // The region selected is a state
						$('in_destform_state').value = $F('in_region_pd_key');
					}
				}
				
				// Set the in_region_pd_key as every package_destination_key that belongs to the region, separated by ,
				var cities_keys = '';
				cities_hash = new Hash();
				if (IsRegion($F('in_region_pd_key'))) {
					for (var i = 0; i < widget_data.length; i++)
						if (widget_data[i][1] == $F('in_region_pd_key'))
							cities_hash.set(widget_data[i][6], widget_data[i][6]);
				} else {
					for (var i = 0; i < widget_data.length; i++)
						if (widget_data[i][0] == $F('in_region_pd_key'))
							cities_hash.set(widget_data[i][6], widget_data[i][6]);
							
				}
				cities_hash.each(function f(item) {
					cities_keys = cities_keys + item.value + ",";
				});
				cities_keys = cities_keys.substring(0, cities_keys.length - 1); // Take off the last ,
				$('in_region_pd_key')[$('in_region_pd_key').selectedIndex].value = cities_keys;
				
				// Submit the form
				$('widget_form').submit();
			}
 


