// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function add_option(select_id, opt_text, opt_value)
{
	x = $(select_id).options.length;
	var y = new Option(opt_text, opt_value);
	$(select_id).options[x] = y;
}

function delete_option(select_id, opt_value)
{
	for (z=0; z < $(select_id).options.length; z++)
	{
		if ($(select_id).options[z].value == opt_value)
			$(select_id).options[z] = null;
	}
}

function routeView() {
	new Element.hide('enter_route'); 
	new Element.hide('to_field'); 
	new Element.hide('from_field'); 
	new Element.show('route_field'); 
	new Element.show('enter_from_to');
}

function tofromView() {
	new Element.hide('route_field'); 
	new Element.hide('enter_from_to');
	new Element.show('enter_route'); 
	new Element.show('to_field'); 
	new Element.show('from_field');
}

function hasInstructor() {
	if ($("logentry_has_instructor").checked == true && $("logentry_instruction_time").style.display == 'none')
		{ 
			new Effect.Appear("logentry_instruction_type"); 
			new Effect.Appear("logentry_instruction_time"); 
		}
	else if  ($("logentry_has_instructor").checked == false && $("logentry_instruction_time").style.display != 'none') { 
			
			new Effect.Fade("logentry_instruction_type"); 
			new Effect.Fade("logentry_instruction_time");
		}
	
}

function changeTimes() {
	if (!duration_changed)	{
		if (!instruction_time_changed) $("logentry_instruction_time").value = $("logentry_duration").value;
		if (!pic_time_changed) $("logentry_pilot_in_command").value = $("logentry_duration").value;
		duration_changed = true;		
	}
}

function CheckLandings() {

	var errorList = "<ul>";
	var returnval = true;
	
	if ($("logentry_other_time").value > 0 && $("logentry_other_time_text").value == "") {
		errorList += "<li>Please supply a description of the <em>Other</em>&nbsp;  time logged in this entry.";
		new Effect.Appear("other_time_row");
		$("logentry_other_time_text").className = "fieldWithErrors";
		returnval = false;
	}
	else {
		$("logentry_other_time_text").className = "NoErrorField";
	}
	
	
	if ($("logentry_duration").value > 77) {
		errorList += "<li>Steve Fossett's around-the-world flight in the Virgin Atlantic Global Flyer in March 2005 took 77 hours. Was your flight longer than 77 hours?";
		$("logentry_duration").className = "fieldWithErrors";
		returnval = false;
	}
	else {
		$("logentry_duration").className = "NoErrorField";
	}
	
	if (returnval == false) {
		errorList += '</ul><br>Please review the form, make any necessary changes and click the "Save" button to submit.';
		$("error_text").innerHTML = errorList
		new Effect.Appear("error_block");		
	} 
	else
	{
		new Element.hide("error_block");
	}
	
	return returnval; 	
}

function other_time_changed() {
	if ($("logentry_other_time").value > 0) {
		new Effect.Appear("other_time_row");
	}
	else if ($("other_time_row").display != 'none') {
		new Effect.Fade("other_time_row");
	}
}


function attachFunctions(item, itemValue, rating_field) {
	
	item.onmouseout=function(){
		setStars($(rating_field).value, rating_field);
  	}
	item.onmouseover=function(){
		if ($(rating_field).value != itemValue)
			setStars(itemValue, rating_field);
	}
	item.onclick=function(){
		if ($(rating_field).value == itemValue) {
			$(rating_field).value = (itemValue - 1);
		}
		else {
			$(rating_field).value = itemValue;
		}
		setStars($(rating_field).value, rating_field);
	}
}


function setStars(starValue, rating_field_name) {
	z = parseInt(starValue);
	for(x=1; x <= z; x++) {
		starName2 = rating_field_name + "star" + x;
		$(starName2).src = "/images/star.png";
	}
	z = z + 1;
	for(y = z; y <= 5; y++) {
		starName1 = rating_field_name + "star" + y;
		$(starName1).src = "/images/star_inactive.png";
	}
}

function expose_custom(field_value, custom_value, custom_name) {
	//alert(custom_name);
	if (field_value == custom_value) {
		new Effect.Appear(custom_name);
	}
	else {
		new Effect.Fade(custom_name);
	}
}