function disableEnterKey() 
{ 
   if (window.event.keyCode == 13) window.event.keyCode = 0; 
}

function handleAssigner() {
	dropdown = $('assigner');
	recipient_text_box = $('recipient');
	if($F('assigner') == 'other') 
	{ recipient_text_box.value = "";}
	else
	{ recipient_text_box.value = users[dropdown.options[dropdown.selectedIndex].value]; }
	if (dropdown.options[dropdown.selectedIndex].text == 'Other')
		{
			 Effect.BlindDown('other_assigner_info', {duration:0.25});
		}
	else {
			Effect.BlindUp('other_assigner_info', {duration:0.25});
		}
		target_checkbox = $('target_' + default_targets[dropdown.options[dropdown.selectedIndex].value]);
			// Clear all
			for (i=0; i<document.photo_request.elements.length; i++)
			{
				if (document.photo_request.elements[i].name == 'for')
					{
						document.photo_request.elements[i].checked = false;
					}
			}
			
		if (target_checkbox)
		{
			//Check desired
			target_checkbox.checked = 'checked'; 
		}
	}
	
function handleOtherTarget() {
	if(Form.Element.getValue('target_other') == "Other")
	{
		Effect.BlindDown('OtherTarget', {duration:0.25});
	}
	else
	{
		Effect.BlindUp('OtherTarget', {duration:0.25});
	}

}

function handleSpecificDateTime(value) {
	
	if (value == 'yes')
		{
			 Effect.BlindDown('shoot_datetime', {duration:0.25});
		}
	else {
			Effect.BlindUp('shoot_datetime', {duration:0.25});
		}
	}



function nextPage(currentPage) {
	// Page 1 validation
	if (currentPage == 1) {
		if(Form.Element.getValue('assigner') == "None")
		{
			alert('Please choose your name from the "Assigner" drop-down menu.');
			new Effect.Highlight('assigner');
			return 0;
		}
		
		if(Form.Element.getValue('assigner') == "other")
		{
			if(Form.Element.getValue('assigner_name') == "")
			{
				alert('Please enter your name.');
				new Effect.Highlight('assigner_name');
				return 0;
			}
			if(Form.Element.getValue('assigner_email') == "")
			{
				alert('Please enter your e-mail address.');
				new Effect.Highlight('assigner_email');
				return 0;
			}
			if(Form.Element.getValue('assigner_phone') == "")
			{
				alert('Please enter your phone number.');
				new Effect.Highlight('assigner_phone');
				return 0;
			}

		}
		
		
		var anyChecked = false;	
		for (i=0; i<document.photo_request.elements.length; i++)
		{
			if (document.photo_request.elements[i].name == 'for[]')
				{
					if(document.photo_request.elements[i].checked == false)
						{ anyChecked = false; }
					else
						{ anyChecked = true; break;}
				}
		}
			if (!anyChecked) {
				alert('Please check at least one target publication.');
				new Effect.Highlight('for_checkboxes');
				return 0;
				
			}
		
	}
	
	// Page 2 validation
	if (currentPage == 2) {
		if(Form.Element.getValue('summary') == "")
		{
			alert('Please enter the story title or the name of the event.');
			new Effect.Highlight('summary');
			return 0;
		}

		//if(Form.Element.getValue('specific_datetime_yes') == 'yes')
		//{
		var today = new Date();
		//today.setHours(0);
		//today.setMinutes(0);
		//today.setSeconds(0);
		today.setDate(today.getDate()-1);

	
		var shoot_date = new Date(Form.Element.getValue('shoot_date'));
		var shoot_end_date = new Date(Form.Element.getValue('shoot_date'));
		
		if($F('start_time_ampm') == 'am')
		{	shoot_date.setHours($F('start_time_hr')); }
		else //pm
		{
			shoot_date.setHours(parseInt($F('start_time_hr'))+12);
		}
		shoot_date.setMinutes($F('start_time_min'));
		shoot_date.setSeconds(0);
		
		// End time
		if($F('end_time_ampm') == 'am')
		{	shoot_end_date.setHours($F('end_time_hr')); }
		else //pm
		{
			shoot_end_date.setHours(parseInt($F('end_time_hr'))+12);
		}
		shoot_end_date.setMinutes($F('end_time_min'));
		shoot_end_date.setSeconds(0);
		
		if(shoot_date >= shoot_end_date) {
			alert("The end time for the shoot must be after the start time.")
			new Effect.Highlight('end_time_hr');
			return 0;
			
		}
		
		if(shoot_date == "Invalid Date")
		{
			alert("You've entered an invalid format for the shoot date. Use the calendar or enter a date in mm/dd/yyyy.")
			new Effect.Highlight('shoot_date');
			return 0;
		}
		
		if (shoot_date < today)
		{	
			alert("The specified shoot date cannot be in the past.")
			new Effect.Highlight('shoot_date');
			return 0;
			
		}
		
	
		
		//} // If specific datetime

		if(Form.Element.getValue('instructions') == "")
		{
			alert('Please provide detailed instructions to assist the photographer during the shoot. This could include the main idea of the story or a list of important people and events you want photographed.');
			new Effect.Highlight('instructions');
			return 0;
		}
		
		if(Form.Element.getValue('contact_name') == "")
		{
			alert('We need the name of a contact at the shoot location.');
			new Effect.Highlight('contact_name');
			return 0;
		}
		
		if(Form.Element.getValue('contact_phone') == "")
		{
			alert('We need the phone number of a contact at the shoot location.');
			new Effect.Highlight('contact_phone');
			return 0;
		}

		
	} // If currentpage is 2
	
	Effect.Fade('stage' + currentPage, { queue: 'front', duration:0.5});
	Effect.Appear('stage' + (currentPage+1), { queue: 'end', duration:0.1});
}

function prevPage(currentPage) {
	
	Effect.Fade('stage' + currentPage, { queue: 'front', duration:0.5});
	Effect.Appear('stage' + (currentPage-1), { queue: 'end', duration:0.1});
}

function finalValidation() {
	if(Form.Element.getValue('proof_deadline') != "")
	{
			var today = new Date();
			today.setHours(0);
			today.setMinutes(0);
			today.setSeconds(0);
			today.setDate(today.getDate()-1);
			
			var shoot_date = new Date(Form.Element.getValue('shoot_date'));
			
			var proof_deadline = new Date(Form.Element.getValue('proof_deadline'));
			proof_deadline.setHours(0);
			proof_deadline.setMinutes(0);
			proof_deadline.setSeconds(0);

			if(proof_deadline == "Invalid Date")
			{
				alert("You've entered an invalid format for the proof deadline. Use the calendar or enter a date in mm/dd/yyyy.")
				new Effect.Highlight('proof_deadline');
				return false;
			}
			//shoot_date.setFullYear(Form.Element.getValue('shoot_date_year'), Form.Element.getValue('shoot_date_month')-1, Form.Element.getValue('shoot_date_day') ) 

			if (proof_deadline < shoot_date)
			{	
				alert("The proof deadline cannot be before the shoot.")
				new Effect.Highlight('proof_deadline');
				return false;

			}
		
	}
	
	if(Form.Element.getValue('deadline') != "")
	{
			var today = new Date();
			today.setHours(0);
			today.setMinutes(0);
			today.setSeconds(0);
			today.setDate(today.getDate()-1);


			var deadline = new Date(Form.Element.getValue('deadline'));
			deadline.setHours(0);
			deadline.setMinutes(0);
			deadline.setSeconds(0);

			if(deadline == "Invalid Date")
			{
				alert("You've entered an invalid format for the deadline. Use the calendar or enter a date in mm/dd/yyyy.")
				new Effect.Highlight('deadline');
				return false;
			}
			//shoot_date.setFullYear(Form.Element.getValue('shoot_date_year'), Form.Element.getValue('shoot_date_month')-1, Form.Element.getValue('shoot_date_day') ) 

			if (deadline < shoot_date)
			{	
				alert("The deadline cannot be before the shoot.")
				new Effect.Highlight('deadline');
				return false;

			}
	}
	
	return true;
}

var oldLink = null;
// code to change the active stylesheet


// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    cal.callCloseHandler();
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

// If this handler returns true then the "date" given as
// parameter will be disabled.  In this example we enable
// only days within a range of 10 days from the current
// date.
// You can use the functions date.getFullYear() -- returns the year
// as 4 digit number, date.getMonth() -- returns the month as 0..11,
// and date.getDate() -- returns the date of the month as 1..31, to
// make heavy calculations here.  However, beware that this function
// should be very fast, as it is called for each day in a month when
// the calendar is (re)constructed.
function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(0, null, flatSelected);

  // hide week numbers
  cal.weekNumbers = false;

  // We want some dates to be disabled; see function isDisabled above
  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showCalendar" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  cal.create(parent);

  // ... we can show it here.
  cal.show();
}
