<?php
include("includes/site_info.php");
include('session_handler.inc');

$WUserID = $_SESSION['Sess_UserId'];
$WUsername = $_SESSION['Sess_UserName'];

if ($_REQUEST['aid']) { $aid = $_REQUEST['aid']; }
else { $aid = 0; }

$articleitem_query=mysql_query("SELECT *, UNIX_TIMESTAMP(art_datetime) as artdate FROM articles WHERE art_userid = '$WUserID' AND art_approved='Y' AND art_id='$aid'") or die(mysql_error());
$articleitem_TOTAL = mysql_num_rows($articleitem_query);
$article = mysql_fetch_object($articleitem_query);

?>
<div id="top_panel" class="grey_row_back">
  <?php include ("panel_top.php"); ?>
</div>

<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
   	jQuery('#edit_note').ajaxForm({
   	type: "post",
   	url: "article_submit.php",
   	data: { Submit: 'edit_note'},
    beforeSubmit: validate,
   	success: function(result){
		jQuery('#loading').hide();	
	 	jQuery('#right_loading_c').hide();
 		//jQuery("#msg_content_error").fadeIn('slow');
 		//jQuery('#msg_content_error').html(result);
		result = JSON.parse(result);
		
				if (result.error){
		 		jQuery("#msg_content_error").fadeIn('slow');
	 			jQuery('#msg_content_error').html('Ops! Error, please try again!');
				if (jQuery.isArray(result.error)==true){
				jQuery.each(result.error, function(key, value) { 
 					jQuery('#msg_content_error').append('<br>'+value);
				});
				} else {
 					jQuery('#msg_content_error').append('<br>'+result.error);
				}
				}
				if (result.success){
 				jQuery("#msg_content_done").fadeIn('slow');
 				jQuery('#msg_content_done').html('Wow, Your request was done!');
				if (jQuery.isArray(result.success)==true){
				jQuery.each(result.success, function(key, value) { 
 					jQuery('#msg_content_done').append('<br>'+value);
				});
				} else {
 					jQuery('#msg_content_done').append('<br>'+result.success);
				}
						setTimeout(function() {
						if(result.status == '1') { 
			location.href='#cshownote?tab=thought&aid=<?=$article->art_id?>';
			jQuery.get('showarticle.php?tab=thought&aid=<?=$article->art_id?>', function(data) {
 				jQuery("#main_content").fadeIn('slow');
 				jQuery('#main_content').html(data);
			});
			}  else if (result.status == '2') { 
			location.href='#cnote?tab=thought';
			jQuery.get('article.php?tab=thought', function(data) {
 				jQuery("#main_content").fadeIn('slow');
 				jQuery('#main_content').html(data);
			});
			}
			}, 1000);

				}
		
	}
 });
});
 function validate(formData, jqForm, options) { 
    var title = jQuery('input[name=title]').fieldValue(); 
    var post = jQuery('textarea[name=txtArticle]').fieldValue(); 
    var file = jQuery('input[name=image_upload]').fieldValue(); 
    var privacy = jQuery('input[type=radio]').fieldValue(); 
 
	if ( !title[0] )
    {
      alert("Title is empty!");
      return false;
    }
	if ( !post[0] )
    {
      alert("Post is empty!");
      return false;
    }
    /* Checking file type */
    var re_text = /\.jpeg|\.jpg|\.gif|\.png|\.JPG|\.GIF|\.PNG/i; 

	var filename = file[0];
	if (filename!=''){
		if (filename.search(re_text) == -1)
    	{
        	alert(filename + " is not a valid file. Please select a file with JPEG / GIF / PNG  format!"); 
        	return false;
    	}
	}
	if ( !(privacy[0] || privacy[1]))
    {
      alert("Invalid Privacy Setting!");
      return false;
    }
    /* Checking agree check */
	 jQuery('#loading').show();
	 jQuery('#right_loading_c').show();
}

   </script>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="5" class="white_back_t_xb">
        <?php if ($articleitem_TOTAL > 0){ ?>
        <tr>
          <td><div class="grey_12 pink_row_back solid_line_separator padding_5">Edit Note -
              <?=$article->art_title?>
            </div></td>
        </tr>
        <tr>
          <td valign="top" width="100%"><div id="msg_content_error" style="display:none;"></div>
            <div id="msg_content_done" style="display:none;"></div>
            <div id="sub_content">
              <form method="post" enctype="multipart/form-data" name="edit_note" id="edit_note" style="padding:0; margin:0; border:0;">
                <input name="userid" type="hidden" value="<?=$WUserID?>" />
                <input type="hidden" name="articleid" value="<?=$article->art_id?>">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td><table width="100%" border="0" cellspacing="0" cellpadding="5" class="white_back_t_xb">
                        <tr>
                          <td valign="top">Title: </td>
                          <td width="100%" valign="top"><input type="text" size="40" name="title" id="title" value="<?=$article->art_title?>" class="grey_12_xb"/></td>
                        </tr>
                        <tr>
                          <td valign="top">Post:</td>
                          <td valign="top"><textarea id="txtArticle" name="txtArticle" rows="20" cols="80" class="grey_12_xb"><?=$article->art_message?></textarea></td>
                        </tr>
                        <tr>
                          <td valign="top">Tags:</td>
                          <td valign="top"><input type="text" size="40" name="tags" value="<?=$article->art_tags?>" class="grey_12_xb"/></td>
                        </tr>
                        <tr>
                          <td valign="top">Add&nbsp;photo&nbsp;to&nbsp;my&nbsp;note:</td>
                          <td valign="top"><input type="file" name="image_upload" size="30"/></td>
                        </tr>
                        <tr>
                          <td valign="top">Privacy:</td>
                          <td valign="top"><input name="privacy" type="radio" value="N" <?php if ($article->art_private =='N') echo "checked"; ?>/>
                            Public
                            <input name="privacy" type="radio" value="Y" <?php if ($article->art_private =='Y') echo "checked"; ?>/>
                            Private</td>
                        </tr>
                        <tr>
                          <td valign="top">Delete&nbsp;this&nbsp;note:</td>
                          <td valign="top"><input type="checkbox" name="delete"  id=="delete" value="1" /></td>
                        </tr>
                        <tr>
                          <td valign="top">&nbsp;</td>
                          <td valign="top">&nbsp;</td>
                        </tr>
                      </table>
                      <div class="left solid_line"><img src="images/spacer.gif" width="1" height="1" /></div></td>
                  </tr>
                  <tr>
                    <td align="center"><input name="save" type="submit" class="button" id="save" value="Save Changes"/>
                      
                      <input name="cancel" type="button" class="button" id="cancel" value="Cancel" onclick="javascript: callpage('#cshownote?tab=thought&aid=<?=$article->art_id?>');"/></td>
                  </tr>
                  <tr>
                    <td align="center">&nbsp;</td>
                  </tr>
                </table>
              </form>
            </div></td>
        </tr>
        <?php  } else {
?>
        <tr class="lightgrey_10">
          <td align="center"><a title="Add Note" href="#caddnote?tab=thought" onclick="javascript: callpage(this.href);">Add Note</a> | <a href="#cnote?tab=thought" onclick="javascript: callpage(this.href);">Back to Note</a></td>
        </tr>
        <tr class="solid_line_separator">
          <td align="center"><div class="padding_10">Can't find a note to view.</div></td>
        </tr>
        <?php
	die;
}
?>
      </table></td>
  </tr>
</table>
