$(document).observe('dom:loaded', function(){
  

  // Select all textboxes and assign them to an array
  var textboxes = $$('form.awesome input.input-text');

  // Iterate through all textboxes in the form
  textboxes.each(function(input, index){
		
    var label = input.previous();
	
	// Check for autofilled text and empty the label if found (fix for autofill and back artifacts)
	if (index == 0){
		new Form.Observer(input.up('form'), 0.1, function(form, value) {
			textboxes.each(function(inputX){
				if (!inputX.value.empty()) {
					inputX.previous().addClassName('hastext');
				}
			});
		});
	}


    // Fade the label back when a field gains focus		
    input.onfocus = function(){
      if (input.value.empty()){
        label.addClassName('focus');            
      }
    }

    // Check if a field is empty when the user switches out
    input.onblur = function(){
      if (input.value.empty()){
        label.removeClassName('focus').removeClassName('hastext');          
      }
    }

    // Fade the label back if a field has text		
    if (!input.value.empty()) {
      label.addClassName('hastext');
    }

    // Fade the label back when the user starts to type		
    input.onkeypress = function(){
      label.addClassName('hastext');
    };
  });

  var loginBoxes = $$('form#loginForm input.labeled');
  
  loginBoxes.each(function(input, index){
  	var label = input.previous();
  	
  	if (index == 0){
  		new Form.Observer('loginForm', 0.1, function(form, value) {
  			loginBoxes.each(function(inputX){
  				if (!inputX.value.empty()) {
  					inputX.previous().addClassName('hidden');
  				}
  			});
  		});
  	}
  
  	// Fade the label back when a field gains focus		
    input.onfocus = function(){
      if (input.value.empty()){
        label.addClassName('hidden');            
      }
    }

		// Check if a field is empty when the user switches out
		input.onblur = function(){
		  if (input.value.empty()){
		    label.removeClassName('hidden');          
		  }
		}

		// Fade the label back if a field has text		
		if (!input.value.empty()) {
		 	label.addClassName('hidden');
		}
	});
  
  
  
  var nameBoxes = $$('form input.name');
  
  nameBoxes.each(function(input, index){
  	var initialValue = input.value;
  	
  	// Fade the label back when a field gains focus		
    input.onfocus = function(){
      if (!(input.value.empty())){
        input.value = '';            
      }
    }

    // Check if a field is empty when the user switches out
    input.onblur = function(){
      if (input.value.empty()){
        input.value = initialValue;          
      }
    }
    
  });
  
  
  
  
  
  $$('input[type="radio"]').each(function(radio){
  	radio.observe('click', function(){
  		switch(radio.name){  		
  			case "married":
  				(radio.value == 'yes') ? (Effect.Appear('marriedDependency')) : (Effect.Fade('marriedDependency'));
  				break;
  			case "children":
  				if(radio.value == 'yes')
                {
                    Effect.Appear('childrenDependency1');
                    Effect.Appear('livingArrangement1');
                    Effect.Appear('childrenDependency2')
                }
                else
                {
                    Effect.Fade('childrenDependency1');
                    Effect.Fade('livingArrangement1');
                    Effect.Fade('childrenDependency2')
                }                                                                                                       
  				break;
  			case "language":
  				(radio.value == 'yes') ? (Effect.Appear('languageDependency')) : (Effect.Fade('languageDependency'));
  				break;
  		}
  	});
  });
  
  $$('a.more').each(function(link, index){
		link.onclick = function() {
			link.hide().next().show();
			return false;
		};
	});
  
  
});
