//public

function dropDownDestinationJump(id) {
		var destinationDropDown = $('destinationDropDown');
		var selectedIndex = destinationDropDown.selectedIndex;
		location.href= destinationDropDown[selectedIndex].value;
}


function filterTrips(filterValue, type, id) {
		new Ajax.Updater('tripList', baseDirectory + 'trip_list.mvc', {
		   method: 'get',
		   parameters: {filterValue: filterValue, type: type, id: id}
		});	
}




//provider / admin private
//Begin Section Admin
function saveProviderForm() {
	new Ajax.Updater('providerFormContainer', baseDirectory + 'admin/provider_form.mvc', {
	  parameters: $('providerForm').serialize(true),
	  evalScripts:true
	});
}

function saveTripForm() {
	new Ajax.Updater('tripFormContainer', baseDirectory + 'admin/trip_form.mvc', {
	  parameters: $('tripForm').serialize(true),
	  evalScripts:true, 
      onSuccess: function(transport) {
	      try {
	        tinyMCE.execCommand('mceFocus', false, 'description'); 
	        tinyMCE.execCommand('mceRemoveControl', false, 'description');
	      } catch(err2) {}
      }
	});
}

function addTripCountry() {
	new Ajax.Updater('tripCountryList', baseDirectory + 'admin/trip_country_list.mvc', {
	  parameters: $('tripCountryForm').serialize(true)
	});
	removeFromList($('tripCountry'));
}

function removeTripCountry(tripId, id, name) {
	new Ajax.Updater('tripCountryList', baseDirectory + 'admin/trip_country_list.mvc', {
	  parameters: {tripId: tripId, id: id, action: 'DELETE'}
	});
	addToList($('tripCountry'), name, id);
}

function addTripActivity() {
	new Ajax.Updater('tripActivityList', baseDirectory + 'admin/trip_activity_list.mvc', {
	  parameters: $('tripActivityForm').serialize(true)
	});
	removeFromList($('tripActivity'));
}

function removeTripActivity(tripId, id, name) {
	new Ajax.Updater('tripActivityList', baseDirectory + 'admin/trip_activity_list.mvc', {
	  parameters: {tripId: tripId, id: id, action: 'DELETE'}
	});
	addToList($('tripActivity'), name, name);
}


function addImageToTripCarousel(tripId, url, thumbnailUrl, reloadPage) {
	new Ajax.Request(baseDirectory + 'trip_carousel.mvc', {
		method: 'post',
		parameters: {action: 'SAVEORUPDATE', tripId: tripId, url: url, thumbnailUrl: thumbnailUrl},
		onSuccess: function(transport) {
			imageId = transport.responseText;
			carousel.insertAfter(carousel.calculateSize(), fmtItem(tripId, imageId, thumbnailUrl, url, "",-1));
		},
		onComplete: function() {
			if (reloadPage)//needed for first image in carousel
				window.location.reload();
		}
	});
}


function removeImageFromTripCarousel(tripId, imageId) {
	new Ajax.Request(baseDirectory + 'trip_carousel.mvc', {
		method: 'post',
		parameters: {action: 'DELETE', tripId: tripId, imageId: imageId},
		onComplete: function() {
			window.location.reload();//TODO: I can't find a way to remove the the items properly via javascript so I will wait un
		}
	})
}


//admin only private
function updateCountryHeaderImage(countryId, imageId, imageUrl) {
	new Ajax.Request(baseDirectory + 'country.mvc', {
		method: 'post',
		parameters: {action: 'SAVEORUPDATE', id: countryId, imageId: imageId, imageUrl: imageUrl}
	});
}

activeDestinationEditContainer = -1;
function showDestinationEditForm(type, id, show) {
	if (activeDestinationEditContainer != -1) {
		$(type + 'Editor_' + activeDestinationEditContainer).hide();
		$(type + 'Container_' + activeDestinationEditContainer).show();
		//activeEditContainer = -1;
	}
	if (show) {
		activeDestinationEditContainer = id;
		if (id == 'dropDown') {
			destinationDropDown = $('destinationDropDown');
			selectedIndex = destinationDropDown.selectedIndex;
			destinationForm = $('destinationForm_dropDown');
			destinationForm.name.value = destinationDropDown.options[selectedIndex].text;
			destinationForm.id.value = destinationDropDown.options[selectedIndex].id;
		}
		$(type + 'Container_' + id).hide();
		$(type + 'Editor_' + id).show();
	} else {
		$(type + 'Editor_' + id).hide();
		$(type + 'Container_' + id).show();
	}

}

function showNewForm(id, show) {
	if (show) {
		$(id).hide();
		$(id +'Form').show();
	} else {
		$(id).show();
		$(id + 'Form').hide();
	}
}

function saveOrUpdateDestination(type, id) {
	if (id == -1) {//new
		new Ajax.Updater('destinationList', baseDirectory + type + '_list.mvc', {
		  parameters: $('destinationForm').serialize(true)
		});	
		$('destinationName').value = '';//clear
	} else {//edit
		new Ajax.Updater('destinationList', baseDirectory + type + '_list.mvc', {
		  parameters: $('destinationForm_' + id).serialize(true)
		});
	}
}

activeGenericPageEditContainer = -1;
function showGenericPageEditForm(id, show) {
	if (activeGenericPageEditContainer != -1) {
		$('genericPageEditor_' + activeGenericPageEditContainer).hide();
		$('genericPageContainer_' + activeGenericPageEditContainer).show();
	}
	if (show) {
		activeGenericPageEditContainer = id;
		$('genericPageContainer_' + id).hide();
		$('genericPageEditor_' + id).show();
	} else {
		$('genericPageEditor_' + id).hide();
		$('genericPageContainer_' + id).show();
	}

}

function saveOrUpdateGenericPage(id) {
	if (id == -1) {//new
		new Ajax.Updater('genericPageList', baseDirectory + '/admin/generic_page_list.mvc', {
		  parameters: $('genericPageForm').serialize(true)
		});	
	$('genericPageName').value = '';//clear
	} else {//edit
		new Ajax.Updater('genericPageList', baseDirectory + '/admin/generic_page_list.mvc', {
		  parameters: $('genericPageForm_' + id).serialize(true)
		});
	}
}

function confirmGenericPageDelete(id) {
	new Ajax.Updater('genericPageList', baseDirectory + '/admin/generic_page_list.mvc', {
	   parameters: {id: id, action: 'DELETE'}
	 });
}

function confirmDestinationDelete(type, id) {
	if (id == 'dropDown') {
			var destinationDropDown = $('destinationDropDown');
			var selectedIndex = destinationDropDown.selectedIndex;
			var id = destinationDropDown.options[selectedIndex].id;
	}
	new Ajax.Updater('destinationList', baseDirectory + type + '_list.mvc', {
	   parameters: {id: id, action: 'DELETE'}
	 });
}

//Begin Section Admin
function addSection() {
	new Ajax.Updater('sectionList', baseDirectory + 'section_list.mvc', {
	  parameters: $('sectionForm').serialize(true)
	});
	removeFromList($('sectionName'));
}

function removeSection(id, name) {
	new Ajax.Updater('sectionList', baseDirectory + 'section_list.mvc', {
	  parameters: {id: id, action: 'DELETE'}
	});
	addToList($('sectionName'), name, name);
}
//End Section Admin

//Begin WYSIWYG
function showEditPageTextForm(type, show) { 
	if (show) {
		$(type + 'PageTextContainer').hide();
		$(type + 'PageTextEditor').show();
	} else {
		$(type + 'PageTextEditor').hide();
		$(type + 'PageTextContainer').show();
	}
}

function previewPageText(type, pageText) {
	$(type + 'PageTextContainer').innerHTML = pageText;
	showEditPageTextForm(type, false);
}

function savePageText(type, pageText) {
	$(type + 'PageTextForm').pageText.value = pageText;
	new Ajax.Request(baseDirectory + 'page_text.mvc', {
	  parameters: $(type + 'PageTextForm').serialize(true),
	  onSuccess: function(transport) {
	    alert(transport.responseText);
  	  }
	});
	
	$(type + 'PageTextContainer').innerHTML = pageText;
	showEditPageTextForm(false);
}
//End WYSIWYG


function removeFromList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be removed!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be removed!");
      } else {  // Build arrays with the text and values to remain
         var replaceTextArray = new Array(listField.length-1);
         var replaceValueArray = new Array(listField.length-1);
         for (var i = 0; i < listField.length; i++) {
            // Put everything except the selected one into the array
            if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
            if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
            if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
            if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
         }
         listField.length = replaceTextArray.length;  // Shorten the input list
         for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
            listField.options[i].value = replaceValueArray[i];
            listField.options[i].text = replaceTextArray[i];
         }
      } // Ends the check to make sure something was selected
   } // Ends the check for there being none in the list
}

function addToList(listField, newText, newValue) {
   if ( ( newValue == "" ) || ( newText == "" ) ) {
      alert("You cannot add blank values!");
   } else {
      var len = listField.length++; // Increase the size of list and return the size
      listField.options[len].value = newValue;
      listField.options[len].text = newText;
      listField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered)
   } // Ends the check to see if the value entered on the form is empty
}