<?php
global $wpdb;
global $js_values;
$CSVid = array();
$CSVyear = array();
$CSVmonth = array();
$CSVday = array();
$CSVtime = array();
$CSValert = array();
$stra = array();
$indf=0;
$tbname = "qg_calendar";

//----------- Get month and Year -------------------------------
          if(isset($_REQUEST["yearr"])){
	     $nowyear = $_REQUEST["yearr"];
	     $nowmonth = $_REQUEST["monthh"];
	     $nowmonthname = trim($_REQUEST["monthname"]); //monthname     
	  }else{
             $nowyear = date("Y");
	     $nowmonth = date("m");
	     $nowmonthname = date("F");
	  }



//--------- Read Calendar --------------------
 $qgjsc = sprintf("%s/quotegenerator/assets/js/qg_query.js", plugins_url());
 $calendar_event ='';
 
 $sql12 = "SELECT * FROM ".$wpdb->prefix.$tbname." WHERE visible='yes' AND field_1='$nowyear' AND field_2='$nowmonth' ORDER BY id";  
 $moderation3 = $wpdb->get_results($sql12, ARRAY_A); 
	foreach ( (array) $moderation3 as $row ) {
	   $calendar_event .= mysql_real_escape_string( $row["field_1"].",".$row["field_2"].",".$row["field_3"].",".$row["field_4"].",".$row["field_5"].html_entity_decode("\r\n"));
           
	   $CSVid[$indf]  = $row["id"];
	   $CSVyear[$indf]  = $row["field_1"];
	   $CSVmonth[$indf] = $row["field_2"];
	   $CSVday[$indf]   = $row["field_3"];
	   $CSVtime[$indf]  = $row["field_4"];
	   $CSValert[$indf] = stripslashes($row["field_5"]);
	
	   $indf++;
	}
 //array_multisort($CSVday, SORT_NUMERIC, $CSVyear, $CSVmonth, $CSVtime, $CSValert, $CSVid);
 

//----------------------- Time table ---------------------------

   $time_out = '';
   $option_name = 'setting_qg_time';
   $new_value = '07:00 AM;08:00 AM;09:00 AM;10:00 AM;11:00 AM;12:00 PM;01:00 PM;02:00 PM;03:00 PM;04:00 PM;05:00 PM;06:00 PM;07:00 PM';
   
        if ( get_option($option_name) !== false ) {
             $new_value = get_option( $option_name );
        }  else  {
            add_option( $option_name, $new_value, '','yes' );
        }
	
    $timesx = explode(";",$new_value);
    function sortFunction( $a, $b ) { return strtotime($a) - strtotime($b);}
    usort($timesx, "sortFunction");
    
    for($i4=0;$i4<count($timesx);$i4++) {
          $time_out .= '<option value="'.$timesx[$i4].'"> '.$timesx[$i4].' </option>';
    }
    
   $imaghelp = sprintf("%s/quotegenerator/assets/img/help.png", plugins_url());
   $calvalues = array('','January','February','March','April','May','June','July','August','September','October','November','December','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
   $js_values = '<script type="text/javascript">var mySplitXt=new Array;var Cyy=new Array;var Cmm=new Array;var Cdd=new Array;var Ctt=new Array;var Caa=new Array;var now = new Date;var sccd=now.getDate();var sccm=now.getMonth();var sccy=now.getFullYear();var selectedd, selectedm, selectedy;var updobj;var mnn=new Array(31,28,31,30,31,30,31,31,30,31,30,31);var mnl=new Array(31,29,31,30,31,30,31,31,30,31,30,31);var calvalarr=new Array(42); var ccm = '. intval($nowmonth-1).';  var NMONTHM = '. intval($nowmonth-1).';  var NYEARS = '. $nowyear.';  var ccy= '. $nowyear.';  var DateList = "'.$calendar_event.'";  var mn=new Array("'.$calvalues[1].'","'. $calvalues[2].'","'. $calvalues[3].'","'.$calvalues[4].'","'. $calvalues[5].'","'. $calvalues[6].'","'. $calvalues[7].'","'. $calvalues[8].'","'. $calvalues[9].'","'. $calvalues[10].'","'. $calvalues[11].'","'. $calvalues[12].'"); prepcalendar("",NMONTHM,NYEARS);</script>';
?>


 <div class="head1cl">
        <img src="<?php echo $imaghelp; ?>" class="cq38"alt="help" onclick="cshownew('calend_settings');">
	<img src="<?php echo sprintf("%s/quotegenerator/assets/img/addevent.png", plugins_url()); ?>" class="cq38" alt="Addevent" onclick="cshownew(1);">
	<img src="<?php echo sprintf("%s/quotegenerator/assets/img/event.png", plugins_url()); ?>" class="cq38" alt="Event" onclick="cshownew(2);">
	<img src="<?php echo sprintf("%s/quotegenerator/assets/img/language.png", plugins_url()); ?>" class="cq38" alt="Language" onclick="cshownew('langsettings');">
 </div>
 
 <div id="langsettings">
   <div class="cq37"><img src="<?php echo sprintf("%s/quotegenerator/assets/img/no.png", plugins_url()); ?>" alt="no" onclick="document.getElementById('langsettings').style.display = 'none';"></div>
   
   <table class="tbl9cl"> 
   <?php
      $calname = array('','Month #1','Month #2','Month #3','Month #4','Month #5','Month #6','Month #7','Month #8','Month #9','Month #10','Month #11','Month #12','Day of the week #1','Day of the week #2','Day of the week #3','Day of the week #4','Day of the week #5','Day of the week #6','Day of the week #7');

      for($xv=1; $xv<=19; $xv++){
          $option_name = 'setting_qgc'.$xv;
    	     if ( get_option($option_name) == false ) {
                  add_option( $option_name, $calvalues[$xv], '', 'yes' );
	     }else{
		$calvalues[$xv] = get_option( $option_name );
		echo '<tr class="cq35"><td class="cq36">'.$calname[$xv].'</td><td><input name="setting_qgc'.$xv.'" type="text" class="calday" value="'.$calvalues[$xv].'" /></td></tr>';
	     }
      }
      echo '<tr class="cq35"><td colspan="2" class="cq34"><a href="javascript:save_calname();" class="but">&nbsp; &nbsp; &nbsp; Save &nbsp; &nbsp; &nbsp;</a></td></tr>';
   ?>
   </table>
 </div>

 <div id="calend_settings">
      <div class="cq33">
	 <img src="<?php echo sprintf("%s/quotegenerator/assets/img/no.png", plugins_url()); ?>" alt="no" onclick= "document.getElementById('calend_settings').style.display = 'none';">
      </div>
      <?php echo $calendartext; ?>
 </div>
    
     
<div class="labelsscl">
   
 <div id="cal_contr">
  <table class="tab1cl"> 
   <tr><td colspan="3" class="cq32">Create an Event</td></tr>
   <tr class="cq31">
    <td class="cq30">Year</td>
    <td>Month</td>
    <td class="cq29">Day</td>
  </tr>
   
  <tr>
    <td>
      <select name="sy">
        <option value="<?php echo date("Y"); ?>" selected="selected">&nbsp;<?php echo date("Y"); ?></option>
        <option value="<?php echo date("Y")+1; ?>">&nbsp;<?php echo date("Y")+1; ?></option>
      </select>
    </td>
    <td>
      <select name="sm" >
       <option value="<?php echo date("m"); ?>">&nbsp;<?php echo date("F"); ?></option>
       <option value="01">&nbsp;<?php echo $calvalues[1]; ?></option>    <option value="02">&nbsp;<?php echo $calvalues[2]; ?></option>  
       <option value="03">&nbsp;<?php echo $calvalues[3]; ?></option>    <option value="04">&nbsp;<?php echo $calvalues[4]; ?></option>
       <option value="05">&nbsp;<?php echo $calvalues[5]; ?></option>    <option value="06">&nbsp;<?php echo $calvalues[6]; ?></option>
       <option value="07">&nbsp;<?php echo $calvalues[7]; ?></option>    <option value="08">&nbsp;<?php echo $calvalues[8]; ?></option>
       <option value="09">&nbsp;<?php echo $calvalues[9]; ?></option>    <option value="10">&nbsp;<?php echo $calvalues[10]; ?></option>
       <option value="11">&nbsp;<?php echo $calvalues[11]; ?></option>    <option value="12">&nbsp;<?php echo $calvalues[12]; ?></option>  
      </select>
    </td>
    <td>
    <select name="sd" >
    <option value="0">&nbsp;</option>
    <option value="01">&nbsp;01</option>    <option value="02">&nbsp;02</option>      <option value="03">&nbsp;03</option>
    <option value="04">&nbsp;04</option>    <option value="05">&nbsp;05</option>    <option value="06">&nbsp;06</option>
    <option value="07">&nbsp;07</option>    <option value="08">&nbsp;08</option>    <option value="09">&nbsp;09</option>
    <option value="10">&nbsp;10</option>    <option value="11">&nbsp;11</option>    <option value="12">&nbsp;12</option>
    <option value="13">&nbsp;13</option>    <option value="14">&nbsp;14</option>    <option value="15">&nbsp;15</option>
    <option value="16">&nbsp;16</option>    <option value="17">&nbsp;17</option>    <option value="18">&nbsp;18</option>
    <option value="19">&nbsp;19</option>    <option value="20">&nbsp;20</option>    <option value="21">&nbsp;21</option>
    <option value="22">&nbsp;22</option>    <option value="23">&nbsp;23</option>    <option value="24">&nbsp;24</option>
    <option value="25">&nbsp;25</option>    <option value="26">&nbsp;26</option>     <option value="27">&nbsp;27</option> <option value="28">&nbsp;28</option>     <option value="29">&nbsp;29</option>     <option value="30">&nbsp;30</option>    <option value="31">&nbsp;31</option> 
    </select></td>
  </tr>
  
  <tr><td colspan="3" class="cq28">
      <input type="checkbox" id="evvt" onchange="caddev();">&nbsp;Block out the day in your calendar.<br>
      <input name="evo" type="hidden" value=""  />
      <div class="cq26">Event: 
	 <input name="ev" type="text" class="cq27" value="" />
      </div><br>
      <div id="timeline" class="cq26">
	 Start Time:
	 <select name="stf" id="stf" class="cq25">
            <option value="">&nbsp;</option><?php echo $time_out; ?>
	 </select>
	 <br><br>
	 End Time:&nbsp;
	 <select name="stt" id="stt" class="cq25">
               <option value="">&nbsp;</option><?php echo $time_out; ?>
	 </select> (included)
      </div>
  </td></tr>
  
  <tr class="cq24">
    <td colspan="3" class="cq23">
      <input type="button" class="but" value=" &nbsp; Create event &nbsp; " onClick="cadd()" />
    </td>
   </tr>
  </table>
 </div>



<div id="editev">
 <table class="tbl2cl">
   <tr><td class="cq22">Events <input name="indf" type="hidden" value="<?php echo( $indf );?>"></td></tr>
   <tr class="cq21"><td class="cq20"> 
       &nbsp; Year &nbsp;
       <select name="yearr" class="cq19" onchange="copennew();">
         <option value="<?php echo $nowyear; ?>"><?php echo $nowyear; ?></option>
	 <option value="<?php $mb = intval(date("Y"))-1; echo $mb; ?>"><?php echo $mb; ?></option>
	 <option value="<?php echo date("Y"); ?>"><?php echo date("Y"); ?></option>
	 <option value="<?php $mba = intval(date("Y")+1); echo $mba; ?>"><?php echo $mba; ?></option>
       </select> 
       Month
       <select name="monthh" class="cq18" onchange="copennew();">
         <option value="<?php echo $nowmonth; ?>">&nbsp;<?php echo $nowmonthname; ?></option>
         <option value="01">&nbsp;<?php echo $calvalues[1]; ?></option>    <option value="02">&nbsp;<?php echo $calvalues[2]; ?></option>  
         <option value="03">&nbsp;<?php echo $calvalues[3]; ?></option>    <option value="04">&nbsp;<?php echo $calvalues[4]; ?></option>
         <option value="05">&nbsp;<?php echo $calvalues[5]; ?></option>    <option value="06">&nbsp;<?php echo $calvalues[6]; ?></option>
         <option value="07">&nbsp;<?php echo $calvalues[7]; ?></option>    <option value="08">&nbsp;<?php echo $calvalues[8]; ?></option>
         <option value="09">&nbsp;<?php echo $calvalues[9]; ?></option>    <option value="10">&nbsp;<?php echo $calvalues[10]; ?></option>
         <option value="11">&nbsp;<?php echo $calvalues[11]; ?></option>    <option value="12">&nbsp;<?php echo $calvalues[12]; ?></option> 
       </select> 
        <input name="monthname" type="hidden" value="<?php echo $nowmonthname; ?>" />
       </td>
   </tr>
 </table>

 
 
 
 <div id="cal_data">
  <table class="tbl3cl">
   <tr class="cq11">
    <td class="cq12">Delete <input name="delets" type="hidden" value=""></td>
    <td class="cq13">Year</td>
    <td class="cq14">Month</td>
    <td class="cq15">Day</td>
    <td class="cq16">Time</td>
    <td class="cq17">Event</td>
   </tr>
  
<?php    
    $text2='';
    $shx = 0; // total events
    $color = "F0F0F0";
    
    
   for ($day=1; $day <=31; $day++) {
    for ($i=0; $i < $indf; $i++) {
      if($day==$CSVday[$i]){
	 
	if($i!=0 && $CSVyear[$i]==$CSVyear[$i-1]
	         && $CSVmonth[$i]==$CSVmonth[$i-1]
	         && $CSVday[$i]==$CSVday[$i-1]
	         && $CSValert[$i]==$CSValert[$i-1]){ // same event
	    
	    $text2.='<tr><td style="background:#'.$color.';"><input type="checkbox" value="'.$CSVid[$i].'" name="f'.$i.'" id="f'.$i.'"><input type="hidden" id="ft'.$i.'" name="ft'.$i.'" value="'.$CSVid[$i].'" /></td>';
	    $text2.='<td><input type="hidden" id="at'.$i.'" name="at'.$i.'" value="'.$CSVyear[$i].'" /></td>';
	    $text2.='<td><input type="hidden" id="bt'.$i.'" name="bt'.$i.'" value="'.$CSVmonth[$i].'" /></td>';
	    $text2.='<td><input type="hidden" id="ct'.$i.'" name="ct'.$i.'" value="'.$CSVday[$i].'" /></td>';
	    $text2.='<td><input type="text" id="dt'.$i.'" name="dt'.$i.'" value="'.$CSVtime[$i].'" /></td>';
	    $text2.='<td style="background:transparent;border:0px;"><input type="text" id="et'.$i.'" name="et'.$i.'" value="'.$CSValert[$i].'" style="color:#cccccc;border:0px;" /></td></tr>';

	}else{ // new event
	    $color = dechex(hexdec($color) - hexdec("FF9"));
	    $text2.='<tr><td style="background:#'.$color.';"><input type="checkbox" value="'.$CSVid[$i].'" name="f'.$i.'" id="f'.$i.'"><input type="hidden" id="ft'.$i.'" name="ft'.$i.'" value="'.$CSVid[$i].'" /></td>';
	    $text2.='<td style="background:#'.$color.';"><input type="text" id="at'.$i.'" name="at'.$i.'" value="'.$CSVyear[$i].'" /></td>';
	    $text2.='<td style="background:#'.$color.';"><input type="text" id="bt'.$i.'" name="bt'.$i.'" value="'.$CSVmonth[$i].'" /></td>';
	    $text2.='<td style="background:#'.$color.';"><input type="text" id="ct'.$i.'" name="ct'.$i.'" value="'.$CSVday[$i].'" /></td>';
	    $text2.='<td style="background:#'.$color.';"><input type="text" id="dt'.$i.'" name="dt'.$i.'" value="'.$CSVtime[$i].'" /></td>';
	    $text2.='<td style="background:#'.$color.';"><input type="text" id="et'.$i.'" name="et'.$i.'" value="'.$CSValert[$i].'" /></td></tr>';
	    $shx ++;
	        
	}  
      }
    }
    
   }
  echo $text2;
?>
  <tr><td colspan="6" class="cq10">&nbsp;Total events: <input name="indf2" type="text" class="cq9" value="<?php echo $shx; ?>"></td></tr>
  <tr><td colspan="6" class="cq8">
	<input type="button" class="but" value="&nbsp; Save events &nbsp;" onClick="csave()"> &nbsp; &nbsp;
	<input type="button" class="but" value="&nbsp; Delete event &nbsp;" onClick="cdelet()"> 
	<input name="savevents" type="hidden" value="" />
	<input name="line" type="hidden" value="" />
    </td>
  </tr>
  </table>
  </div>
 </div>
	
	
</div> 




<div id="runfuncl" class="labelsscl">
 <table id="fc" class="tbl4cl">
   <tr class="cq4" onselectstart="return false">
      <td class="cq5" onclick="upmonth(-1)">&laquo; </td>
      <td colspan="5" id="mns" class="cq6"></td>
      <td class="cq7" onclick="upmonth(1)"> &raquo;</td>
   </tr>
   <tr class="cq3">
      <td class="cq1"><?php echo $calvalues[13]; ?></td>
      <td class="cq1"><?php echo $calvalues[14]; ?></td>
      <td class="cq1"><?php echo $calvalues[15]; ?></td>
      <td class="cq1"><?php echo $calvalues[16]; ?></td>
      <td class="cq1"><?php echo $calvalues[17]; ?></td>
      <td class="cq1"><?php echo $calvalues[18]; ?></td>
      <td class="cq1"><?php echo $calvalues[19]; ?></td>
   </tr>
   <?php 
     for($kk=1;$kk<=6;$kk++) {
	echo '<tr>';
	for($tt=1;$tt<=7;$tt++) {
		$num = 7 * ($kk-1) - (-$tt);
		echo '<td id="v'. $num .'" class= "cq2">&nbsp;</td>';
	}
	echo '</tr>';
      }
   ?>
 </table>
 <?php add_action( 'admin_print_footer_scripts',function() {global $js_values; echo $js_values;}); ?>
</div>