var currentsection = '';
var currenttab = '';
var currentsb = '';
var ajaxurl = 'ajax.php';
var myAjax = null;
var myAjaxSB = null;

var ajax_indato = function(idx, ato, txt) {
  var ai = $('ajax_indicator_'+idx);
  
  if ($defined(ai)) {
    
    if (txt) 
      ai.setHTML(txt);
    
    ai.alphafx = ai.alphafx || ai.effect('opacity', {wait: false, duration: 300});
    ai.alphafx.start(ato);
  }  
}

var ajax_buttonstate = function(idx, sts) {
  var ss = $(idx);
  ss.disabled = sts;
  
  if (sts)
    ss.setStyle('opacity', 0.30);
  else
    ss.setStyle('opacity', 1.00);
}

function axChangeSection(asection, atab) {
  var aun = 1;
  var elm = null;
  var asb = axChangeSection.arguments.length > 2 ? axChangeSection.arguments[2] : '';
  var tabbed = axChangeSection.arguments.length > 3 ? axChangeSection.arguments[3] : true;
  var aaction = axChangeSection.arguments.length > 4 ? axChangeSection.arguments[4] : '';
  
  if (tabbed && atab && (currentsection == asection)) {
    aun = 0;    
    elm = $('contentband');
    
    if (currenttab == atab) {
      
    } else {
      var tbs = $$('ul.tabs li');
      // alert(tbs.length);
      var nextnodiv = true;
      
      if (currenttab) {
        el = $(currenttab);
        if (el) 
          el.removeClass('active');
      }
      
      $each(tbs, function(el) {
        if (nextnodiv) {
          el.addClass('nodiv');
          nextnodiv = false;
        }
        else 
          el.removeClass('nodiv');
        
        if (el.id == atab) {
          el.addClass('active');
          nextnodiv = true;
        }
        else
          el.removeClass('active');
      });
      
      // graphically change the active tab
    }     
  }
  else
    elm = $('mainband');
    
  var maheight = $('sideband').getStyle('height');
  elm.setHTML('<div class="bigindicator" style="height:'+maheight+'">&nbsp;</div>');    
  
  myAjax = myAjax || new Ajax(ajaxurl, {autoCancel: true, evalScripts: true, method: 'post'});
  myAjax.options.data = {job: 'content', section: asection, tab: atab, sb: asb, updatenav: aun, action: aaction};
  myAjax.options.update = elm;
  myAjax.request();
  
  if (currentsb != asb) {
    myAjaxSB = myAjaxSB || new Ajax(ajaxurl, {autoCancel: true, evalScripts: true, method: 'post', update: $('sideband')});
    myAjax.options.data = {job: 'sideband', section: asection, tab: atab, sb: asb, updatenav: aun};
    myAjaxSB.request();
  } 
  
  currentsection = asection;
  currenttab = atab;
  currentsb = asb;
  
  return false;
}

var ajaxContactFormFailure = function() {
  ajax_indato('flower', 0.00);
  ajax_indato('status', 1.00, 'The message could not be delivered.');
  ajax_indato.delay(5000, this, ['status', 0.00]);
  ajax_buttonstate('submit-contact-us-form', false);
}

var ajaxContactFormSuccess = function() {
  ajax_indato('flower', 0.00);
  ajax_indato('status', 1.00, 'Thank You for Your Message!');
  ajax_indato.delay(5000, this, ['status', 0.00]);
  ajax_buttonstate('submit-contact-us-form', false);
  
  document.forms['contactusform'].reset();
}


var ajaxContactFormSubmit = function(form) {
  if (validateForm(form)) {
    objForm = $(form);
    
    ajax_indato('flower', 1.00);
    ajax_buttonstate('submit-contact-us-form', true);
    
    objForm.send({onFailure: ajaxContactFormFailure, onComplete: ajaxContactFormSuccess, 
      update: null, evalResponse: true, evalScripts: false});
  }
    
  return false;
}
