(function($){

var RedingerEHS = {
   ondomready: function(){
	RedingerEHS.handleLogo();	
	RedingerEHS.handleInputs();
	RedingerEHS.bindFormHandler();
	
   },
   
   
   bindFormHandler: function() {
   
	$("#theform").validate({
		submitHandler: function() {
			$("#register").hide();
			$("#loading").show();
			RedingerEHS.submitFormHandler();
		}
	});
	
	$.validator.addClassRules("surveyData", {
		min: 0,
		max: 5
	});
	
	$(".surveyData").not(".error").change(function() {
		var question = $(this).parent().parent().find(".num").html();
		var sec = question.substring(0,1);
		
		var targ = $("#counter-"+sec);
		
		var total = 0;
		
		$(this).parent().parent().parent().find(".surveyData").each(function() {
			if ($(this).val() != "") {
				total += parseInt($(this).val());
			}
		});
		
		if (total) {
			targ.find("span").html(total);
		} else {
			targ.find("span").html("");
		}
		RedingerEHS.updateSampleTotal();
	});
	
	$("#thecheckupform table tr:odd").addClass("odd");
	
	$("#thecheckupform").validate();
  },
  
  updateSampleTotal: function() {
	var thetotal = 0;
	$(".counterHolder").each(function() {
		var val = parseInt($(this).find("span").html());
		
		if (val) {
			thetotal += val;
		}
	});
	
	$("#counter-total").find("span").html(thetotal);
  },
  
  submitFormHandler: function() {

	var namefirst = $("#namefirst").val();
	var namelast = $("#namelast").val();
	var company = $("#companyf").val();
	var email = $("#emailaddressf").val();
	
	var dataString = "namefirst="+namefirst+"&namelast="+namelast+"&companyf="+company+"&emailaddressf="+email;
	
	
	
	$.ajax({
		type: "POST",
		url: "/formhandler.php",
		data: dataString,
		success: function(data) {
			
			if (trim(data) != "") {
				//alert(data);
				$("#loading").hide();
				$("#toload").html(data);
				$("#toload").show();
			}
		}
	});	

   },

   handleLogo: function() {
   	$("#logo").click(function() {
   		document.location.href="/";
   		return ( false );
   	
   	});
   },
   
   handleInputs: function() {
   	
   	
   	$("#s").focus(function() {
   		var strTerm = "Search";
		if ($(this).val() == strTerm)
   			$(this).val("");
		$(this).addClass("textinputactive");  			
   	}).blur(function() {
   		var strTerm = "Search";
		if ($(this).val() == "")
   			$(this).val(strTerm);
		$(this).removeClass("textinputactive");  			
   	});
   	
   	$("#email").focus(function() {	
   		var strTerm = "Enter E-mail Address";
		if ($(this).val() == strTerm)
   			$(this).val("");
		$(this).addClass("textinputactive");  			
   	}).blur(function() {
   		var strTerm = "Enter E-mail Address";
		if ($(this).val() == "")
   			$(this).val(strTerm);
		$(this).removeClass("textinputactive");  			
   	});
   	
   	
   	
   	
   }
   
   
   
}

window.RedingerEHS = RedingerEHS;

$(document).ready(function(){RedingerEHS.ondomready();});

})(jQuery);

function trim(str) {
        return str.replace(/^\s+|\s+$/g,"");
    }
