var current_list;
var current_head;

function menu( current_section ) {
  current_head = current_section;
  
  if ( current_section == 'home' ) {
    current_list = document.getElementById( 'home_sub' );
    current_list.style.height = 'auto';
    document.getElementById( 'home' ).className = 'biggy expander';
  }
  else if ( current_section == 'jobseeker' ) {
    current_list = document.getElementById( 'jobseeker_sub' );
    current_list.style.height = 'auto';
    document.getElementById( 'jobseeker' ).className = 'biggy expander';
  }
  else if ( current_section == 'employer' ) {
    current_list = document.getElementById( 'employer_sub' );
    current_list.style.height = 'auto';
    document.getElementById( 'employer' ).className = 'biggy expander';
  }
}

function bookmark() {
  var url = window.location.href;
  var page = document.title;
  
   if ( document.all ) {
      window.external.AddFavorite(window.location.href, document.title);       
   }
   else if ( window.sidebar ) {
      window.sidebar.addPanel(document.title, window.location.href, '');     
   }
   else if ( window.opera && window.print ) { // Opera Hotlist
      return true;
   }
}



function form_shower( row_div ) {
  
  
}

function form_hider(our_form, hidden_div) {
  if ( our_form.value.toLowerCase() == 'other' )
  {
    inputs = document.getElementById( hidden_div ).getElementsByTagName('input');
    
    for ( i = 0; i < inputs.length; i++)
      inputs[i].disabled = false;
    
    document.getElementById( hidden_div ).style.height = "auto";
    document.getElementById( hidden_div ).style.margin = "0 auto 15px auto";
  }
  else if ( document.getElementById( hidden_div ) )
  {
    inputs = document.getElementById( hidden_div ).getElementsByTagName('input');
    
    for ( i = 0; i < inputs.length; i++)
      inputs[i].disabled = true;
    
    document.getElementById( hidden_div ).style.height = "0";
    document.getElementById( hidden_div ).style.margin = "0 auto 0 auto"; 
  }
}






















// WELCOME TO THE CALENDARER //

var calContainer = false;
var calWrapper = false;
var origClass = '';
var monthSlot;
var monthDiv = false;
var currentDate = new Date();
var theDate = new Date();
var selectedDate = new Date();
var month;
var year;

var theInput;

function start_calendar ( inputSpan ) {
  if ( !calContainer ) {
    calContainer = inputSpan.parentNode;
    origClass = calContainer.className;
    calContainer.className = origClass + ' show_calendar';
    
    // create the calendar wrapper div and append to the calendar container
    calWrapper = document.createElement( 'div' );
    calContainer.appendChild( calWrapper );
    calWrapper.className = 'calendar';
    
    // create & append the month slot div to the calendar wrapper
    monthSlot = document.createElement( 'div' );
    calWrapper.appendChild( monthSlot );
    monthSlot.className = 'month_slot';
    
    // get the input contained by the calendar and disable it so folks can't click on it
    theInput = inputSpan.getElementsByTagName( 'input' )[0];
    theInput.disabled = true;
    
    // get the date out of the input if we can
    var dt = Date.parse( theInput.value );
    if ( dt ) // use date if we get one
      theDate.setTime( dt );
    selectedDate = theDate;
    // draw the month
    new_month( 0 );
  }  
}

var monthNames = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];

function new_month ( theOffset ) {
  if ( theOffset )
    theDate = new Date( theDate.getFullYear(), theDate.getMonth() + theOffset, theDate.getDate() );
  
  // if this is not first call, we have a month div, so get rid of it.
  if ( monthDiv )
    monthSlot.removeChild( monthDiv );
    
  // create the month div, and all the days.
  monthDiv = document.createElement( 'div' );
  monthSlot.appendChild( monthDiv );
  monthDiv.className = 'month';
    
  var h2 = document.createElement( 'h2' );
  
  var theMonth = theDate.getMonth();
  var theYear = theDate.getFullYear();
  // calculate how many days in the month (math hack on the date obj)
  var totalDays = 32 - new Date( theYear, theMonth, 32 ).getDate();
  
  h2.appendChild( document.createTextNode( monthNames[theMonth] + ' ' + theYear ) );
  
  // var theOffset = theDate.getDay();
  // the old code above was taking the number of the week of the selected day to create
  // the offset. Now we take the number of the first day in the month (or the second,
  // actually) and it seems to work without having to change anything else in the old code
  
  theOffset = new Date( theYear, theMonth, 1 ).getDay();
  
  monthDiv.appendChild( h2 );
  
  var month_ul = document.createElement('ul');
  var count = 0;
  var numRows = 6;
  for ( var d = 1; d <= totalDays; d++ ) {
    var aListItem = document.createElement( 'li' );
    if ( theOffset == 0 ) {
      var daySelected = false;
      aListItem.appendChild( document.createTextNode( d ) );
      if ( ( d == selectedDate.getDate() ) && ( theMonth == selectedDate.getMonth() ) && ( theYear == selectedDate.getFullYear() ) ) {
        aListItem.className = 'selected';
        daySelected = true;
      }
      if ( ( d == currentDate.getDate() ) && ( theMonth == currentDate.getMonth() ) && ( theYear == currentDate.getFullYear() ) ) {
        if ( daySelected )
          aListItem.className = 'selected today';
        else
          aListItem.className = 'today';
      }
      aListItem.onmouseover = function () { addClass( this, 'over' ) }
      aListItem.onmouseout = function () { removeClass( this, 'over' ) }
      
      aListItem.onclick = function () { month_off( this ) }
      
      aListItem.style.cursor = 'pointer';
    }
    
    else if ( theOffset != 0 ) {
      aListItem.appendChild( document.createTextNode( 'space' ) );
      
      aListItem.className = 'space';
      
      d--;
      theOffset--;
    }
    
    month_ul.appendChild( aListItem );
    count++;
    
    if ( ( count % 7 ) == 0 ) {
      aListItem = document.createElement( 'li' );
      aListItem.appendChild( document.createTextNode( 'break' ) );
      aListItem.className = 'destroyer';
      month_ul.appendChild( aListItem );
      numRows--;
    }
  }
  
  if ( numRows > 0 ) {
    while ( numRows-- ) {
      aListItem = document.createElement( 'li' );
      aListItem.appendChild( document.createTextNode( 'space' ) );
      aListItem.className = 'space';
      month_ul.appendChild( aListItem );
      aListItem = document.createElement( 'li' );
      aListItem.appendChild( document.createTextNode( 'break' ) );
      aListItem.className = 'destroyer';
      month_ul.appendChild( aListItem );
    }
  }
  monthDiv.appendChild( month_ul );
    
  var back_span = document.createElement( 'span' );
  back_span.className = 'back_span';
  back_span.appendChild( document.createTextNode( '<< previous month' ) );
  back_span.onclick = function () { new_month( -1 ); }
  
  var front_span = document.createElement('span');
  front_span.className = 'front_span';
  front_span.appendChild( document.createTextNode( 'next month >>' ) );
  front_span.onclick = function () { new_month( 1 ); }
  
  monthDiv.appendChild( back_span );
  monthDiv.appendChild( front_span );
  
  back_span = document.createElement( 'span' );
  back_span.className = 'back_span';
  back_span.appendChild( document.createTextNode( '<< previous year' ) );
  back_span.onclick = function () { new_month( -12 ); }
  
  front_span = document.createElement( 'span' );
  front_span.className = 'front_span';
  front_span.appendChild( document.createTextNode( 'next year >>' ) );
  front_span.onclick = function () { new_month( 12 ); }
  
  var can_span = document.createElement( 'span' );
  can_span.className = 'can_span';
  can_span.appendChild( document.createTextNode( 'cancel' ) );
  can_span.onclick = function () { month_off(); }
  
  monthDiv.appendChild( back_span );
  monthDiv.appendChild( front_span );
  monthDiv.appendChild( can_span );
}

function month_off ( nodeClicked ) {
  if ( nodeClicked )
    theInput.value = ( theDate.getMonth() + 1 ) + '/' + nodeClicked.innerHTML + '/' + theDate.getFullYear();
    
  theInput.disabled = false;
  
  // remove the entire calendar wrapper div from the container.
  calContainer.removeChild( calWrapper );
  
  // revert the calendar wrapper
  calContainer.className = origClass;
  monthDiv = false;
  calContainer = false;
}

function hasClass ( ele, cls ) {
  return ele.className.match( new RegExp( '(\\s|^)' + cls + '(\\s|$)' ) );
}

function addClass ( ele, cls ) {
  if ( !this.hasClass( ele, cls ) )
    ele.className += ' ' + cls;
}

function removeClass ( ele, cls ) {
  if ( hasClass( ele, cls ) ) {
    var reg = new RegExp( '(\\s|^)' + cls + '(\\s|$)' );
    ele.className = ele.className.replace( reg, ' ' );
  }
}

function selectAll ( selectID ) {
  var theSelect = document.getElementById( selectID );
  if ( theSelect ) {
    for ( var p = theSelect.length; p > 0;  ) {
      p--;
      theSelect.options[p].selected = true;
    }
  }
}
