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

function equalizeDivHeight(div1, div2)
{
  y1 = div1.getHeight();
  y2 = div2.getHeight();

  if(y2 < y1)
    y = y1;
  else
    y = y2;

  div1.style.height = y + "px";
  div2.style.height = y + "px";
}

function equalizeMorePlace(div1, more1, div2, more2)
{
  y1 = div1.getHeight();
  y2 = div2.getHeight();

  if(y2 < y1)
    y = y1;
  else
    y = y2;

  div1.style.height = y + "px";
  div2.style.height = y + "px";

  o1 = (y - y1);
  o2 = (y - y2);

  more1.style.marginTop = o1 + "px";
  more2.style.marginTop = o2 + "px";
}

function changeExpirationDate(expirationDateDiv, numberOfDays) {
  selects = expirationDateDiv.getElementsByTagName("select");
  yearSelect = selects[0];
  monthSelect = selects[1];
  daySelect = selects[2];
  var year = yearSelect.options[yearSelect.selectedIndex].value;
  var month = monthSelect.options[monthSelect.selectedIndex].value - 1;
  var day = daySelect.options[daySelect.selectedIndex].value;
  myDate = new Date();
  myDate.setFullYear(year, month, day);
  myDate.setDate(myDate.getDate() + numberOfDays);
  selectOption(yearSelect, myDate.getFullYear());
  selectOption(monthSelect, myDate.getMonth() + 1);
  selectOption(daySelect, myDate.getDate());


}

function selectOption(select, option) {
  for (var i = 0; i < select.length; i++) {
    if (select.options[i].value == option) {
      select.options[i].selected = true;
      return;
    }
  }
}

function addHyperlink(textAreaId) {
  var url = prompt("Please enter the URL to link to", "http://");
  if (url != null && url.length > 0) {
    startTag = "<a href=\""+url+"\">";
    endTag = "<\/a>";
    insertIntoTextArea(textAreaId, startTag, url, endTag);
  } else {
    alert('You must enter a URL to link to.');
  }
}
function insertIntoTextArea(textAreaId, startTag, url, endTag) {
  // grab the textarea off the dom tree
  var ta = document.getElementById(textAreaId);

  if (document.selection) { //IE win
    // code from Meg Hourihan
    // http://www.oreillynet.com/pub/a/javascript/2001/12/21/js_toolbar.html
    var str = document.selection.createRange().text;
                if (str == '') { str = url; }
    ta.focus();
    var sel = document.selection.createRange();
    sel.text = startTag + str + endTag;

  } else if (ta.selectionStart | ta.selectionStart == 0) { // Mozzzzzzila relies on builds post bug #88049
    // work around Mozilla Bug #190382
    if (ta.selectionEnd > ta.value.length) { ta.selectionEnd = ta.value.length; }

    // decide where to add it and then add it
    var firstPos = ta.selectionStart;
    var secondPos = ta.selectionEnd+startTag.length; // cause we're inserting one at a time
                if (ta.selectionStart == ta.selectionEnd) { // add url if there was no text selected
      ta.value=ta.value.slice(0,firstPos)+startTag+url+ta.value.slice(firstPos);
      secondPos += url.length;
                } else {
      ta.value=ta.value.slice(0,firstPos)+startTag+ta.value.slice(firstPos);
                }
    ta.value=ta.value.slice(0,secondPos)+endTag+ta.value.slice(secondPos);

    // reset selection & focus... after the first tag and before the second
    ta.selectionStart = firstPos+startTag.length;
    ta.selectionEnd = secondPos;
    ta.focus();
  }
}
  function toggle_visibility(id) {
     var e = document.getElementById(id);
     if(e.style.display == 'block')
  e.style.display = 'none';
     else
  e.style.display = 'block';
  }

function changeAllPosts() {
  var c = $('select_all_box').checked;
  var boxes = $$('input.check-box-tag');
  for (var i = 0; i < boxes.length; i++) {
    boxes[i].checked = c;
  }
}

function DoWaterMarkOnFocus(txt, text)
{
  if (txt.value == text) {
    txt.value = "";
  }
}

function DoWaterMarkOnBlur(txt, text)
{
  if (txt.value == "") {
    txt.value = text;
  }
}
