function changeDisplayStyle(targetId,style)
{
  target=getField(targetId);
  if (!target)
     return;
  if(!target.style){
     return;
  }
  target.style.display=style;
}

function getField(fieldId)
{
	var object=document.getElementById(fieldId);
	if(null != object)
	{	    
	    if(!object)
	      alert("Configuartion error: '" + fieldId + "' not a valid Id");
    }
    return object;
}

function hideSection(sectionName, hideButtonName, showButtonName)
{
	changeDisplayStyle(sectionName,'none');
	changeDisplayStyle(showButtonName,'');
	changeDisplayStyle(hideButtonName,'none');
}

function showSection(sectionName, hideButtonName, showButtonName)
{
	changeDisplayStyle(sectionName,'');
	changeDisplayStyle(showButtonName,'none');
	changeDisplayStyle(hideButtonName,'');
}

function hideAllPreviewImages()
{
	changeDisplayStyle('mainContentImage','none');
	changeDisplayStyle('parkSignatureImage','none');
}

// Scripts "borrowed" from http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C5F00602363
function moveUpList(fieldId) {
   listField = document.getElementById(fieldId);
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected 
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               alert("The first entry in the list cannot be moved up.");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected-1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected-1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected-1].text = moveText2;
               listField[selected-1].value = moveValue2;
               listField.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
   checkForEmptySelect();
}

function moveDownList(fieldId) {
   listField = document.getElementById(fieldId);
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected 
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == listField.length-1 ) {
               alert("The last entry in the list cannot be moved down.");
            } else {
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected+1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected+1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected+1].text = moveText2;
               listField[selected+1].value = moveValue2;
               listField.selectedIndex = selected+1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
   checkForEmptySelect();
}

function removeFromList(fieldId) {
	   listField = document.getElementById(fieldId);
	   var selected = listField.selectedIndex;
	   if (selected == -1)
	   {
	   	alert("You must select an entry to be removed!");
	   }
   if (selected != -1 && confirm("Are you sure you want to delete this item?")) {
	   if ( listField.length == -1) {  // If the list is empty
	      alert("There are no values which can 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 for there being none in the list
	   checkForEmptySelect();
   }
}



function hideOrShowElement (whichWay, element, hideLink, showLink) 
{

	if (whichWay == "hide")
	{
		changeDisplayStyle(hideLink, 'none');
		changeDisplayStyle(showLink, '');
		changeDisplayStyle(element, 'none');
	}

	if (whichWay == "show")
	{
		changeDisplayStyle(hideLink, '');
		changeDisplayStyle(showLink, 'none');
		changeDisplayStyle(element, '');

	}

}


function submitForm () {
	document.getElementById('formSubmitButton').click();
}


function confirmDelete(url, itemType)
{
	var confirmText = "Are you sure you want to delete this item of type " + itemType + "?";
	if (confirm(confirmText)) 
	{
		document.location.href = url;
	}
}
