jQuery.download = function(url, data, method){
    //url and data options required
    if( url && data ){ 
        //data can be string of parameters or array/object
        data = typeof data == 'string' ? data : jQuery.param(data);
        //split params into form inputs
        var inputs = '';
        jQuery.each(data.split('&'), function(){ 
            var pair = this.split('=');
            inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />'; 
        });
        //send request
        jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
        .appendTo('body').submit().remove();
    };
};


$(function(){
    $(".sidebarMenu").find('li ul li').hide();
    $(".sidebarMenu").find('li').hover(
    //hover in
    function(){
        var $submenu = $(this).find("ul");
        if($submenu){
            $submenu.find('li:hidden').slideDown('slow');
        }
    },
    //hover out
    function(){
        $(this).find("ul li:visible").slideUp('slow');
    });
    
    
    $("img[rel]").overlay({effect: 'apple'});
    
    $('.report').find('input').each(function(){
        $(this).removeClass('error');
    });
    
    $('.report').submit(function(){
        var $error = false; //If $error remains false the form will validate and submit
        $(this).find("input").each(function(){
            $(this).removeClass('error'); //removes the error class from the input elements while we try validation
            var $rule = $(this).attr('validation'); //This is the attribute added to each of the input elements that specifies what rules they must have
            var $string = $(this).val();
            if(!validate($string,$rule)){
                $(this).addClass('error');
                $error = true;
            }
        });
        if(!$error){
            var $id = $(this).attr('id');
            switch($id){
                case 'freeReport1':
                    $file = "VGS Analytics and Response.pdf";    
                    break;
               case 'freeReport2':
                    $file = "VGS Donor Support Strategy.pdf";    
                    break;
               case 'freeReport3':
                    $file = "VGS DM Package Design.pdf";    
                    break;   
            }
            //mail the form 
            $.post('includes/mailer.php',$(this).serialize(),function(data){
                if(data == 'success'){
                    $.download('includes/download.php','download_file='+$file,'GET');    
                }else{
                    alert(data);
                }
            }); 
            closeAll();
;
        }else{
            alert('There were some problems with the form, they have been highlighted');  //alerts if there are problems with the form
        }
        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;
        }
    }
    
    // close all overlays
function closeAll() {
    $("img[rel]").each(function() {
        $(this).overlay().close();
    });
}

})