
// Forms
if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', cmxform, false );

/**
* Function to properly format the form in mozilla
*/
function cmxform(){
  // Hide forms
 $( 'form.cmxform' ).hide().end();
  
  // Processing
  $( 'form.cmxform' ).find( 'li/label' ).not( '.nocmx' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = null;
    this.appendChild( labelSpan );
  } ).end(); 
  
  // Show forms
  $( 'form.cmxform' ).show().end();
}

// JS Calendar
var _dynarch_popupCalendar   = null; // remember the calendar object so that we reuse
var _dynarch_popupCalendarId = null; // remember the id so we can update the drop downs

// it and avoid creating another

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
  if (cal.dateClicked) {
	  updateSelect(cal); // update drop downs
	  cal.callCloseHandler();
  }
}

// This function updates the drop down select boxes
function updateSelect(cal) {
  var date = cal.date;

  var selectMonth = document.getElementById(_dynarch_popupCalendarId+'-Month');
  selectMonth.selectedIndex = date.getMonth();

  var selectDay = document.getElementById(_dynarch_popupCalendarId+'-Day');
  selectDay.selectedIndex = (date.getDate() - 1);

  var selectYear = document.getElementById(_dynarch_popupCalendarId+'-Year');
  selectYear.selectedIndex = (date.getFullYear() - cal.minYear);
}

// This function parses the drop down select boxes
function parseSelect() {
  var selectMonth = document.getElementById(_dynarch_popupCalendarId+'-Month');
  var selectDay = document.getElementById(_dynarch_popupCalendarId+'-Day');
  var selectYear = document.getElementById(_dynarch_popupCalendarId+'-Year');

  var fMonth = selectMonth.value;

  if (selectMonth.value < 10)
    fMonth = '0' + selectMonth.value;

  return selectDay.value + '/' + fMonth + '/' + selectYear.value;
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar   = null;
  _dynarch_popupCalendarId = null;
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id) {
  var el = document.getElementById(id+'-Month');

  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);

    cal.weekNumbers = false; // set to true to show the week numbers
    cal.showsOtherMonths = true;

    _dynarch_popupCalendar = cal;                  // remember it in the global var
	_dynarch_popupCalendarId = id;

    cal.setRange(2005, 2020);        // min/max year allowed.
    cal.create();
  }

  _dynarch_popupCalendar.setDateFormat('%e/%m/%Y');    // set the specified date format
  _dynarch_popupCalendar.parseDate(parseSelect());      // parse the drop downs

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el);        // show the calendar

  return false;
}

// this function switches the divs for the tabs
function flipTab(old_tab, new_tab) {
	// hide the old tab and change it's class
	var ot    = document.getElementById(old_tab);
	var otBox = document.getElementById(old_tab + 'Box');

	// test to make sure they aren't clicking on the same box that is open
	var otCheck = new RegExp('\\bactiveTab\\b').test(ot.className);

	if (!otCheck) {
		return;
	}

	ot.className        = ot.className.replace('activeTab', 'tab');
	otBox.style.display = "none";

	// show the new tab
	var nt    = document.getElementById(new_tab);
	var ntBox = document.getElementById(new_tab + 'Box');

	nt.className        = nt.className.replace('tab', 'tab activeTab');
	ntBox.style.display = "block";
}

// This function turns off the teaser text popup
function teaserOff(link) {
   var teaser = document.getElementById('teaserBox');
   teaser.style.display = "none";
}

// This function turns on the teaser text popup
function teaserOn(link, t, img) {
   var teaser = document.getElementById('teaserBox');
   if (teaser == null) return;

   if (img != '') {
	  t='<img src="'+img+'" style="float:right;margin:3px" height="60px">'+t;
   }

   teaser.innerHTML = t;
   var o = link;
   var t = 0;
   var l =0;

   while (o) {
	   t+=o.offsetTop;
	   l+=o.offsetLeft;
	   o=o.offsetParent;
   }

   t+=link.offsetHeight;
   teaser.style.top = t+"px";
   teaser.style.left = l+"px";
   teaser.style.display = "block";
}

// This function changes the style sheet for font size changes
function setActiveStyleSheet(title) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

// A simple popup box
function popupBox(name, url, height, width, scroll) {
	newwindow = window.open(url,name,'height='+height+',width='+width+',scrollbars='+scroll);
	if (window.focus) {newwindow.focus()}
	return false;
}

// Changes the style
function changeFontSizeClass(idToChange, fontSize)
{
	var elem = document.getElementById(idToChange);
	elem.setAttribute("class", fontSize);
	elem.setAttribute("className", fontSize);
	return;
}