/***Function for checking Date of birthday---*/
function checkDOB(str){
      var validformat=/^\d{2}\/\d{4}$/ //Basic check for format validity
        		
       if (! validformat.test(str))
    	   return true;
       else{ //Detailed check for valid date ranges
        			
	        var monthfield 	= str.split("/")[0]
	        var yearfield	= str.split("/")[1]
	        var dayobj = new Date(yearfield, monthfield-1, 1)
	        			
	        if ((dayobj.getMonth()+1 != monthfield)||(dayobj.getFullYear() != yearfield))
	        	return false;
	        else
	        	return true
       }
        	
}

/* Created by jankoatwarpspeed.com */
(function($) {

    $.fn.formtowizard = function(options) {
        options = $.extend({  
            submitButton: "" 
        }, options); 
        
        var element = this;

        var steps = $(element).find("fieldset");
        var count = steps.size();
        var submmitButtonName = "#" + options.submitButton;
        $(submmitButtonName).hide();

        // 2
        $(element).before("<ul id='steps'></ul>");

        steps.each(function(i) {
            $(this).wrap("<div id='step" + i + "'></div>");
            $(this).append("<p id='step" + i + "commands'></p>");

            // 2
            var name = $(this).find("legend").html();
            $("#steps").append("<li id='stepDesc" + i + "'>Step " + (i + 1) + "<span>" + name + "</span></li>");

            if (i == 0) {
                createNextButton(i);
                selectStep(i);
            }
            else if (i == count - 1) {
                $("#step" + i).hide();
                createPrevButton(i);
            }
            else {
                $("#step" + i).hide();
                createPrevButton(i);
                createNextButton(i);
            }
        });

        function createPrevButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='javascript:' id='" + stepName + "Prev' class='prev'>< Back</a>");

            $("#" + stepName + "Prev").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#step" + (i - 1)).show();
                $(submmitButtonName).hide();
                selectStep(i - 1);
            });
        }
       
        function createNextButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='javascript:' id='" + stepName + "Next' class='next' >Next ></a>");

            $("#" + stepName + "Next").bind("click", function(e) {
            	if (validate_first_step()){	//call validating first step. see below
	                $("#" + stepName).hide();
	                $("#step" + (i + 1)).show();
	                if (i + 2 == count)
	                    $(submmitButtonName).show();
	                selectStep(i + 1);
            	}
            });
        }
        function selectStep(i) {
            $("#steps li").removeClass("current");
            $("#stepDesc" + i).addClass("current");
        }
        
        /***Validating first step (start)-------------------------*/
        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;
        }
        
        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
        }
        function isPhoneNumber(str){
        	return str.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);	
        }
        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)-------------------------*/
        $("#submitSignUpForm").click(function(){
        	
        	validate_second_step();
        		
        });
       
        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 ($("#dob").val() == '' || ! checkDOB($("#dob").val())){
     			$("#dob").css("border","1px red dotted");
     			result = false
     		}else{
     			$("#dob").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
        						$("#SignupForm").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)-------------------------*/
        
        /***Switch breeds (start)------------------------*/
        $("#pet_type_dog").click(function(){

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

        	$("#breed_cat").attr("disabled",false);
         	$("#breed_cat").show();
         	
         	$("#breed_dog").attr("disabled","disabled");
         	$("#breed_dog").hide();
        	
        })
        /***Switch breeds (end)------------------------*/
    }
})(jQuery); 
