// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function postTZoffset(form) {
	var identifier = form.id + '_client_tz_offset';
	form[identifier].value = timeZoneOffset();
	//alert("Form Id is: "+form.id+" Client offset is: "+eval('form.'+form.id+'_client_tz_offset.value'));
	return;
}

function timeZoneOffset() {
	var thisDate = new Date();
	return -((thisDate.getTimezoneOffset())*60);
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

function isDisabled(date) {
  var today = new Date();
	return ((date.getTime() - today.getTime())/DAY ) < -1;
}

function catcalc2(cal) {
    var date = cal.date;
    var time = date.getTime()
    // use the _other_ field
    var field = document.getElementById("f_calcdate");
    if (field == cal.params.inputField) {
        field = document.getElementById("f_date_a");
        time -= Date.WEEK; // substract one week
    } else {
        time += Date.WEEK; // add one week
    }
    var date2 = new Date(time);
    field.value = date2.print("%Y-%m-%d %H:%M");
}

function stringToDate(datetimestring) {
	if (datetimestring == "MM/DD/YYYY HH:MM AM") {
		return "";
	}
   var parts = datetimestring.match(/(\d+)\/(\d+)\/(\d\d\d?\d?) (\d+):(\d\d)\s?(AM|PM)/);
   if(parts) {
	var month = (parseInt(parts[1]) - 1);
	var day = parts[2];
	var year = parts[3];
	var hour = parts[4];
	var minute = parts[5];
	var am_pm = parts[6];
	if(year.length == 2) {
		if(year.indexOf("0") == 0) {
			year = year.substring(1,2);
		}
		year = parseInt(year) + 2000;
	}
		if ((hour < 12) && (am_pm == "PM")) {
			hour = parseInt(hour, 10) + 12; 
		} else if ((hour == 12) && (am_pm == "AM")) {
			hour = 0;
    	}
		var d = new Date();
		d.setYear(year);
		d.setMonth(month);
		d.setDate(day);
		d.setHours(hour);
		d.setMinutes(minute);
		return d;
   } else {
		return new Date();
   }
}

var DATE_TEMPLATE_TEXT = "MM/DD/YYYY HH:MM AM";

function catcalc(this_field_id) {
  var this_field = $(this_field_id);
  var start_field = $('invitation[start_time_local]');
  var end_field = $('invitation[end_time_local]');
  var start_time = stringToDate($F(start_field));
  var end_time = stringToDate($F(end_field));
  if (this_field == start_field) {
    if (( $F(end_field) == DATE_TEMPLATE_TEXT) || (end_time < start_time)) {
      clear_e_g_input(DATE_TEMPLATE_TEXT, end_field);
      end_field.value = $F(start_field);
    }
  } else {
    if (start_time > end_time) {
      alert("The end time must be later than the start time.");
      clear_e_g_input(DATE_TEMPLATE_TEXT, end_field);
      end_field.value = $F(start_field);
    }		
  }
}

function clear_e_g_input(e_g_text, id) {
	 if($F(id) == e_g_text) {
		$(id).value = '';
		$(id).style.color = '#000';
	}   
}

function restore_e_g_input(e_g_text, id) {
	if($F(id) == '') {
		$(id).value = e_g_text;
		$(id).style.color = '#BBB';
	} else { $(id).style.color = '#000'; }
}

function disable_enable_state_field() {
	var state = $('address_state');

	if($F('address_country') == 'United States') {
		if(state.disabled) {
			state.disabled = false;
		}
	} else {
		if(!state.disabled) {
			state.disabled = true;
		}
	}
}

function find_option_index(options, find_value) {
	var result = 0;
	for (var i = 0; i < options.length; i++) {
		if (options[i].value == find_value) { result = i; }
	}
	return result;
}

function select_time_zone(tz_value) {
	var tz = document.getElementById('invitation_time_zone');
	var tz_index = find_option_index(tz.options, tz_value);
	tz.selectedIndex = tz_index;
	check_tz_selected(tz_value);
}

function check_tz_selected(address_tz_value) {
	var tz = document.getElementById('invitation_time_zone');
	var address_tz_index = find_option_index(tz.options, address_tz_value); 
	
	if ( not_same_offset_as_address(tz.options, address_tz_index, tz.selectedIndex )  )  {
		$('tz_notice').show();
		new Effect.Highlight('tz_notice', { duration:3.0 });
	} else {
		$('tz_notice').hide();
	}
}  

function not_same_offset_as_address(options, addressIndex, selectedIndex) {
	
	var address_tz = options[addressIndex].text;
	var selected_tz = options[selectedIndex].text; 
    var parts = address_tz.match(/(GMT.*?)\s+/);
    address_offset = parts[1]          ;
    parts = selected_tz.match(/(GMT.*?)\s+/);
    selected_offset = parts[1]            ;

    return(address_offset != selected_offset);
	
}

function update_table_highlights(table_id) {
  var selector = "#" + table_id + " tbody tr";
  var tableRows = $$(selector);
  tableRows.each(function(element, index) {  
    var className =  ((index % 2 == 0) ? "even" : "odd");
    element.className = className;
  });
}


function highlight_flash() {
  Element.show("flash");
  new Effect.Highlight("flash", { duration:2.0, startcolor:'#C8D6EA', endcolor:'#F0F0E5'});
}

function showAddressType(addr_type) {
  $('regular').hide();
  $('conference').hide();
  $('airport').hide();
  $(addr_type).show();
  if(addr_type == 'regular' || addr_type == 'conference') {
    $('base_address_form').show();
	 $('address_country_div').show();
  } else {
    $('base_address_form').hide();
	 if ($('address_airport_type').value == 'airport_us') {
		$('airport_non_us').hide();
		$('address_country_div').hide();
	 }
  }
}

var WHO_PUBLIC = "1";
var WHO_PRIVATE = "0";



function showWhoType(who_type) {
  if(who_type == WHO_PRIVATE) {
	$('invitees_select').show();
	$('private_invite').show();
    $('invitee_profile_form').hide();
	$('open_invite').hide();
  } else if(who_type == WHO_PUBLIC) {
    $('invitees_select').show();
    $('invitee_profile_form').show();
	$('open_invite').show();
	$('private_invite').hide();
  }
}

function showAirportType(air_type) {
  $('airport_us').hide();
  $('airport_non_us').hide();
  $(air_type).show();
  if(air_type == 'airport_non_us') {
      if( $('address_country_airport').value == null ){
              $('address_country_airport').value = '' ;
      }
    $('airport_country_div').show();
  } else {
    $('address_country_airport').value = 'United States';
    $('airport_country_div').hide();
  }
}

function noenter() { return !(window.event && window.event.keyCode == 13); }

function thursdaysOnly(date) {
	if(date.getDay() == 4){
		return false;
	} else {
		return true;
	}
}


function select_new_date() {
	Element.show('show_cal'); 
	Element.hide('show_date');
}                                

function getCookieValue(name) {
   var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]+)'));
  return  (cookie ? unescape(cookie[2]) : null);
   
}   



function userName(){
	document.write( 
	    getCookieValue('ttb_user_name').gsub("[\+]", " ") );
}  

  
  
  function showCurrentEmails(){
         new Effect.Parallel( [
          new Effect.Fade('save-search-box'),
          new Effect.Appear('current_emails'),
          new Effect.Appear('search_config'),

          new Effect.Morph('search_config', { style: { width:"300px", height: "300px"  }})
          ]);    
  }
  
  function showSaveEmail(){
    new Effect.Parallel( [
          new Effect.Fade('current_emails'),
          new Effect.Appear('save-search-box'),
          new Effect.Appear('search_config'),
          new Effect.Morph('search_config', { style: { width:"300px", height: "125px"  }})
          ]);    
    
  }
  
 function logoutWithFacebook(url){
        logoutURL = url; 
        FB.Connect.ifUserConnected(logoutFacebook, logoutNormal);
 };
    
 function logoutFacebook(){
      FB.Connect.logoutAndRedirect(logoutURL);
  };
  
  function logoutNormal(){
       window.location.href = logoutURL;     
   };
  


