tinyMCE.init({
    // General options
    mode : "exact",
    elements : "article",
    theme : "advanced",
    plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",

    // Theme options
    theme_advanced_buttons1 : "bold,italic,underline,|,bullist,numlist,blockquote,link",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    theme_advanced_buttons4 : "",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,


    // Example word content CSS (should be your site CSS) this one removes paragraph margins
    content_css : "css/word.css",

    // Drop lists for link/image/media/template dialogs
    template_external_list_url : "lists/template_list.js",
    external_link_list_url : "lists/link_list.js",
    external_image_list_url : "lists/image_list.js",
    media_external_list_url : "lists/media_list.js"

});

jQuery(document).ready(function(){
    jQuery('#submission_form_form').submit(sf_validate_submit);
});

function sf_validate_submit(){    
    __sf_err = false;
    var thisform = jQuery(this);
    
    jQuery('#submit_save, #submit_update').attr('disabled','disabled');    
    jQuery('#sf_err_message').hide();
    jQuery('#sf_saved_message').hide();
    
    jQuery('#article').val(tinyMCE.activeEditor.getContent());
    
    thisform.contents().find('input.required, textarea.required').each(function(){
        jQuery(this).parent().removeClass('err_field');
        if(!jQuery.trim(jQuery(this).val()) || (jQuery(this).hasClass('email') && !sf_is_email(jQuery.trim(jQuery(this).val())))){
            jQuery(this).parent().addClass('err_field');
            __sf_err = true;
        }
    });
    if(__sf_err){
        jQuery('#sf_err_message').fadeIn();
        jQuery('#submit_save, #submit_update').removeAttr('disabled'); 
        return false;
    }
    jQuery('#sf_progress_message').fadeIn();
    jQuery.ajax({
        type: "POST",
        url: location.href,
        dataType: 'json',
        data: thisform.serialize(),
        success: function(resp){
            if(parseInt(resp.post_id) > 0){
                location.href = location.href.replace(/#(.*)$/,'')+'?sent=1';
                /*
                jQuery('#sf_post_id').val(resp.post_id);
                jQuery('#submit_save').fadeOut('fast', function(){jQuery('#submit_update').fadeIn()});
                jQuery('#sf_saved_message').fadeIn();
                jQuery('#sf_new_submission_link').show();            
                jQuery('#sf_captcha').parent().parent().remove();    
                */
            }else if(resp.captcha_error){
                jQuery('#sf_captcha').parent().addClass('err_field');
                jQuery('#sf_err_message').fadeIn();
                jQuery('#submit_save').removeAttr('disabled');    
            }
            jQuery('#submit_update').removeAttr('disabled');    
            jQuery('#sf_progress_message').hide();            
        } 
    });
    
    return false;
}

function sf_is_email(str){
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){return false;}
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;}
    if (str.indexOf(at,(lat+1))!=-1){return false;}
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
    if (str.indexOf(dot,(lat+2))==-1){return false;}
    if (str.indexOf(" ")!=-1){return false;}
    return true                    
}
