$(document).ready(function(){
  $("#js_enabled").hide();
  if( $("#credit").children(":selected[name=OTHER]").length ){
    $("#credit_field").show();
  }
  if( $("#degree").children(":selected[name=OTHER]").length ){
    $("#degree_field").show();
  }
  if( $("#primaryarea").children(":selected[name=OTHER]").length ){
    $("#primary_field").show();
  }
  if( $("#ethnicity").children(":selected[name=OTHER]").length ){
    $("#ethnicity_field").show();
  }

  $("#credit").change(function(){
    if( $(this).children(":selected[name=OTHER]").length ){
      $("#credit_field").show("fast").focus();
    }else{
      $("#credit_field").hide("fast").val('');
    }
  });

  $("#degree").change(function(){
    if( $(this).children(":selected[name=OTHER]").length ){
      $("#degree_field").show("fast").focus();
    }else{
      $("#degree_field").hide("fast").val('');
    }
  });

  $("#practice").change(function(){
    if( $(this).children(":selected[name=OTHER]").length ){
      $("#practice_field").show("fast").focus();
    }else{
      $("#practice_field").hide("fast").val('');
    }
  });

  $("#primaryarea").change(function(){
    if( $(this).children(":selected[name=OTHER]").length ){
      $("#primary_field").show("fast").focus();
    }else{
      $("#primary_field").hide("fast").val('');
    }
  });

  $("#ethnicity").change(function(){
    if( $(this).children(":selected[name=OTHER]").length ){
      $("#ethnicity_field").show("fast").focus();
    }else{
      $("#ethnicity_field").hide("fast").val('');
    }
  });

  $("input").focus(function(){
    $(this).next("label").addClass("labelon");
  });
  $("input").blur(function(){
    $(this).next("label").removeClass("labelon");
  });
    
  $("label").click(function(){
    $(this).prev(":input").click();
  });
  
  
   $("#registration").submit(function(){
		var ret = true;
		var require = "<br />Required.";
		
		$("#credit").children("[name=OTHER]").val( $('#credit_field').val() );
		if( $('#credit').val() == 0 ){
			$('#credit_msg').html(require).show();
			ret = false;
		}else $('#credit_msg').html('');
		
		
		if( $(":checkbox:checked[name='topics[]']").length == 0 ){
			$('#topics_msg').html(require).show();
			ret = false;
		}else $('#topics_msg').html('');

		$("#degree").children('[name=OTHER]').val( $('#degree_field').val() );
        if( $('#degree').val() == 0 ){
			$('#degree_msg').html(require).show();
			ret = false;
		}else $('#degree_msg').html('');

		if( $('#certified').val() == 0 ){
			$('#certified_msg').html(require).show();
			ret = false;
		}else $('#certified_msg').html('');
		
		$('#practice').children('[name=OTHER]').val( $('#practice_field').val() );
		if( $('#practice').val() == 0 ){
			$('#practice_msg').html(require).show();
			ret = false;
		}else $('#practice_msg').html('');
		
		
		if( $('#diabetes').val() == 0  ){
			$('#diabetes_msg').html(require).show();
			ret = false;
		}else $('#diabetes_msg').html('');
		
		if( $('#patients').val() == 0 ){
			$('#patients_msg').html(require).show();
			ret = false;
		}else $("#patients_msg").html('');
		
		if( $('#agegroup').val() == 0 ){
			$('#agegroup_msg').html(require).show();
			ret = false;
		}else $('#agegroup_msg').html('');
		
		if( $('#yearsworked').val() == 0 ){
			$('#yearsworked_msg').html(require).show();
			ret = false;
		}else $('#yearsworked_msg').html('');
		
		$('#primaryarea').children('[name=OTHER]').val( $('#primary_field').val() );
		if( $('#primaryarea').val() == 0 ){
			$("#primaryarea_msg").html(require).show();
			ret = false;
		}else $('#primaryarea_msg').html('');

		$('#ethnicity').children('[name=OTHER]').val( $('#ethnicity_field').val() );
		
		if(!ret){
		    window.scrollTo(0, $('.feedback:contains(Required):visible').offset().top - 15);
		    window.alert('Please complete all required fields. Required fields that were left blank are now marked in red.');
		}
		return ret;
	});

  $("#newuser").submit(function(){
		var ret = true;
		var require = " Required.";
		if( $('#email').val() == "" || !$("#email").val().match( /^[^@]+@[^@]+\.[\w]{2,4}$/ ) ){
			$("#email_msg").html(require).show();
   		ret = false;
		}else $("#email_msg").hide();
		
		if( $('#pass2').val() == ""  ){
			$("#pass1_msg").html(require).show();
			ret = false;
		}else $("#pass1_msg").hide();
		
		if( $('#firstname').val() == "" ){
			$("#firstname_msg").html(require).show();
			ret = false;
		}else $("#firstname_msg").hide();
		
		if( $('#lastname').val() == "" ){
			$("#lastname_msg").html(require).show();
			ret = false;
		}else $("#lastname_msg").hide();
		
		if( $('#street1').val() == "" ){
			$("#street1_msg").html(require).show();
			ret = false;
		}else $("#street1_msg").hide();
		
		if( $('#city').val() == "" ){
			$("#city_msg").html(require).show();
			ret = false;
		}else $("#city_msg").hide();
		
  	if( $('#state').val() == "" ){
			$("#state_msg").html(require).show();
			ret = false;
		}else $("#state_msg").hide();

		if( $('#zipcode').val() == "" ){
			$("#zipcode_msg").html(require).show();
			ret = false;
		}else $("#zipcode_msg").hide();

		if( $('#phone').val() == "" ){
			$("#phone_msg").html(require).show();
			ret = false;
		}else $("#phone_msg").hide();
		
		
		if(!ret){
		    window.scrollTo(0, $('.feedback:contains(Required):visible').offset().top);
		    window.alert('Please complete all required fields. Required fields that were left blank are now marked in red.');
		}
		return ret;
	});
  
  $("#returnuser").submit(function(){
		var ret = true;
		var require = " Required.";
		if( $('#firstname').val() == "" ){
			$("#firstname_msg").html(require).show();
			ret = false;
		}else $("#firstname_msg").hide();
		
		if( $('#lastname').val() == "" ){
			$("#lastname_msg").html(require).show();
			ret = false;
		}else $("#lastname_msg").hide();
		
		if( $('#street1').val() == "" ){
			$("#street1_msg").html(require).show();
			ret = false;
		}else $("#street1_msg").hide();
		
		if( $('#city').val() == "" ){
			$("#city_msg").html(require).show();
			ret = false;
		}else $("#city_msg").hide();
		
  	if( $('#state').val() == "" ){
			$("#state_msg").html(require).show();
			ret = false;
		}else $("#state_msg").hide();

		if( $('#zipcode').val() == "" ){
			$("#zipcode_msg").html(require).show();
			ret = false;
		}else $("#zipcode_msg").hide();

		if( $('#phone').val() == "" ){
			$("#phone_msg").html(require).show();
			ret = false;
		}else $("#phone_msg").hide();

		if(!ret){
		    window.scrollTo(0, $('.feedback:contains(Required):visible').offset().top);
		    window.alert('Please complete all required fields. Required fields that were left blank are now marked in red.');
		}
		return ret;
	});
  
});

function checkEmail( addr ){
  $.post( '../logic/check_email.php', { email:addr }, function(data){
    if( "true" == data ){
      $("#pass2").attr("disabled","disabled");
      $("#ajax_feedback").html("That email address is already registered with this program. <a href='../pages/login.php?email="+$("#email").val()+"'>Click here to log in</a>.").show();
    }else{
      $("#pass2").removeAttr("disabled");
      $("#ajax_feedback").html(" ").hide();
    }
  });
}

function confirmPass(){
  pass1 = document.getElementById("pass1");
  pass2 = document.getElementById("pass2");
  pass_out = document.getElementById("pass_feedback");
  if( pass1.value != pass2.value ){
    pass2.value = "";
    pass1.focus();
    pass_out.innerHTML = "Does not match.";
  }else pass_out.innerHTML = "";
}
