﻿
  function deleteActionLink(actionLinkId, message) {
      $(actionLinkId).click(function() {
          var actionHref = $(this).attr("href");
          $(this).attr( { "href" : "javascript:;" } );
          if (confirm(message)) {
              $(this).parents().filter("form").attr({ action : actionHref });
              $(this).parents().filter("form").trigger("submit");
          }
      });
  }

  function submitActionLink(actionLinkId) {
      $(actionLinkId).click(function() {
          var actionHref = $(this).attr("href");
          $(this).attr( { "href" : "javascript:;" } );
          $(this).parents().filter("form").attr({ action : actionHref });
          $(this).parents().filter("form").trigger("submit");
      });
  }

  function addInputValue(actionLinkId, inputElementId, posElementId, containerId, nameRemoveLink) {
      $(actionLinkId).click(function() {

          var currentUpload = $(containerId).html();
          var uploadCount = parseInt($(inputElementId).val());
          var uploadPos = $(posElementId).val();
          if (uploadCount == "undefined" || uploadCount == "" || uploadCount == "N/A") {
              uploadCount = 1;
          }
          var newUploadCount = uploadCount + 1;

          if (newUploadCount >= uploadPos) {
              $(posElementId).val(newUploadCount);
          }
          $(inputElementId).val(newUploadCount);

          var newUpload = "<div id=\"startupload" + newUploadCount + "\">\n\n";

          newUpload += "<div class=\"form_field\">\n";
          newUpload += "\t <label for=\"ThumbNailUpload" + newUploadCount + "\" style=\"margin-right: 50px;\"><strong>Thumb Nail:</strong> (Recommended) 80px by 80px</label>\n";
          newUpload += "\t <input type=\"file\" id=\"ThumbNailUpload" + newUploadCount + "\" style=\"border: 1px solid #666; width: 250px; height: 25px; font-size: 12px; padding-left: 2px; padding-top: 2px;\" name=\"ThumbNailUpload" + newUploadCount + "\" size=\"23\" />\n";
          newUpload += "</div>\n\n";

          newUpload += "<div class=\"form_field\">";
          newUpload += "\t <label for=\"LargePicUpload" + newUploadCount + "\" style=\"margin-right: 40px;\"><strong>Large Image:</strong> (Recommended) 350px by 350px</label>";
          newUpload += "\t <input type=\"file\" id=\"LargePicUpload" + newUploadCount + "\" style=\"border: 1px solid #666; width: 250px; height: 25px; font-size: 12px; padding-left: 2px; padding-top: 2px;\" name=\"LargePicUpload" + newUploadCount + "\" size=\"23\" />";
          newUpload += "</div>\n\n";

          newUpload += "<a class=\"submenu\" id=\"" + nameRemoveLink + newUploadCount + "\">Remove File</a>\n\n";

          newUpload += "<script type=\"text/javascript\">\n";
          newUpload += "\t $(document).ready(function() {\n";
          newUpload += "\t\t removeInputValue(\"#" + nameRemoveLink + newUploadCount + "\", \"" + inputElementId + "\", \"" + posElementId + "\", \"" + containerId + "\");\n";
          newUpload += "\t });\n";
          newUpload += "</script>\n\n";

          newUpload += "</div>\n\n";

          $(containerId).append(newUpload);  

      });  
  }

  function removeInputValue(actionLinkId, inputElementId, posElementId, containerId) {
      $(actionLinkId).click(function() {

          var currentUpload = $(containerId).html();
          var uploadCount = $(inputElementId).val();
          var uploadPos = $(posElementId).val();
          if (uploadCount == "undefined" || uploadCount == "") {
              uploadCount = 2;
          }

          if (uploadCount < 1) {
              uploadCount = 1; // must never be zero;
          }
          var newUploadCount = uploadCount - 1;
          if (uploadCount >= uploadPos) {
              $(posElementId).val(newUploadCount);    
          }
          $(inputElementId).val(newUploadCount);

          if (newUploadCount > 0) {
              $("#startupload" + uploadPos).remove();
          } else {
              alert("Cannot Remove File.");
          }

      });
  }

  function handleMore(actionLinkId, moreBodyId) {
      $(actionLinkId).click(function() {
          if ($(moreBodyId).is(":visible")) {
              $(actionLinkId).html("more »");
              $(moreBodyId).hide("slow");
          } else {
              $(actionLinkId).html("« less");
              $(moreBodyId).show("slow");
          }
      });
  }
  
  function wordLeftCount(descriptionId, max, outputId) {

      if (max == null || max == 'undefined') {
          max = 225;
      }
      if (max <= 0) {
          max = 225;  
      }
      $(outputId).html(leftCount(descriptionId, max));

      $(descriptionId).keypress(function() {
          var words = $(this).val();
          $(outputId).html(leftCount(descriptionId, max));
      });

      $(descriptionId).change(function() {
          var words = $(this).val();
          $(outputId).html(leftCount(descriptionId, max));
      });

  }

  function linkOpenDocument(actionId, documentPath, title, width, height) {

      $("#dialogframe").attr({ "src": documentPath, "height": height + "px", "width": "100%" });
      $("#dialoghref").attr({ "href": documentPath });
      $("#dialog").attr({ "title": title }).dialog({
          "bgiframe" : true,
          "height": height,
          "width": width,
          autoOpen: false,
          "modal" : true
      });
       
      $(actionId).click(function() {
        $('#dialog').dialog('open');
      });
  }

  function leftCount(descriptionId, max) {
      var wordLeft = max;
      var words = $(descriptionId).val();
          wordLeft = max - words.length;
      if (wordLeft <= 0) {
          wordLeft = 0;
      }
      return wordLeft;
  }  

  function editProfile(formId, actionControllerUrl, submitButton, dateofBirthTextBox, uploadButton) {
      submitImageForm(formId, actionControllerUrl, submitButton, uploadButton);
      displayDatePicker(dateofBirthTextBox);
  }

  function loadBannerBackGround() {
      banner = new Array("", "bannerbgcontainer", "bannerbgcontainer2", "bannerbgcontainer3", "bannerbgcontainer4");
      var slotnumber = 0;
      while (slotnumber <= 0) {
          slotnumber = Math.floor(Math.random() * 5);
      }
      $("#homemenu").attr("class", banner[slotnumber]);
  }

  function formatDescription() {
      var content = $("#Description").html();
      content = content.replace("&lt;p&gt;", "");
      content = content.replace("&lt;/p&gt;", "");
      $("#Description").html(content);
  }

  function displayDate() {
      displayDatePicker("#DateOfBirth");
  }

  function displayPublication() {
      displayDatePicker("#PublishedOn");
  }

  function displayStartDate() {
      displayDatePicker("#StartDate");
  }

  function displayEditor() {
      CKEDITOR.replace('Body');
  }

  function resolveCheckBox(checkBoxId) {

      var checkBox = (checkBoxId.indexOf("#") != -1 ? checkBoxId.substr(1) : checkBoxId);

      $(checkBoxId).click(function() {

          if ($("input[name='" + checkBox + "'][type='checkbox']").is(":checked")) {
              $("input[name='" + checkBox + "'][type='hidden']").val("true");
              $(this).val("true");
          } else {
              $("input[name='" + checkBox + "'][type='hidden']").val("false");
              $(this).val("false");
          }
      });
    
  }    
  
  function submitImageForm(formId, actionControllerUrl, submitButton, uploadButton) {

      if ($(formId).attr("enctype") == "multipart/form-data") {
        $(submitButton).attr({ disabled: "disabled" });
      } else {
        $(submitButton).removeAttr("disabled");
      }

      $(uploadButton).click(function() {
          $(this).parents().filter("form").attr({ action: actionControllerUrl });
          $(this).parents().filter("form").trigger("submit");
      });

  }

  function preload(images) {
      imageObj = new Image();
      for (i = 0; i <= images.length; i++)
          imageObj.src = images[i];
  }

  function displayDatePicker(datePickerId) {
      $(datePickerId).datepicker(); 
  }

  function displayJavascriptEditor(DescriptionTextAreaId) {
      CKEDITOR.replace(DescriptionTextAreaId);
  }
  