$(function(){
    //mask for form
    $("#free_eval").find("input[name=phone]").mask("?(999) 999-9999");
    $("#boostability").find("input[name=phone]").mask("?(999) 999-9999");
    
    
    $(".nav").hover(function(){
        $("#rollover").show();
        var $id = $(this).find("a").attr("id");
        switch($id){
            case "WebDesign":
                $("#image_holder").css('background-position','0px -292px');
                $("#text_holder").find("p").empty().html("Instead of resorting to the cookie cutter designs of other companies we create custom designs made specifically with your business in mind. We provide affordable website design that gives you fashion and function to maximize results.");
                $("#text_holder").find("a").attr("href","web-design.php");
                break;
            case "PPC":
                $("#image_holder").css('background-position','0px -73px'); 
                $("#text_holder").find("p").empty().html("You can certainly do Pay-Per-Click advertising on your own, but with Leadgenix you can do it right.  We guarantee you more bang for your buck. Stop spending so much time trying to figure out paid search and focus on what you do best- running your business.");
                $("#text_holder").find("a").attr("href","ppc-management.php");
                break;
            case "SEO":
                $("#image_holder").css('background-position','0px 0px');
                $("#text_holder").find("p").empty().html("Search engines are the number one way in which people look for information. People are searching for your product or service right now, but are they finding you? If you don't want them to keep finding someone else, then let us put you in front of the people who are searching for you.");
                $("#text_holder").find("a").attr("href","seo.php");
                break;
            case "SocialMedia":
                $("#image_holder").css('background-position','0px -146px'); 
                $("#text_holder").find("p").empty().html("With the latest advancements in technology, word-of-mouth is now giving way to word-of-mouse, meaning people are talking about you and your product or service online.  If you want to be part of that conversation fill out a form and request your proposal today!");
                $("#text_holder").find("a").attr("href","social-media-optimization.php");
                break;
            case "RepManagement":
                $("#image_holder").css('background-position','0px -219px'); 
                $("#text_holder").find("p").empty().html("No one can control the negative things that are being said online. But with our Reputation Management services, we can make the positive voices louder than the negative ones. Let us help you protect your brand today!");
                $("#text_holder").find("a").attr("href","online-reputation-management.php");
                break;
            case "LeadGeneration":
                $("#image_holder").css('background-position','0px -365px'); 
                $("#text_holder").find("p").empty().html("We can turn visitors from other websites into your customers through Lead Generation.  We will help you get quality leads and start expanding your business. Get access to potential customers who are most likely to convert into paying customers.");
                $("#text_holder").find("a").attr("href","lead-generation.php");
                break;
        }
    },
    function(){
        $("#rollover").hide();
        //$("#image_holder").css('background-image','none');
   });
   $("#rollover").hover(function(){
        $(this).show(); 
   },
   function(){
        $(this).hide();
   });
   
   //this only needs to go on the home page
   var $random =Math.floor(Math.random()*2);
   if($random == "1"){
    $("#testimonial").empty().html("<p>I was no stranger to SEO companies.  I had personally worked with 3 different ones in an effort to obtain top positioning in Google with minimal results. In addition, I had vetted dozens of other SEO companies looking for one that I felt could ring the cash register. Leadgenix emerged as a company willing to stand behind their work. I gave them a chance and within a matter of weeks we were where we wanted to be. Sales have increased by double digit percentages as a result. No arguing with those numbers.  Leadgenix worked.</p><p>I highly recommend using Leadgenix for your SEO needs!  They delivered just as they said they would for me!</p><p><b>McKay Thomas</b> - Billiardex</p>");
    $("#testimonial_image").empty().html("<img src=\"images/poolTables_03.jpg\" />");    
   }
   
    $("input").focus(function(){
        $(this).parents().find("#error_field").remove(); 
        $(this).removeClass('error');   
    });
    
    $("#free_eval").submit(function(){
        //first need to validate data
        var $error = false;
        $(this).find(":input").each(function(){
            $(this).find("#error_field").remove();
            $(this).removeClass('error');
            var $rule = $(this).attr('rule');
            var $string = $(this).val();
            if(!validate($string,$rule)){
                $(this).addClass('error');
                $error = true;
            }
        });
        if(!$error){
            $(this).find('#submit_button').remove().end().append("<center><img src='images/ajax-loader.gif' /></center>");
            $.post('includes/capture_data.php',$("#free_eval").serialize(),function(data){
                //need to capture the basic information and then slide the rest of the form out.
                $(this).find('input').each(function(){
                            $(this).attr('disabled','disabled');
                });
                $(this).find('table').css({
                    color: "#C0C0C0"
                });
                if(data == 'success'){
                    $("#free_eval").find('center').remove().end().append('<input type="image" src="images/getStartedLow_03.png" id="submit_button" disabled="disabled" />');
                    $("#form_back").show();
                    $("#free_eval").parent().animate({marginLeft:"-320px"});
                }else if(data == "regular"){
                    $("#free_eval").find('center').remove().end().append('<h2>Thank you for your submission.</h2>');
                }else{
                    alert(data);
                }
            
            });
        }else{
            $(this).find("#submit_button").before("<h6 style='color:red' id='error_field'>Some fields have been filled incorrectly.</h6>");
        }
        
        return false;
        
        
        
        return false;
    });
    
    $("#boost_submit").click(function(){
        //first need to validate data
        var $error = false;
        $("#boostability").find("input").each(function(){
            $(this).find("#error_field").remove();
            $(this).removeClass('error');
            var $rule = $(this).attr('rule');
            var $string = $(this).val();
            if(!validate($string,$rule)){
                $(this).addClass('error');
                $error = true;
            }
        });
        if(!$error){
            $.ajaxSetup({'async':'false'});
            $.post('includes/capture_data.php',$("#boostability").serialize(),function(data){
                if(data == 'boostability'){
                    $("#boostability").submit();    
                }else{
                    alert(data);
                }
            });
        }else{
            $(this).find("#submit_button").before("<h6 style='color:red' id='error_field'>Some fields have been filled incorrectly.</h6>");
        }      
    });
    
    $("#close_button").click(function(){
        $("#free_eval").parent().animate({marginLeft:"0px"});
        $("#free_eval").find('input').each(function(){
                    $(this).removeAttr('disabled');
        });
        $("#free_eval").find("#submit_button").attr('src','images/submit_button_07.png');
        $("#free_eval").find('table').css({
            color: "#000"
        });
        $("#form_back").hide();
        
    });
    
    function validate($string,$rule){
            switch($rule){
                case 'alpha':
                    var $regex ='[a-zA-Z]+';
                    break;
                case 'alphanum':
                    var $regex ='[a-zA-Z0-9]+';
                    break;
                case 'num':
                    var $regex ='[0-9]+';
                    break;
                case 'email':
                    var $regex = '[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+[.][a-zA-Z]{2,3}';
                    break;
                case 'phone':
                    var $regex = "[(][0-9]{3}[)] [0-9]{3}-[0-9]{4}";
                    break;
                case 'url':
                    var $regex = "(http://)?(www.)?[a-zA-Z0-9.-]+[.][a-zA-Z]{2,3}";
                    break;
                default:
                    return true;
            }
            if($string.search($regex) < 0){
                return false;
            }else{
                return true;
            }
}
    //newsletter signup stuff
    $("#newsletter_signup").find("input[name=email]").focus(function(){
        $(this).val('');
    });
    $("#newsletter_signup").find("input[name=email]").blur(function(){
        $(this).val( ($(this).val() == "")? 'your email address':$(this).val());
    });
    $("#newsletter_signup").submit(function(){
        if(validate($(this).find('input[name=email]').val(),'email')){
            $.post('includes/capture_data.php',$(this).serialize(),function(data){
                if(data == "success"){
                     $("#newsletter_signup").empty().html("<h2>Thank you for your submission.</h2>");
                }else{
                    alert(data);
                }
            });
        }else{
            $(this).find('input[name=email]').addClass('error');
            alert('The email you entered was in an incorrect format');
        }
        return false;
    });
});
