jQuery.fn.exists = function(){return jQuery(this).length>0;}
$(function(){
    
    
    //var $width = ($(window).width() / 2);
//    
//    $('#overlayPiece').css('width',$width);
    
    /*
	if( $('.podcasts').exists() ){
    
        $('.podcasts').find('a').each(function(){
            if($(this).attr('href').search('.mp3') > 0){
                $(this).addClass('podcastAnchor');
            }
        });
        
        $('#player').attr('href',$('.podcasts').find('.podcastAnchor:first').attr('href')); //changes the player to the first podcast
        $('.title').html($('.podcastAnchor:first').html()); //changes the player to the first podcast
        
       // var $player = $f("player","http://www.leadgenix.com/design/paradigmlife/wp-content/themes/iva/scripts/flowplayer-3.2.1.swf",{
		  var $player = $f("player","http://www.paradigmlife.net/wp-content/themes/iva/scripts/flowplayer-3.2.1.swf",{
                // fullscreen button not needed here
             canvas: {backgroundColor: "#a3a48e"},
             plugins: {
                    controls: {
                        fullscreen: false,
                        height: 30,
                        autoHide: false,
                        backgroundGradient: [0.5,0,0.3],
                          bufferColor: '#445566',
                          buttonOverColor: '#ffffff',
                          sliderBorder: '1px solid rgba(128, 128, 128, 0.7)',
                          volumeSliderColor: '#ffffff',
                          progressColor: '#a3a48e',
                          progressGradient: 'none',
                          autoHide: 'never',
                          volumeBorder: '1px solid rgba(128, 128, 128, 0.7)',
                          sliderGradient: 'none',
                          backgroundColor: '#7a7a7a',
                          volumeSliderGradient: 'none',
                          timeSeparator: ' ',
                          tooltipTextColor: '#ffffff',
                          timeBgColor: 'rgb(0, 0, 0, 0)',
                          tooltipColor: '#000000',
                          timeBorder: '0px solid rgba(0, 0, 0, 0.3)',
                          timeColor: '#ffffff',
                          borderRadius: '0px',
                          durationColor: '#171717',
                          buttonColor: '#ffffff',
                          sliderColor: '#000000',
                          volumeColor: '#ffffff',
                          bufferGradient: 'none',
                          height: 24,
                          opacity: 1.0
                    },
                    audio: {
                        //url: 'http://www.leadgenix.com/design/paradigmlife/wp-content/themes/iva/scripts/flowplayer.audio-3.2.0.swf'
						url: 'http://www.paradigmlife.net/wp-content/themes/iva/scripts/flowplayer.audio-3.2.0.swf'
                    }
                },
                 clip:{
                    autoPlay: false
//                    baseUrl: 'http://localhost/paradigmlife/wordpress/wp-content/themes/iva/scripts/'
                },
                key: '#@dfb7032a94be1a3b894'
        });
        
        $('a.podcastAnchor').click(function($event){
            $event.preventDefault();
            $('.title').html($(this).html());
            $f("player").play({'url':$(this).attr('href')});
            $(".podcastplayer").focus();
        });

    }*/
    
    $('form').find("input[name=phone]").mask("?(999) 999-9999").val('Phone'); //maskedinput plugin
    
    $('form').find('input').each(function(){
        $(this).data('default',$(this).val());
        $(this).focus(function(){ //if the default value is still the same remove
            $(this).val( ($(this).val() == $(this).data('default'))? '' : $(this).val() ); 
        });
        $(this).blur(function(){ //if blank add default value
            $(this).val( ($(this).val() == '')? $(this).data('default') : $(this).val() );    
        });
    });
    
    $('form').submit(function(){ //on form submit
        
        $error = false; //this will stay false if there are no errors
        
        $(this).find('input').each(function(){ //first remove all errors from inputs in the form
            $(this).removeClass('error');
            
            if( !validate($(this).val(),$(this).attr('validation')) ){
                $error = true; //sets error to true because found javascript validation issue
//                $(this).addClass('error'); //adds the error class to the element
                switch($(this).attr('validation')){
                    case 'email':
                        $(this).after('<br /><span class="errorText">Insert a valid email</span>');
                        break;
                    case 'phone':
                        $(this).after('<br /><span class="errorText">Insert a valid phone number</span>');
                        break;
                }
            }
            
        });
        
        if(!$error){ //if no errors we pass it on to what its supposed to do
            $vars = $(this).serialize();
            switch($(this).attr('id')){
                
                case 'infiniteBanking':
                    $(this).find('.download').remove().add('<img src="http://www.leadgenix.com/design/paradigmlife/wp-content/themes/iva/images/ajax-loader.gif" />');
                    $.post('wp-content/themes/iva/php/form_mailer.php', $vars, function(data){
                        if(data == 'success'){
//                            window.location = ('thank-you.php');
                            $('#infiniteBanking').empty().html('<div class="thank-you1"></div>'); 
//                            pageTracker._trackPageview("/confirm-report.php");
                            _gaq.push(['_trackPageview', '/confirm-report.php']);
                        }else{
                            alert('Our System is experiencing technical difficulties');
                            alert(data);
                        }
                    });
                    return false;
                    break;
                case 'annuitiesForm':
                    $(this).find('.download').remove().add('<img src="http://www.leadgenix.com/design/paradigmlife/wp-content/themes/iva/images/ajax-loader.gif" />');
                    $.post('wp-content/themes/iva/php/form_mailer2.php', $vars, function(data){
                        if(data == 'success'){
//                            window.location = ('thank-you.php');
                            $('#annuitiesForm').empty().html('<div class="thank-you1"></div>'); 
                            _gaq.push(['_trackPageview', '/confirm-report.php']);
//                            pageTracker._trackPageview("/confirm-report.php");
                        }else{
                            alert('Our System is experiencing technical difficulties');
                            alert(data);
                        }
                    });
                    return false;
                    break;    
                case 'newsletterSignup':
                    $.post('wp-content/themes/iva/php/newsletter.php', $vars, function(data){
                        if(data == 'success'){
//                            window.location = ('thank-you.php');
                              $('#newsletterSignup').empty().html('<div class="thank-you2"></div>'); 
                              _gaq.push(['_trackPageview', '/confirm-newsletter.php']);
//                              _gaq._trackPageview("/confirm-newsletter.php");
                            
                        }else{
                            alert('Our System is experiencing technical difficulties');
//                            alert(data);
                        }
                    });
                    return false;
//                    return true;
                    break;
            }
        }else{
            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;
        case 'required':
            if($string == '' || $string == null){ //this assusmes that the default select option will have no value 
                return false;       
            } 
            break;
        case 'money':
            $regex = '($)?([0-9]{3})?([0-9]{3})?([0-9]{3})[.]([0-9]{2})';    //this regex will accept numbers up to 100 million
            break;
        default:
            return true;
    }
    if($string.search($regex) < 0){
        return false;
    }else{
        return true;
    }
}

