/* 
* Forms.js
*
* Michael Harrison, for ExchangePlace. 3/18/2008
* mh@michaelharrison.ws
*
* Depends on prototype.js 1.6: http://www.prototypejs.org/api
*
*/

// Uses prototype.js extensions to define JS pseudoclass object in JSON notation
Overlays = Class.create(
{
  initialize: function() { // initialize(args) is called when 'new Overlays(args)' executes
    this.buttons = $A();
    this.overlays = $A();
    this.states = {'btn': null, 'ovr': null};
  }, // end 'initialize' definition
  
  closeOverlays: function() {
    this.overlays.each(function(o) {
       o.hide();
    });
  }, // end 'closeOverlays' definition
  
  overlayFor: function(id) {
    return $('overlay_' + id);
  }, // end 'overlayFor' definition


  notify: function(params) {
    Object.extend(this.states, params); // overwrite one of the two states values
  }, // end 'notify' definition
  
  // reset the visibility or behavior of elements on the page now that overlays are inactive.
  resetPage: function() {
    
  }, // end 'resetPage' definition

  setIds: function(button_ids) {
    $A(button_ids).each(function(id) {
      if ($(id) && this.overlayFor(id)) {
        var btn = $(id);
        var ovr = this.overlayFor(id);
        
        this.buttons.push(btn);
        this.overlays.push(this.overlayFor(id));
        
        Element.observe(btn, "mouseover", 
    	    function() {
    	      this.notify({'btn': id});
    	      this.showOverlay(id);
    	      btn.addClassName("rollover");
    	    }.bind(this));
        
        Element.observe(ovr, "mouseover", 
    	    function() {
    	      // alert("mouseover: " + ovr.id);
    	      this.notify({'ovr': id});
    	    }.bind(this));
    	    
        Element.observe(btn, "mouseout", 
    	    function() {
    	      this.notify({'btn': null});
    	      btn.removeClassName("rollover");
    	      this.waitAndUpdate();
    	    }.bind(this));
        
        Element.observe(ovr, "mouseout", 
    	    function() {
    	      this.notify({'ovr': null});
    	      this.waitAndUpdate();
    	    }.bind(this));    
      }
    }.bind(this));
  }, // end 'setIds' definition
  
  waitAndUpdate: function() {
    setTimeout(function() { this.update(); }.bind(this), 350);
  }, // end 'waitAndUpdate' definition
  
  // set visibility or behavior of any items on page so they won't interfere with overlays.
  // Flash and (in IE6) select form controls in particular need to be hidden
  setPageForOverlays: function() {
    
  }, // end 'setPageForOverlays' definition  
  
  showOverlay: function(id) {
    this.setPageForOverlays();
    this.closeOverlays();
    this.overlayFor(id).show();
  }, // end 'showOverlay' definition
  
  // if no mouse over events registered for button or overlay, close overlays and reset page
  update: function() {
    if (!(this.states.btn || this.states.ovr)) {
      this.closeOverlays();
      this.resetPage();
    }
  } // end 'update' definition
  
});
	
var overlays_control = new Overlays();

function readParam(name_string) {
  var pattern = new RegExp("[\?\&]" +name_string+ "=([^\&]+)");
  if (pattern.exec(document.location)) {
    var results = pattern.exec(document.location);
    return results[1];
  }
  return false;
}

/* Modified below for America Shops project */

var events=function() {
  // control privacy policy overlay
  overlays_control.setIds(["privacy"]);
  
  // make Diabetes-dependent fields appear when appropriate
  if ($('diabetes_1')) {
	  var toggleDiabetesQs = function() {
	  	if($('diabetes_1').checked) {
	  		Element.show('DiabetesAgeGroupYou');
	  		Element.hide('DiabetesAgeGroupOther');
	  		Element.show('DiabetesFrequencyYou');
	  		Element.hide('DiabetesFrequencyOther');
	  	} else if ($('diabetes_2').checked) {
	  		Element.hide('DiabetesAgeGroupYou');
	  		Element.show('DiabetesAgeGroupOther');
	  		Element.hide('DiabetesFrequencyYou');
	  		Element.show('DiabetesFrequencyOther');
	  	} else {
	  		Element.hide('DiabetesAgeGroupYou');
	  		Element.hide('DiabetesAgeGroupOther');
	  		Element.hide('DiabetesFrequencyYou');
	  		Element.hide('DiabetesFrequencyOther');
	  	}
	  }
  		
  Element.observe($('diabetes_0'), "click", toggleDiabetesQs);
  Element.observe($('diabetes_1'), "click", toggleDiabetesQs);
  Element.observe($('diabetes_2'), "click", toggleDiabetesQs);

	}
	
	// Make follow-up question mandatory or not
	if ($('T675_0000Z_2')) { // actually checks for the "No" radio button, only present on first page
		
		var toggleFollowUp = function() {
	  	if($('T675_0000Z').checked) {
	  		requireAnswer($('survey_form'), "X675_00001");
	  	} else {
	  		unrequireAnswer($('survey_form'), "X675_00001");
	  	}
	  }
	  
	  Element.observe($('T675_0000Z'), "change", toggleFollowUp);
	  Element.observe($('T675_0000Z_2'), "change", toggleFollowUp);
		
	}
}

