function addHover ( theSeat, theType, ticketType ) {
  var theRow = document.getElementById( 'type_row_' + theType );
  if ( theRow )
    AddClassName( theRow, 'hover_row', true );
  if ( ticketType == 'season' ) {
    var typeInput = document.getElementById( 'type' );
    if ( typeInput.value == 'Flex' )
      return;
  }
  addClassToClass( 'seat_col_' + theSeat, 'hover_col', 'ticket_form', 'td' );
}

function remoHover ( theSeat, theType ) {
  removeClassFromClass( 'seat_col_' + theSeat, 'hover_col', 'ticket_form', 'td' );
  var theRow = document.getElementById( 'type_row_' + theType );
  if ( theRow )
    RemoveClassName( theRow, 'hover_row' );
}

function hoverConcert ( theDiv, ticketType ) {
  if ( ticketType == 'season' ) {
    var typeInput = document.getElementById( 'type' );
    if ( typeInput.value != 'Mini Season' )
      return;
  }
  AddClassName( theDiv, 'hover_concert');
}

function removeHoverConcert ( theDiv, ticketType ) {
  RemoveClassName( theDiv, 'hover_concert' );
}

function selectConcert ( theDiv, ticketType, theConcert ) {
  if ( ticketType == 'season' ) {
    var typeInput = document.getElementById( 'type' );
    if ( typeInput.value == 'Mini Season' ) {
      var concertHolder;
      var turnOff = false;
      var emptyHolder = false;
      var numConcerts = 0;
      for ( var i = 1; i <= 4; i++ ) {
        concertHolder = document.getElementById( 'miniSEASON_' + i );
        if ( concertHolder.value == theConcert ) {
          turnOff = true;
          numConcerts++;
          concertHolder.value = '';
          if ( !emptyHolder )
            emptyHolder = concertHolder;
        }
        else if ( concertHolder.value == '' ) {
          numConcerts++;
          if ( !emptyHolder )
            emptyHolder = concertHolder;
        }
      }
      if ( turnOff )
        RemoveClassName( theDiv, 'current_concert' );
      else {
        if ( emptyHolder ) {
          emptyHolder.value = theConcert;
          numConcerts--;
          AddClassName( theDiv, 'current_concert', true );
        }
        else
          alert( 'You may only select 4 concerts. Please deselect one before adding this one.' );
      }
      if ( numConcerts ) {
        document.getElementById( 'mini_add' ).style.display = '';
        document.getElementById( 'mini_specifics' ).innerHTML = numConcerts + ( ( numConcerts < 4 ) ? ' remaining Concert' + ( ( numConcerts > 1 ) ? 's' : '' ) : ' Concerts' );
        if ( numConcerts < 4 )
          document.getElementById( 'mini_remove' ).style.display = '';
        else
          document.getElementById( 'mini_remove' ).style.display = 'none';
      }
      else {
        document.getElementById( 'mini_add' ).style.display = 'none';
        document.getElementById( 'mini_remove' ).style.display = '';
      }
    }
  }
  else { // single
    var concertInput = document.getElementById( 'concert' );
    if ( concertInput.value == theConcert )
      return;
      
    if ( concertInput.value.length ) {
      var theOldDiv = document.getElementById( 'concert_' + concertInput.value );
      RemoveClassName( theOldDiv, 'current_concert' ); 
    }
    concertInput.value = theConcert;
    AddClassName( theDiv, 'current_concert', true );
  }
}

function updateTotal ( ticketType ) {
  var theQuantity = document.getElementById( 'quantity' ).value.replace( /[^0-9]/, '' );
  if ( !parseInt( theQuantity ) || parseInt( theQuantity ) == 'NaN' )
    theQuantity = 0;
  var theSeat = document.getElementById( 'seat' ).value;
  var theType = document.getElementById( 'type' ).value;
  var thePrice = 0;
  if ( theType == 'Flex' )
    thePrice = parseFloat( document.getElementById( 'price_' + theType ).innerHTML.substring( 1 ) );
  else {
    if ( theSeat && theType )
      thePrice = parseFloat( document.getElementById( 'price_' + theType + '_' + theSeat ).innerHTML.substring( 1 ) );
  }
  if ( ticketType == 'season' ) {
    if ( document.getElementById( 'new' ).checked )
      thePrice /= 2;
  }
  document.getElementById( 'total_holder' ).innerHTML = toCurrency( ( thePrice * theQuantity ) );
}

function toCurrency ( theValue ) {
  return '$' + addCommas( theValue.toFixed( 2 ) );
}

function addCommas ( nStr ) {
  nStr += '';
  x = nStr.split( '.' );
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while ( rgx.test( x1 ) )
    x1 = x1.replace( rgx, '$1' + ',' + '$2' );
  return x1 + x2;
}

function setSeat ( theSeat, ticketType ) {
  if ( ticketType == 'season' ) {
    var typeInput = document.getElementById( 'type' );
    if ( typeInput.value == 'Flex' )
      return;
  }
  var seatInput = document.getElementById( 'seat' );
  if ( seatInput.value == theSeat )
    return;
    
  if ( seatInput.value.length )
    removeClassFromClass( 'seat_col_' + seatInput.value , 'active_col', 'ticket_form', 'td' ); 
  seatInput.value = theSeat;
  addClassToClass( 'seat_col_' + theSeat, 'active_col', 'ticket_form', 'td' );
  updateTotal( ticketType );
}

function setType ( theType, ticketType ) {
  var typeInput = document.getElementById( 'type' );
  if ( typeInput.value == theType )
    return;
    
  if ( typeInput.value.length ) {
    var theOldRow = document.getElementById( 'type_row_' + typeInput.value );
    RemoveClassName( theOldRow, 'active_row' ); 
  }
  typeInput.value = theType;
  var theRow = document.getElementById( 'type_row_' + theType );
  AddClassName( theRow, 'active_row', true );
  if ( ticketType == 'season' ) {
    if ( theType == 'Flex' )
      removeClassFromClass( '', 'active_col', 'ticket_form', 'td' );
    if ( theType == 'Mini Season' ) {
      document.getElementById( 'mini_season_instructions' ).style.display = '';
      for ( var i = 1; i <= 4; i++ ) {
        concertHolder = document.getElementById( 'miniSEASON_' + i );
        if ( concertHolder.value ) {
          concertDiv = document.getElementById( 'concert_' + concertHolder.value );
          if ( concertDiv )
            AddClassName( concertDiv, 'current_concert' );
          else
            concertHolder.value = '';
        }
      }
    }
    else {
      document.getElementById( 'mini_season_instructions' ).style.display = 'none';
      removeClassFromClass( '', 'current_concert', 'ticket_form', 'div' );
    }
  }
  else { // single
    // hide and show the concert divs:
    var hideClass;
    var showClass = 'type' + theType;
    if ( theType == 'Pops' ) 
      hideClass = 'typeClassical';
    else
      hideClass = 'typePops';
    addClassToClass( hideClass, 'hidden', 'ticket_form', 'div' );
    removeClassFromClass( showClass, 'hidden', 'ticket_form', 'div' );
    
  }
  updateTotal( ticketType );
}

function addClassToClass ( searchClass, extraClass, node, tag ) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  else if ( typeof( node ) == 'string' )
    node = document.getElementById( node );
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName( tag );
  var elsLen = els.length;
  for ( i = 0; i < elsLen; i++ ) {
    if ( HasClassName( els[i], searchClass ) )
      AddClassName( els[i], extraClass, true );
  }  
}

function removeClassFromClass ( searchClass, removeClass, node, tag ) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  else if ( typeof( node ) == 'string' )
    node = document.getElementById( node );
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName( tag );
  var elsLen = els.length;
  for ( i = 0; i < elsLen; i++ ) {
    if ( ( searchClass.length == 0 || HasClassName( els[i], searchClass ) ) && HasClassName( els[i], removeClass  ) )
      RemoveClassName( els[i], removeClass );
  }  
}

// ----------------------------------------------------------------------------
// HasClassName
//
// Description : returns boolean indicating whether the object has the class name
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
function HasClassName(objElement, strClass)
   {

   // if there is a class
   if ( objElement.className )
      {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // get uppercase class for comparison purposes
      var strClassUpper = strClass.toUpperCase();

      // find all instances and remove them
      for ( var i = 0; i < arrList.length; i++ )
         {

         // if class found
         if ( arrList[i].toUpperCase() == strClassUpper )
            {

            // we found it
            return true;

            }

         }

      }

   // if we got here then the class name is not there
   return false;

   }
// 
// HasClassName
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// AddClassName
//
// Description : adds a class to the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
function AddClassName(objElement, strClass, blnMayAlreadyExist)
   {

   // if there is a class
   if ( objElement.className )
      {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // if the new class name may already exist in list
      if ( blnMayAlreadyExist )
         {

         // get uppercase class for comparison purposes
         var strClassUpper = strClass.toUpperCase();

         // find all instances and remove them
         for ( var i = 0; i < arrList.length; i++ )
            {

            // if class found
            if ( arrList[i].toUpperCase() == strClassUpper )
               {

               // remove array item
               arrList.splice(i, 1);

               // decrement loop counter as we have adjusted the array's contents
               i--;

               }

            }

         }

      // add the new class to end of list
      arrList[arrList.length] = strClass;

      // add the new class to beginning of list
      //arrList.splice(0, 0, strClass);
      
      // assign modified class name attribute
      objElement.className = arrList.join(' ');

      }
   // if there was no class
   else
      {

      // assign modified class name attribute      
      objElement.className = strClass;
   
      }

   }
// 
// AddClassName
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// RemoveClassName
//
// Description : removes a class from the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to remove
//
function RemoveClassName(objElement, strClass)
   {

   // if there is a class
   if ( objElement.className )
      {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // get uppercase class for comparison purposes
      var strClassUpper = strClass.toUpperCase();

      // find all instances and remove them
      for ( var i = 0; i < arrList.length; i++ )
         {

         // if class found
         if ( arrList[i].toUpperCase() == strClassUpper )
            {

            // remove array item
            arrList.splice(i, 1);

            // decrement loop counter as we have adjusted the array's contents
            i--;

            }

         }

      // assign modified class name attribute
      objElement.className = arrList.join(' ');

      }
   // if there was no class
   // there is nothing to remove

   }
// 
// RemoveClassName
// ----------------------------------------------------------------------------
  
