<script type="text/javascript">
/**
  *  jQuery Validation 
  * Copyright naxialis 2012
  * /
  */
(function($,W,D)
{
    var JQUERY4U = {};

    JQUERY4U.UTIL =
    {
        setupFormValidation: function()
        {
            //form validation rules
            $("#new_post").validate({
			 errorLabelContainer: $("ul", $('div.error-container')),
             wrapper: 'li',
                rules: {
                    title: "required",
					 description: {
                        required: true,
                        maxlength: 1216
                    },
					
                    <!--title_link: "required",-->
					winerating: "required",
					post_tags: "required"
                },
                messages: {
					
                    title: "Merci d'ajouter un titre",
                    description: "Merci de remplir la description",
					description: {
                        required: "Merci de remplir la description",
                        maxlength: "Votre description doit contenir maximum 200 mots",
                    },
					winerating: "Merci d'ajouter votre tel et ou email de contact ",
					<!--title_link: "Merci d'ajout&eacute; l'intitul&eacute; de votre lien",-->
					post_tags: "Merci d'ajouter vos mots cl&eacute;s",
                   
                },
                submitHandler: function(form) {
                    form.submit();
                }
            });
        }
    }

    //when the dom has loaded setup form validation rules
    $(D).ready(function($) {
        JQUERY4U.UTIL.setupFormValidation();
    });

})(jQuery, window, document);
</script>
<script type="text/javascript">
jQuery(document).ready(function($){
$.validator.addMethod("image",
function(value,element){if($(element).attr('type')=="file"
&&($(element).hasClass('required')
||$(element).get(0).value.length>0))
return value.match(/\.(png|gif|jpg|jpeg])$/i);
else
return true;},

"Le fichier doit &ecirc;tre au format JPG, GIF ou PNG.");

$.validator.addMethod("filesize",
function(value,element){
if(window.File&&window.FileList){
if($(element).attr('type')=="file"
&&($(element).hasClass('required')
||element.files.length>0)){
var size=0;var $form=$(element).parents('#new_post')
.first();var $fel=$form.find('input[type=file]');
var $max=$form.find('input[name=MAX_FILE_SIZE]')
.first();if($max){for(var j=0,fo;fo=$fel[j];j++){
files=fo.files;for(var i=0,f;f=files[i];i++){size+=f.size;}}
return size<=$max.val();}}}
return true;},
"Votre JPG, GIF ou PNG ne dois pas d&eacute;passer : 1Mo");
});

</script>
 <style>

.word-good { color: #F00; }
#description{ margin-bottom:0;}
.wordCount{  clear:both; margin-left:889px;border:solid 1px #b7ddf2; border-top:none; width:80px; margin-top:0; margin-bottom:15px; background-color:#FFFFFF; text-align:center;}
.error-container ul li{ list-style:none;}
.error-container label.error {
    color: #FB3A3A;
    display: inline-block;
    margin: 0 0 0 10px;
    padding: 0;
    text-align: left;
    width: 500px;
}
</style>
<?php

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

	// Do some minor form validation to make sure there is content
	if (isset ($_POST['title'])) {
		$title =  $_POST['title'];
	} else {
		echo '';
	}
	if (isset ($_POST['description'])) {
		$description = $_POST['description'];
	} else {
		echo '';
	}

	$tags = $_POST['post_tags'];
	$winerating = $_POST['winerating'];
	//$title_link = $_POST['title_link'];

	// ADD THE FORM INPUT TO $new_post ARRAY
	$new_post = array(
	'post_title'	=>	$title,
	'post_content'	=>	$description,
	'post_category'	=>	array($_POST['cat'],70),
	'tags_input'	=>	array($tags),
	'post_status'	=>	'draft',           // Choose: publish, preview, future, draft, etc.
	'post_type'	=>	'post',  //'post',page' or use a custom post type if you want to
	'winerating'	=>	$winerating
	//'title_link'	=>	$title_link
	);

	//SAVE THE POST
	$pid = wp_insert_post($new_post);

             //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
	wp_set_post_tags($pid, $_POST['post_tags']);

	//REDIRECT TO THE NEW POST ON SAVE
	$link = get_permalink( $pid );
	$redirect_publish_press = get_option('url_id_code_press');
	wp_redirect( $redirect_publish_press );

	//ADD OUR CUSTOM FIELDS
	add_post_meta($pid, 'rating', $winerating, true); 
	//add_post_meta($pid, 'intitule', $title_link, true);

	//INSERT OUR MEDIA ATTACHMENTS
	if ($_FILES) {
		foreach ($_FILES as $file => $array) {
		$newupload = insert_attachment($file,$pid);
		// $newupload returns the attachment id of the file that
		// was just uploaded. Do whatever you want with that now.
		}

	} // END THE IF STATEMENT FOR FILES

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');
$cat_press_free = get_option('press_category_free') ;
$args_cat = array(
        'tab_index' => '10',
        'taxonomy' => 'category',
        'hide_empty' => 0,
        'include' => $cat_press_free
    );
?>
<div class="error-container">
            <ul>
            </ul>
        </div>
		<div class="wpcf7">
		<form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
			<!-- post name -->
			<fieldset name="name">
				<label class="title-front" for="title">Titre de votre annonce :</label>
				<input type="text" id="title" value="" tabindex="5" name="title" />
			</fieldset>

		
			<!-- post Content -->
			<fieldset class="content">
				<label for="description">Description : max 200 mots </label>
				<textarea id="description" tabindex="15" name="description" class="count[0,200]" cols="100" rows="20"></textarea>
			</fieldset>
            
            <fieldset class="category">
				<label for="cat">Cat&eacute;gorie:</label>
                <div class="category-front">
				<?php wp_dropdown_categories($args_cat); ?>
                </div>
			</fieldset>
            

			<!-- contact-->
			<fieldset class="winerating">
				<label for="winerating">Adresse email ou t&eacute;l pour &ecirc;tre contact&eacute;: </label>
				<input type="text" value="" id="winerating" tabindex="20" name="winerating" />
                <div class="clear"></div>	
              <small><em>(les coordonn&eacute;es de contact seront uniquement visible pour les membres de la pr&eacute;vention)</em></small><br /><br />
			</fieldset>
          <!-- champ supplémentaire
			<fieldset class="title_link">
				<label for="title_link">intitul&eacute; du lien : </label>
				<input type="text" value="" id="title_link" tabindex="20" name="title_link" />
			</fieldset>
            -->
            
            <!-- images -->
			<fieldset class="images">
				<label for="bottle_front"> Image  : </label> 
                <div class="file">
                <input type="hidden" name="MAX_FILE_SIZE" value="1048576"/>
				<input  type="file" name="bottle_front" id="bottle_front" class="filesize image" size="55" onchange="return validateFileExtension(this)" />
                <span class="button-annonces">Choisir une image</span>
                
               </div><br />
              <small><em>(fichier doit &ecirc;tre au format JPG, GIF ou PNG, et ne doit pas d&eacute;passer : 1Mo )</em></small><br /><br />
			</fieldset>
            
            
             
            
			<!-- post tags -->
			<fieldset class="tags-2">
				<label for="post_tags">Mots cl&eacute;s <em>(s&eacute;par&eacute;s une virgules)</em>:</label>
				<input type="text" value="" tabindex="35" name="post_tags" id="post_tags" />
			</fieldset>
            
            <div class="tag-annonces">
            <h5><strong>Mot(s) disponible(s) :</strong></h5>
            <ul>
    <?php
        query_posts('cat=9');
        if (have_posts()) : while (have_posts()) : the_post();
           if( get_the_tag_list() ){
              echo get_the_tag_list('<li>','</li><li>','</li>');
           }
        endwhile; endif; 
        wp_reset_query(); 
    ?>
</ul>
            
            </div>

			<fieldset class="submit">
				<input type="submit" value="Envoyer" tabindex="40" id="submit" name="submit" />
			</fieldset>

			<input type="hidden" name="action" value="new_post" />
			<?php wp_nonce_field( 'new-post' ); ?>
		</form>
        </div>