$(document).ready(function(){
	var whichform = $("form");
	clearfeedback($("form"),"start");
	//validateForm(whichform,"start");
	
	//var formFields = $(':input').not($("[@type=submit]")).not($("[@value=http://]"));
	//formFields.focus(function() {
	//	if (this.value == this.defaultValue) this.value = "";
	//});
	//formFields.blur(function() {
	//	if (this.value == "") this.value = this.defaultValue;
	//});
	$("form").submit(function() {
		clearfeedback(this);
		return (validateForm(this,"submit") ? true : false);
	});
	
	//$(':input').blur(function() {
	//	clearfeedback($("form"),"blur");
	//	validateForm($("form"),"blur");
	//});
	
	function validateForm(whichform,method) {
		var errors = new Array();
		$(':input[@class*=required]',whichform).add('select.required',whichform).each(function() {
			if(!isFilled(this)) {
				//var feedback = " - " + $(this).siblings()[0].firstChild.nodeValue + " required";
				var feedback = "Required";
				errors[errors.length] = new Array(feedback,this);
			} else if (this.className.indexOf("email") != -1) {
				if (!isEmail(this)) {
					var feedback = "Invalid email";
					errors[errors.length] = new Array(feedback,this);
				}
			} else if (this.nodeName == "SELECT") {
				if (this.options[this.selectedIndex].value == 0) {
					//var feedback = " - " + $(this).siblings()[0].firstChild.nodeValue + " required";
					var feedback = "Required";
					errors[errors.length] = new Array(feedback,this);
				}
			}
			
		});
		if(errors.length != 0) {
			for(var i=0; i < errors.length; i++){
				addfeedback(errors[i][0],errors[i][1]);
			}
			if (method === "submit") {
				errors[0][1].focus();	
			}
			return false;
		} else {
			return true;	
		}
	}


  form_manager();

  function form_manager() {

	 		$('#gs_advertising .additional').hide();
			$('#gs_advertising .advertising_sources select').bind('change', function(){
				var value = $(this).val();
				switch(value) {
					case "Other" :
						$('#gs_advertising .additional').show();
					break;
					default :
						$('#gs_advertising .additional').hide();
					break;
				}
			});
  	
  		$('.ethnic_group .additional').hide();
      $('.ethnic_group .options input').bind('click', function(){
          var id = $(this)[0].getAttribute('id');
          switch(id)
          {
              case 'ethnicity_other' :
                        $('.ethnic_group .additional_other').show();
                        $('.ethnic_group .additional_white').hide();
                        $('.ethnic_group .additional_white input').val('');
                        $('.ethnic_group .additional_mixed').hide();
                        $('.ethnic_group .additional_mixed input').val('');
                        $('.ethnic_group .additional_black').hide();
                        $('.ethnic_group .additional_black input').val('');
                        $('.ethnic_group .additional_asian').hide();
                        $('.ethnic_group .additional_asian input').val('');
                        break;
                case 'ethnicity_white_other' :
                        $('.ethnic_group .additional_other').hide();
                        $('.ethnic_group .additional_other input').val('');
                        $('.ethnic_group .additional_white').show();
                        $('.ethnic_group .additional_mixed').hide();
                        $('.ethnic_group .additional_mixed input').val('');
                        $('.ethnic_group .additional_black').hide();
                        $('.ethnic_group .additional_black input').val('');
                        $('.ethnic_group .additional_asian').hide();
                        $('.ethnic_group .additional_asian input').val('');
                        break;
                case 'ethnicity_mixed_other' :
                        $('.ethnic_group .additional_other').hide();
                        $('.ethnic_group .additional_other input').val('');
                        $('.ethnic_group .additional_white').hide();
                        $('.ethnic_group .additional_white input').val('');
                        $('.ethnic_group .additional_mixed').show();

                        $('.ethnic_group .additional_black').hide();
                        $('.ethnic_group .additional_black input').val('');
                        $('.ethnic_group .additional_asian').hide();
                        $('.ethnic_group .additional_asian input').val('');
                        break;
                case 'ethnicity_black_other' :
                        $('.ethnic_group .additional_other').hide();
                        $('.ethnic_group .additional_other input').val('');
                        $('.ethnic_group .additional_white').hide();
                        $('.ethnic_group .additional_white input').val('');
                        $('.ethnic_group .additional_mixed').hide();
                        $('.ethnic_group .additional_mixed input').val('');
                        $('.ethnic_group .additional_black').show();

                        $('.ethnic_group .additional_asian').hide();
                        $('.ethnic_group .additional_asian input').val('');
                        break;
                case 'ethnicity_asian_other' :
                        $('.ethnic_group .additional_other').hide();
                        $('.ethnic_group .additional_other input').val('');
                        $('.ethnic_group .additional_white').hide();
                        $('.ethnic_group .additional_white input').val('');
                        $('.ethnic_group .additional_mixed').hide();
                        $('.ethnic_group .additional_mixed input').val('');
                        $('.ethnic_group .additional_black').hide();
                        $('.ethnic_group .additional_black input').val('');
                        $('.ethnic_group .additional_asian').show();

                        break;
                default :
                        $('.ethnic_group .additional_other').hide();
                        $('.ethnic_group .additional_other input').val('');
                        $('.ethnic_group .additional_white').hide();
                        $('.ethnic_group .additional_white input').val('');
                        $('.ethnic_group .additional_mixed').hide();
                        $('.ethnic_group .additional_mixed input').val('');
                        $('.ethnic_group .additional_black').hide();
                        $('.ethnic_group .additional_black input').val('');
                        $('.ethnic_group .additional_asian').hide();
                        $('.ethnic_group .additional_asian input').val('');
                        break;
          }
      });
      $('.religion input').bind('click', function() {
            var id = $(this)[0].getAttribute('id');
            if(id != 'religion_other_text')
                {
            if(id == 'religion_other') {
                $('.religion .additional').show();
            }
            else
            {
                $('.religion .additional').hide();
            }
                }
      });
      $('.sexual_orientation input').bind('click', function() {
            var id = $(this)[0].getAttribute('id');
            if(id != 'orientation_other_text')
            {
            if(id == 'orientation_other') {
                $('.sexual_orientation .additional').show();
            }
            else
            {
                $('.sexual_orientation .additional').hide();
            }
            }
      });

      $('.ethnic_group .additional_other').hide();
      $('.ethnic_group .additional_white').hide();
      $('.ethnic_group .additional_mixed').hide();
      $('.ethnic_group .additional_black').hide();
      $('.ethnic_group .additional_asian').hide();
      inputs = $('.ethnic_group .options input');
      for(i=0;i<inputs.length;i++) {
          if(inputs[i].checked) {
              switch($inputs[i].id)
              {
                  case 'ethnicity_other' : $('.ethnic_group .additional_other').show(); break;
                  case 'ethnicity_white_other' : $('.ethnic_group .additional_white').show(); break;
                  case 'ethnicity_mixed_other' : $('.ethnic_group .additional_mixed').show(); break;
                  case 'ethnicity_black_other' : $('.ethnic_group .additional_black').show(); break;
                  case 'ethnicity_asian_other' : $('.ethnic_group .additional_asian').show(); break;
              }
          }
      }
      inputs = $('.religion .radio_group input');
      $('.religion .additional').hide();
      for(i=0;i<inputs.length;i++) {
          if(inputs[i].checked) {
              if(inputs[i].id == "religion_other") {
                  $('.religion .additional').show();
              } else {
                  $('.religion .additional').hide();
              }
          }
      }
      inputs = $('.sexual_orientation .radio_group input');
       $('.sexual_orientation .additional').hide();
      for(i=0;i<inputs.length;i++) {
          if(typeof(inputs[i].checked) != 'undefined' && inputs[i].checked) {
              if(inputs[i].id == "orientation_other") {
                  $('.sexual_orientation .additional').show();
              } else {
                  $('.sexual_orientation .additional').hide();
              }
          }
      }
       inputs = $('#disability_info .radio_group input');
      $('#disability_info .additional').hide();
      for(i=0;i<inputs.length;i++) {
          if(inputs[i].checked) {
              switch(inputs[i].id) {
              case "disability_yes" :
                  $('#disability_info .additional').show();
              break;
              case "disability_no" :
                  $('#disability_info .additional').hide();
              break;
              }
          }
      }
      $('#disability_info .radio_group input').bind('click', function(){
          var id = $(this)[0].getAttribute('id');
          switch(id) {
              case "disability_yes" :
                  $('#disability_info .additional').show();
              break;
              case "disability_no" :
                  $('#disability_info .additional').hide();
              break;
          }
      });
  }


	function addfeedback(feedback,field) {
		$($(field).siblings()[0]).append('<em class="feedback">' + feedback + '</em>');
		$(field).addClass('error');
		$(field).parent().addClass('error');
		var fred = $(field).parent();
		if(fred[0].className.indexOf("correct error") != -1) {
			$(this).parent().removeClass('correct');
		}
	}
	function clearfeedback(whichform) {
		$('em',whichform).remove(".feedback");
		$(':input[@class*=required]',whichform).add('select.required',whichform).each(function() {													  
			$(this).removeClass('error');
			$(this).parent().removeClass('error');
			$(this).parent().addClass('correct');
		});
		$('div.error',whichform).removeClass('error');
	}
	function isFilled(x) {
		return (x.value.length < 1 ? false : true);
	}
	function isEmail(field) {
		var regEx = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
		return (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1 || !field.value.match(regEx) ? false : true);
	}
});
