
$(document).ready(function(){
						   
	//tabs initiate
	$(function () {
		var tabContainers = $('div#tabContent > div');
		tabContainers.hide().filter(':first').show();

		$('div#tabs ul li a').click(function () {
			tabContainers.hide();
			tabContainers.filter(this.hash).show();
			$('div#tabs ul li a').removeClass('current');
			$(this).addClass('current');
			return false;
		}).filter(':first').click();
	});		
	
	//annual report
	$("#goToPage").click(function(){
		if(formValidate("step1")){
			$("#step1").hide();
			$("#step2").show();
			window.location = "#";
			$("#formPage").text("Page 2 of 2");
		}
		else{
			return false;
		}
	});
	
	$("#annualReportForm").submit(function(){
		if(formValidate("step2")){
			return true;
		}
		else{
			return false;
		}						  
	});
	
	$("#startForm").submit(function(){
		if(formValidate("startForm")){
			return true;
		}
		else{
			return false;
		}							  
	});
	
	$("#evalForm").submit(function(){
		if(evalValidate("evalForm")){
			return true;
		}
		else{
			return false;
		}							  
	});
	
	$(".countyHead").click(function(){
		if( $(this).next().length != 0){
			$(this).toggleClass("selected");
			$(this).next().toggle();
		}
		return false;
	});
	
});

function formValidate(step){
	var validate;
	var formValidate = false;
	var emailValidate = false;

	//alert($("#"+ step + " :input[name!=submit]").length)
	$("#"+ step + " :input[name!=submit]").each(function (i) {
		if($(this).val() == "" && !$(this).hasClass("optional") && $(this).attr("type") != "radio"){
			if($(this).parent().hasClass("multiField")){
				if($(this).val() == "" && $(this).siblings("input").val() == ""){
					$(this).parent().addClass("error");	
				}
			}else{
				$(this).parent().addClass("error");		
			}
		}
		else if($(this).attr("type") == "radio"){
			
			if($(this).parent().find("input:radio:checked").length != 1){
				$(this).parent().addClass("error");
				//alert($(this).parent().attr("class"));
			}else{
				$(this).parent().removeClass("error");
			}
			
			if($(this).parent().find("input:radio:last").attr("checked") == true){
				if($(this).parent().find("input:text").val() == ""){
					$(this).parent().find("input:text").addClass("error");
				}
			}
		}
		else if(!$(this).hasClass("other")){
			$(this).parent().removeClass("error");
		}
	});
		
	
	
	if($("#"+ step + " .error").length > 0){
		$("#errorMsg").show();
		window.location = "#";
		formValidate = false;
	}else{
		$("#errorMsg").hide();
		formValidate = true;
	}
	
	$(".email").each(function(){
		if($(this).val() != ""){
			if (echeck($(this).val())){
				emailValidate = true;	
				$("#emailErrorMsg").hide();
				$(this).parent().removeClass("error");
			}else{
				emailValidate = false;
				$("#emailErrorMsg").show();
				$(this).parent().addClass("error");
				window.location = "#";
			}
		}
	});
	
	
	if (emailValidate == true && formValidate == true){
		validate = true;	
	}
	else{
		validate = false;	
	}
	return validate;
}

function evalValidate(step){
	var formValidate = false;
	$(".inputSet").each(function (i) {
		if($(this).find("input:radio:checked").length != 1)
		{
			$(this).addClass("error");
		}else{
			$(this).removeClass("error");
		}
	});
	
	if($("#"+ step + " .error").length > 0){
		$("#errorMsg").show();
		window.location = "#";
		formValidate = false;
	}else{
		$("#errorMsg").hide();
		formValidate = true;
	}
	
	return formValidate;
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//alert("Invalid E-mail ID")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		//alert("Invalid E-mail ID")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//alert("Invalid E-mail ID")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		//alert("Invalid E-mail ID")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		//alert("Invalid E-mail ID")
		return false
	 }

	 return true					
}


