/**
 * Base JavaScript file for undergraduate application. Contains initialization and helper functions used by other
 * scripts.
 */

(function() { // Start of closure idiom to provide private variable scoping

   // Global variables
   request = null;
   AppForm = null; // Form validation
   var applicationDomestic = true; // boolean indicating whether this is a domestic or international application
   otherNamesController = null;
   emgContactController = null;
   domCollegeController = null;
   logger = null;
   WebappID = null; // The ID number assigned to this application

   // Shortcut YUI definitions
   var Dom = YAHOO.util.Dom;
   var $ = Dom.get;
   var addClass = Dom.addClass;
   var hasClass = Dom.hasClass;
   var removeClass = Dom.removeClass;
   var setStyle = Dom.setStyle;
   var query; // YAHOO.util.Selector.query;

   var LOG;
   var oDate1 = { srv: true };
   var initstarted = false;
   var ctxEval;
   var state;
   var feedata;


   var onCompleteHide = function() {
      // console.log(this, arguments);
      addClass(this.getEl(), 'inactive');
   };

   hide = function(el) {
      // hideanim.setEl(el);
      // hideanim.animate();
      // hideanim.onComplete.subscribe(onCompleteHide);
      addClass(el, 'inactive');
   };

   show = function(el) {
      removeClass(el, 'inactive');
      // showanim.setEl(el);
      // showanim.animate();
   };



   var watchForm = function(e) {
      var target, formname, value;

      target = YAHOO.util.Event.getTarget(e, 1);
      LOG.log("event " + e.type + " received for element " + target.id, "debug");

      try {
         // console.log(target);
         if (target.nodeName === 'INPUT' || target.nodeName === 'SELECT' || target.nodeName === 'TEXTAREA') {
            // formprefix = (target.form.name || "form") + ".";
            formprefix = "";
            value = target.value;
            if (target.type === 'checkbox' && !target.checked) {
               value = null;
            }
            APP.setValue(formprefix + target.name, value);
         }
      } catch (ex) {
         // Nothing. We'll get here if target.form doesn't exist,
         // but that seems unlikely.
      }
   };


   var initAfterInsert = function() {
      LOG = new YAHOO.widget.LogWriter("app");
      request = KSU.util.parseRequest();
      if (request['log'] == 'y') {
         logger = new YAHOO.widget.LogReader();
         if (typeof console !== "undefined" && typeof console.log === "function") {
            YAHOO.widget.Logger.enableBrowserConsole();
         }
      }


      LOG.log("Initialization started", "info");

      query = YAHOO.util.Selector.query;

      ctxEval = new KSU.expression.Evaluator();

      APP.setValue("method", "neither");

      APP.watchFields();

      APP.updateVisible();
         
   };


   APP = {


      watchFields: function(root) {

         var nodes, i, n, watchfor;

         nodes = query('input, select, textarea', root);

         for (i = 0; i < nodes.length; i++) {
            n = nodes[i];
            if (n.type === 'radio' || n.type === 'checkbox') {
               watchfor = 'click';
            } else {
               watchfor = 'change';
            }

            YAHOO.util.Event.addListener(n, watchfor, watchForm, this, true);
         }

      },

      isLogEnabled: function() {
         return request && request.log === 'y';
      },


      updateVisible: function(root) {
         KSU.dynamic.Visible.update(root, ctxEval);
      },

      setValue: function(name, val) {
         ctxEval.setValue(name, val);
      },

      getValue: function(name) {
         return ctxEval.getValue(name);
      },

      isYes: function(name) {
         return ctxEval.getValue(name) === 'Y';
      },

      /**
       * Displays an element if its corresponding trigger is true
       */
      visibleIfSelected: function(e) {
         // Get source element of event
         var evt = YAHOO.util.Event.getTarget(e);

         var sSource = evt.id;
         var oSource = $(sSource);

         var oTarget = $(sSource + "_toggle");

         if (oSource.checked) {
            removeClass(oTarget, "inactive");
         } else {
            addClass(oTarget, "inactive");
         }
      },



      loader: new YAHOO.util.YUILoader( {
         require: [ "dom", "event", "connection", "selector", "logger" ],
         base: '/KSU_resources/yui/2.7.0/build/',
         loadOptional: true,
         onSuccess: initAfterInsert
      }),

      init: function() {
       //  LOG.log("initializing", "info");
         APP.loader.insert();
      }

   };



})();

window.onerror = function() {
   // console.log(arguments);
};

YAHOO.util.Event.onDOMReady(APP.init);

