$(function(){
    $('.question').click(function(){
        if($(this).next('.answer').is(':visible')){
            $(this).next('.answer').hide();
        }else{
            $(this).next('.answer').show();
        }
    });
    
    $('.call').click(function(){
        var $url = window.location; 
        $url = String($url);       
        $url = $url.split('?');
        var $vars = $url[1].split('&');
        for($i = 0; $i < $vars.length; $i++){
            var $value = $vars[$i].split('=');
            var $n = $value[0];
            var $v = $value[1];
            $('#callForm').append('<input type="hidden" value="'+$v+'" name="'+$n+'"');
        }
        $.post('includes/cco_call.php',$('#callForm').serialize(),function(data){
            if(data == 'success'){
                $('#match').empty().html('<img src="images/thankYou.png" /><br /><br /><p style="text-align:center;>One of Check Citys loan processors will contact you shortly.</p>');    
            }else{
                alert(data);
            }        
        });
        return false;
    });
    
    $location = window.location.href;
    $location = $location.split('/');
    $location2 = $location[$location.length - 2];
    $location = $location[$location.length - 1];
    switch($location){
        case "faqs.php":
            $('#faqs').addClass('active');
            break;
        case "privacy.php":
            $('#privacy').addClass('active');
            break;
        case "terms.php":
            $('#terms').addClass('active');
            break;
        default:
            if($location2 == 'blog'){
                $('#blog').addClass('active');    
            }else{
                $('#home').addClass('active');
            }
    }
    
    $form = $('#cash');
    if($form.length > 0){
        $form.find('input').each(function(){ //this sets a default value so we can make sure it changes.
            $(this).data('i',$(this).val());
            
            $(this).focus(function(){ //actions for when focus is on input element
                $(this).removeClass('error'); //remove error class when focus is on element
                if($(this).val() == $(this).data('i')){
                    $(this).val('');
                }
            });
            $(this).blur(function(){
                if($(this).val() == "" ){
                    $(this).val($(this).data('i'));
                }  
            });
        });
        
       
        
        $form.find("input[name=phone]").mask("?(999) 999-9999").val('(Ph )   -');
        
        $form.submit(function(){
            $(this).find('.submit').replaceWith('<center><img src="images/ajax-loader2.gif" class="loader" /></center>');
            $error = false;
            $form.find('.form').each(function(){
                $(this).removeClass('error'); //remove errors
                if(!validate($(this).val(),$(this).attr('validation'))){
                    $(this).addClass('error');
                    $error = true;
                }else if($(this).val() == $(this).data('i')){
                    $(this).addClass('error');
                    $error = true;
                }
            });
            if(!$error){
                $.post('includes/mailer.php',$form.serialize(),function(data){
                    if(data == 'success'){
                        $form.empty().html('<img src="images/thankYou.png" />');
                    }else if(data == 'good state'){
                        window.location = 'match.php?'+$form.serialize();
                    }else{
                        alert(data);
                    }
                });
            }else{
                 $(this).find('.loader').replaceWith('<input type="submit" value="" class="submit">');    
            } 
            return false;   
        });
    
    }
    
     function validate($string,$rule){   //function that validates data - always returns true if their are no validation requirements
            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 'zip':
                    var $regex ='[0-9]{5}';
                    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 'date':
                    $array = $string.split('/'); //first we split the string to make sure that it has all its parts
                    if($array.length = 3){ //if the array doesn't have 3 parts, it auto fails
                        if($array[0] > 12){ // if the month is greater than 12 it fails
                            return false;
                        }else if($array[1] > 31){ //if the day is greater than 31 it fails
                            return false;
                        }else if($array[2] < 2010){ //if the date is less than 2010 it fails -- only for this id
                            return false;
                        }
                    }else{
                        return false;
                    }
                    var $regex = "[0-1]{1}[0-9]{1}[/][0-3]{1}[0-9]{1}[/][0-9]{4}";
                    break;
                case 'select':
                    if($string == ''){ //this assusmes that the default select option will have no value
                        return false;
                    }else{
                        return true;
                    }
                    break;
                default:
                    return true;
            }
            if($string.search($regex) < 0){
                return false;
            }else{
                return true;
            }
     }
     

     
})
