﻿$(function() {
   initializeDatePicker();
   $('a[rel*=facebox]').facebox();
});

function markRequiredFields() {
     $('.required').prev('label').prepend('<em>*</em> ');
 }

 function notifyMessage() {
     $("#message").fadeIn("slow");
     $("#message a.close-notify").click(function() {
         $("#message").fadeOut("slow");
         return false;
     });
 }

function initializeDatePicker() {
   $(".date-input").datepicker();
   $(".required-date-input").datepicker();
}

function initializeTooltips() {
            $("#form [title]").tooltip({      
	        // place tooltip on the right edge
	        position: "center left",
	        // a little tweaking of the position
	        offset: [1, 0],
	        // use the built-in fadeIn/fadeOut effect
	        effect: "fade",
	        // custom opacity setting
	        opacity: 1,
	        // use this single tooltip element
	        tip: '.tooltip',
	        //positioning fix for tooltip
	        relative: 'true',
	        delay: 0
         });
}

function initializeWizard() {
   $("ul.tabs").tabs("div.panes > div");
   var api = $("ul.tabs").tabs(0);
   $(":input[type='button'].next").click(function() { api.next(); moveToTop(); });
   $(":input[type='button'].previous").click(function() { api.prev(); moveToTop(); });
}

function moveToTop() {
   $("html").animate({ scrollTop: 460 }, "fast");
}

function updateList(listId, items, clearFirstOption, showFirstOption) {
   clearList(listId, clearFirstOption, showFirstOption);
   if (items.length > 0) {
      for (i in items) {
         var item = items[i];
         $(listId).append('<option value="' + item.Id + '">' + item.Name + '</option>');
      }
   }
   $(listId).fadeOut('fast').fadeIn('fast');
}

function clearList(listId, clearFirstOption, showFirstOption) {
   if (!clearFirstOption) {
      var firstOptionText = $(listId + " option:first").text();
      var firstOptionValue = $(listId + " option:first").val();
   }
   $(listId).html("");
   
   if (showFirstOption) {
      $(listId).append("<option value='" + firstOptionValue + "'>" + firstOptionText + "</option>");
   }
}

//copys the elements of the source list to the cleared destinationlist
function copylist(sourceListId, destinationListId) 
{
   var selectClone = $(sourceListId).clone();
   clearList(destinationListId, false, true);
   $(destinationListId).html(selectClone.html());
}

// returns an array of checked values for elements that begin with [elementName]
function getCheckedValues(elementName) {
   var result = new Array();
   var checkedItems = $("input[name^='" + elementName + "']:checked");
   if (checkedItems != null) {
       checkedItems.each(function() { result.push(this.value); });
   }
   return result;
}

// returns an comma delimited string of checked values for elements that begin with [elementName]
function getStringOfCheckedValues(elementName) {
    var result = "";
    var checkedItems = $("input[name^='" + elementName + "']:checked");
    if (checkedItems != null) {
        checkedItems.each( function() 
                    {
                        if (result.length == 0) {
                            result = this.value;
                        } else {
                            result = result + "," + this.value;
                        }
                    }
                );
    }
    return result;
}

// returns a comma delimited string of values for elements that begin with [elementName]
function getStringOfValues(elementName) {
   var result = "";
   var items = $("input[name^='" + elementName + "']");
   if (items != null) {
      items.each(function() {
         if (result.length == 0) {
            result = this.value;
         } else {
            result = result + "," + this.value;
         }
      }
      );
   }
   return result;
}

// returns a comma delimited string of values for input elements that begin with [elementName]
function getStringOfValues(elementName) {
   var result = "";
   var items = $("input[name^='" + elementName + "']");
   if (items != null) {
      items.each(function() {
         if (result.length == 0) {
            result = this.value;
         } else {
            result = result + "," + this.value;
         }
      }
      );
   }
   return result;
}

function getStringOfSelectedValues(elementName) 
{
   var selectedItems = $(elementName + ' :selected');
   var result = "";
   
   if (selectedItems != null) {
      selectedItems.each(function() {
         if (result.length == 0) {
            result = this.value;
         } else {
            result = result + "," + this.value;
         }
      });
   }
   return result;
}

//sets the days in a month for select assumes that numbers are used as the value of the months select
function setDaysInMonth(dayElementName, monthElementName, yearElementName) {
   var monthsWith31Days = new Array('1', '3', '5', '7', '8', '10', '12');
   var selectedMonth = $(monthElementName).val();  
   var selectedYear = $(yearElementName).val();

   if ($.inArray(selectedMonth, monthsWith31Days) == -1) 
   {
       if ($('' + dayElementName + ' option[value=31]').length > 0) {
         $(dayElementName + " option[value='31']").remove();
      }

      if (selectedMonth == 2) {
         if ($('' + dayElementName + ' option[value=30]').length > 0) {
             $(dayElementName + " option[value='30']").remove();
         }

         if (((selectedYear % 4 == 0) & (selectedYear % 100 != 0)) || (selectedYear % 400 == 0)) {
            if ($('' + dayElementName + ' option[value=29]').length == 0) {
               $(dayElementName).append("<option value='29'>29</option>");
            }
         } else {
            if ($('' + dayElementName + ' option[value=29]').length > 0) {
                $(dayElementName + " option[value='29']").remove();
            }
         }
      } else {

         if ($('' + dayElementName + ' option[value=29]').length == 0) {
             $(dayElementName).append("<option value='29'>29</option>");
         }

         if ($('' + dayElementName + ' option[value=30]').length == 0) {
             $(dayElementName).append("<option value='30'>30</option>");
         }
      }
   } else {

      if ($('' + dayElementName + ' option[value=29]').length == 0) {
          $(dayElementName).append("<option value='29'>29</option>");
      }

      if ($('' + dayElementName + ' option[value=30]').length == 0) {
          $(dayElementName).append("<option value='30'>30</option>");
      }

      if ($('' + dayElementName + ' option[value=31]').length == 0) {
          $(dayElementName).append("<option value='31'>31</option>");
      }
   }
}

// returns an array of related values for elements that begin with [elementName]
// relies on the related text input being names checkname_value
function getCheckedInputValues(elementName) {
   var result = new Array();
   var ids = "";
   var amounts = "";
   
   var checkedItems = $("input[name^='" + elementName + "']:checked");
   var first = true;
   
   if (checkedItems != null) 
   {
      checkedItems.each
      (
         function() {
            var correspondingItem = $("input[id^='Applied" + this.id + "']");
            if (correspondingItem != null) {
               if (!first) {
                  ids = ids + ","
                  amounts = amounts + ","
               } else {
                  first = false;
               }
               ids = ids + this.value;
               amounts = amounts + parseFloat((correspondingItem).val().replace(",", "").replace("$", ""));
            }
         }
      );
      
      result.push(ids);
      result.push(amounts);
   }
   return result;
}

// returns an array of related values for elements that begin with [elementName]
// relies on the related text input being names checkname_value
function getInputValues(elementName) {
   var result = new Array();
   var ids = "";
   var amounts = "";

   var items = $("input[name^='" + elementName + "']");
   var first = true;

   if (items != null) {
      items.each
      (
         function() {
            var correspondingItem = $("input[id^='Applied" + this.id + "']");
            if (correspondingItem != null) {
               if (!first) {
                  ids = ids + ","
                  amounts = amounts + ","
               } else {
                  first = false;
               }

               ids = ids + this.value;
               amounts = amounts + (correspondingItem).val();
            }
         }
      );

      result.push(ids);
      result.push(amounts);
   }
   return result;
}

function escapeChars(myid) {
   return myid.replace(/(\[|\]|:|\.)/g, '\\$1');
}

function getAndClearValue(inputId) {
   var element = $(inputId);
   var value = element.val();
   element.val('');
   return value;
}

