
function PickLocation()
{
	this.location_script_url = "/ajax/ajax_pickLocation.inc.php";
	this.location_on_success = "/index.php";
	
	this.date_script_url = "/ajax/ajax_pickLocationDateChange.php";
	
	this.get_calendar_url = "/ajax/ajax_pickLocationCalendar2.php";
	
	this.on_start = true;
	
	
	this.start_date = "";
	this.end_date   = "";
	this.selected_start = new Array();
	
	this.start_date_id = "";
	this.end_date_id   = "";
	this.selected_end  = new Array();
	
	this.curr_cal = "start";
	
	this.lang = "EN";
	
	this.month_names;
	this.month_names_EN = new Array('January','February','March','April','May','June',
								'July','August','September','October','November','December');
	this.month_names_ES = new Array('Enero','Febrero','Marzo','April','Mayo','Junio',
								'Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre');
	this.month_names_FR = new Array('Janvier','Février','Mars','Avril','Mai','Juin',
								'Juillet','Août','Septembre','Octobre','Novembre','Décembre');
	this.month_names_DE = new Array('Januar','Februar','März','April','Mai','Juni',
								'Juli','August','September','Oktober','November','Dezember');
									
	this.day_titles;
	this.day_titles_EN = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
	this.day_titles_ES = new Array('Dom','Lun','Mar','Mie','Jue','Vie','Sab');
	this.day_titles_FR = new Array('Dim','Lun','Mar','Mer','Jeu','Ven','Sam');
	this.day_titles_DE = new Array('Son','Mon','Die','Mit','Don','Fre','Sam');
	
	this.dates_popup_id = "CurrentDatesPopup";
	this.dest_popup_id  = "CurrentDestinationPopup";
	
	
	
	this.toggleDestinations = function(to_open)
	{
		if(to_open)
		{	document.getElementById(this.dest_popup_id).style.display = "inline";
			document.getElementById(this.dates_popup_id).style.display = "none";
		}
		else
		{	document.getElementById(this.dest_popup_id).style.display = "none";
		}
	}
	
	this.toggleDates = function(to_open)
	{	
		if(to_open)
		{	document.getElementById(this.dates_popup_id).style.display = "inline";
			document.getElementById(this.dest_popup_id).style.display = "none";
		}
		else
		{	document.getElementById(this.dates_popup_id).style.display = "none";
		}
	}
	
	this.setStartDate = function()
	{	
		/*document.getElementById("pickDatesStep1").className = "stepNotChosen";
		document.getElementById("pickDatesStep2").className = "stepChosen";
		
		document.getElementById("pickLocationDatesStep1").checked = false;
		document.getElementById("pickLocationDatesStep2").checked = true;
	
		this.on_start = false;*/
	}
	
	this.setEndDate = function()
	{	
		/*document.getElementById("pickDatesStep1").className = "stepChosen";
		document.getElementById("pickDatesStep2").className = "stepNotChosen";
		
		document.getElementById("pickLocationDatesStep1").checked = true;
		document.getElementById("pickLocationDatesStep2").checked = false;
			
		this.on_start = true;*/
	}
	
	this.onStart = function(on_start)
	{	
		if(!on_start)
		{	this.setStartDate();
		}
		else
		{	this.setEndDate();
		}
	}
	
	this.clearEndDate = function()
	{	
		if(document.getElementById(this.end_date_id))
		{	document.getElementById(this.end_date_id).className = "dayBlock";
		}
				
		document.getElementById("end_date_disp").innerHTML  = "";
		
		this.end_date_id = "";
		
		this.end_date = "";
	}
	
	this.checkEndDate = function()
	{
		if(this.selected_end['year'] < this.selected_start['year'])
		{	this.clearEndDate();
		}
		else if(this.selected_end['year'] == this.selected_start['year'] && 
				this.selected_end['month'] == this.selected_start['month'] && 
				this.selected_end['day'] <= this.selected_start['day'])
		{	
			this.clearEndDate();			
		}
		
		if(document.getElementById("pickLocationDayEnd_" + this.curr_end_date['month'] + "_1"))
		{	
			month = this.curr_end_date['month'];
			
			for(i = 1;i <= this.selected_start['day'];i++)
			{	
				if(!document.getElementById("pickLocationDayEnd_" + month + "_" + i))
				{	break;
				}
				
				document.getElementById("pickLocationDayEnd_" + month + "_" + i).className = "dayBlockInactive";
			}
			
			while(document.getElementById("pickLocationDayEnd_" + month + "_" + i))
			{	
				if("pickLocationDayEnd_" + month + "_" + i == this.end_date_id)
				{	i++;
					continue;
				}
			
				document.getElementById("pickLocationDayEnd_" + month + "_" + i).className = "dayBlock";
				i++;
			}
		}
	}
	
	this.selectStartDate = function(year, month, day)
	{
		if(document.getElementById(this.start_date_id))
		{	document.getElementById(this.start_date_id).className = "dayBlock";
		}
		
		this.selected_start['month'] = month;
		this.selected_start['year']  = year;
		this.selected_start['day']   = day;
		
		this.start_date_id = "pickLocationDayStart_" + month + "_" + day;
		
		document.getElementById(this.start_date_id).className = "dayBlockSelected";
		
		date = new Date();
		date.setDate(day);
		date.setMonth(month);
		date.setFullYear(year);
						
		document.getElementById("start_date_disp").innerHTML = 
			this.day_titles[date.getDay()] + " " + day + " " + this.month_names[month];
			
		this.start_date = this.day_titles_EN[date.getDay()] + " " + day + " " + this.month_names_EN[month];
						
		this.checkEndDate();
				
		//this.setStartDate();
	}
	
	this.selectEndDate = function(year, month, day)
	{
		if(document.getElementById(this.end_date_id))
		{	document.getElementById(this.end_date_id).className = "dayBlock";
		}
		
		this.selected_end['month'] = month;
		this.selected_end['year']  = year;
		this.selected_end['day']   = day;
		
		this.end_date_id = "pickLocationDayEnd_" + month + "_" + day;
	
		document.getElementById(this.end_date_id).className = "dayBlockSelected";
				
		date = new Date();
		date.setDate(day);
		date.setMonth(month);
		date.setFullYear(year);
				
		document.getElementById("end_date_disp").innerHTML = 
			this.day_titles[date.getDay()] + " " + day + " " + this.month_names[month];
		
		this.end_date = this.day_titles_EN[date.getDay()] + " " + day + " " + this.month_names_EN[month];
		
		this.checkEndDate();
		
		//this.setEndDate();
	}
		
	this.init = function(lang)
	{		
		this.lang = (lang) ? lang : "EN";
	
		if(this.lang == "EN")
		{	this.month_names = this.month_names_EN;
			this.day_titles  = this.day_titles_EN;
		}
		else if(this.lang == "ES")
		{	this.month_names = this.month_names_ES;
			this.day_titles  = this.day_titles_ES;
		}
		else if(this.lang == "FR")
		{	this.month_names = this.month_names_FR;
			this.day_titles  = this.day_titles_FR;
		}
		else if(this.lang == "DE")
		{	this.month_names = this.month_names_DE;
			this.day_titles  = this.day_titles_DE;
		}
		
		today = new Date();
	
		this.curr_start_date = new Array();
	
		// set to one year in future
		this.curr_start_date['year']  = today.getFullYear();
		this.curr_start_date['month'] = today.getMonth();
		this.curr_start_date['day']   = today.getDate();
			
		this.bkupDate(false);
		
		this.getCalendarStart(this.curr_start_date['month'], this.curr_start_date['year']);
		
		this.curr_end_date = new Array();
	
		// set to one year in future
		this.curr_end_date['year']  = today.getFullYear();
		this.curr_end_date['month'] = today.getMonth();
		this.curr_end_date['day']   = today.getDate();
			
		// set date to init end date
		this.getCalendarEnd(this.curr_end_date['month'], this.curr_end_date['year']);
	}
	
	this.bkupDate = function(forward)
	{	
		this.bkup_date = forward;
	}
	
	this.revertDate = function()
	{			
		today = new Date();
		
		// you tried to go too far into the future
		if(this.bkup_date)
		{	this.curr_start_date = new Array();
	
			// set to one year in future
			this.curr_start_date['year']  = today.getFullYear();
			this.curr_start_date['month'] = today.getMonth();
			this.curr_start_date['day']   = today.getDate();
									
			this.getCalendarStart(this.curr_start_date['month'], this.curr_start_date['year']);
			
			this.curr_end_date = new Array();
	
			// set to one year in future
			this.curr_end_date['year']  = today.getFullYear();
			this.curr_end_date['month'] = today.getMonth();
			this.curr_end_date['day']   = today.getDate();
			
			this.getCalendarEnd(this.curr_end_date['month'], this.curr_end_date['year']);
		}
		else
		{	//this.init();
		}
	}
	
	this.incMonth_start = function()
	{			
		this.curr_start_date['month']++;
		
		if(this.curr_start_date['month'] >= 12)
		{	this.curr_start_date['year']++;
			this.curr_start_date['month'] = 0;
		}
			
		this.getCalendarStart(this.curr_start_date['month'], this.curr_start_date['year']);
		
		if(this.curr_start_date['year'] < this.curr_end_date['year'] || 
				(this.curr_start_date['year'] == this.curr_end_date['year'] && 
				this.curr_start_date['month'] > this.curr_end_date['month']))
		{	
			this.incMonth_end();
		}
	}
	
	this.incMonth_end = function()
	{			
		this.curr_end_date['month']++;
		
		if(this.curr_end_date['month'] >= 12)
		{	this.curr_end_date['year']++;
			this.curr_end_date['month'] = 0;
		}
		
		this.getCalendarEnd(this.curr_end_date['month'], this.curr_end_date['year']);
	}
	
	this.decMonth_start = function()
	{
		this.curr_start_date['month']--;
		
		if(this.curr_start_date['month'] < 1)
		{	this.curr_start_date['year']--;
			this.curr_start_date['month'] = 11;
		}
		
		this.getCalendarStart(this.curr_start_date['month'], this.curr_start_date['year']);
	}
	
	this.decMonth_end = function()
	{		
		if(this.curr_start_date['year'] >= this.curr_end_date['year'] && 
				this.curr_start_date['month'] >= this.curr_end_date['month'])
		{	return;
		}
	
		this.curr_end_date['month']--;
		
		if(this.curr_end_date['month'] < 1)
		{	this.curr_end_date['year']--;
			this.curr_end_date['month'] = 11;
		}
			
		this.getCalendarEnd(this.curr_end_date['month'], this.curr_end_date['year']);		
	}
	
	this.validMonth = function(month, year)
	{
		today = new Date();
		
		if(year == today.getFullYear())
		{	
			if(month < today.getMonth())
			{	return false;
			}
		}
		else if(year < today.getFullYear())
		{	return false;
		}
		else if(year > today.getFullYear() + 1)
		{	return false;
		}
		else if(year == today.getFullYear() + 1)
		{	
			if(month > today.getMonth())
			{	return false;
			}
		}		
		
		return true;
	}
	
	this.getCalendarStart = function(month, year)
	{		
		if(!document.getElementById("pickLocationCalendarStart") || !this.validMonth(month, year))
		{	return;
		}
	
		callback = function()
		{
			if(xmlHttp.readyState == 4 && xmlHttp.responseText != "failure")
			{					
				output = eval("(" + xmlHttp.responseText + ")");
							
				document.getElementById("pickLocationCalendarStart").innerHTML = output.calendar;
											
				if(document.getElementById(jsPickLocation.start_date_id))
				{	document.getElementById(jsPickLocation.start_date_id).className = "dayBlockSelected";
				}
			}
			else if(xmlHttp.readyState == 4)
			{	//jsPickLocation.revertDate();
			}
		}
		
		month++;
		
		params = "month=" + escape(month) + "&year=" + escape(year) + "&curr_cal=start";
		
		ajaxManager.ajaxGet(callback, this.get_calendar_url, params);
	}	
	
	this.getCalendarEnd = function(month, year)
	{		
		if(!document.getElementById("pickLocationCalendarEnd") || !this.validMonth(month, year))
		{	return;
		}
	
		callback = function()
		{
			if(xmlHttp2.readyState == 4 && xmlHttp2.responseText != "failure")
			{			
				output = eval("(" + xmlHttp2.responseText + ")");
				
				document.getElementById("pickLocationCalendarEnd").innerHTML = output.calendar;	
												
				if(document.getElementById(jsPickLocation.end_date_id))
				{	document.getElementById(jsPickLocation.end_date_id).className = "dayBlockSelected";
				}
			}
			else if(xmlHttp2.readyState == 4)
			{	//jsPickLocation.revertDate();
			}
		}
		
		if(month != "init")
		{	month++;
		}
		
		params = "month=" + escape(month) + "&year=" + escape(year) + "&curr_cal=end";
		
		ajaxManager.ajaxGet2(callback, this.get_calendar_url, params);
	}	
		
	this.setOnSuccess = function(on_success)
	{	this.on_success = on_success;		
	}
	
	this.setLocation = function(location_given)
	{	
		location_param = "";
		
		if(location_given == "0")
		{	return;
		}
		
		if(location_given && location_given.length > 0)
		{	location_param = "locationID=" + location_given;
		}
		else if(document.getElementById("locationID") && document.getElementById("locationID").value.length > 0)
		{	location_param = "locationID=" + document.getElementById("locationID").value;
		}
		else if(document.getElementById("location_text"))
		{	location_param = "location=" + escape( document.getElementById("location_text").value );
		}
									
		// The ___p var needs to be something.  If it wasn't there the pickLocation ajax wouldn't
		// do anything.  
		// <THAD>
		params = location_param + "&___p=true";
		
		callback = function()
		{	
			// if done and success
			if(xmlHttp.readyState == 4 && xmlHttp.responseText == "success")
			{	
				document.location = jsPickLocation.location_on_success;
			}
			// if done and failure
			else if(xmlHttp.readyState == 4)
			{												
				errors = eval("(" + xmlHttp.responseText + ")");
				
				if(errors.dropdown != "")
				{	document.getElementById("pickLocation_LocationError").innerHTML  = "<br />\n" + 
						errors.dropdown;
				}
				else
				{	document.getElementById("pickLocation_LocationError").innerHTML  = errors.location;
				}
			}
		}
	
		ajaxManager.ajaxGet(callback, this.location_script_url, params);
	}
		
	this.setDates = function()
	{	
		if(this.start_date.length == 0 && this.end_date.length == 0)
		{	this.closeWindow();
			
			return;
		}
	
		startDate = escape( this.start_date + " " + this.selected_start['year'] );
		endDate   = escape( this.end_date   + " " + this.selected_end['year']);
	
		// The ___p var needs to be something.  If it wasn't there the pickLocation ajax wouldn't
		// do anything.  
		// <THAD>
		params = "startDate=" + startDate + "&endDate=" + endDate + "&___p=true";
	
		callback = function()
		{	
			// if done and success
			if(xmlHttp.readyState == 4 && xmlHttp.responseText == "success")
			{	document.location = jsPickLocation.location_on_success;		
			}
			// if done and failure
			else if(xmlHttp.readyState == 4)
			{	document.getElementById("pickLocationDates_Error").innerHTML = xmlHttp.responseText;
			}
		}
	
		ajaxManager.ajaxGet(callback, this.date_script_url, params);
	}
	
	this.setLocationAndDates = function(location, locationID, start_date, end_date)
	{
		this.setDates();		
		this.setLocation();
	}
	
	
	
}

jsPickLocation = new PickLocation();

function processKey(e)
{
	if (null == e)
	e = window.event ;
	if (e.keyCode == 13)  {
		document.getElementById("submit").click();
		return false;
	}
}

function clickDestination(value) {
	if( value == document.getElementById('valueHolder').value )
		document.getElementById('location_text').value = '';
}

function blurDestination(value) {
	if( value == "" )
		document.getElementById('location_text').value = document.getElementById('valueHolder').value;
}

