
if( typeof jQuery != 'undefined' )
{
  var $j = jQuery.noConflict();
}

function setValue( id, value )
{
  var o = objById( id );
  o.value = value;
}

function objById( id )
{
  if( document.getElementById )
    return document.getElementById( id );

  if( document.all )
    return document.all[ id ];

  if( document.layers )
    return document.layers[ id ];
}

function swapMouseover( element )
{
  if( element == 1 )
  {
    document.getElementById("swapImg1").src= "/images/icon_swap_on.png";
    document.getElementById("swap1").className = "swap-on-left";
    document.getElementById("swap2").className = "swap-on-right";
  }
  else
  {
    document.getElementById("swapImg2").src="/images/icon_swap_on.png";
    document.getElementById("swap2").className = "swap-on-left";
    document.getElementById("swap3").className = "swap-on-right";
  }
}

function swapMouseout( element )
{
  if( element == 1 )
  {
    document.getElementById("swapImg1").src= "/images/icon_swap.png";
    document.getElementById("swap1").className = "";
    document.getElementById("swap2").className = "";
  }
  else
  {
    document.getElementById("swapImg2").src="/images/icon_swap.png";
    document.getElementById("swap2").className = "";
    document.getElementById("swap3").className = "";
  }
}

function switchRadioPlus( id, val )
{
  $j(document).ready( function() {
    $j.each( $j( 'div[id^=q_' + id + ']' ), function( i, obj ){
      $j( 'div[id^=q_' + $j(obj).attr( 'id' ).split( '_' )[ 1 ] + ']' ).removeClass( 'yes' ).addClass( 'no' );
    });
    $j( '#q_' + id + '_' + val ).removeClass( 'no' ).addClass( 'yes' );
  });
}

function radioGridPlusCheckAll( t, val )
{
  $j(document).ready( function() {
    $j.each( $j( 'table[id^=t_' + t + '] div' ), function( i, obj ){
      switchRadioPlus( $j(obj).attr( 'id' ).split( '_' )[ 1 ], val );
    });
  });
}

function scheduleFor( t )
{
  $j(document).ready(function()
  {
    if( t == 's' )
    {
      $j('#visit_confirm_emails').fadeIn('slow');
      $j('#candidate_reasons_table').hide();
      $j('#subject_reasons_table').fadeIn('slow');
      $j('#visit_candidate_header').hide();
      $j('#visit_subject_header').fadeIn('slow');
      $j('#candidate_tr').hide();
      $j('#subject_tr').fadeIn('slow');
    }
    else
    {
      $j('#visit_confirm_emails').fadeOut('slow');
      $j('#subject_reasons_table').hide();
      $j('#candidate_reasons_table').fadeIn('slow');
      $j('#visit_subject_header').hide();
      $j('#visit_candidate_header').fadeIn('slow');
      $j('#subject_tr').hide();
      $j('#candidate_tr').fadeIn('slow');
    }
  });
}

function radioGridCheckAll( t, b )
{
  $j(document).ready( function() {
    $j.each( $j( 'table.radiogrid#t_' + t + ' td div' ), function( i, obj ){
      switchRadio( $j(obj).attr( 'id' ).match( /\d+/ ), b );
    });
  });
}

function switchRadio( id, b )
{
  $j(document).ready( function() {
    var y = $j( '#q_' + id + '_y' );
    var n = $j( '#q_' + id + '_n' );
    var h = $j( "input[id^=fq_" + id + "]" );
    if( b == 'y' )
    {
      y.removeClass( 'no'  ).addClass( 'yes' );
      n.removeClass( 'yes' ).addClass( 'no'  );
      h.val( 'Y' );
    }
    else
    {
      y.removeClass( 'yes' ).addClass( 'no'  );
      n.removeClass( 'no'  ).addClass( 'yes' );
      h.val( 'N' );
    }
  });
}

function expandOne( id )
{
  $j(document).ready( function() {
    $j( 'table#sf' + id + ' tr' ).fadeIn( 'slow' );
    replaceExpandImg( id );
  });
}

function collapseOne( id )
{
  $j(document).ready( function() {
    $j( 'table#sf' + id + ' tr' ).not( 'tr[class^=section]' ).fadeOut( 'slow' );
    replaceCollapseImg( id );
  });
}

function expandAll()
{
  $j(document).ready( function() {
    $j( 'table.dform tr' ).fadeIn( 'slow' );
    $j.each( $j( 'div[id^=ec]' ), function( i, obj ){
      replaceExpandImg( $j(obj).attr( 'id' ).match( /\d+/ ) );
    });
  });
}

function collapseAll()
{
  $j(document).ready( function() {
    $j( 'table.dform tr' ).not( 'tr[class^=section]' ).fadeOut( 'slow' );
    $j.each( $j( 'div[id^=ec]' ), function( i, obj ){
      replaceCollapseImg( $j(obj).attr( 'id' ).match( /\d+/ ) );
    });
  });
}

function replaceCollapseImg( id )
{
  $j( 'div#ec' + id ).replaceWith( '<div id="ec' + id + '" class="expand_collapse"><a href="javascript:void(0);" onclick="javascript:expandOne(' + id + ');"><img src="/images/expand.gif" alt="Expand" title="Expand" /></a></div>' );
}

function replaceExpandImg( id )
{
  $j( 'div#ec' + id ).replaceWith( '<div id="ec' + id + '" class="expand_collapse"><a href="javascript:void(0);" onclick="javascript:collapseOne(' + id + ');"><img src="/images/collapse.gif" alt="Collapse" title="Collapse" /></a></div>' );
}

function collapseOthers( id )
{
  $j(document).ready( function() {
    $j( 'table.dform tr' ).not( 'tr[class^=section]' ).hide();
    $j.each( $j( 'div[id^=ec]' ), function( i, obj ){
      replaceCollapseImg( $j(obj).attr( 'id' ).match( /\d+/ ) );
    });
    $j( 'table#sf' + id + ' tr' ).show();
    replaceExpandImg( id );
  });
}

function toggleBox( id )
{
  $j(document).ready( function() {
    if( $j( '#q_' + id ).attr( 'class' ) == 'yes' )
    {
      $j( '#q_' + id ).removeClass( 'yes' );
      $j( 'input[id^=fq_' + id + '_]' ).val( 'N' );
    }
    else
    {
      $j( '#q_' + id ).addClass( 'yes' );
      $j( 'input[id^=fq_' + id + '_]' ).val( 'Y' );
    }
  });
}

function toggleCheckBox( id, value )
{
  $j(document).ready( function() {
    if( $j( '#q_' + id + '_' + value ).attr( 'class' ) == 'yes' )
    {
      $j( '#q_' + id + '_' + value ).removeClass( 'yes' );
      $j( 'input[id=fq_' + id + '_' + value + ']' ).val( '' );
    }
    else
    {
      $j( '#q_' + id + '_' + value ).addClass( 'yes' );
      $j( 'input[id=fq_' + id + '_' + value + ']' ).val( value );
    }
  });
}

function renderGraph(graphName) {
  // call $j.get(url, graphName) and get an array of objects to load
        var ascertained = [[0, 3], [4, 8], [8, 5], [9, 13], [2, 5], [10, 3], [7, 11]];
        var enrolled = [[0, 3], [4, 8], [8, 5], [9, 13]];
        $j.plot($j("#graph_content"), [
                {
                        data: ascertained,
                        label: "Ascertained",
                        bars: { show: true }
                },
                {
                        data: enrolled,
                        label: "Enrolled",
                        bars: { show: true }
                }], {
                        legend: {
                                show: true,
                                position: "ne"
                        }
                });
}
