window.addEvent('domready', function() {
  //toggles two classes viz between 'hidden' and 'visible'
  var toggleClass = function(c1, c2) {
    
    //if c1 exists, replace with c2
    //if c2 exists, replace with c1
    //if none exists, add c1
    //at any point both c1 and c2 can't exist
    
    if(this.hasClass(c1)) {
      this.removeClass(c1);
      if(c2 != '') {
        this.addClass(c2);
      }
    } else if(c2 == '' || this.hasClass(c2)) {
      if(c2 != '') {
        this.removeClass(c2);
      }
      this.addClass(c1);
    } else {
      this.addClass(c1);
    }
  }
  
  var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
  
  //add onclick event to 'find out how' buttons
  
  var fixTabHeight = function() {
    mainContent = this.getElements('div.tabMainContent')[0];
    tabForm = this.getElements('div.tabForm')[0];
    this.setStyle('height', mainContent.getSize().y + tabForm.getSize().y + 'px');
  }
  
  if($('tabsWrapper')) {
    //initially all 'reveal' anchorsare 'open' to support browsers without javascript
    //set all tabs close upon domready
    
    $$('#tabsWrapper h3.tabHeader').each(function(tabHeader) {
      openSpan = tabHeader.getElements('span.reveal.open')[0];
      toggleClass.run(['open', 'close'], openSpan);
    });
    
    $$('#tabsWrapper div.tab.open').each(function(tabDiv) {
      toggleClass.run(['open', 'close'], tabDiv);
    });
    
    //div.findOutHow has been hidden initially for browsers without javasript
    $$('#tabsWrapper div.tabForm div.findOutHow').each(function(findout) {
      toggleClass.run(['open', 'close'], findout);
    });
    
    $$('#tabsWrapper div.tabForm div.form').each(function(formdiv) {
      toggleClass.run(['open', 'close'], formdiv);
    });
    
    $$('#tabsWrapper h3.tabHeader').each(function(tabHeader) {
      tabHeader.addEvent('click', function() {
        $$('#tabsWrapper h3.tabHeader span.reveal.open').each(function(eachSpan) {
          toggleClass.run(['open', 'close'], eachSpan);
        });
        toggleClass.run(['open', 'close'], tabHeader.getElements('span.reveal')[0]);
        $$('#tabsWrapper div.tab.open').each(function(openDiv) {
          toggleClass.run(['open', 'close'], openDiv);
        });
        toggleClass.run(['open', 'close'], tabHeader.getParent());
      });
    });
    
    if(isIE6) {
      $$('span.reveal').each(function(revSpan) {
        revSpan.setStyle('padding-right', '130px');
      });
      
      $$('div.tabContent').each(function(tabDiv) {
        tabDiv.setStyle('display', 'none');
      });
    }
    
    var myAccordion = new Fx.Accordion($$('h3.tabHeader'), $$('div.tabContent'), {
      display: -1,
      link: 'cancel',
      alwaysHide: false,
      height: true,
      fixedHeight: false,
      onBackground : function(trigger, element) {
        if(isIE6) {
          element.setStyle('display', 'none');
        }
      },
      onActive : function(trigger, element) {
        if(isIE6) {
          element.setStyle('display', 'block');
        }
      }
    });
  }
  
  $$('div.findOutHow a.findOutAnchor').each(function(find) {
    find.addEvent('click', function() {
      toggleClass.run(['open', 'close'], this.getParent());
      toggleClass.run(['open', 'close'], this.getParent().getParent().getElements('div.form')[0]);
      tabContent = this.getParent('div.tabContent');
      fixTabHeight.run([], tabContent);
      return false;
    });
  });
  
  if(isIE6) {
    //if crap, make p tags with class 'msg' hidden
    $$('p.msg').each(function(pmsg) {
      pmsg.setStyle('display', 'none');
    });
  } else {
    var myAccordion2 = new Fx.Accordion($$('input.txt'), $$('p.msg'), {
      display: -1,
      link: 'cancel',
      alwaysHide: false,
      trigger: 'focus'
    });
  }
  
  var validate = function(msg) {
    var err = false;
    if(this.get('value').trim() == '' || this.get('value') == msg + this.get('title')) {
      err = true;
      retmsg = 'The field ' + this.get('title') + ' cannot be left empty';
    } else if(this.get('name') == 'email' && !(this.value.match(/^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$/))) {
      err = true;
      retmsg = 'Please enter a valid email id';
    } else if(this.get('name') == 'phone' && !this.value.test(/^[-\+\s\.\(\)0-9]{8,}$/)) {
      err = true;
      retmsg = 'Please enter a valid phone number';
    }
    
    if(err) {
      this.addClass('error');
      if(this.hasClass('noerror')) {
        this.removeClass('noerror');
      }
    } else {
      retmsg = null;
      this.addClass('noerror');
      if(this.hasClass('error')) {
        this.removeClass('error');
      }
    }
    return retmsg;
  }
  
  //if 'tabMsg' div exists, add event to hide it after 5 secs
  $$('div.tabMsg').each(function(eachMsg) {
    eachMsg.addEvent('domready', function() {
      this.setStyle.delay(15000, this, ['display', 'none']);
    });
  });
  
  //form related script
  $$('form.contactDetails').each(function(eachForm) {
    msg = 'Please type your ';
    eachForm.getElements('.txt').each(function(inp) {
      inp.addEvent('blur', function() {
        fixTabHeight.delay(480, this.getParent('div.tabContent'), []);
        toggleClass.run(['selected', ''], this);
        if(this.value.trim() == '') {
          this.value = ((this.tagName == 'TEXTAREA') ? '' : msg) + this.get('title');
          this.addClass('empty');
        }
        validate.run([msg], this);
        if(myAccordion2) {
          myAccordion2.display(-1);
        }
      });
      inp.addEvent('focus', function() {
        toggleClass.run(['selected', ''], this);
        if(this.value == msg + this.get('title') || this.value == this.get('title')) {
          this.value = '';
          this.removeClass('empty');
        }
        fixTabHeight.delay(480, this.getParent('div.tabContent'), []);
      });
    });
    
    eachForm.addEvent('submit', function() {
      errmsg = '';
      count = 0;
      this.getElements('.txt').each(function(inp) {
        ret = validate.run([msg], inp);
        if(ret) {
          count++;
          if(count > 1) {
            errmsg += '\n';
          }
          errmsg += count + '. ' + ret;
        }
      });
      if(errmsg != '') {
        alert(errmsg);
        return false;
      } else {
        return true;
      }
    });
  });
});
