/*** Check ZIP field---*/
function isZip(s)  {
    // Check for correct zip code
    reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

    if (! reZip.test(s)) {
         return false;
    }

    return true;
}

/**** Check email address--*/
function isEmail(str) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(str)) {
		return false;
	}
	return true
}

/****Check Phone Number---*/
function isPhoneNumber(str){
	return str.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);	
}




$().ready(function(){
	
	/*** Already submitted (start)---*/
	url_splited = window.location.href.split('/');
	if (url_splited[url_splited.length - 2] == 'thank-you'){
		$(".slide3").trigger('slide');
		$("#information-has-been-submitted").show();
	}
	/*** Already submitted (end)---*/
	
	/***Switch Illness details (start)---*/
	$("#illness_yes").click(function(){

		$("#illness-details-line").show();
		$("#illness_details").attr("disabled",false);
			
	})
	$("#illness_no").click(function(){

		$("#illness-details-line").hide();
		$("#illness_details").attr("disabled",true);
			
	})
	/***Switch Illness details (end)---*/
	
	
	
	
	/***Switch breeds (start)------------------------*/
    $("#pet_type_dog").click(function(){

	    $("#breed_dog").attr("disabled",false);
	    $("#breed_dog").show();
	        	
	    $("#breed_cat").attr("disabled",true);
	    $("#breed_cat").hide();
        	
    })
        
    $("#pet_type_cat").click(function(){

	    $("#breed_cat").attr("disabled",false);
	    $("#breed_cat").show();
	         	
	    $("#breed_dog").attr("disabled",true);
	    $("#breed_dog").hide();
        	
    })
	/***Switch breeds (end)------------------------*/
	
    
    
    /***Validating first step (start)-------------------------*/
    function validate_first_step(){
 		
    	var result = true;
    	
    	if ($("#first_name").val() == '' || $("#first_name").val().length > 50){
 			$("#first_name").css("border","1px red dotted");
 			result = false
 		}else{
 			$("#first_name").css("border","1px solid #CCCCCC");
 		}
    	
    	if ($("#last_name").val() == '' || $("#last_name").val().length > 50){
 			$("#last_name").css("border","1px red dotted");
 			result = false
 		}else{
 			$("#last_name").css("border","1px solid #CCCCCC");
 		}
    	
    	if ($("#zip").val() == '' || ! isZip($("#zip").val())){
 			$("#zip").css("border","1px red dotted");
 			result = false
 		}else{
 			$("#zip").css("border","1px solid #CCCCCC");
 		}
    	
    	if ($("#email").val() == '' || ! isEmail($("#email").val()) || $("#email").val().length > 500){
 			$("#email").css("border","1px red dotted");
 			result = false
 		}else{
 			$("#email").css("border","1px solid #CCCCCC");
 		}
    	
    	if ($("#phone").val() == '' || ! isPhoneNumber($("#phone").val())){
 			$("#phone").css("border","1px red dotted");
 			result = false
 		}else{
 			$("#phone").css("border","1px solid #CCCCCC");
 		}
    	
    	if (result)
    		return true;
    	else
    		return false;
 	}
    /***Validating first step (end)-------------------------*/
    
    
    
    
    /***Validating second step (end)-------------------------*/
    function validate_second_step(){
 		
    	result = true;
    	
    	if ($("#pet_name").val() == '' || $("#pet_name").val().length > 50){
 			$("#pet_name").css("border","1px red dotted");
 			result = false
 		}else{
 			$("#pet_name").css("border","1px solid #CCCCCC");
 		}
    	
    	if ($("#breed_dog").attr('disabled') == false){
    	
    		if ($("#breed_dog option:selected").val() == ''){
    			$("#breed_dog").css("border","1px red dotted");
     			result = false
    		}else{
    			$("#breed_dog").css("border","1px solid #CCCCCC");
    		}
    			
    	}
    	if ($("#pet_1_month option:selected").val() == ''){
    		$("#pet_1_month").css("border","1px red dotted");
     		result = false
    	}else{
    		$("#pet_1_month").css("border","1px solid #CCCCCC");
    	}
    	if ($("#pet_1_year option:selected").val() == ''){
    		$("#pet_1_year").css("border","1px red dotted");
     		result = false
    	}else{
    		$("#pet_1_year").css("border","1px solid #CCCCCC");
    	}
    	if ($("#breed_cat").attr('disabled') == false){
        	
    		if ($("#breed_cat option:selected").val() == ''){
    			$("#breed_cat").css("border","1px red dotted");
     			result = false
    		}else{
    			$("#breed_cat").css("border","1px solid #CCCCCC");
    		}
    			
    	}
    	if (($("#illness_details").val().length == 0 && $("#illness_details").attr("disabled") == false) || $("#illness_details").val().length > 500 ){
 			$("#illness_details").css("border","1px red dotted");
 			result = false
 		}else{
 			$("#illness_details").css("border","1px solid #CCCCCC");
 		}
    	
    	/**Checking captcha--*/
    	$.post(
    			template_directory + "/captcha/checkCaptchaAjax.php",
    			{ captcha_code: $("#security_code").val() },
    			function(res){
    			    
    				if (res == "Success"){
    					
    					$("#security_code").css("border","1px solid #CCCCCC");
    					if (result){					//if all checked and validate then submit form
    						$("#lead-quotes-registration-form").submit();
    					}
    					
    				}else{								//if validation captcha failed then update captcha 
    					
    				   image = template_directory + "/captcha/CaptchaSecurityImages.php?width=70&height=20&characters=3" //name of the image
    				   function refreshCaptcha() {
    				      tmp = new Date();
    				      tmp = "&"+tmp.getTime();
    				      document.images["captcha_image"].src = image + tmp
    				   }
    				   refreshCaptcha(); //refresh captcha image
    				   
    				   $("#security_code").css("border","1px dotted red");
    				  
    				}
    			    
    			}

    			);
    	     
    	
    }	
    /***Validating second step (end)-------------------------*/
	
    
    
    /***Go to step2 by clicking on the "Next" button (start)---*/
    $("#next-button").click(function(){
		$(".slide2").trigger('slide');
		return false;
    	
    })
	/***Go to step2 by clicking on the "Next" button (end)---*/
    
    
    /***Go to registration (start)---*/
    $("#get-quotes-button").click(function(){
    	
    	if (validate_first_step()){
    		validate_second_step();
    	}else{
    		$(".slide1").trigger('slide');
    		return false;
    	}
    	
    });
    /***Go to registration (end)---*/
     
    
    
    
	/**** Additional fields (start)----*/
		
		//switcher for the dog/cat breeds
		$(".additional_pet_type_dog").click(function(){
			
			$(this).parents('table').find('.additional_breed_dog').attr("disabled",false);
			$(this).parents('table').find('.additional_breed_dog').show();
	    	
			$(this).parents('table').find('.additional_breed_cat').attr("disabled","disabled");
			$(this).parents('table').find('.additional_breed_cat').hide();
	    	
	    });
	    
	    //switcher for the dog/cat breeds
	     $(".additional_pet_type_cat").click(function(){
	
	    	 $(this).parents('table').find('.additional_breed_cat').attr("disabled",false);
	    	 $(this).parents('table').find('.additional_breed_cat').show();
	     	
	    	 $(this).parents('table').find('.additional_breed_dog').attr("disabled","disabled");
	    	 $(this).parents('table').find('.additional_breed_dog').hide();
	    	
	    });
	    
	    //switcher for illness details
		$(".additional_illness_yes").click(function(){
	
			$(this).parents('table').find(".additional_illness_details").attr('disabled',false);
			$(this).parents('table').find(".additional_illness_details_box").show();
			
		});
		$(".additional_illness_no").click(function(){
	
			$(this).parents('table').find(".additional_illness_details").attr('disabled',true);
			$(this).parents('table').find(".additional_illness_details_box").hide();
			
		});
	
		/***Show popup---*/
		function show_additional_popup(){
	
			$("#blurDiv").css('width',"100%");
			$("#blurDiv").css('height',document.body.clientHeight);
			$("#blurDiv").css('z-index','300');
			
			$("#blurDiv").show();
			$("#additional_form_fields_container").show();
			
		}
		
		/***Validate all additinal fields---*/
		function validate_additional_fields(){
			result = true;
			
			$(".additional:visible .additional_pet_name").each(function(){
				if ($(this).val() == '' || $(this).val().length > 50){
			 		$(this).css("border","1px red dotted");
			 		result = false
			 	}else{
			 		$(this).css("border","1px solid #CCCCCC");
			 	}
			})
			
			for (i = 2; i <=3 ; i++){
				
				if ($("#" + i + "_additional_breed_dog:visible").attr('disabled') == false){
				    	
			    	if ($("#" + i + "_additional_breed_dog option:selected").val() == ''){
			    		$("#" + i + "_additional_breed_dog").css("border","1px red dotted");
			     		result = false
			    	}else{
			    		$("#" + i + "_additional_breed_dog").css("border","1px solid #CCCCCC");
			    	}
			    			
			    }
				
				if ($("#" + i + "_additional_breed_cat:visible").attr('disabled') == false){
			    	
			    	if ($("#" + i + "_additional_breed_cat option:selected").val() == ''){
			    		$("#" + i + "_additional_breed_cat").css("border","1px red dotted");
			     		result = false
			    	}else{
			    		$("#" + i + "_additional_breed_cat").css("border","1px solid #CCCCCC");
			    	}
			    			
			    }
			}
			
			$(".additional:visible .additional_dob_month").each(function(){
				if ($(this).val() == ''){
		    		$(this).css("border","1px red dotted");
		     		result = false;
				}else{
		    		$(this).css("border","1px solid #CCCCCC");
		    	}
	    	})
			$(".additional:visible .additional_dob_year").each(function(){
				if ($(this).val() == ''){
		    		$(this).css("border","1px red dotted");
		     		result = false;
				}else{
		    		$(this).css("border","1px solid #CCCCCC");
		    	}
	    	})
	    	
			$(".additional:visible .additional_illness_details").each(function(){
	        	if (($(this).val().length == 0 && $(this).attr("disabled") == false) || $(this).val().length > 500 ){
	     			$(this).css("border","1px red dotted");
	     			result = false
	     		}else{
	     			$(this).css("border","1px solid #CCCCCC");
	     		}
			})
			return result;
		}
		
		/***Reset all fields in the all additional form---*/
		function reset_additional_form(){
			
			$(".additional_pet_name").each(function(){
				$(this).val('');
				$(this).css("border","1px solid #CCCCCC");
			})
			$(".additional_illness_details").each(function(){
				$(this).val('');
				$(this).css("border","1px solid #CCCCCC");
			})
			$(".additional_breed_dog option").each(function(){
				if ($(this).val() == ''){
					$(this).attr('selected','selected');
				}else{
					$(this).attr('selected',false);
				}
			})
			$(".additional_dob_year option").each(function(){
				if ($(this).val() == ''){
					$(this).attr('selected','selected');
				}else{
					$(this).attr('selected',false);
				}
			})
			$(".additional_dob_month option").each(function(){
				if ($(this).val() == ''){
					$(this).attr('selected','selected');
				}else{
					$(this).attr('selected',false);
				}
			})
			$(".additional_breed_cat option").each(function(){
				if ($(this).val() == ''){
					$(this).attr('selected','selected');
				}else{
					$(this).attr('selected',false);
				}
			})
			
			$("#3rd_pet_info").hide();
			$("#remove_3rd_pet_button").hide();
			$("#add_3rd_pet_button").show();
		}
		
		/***Reset all fields in the 3rd pet additional form---*/
		function reset_3rd_pet_form(){
			
			$(".additional_pet_name:last").each(function(){
				$(this).val('');
				$(this).css("border","1px solid #CCCCCC");
			})
			$(".additional_illness_details:last").each(function(){
				$(this).val('');
				$(this).css("border","1px solid #CCCCCC");
			})
			$(".additional_breed_dog:last option").each(function(){
				if ($(this).val() == ''){
					$(this).attr('selected','selected');
				}else{
					$(this).attr('selected',false);
				}
			})
			$(".additional_breed_cat:last option").each(function(){
				if ($(this).val() == ''){
					$(this).attr('selected','selected');
				}else{
					$(this).attr('selected',false);
				}
			})
			$(".additional_dob_year:last option").each(function(){
				if ($(this).val() == ''){
					$(this).attr('selected','selected');
				}else{
					$(this).attr('selected',false);
				}
			})
			$(".additional_dob_month:last option").each(function(){
				if ($(this).val() == ''){
					$(this).attr('selected','selected');
				}else{
					$(this).attr('selected',false);
				}
			})
			$("#3rd_pet_info").hide();
			$("#remove_3rd_pet_button").hide();
			$("#add_3rd_pet_button").show();
		}
	
		/***Calculate count of additional pets an showing results on the link---*/
		function count_pets(){
			count = 0;
			$(".additional_pet_name").each(function(){
				if ($(this).val() != '') {count++};
			})
			if (count > 0){
				$("#add_another_pet_button").text('');
				if (count > 1){s = 's';}else{s = '';};
				$("#pets_count").text("+" + count + " pet" + s);
			}else{
				$("#add_another_pet_button").text('Add a 2nd Pet');
				$("#pets_count").text("");
			}
		}
		
		/***If needs to add 3rd pet then validate info about 2nd pet and show form for the adding 3rd pet---*/
		$("#add_3rd_pet_button").click(function(){
			if (validate_additional_fields()){
				$("#3rd_pet_info").show();
				$(this).hide();
				$("#remove_3rd_pet_button").show();
			}
			
		});
		
		/***If needs to remove 3rd pet then validate info about 2nd pet and show form for the adding 3rd pet---*/
		$("#remove_3rd_pet_button").click(function(){
		
			$("#3rd_pet_info").hide();
			$(this).hide();
			$("#add_3rd_pet_button").show();
			reset_3rd_pet_form();
		});
		
		
		/***Close popup function by clicking on OK button. Calculate number of pets---*/
		$(".close_popup.ok").click(function(){
			
			if (validate_additional_fields()){
				
				count_pets();
				$("#blurDiv").hide();
				$("#additional_form_fields_container").hide();
			}
			
		})
		
		/***Close popup function---*/
		$(".close_popup.cancel").click(function(){
		
			reset_additional_form();
			count_pets();
			$("#blurDiv").hide();
			$("#additional_form_fields_container").hide();
			
		})
		
		/***Add another pet button click. Show Popup additional form---*/
		$("#add_another_pet_button").click(function(){
			show_additional_popup();
			$(".close_popup.cancel").text('Cancel');
			return false;
		})
		
		$("#pets_count").click(function(){
			
			show_additional_popup();
			$(".close_popup.cancel").text('Remove all');
			return false;
			
		})
	/**** Additional fields (enb)----*/
})
