$WELCOME = "";

function init() {

	//is called onLoad of the body
	
	//retrieve locations
	getLocations();
	
	getJSTemplates();

	//after the corners of our buttons are round, we can move the template stuff into the textarea 
	$('personal_details').value = $('personal_details_').innerHTML;
	$('personal_details_').innerHTML = "";
	
	//and again...
	$('week').value = $('week_').innerHTML;
	$('week_').innerHTML = "";
	
	//and yet again...
	$('browse_programs').value = $('browse_programs_').innerHTML;
	$('browse_programs_').innerHTML = "";
	
	//and another one!
	$('contact').value = $('contact_').innerHTML;
	$('contact_').innerHTML = "";

	$WELCOME = $('tab_content').innerHTML;
	
	setInterval("saveLog()", 15000);
}

function doStatic(file) {
	if(file != "") {
		
		loading();
		
		new Ajax.Request('/ch/static/'+file,
		{
			method:'get',
			onSuccess: function(transport) {

				var response = transport.responseText || "empty";
			
				$('tab_content').innerHTML = response;
				
				resetTabs();
				
				$('tab_content').style.overflow = 'auto';
				
				finished();
			},
			onFailure: function(){
				finished();
			}
		});
	}
}

function saveLog() {
	
	if($('logger').value != "") {
		new Ajax.Request('/ch/php/saveLog.php',
		{
			method:'post',
			onSuccess: function(transport) {

				var response = transport.responseText || "empty";
			
				$('logger').value = "";
			
				//alert(response)
			},
			onFailure: function(){
			
			},
			parameters: {log: $('logger').value, clientID: $CLIENTID}
		});
	}
}

function log(str) {
	$('logger').value += currentTime()+" "+str+"<br/>";
}

function currentTime() {
	
	Stamp = new Date();
	
	if((Stamp.getMonth()+1) < 10) {
		Month = "0"+(Stamp.getMonth()+1);
	} else {
		Month = (Stamp.getMonth()+1);
	}
	
	if((Stamp.getDate()) < 10) {
		Datee = "0"+(Stamp.getDate());
	} else {
		Datee = Stamp.getDate();
	}
	
	Hours = Stamp.getHours();
	Min = Stamp.getMinutes();
	
	return Month+ "-"+Datee+"_"+Hours+":"+Min;
}

function doWelcome() {
	
	$('tab_content').style.overflow = 'hidden';
		
	$('tab_content').innerHTML = $WELCOME;
}


function doContact() {
		
	$('tab_content').innerHTML = "";
	
	var result = TrimPath.processDOMTemplate("contact");
	
	$('tab_content').innerHTML = result;
}

function getApp(id) {
	
	loading();
	
	new Ajax.Request('/ch/php/getParticipant.php',
	{
		method:'post',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";
			
			response = eval('(' + response + ')');
			
			$ARRAY = new Array();
			$ARRAY['options'] = response;

			var result = TrimPath.processDOMTemplate("part_dataa", $ARRAY);
			$('part_data').innerHTML = result;
			
			finished();
			
		},
		onFailure: function(){
			alert('There was a problem getting the application data.');
			
			finished();
		},
		parameters: {clientID: $CLIENTID, partID: id}
	});
}

function Personals() {
	
	this.family_name;
	this.surname;
	this.dob;
	
	this.dob_year;
	this.dob_month;
	this.dob_day;
	
	this.gender;
	this.nationality;
	this.address1;
	this.address2;
	this.home_phone;
	this.mobile_phone;
	this.email;
	this.emergency_name;
	this.emergency_telephone;
	this.startingdate;
	
	this.total_price;
	
	this.share;
}

$PERSONALS = new Personals();

function checkPersonal() {
	
	if($('share').checked) {
		$PERSONALS.share = 1;
	} else {
		$PERSONALS.share = 0;
	}
		
	if($('family_name').value == "") {
		alert('Please enter your family name.');
		log("App form error: no or wrong family name");
		return false;
	} else if($('surname').value == "") {
		alert('Please enter your given name(s).');
		log("App form error: no or wrong sur name");
		return false;
	} else if($('dob_year').value == "" || $('dob_year').value == 'yyyy') {
		alert('Please enter a date of birth.');
		log("App form error: no or wrong date of birth");
		return false;
	} else if($('dob_month').value == '' || $('dob_month').value == 'mm') {
		alert('Please enter a date of birth.');
		log("App form error: no or wrong date of birth");
		return false;
	} else if($('dob_day').value == '' || $('dob_day').value == 'dd') {
		alert('Please enter a date of birth.');
		log("App form error: no or wrong date of birth");
		return false;
	} else if($('nationality').value == '') {
		alert('Please enter a nationality.');
		log("App form error: no or wrong nationality");
		return false;
	} else if($('address1').value == '') {
		alert('Please enter a address.');
		log("App form error: no or wrong address1");
		return false;
	} else if($('mobile_phone').value == '') {
		alert('Please enter a mobile phone number.');
		log("App form error: no or wrong mobile phone number");
		return false;
	} else if($('email').value == '') {
		alert('Please enter an email address.');
		log("App form error: no or wrong email address");
		return false;
	} else if($('emergency_name').value == '') {
		alert('Please enter an emergency contact.');
		log("App form error: no or wrong emergency contact");
		return false;
	} else if($('emergency_telephone').value == '') {
		alert('Please enter an emergency contact.');
		log("App form error: no or wrong emergency phone");
		return false;
	}

		
	$PERSONALS.family_name = $('family_name').value;
	$PERSONALS.surname = $('surname').value;
	$PERSONALS.dob = $('dob_year').value+"-"+$('dob_month').value+"-"+$('dob_day').value;
	$PERSONALS.gender = $('gender').value;
	$PERSONALS.nationality = $('nationality').value;
	$PERSONALS.address1 = $('address1').value;
	$PERSONALS.address2 = $('address2').value;
	$PERSONALS.home_phone = $('home_phone').value;
	$PERSONALS.mobile_phone = $('mobile_phone').value;
	$PERSONALS.email  = $('email').value;
	$PERSONALS.emergency_name = $('emergency_name').value;
	$PERSONALS.emergency_telephone = $('emergency_telephone').value;
	
	createProgram();
	log('The application form was filled out correctly.')
}

function loading() {
	$('loader').style.visibility = 'visible';
}

function finished() {
	$('loader').style.visibility = 'hidden';
}

function doPersonal() {
	//$('tab_content').innerHTML = $('personal_details').value; 
	
	//parse the personal details template
	var result = TrimPath.processDOMTemplate("personal_details");
	
	$('tab_content').innerHTML = result;
	
	if($PERSONALS.family_name)
		$('family_name').value = $PERSONALS.family_name;
		
	if($PERSONALS.surname)
		$('surname').value = $PERSONALS.surname;
		
	if($PERSONALS.gender)
		$('gender').value = $PERSONALS.gender;
		
	if($PERSONALS.dob_year)
		$('dob_year').value = $PERSONALS.dob_year;
	
	if($PERSONALS.dob_month)
		$('dob_month').value = $PERSONALS.dob_month;
		
	if($PERSONALS.dob_day)
		$('dob_day').value = $PERSONALS.dob_day;
		
	if($PERSONALS.nationality)
		$('nationality').value = $PERSONALS.nationality;
		
	if($PERSONALS.passport_nr)
		$('passport_nr').value = $PERSONALS.passport_nr;
	
	if($PERSONALS.address1)
		$('address1').value = $PERSONALS.address1;
		
	if($PERSONALS.address2)
		$('address2').value = $PERSONALS.address2;
		
	if($PERSONALS.home_phone)
		$('home_phone').value = $PERSONALS.home_phone;
		
	if($PERSONALS.mobile_phone)
		$('mobile_phone').value = $PERSONALS.mobile_phone;
		
	if($PERSONALS.email)
		$('email').value = $PERSONALS.email;
		
	if($PERSONALS.emergency_name)
		$('emergency_name').value = $PERSONALS.emergency_name;
		
	if($PERSONALS.emergency_telephone)
		$('emergency_telephone').value = $PERSONALS.emergency_telephone;

}

function doOP() {
		
	$('tab_content').innerHTML = "";
	
	loading();
	
	//get the list of participants for this agent for the coming year
	new Ajax.Request('/ch/php/getParticipants.php',
	{
		method:'post',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";

			//alert(response);
			
			apps = eval('(' + response + ')');
			
			$ARRAY = new Array();
			$ARRAY['apps'] = apps;
			$ARRAY['selected'] = "";
			$ARRAY['classs'] = 'option_dropdown';
			$ARRAY['id'] = 'locations';
			$ARRAY['onChange'] = 'setLocation(this.value); getAllOptionsForCountry()';
			var result = TrimPath.processDOMTemplate("mattijs", $ARRAY);
			$('tab_content').innerHTML = result;
			
			finished();
		},
		onFailure: function(){
			alert('There was a problem booking your program.');
			
			finished();
		},
		parameters: {clientID: $CLIENTID}
	});
}

function doFAQ() {
	$('tab_content').style.overflow = 'auto';
	$('tab_content').innerHTML = "";
		
	$('tab_content').innerHTML = $('faq').value;
	
	//get the list of participants for this agent for the coming year
	/*new Ajax.Request('/ch/php/getFAQ.php',
	{
		method:'post',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";
			
			
			
			finished();
		},
		onFailure: function(){
			alert('There was a problem booking your program.');
			
			finished();
		},
		parameters: {clientID: $CLIENTID}
	});*/
}

function faq(id) {
		
	if($(id).style.display == 'none' || $(id).style.display == "") {
		$(id).style.display = 'block';
	} else {
		$(id).style.display = 'none';
	}
}

function bookProgram() {
	
	loading();
	
	var JSONText = JSON.stringify($WEEKS_ARRAY);
	
	if(($STARTINGDATE.getMonth()+1) < 10) {
		month = "0"+($STARTINGDATE.getMonth()+1);
	} else {
		month = $STARTINGDATE.getMonth()+1;
	}
	
	startingdate = $STARTINGDATE.getFullYear()+"-"+month+"-"+$STARTINGDATE.getDate();
	$PERSONALS.startingdate = startingdate
	
	var personals = JSON.stringify($PERSONALS);
		
	new Ajax.Request('/ch/php/bookProgram.php',
	{
		method:'post',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";

			if(response == 1) {
				$('tab_content').innerHTML = "Your booking has been received and will now be processed. You will receive a confirmation email within 12 hours as well as an email containing payment instructions.";
			}
			
			finished();
			
		},
		onFailure: function(){
			alert('There was a problem booking your program.');
			
			finished();
		},
		parameters: {clientID: $CLIENTID, booking: JSONText, personals: personals}
	});
}

function comleteProgram() {
	alert('')
	//var result = TrimPath.processDOMTemplate("complete_program");
	
	//$('tab_content').innerHTML = result;
}

function getAllOptionsForCountry() {
	
	loading();
	
	$LOCATION = $('locations').value;
	$STARTINGDATE = '';
		
	new Ajax.Request('php/getMon-fri.php',
	{
		method:'post',
		onSuccess: function(transport) {
			
			var response = transport.responseText || "empty";
			$MON_FRI_OPTIONS = eval('(' + response + ')');
			
			$ARRAY = new Array();
			$ARRAY['options'] = $MON_FRI_OPTIONS;
			$ARRAY['classs'] = 'option_dropdown';
			$ARRAY['id'] = 'mon_fri';
			$ARRAY['onChange'] = "getModule(this.value, 'm')";
			$ARRAY['selected'] = "";
			var result = TrimPath.processDOMTemplate("options", $ARRAY);
			$('mon_fri_').innerHTML = result
		},
		onFailure: function(){
			alert('There was a problem loading the mon-fri options.')
		},
		parameters: {clientID: $CLIENTID, locationID: $LOCATION}
	});
	
	new Ajax.Request('php/getSat-sun.php',
	{
		method:'post',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";

			$SAT_SUN_OPTIONS = eval('(' + response + ')');

			$ARRAY = new Array();

			$ARRAY['options'] = $SAT_SUN_OPTIONS;
			$ARRAY['classs'] = 'option_dropdown';
			$ARRAY['id'] = 'sat_sun';
			$ARRAY['onChange'] = "getModule(this.value, 's')";
			$ARRAY['selected'] = "";
			var result = TrimPath.processDOMTemplate("options", $ARRAY);
			$('sat_sun_').innerHTML = result;
			
			finished();

		},
		onFailure: function(){
			alert('There was a problem loading the sat-sun options.');
			
			finished();
		},
		parameters: {clientID: $CLIENTID, locationID: $LOCATION}
	});
}

function getModule(id, type) {
	
	//get the link to the program description

	if(type == 'm' && id != "" && id != "x") {
		
		log("Option "+ id +" was selected for Mon-Fri");

		new Ajax.Request('php/getModule.php',
		{
			method:'post',
			onSuccess: function(transport) {

				var response = transport.responseText || "empty";
				
				$('loading_mon_fri').style.visibility = 'visible';

				$('descr_mon_fri').src = response;
				
				$WEEKS_ARRAY[$WEEK_CURRENT].mon_fri_link = response;
				
			},
			onFailure: function(){
				alert('There was a problem loading the program description.')
			},
			parameters: {id: id, type: type, clientID: $CLIENTID}
		});
		
	} else if (type == 's' && id != "" && id != "x") {
		
		log('Option '+id+' was selected for Sat-Sun');
		
		new Ajax.Request('php/getModule.php',
		{
			method:'post',
			onSuccess: function(transport) {

				var response = transport.responseText || "empty";

				$('loading_sat_sun').style.visibility = 'visible';

				$('descr_sat_sun').src = response;
				
				$WEEKS_ARRAY[$WEEK_CURRENT].sat_sun_link = response;

			},
			onFailure: function(){
				alert('There was a problem loading the program description.')
			},
			parameters: {id: id, type: type, clientID: $CLIENTID}
		});
		
	}
}

function popOut(link) {
	
	window.open(link,'Culture House programs')
	
}

function getJSTemplates() {
	
	//download the jstemplates from the server
	new Ajax.Request('/ch/html/jstemplates.html',
	{
		method:'get',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";

			$('jstemplates_container').innerHTML = response;
		},
		onFailure: function(){
			alert('There was a problem loading the jstemplates.')
		}
	});
}

//storage for the locations
$LOCATIONS = "";

//storage for mon - fri options
$MON_FRI_OPTIONS = "";

function getLocations() {
	
	new Ajax.Request('/ch/php/getLocations.php',
	{
		method:'post',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";

			$LOCATIONS = eval('(' + response + ')');
			
		},
		onFailure: function(){
			alert('There was a problem loading the locations.')
		},
		parameters: {clientID: $CLIENTID}
	});
}


//track the total number of weeks
$WEEKS = 0;

//track the current week/selected week
$WEEK_CURRENT = 0;

//array for all the week data
$WEEKS_ARRAY = new Array();

function doTabSwitch(element) {
	//is called when a tab is clicked and only takes care of the graphical part
	//of switching tabs

	var mat = document.getElementsByClassName('tab');
			
	for(x=0; x < mat.length; x++) {
		mat[x].className = "tab";
	}
		
	var matt = document.getElementsByClassName('selected');
		
	for(x=0; x < matt.length; x++) {
		matt[x].className = "tab";
	}	
		
		
	element.className = "selected";

}

function resetTabs() {
	//reset all tabs
	var mat = document.getElementsByClassName('tab');

	for(x=0; x < mat.length; x++) {
		mat[x].className = "tab";
	}

	var matt = document.getElementsByClassName('selected');

	for(x=0; x < matt.length; x++) {
		matt[x].className = "tab";
	}
}

//Week object
function Week() {

	this.id;//week id, usually the week number
		
	this.location;//location
	
	this.startingdate;

	this.mon_fri;//option for MON - FRI
	this.sat_snu;//option for SAT - SUN
	
	this.mon_fri_type;//either short or long
	
	this.mon_fri_link;
	this.sat_sun_link;
	
	this.total_price;//price of the created program
}

$TOTAL_COST = new Array();

function setMonFri(id) {
	
	//store the selected option for mon - fri in the WEEKS_ARRAY for this current selected week
	$WEEKS_ARRAY[$WEEK_CURRENT].mon_fri = id;
	
	log('Option '+id+" has been selected for Mon-Fri in week "+$WEEK_CURRENT);
	
	new Ajax.Request('/ch/php/getType.php',
	{
		method:'post',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";
			
			if(response == 'long') {//disable the weekend option for this week
				//$('sat_sun_').innerHTML = 'no sat-sun options available'
				
				$WEEKS_ARRAY[$WEEK_CURRENT].mon_fri_type = 'long';
				
				$('sat_sun_').innerHTML = "no SAT-SUN options available";
				
				$('descr_sat_sun').style.display = 'none';
				$('pop_out_SatSun').style.display = 'none';
				
				$('descr_mon_fri').style.height = "460px";
			} else if (response == 'short') {
				
				$('descr_sat_sun').style.display = 'block';
				$('pop_out_SatSun').style.display = 'block';
				
				$('descr_mon_fri').style.height = "220px";
				
				//now we can get the mon-fri options and sat-sun options
				new Ajax.Request('php/getSat-sun.php',
				{
					method:'post',
					onSuccess: function(transport) {

						var response = transport.responseText || "empty";

						$SAT_SUN_OPTIONS = eval('(' + response + ')');

						$ARRAY = new Array();

						$ARRAY['options'] = $SAT_SUN_OPTIONS;
						$ARRAY['classs'] = 'option_dropdown';
						$ARRAY['id'] = 'sat_sun';
						$ARRAY['onChange'] = "setSatSun(this.value); getModule(this.value, 's')";
						$ARRAY['selected'] = "";
						var result = TrimPath.processDOMTemplate("options", $ARRAY);
						$('sat_sun_').innerHTML = result;

						finished();

					},
					onFailure: function(){
						alert('There was a problem loading the sat-sun options.');

						finished();
					},
					parameters: {clientID: $CLIENTID, locationID: $WEEKS_ARRAY[$WEEK_CURRENT].location, startdate: $WEEKS_ARRAY[$WEEK_CURRENT].startingdate}
				});
				
				$WEEKS_ARRAY[$WEEK_CURRENT].mon_fri_type = 'short';
				
			}
			
		},
		onFailure: function(){
			alert('There was a problem checking the status on this experience.')
		},
		parameters: {clientID: $CLIENTID, id: id}
	});
	
	new Ajax.Request('/ch/php/getPrice.php',
	{
		method:'post',
		onSuccess: function(transport) {
			
			var response = transport.responseText || "empty";

			t = eval('(' + response + ')');
			
			temp = new Number(t[1]);

			if(!$TOTAL_COST[$WEEK_CURRENT]) {
				$TOTAL_COST[$WEEK_CURRENT] = new Array();
			}
			
			$TOTAL_COST[$WEEK_CURRENT]['mon_fri'] = temp;
			
			if(!$TOTAL_COST[$WEEK_CURRENT]['sat_sun']) {
				$TOTAL_COST[$WEEK_CURRENT]['sat_sun'] = 0;
			}
						
			//get total cost
			
			tcc = 0;

			for(x=0; x<=$TOTAL_COST.length; x++) {
				
				if($TOTAL_COST[x]) {
					tcc += $TOTAL_COST[x]['mon_fri'];
					tcc += $TOTAL_COST[x]['sat_sun'];
				}
			}
						
			$('tc').innerHTML = tcc;
			
			$PERSONALS.total_price = tcc;
			
			if(tcc > 0) {
				
				if(t[2] == 'EU') {
					$('currency').innerHTML = "&#8364;";
				} else if (t[2] == 'US') {
					$('currency').innerHTML = "&#36;";
				}
				
				$('programCost').style.visibility = 'visible';
			}
			
		},
		onFailure: function(){
			alert('There was a problem retrieving the price of this module.')
		},
		parameters: {clientID: $CLIENTID, id: id, type: 'm'}
	});
}

function setSatSun(id) {
	//store the selected option for sat - sun in the WEEKS_ARRAY for this current selected week
	$WEEKS_ARRAY[$WEEK_CURRENT].sat_sun = id;
	
	log('Option '+id+" has been selected for Sat-Sun in week "+$WEEK_CURRENT);
	
	new Ajax.Request('/ch/php/getPrice.php',
	{
		method:'post',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";
			
			t = eval('(' + response + ')');
			
			temp = new Number(t[1]);

			if(!$TOTAL_COST[$WEEK_CURRENT]) {
				$TOTAL_COST[$WEEK_CURRENT] = new Array();
			}
			
			$TOTAL_COST[$WEEK_CURRENT]['sat_sun'] = temp;
			
			if(!$TOTAL_COST[$WEEK_CURRENT]['mon_fri']) {
				$TOTAL_COST[$WEEK_CURRENT]['mon_fri'] = 0;
			}
						
			//get total cost
			tcc = 0;
						
			for(x=0; x<=$TOTAL_COST.length; x++) {
				
				if($TOTAL_COST[x]) {
					tcc += $TOTAL_COST[x]['mon_fri'];
					tcc += $TOTAL_COST[x]['sat_sun'];
				}
			}
			
			$('tc').innerHTML = tcc;
			
			$PERSONALS.total_price = tcc;
						
			if(tcc > 0) {
				
				if(t[2] == 'EU') {
					$('currency').innerHTML = "&#8364;";
				} else if (t[2] == 'US') {
					$('currency').innerHTML = "&#36;";
				}
				
				$('programCost').style.visibility = 'visible';
			}
			
		},
		onFailure: function(){
			alert('There was a problem retrieving the price of this module.')
		},
		parameters: {clientID: $CLIENTID, id: id, type: 's'}
	});
}

function setLocation(id) {
	
	log('Location: '+id+" has been selected");
	
	if($WEEKS_ARRAY[$WEEK_CURRENT]) {
		$WEEKS_ARRAY[$WEEK_CURRENT].location = id;
	}
	
	//reset the mon - fri and sat - sun options
	$('mon_fri_').innerHTML = "";
	$('sat_sun_').innerHTML = "";
}


function doCompleteProgram() {
	
	log('The button Complete program was clicked');
	
	//get some usable data
	var JSONText = JSON.stringify($WEEKS_ARRAY);
	
	loading();

	new Ajax.Request('php/getProgram.php',
	{
		method:'post',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";

			options = eval('(' + response + ')');

			var result = TrimPath.processDOMTemplate("cp", options);

			$('tab_content').innerHTML = result;
			
			finished();
			
			$('test').innerHTML = $('complete_program').innerHTML

		},
		onFailure: function(){
			alert('There was a problem loading your program.');
			
			finished();
		},
		parameters: {clientID: $CLIENTID, program_array: JSONText}
	});
}

function doTerms() {
	
	loading();
	
	new Ajax.Request('php/getTerms.php',
	{
		method:'post',
		onSuccess: function(transport) {

			var response = transport.responseText || "empty";

			$('tab_content').innerHTML = response;
			
			finished();
			
		},
		onFailure: function(){
			alert('There was a problem loading your program.');
			
			finished();
		},
		parameters: {clientID: $CLIENTID}
	});
}

//used to keep track of an open/closed calendar
$CALENDAR_OPEN = 0;

function createCal(date) {
	//first check if a location has been selected
	//is called onLoad to get all available locations
	if($('locations').value == '') {
		alert('Please select a country first.')
		return false;
	}
	
	if($CALENDAR_OPEN == 0) {
		Calendar.setup(
			{
				flat         : "calendar-container", // ID of the parent element
		    	flatCallback : setDate           // our callback function
			}
		);
	}
	$CALENDAR_OPEN = 1;
	
	//hide the highest popout button
	popouts = document.getElementsByClassName('popout');
	
	popouts[0].style.visibility = 'hidden';
	
	//and hide the "Add week" and "Complete program" button
	buttons = document.getElementsByClassName('button_small');
	
	for(x=0; x<buttons.length; x++) {
		buttons[x].style.visibility = 'hidden'
	}
}

$FIRST_DATE = 0;
$STARTINGDATE = 0;

function setDate(calendar) {
	// Beware that this function is called even if the end-user only
    // changed the month/year.  In order to determine if a date was
    // clicked you can use the dateClicked property of the calendar:
    if (calendar.dateClicked) {
	
		//show the highest popout button
		popouts = document.getElementsByClassName('popout');

		popouts[0].style.visibility = 'visible'
		
		//show the other two buttons
		buttons = document.getElementsByClassName('button_small');

		for(x=0; x<buttons.length; x++) {
			buttons[x].style.visibility = 'visible'
		}
	
		calendar.hide();
    	// OK, a date was clicked, redirect to /yyyy/mm/dd/index.php
      	var y = calendar.date.getFullYear();
      	var m = calendar.date.getMonth()+1;     // integer, 0..11
      	var d = calendar.date.getDate();      // integer, 1..31

		$DATE = calendar.date;
		
		if($FIRST_DATE == 0) {
			
			$STARTINGDATE = $DATE;
			
			$FIRST_DATE = 1;
		} else if($DATE < $STARTINGDATE) {
			
			alert('The selected date is before the startingdate of your first week. Please select a different date.');
			$CALENDAR_OPEN = 0;
			
			$('mon_fri_').innerHTML = "";
			$('sat_sun_').innerHTML = "";
			
			return false;
			
		}

      	var date = y+"/"+m+"/"+d;
		
	  	$WEEKS_ARRAY[$WEEK_CURRENT].startingdate = date;
		
		log(date+" was selected for week "+$WEEK_CURRENT);
		
		$('date').value = date;
		
		$CALENDAR_OPEN = 0;
		
		loading();
		
		//now we can get the mon-fri options and sat-sun options
		new Ajax.Request('php/getMon-fri.php',
		{
			method:'post',
			onSuccess: function(transport) {
				
				var response = transport.responseText || "empty";
				$MON_FRI_OPTIONS = eval('(' + response + ')');

				$ARRAY = new Array();

				$ARRAY['options'] = $MON_FRI_OPTIONS;
				$ARRAY['classs'] = 'option_dropdown';
				$ARRAY['id'] = 'mon_fri';
				$ARRAY['onChange'] = "setMonFri(this.value); getModule(this.value, 'm')";
				$ARRAY['selected'] = "";
				var result = TrimPath.processDOMTemplate("options", $ARRAY);
				
				$('mon_fri_').innerHTML = result;
				
				finished();

			},
			onFailure: function(){
				alert('There was a problem loading the mon-fri options.');
				
				finished();
			},
			parameters: {clientID: $CLIENTID, locationID: $WEEKS_ARRAY[$WEEK_CURRENT].location, startdate: $WEEKS_ARRAY[$WEEK_CURRENT].startingdate}
		});	

		loading();
		
		//put the date in the tab of the corresponding week, only if it's the first week!
		$("t"+$WEEK_CURRENT).innerHTML = "";
		
		put = "Week "+$WEEK_CURRENT+": "+date;
		
		$("t"+$WEEK_CURRENT).innerHTML = put;
    }
};

