

function submitForm(){
    if($('#submitbutton').data('sending')=='yes'){
        return;
    }
    $('#submitbutton').data('sending','yes');


    $('#submitbutton').data('wrong',0);
    $('#formindy input').each(function(i){
        if($(this).hasClass('grequired')){
            if($(this).val()==''){
                $('#submitbutton').data('wrong',1);
                $(this).parent().css('border-color','white');
            }else{
                $(this).parent().css('border-color','#7c151a');                
            }
        }
    });
    if($('#submitbutton').data('wrong')==1){
        $('#submitbutton').data('sending','no');
        return;            
    }      
    
    var addData = { jobs: 0, dir: 0, leads: 0, discounts: 0 };
    if($('#gjobs').hasClass('active')){ addData.jobs = 1;}
    if($('#gdir').hasClass('active')){ addData.dir = 1;}
    if($('#gleads').hasClass('active')){ addData.leads = 1;}
    if($('#gdiscounts').hasClass('active')){ addData.discounts = 1;}
    var params = { data: addData, success: function(responseText, statusText){ showSent(responseText, statusText); $('#submitbutton').data('sending','no'); }};    
    $('#formindy').ajaxSubmit(params);        
    
}

function showSent(responseText, statusText){
    var whe = 180;
    $('#submitbutton').data('toclear',0);
    if(typeof(responseText) === "undefined"){
        $('#popupinfo').html('<h1><span class="green">Ups...</span></h1><h3>There was an unexpected problem.</h3>');        
    }else if(responseText.substring(0,2)=='OK'){        
        $('#popupinfo').html('<h1><span class="green">Thank you!</span></h1>');
        whe = 140;
        $('#submitbutton').data('toclear',1)
    }else{
        var msg = responseText.substring(3);
        $('#popupinfo').html('<h1><span class="green">Ups...</span></h1><h3>'+msg+'</h3>');        
    }
    
        
    $('#overlay').css('height',$('#ibody').outerHeight()+'px').css('top',$('#ibody').offset().top).fadeIn(500,function(){
        $('#popupinfo').css('top',$('#ibody').offset().top+150).fadeIn(500);    
    });  
    
        
}

function resetForm(){
    if($('#submitbutton').data('toclear')==1){
        $('#submitbutton').data('toclear',0);
    
        $('#formindy input').each(function(i){
            $(this).val('');
        });
        $('.gcheckboxinput').each(function(i){  
            if($(this).hasClass('active')){ $(this).removeClass('active'); }
        });
    }
    
    
    $('#popupinfo').fadeOut(500,function(){
        $('#overlay').fadeOut(200);    
    });
}

$(document).ready(function(){

    $('.gcheckboxinput').each(function(i){ $(this).bind('click',function(event){ 
        if($(this).hasClass('active')){ $(this).removeClass('active'); }else{ $(this).addClass('active');}
    });});
    $('#formindy').ajaxForm(); 
    $('#submitbutton').bind('click',function(){ submitForm(); });
    
    $('#overlay').bind('click',function(){ resetForm(); });
    $('#popupinfo').bind('click',function(){ resetForm(); });
    
    
});
