<style>

</style>
 
 <div id="myDatediv" style="" hidden >
  <div id="myDatedivheader">ΕΠΙΛΟΓΗ ΠΕΡΙΟΔΟΥ
     <span onclick="document.getElementById('myDatediv').style.display='none'" class="close" >&#x2716;</span>
  </div>
    
	<div  class="vsBoxNew"  style="margin:20px 20px 20px 20px;padding:10px 5px 10px 5px ; ">
 
		<div class="row">
			<div class="col-10"><label for="HmApo">Από</label></div>
			<div class="col-40">
					<div class="vscombo-div">
					  <input class="datepicker" type="text" id="HmApo" name="HmApo" value="<?php echo $HmApo;?>" >
					  <a class="vscombo_date_button" name="Hmnia_Button" id="Hmnia_Button" ></a></input>
					</div>  
			</div>
			
			<div class="col-10"><label for="HmEos">Έως</label></div>
			<div class="col-40">
					<div class="vscombo-div">
					  <input class="datepicker" type="text" id="HmEos" name="HmEos" value="<?php echo $HmEos;?>" >
					  <a class="vscombo_date_button" name="Hmnia_Button" id="Hmnia_Button" ></a></input>
					</div>  
			</div>		
		
		</div>	
	</div>	
 
    <div style="padding: 10px 10px 20px 10px;">
		<div style="padding: 10px 0px 10px 0px;">
			<a href="javascript:cal_update('day');" class="button "> Σήμερα </a>
			<a href="javascript:cal_update('week');" class="button "> Τρέχουσα Εβδομάδα </a>
			<a href="javascript:cal_update('month');" class="button "> Τρέχων Μήνας </a>
			<a href="javascript:cal_update('year');" class="button "> Τρέχον Ετος </a>
		</div>	 
 
 		<a class='button' style='padding-right:4px;'>Εμφάνιση όλων των εγγραφών
			<label class='t2' >
			<input type="checkbox" id="<?php echo $show_all_fld;?>" name="<?php echo $show_all_fld;?>" <?php echo $show_all_value; ?> onclick="javascript:show_all(this);" /> 
			<span class='value'></span>
			</label>
		</a> 
	</div>
	  
	<div id="myDatedivfooter" style="background-color:#f1f1f1;">
	      <a href="javascript:HideDateDiv();" class="button red" style="float:right;margin-left:5px;"> Άκυρο </a>
	      &nbsp;&nbsp;
	     <a  id="ok_button"  class="button green" style=" float:right;width:180px;">  Εντάξει</a>
	</div>
 
 
</div>
 
<script language="JavaScript" type="text/JavaScript">


//if(document.getElementById("vs_calendar_tool_bar")!=null){
//	
//	var a = document.getElementById("vs_calendar_tool_bar").children;
//	for(var i = 0; i < a.length; i++) {
//		alert(a.id + " === " + a.tagName);
//		if(a[i].tagName == 'A' {
//			 a[i].onclick = function() { updDates(this);}  
//			}
//		}
//}
 
var m_prefix="", m_btn="", btn="", cmd="";
var c_HmApoName="", c_HmEosName="", HmApo, HmEos;
 
document.addEventListener('click', function(e) {
    e = e || window.event;
    var target = e.target || e.srcElement,
          text = target.textContent || target.innerText;

 if(e.target.id=="ok_button"){ ok_btn_pressed();return; }
 
var a = e.target.id;var b=a.split("@");
	if(b.length<=1){return;}
	
	if(b[0]!=""){
		m_prefix=b[0]; a=b[1]; btn=b[1]; a=a.split("_");
		//if(a.length<=1){return;}
		 cmd=a[1];
	}

	
m_btn=e.target.id;
m_prefix=m_prefix.concat("@");
c_HmApoName=m_prefix.concat("HmApo");
c_HmEosName=m_prefix.concat("HmEos");

HmApo =  getCookie(c_HmApoName) ; 
HmEos =  getCookie(c_HmEosName) ; 

m_period_type=getCookie(m_prefix.concat("period_type"));

 //  alert(e.target.id+ " ==== " + b[0] + " == " + b[1] + "   btn=" + btn + "   command: " + cmd);
 // alert("1... " + m_prefix+ " , " + btn + "  ,  " + m_period_type);
  
  if(btn==""){return;}
  if(btn=="FindWhat"){return;}
  if(btn=="cboRecPP"){return;}
  if(btn=="calendar_select" || btn=="HmSelect" ){ShowDateDiv();return;}  	  
  if(btn=="calendar_prv"){ cal_update('prv');return;}
  if(btn=="calendar_next"){ cal_update('next');return; }

 if(btn=="calendar_day"){ cal_update('day');return;}
 if(btn=="calendar_week"){ cal_update('week');return; }
 if(btn=="calendar_month"){ cal_update('month');return;}
 if(btn=="calendar_year"){ cal_update('year');return; }
  
  // updDates(cmd);
 		
}, false);


dragElement(document.getElementById("myDatediv"));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    /* if present, the header is where you move the DIV from:*/
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    /* otherwise, move the DIV from anywhere inside the DIV:*/
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}

// =============================================
//
// =============================================
function cal_update(cmd){
	// document.cookie = m_prefix+'period_type=' + cmd;updDates(cmd);
	updDates(cmd);
}

function updDates(cmd){
 
 
// alert("cal_update     m_prefix:" +   m_prefix+ " , btn:" + btn + "  ,  m_period_type:" + m_period_type + "  ,  cmd:" + cmd);
 
switch(cmd) {
	  case 'day':	day_change('day');  break;
	  case 'week':  week_change('week');  break;
	  case 'month': month_change('month');  break;	
	  case 'year':	year_change('year');  break;
	  case 'prv': 
	  case 'next':
					if (m_period_type=="day"){day_change(cmd);} else
					if (m_period_type=="week"){week_change(cmd);} else 
					if (m_period_type=="month"){month_change(cmd);} else 
					if (m_period_type=="year"){year_change(cmd);}
					break;
	  case 'select':
		//		document.cookie = c_HmApoName+'=' + HmApo
		//		document.cookie = c_HmEosName+'=' + HmEos;    	  
}	 
//document.cookie = m_prefix+'ShowAll=unchecked' ; 
document.location.reload(true);
}


function day_change(cmd){
var d, days_add=0 ;

/* alert("1. HmApo : "+HmApo  +  '\r\n' +
        "m_prefix 4 : " +   m_prefix+   '\r\n' +
		"btn:" + btn + '\r\n' +
		"m_period_type : " + m_period_type + '\r\n' +
		"cmd : " + cmd + '\r\n' +
		"HmApo : " + HmApo + '\r\n' +
		"d : " + d  + '\r\n'  );
		
	*/	
 
switch(cmd) {
 case 'day':
		    HmApo = new Date();
		    days_add=0;
		    break;
 case 'prv':
			HmApo=js_DateToParts(HmApo).yyyymmdd;  
			HmApo=new Date(HmApo);
			days_add=-1;
			break;
 case 'next':
			HmApo=js_DateToParts(HmEos).yyyymmdd;  
			HmApo=new Date(HmApo);
			days_add=1;
			break;
}
   
			 HmApo = new Date(Date.parse(HmApo) + days_add*24*60*60*1000 ) ;
			 HmApo = HmApo.toISOString().split('T')[0];		
			 HmApo = js_DateToParts(HmApo).ddmmyyyy;
		     HmEos = HmApo;
			
/* 			alert("4. HmApo : "+HmApo);
 			  alert("m_prefix 4 : " +   m_prefix+ 
					"btn:" + btn + '\r\n' +
					"m_period_type : " + m_period_type + '\r\n' +
					"cmd : " + cmd + '\r\n' +
					"HmApo : " + HmApo + '\r\n' +
					"d : " + d  
					);
*/  
  	 	document.cookie = m_prefix+'period_type=day' ;
		document.cookie = m_prefix+'ShowAll=unchecked' ;
	 	document.cookie = c_HmApoName+'=' + HmApo;
	 	document.cookie = c_HmEosName+'=' + HmEos;  
		document.getElementById("HmApo").value=HmApo;
		document.getElementById("HmEos").value=HmEos;
	
		document.location.reload(true); 
}

function incr_date(date_str,no_of_days_to_add){
 var futureDate=new Date(date_str);

  futureDate.setDate(new Date().getDate()+no_of_days_to_add);
  var next_date_str= futureDate;

    return next_date_str;
}

function week_change(cmd){
	var d, days_add=0, Hmeres;
 
switch(cmd) {
	 case 'week': 	d = new Date();	d = d.toISOString().split('T')[0]; days_add=0;	break;
	 case 'prv':	d=formatMyDate(HmApo,"GrToMySql",""); days_add=-7; break;
	 case 'next':	d=formatMyDate(HmEos,"GrToMySql","");	days_add= 1;break;
	 default:		d = new Date();	days_add=0;
	}
		      d = new Date(Date.parse(d) + days_add*24*60*60*1000 ) ;
			  d = d.toISOString().split('T')[0];
		 Hmeres = get_week(d);
 
  	 	document.cookie = m_prefix+'period_type=week';
		document.cookie = m_prefix+'ShowAll=unchecked';
	 	document.cookie = c_HmApoName+'=' + Hmeres.DeyteraGr;
	 	document.cookie = c_HmEosName+'=' + Hmeres.KyriakiGr;  
		document.getElementById("HmApo").value=Hmeres.DeyteraGr;
		document.getElementById("HmEos").value=Hmeres.KyriakiGr;	
//	alert("week_update :   "+c_HmApoName+" : " + Hmeres.DeyteraGr + ",  KyriakiGr : " + Hmeres.KyriakiGr);	
		document.location.reload(true); 
}

 
function month_change(cmd){
 
var d, Hmeres, Hmnia="", Minas, Etos;
var days_add=0;
 
switch(cmd) {
 case 'month':
		   d = new Date();	d = d.toISOString().split('T')[0];
		   Hmnia = js_DateToParts(d);
		   HmApo = "01/"+Hmnia.Minas+"/"+Hmnia.Etos;   
		   d = new Date(Hmnia.Etos+"-"+Hmnia.Minas+"-"+"01");
 		   HmEos = new Date(d.getFullYear(), d.getMonth() + 1, 1);
		       d = new Date(Date.parse(HmEos) + days_add*24*60*60*1000 ) ;
		   HmEos = d.toISOString().split('T')[0]; 
		   HmEos = js_DateToParts(HmEos).ddmmyyyy;
 		  break;
 case 'prv':		 
			       d = formatMyDate(HmApo,"GrToMySql",""); 
			var date = new Date(d), y = date.getFullYear(), m = date.getMonth()+1;
		       Minas = m-1; Etos=y; if(Minas<1){Minas=12;Etos=Etos-1;}
			   Minas=padTo2Digits(Minas);
			   HmApo = "01/"+ Minas +"/"+Etos; 
				   d = Etos+"-"+Minas+"-"+"01"; d = new Date(d);
			   HmEos = daysInMonth(d)+"/"+Minas+"/"+Etos;
			break;
 case 'next':
			
			d = formatMyDate(HmApo,"GrToMySql",""); 
			var date = new Date(d), y = date.getFullYear(), m = date.getMonth()+1;
		       Minas = m+1; Etos=y; if(Minas>12){Minas=1;Etos=Etos+1;}
			   Minas=padTo2Digits(Minas);
			   HmApo = "01/"+ Minas +"/"+Etos; 
				   d = Etos+"-"+Minas+"-"+"01"; d = new Date(d);
			   HmEos = daysInMonth(d)+"/"+Minas+"/"+Etos;
 
			break;
}			
// alert("HmApo : " + HmApo + "    HmEos : " + HmEos);
   	 	document.cookie = m_prefix+'period_type=month' ;
		document.cookie = m_prefix+'ShowAll=unchecked' ;
	 	document.cookie = c_HmApoName+'=' + HmApo;
	 	document.cookie = c_HmEosName+'=' + HmEos;
		document.getElementById("HmApo").value=HmApo;
		document.getElementById("HmEos").value=HmEos;	
		document.location.reload(true);  	
}

function year_change(cmd){
	var d, Hmnia, Etos, year_add=0;
	if(cmd=='prv'){year_add=-1;}else if(cmd=='next') {year_add=1;}
 
switch(cmd) {
	 case 'year': d = new Date();d = d.toISOString().split('T')[0];	Hmnia = js_DateToParts(d);
				  HmApo = "01/01/"+Hmnia.Etos; HmEos = "31/12/"+Hmnia.Etos;   
				  break;
	 case 'prv':
	 case 'next':
				d = formatMyDate(HmApo,"GrToMySql",""); d = new Date(d);
				Etos = d.getFullYear()+year_add; HmApo = "01/01/"+Etos; HmEos = "31/12/"+Etos; 
				break;
}
   	 	document.cookie = m_prefix+'period_type=year' ;
		document.cookie = m_prefix+'ShowAll=unchecked' ;
	 	document.cookie = c_HmApoName+'=' + HmApo;
	 	document.cookie = c_HmEosName+'=' + HmEos;
		document.getElementById("HmApo").value=HmApo;
		document.getElementById("HmEos").value=HmEos;	
		document.location.reload(true);  	
}	

function ok_btn_pressed(){
		HmApo=document.getElementById("HmApo").value;
		HmEos=document.getElementById("HmEos").value;
	 	document.cookie = c_HmApoName+'=' + HmApo;
	 	document.cookie = c_HmEosName+'=' + HmEos;
		document.getElementById("HmApo").value=HmApo;
		document.getElementById("HmEos").value=HmEos;	
		document.location.reload(true);	
}

function getMonday(d) {
  d = new Date(d); var day = d.getDay(), diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
  return new Date(d.setDate(diff));
}

function getSunday(d) {
  var t = new Date(d); t.setDate(t.getDate() + 6); return  t ;
}

function dateToFormat(d, format){
 
 var dd = String(d.getDate()).padStart(2, '0');
 var mm = String(d.getMonth() + 1).padStart(2, '0'); //January is 0!
 var yyyy = d.getFullYear();
	
	switch(format) {
	   case 'dd/mm/yyyy':
               	    d=dd + '/' + mm + '/' + yyyy;
					break;
	   case 'mm/dd/yyyy':
					d=mm + '/' + dd + '/' + yyyy; 
					break;
	   case 'yyyy/mm/dd': 
					d=yyyy + '/' + mm + '/' + dd; 
					break;
	   default:	      	  
					d=dd + '/' + mm + '/' + yyyy;
	}
	
return d;
}
 
function get_week(HmApo) {
	
 var curr = new Date(HmApo);
 var week = [], first=0, day=0;
 
  var firstDay=new Date(curr.setDate(curr.getDate() - curr.getDay()+1));
   var lastDay=new Date(curr.setDate(curr.getDate() - curr.getDay() + 7));
      firstDay=firstDay.toISOString().split('T')[0];
       lastDay=lastDay.toISOString().split('T')[0];
 
//for (var i = 1; i <= 7; i++) {
//    first = curr.getDate() - curr.getDay() + i ;
//	day = new Date(curr.setDate(first)).toISOString().slice(0, 10);
//  week.push(day); }
 
//  alert("......HmApo= "+HmApo +" , " +firstDay + "     ,    "+ lastDay + "    1");
 
var fDay = firstDay.split("-");
var lDay = lastDay.split("-");

// month is 0-based, that's why we need dataParts[1] - 1
var en1 = fDay[2]+"/"+ fDay[1]+"/"+fDay[0] ;
var en6 = lDay[2]+"/"+ lDay[1]+"/"+lDay[0] ;
 
return {
        Deytera: firstDay,
        Kyriaki: lastDay,
		DeyteraGr: en1,
		KyriakiGr: en6
    };
	
}
 
function addDays(date, days) {
  date.setDate(date.getDate() + days);
return date;  
}


function formatMyDate(mydate, fromTo , def_value  ) {

var valForExit="", d_array, d;
 
	if(fromTo=="GrToMySql"){
		d_array = mydate.split("/");d = d_array[2]+"-"+d_array[1]+"-"+d_array[0];return d;
	}	
	
	if(fromTo=="MySqlToGr"){
		 d_array = mydate.split("-"); d = d_array[2]+"-"+d_array[1]+"-"+d_array[0];return d;
	}
	
	if(fromTo=="MyJSToGr"){
		  var dd = String(mydate.getDate()).padStart(2, '0');
	  	  var mm = String(mydate.getMonth() + 1).padStart(2, '0'); //January is 0!
		  var yyyy = mydate.getFullYear();
		  wd1 = dd + '/' + mm + '/' + yyyy; 
		return wd1;
	}
 	
}

function is_numeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function js_DateToParts(d){
 var d_array, dd, mm, yyyy;
 
 // alert("100 ======> " + d);
 
	if(d.indexOf("/")>=0){
		d_array = d.split("/"); d = d_array[2]+"-"+d_array[1]+"-"+d_array[0] ;
		dd=d_array[0];
		mm=d_array[1];
		yyyy=d_array[2];
	 } else	{
		//		alert("2. ======> " + d );
		   d = new Date(d);
		//		alert("3. ======> " + d );
				
		  dd = String(d.getDate()).padStart(2, '0');
		  mm = String(d.getMonth() + 1).padStart(2, '0'); //January is 0!
		yyyy = d.getFullYear();	 
	 }
 
 // alert("d22221 : " + d  );
 
return {
        Hmera :dd,
        Minas : mm,
		Etos : yyyy,
		ddmmyyyy: dd + '/' + mm + '/' + yyyy,
		mmddyyyy: mm + '-' + dd + '-' + yyyy,
		yyyymmdd: yyyy + '-' + mm + '-' + dd 
    };
}


	
function daysInMonth (d) {
    return new Date(d.getFullYear(), d.getMonth()+1, 0).getDate(); 
}

function leftPad(number, targetLength) {
    var output = number + '';
    while (output.length < targetLength) {
        output = '0' + output;
    }
    return output;
}
function ShowDateDiv(){
  document.getElementById('myDatediv').style.display='block' ;
  document.getElementById('HmApo').focus();
}

function HideDateDiv(){
  document.getElementById('myDatediv').style.display='none'
}

function eraseCookie(c_name) {
  createCookie(cookie_name,"",-1);
}	 
	
function isValidDate(dateString){
	
    // First check for the pattern
    if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
        return false;

    // Parse the date parts to integers
    var parts = dateString.split("/");
    var day = parseInt(parts[1], 10);
    var month = parseInt(parts[0], 10);
    var year = parseInt(parts[2], 10);

    // Check the ranges of month and year
    if(year < 1000 || year > 3000 || month == 0 || month > 12)
        return false;

    var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    // Adjust for leap years
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
        monthLength[1] = 29;

    // Check the range of the day
    return day > 0 && day <= monthLength[month - 1];
} 

function date_format_sql(date = new Date()) {
  return [
    date.getFullYear(),
    padTo2Digits(date.getMonth() + 1),
    padTo2Digits(date.getDate()),
  ].join('-');
}

function date_format_gr(date = new Date()){

return [ 
	padTo2Digits(date.getDate()),
	padTo2Digits(date.getMonth() + 1),
	date.getFullYear(), 
	].join('/');	
	
}

function padTo2Digits(num) { 
  return num.toString().padStart(2, '0');
}
	
</script>
