$(document).ready(function(){
  $("#js_enabled").hide();
  if( $("#credit").children(":selected").attr("name") == "OTHER" ){
    $("#credit_field").show();
  }
  if( $("#degree_other").is(":checked") ){
    $("#degree_field").show();
  }
  if( $("#primaryarea").children(":selected").attr("name") == "OTHER" ){
    $("#primary_field").show();
  }
  if( $("#ethnicity").children(":selected").attr("name") == "OTHER" ){
    $("#ethnicity_field").show();
  }

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

  $("#degree_other").click(function(){
    if( $("#degree_other").is(":checked") ){
      $("#degree_field").show("fast").focus();
    }else{
      $("#degree_field").hide("fast");
    }
  });

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

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

  $("input").focus(function(){
    $(this).next("label").addClass("labelon");
  });
  $("input").blur(function(){
    $(this).next("label").removeClass("labelon");
  });
    
  $("label").click(function(){
    if( "degree_other" == $(this).prev(":input").attr("id") ){
       if( $("#degree_other").is(":checked") ){
         $("#degree_other").removeAttr("checked");
         $("#degree_field").hide("fast");
       }else{
         $("#degree_other").attr("checked","checked");
         $("#degree_field").show("fast").focus();
       }
    }else $(this).prev(":input").click();
  });
  
  
  
  $("#registration").submit(function(){
		var ret = true;
		var require = "<br />Required.";
		if( $('#credit').val() == 0 ){
			$("#credit_msg").html(require);
			ret = false;
		}else $("#credit_msg").hide();
		if( cred=$("#credit_field").val() ){
		  $("#credit").children("[name=OTHER]").val( cred );
		}
		
		if( !$(":checkbox:checked").attr({name:"degree[]"}).val() || ( $("#degree_other:checked").val() && !$("#degree_field").val() ) ){
			$("#degree_msg").html(require);
			ret = false;
		}else $("#degree_msg").hide();
		if( deg=$("#degree_field").val() ){
		  $("#degree_other").val( deg );
		}

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

		if( eth=$("#ethnicity_field").val() ){
		  $("#ethnicity").children("[name=OTHER]").val( eth );
		}
		
		return ret;
	});

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

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

		if( $('#phone').val() == "" ){
			$("#phone_msg").html(require);
			ret = false;
		}else $("#phone_msg").hide();
		
		return ret;
	});
  
  $("#returnuser").submit(function(){
		var ret = true;
		var require = " Required.";
		if( $('#firstname').val() == "" ){
			$("#firstname_msg").html(require);
			ret = false;
		}else $("#firstname_msg").hide();
		
		if( $('#lastname').val() == "" ){
			$("#lastname_msg").html(require);
			ret = false;
		}else $("#lastname_msg").hide();
		
		if( $('#street1').val() == "" ){
			$("#street1_msg").html(require);
			ret = false;
		}else $("#street1_msg").hide();
		
		if( $('#city').val() == "" ){
			$("#city_msg").html(require);
			ret = false;
		}else $("#city_msg").hide();
		
  	if( $('#state').val() == "" ){
			$("#state_msg").html(require);
			ret = false;
		}else $("#state_msg").hide();

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

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

function checkEmail( em ){
 $.post( "../logic/check_email.php", { email:em }, function( data ){
    if( data == "true" ){
       $("#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>.");
    }else{
       $("#pass2").removeAttr("disabled");
       $("#ajax_feedback").html(" ");
    }
  });
}

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 = "";
}


