/*
File: internship/scripts/intern-submit-global-script.js

MAKE SURE YOU KEEP TRACK OF EVERYTHING YOU DO!

Author: Dwight VanTuyl
DD-MMM-YYYY: 14-Mar-2008
Purpose: Useful functions for all submit form tabs.
+++++++++++++
Modifications
+++++++++++++
Editor:		
DD-MMM-YYYY:
Watcha did:	
~~~~~~~~~~~~~
*/

Ext.namespace('Intern.submit');

Intern.submit.global = {

  init: function() {
    
    //Generate tabs from local html divs
    this.tabContainer = new Ext.TabPanel('formtabs');
    this.tabs = $("div.tab");
    this.tabs.each(function(){
      Intern.submit.global.tabContainer.addTab( $(this).attr('id'), $(this).attr('title'));
    });
    this.tabContainer.activate(0);
    
    //Render date fields
  $("input.dateField").each(function(){
      $(this).datepicker();
    });
    
    //Render resizeable textfields
    $('textarea.resizable:not(.processed)').TextAreaResizer();
    
    //Render all help buttons
    $("span.help").each(function(){
      var helpText = $(this).text();
      $(this).replaceWith('<input class="helpBtn" type="button" value="?" msg="'+helpText+'"/>');
    });
    $("input.helpBtn:button").each(function(){
      $(this).click(function(){
        Ext.Msg.alert('Help', $(this).attr('msg'));
      });
    });
    
    //Render all example links
    $("span.example").each(function(){
      var helpText = $(this).text();
      $(this).replaceWith('<a href="###" class="exampleLink" msg="'+helpText+'">Example<\/a>');
    });
    $("a.exampleLink").each(function(){
      $(this).click(function(){
        Ext.Msg.alert('Example', $(this).attr('msg'));
      });
    });
    
    /*Show USA state select or editable field based on whether country is 'USA' 
              - Both selectRegion and textRegion must have two class names:
              - First class is the selectCountry's name attribute. 
              - Second class is the the type of field that is used to either show or hide. Must be either selectRegion or textRegion*/
    $("select.selectCountry").change(function(){
      var group = $(this).attr('name');
        if($(this).val().match('USA')){
          $("select[class='"+group+" selectRegion']").show();
          $("input[class='"+group+" textRegion']").hide();
        }else{
          $("input[class='"+group+" textRegion']").show();
          $("select[class='"+group+" selectRegion']").hide();               
        }
    });
  }, 
  
  //Listen to "Go to Step #" buttons
  gotoTab: function(tabNum){
    var activeTab = Intern.submit.global.tabContainer.getActiveTab();
    if(Intern.submit.validate.validateTab(activeTab.id)){
      this.tabContainer.activate(tabNum-1);
    }
  },
  
  //Confirm navigating away from page
  setConfirmUnload: function(on){

     //var message = 'Your announcement has not been submitted yet. If you navigate away from this page, all changes will be lost.';
     //window.onbeforeunload = (on) ? message : null;
  }
}

Ext.onReady(Intern.submit.global.init, Intern.submit.global);